Merge "Make image pagination not require a page load."
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index 0f37182..32fc2c5 100644 (file)
@@ -51,13 +51,15 @@ 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 );
@@ -70,7 +72,6 @@ class BlockTest extends MediaWikiLangTestCase {
                $this->assertTrue( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
 
                $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" );
-
        }
 
        /**
@@ -80,7 +81,6 @@ class BlockTest extends MediaWikiLangTestCase {
                // 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()" );
-
        }
 
        /**
@@ -131,7 +131,6 @@ class BlockTest extends MediaWikiLangTestCase {
                $u->addToDatabase();
                unset( $u );
 
-
                // Sanity check
                $this->assertNull(
                        Block::newFromTarget( $username ),
@@ -229,39 +228,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 +294,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 +339,16 @@ 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
+        */
+       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 );
        }
 }