Merge "Make image pagination not require a page load."
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index 19c9b68..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 ),
@@ -228,4 +227,128 @@ class BlockTest extends MediaWikiLangTestCase {
                $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
                $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
        }
+
+       protected function addXffBlocks() {
+               static $inited = false;
+
+               if ( $inited ) {
+                       return;
+               }
+
+               $inited = true;
+
+               $blockList = array(
+                       array( 'target' => '70.2.0.0/16',
+                               'type' => Block::TYPE_RANGE,
+                               'desc' => 'Range Hardblock',
+                               'ACDisable' => false,
+                               'isHardblock' => true,
+                               'isAutoBlocking' => false,
+                       ),
+                       array( 'target' => '2001:4860:4001::/48',
+                               'type' => Block::TYPE_RANGE,
+                               'desc' => 'Range6 Hardblock',
+                               'ACDisable' => false,
+                               'isHardblock' => true,
+                               'isAutoBlocking' => false,
+                       ),
+                       array( 'target' => '60.2.0.0/16',
+                               '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,
+                               'desc' => 'Range Softblock',
+                               'ACDisable' => false,
+                               'isHardblock' => false,
+                               'isAutoBlocking' => false,
+                       ),
+                       array( 'target' => '50.1.1.1',
+                               'type' => Block::TYPE_IP,
+                               'desc' => 'Exact Softblock',
+                               'ACDisable' => false,
+                               'isHardblock' => false,
+                               'isAutoBlocking' => false,
+                       ),
+               );
+
+               foreach ( $blockList as $insBlock ) {
+                       $target = $insBlock['target'];
+
+                       if ( $insBlock['type'] === Block::TYPE_IP ) {
+                               $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
+                       } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
+                               $target = IP::sanitizeRange( $target );
+                       }
+
+                       $block = new Block();
+                       $block->setTarget( $target );
+                       $block->setBlocker( 'testblocker@global' );
+                       $block->mReason = $insBlock['desc'];
+                       $block->mExpiry = 'infinity';
+                       $block->prevents( 'createaccount', $insBlock['ACDisable'] );
+                       $block->isHardblock( $insBlock['isHardblock'] );
+                       $block->isAutoblocking( $insBlock['isAutoBlocking'] );
+                       $block->insert();
+               }
+       }
+
+       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'
+                       ),
+                       array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Range Softblock with AC Disabled'
+                       ),
+                       array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Exact Softblock'
+                       ),
+                       array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
+                               'count' => 3,
+                               'result' => 'Exact Softblock'
+                       ),
+                       array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Range Hardblock'
+                       ),
+                       array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Range Hardblock'
+                       ),
+                       array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Range Softblock with AC Disabled'
+                       ),
+                       array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Exact Softblock'
+                       ),
+                       array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
+                               'count' => 1,
+                               'result' => 'Range Softblock with AC Disabled'
+                       ),
+                       array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
+                               'count' => 2,
+                               'result' => 'Range6 Hardblock'
+                       ),
+               );
+       }
+
+       /**
+        * @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 );
+       }
 }