Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index 0f37182..f0049fe 100644 (file)
@@ -6,7 +6,9 @@
  */
 class BlockTest extends MediaWikiLangTestCase {
 
-       private $block, $madeAt;
+       /** @var Block */
+       private $block;
+       private $madeAt;
 
        /* variable used to save up the blockID we insert in this test suite */
        private $blockId;
@@ -51,36 +53,42 @@ class BlockTest extends MediaWikiLangTestCase {
                } else {
                        throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
                }
+
+               $this->addXffBlocks();
        }
 
        /**
         * debug function : dump the ipblocks table
         */
        function dumpBlocks() {
-               $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' );
+               $v = $this->db->select( 'ipblocks', '*' );
                print "Got " . $v->numRows() . " rows. Full dump follow:\n";
                foreach ( $v as $row ) {
                        print_r( $row );
                }
        }
 
-       function testInitializerFunctionsReturnCorrectBlock() {
-               // $this->dumpBlocks();
-
+       /**
+        * @covers Block::newFromTarget
+        */
+       public function testINewFromTargetReturnsCorrectBlock() {
                $this->assertTrue( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
+       }
 
+       /**
+        * @covers Block::newFromID
+        */
+       public function testINewFromIDReturnsCorrectBlock() {
                $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" );
-
        }
 
        /**
         * per bug 26425
         */
-       function testBug26425BlockTimestampDefaultsToTime() {
+       public function testBug26425BlockTimestampDefaultsToTime() {
                // delta to stop one-off errors when things happen to go over a second mark.
                $delta = abs( $this->madeAt - $this->block->mTimestamp );
                $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
-
        }
 
        /**
@@ -90,8 +98,9 @@ class BlockTest extends MediaWikiLangTestCase {
         * This stopped working with r84475 and friends: regression being fixed for bug 29116.
         *
         * @dataProvider provideBug29116Data
+        * @covers Block::load
         */
-       function testBug29116LoadWithEmptyIp( $vagueTarget ) {
+       public function testBug29116LoadWithEmptyIp( $vagueTarget ) {
                $this->hideDeprecated( 'Block::load' );
 
                $uid = User::idFromName( 'UTBlockee' );
@@ -110,8 +119,9 @@ class BlockTest extends MediaWikiLangTestCase {
         * had. Regression bug 29116.
         *
         * @dataProvider provideBug29116Data
+        * @covers Block::newFromTarget
         */
-       function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
+       public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
                $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
                $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 ) );
        }
@@ -124,14 +134,16 @@ class BlockTest extends MediaWikiLangTestCase {
                );
        }
 
-       function testBlockedUserCanNotCreateAccount() {
+       /**
+        * @covers Block::prevents
+        */
+       public function testBlockedUserCanNotCreateAccount() {
                $username = 'BlockedUserToCreateAccountWith';
                $u = User::newFromName( $username );
                $u->setPassword( 'NotRandomPass' );
                $u->addToDatabase();
                unset( $u );
 
-
                // Sanity check
                $this->assertNull(
                        Block::newFromTarget( $username ),
@@ -185,7 +197,10 @@ class BlockTest extends MediaWikiLangTestCase {
                );
        }
 
-       function testCrappyCrossWikiBlocks() {
+       /**
+        * @covers Block::insert
+        */
+       public function testCrappyCrossWikiBlocks() {
                // Delete the last round's block if it's still there
                $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
                if ( $oldBlock ) {
@@ -229,39 +244,46 @@ class BlockTest extends MediaWikiLangTestCase {
                $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
        }
 
-       function testBlocksOnXff() {
+       protected function addXffBlocks() {
+               static $inited = false;
+
+               if ( $inited ) {
+                       return;
+               }
+
+               $inited = true;
 
                $blockList = array(
                        array( 'target' => '70.2.0.0/16',
-                               'type' =>  Block::TYPE_RANGE,
+                               'type' => Block::TYPE_RANGE,
                                'desc' => 'Range Hardblock',
                                'ACDisable' => false,
                                'isHardblock' => true,
                                'isAutoBlocking' => false,
                        ),
                        array( 'target' => '2001:4860:4001::/48',
-                               'type' =>  Block::TYPE_RANGE,
+                               'type' => Block::TYPE_RANGE,
                                'desc' => 'Range6 Hardblock',
                                'ACDisable' => false,
                                'isHardblock' => true,
                                'isAutoBlocking' => false,
                        ),
                        array( 'target' => '60.2.0.0/16',
-                               'type' =>  Block::TYPE_RANGE,
+                               'type' => Block::TYPE_RANGE,
                                'desc' => 'Range Softblock with AC Disabled',
                                'ACDisable' => true,
                                'isHardblock' => false,
                                'isAutoBlocking' => false,
                        ),
                        array( 'target' => '50.2.0.0/16',
-                               'type' =>  Block::TYPE_RANGE,
+                               'type' => Block::TYPE_RANGE,
                                'desc' => 'Range Softblock',
                                'ACDisable' => false,
                                'isHardblock' => false,
                                'isAutoBlocking' => false,
                        ),
                        array( 'target' => '50.1.1.1',
-                               'type' =>  Block::TYPE_IP,
+                               'type' => Block::TYPE_IP,
                                'desc' => 'Exact Softblock',
                                'ACDisable' => false,
                                'isHardblock' => false,
@@ -288,8 +310,10 @@ class BlockTest extends MediaWikiLangTestCase {
                        $block->isAutoblocking( $insBlock['isAutoBlocking'] );
                        $block->insert();
                }
+       }
 
-               $xffHeaders = array(
+       public static function providerXff() {
+               return array(
                        array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
                                'count' => 2,
                                'result' => 'Range Hardblock'
@@ -331,14 +355,18 @@ class BlockTest extends MediaWikiLangTestCase {
                                'result' => 'Range6 Hardblock'
                        ),
                );
+       }
 
-               foreach ( $xffHeaders as $test ) {
-                       $list = array_map( 'trim', explode( ',', $test['xff'] ) );
-                       $xffblocks = Block::getBlocksForIPList( $list, true );
-                       $this->assertEquals( $test['count'], count( $xffblocks ), 'Number of blocks for ' . $test['xff'] );
-                       $block = Block::chooseBlock( $xffblocks, $list );
-                       $this->assertEquals( $test['result'], $block->mReason, 'Correct block type for XFF header ' . $test['xff'] );
-               }
-
+       /**
+        * @dataProvider providerXff
+        * @covers Block::getBlocksForIPList
+        * @covers Block::chooseBlock
+        */
+       public function testBlocksOnXff( $xff, $exCount, $exResult ) {
+               $list = array_map( 'trim', explode( ',', $xff ) );
+               $xffblocks = Block::getBlocksForIPList( $list, true );
+               $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
+               $block = Block::chooseBlock( $xffblocks, $list );
+               $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
        }
 }