MediaWiki  REL1_23
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( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
00076     }
00077 
00081     public function testINewFromIDReturnsCorrectBlock() {
00082         $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" );
00083     }
00084 
00088     public function testBug26425BlockTimestampDefaultsToTime() {
00089         // delta to stop one-off errors when things happen to go over a second mark.
00090         $delta = abs( $this->madeAt - $this->block->mTimestamp );
00091         $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
00092     }
00093 
00102     public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
00103         $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
00104         $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 ) );
00105     }
00106 
00107     public static function provideBug29116Data() {
00108         return array(
00109             array( null ),
00110             array( '' ),
00111             array( false )
00112         );
00113     }
00114 
00118     public function testBlockedUserCanNotCreateAccount() {
00119         $username = 'BlockedUserToCreateAccountWith';
00120         $u = User::newFromName( $username );
00121         $u->setPassword( 'NotRandomPass' );
00122         $u->addToDatabase();
00123         unset( $u );
00124 
00125         // Sanity check
00126         $this->assertNull(
00127             Block::newFromTarget( $username ),
00128             "$username should not be blocked"
00129         );
00130 
00131         // Reload user
00132         $u = User::newFromName( $username );
00133         $this->assertFalse(
00134             $u->isBlockedFromCreateAccount(),
00135             "Our sandbox user should be able to create account before being blocked"
00136         );
00137 
00138         // Foreign perspective (blockee not on current wiki)...
00139         $block = new Block(
00140             /* $address */ $username,
00141             /* $user */ 14146,
00142             /* $by */ 0,
00143             /* $reason */ 'crosswiki block...',
00144             /* $timestamp */ wfTimestampNow(),
00145             /* $auto */ false,
00146             /* $expiry */ $this->db->getInfinity(),
00147             /* anonOnly */ false,
00148             /* $createAccount */ true,
00149             /* $enableAutoblock */ true,
00150             /* $hideName (ipb_deleted) */ true,
00151             /* $blockEmail */ true,
00152             /* $allowUsertalk */ false,
00153             /* $byName */ 'MetaWikiUser'
00154         );
00155         $block->insert();
00156 
00157         // Reload block from DB
00158         $userBlock = Block::newFromTarget( $username );
00159         $this->assertTrue(
00160             (bool)$block->prevents( 'createaccount' ),
00161             "Block object in DB should prevents 'createaccount'"
00162         );
00163 
00164         $this->assertInstanceOf(
00165             'Block',
00166             $userBlock,
00167             "'$username' block block object should be existent"
00168         );
00169 
00170         // Reload user
00171         $u = User::newFromName( $username );
00172         $this->assertTrue(
00173             (bool)$u->isBlockedFromCreateAccount(),
00174             "Our sandbox user '$username' should NOT be able to create account"
00175         );
00176     }
00177 
00181     public function testCrappyCrossWikiBlocks() {
00182         // Delete the last round's block if it's still there
00183         $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
00184         if ( $oldBlock ) {
00185             // An old block will prevent our new one from saving.
00186             $oldBlock->delete();
00187         }
00188 
00189         // Foreign perspective (blockee not on current wiki)...
00190         $block = new Block(
00191             /* $address */ 'UserOnForeignWiki',
00192             /* $user */ 14146,
00193             /* $by */ 0,
00194             /* $reason */ 'crosswiki block...',
00195             /* $timestamp */ wfTimestampNow(),
00196             /* $auto */ false,
00197             /* $expiry */ $this->db->getInfinity(),
00198             /* anonOnly */ false,
00199             /* $createAccount */ true,
00200             /* $enableAutoblock */ true,
00201             /* $hideName (ipb_deleted) */ true,
00202             /* $blockEmail */ true,
00203             /* $allowUsertalk */ false,
00204             /* $byName */ 'MetaWikiUser'
00205         );
00206 
00207         $res = $block->insert( $this->db );
00208         $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
00209 
00210         // Local perspective (blockee on current wiki)...
00211         $user = User::newFromName( 'UserOnForeignWiki' );
00212         $user->addToDatabase();
00213         // Set user ID to match the test value
00214         $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
00215         $user = null; // clear
00216 
00217         $block = Block::newFromID( $res['id'] );
00218         $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
00219         $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
00220         $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
00221         $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
00222         $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
00223     }
00224 
00225     protected function addXffBlocks() {
00226         static $inited = false;
00227 
00228         if ( $inited ) {
00229             return;
00230         }
00231 
00232         $inited = true;
00233 
00234         $blockList = array(
00235             array( 'target' => '70.2.0.0/16',
00236                 'type' => Block::TYPE_RANGE,
00237                 'desc' => 'Range Hardblock',
00238                 'ACDisable' => false,
00239                 'isHardblock' => true,
00240                 'isAutoBlocking' => false,
00241             ),
00242             array( 'target' => '2001:4860:4001::/48',
00243                 'type' => Block::TYPE_RANGE,
00244                 'desc' => 'Range6 Hardblock',
00245                 'ACDisable' => false,
00246                 'isHardblock' => true,
00247                 'isAutoBlocking' => false,
00248             ),
00249             array( 'target' => '60.2.0.0/16',
00250                 'type' => Block::TYPE_RANGE,
00251                 'desc' => 'Range Softblock with AC Disabled',
00252                 'ACDisable' => true,
00253                 'isHardblock' => false,
00254                 'isAutoBlocking' => false,
00255             ),
00256             array( 'target' => '50.2.0.0/16',
00257                 'type' => Block::TYPE_RANGE,
00258                 'desc' => 'Range Softblock',
00259                 'ACDisable' => false,
00260                 'isHardblock' => false,
00261                 'isAutoBlocking' => false,
00262             ),
00263             array( 'target' => '50.1.1.1',
00264                 'type' => Block::TYPE_IP,
00265                 'desc' => 'Exact Softblock',
00266                 'ACDisable' => false,
00267                 'isHardblock' => false,
00268                 'isAutoBlocking' => false,
00269             ),
00270         );
00271 
00272         foreach ( $blockList as $insBlock ) {
00273             $target = $insBlock['target'];
00274 
00275             if ( $insBlock['type'] === Block::TYPE_IP ) {
00276                 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
00277             } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
00278                 $target = IP::sanitizeRange( $target );
00279             }
00280 
00281             $block = new Block();
00282             $block->setTarget( $target );
00283             $block->setBlocker( 'testblocker@global' );
00284             $block->mReason = $insBlock['desc'];
00285             $block->mExpiry = 'infinity';
00286             $block->prevents( 'createaccount', $insBlock['ACDisable'] );
00287             $block->isHardblock( $insBlock['isHardblock'] );
00288             $block->isAutoblocking( $insBlock['isAutoBlocking'] );
00289             $block->insert();
00290         }
00291     }
00292 
00293     public static function providerXff() {
00294         return array(
00295             array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
00296                 'count' => 2,
00297                 'result' => 'Range Hardblock'
00298             ),
00299             array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
00300                 'count' => 2,
00301                 'result' => 'Range Softblock with AC Disabled'
00302             ),
00303             array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
00304                 'count' => 2,
00305                 'result' => 'Exact Softblock'
00306             ),
00307             array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
00308                 'count' => 3,
00309                 'result' => 'Exact Softblock'
00310             ),
00311             array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
00312                 'count' => 2,
00313                 'result' => 'Range Hardblock'
00314             ),
00315             array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
00316                 'count' => 2,
00317                 'result' => 'Range Hardblock'
00318             ),
00319             array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
00320                 'count' => 2,
00321                 'result' => 'Range Softblock with AC Disabled'
00322             ),
00323             array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
00324                 'count' => 2,
00325                 'result' => 'Exact Softblock'
00326             ),
00327             array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT>, 60.2.1.1, 2.3.4.5',
00328                 'count' => 1,
00329                 'result' => 'Range Softblock with AC Disabled'
00330             ),
00331             array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
00332                 'count' => 2,
00333                 'result' => 'Range6 Hardblock'
00334             ),
00335         );
00336     }
00337 
00343     public function testBlocksOnXff( $xff, $exCount, $exResult ) {
00344         $list = array_map( 'trim', explode( ',', $xff ) );
00345         $xffblocks = Block::getBlocksForIPList( $list, true );
00346         $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
00347         $block = Block::chooseBlock( $xffblocks, $list );
00348         $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
00349     }
00350 }