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 91c33cec3SAndreas 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 757866d571SSatoshi Sahara // run once to prepare meta/wiki/syntax.change file for existing page 767866d571SSatoshi Sahara // because pageinfo() set $info['meta']['last_change'] entry 777866d571SSatoshi Sahara pageinfo(); 787866d571SSatoshi Sahara 79af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 80af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 81af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 82af2cfd7bSChristopher Smith $info['filepath'] = $filename; 83af2cfd7bSChristopher Smith $info['exists'] = true; 84af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 855d873dd4SAndreas Gohr $info['currentrev'] = $rev; 86af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 877866d571SSatoshi Sahara // set from revinfo, $pagelog->getRevisionInfo($info['lastmod']) 887866d571SSatoshi Sahara $info = array_merge($info, array( 897866d571SSatoshi Sahara 'ip' => '127.0.0.1', 907866d571SSatoshi Sahara 'user' => '', 917866d571SSatoshi Sahara 'sum' => 'created - external edit', 927866d571SSatoshi Sahara )); 937866d571SSatoshi Sahara $info['editor'] = '127.0.0.1'; 94af2cfd7bSChristopher Smith 95af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 96af2cfd7bSChristopher Smith } 97af2cfd7bSChristopher Smith 98af2cfd7bSChristopher Smith /** 99af2cfd7bSChristopher Smith * check info keys and values for anonymous user 100af2cfd7bSChristopher Smith */ 101af2cfd7bSChristopher Smith function test_anonymoususer() { 102af2cfd7bSChristopher Smith global $ID,$conf,$REV; 103af2cfd7bSChristopher Smith 104af2cfd7bSChristopher Smith unset($_SERVER['REMOTE_USER']); 105af2cfd7bSChristopher Smith global $USERINFO; $USERINFO = array(); 106af2cfd7bSChristopher Smith 107af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 108af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 109af2cfd7bSChristopher Smith $rev = filemtime($filename); 110af2cfd7bSChristopher Smith 111af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 112af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 113af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 114af2cfd7bSChristopher Smith $info['filepath'] = $filename; 115af2cfd7bSChristopher Smith $info['exists'] = true; 116af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 1175d873dd4SAndreas Gohr $info['currentrev'] = $rev; 118af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 119af2cfd7bSChristopher Smith $info['rev'] = ''; 1207866d571SSatoshi Sahara // set from revinfo, $pagelog->getRevisionInfo($info['lastmod']) 1217866d571SSatoshi Sahara $info = array_merge($info, array( 1227866d571SSatoshi Sahara 'ip' => '127.0.0.1', 1237866d571SSatoshi Sahara 'user' => '', 1247866d571SSatoshi Sahara 'sum' => 'created - external edit', 1257866d571SSatoshi Sahara )); 1267866d571SSatoshi Sahara $info['editor'] = '127.0.0.1'; 127af2cfd7bSChristopher Smith 1287866d571SSatoshi Sahara // anonymous user 129af2cfd7bSChristopher Smith $info = array_merge($info, array( 130af2cfd7bSChristopher Smith 'isadmin' => false, 131af2cfd7bSChristopher Smith 'ismanager' => false, 132af2cfd7bSChristopher Smith 'perm' => 8, 133af2cfd7bSChristopher Smith 'client' => '1.2.3.4', 134af2cfd7bSChristopher Smith )); 135af2cfd7bSChristopher Smith unset($info['userinfo']); 1367866d571SSatoshi Sahara 137af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 138af2cfd7bSChristopher Smith } 139af2cfd7bSChristopher Smith 140af2cfd7bSChristopher Smith /** 141af2cfd7bSChristopher Smith * check info keys and values with $REV 142af2cfd7bSChristopher Smith * (also see $RANGE tests) 143af2cfd7bSChristopher Smith */ 144af2cfd7bSChristopher Smith function test_rev() { 145af2cfd7bSChristopher Smith global $ID,$conf,$REV; 146af2cfd7bSChristopher Smith 147af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 148af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 149af2cfd7bSChristopher Smith $rev = filemtime($filename); 150af2cfd7bSChristopher Smith $REV = $rev - 100; 151b6cc2b69SGerrit Uitslag $ext = '.txt'; 1527866d571SSatoshi Sahara if ($conf['compression']) { 1537866d571SSatoshi Sahara //compression in $info['filepath'] determined by wikiFN depends also on if the page exist 154b6cc2b69SGerrit Uitslag $ext .= "." . $conf['compression']; //.gz or .bz2 155b6cc2b69SGerrit Uitslag } 156af2cfd7bSChristopher Smith 157af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 158af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 159af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 160af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 161af2cfd7bSChristopher Smith $info['rev'] = $REV; 1625d873dd4SAndreas Gohr $info['currentrev'] = $rev; 163b6cc2b69SGerrit Uitslag $info['filepath'] = str_replace('pages','attic',substr($filename,0,-3).$REV.$ext); 164af2cfd7bSChristopher Smith 165af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 166af2cfd7bSChristopher Smith $this->assertEquals($rev-100, $REV); 167af2cfd7bSChristopher Smith } 168af2cfd7bSChristopher Smith 169af2cfd7bSChristopher Smith /** 170af2cfd7bSChristopher Smith * check info keys and values with $RANGE 171af2cfd7bSChristopher Smith */ 172af2cfd7bSChristopher Smith function test_range() { 173af2cfd7bSChristopher Smith global $ID,$conf,$REV,$RANGE; 174af2cfd7bSChristopher Smith 175af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 176af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 177af2cfd7bSChristopher Smith $rev = filemtime($filename); 178af2cfd7bSChristopher Smith $range = '1000-2000'; 179af2cfd7bSChristopher Smith 180af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 181af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 182af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 183af2cfd7bSChristopher Smith $info['exists'] = true; 184af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 1855d873dd4SAndreas Gohr $info['currentrev'] = $rev; 186af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 187af2cfd7bSChristopher Smith $info['filepath'] = $filename; 1887866d571SSatoshi Sahara // set from revinfo, $pagelog->getRevisionInfo($info['lastmod']) 1897866d571SSatoshi Sahara $info = array_merge($info, array( 1907866d571SSatoshi Sahara 'ip' => '127.0.0.1', 1917866d571SSatoshi Sahara 'user' => '', 1927866d571SSatoshi Sahara 'sum' => 'created - external edit', 1937866d571SSatoshi Sahara )); 1947866d571SSatoshi Sahara $info['editor'] = '127.0.0.1'; 195af2cfd7bSChristopher Smith 196af2cfd7bSChristopher Smith // check $RANGE without $REV 197af2cfd7bSChristopher Smith // expected result $RANGE unchanged 198af2cfd7bSChristopher Smith $RANGE = $range; 199af2cfd7bSChristopher Smith 200af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 201af2cfd7bSChristopher Smith $this->assertFalse(isset($REV)); 202af2cfd7bSChristopher Smith $this->assertEquals($range, $RANGE); 203af2cfd7bSChristopher Smith 204af2cfd7bSChristopher Smith // check $RANGE with $REV = current 205af2cfd7bSChristopher Smith // expected result: $RANGE unchanged, $REV cleared 206af2cfd7bSChristopher Smith $REV = $rev; 207af2cfd7bSChristopher Smith $info['rev'] = ''; 208af2cfd7bSChristopher Smith 209af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 210af2cfd7bSChristopher Smith $this->assertEquals('',$REV); 211af2cfd7bSChristopher Smith $this->assertEquals($range, $RANGE); 212af2cfd7bSChristopher Smith 213af2cfd7bSChristopher Smith // check with a real $REV 214af2cfd7bSChristopher Smith // expected result: $REV and $RANGE are cleared 215af2cfd7bSChristopher Smith $REV = $rev - 100; 216af2cfd7bSChristopher Smith 217af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 218af2cfd7bSChristopher Smith $this->assertEquals('', $REV); 219af2cfd7bSChristopher Smith $this->assertEquals('', $RANGE); 220af2cfd7bSChristopher Smith } 221af2cfd7bSChristopher Smith 222af2cfd7bSChristopher Smith /** 223af2cfd7bSChristopher Smith * test editor entry and external edit 224af2cfd7bSChristopher Smith */ 225af2cfd7bSChristopher Smith function test_editor_and_externaledits() { 226af2cfd7bSChristopher Smith global $ID,$conf; 227af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 228af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 229af2cfd7bSChristopher Smith $rev = filemtime($filename); 230af2cfd7bSChristopher Smith 231af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 232af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 233af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 234af2cfd7bSChristopher Smith $info['filepath'] = $filename; 235af2cfd7bSChristopher Smith $info['exists'] = true; 236af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 2375d873dd4SAndreas Gohr $info['currentrev'] = $rev; 238af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); // need $INFO set correctly for addLogEntry() 239af2cfd7bSChristopher Smith 240af2cfd7bSChristopher Smith global $INFO; 241af2cfd7bSChristopher Smith $INFO = $info; 242af2cfd7bSChristopher Smith 243af2cfd7bSChristopher Smith // add an editor for the current version of $ID 244af2cfd7bSChristopher Smith addLogEntry($rev, $ID); 245af2cfd7bSChristopher Smith 246af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 247af2cfd7bSChristopher Smith $info['ip'] = $_SERVER['REMOTE_ADDR']; 2487866d571SSatoshi Sahara $info['user'] = $_SERVER['REMOTE_USER']; 249af2cfd7bSChristopher Smith $info['sum'] = ''; 2507866d571SSatoshi Sahara $info['editor'] = $info['user']; 251af2cfd7bSChristopher Smith 252af2cfd7bSChristopher Smith // with an editor ... 253af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 254af2cfd7bSChristopher Smith 255af2cfd7bSChristopher Smith // clear the meta['last_change'] value, pageinfo should restore it 256af2cfd7bSChristopher Smith p_set_metadata($ID, array('last_change' => false)); 257af2cfd7bSChristopher Smith 258af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 259af2cfd7bSChristopher Smith $this->assertEquals($info['meta']['last_change'], p_get_metadata($ID,'last_change')); 260af2cfd7bSChristopher Smith 261af2cfd7bSChristopher Smith // fake an external edit, pageinfo should clear the last change from meta data 262af2cfd7bSChristopher Smith // and not return any editor data 263af2cfd7bSChristopher Smith $now = time() + 10; 264af2cfd7bSChristopher Smith touch($filename, $now); 265af2cfd7bSChristopher Smith 266af2cfd7bSChristopher Smith $info['lastmod'] = $now; 2675d873dd4SAndreas Gohr $info['currentrev'] = $now; 268af2cfd7bSChristopher Smith $info['meta']['last_change'] = false; 269af2cfd7bSChristopher Smith $info['ip'] = null; 270af2cfd7bSChristopher Smith $info['user'] = null; 271af2cfd7bSChristopher Smith $info['sum'] = null; 272af2cfd7bSChristopher Smith $info['editor'] = null; 273af2cfd7bSChristopher Smith 274af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 275af2cfd7bSChristopher Smith $this->assertEquals($info['meta'], p_get_metadata($ID)); // check metadata has been updated correctly 276af2cfd7bSChristopher Smith } 277af2cfd7bSChristopher Smith 278af2cfd7bSChristopher Smith /** 279af2cfd7bSChristopher Smith * check draft 280af2cfd7bSChristopher Smith */ 281af2cfd7bSChristopher Smith function test_draft() { 282af2cfd7bSChristopher Smith global $ID,$conf; 283af2cfd7bSChristopher Smith $ID = 'wiki:syntax'; 284af2cfd7bSChristopher Smith $filename = $conf['datadir'].'/wiki/syntax.txt'; 285af2cfd7bSChristopher Smith $rev = filemtime($filename); 286af2cfd7bSChristopher Smith 2877866d571SSatoshi Sahara // run once to prepare meta/wiki/syntax.change file for existing page 2887866d571SSatoshi Sahara // because pageinfo() set $info['meta']['last_change'] entry 2897866d571SSatoshi Sahara pageinfo(); 2907866d571SSatoshi Sahara 291af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 292af2cfd7bSChristopher Smith $info['id'] = 'wiki:syntax'; 293af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 294af2cfd7bSChristopher Smith $info['filepath'] = $filename; 295af2cfd7bSChristopher Smith $info['exists'] = true; 296af2cfd7bSChristopher Smith $info['lastmod'] = $rev; 2975d873dd4SAndreas Gohr $info['currentrev'] = $rev; 298af2cfd7bSChristopher Smith $info['meta'] = p_get_metadata($ID); 2997866d571SSatoshi Sahara// $info['ip'] = $_SERVER['REMOTE_ADDR']; 3007866d571SSatoshi Sahara// $info['user'] = $_SERVER['REMOTE_USER']; 3017866d571SSatoshi Sahara// $info['sum'] = ''; 3027866d571SSatoshi Sahara// $info['editor'] = $info['user']; 3037866d571SSatoshi Sahara // set from revinfo, $pagelog->getRevisionInfo($info['lastmod']) 3047866d571SSatoshi Sahara $info = array_merge($info, array( 3057866d571SSatoshi Sahara 'ip' => '127.0.0.1', 3067866d571SSatoshi Sahara 'user' => '', 3077866d571SSatoshi Sahara 'sum' => 'external edit', 3087866d571SSatoshi Sahara )); 3097866d571SSatoshi Sahara $info['editor'] = '127.0.0.1'; 310af2cfd7bSChristopher Smith 311af2cfd7bSChristopher Smith // setup a draft, make it more recent than the current page 312af2cfd7bSChristopher Smith // - pageinfo should recognise it and keep it 313*357931f3SGerrit Uitslag 3141ef67323SAndreas Gohr $draft = getCacheName($info['client']."\n".$ID,'.draft'); 315af2cfd7bSChristopher Smith touch($draft, $rev + 10); 316af2cfd7bSChristopher Smith 317af2cfd7bSChristopher Smith $info['draft'] = $draft; 318af2cfd7bSChristopher Smith 319af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 320af2cfd7bSChristopher Smith $this->assertFileExists($draft); 321af2cfd7bSChristopher Smith 322af2cfd7bSChristopher Smith // make the draft older than the current page 323af2cfd7bSChristopher Smith // - pageinfo should remove it and not return the 'draft' key 324af2cfd7bSChristopher Smith touch($draft,$rev - 10); 325af2cfd7bSChristopher Smith unset($info['draft']); 326af2cfd7bSChristopher Smith 327af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); 328af2cfd7bSChristopher Smith $this->assertFalse(file_exists($draft)); 329af2cfd7bSChristopher Smith } 330af2cfd7bSChristopher Smith 331af2cfd7bSChristopher Smith /** 332af2cfd7bSChristopher Smith * check ismobile 333af2cfd7bSChristopher Smith */ 334af2cfd7bSChristopher Smith function test_ismobile() { 335af2cfd7bSChristopher Smith global $ID,$conf; 336af2cfd7bSChristopher Smith $ID = 'wiki:start'; 337af2cfd7bSChristopher Smith 338af2cfd7bSChristopher Smith $info = $this->_get_expected_pageinfo(); 339af2cfd7bSChristopher Smith $info['id'] = 'wiki:start'; 340af2cfd7bSChristopher Smith $info['namespace'] = 'wiki'; 341af2cfd7bSChristopher Smith $info['filepath'] = $conf['datadir'].'/wiki/start.txt'; 342af2cfd7bSChristopher Smith 343af2cfd7bSChristopher Smith // overkill, ripped from clientismobile() as we aren't testing detection - but forcing it 344af2cfd7bSChristopher Smith $_SERVER['HTTP_X_WAP_PROFILE'] = 'a fake url'; 345af2cfd7bSChristopher Smith $_SERVER['HTTP_ACCEPT'] .= ';wap'; 346af2cfd7bSChristopher Smith $_SERVER['HTTP_USER_AGENT'] = 'blackberry,symbian,hand,mobi,phone'; 347af2cfd7bSChristopher Smith 348af2cfd7bSChristopher Smith $info['ismobile'] = clientismobile(); 349af2cfd7bSChristopher Smith 350af2cfd7bSChristopher Smith $this->assertTrue(clientismobile()); // ensure THIS test fails if clientismobile() returns false 351af2cfd7bSChristopher Smith $this->assertEquals($info, pageinfo()); // it would be a test failure not a pageinfo failure. 352af2cfd7bSChristopher Smith } 353af2cfd7bSChristopher Smith} 354af2cfd7bSChristopher Smith 355af2cfd7bSChristopher Smith//Setup VIM: ex: et ts=4 : 356