1af2cfd7bSChristopher Smith<?php 2af2cfd7bSChristopher Smith/** 3af2cfd7bSChristopher Smith * Unit Test for inc/common.php - pageinfo() 4af2cfd7bSChristopher Smith * 5af2cfd7bSChristopher Smith * @author Christopher Smith <chris@jalakai.co.uk> 6af2cfd7bSChristopher Smith */ 7af2cfd7bSChristopher Smithclass common_pageinfo_test extends DokuWikiTest { 8af2cfd7bSChristopher Smith 9*1c33cec3SAndreas Gohr function setup() : void { 10af2cfd7bSChristopher Smith parent::setup(); 11af2cfd7bSChristopher Smith 12af2cfd7bSChristopher Smith global $USERINFO; 13af2cfd7bSChristopher Smith $USERINFO = array( 14af2cfd7bSChristopher Smith 'pass' => '179ad45c6ce2cb97cf1029e212046e81', 15af2cfd7bSChristopher Smith 'name' => 'Arthur Dent', 16af2cfd7bSChristopher Smith 'mail' => 'arthur@example.com', 17af2cfd7bSChristopher Smith 'grps' => array ('admin','user'), 18af2cfd7bSChristopher Smith ); 19af2cfd7bSChristopher Smith $_SERVER['REMOTE_USER'] = 'testuser'; 20af2cfd7bSChristopher Smith $_SERVER['REMOTE_ADDR'] = '1.2.3.4'; 21af2cfd7bSChristopher Smith } 22af2cfd7bSChristopher Smith 23af2cfd7bSChristopher Smith function _get_expected_pageinfo() { 24af2cfd7bSChristopher Smith global $USERINFO; 25af2cfd7bSChristopher Smith $info = array ( 26af2cfd7bSChristopher Smith 'isadmin' => true, 27af2cfd7bSChristopher Smith 'ismanager' => true, 28af2cfd7bSChristopher Smith 'userinfo' => $USERINFO, 29af2cfd7bSChristopher Smith 'perm' => 255, 30af2cfd7bSChristopher Smith 'namespace' => false, 31af2cfd7bSChristopher Smith 'ismobile' => false, 32af2cfd7bSChristopher Smith 'client' => 'testuser', 33af2cfd7bSChristopher Smith ); 34af2cfd7bSChristopher Smith $info['rev'] = null; 35af2cfd7bSChristopher Smith $info['subscribed'] = false; 36af2cfd7bSChristopher Smith $info['locked'] = false; 37af2cfd7bSChristopher Smith $info['exists'] = false; 38af2cfd7bSChristopher Smith $info['writable'] = true; 39af2cfd7bSChristopher Smith $info['editable'] = true; 40af2cfd7bSChristopher Smith $info['lastmod'] = false; 415d873dd4SAndreas Gohr $info['currentrev'] = false; 42af2cfd7bSChristopher Smith $info['meta'] = array(); 43af2cfd7bSChristopher Smith $info['ip'] = null; 44af2cfd7bSChristopher Smith $info['user'] = null; 45af2cfd7bSChristopher Smith $info['sum'] = null; 46af2cfd7bSChristopher Smith $info['editor'] = null; 47af2cfd7bSChristopher Smith 48af2cfd7bSChristopher Smith return $info; 49af2cfd7bSChristopher Smith } 50af2cfd7bSChristopher Smith 51af2cfd7bSChristopher Smith /** 52af2cfd7bSChristopher Smith * check info keys and values for a non-existent page & admin user 53af2cfd7bSChristopher Smith */ 54af2cfd7bSChristopher Smith function test_basic_nonexistentpage(){ 55af2cfd7bSChristopher Smith global $ID,$conf; 56af2cfd7bSChristopher Smith $ID = 'wiki:start'; 57af2cfd7bSChristopher Smith 58af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 59af2cfd7bSChristopher Smith $info['id'] = 'wiki:start'; 60af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 61af2cfd7bSChristopher Smith $info['filepath'] = $conf['datadir'].'/wiki/start.txt'; 62af2cfd7bSChristopher Smith 63af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 64af2cfd7bSChristopher Smith } 65af2cfd7bSChristopher Smith 66af2cfd7bSChristopher Smith /** 67af2cfd7bSChristopher Smith * check info keys and values for a existing page & admin user 68af2cfd7bSChristopher Smith */ 69af2cfd7bSChristopher Smith function test_basic_existingpage(){ 70af2cfd7bSChristopher Smith global $ID,$conf; 71af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 72af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 73af2cfd7bSChristopher Smith $rev = filemtime($filename); 74af2cfd7bSChristopher Smith 75af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 76af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 77af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 78af2cfd7bSChristopher Smith $info['filepath'] = $filename; 79af2cfd7bSChristopher Smith $info['exists'] = true; 80af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 815d873dd4SAndreas Gohr $info['currentrev'] = $rev; 82af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 83af2cfd7bSChristopher Smith 84af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 85af2cfd7bSChristopher Smith } 86af2cfd7bSChristopher Smith 87af2cfd7bSChristopher Smith /** 88af2cfd7bSChristopher Smith * check info keys and values for anonymous user 89af2cfd7bSChristopher Smith */ 90af2cfd7bSChristopher Smith function test_anonymoususer(){ 91af2cfd7bSChristopher Smith global $ID,$conf,$REV; 92af2cfd7bSChristopher Smith 93af2cfd7bSChristopher Smith unset($_SERVER['REMOTE_USER']); 94af2cfd7bSChristopher Smith global $USERINFO; $USERINFO = array(); 95af2cfd7bSChristopher Smith 96af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 97af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 98af2cfd7bSChristopher Smith $rev = filemtime($filename); 99af2cfd7bSChristopher Smith 100af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 101af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 102af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 103af2cfd7bSChristopher Smith $info['filepath'] = $filename; 104af2cfd7bSChristopher Smith $info['exists'] = true; 105af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 1065d873dd4SAndreas Gohr $info['currentrev'] = $rev; 107af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 108af2cfd7bSChristopher Smith $info['rev'] = ''; 109af2cfd7bSChristopher Smith 110af2cfd7bSChristopher Smith $info = array_merge($info, array( 111af2cfd7bSChristopher Smith 'isadmin' => false, 112af2cfd7bSChristopher Smith 'ismanager' => false, 113af2cfd7bSChristopher Smith 'perm' => 8, 114af2cfd7bSChristopher Smith 'client' => '1.2.3.4', 115af2cfd7bSChristopher Smith )); 116af2cfd7bSChristopher Smith unset($info['userinfo']); 117af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 118af2cfd7bSChristopher Smith } 119af2cfd7bSChristopher Smith 120af2cfd7bSChristopher Smith /** 121af2cfd7bSChristopher Smith * check info keys and values with $REV 122af2cfd7bSChristopher Smith * (also see $RANGE tests) 123af2cfd7bSChristopher Smith */ 124af2cfd7bSChristopher Smith function test_rev(){ 125af2cfd7bSChristopher Smith global $ID,$conf,$REV; 126af2cfd7bSChristopher Smith 127af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 128af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 129af2cfd7bSChristopher Smith $rev = filemtime($filename); 130af2cfd7bSChristopher Smith $REV = $rev - 100; 131b6cc2b69SGerrit Uitslag $ext = '.txt'; 132b6cc2b69SGerrit Uitslag if($conf['compression']) { //compression in $info['filepath'] determined by wikiFN depends also on if the page exist 133b6cc2b69SGerrit Uitslag $ext .= "." . $conf['compression']; //.gz or .bz2 134b6cc2b69SGerrit Uitslag } 135af2cfd7bSChristopher Smith 136af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 137af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 138af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 139af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 140af2cfd7bSChristopher Smith $info['rev'] = $REV; 1415d873dd4SAndreas Gohr $info['currentrev'] = $rev; 142b6cc2b69SGerrit Uitslag $info['filepath'] = str_replace('pages','attic',substr($filename,0,-3).$REV.$ext); 143af2cfd7bSChristopher Smith 144af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 145af2cfd7bSChristopher Smith $this->assertEquals($rev-100, $REV); 146af2cfd7bSChristopher Smith } 147af2cfd7bSChristopher Smith 148af2cfd7bSChristopher Smith /** 149af2cfd7bSChristopher Smith * check info keys and values with $RANGE 150af2cfd7bSChristopher Smith */ 151af2cfd7bSChristopher Smith function test_range(){ 152af2cfd7bSChristopher Smith global $ID,$conf,$REV,$RANGE; 153af2cfd7bSChristopher Smith 154af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 155af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 156af2cfd7bSChristopher Smith $rev = filemtime($filename); 157af2cfd7bSChristopher Smith $range = '1000-2000'; 158af2cfd7bSChristopher Smith 159af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 160af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 161af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 162af2cfd7bSChristopher Smith $info['exists'] = true; 163af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 1645d873dd4SAndreas Gohr $info['currentrev'] = $rev; 165af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 166af2cfd7bSChristopher Smith $info['filepath'] = $filename; 167af2cfd7bSChristopher Smith 168af2cfd7bSChristopher Smith // check $RANGE without $REV 169af2cfd7bSChristopher Smith // expected result $RANGE unchanged 170af2cfd7bSChristopher Smith $RANGE = $range; 171af2cfd7bSChristopher Smith 172af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 173af2cfd7bSChristopher Smith $this->assertFalse(isset($REV)); 174af2cfd7bSChristopher Smith $this->assertEquals($range,$RANGE); 175af2cfd7bSChristopher Smith 176af2cfd7bSChristopher Smith // check $RANGE with $REV = current 177af2cfd7bSChristopher Smith // expected result: $RANGE unchanged, $REV cleared 178af2cfd7bSChristopher Smith $REV = $rev; 179af2cfd7bSChristopher Smith $info['rev'] = ''; 180af2cfd7bSChristopher Smith 181af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 182af2cfd7bSChristopher Smith $this->assertEquals('',$REV); 183af2cfd7bSChristopher Smith $this->assertEquals($range,$RANGE); 184af2cfd7bSChristopher Smith 185af2cfd7bSChristopher Smith // check with a real $REV 186af2cfd7bSChristopher Smith // expected result: $REV and $RANGE are cleared 187af2cfd7bSChristopher Smith $REV = $rev - 100; 188af2cfd7bSChristopher Smith 189af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 190af2cfd7bSChristopher Smith $this->assertEquals('',$REV); 191af2cfd7bSChristopher Smith $this->assertEquals('',$RANGE); 192af2cfd7bSChristopher Smith } 193af2cfd7bSChristopher Smith 194af2cfd7bSChristopher Smith /** 195af2cfd7bSChristopher Smith * test editor entry and external edit 196af2cfd7bSChristopher Smith */ 197af2cfd7bSChristopher Smith function test_editor_and_externaledits(){ 198af2cfd7bSChristopher Smith global $ID,$conf; 199af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 200af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 201af2cfd7bSChristopher Smith $rev = filemtime($filename); 202af2cfd7bSChristopher Smith 203af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 204af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 205af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 206af2cfd7bSChristopher Smith $info['filepath'] = $filename; 207af2cfd7bSChristopher Smith $info['exists'] = true; 208af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 2095d873dd4SAndreas Gohr $info['currentrev'] = $rev; 210af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); // need $INFO set correctly for addLogEntry() 211af2cfd7bSChristopher Smith 212af2cfd7bSChristopher Smith global $INFO; 213af2cfd7bSChristopher Smith $INFO = $info; 214af2cfd7bSChristopher Smith 215af2cfd7bSChristopher Smith // add an editor for the current version of $ID 216af2cfd7bSChristopher Smith addLogEntry($rev, $ID); 217af2cfd7bSChristopher Smith 218af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 219af2cfd7bSChristopher Smith $info['editor'] = $_SERVER['REMOTE_USER']; 220af2cfd7bSChristopher Smith $info['user'] = $_SERVER['REMOTE_USER']; 221af2cfd7bSChristopher Smith $info['ip'] = $_SERVER['REMOTE_ADDR']; 222af2cfd7bSChristopher Smith $info['sum'] = ''; 223af2cfd7bSChristopher Smith 224af2cfd7bSChristopher Smith // with an editor ... 225af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 226af2cfd7bSChristopher Smith 227af2cfd7bSChristopher Smith // clear the meta['last_change'] value, pageinfo should restore it 228af2cfd7bSChristopher Smith p_set_metadata($ID,array('last_change' => false)); 229af2cfd7bSChristopher Smith 230af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 231af2cfd7bSChristopher Smith $this->assertEquals($info['meta']['last_change'], p_get_metadata($ID,'last_change')); 232af2cfd7bSChristopher Smith 233af2cfd7bSChristopher Smith // fake an external edit, pageinfo should clear the last change from meta data 234af2cfd7bSChristopher Smith // and not return any editor data 235af2cfd7bSChristopher Smith $now = time()+10; 236af2cfd7bSChristopher Smith touch($filename,$now); 237af2cfd7bSChristopher Smith 238af2cfd7bSChristopher Smith $info['lastmod'] = $now; 2395d873dd4SAndreas Gohr $info['currentrev'] = $now; 240af2cfd7bSChristopher Smith $info['meta']['last_change'] = false; 241af2cfd7bSChristopher Smith $info['ip'] = null; 242af2cfd7bSChristopher Smith $info['user'] = null; 243af2cfd7bSChristopher Smith $info['sum'] = null; 244af2cfd7bSChristopher Smith $info['editor'] = null; 245af2cfd7bSChristopher Smith 246af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 247af2cfd7bSChristopher Smith $this->assertEquals($info['meta'], p_get_metadata($ID)); // check metadata has been updated correctly 248af2cfd7bSChristopher Smith } 249af2cfd7bSChristopher Smith 250af2cfd7bSChristopher Smith /** 251af2cfd7bSChristopher Smith * check draft 252af2cfd7bSChristopher Smith */ 253af2cfd7bSChristopher Smith function test_draft(){ 254af2cfd7bSChristopher Smith global $ID,$conf; 255af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 256af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 257af2cfd7bSChristopher Smith $rev = filemtime($filename); 258af2cfd7bSChristopher Smith 259af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 260af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 261af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 262af2cfd7bSChristopher Smith $info['filepath'] = $filename; 263af2cfd7bSChristopher Smith $info['exists'] = true; 264af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 2655d873dd4SAndreas Gohr $info['currentrev'] = $rev; 266af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 267af2cfd7bSChristopher Smith 268af2cfd7bSChristopher Smith // setup a draft, make it more recent than the current page 269af2cfd7bSChristopher Smith // - pageinfo should recognise it and keep it 270af2cfd7bSChristopher Smith $draft = getCacheName($info['client'].$ID,'.draft'); 271af2cfd7bSChristopher Smith touch($draft,$rev + 10); 272af2cfd7bSChristopher Smith 273af2cfd7bSChristopher Smith $info['draft'] = $draft; 274af2cfd7bSChristopher Smith 275af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 276af2cfd7bSChristopher Smith $this->assertFileExists($draft); 277af2cfd7bSChristopher Smith 278af2cfd7bSChristopher Smith // make the draft older than the current page 279af2cfd7bSChristopher Smith // - pageinfo should remove it and not return the 'draft' key 280af2cfd7bSChristopher Smith touch($draft,$rev - 10); 281af2cfd7bSChristopher Smith unset($info['draft']); 282af2cfd7bSChristopher Smith 283af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 284af2cfd7bSChristopher Smith $this->assertFalse(file_exists($draft)); 285af2cfd7bSChristopher Smith } 286af2cfd7bSChristopher Smith 287af2cfd7bSChristopher Smith /** 288af2cfd7bSChristopher Smith * check ismobile 289af2cfd7bSChristopher Smith */ 290af2cfd7bSChristopher Smith function test_ismobile(){ 291af2cfd7bSChristopher Smith global $ID,$conf; 292af2cfd7bSChristopher Smith $ID = 'wiki:start'; 293af2cfd7bSChristopher Smith 294af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 295af2cfd7bSChristopher Smith $info['id'] = 'wiki:start'; 296af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 297af2cfd7bSChristopher Smith $info['filepath'] = $conf['datadir'].'/wiki/start.txt'; 298af2cfd7bSChristopher Smith 299af2cfd7bSChristopher Smith // overkill, ripped from clientismobile() as we aren't testing detection - but forcing it 300af2cfd7bSChristopher Smith $_SERVER['HTTP_X_WAP_PROFILE'] = 'a fake url'; 301af2cfd7bSChristopher Smith $_SERVER['HTTP_ACCEPT'] .= ';wap'; 302af2cfd7bSChristopher Smith $_SERVER['HTTP_USER_AGENT'] = 'blackberry,symbian,hand,mobi,phone'; 303af2cfd7bSChristopher Smith 304af2cfd7bSChristopher Smith $info['ismobile'] = clientismobile(); 305af2cfd7bSChristopher Smith 306af2cfd7bSChristopher Smith $this->assertTrue(clientismobile()); // ensure THIS test fails if clientismobile() returns false 307af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); // it would be a test failure not a pageinfo failure. 308af2cfd7bSChristopher Smith } 309af2cfd7bSChristopher Smith} 310af2cfd7bSChristopher Smith 311af2cfd7bSChristopher Smith//Setup VIM: ex: et ts=4 : 312