Merge "HTML escape parameter 'text' of hook 'SkinEditSectionLinks'"
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
index 1f11ef2..dac3b87 100644 (file)
@@ -1,8 +1,9 @@
 <?php
 
-use MediaWiki\Block\BlockRestriction;
+use MediaWiki\Block\BlockRestrictionStore;
 use MediaWiki\Block\Restriction\PageRestriction;
 use MediaWiki\Block\Restriction\NamespaceRestriction;
+use MediaWiki\MediaWikiServices;
 
 /**
  * @group Database
@@ -94,7 +95,7 @@ class BlockTest extends MediaWikiLangTestCase {
                $madeAt = wfTimestamp( TS_MW );
 
                // delta to stop one-off errors when things happen to go over a second mark.
-               $delta = abs( $madeAt - $block->mTimestamp );
+               $delta = abs( $madeAt - $block->getTimestamp() );
                $this->assertLessThan(
                        2,
                        $delta,
@@ -130,6 +131,68 @@ class BlockTest extends MediaWikiLangTestCase {
                ];
        }
 
+       /**
+        * @dataProvider provideNewFromTargetRangeBlocks
+        * @covers Block::newFromTarget
+        */
+       public function testNewFromTargetRangeBlocks( $targets, $ip, $expectedTarget ) {
+               $blocker = $this->getTestSysop()->getUser();
+
+               foreach ( $targets as $target ) {
+                       $block = new Block();
+                       $block->setTarget( $target );
+                       $block->setBlocker( $blocker );
+                       $block->insert();
+               }
+
+               // Should find the block with the narrowest range
+               $blockTarget = Block::newFromTarget( $this->getTestUser()->getUser(), $ip )->getTarget();
+               $this->assertSame(
+                       $blockTarget instanceof User ? $blockTarget->getName() : $blockTarget,
+                       $expectedTarget
+               );
+
+               foreach ( $targets as $target ) {
+                       $block = Block::newFromTarget( $target );
+                       $block->delete();
+               }
+       }
+
+       function provideNewFromTargetRangeBlocks() {
+               return [
+                       'Blocks to IPv4 ranges' => [
+                               [ '0.0.0.0/20', '0.0.0.0/30', '0.0.0.0/25' ],
+                               '0.0.0.0',
+                               '0.0.0.0/30'
+                       ],
+                       'Blocks to IPv6 ranges' => [
+                               [ '0:0:0:0:0:0:0:0/20', '0:0:0:0:0:0:0:0/30', '0:0:0:0:0:0:0:0/25' ],
+                               '0:0:0:0:0:0:0:0',
+                               '0:0:0:0:0:0:0:0/30'
+                       ],
+                       'Blocks to wide IPv4 range and IP' => [
+                               [ '0.0.0.0/16', '0.0.0.0' ],
+                               '0.0.0.0',
+                               '0.0.0.0'
+                       ],
+                       'Blocks to wide IPv6 range and IP' => [
+                               [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ],
+                               '0:0:0:0:0:0:0:0',
+                               '0:0:0:0:0:0:0:0'
+                       ],
+                       'Blocks to narrow IPv4 range and IP' => [
+                               [ '0.0.0.0/31', '0.0.0.0' ],
+                               '0.0.0.0',
+                               '0.0.0.0'
+                       ],
+                       'Blocks to narrow IPv6 range and IP' => [
+                               [ '0:0:0:0:0:0:0:0/127', '0:0:0:0:0:0:0:0' ],
+                               '0:0:0:0:0:0:0:0',
+                               '0:0:0:0:0:0:0:0'
+                       ],
+               ];
+       }
+
        /**
         * @covers Block::appliesToRight
         */
@@ -302,8 +365,8 @@ class BlockTest extends MediaWikiLangTestCase {
                        $block = new Block();
                        $block->setTarget( $target );
                        $block->setBlocker( $blocker );
-                       $block->mReason = $insBlock['desc'];
-                       $block->mExpiry = 'infinity';
+                       $block->setReason( $insBlock['desc'] );
+                       $block->setExpiry( 'infinity' );
                        $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
                        $block->isHardblock( $insBlock['isHardblock'] );
                        $block->isAutoblocking( $insBlock['isAutoBlocking'] );
@@ -369,99 +432,9 @@ class BlockTest extends MediaWikiLangTestCase {
                $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 );
-       }
-
-       /**
-        * @covers Block::__construct
-        */
-       public function testDeprecatedConstructor() {
-               $this->hideDeprecated( 'Block::__construct with multiple arguments' );
-               $username = 'UnthinkablySecretRandomUsername';
-               $reason = 'being irrational';
-
-               # Set up the target
-               $u = User::newFromName( $username );
-               if ( $u->getId() == 0 ) {
-                       $u->addToDatabase();
-                       TestUser::setPasswordForUser( $u, 'TotallyObvious' );
-               }
-               unset( $u );
-
-               # Make sure the user isn't blocked
-               $this->assertNull(
-                       Block::newFromTarget( $username ),
-                       "$username should not be blocked"
-               );
-
-               # Perform the block
-               $block = new Block(
-                       /* address */ $username,
-                       /* user */ 0,
-                       /* by */ $this->getTestSysop()->getUser()->getId(),
-                       /* reason */ $reason,
-                       /* timestamp */ 0,
-                       /* auto */ false,
-                       /* expiry */ 0
-               );
-               $block->insert();
-
-               # Check target
                $this->assertEquals(
-                       $block->getTarget()->getName(),
-                       $username,
-                       "Target should be set properly"
+                       $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
                );
-
-               # Check supplied parameter
-               $this->assertEquals(
-                       $block->mReason,
-                       $reason,
-                       "Reason should be non-default"
-               );
-
-               # Check default parameter
-               $this->assertFalse(
-                       (bool)$block->appliesToRight( 'createaccount' ),
-                       "Account creation should not be blocked by default"
-               );
-       }
-
-       /**
-        * @covers Block::getSystemBlockType
-        * @covers Block::insert
-        * @covers Block::doAutoblock
-        */
-       public function testSystemBlocks() {
-               $user = $this->getUserForBlocking();
-               $this->addBlockForUser( $user );
-
-               $blockOptions = [
-                       'address' => $user->getName(),
-                       'reason' => 'test system block',
-                       'timestamp' => wfTimestampNow(),
-                       'expiry' => $this->db->getInfinity(),
-                       'byText' => 'MediaWiki default',
-                       'systemBlock' => 'test',
-                       'enableAutoblock' => true,
-               ];
-               $block = new Block( $blockOptions );
-
-               $this->assertSame( 'test', $block->getSystemBlockType() );
-
-               try {
-                       $block->insert();
-                       $this->fail( 'Expected exception not thrown' );
-               } catch ( MWException $ex ) {
-                       $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
-               }
-
-               try {
-                       $block->doAutoblock( '192.0.2.2' );
-                       $this->fail( 'Expected exception not thrown' );
-               } catch ( MWException $ex ) {
-                       $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
-               }
        }
 
        /**
@@ -665,7 +638,7 @@ class BlockTest extends MediaWikiLangTestCase {
 
                $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
                $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER );
-               BlockRestriction::insert( [ $pageRestriction, $namespaceRestriction ] );
+               $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
 
                $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
                $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
@@ -726,7 +699,7 @@ class BlockTest extends MediaWikiLangTestCase {
                        $block->getId(),
                        $title->getArticleID()
                );
-               BlockRestriction::insert( [ $pageRestriction ] );
+               $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
 
                $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
 
@@ -752,7 +725,7 @@ class BlockTest extends MediaWikiLangTestCase {
                $block->insert();
 
                $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN );
-               BlockRestriction::insert( [ $namespaceRestriction ] );
+               $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
 
                $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) );
                $this->assertFalse( $block->appliesToNamespace( NS_USER ) );
@@ -771,4 +744,12 @@ class BlockTest extends MediaWikiLangTestCase {
                $this->assertFalse( $block->appliesToRight( 'purge' ) );
        }
 
+       /**
+        * Get an instance of BlockRestrictionStore
+        *
+        * @return BlockRestrictionStore
+        */
+       protected function getBlockRestrictionStore() : BlockRestrictionStore {
+               return MediaWikiServices::getInstance()->getBlockRestrictionStore();
+       }
 }