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