MediaWiki
REL1_21
|
00001 <?php 00002 00007 class BlockTest extends MediaWikiLangTestCase { 00008 00009 private $block, $madeAt; 00010 00011 /* variable used to save up the blockID we insert in this test suite */ 00012 private $blockId; 00013 00014 protected function setUp() { 00015 parent::setUp(); 00016 $this->setMwGlobals( array( 00017 'wgLanguageCode' => 'en', 00018 'wgContLang' => Language::factory( 'en' ) 00019 ) ); 00020 } 00021 00022 function addDBData() { 00023 00024 $user = User::newFromName( 'UTBlockee' ); 00025 if ( $user->getID() == 0 ) { 00026 $user->addToDatabase(); 00027 $user->setPassword( 'UTBlockeePassword' ); 00028 00029 $user->saveSettings(); 00030 } 00031 00032 // Delete the last round's block if it's still there 00033 $oldBlock = Block::newFromTarget( 'UTBlockee' ); 00034 if ( $oldBlock ) { 00035 // An old block will prevent our new one from saving. 00036 $oldBlock->delete(); 00037 } 00038 00039 $this->block = new Block( 'UTBlockee', $user->getID(), 0, 00040 'Parce que', 0, false, time() + 100500 00041 ); 00042 $this->madeAt = wfTimestamp( TS_MW ); 00043 00044 $this->block->insert(); 00045 // save up ID for use in assertion. Since ID is an autoincrement, 00046 // its value might change depending on the order the tests are run. 00047 // ApiBlockTest insert its own blocks! 00048 $newBlockId = $this->block->getId(); 00049 if ( $newBlockId ) { 00050 $this->blockId = $newBlockId; 00051 } else { 00052 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" ); 00053 } 00054 } 00055 00059 function dumpBlocks() { 00060 $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' ); 00061 print "Got " . $v->numRows() . " rows. Full dump follow:\n"; 00062 foreach ( $v as $row ) { 00063 print_r( $row ); 00064 } 00065 } 00066 00067 function testInitializerFunctionsReturnCorrectBlock() { 00068 // $this->dumpBlocks(); 00069 00070 $this->assertTrue( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" ); 00071 00072 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" ); 00073 00074 } 00075 00079 function testBug26425BlockTimestampDefaultsToTime() { 00080 // delta to stop one-off errors when things happen to go over a second mark. 00081 $delta = abs( $this->madeAt - $this->block->mTimestamp ); 00082 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" ); 00083 00084 } 00085 00094 function testBug29116LoadWithEmptyIp( $vagueTarget ) { 00095 $this->hideDeprecated( 'Block::load' ); 00096 00097 $uid = User::idFromName( 'UTBlockee' ); 00098 $this->assertTrue( ( $uid > 0 ), 'Must be able to look up the target user during tests' ); 00099 00100 $block = new Block(); 00101 $ok = $block->load( $vagueTarget, $uid ); 00102 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" ); 00103 00104 $this->assertTrue( $this->block->equals( $block ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) ); 00105 } 00106 00114 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) { 00115 $block = Block::newFromTarget( 'UTBlockee', $vagueTarget ); 00116 $this->assertTrue( $this->block->equals( $block ), "newFromTarget() returns the same block as the one that was made when given empty vagueTarget param " . var_export( $vagueTarget, true ) ); 00117 } 00118 00119 public static function provideBug29116Data() { 00120 return array( 00121 array( null ), 00122 array( '' ), 00123 array( false ) 00124 ); 00125 } 00126 00127 function testBlockedUserCanNotCreateAccount() { 00128 $username = 'BlockedUserToCreateAccountWith'; 00129 $u = User::newFromName( $username ); 00130 $u->setPassword( 'NotRandomPass' ); 00131 $u->addToDatabase(); 00132 unset( $u ); 00133 00134 00135 // Sanity check 00136 $this->assertNull( 00137 Block::newFromTarget( $username ), 00138 "$username should not be blocked" 00139 ); 00140 00141 // Reload user 00142 $u = User::newFromName( $username ); 00143 $this->assertFalse( 00144 $u->isBlockedFromCreateAccount(), 00145 "Our sandbox user should be able to create account before being blocked" 00146 ); 00147 00148 // Foreign perspective (blockee not on current wiki)... 00149 $block = new Block( 00150 /* $address */ $username, 00151 /* $user */ 14146, 00152 /* $by */ 0, 00153 /* $reason */ 'crosswiki block...', 00154 /* $timestamp */ wfTimestampNow(), 00155 /* $auto */ false, 00156 /* $expiry */ $this->db->getInfinity(), 00157 /* anonOnly */ false, 00158 /* $createAccount */ true, 00159 /* $enableAutoblock */ true, 00160 /* $hideName (ipb_deleted) */ true, 00161 /* $blockEmail */ true, 00162 /* $allowUsertalk */ false, 00163 /* $byName */ 'MetaWikiUser' 00164 ); 00165 $block->insert(); 00166 00167 // Reload block from DB 00168 $userBlock = Block::newFromTarget( $username ); 00169 $this->assertTrue( 00170 (bool)$block->prevents( 'createaccount' ), 00171 "Block object in DB should prevents 'createaccount'" 00172 ); 00173 00174 $this->assertInstanceOf( 00175 'Block', 00176 $userBlock, 00177 "'$username' block block object should be existent" 00178 ); 00179 00180 // Reload user 00181 $u = User::newFromName( $username ); 00182 $this->assertTrue( 00183 (bool)$u->isBlockedFromCreateAccount(), 00184 "Our sandbox user '$username' should NOT be able to create account" 00185 ); 00186 } 00187 00188 function testCrappyCrossWikiBlocks() { 00189 // Delete the last round's block if it's still there 00190 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' ); 00191 if ( $oldBlock ) { 00192 // An old block will prevent our new one from saving. 00193 $oldBlock->delete(); 00194 } 00195 00196 // Foreign perspective (blockee not on current wiki)... 00197 $block = new Block( 00198 /* $address */ 'UserOnForeignWiki', 00199 /* $user */ 14146, 00200 /* $by */ 0, 00201 /* $reason */ 'crosswiki block...', 00202 /* $timestamp */ wfTimestampNow(), 00203 /* $auto */ false, 00204 /* $expiry */ $this->db->getInfinity(), 00205 /* anonOnly */ false, 00206 /* $createAccount */ true, 00207 /* $enableAutoblock */ true, 00208 /* $hideName (ipb_deleted) */ true, 00209 /* $blockEmail */ true, 00210 /* $allowUsertalk */ false, 00211 /* $byName */ 'MetaWikiUser' 00212 ); 00213 00214 $res = $block->insert( $this->db ); 00215 $this->assertTrue( (bool)$res['id'], 'Block succeeded' ); 00216 00217 // Local perspective (blockee on current wiki)... 00218 $user = User::newFromName( 'UserOnForeignWiki' ); 00219 $user->addToDatabase(); 00220 // Set user ID to match the test value 00221 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) ); 00222 $user = null; // clear 00223 00224 $block = Block::newFromID( $res['id'] ); 00225 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' ); 00226 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' ); 00227 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' ); 00228 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' ); 00229 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' ); 00230 } 00231 }