MediaWiki  REL1_24
BlockTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class BlockTest extends MediaWikiLangTestCase {
00008 
00010     private $block;
00011     private $madeAt;
00012 
00013     /* variable used to save up the blockID we insert in this test suite */
00014     private $blockId;
00015 
00016     protected function setUp() {
00017         parent::setUp();
00018         $this->setMwGlobals( array(
00019             'wgLanguageCode' => 'en',
00020             'wgContLang' => Language::factory( 'en' )
00021         ) );
00022     }
00023 
00024     function addDBData() {
00025 
00026         $user = User::newFromName( 'UTBlockee' );
00027         if ( $user->getID() == 0 ) {
00028             $user->addToDatabase();
00029             $user->setPassword( 'UTBlockeePassword' );
00030 
00031             $user->saveSettings();
00032         }
00033 
00034         // Delete the last round's block if it's still there
00035         $oldBlock = Block::newFromTarget( 'UTBlockee' );
00036         if ( $oldBlock ) {
00037             // An old block will prevent our new one from saving.
00038             $oldBlock->delete();
00039         }
00040 
00041         $this->block = new Block( 'UTBlockee', $user->getID(), 0,
00042             'Parce que', 0, false, time() + 100500
00043         );
00044         $this->madeAt = wfTimestamp( TS_MW );
00045 
00046         $this->block->insert();
00047         // save up ID for use in assertion. Since ID is an autoincrement,
00048         // its value might change depending on the order the tests are run.
00049         // ApiBlockTest insert its own blocks!
00050         $newBlockId = $this->block->getId();
00051         if ( $newBlockId ) {
00052             $this->blockId = $newBlockId;
00053         } else {
00054             throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
00055         }
00056 
00057         $this->addXffBlocks();
00058     }
00059 
00063     function dumpBlocks() {
00064         $v = $this->db->select( 'ipblocks', '*' );
00065         print "Got " . $v->numRows() . " rows. Full dump follow:\n";
00066         foreach ( $v as $row ) {
00067             print_r( $row );
00068         }
00069     }
00070 
00074     public function testINewFromTargetReturnsCorrectBlock() {
00075         $this->assertTrue(
00076             $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ),
00077             "newFromTarget() returns the same block as the one that was made"
00078         );
00079     }
00080 
00084     public function testINewFromIDReturnsCorrectBlock() {
00085         $this->assertTrue(
00086             $this->block->equals( Block::newFromID( $this->blockId ) ),
00087             "newFromID() returns the same block as the one that was made"
00088         );
00089     }
00090 
00094     public function testBug26425BlockTimestampDefaultsToTime() {
00095         // delta to stop one-off errors when things happen to go over a second mark.
00096         $delta = abs( $this->madeAt - $this->block->mTimestamp );
00097         $this->assertLessThan(
00098             2,
00099             $delta,
00100             "If no timestamp is specified, the block is recorded as time()"
00101         );
00102     }
00103 
00112     public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
00113         $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
00114         $this->assertTrue(
00115             $this->block->equals( $block ),
00116             "newFromTarget() returns the same block as the one that was made when "
00117                 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
00118         );
00119     }
00120 
00121     public static function provideBug29116Data() {
00122         return array(
00123             array( null ),
00124             array( '' ),
00125             array( false )
00126         );
00127     }
00128 
00132     public function testBlockedUserCanNotCreateAccount() {
00133         $username = 'BlockedUserToCreateAccountWith';
00134         $u = User::newFromName( $username );
00135         $u->setPassword( 'NotRandomPass' );
00136         $u->addToDatabase();
00137         unset( $u );
00138 
00139         // Sanity check
00140         $this->assertNull(
00141             Block::newFromTarget( $username ),
00142             "$username should not be blocked"
00143         );
00144 
00145         // Reload user
00146         $u = User::newFromName( $username );
00147         $this->assertFalse(
00148             $u->isBlockedFromCreateAccount(),
00149             "Our sandbox user should be able to create account before being blocked"
00150         );
00151 
00152         // Foreign perspective (blockee not on current wiki)...
00153         $block = new Block(
00154             /* $address */ $username,
00155             /* $user */ 14146,
00156             /* $by */ 0,
00157             /* $reason */ 'crosswiki block...',
00158             /* $timestamp */ wfTimestampNow(),
00159             /* $auto */ false,
00160             /* $expiry */ $this->db->getInfinity(),
00161             /* anonOnly */ false,
00162             /* $createAccount */ true,
00163             /* $enableAutoblock */ true,
00164             /* $hideName (ipb_deleted) */ true,
00165             /* $blockEmail */ true,
00166             /* $allowUsertalk */ false,
00167             /* $byName */ 'MetaWikiUser'
00168         );
00169         $block->insert();
00170 
00171         // Reload block from DB
00172         $userBlock = Block::newFromTarget( $username );
00173         $this->assertTrue(
00174             (bool)$block->prevents( 'createaccount' ),
00175             "Block object in DB should prevents 'createaccount'"
00176         );
00177 
00178         $this->assertInstanceOf(
00179             'Block',
00180             $userBlock,
00181             "'$username' block block object should be existent"
00182         );
00183 
00184         // Reload user
00185         $u = User::newFromName( $username );
00186         $this->assertTrue(
00187             (bool)$u->isBlockedFromCreateAccount(),
00188             "Our sandbox user '$username' should NOT be able to create account"
00189         );
00190     }
00191 
00195     public function testCrappyCrossWikiBlocks() {
00196         // Delete the last round's block if it's still there
00197         $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
00198         if ( $oldBlock ) {
00199             // An old block will prevent our new one from saving.
00200             $oldBlock->delete();
00201         }
00202 
00203         // Foreign perspective (blockee not on current wiki)...
00204         $block = new Block(
00205             /* $address */ 'UserOnForeignWiki',
00206             /* $user */ 14146,
00207             /* $by */ 0,
00208             /* $reason */ 'crosswiki block...',
00209             /* $timestamp */ wfTimestampNow(),
00210             /* $auto */ false,
00211             /* $expiry */ $this->db->getInfinity(),
00212             /* anonOnly */ false,
00213             /* $createAccount */ true,
00214             /* $enableAutoblock */ true,
00215             /* $hideName (ipb_deleted) */ true,
00216             /* $blockEmail */ true,
00217             /* $allowUsertalk */ false,
00218             /* $byName */ 'MetaWikiUser'
00219         );
00220 
00221         $res = $block->insert( $this->db );
00222         $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
00223 
00224         // Local perspective (blockee on current wiki)...
00225         $user = User::newFromName( 'UserOnForeignWiki' );
00226         $user->addToDatabase();
00227         // Set user ID to match the test value
00228         $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
00229         $user = null; // clear
00230 
00231         $block = Block::newFromID( $res['id'] );
00232         $this->assertEquals(
00233             'UserOnForeignWiki',
00234             $block->getTarget()->getName(),
00235             'Correct blockee name'
00236         );
00237         $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
00238         $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
00239         $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
00240         $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
00241     }
00242 
00243     protected function addXffBlocks() {
00244         static $inited = false;
00245 
00246         if ( $inited ) {
00247             return;
00248         }
00249 
00250         $inited = true;
00251 
00252         $blockList = array(
00253             array( 'target' => '70.2.0.0/16',
00254                 'type' => Block::TYPE_RANGE,
00255                 'desc' => 'Range Hardblock',
00256                 'ACDisable' => false,
00257                 'isHardblock' => true,
00258                 'isAutoBlocking' => false,
00259             ),
00260             array( 'target' => '2001:4860:4001::/48',
00261                 'type' => Block::TYPE_RANGE,
00262                 'desc' => 'Range6 Hardblock',
00263                 'ACDisable' => false,
00264                 'isHardblock' => true,
00265                 'isAutoBlocking' => false,
00266             ),
00267             array( 'target' => '60.2.0.0/16',
00268                 'type' => Block::TYPE_RANGE,
00269                 'desc' => 'Range Softblock with AC Disabled',
00270                 'ACDisable' => true,
00271                 'isHardblock' => false,
00272                 'isAutoBlocking' => false,
00273             ),
00274             array( 'target' => '50.2.0.0/16',
00275                 'type' => Block::TYPE_RANGE,
00276                 'desc' => 'Range Softblock',
00277                 'ACDisable' => false,
00278                 'isHardblock' => false,
00279                 'isAutoBlocking' => false,
00280             ),
00281             array( 'target' => '50.1.1.1',
00282                 'type' => Block::TYPE_IP,
00283                 'desc' => 'Exact Softblock',
00284                 'ACDisable' => false,
00285                 'isHardblock' => false,
00286                 'isAutoBlocking' => false,
00287             ),
00288         );
00289 
00290         foreach ( $blockList as $insBlock ) {
00291             $target = $insBlock['target'];
00292 
00293             if ( $insBlock['type'] === Block::TYPE_IP ) {
00294                 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
00295             } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
00296                 $target = IP::sanitizeRange( $target );
00297             }
00298 
00299             $block = new Block();
00300             $block->setTarget( $target );
00301             $block->setBlocker( 'testblocker@global' );
00302             $block->mReason = $insBlock['desc'];
00303             $block->mExpiry = 'infinity';
00304             $block->prevents( 'createaccount', $insBlock['ACDisable'] );
00305             $block->isHardblock( $insBlock['isHardblock'] );
00306             $block->isAutoblocking( $insBlock['isAutoBlocking'] );
00307             $block->insert();
00308         }
00309     }
00310 
00311     public static function providerXff() {
00312         return array(
00313             array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
00314                 'count' => 2,
00315                 'result' => 'Range Hardblock'
00316             ),
00317             array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
00318                 'count' => 2,
00319                 'result' => 'Range Softblock with AC Disabled'
00320             ),
00321             array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
00322                 'count' => 2,
00323                 'result' => 'Exact Softblock'
00324             ),
00325             array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
00326                 'count' => 3,
00327                 'result' => 'Exact Softblock'
00328             ),
00329             array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
00330                 'count' => 2,
00331                 'result' => 'Range Hardblock'
00332             ),
00333             array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
00334                 'count' => 2,
00335                 'result' => 'Range Hardblock'
00336             ),
00337             array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
00338                 'count' => 2,
00339                 'result' => 'Range Softblock with AC Disabled'
00340             ),
00341             array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
00342                 'count' => 2,
00343                 'result' => 'Exact Softblock'
00344             ),
00345             array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT>, 60.2.1.1, 2.3.4.5',
00346                 'count' => 1,
00347                 'result' => 'Range Softblock with AC Disabled'
00348             ),
00349             array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
00350                 'count' => 2,
00351                 'result' => 'Range6 Hardblock'
00352             ),
00353         );
00354     }
00355 
00361     public function testBlocksOnXff( $xff, $exCount, $exResult ) {
00362         $list = array_map( 'trim', explode( ',', $xff ) );
00363         $xffblocks = Block::getBlocksForIPList( $list, true );
00364         $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
00365         $block = Block::chooseBlock( $xffblocks, $list );
00366         $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
00367     }
00368 }