Add namespace restrictions to Special:Block and API
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialBlockTest.php
index 080c6e4..55a8b66 100644 (file)
@@ -2,6 +2,7 @@
 
 use MediaWiki\Block\BlockRestriction;
 use MediaWiki\Block\Restriction\PageRestriction;
+use MediaWiki\Block\Restriction\NamespaceRestriction;
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -38,7 +39,6 @@ class SpecialBlockTest extends SpecialPageTestBase {
                $this->assertArrayHasKey( 'Reason', $fields );
                $this->assertArrayHasKey( 'CreateAccount', $fields );
                $this->assertArrayHasKey( 'DisableUTEdit', $fields );
-               $this->assertArrayHasKey( 'DisableUTEdit', $fields );
                $this->assertArrayHasKey( 'AutoBlock', $fields );
                $this->assertArrayHasKey( 'HardBlock', $fields );
                $this->assertArrayHasKey( 'PreviousTarget', $fields );
@@ -46,6 +46,7 @@ class SpecialBlockTest extends SpecialPageTestBase {
 
                $this->assertArrayNotHasKey( 'EditingRestriction', $fields );
                $this->assertArrayNotHasKey( 'PageRestrictions', $fields );
+               $this->assertArrayNotHasKey( 'NamespaceRestrictions', $fields );
        }
 
        /**
@@ -61,6 +62,7 @@ class SpecialBlockTest extends SpecialPageTestBase {
 
                $this->assertArrayHasKey( 'EditingRestriction', $fields );
                $this->assertArrayHasKey( 'PageRestrictions', $fields );
+               $this->assertArrayHasKey( 'NamespaceRestrictions', $fields );
        }
 
        /**
@@ -116,6 +118,7 @@ class SpecialBlockTest extends SpecialPageTestBase {
                $block->setRestrictions( [
                        new PageRestriction( 0, $pageSaturn->getId() ),
                        new PageRestriction( 0, $pageMars->getId() ),
+                       new NamespaceRestriction( 0, NS_TALK ),
                ] );
 
                $block->insert();
@@ -229,7 +232,7 @@ class SpecialBlockTest extends SpecialPageTestBase {
        /**
         * @covers ::processForm()
         */
-       public function testProcessFormRestictions() {
+       public function testProcessFormRestrictions() {
                $this->setMwGlobals( [
                        'wgEnablePartialBlocks' => true,
                ] );
@@ -263,6 +266,7 @@ class SpecialBlockTest extends SpecialPageTestBase {
                        'Watch' => '0',
                        'EditingRestriction' => 'partial',
                        'PageRestrictions' => implode( "\n", $titles ),
+                       'NamespaceRestrictions' => '',
                ];
                $result = $page->processForm( $data, $context );
 
@@ -316,6 +320,7 @@ class SpecialBlockTest extends SpecialPageTestBase {
                        'Watch' => '0',
                        'EditingRestriction' => 'partial',
                        'PageRestrictions' => implode( "\n", $titles ),
+                       'NamespaceRestrictions' => '',
                ];
                $result = $page->processForm( $data, $context );
 
@@ -380,6 +385,63 @@ class SpecialBlockTest extends SpecialPageTestBase {
                $this->assertSame( 0, $count );
        }
 
+       /**
+        * @dataProvider provideCheckUnblockSelf
+        * @covers ::checkUnblockSelf
+        */
+       public function testCheckUnblockSelf(
+               $blockedUser,
+               $blockPerformer,
+               $adjustPerformer,
+               $adjustTarget,
+               $expectedResult,
+               $reason
+       ) {
+               $this->setGroupPermissions( 'sysop', 'unblockself', true );
+               $this->setGroupPermissions( 'user', 'block', true );
+               // Getting errors about creating users in db in provider.
+               // Need to use mutable to ensure different named testusers.
+               $users = [
+                       'u1' => TestUserRegistry::getMutableTestUser( __CLASS__, 'sysop' )->getUser(),
+                       'u2' => TestUserRegistry::getMutableTestUser( __CLASS__, 'sysop' )->getUser(),
+                       'u3' => TestUserRegistry::getMutableTestUser( __CLASS__, 'sysop' )->getUser(),
+                       'u4' => TestUserRegistry::getMutableTestUser( __CLASS__, 'sysop' )->getUser(),
+                       'nonsysop' => $this->getTestUser()->getUser()
+               ];
+               foreach ( [ 'blockedUser', 'blockPerformer', 'adjustPerformer', 'adjustTarget' ] as $var ) {
+                       $$var = $users[$$var];
+               }
+
+               $block = new \Block( [
+                       'address' => $blockedUser->getName(),
+                       'user' => $blockedUser->getId(),
+                       'by' => $blockPerformer->getId(),
+                       'expiry' => 'infinity',
+                       'sitewide' => 1,
+                       'enableAutoblock' => true,
+               ] );
+
+               $block->insert();
+
+               $this->assertSame(
+                       SpecialBlock::checkUnblockSelf( $adjustTarget, $adjustPerformer ),
+                       $expectedResult,
+                       $reason
+               );
+       }
+
+       public function provideCheckUnblockSelf() {
+               // 'blockedUser', 'blockPerformer', 'adjustPerformer', 'adjustTarget'
+               return [
+                       [ 'u1', 'u2', 'u3', 'u4', true, 'Unrelated users' ],
+                       [ 'u1', 'u2', 'u1', 'u4', 'ipbblocked', 'Block unrelated while blocked' ],
+                       [ 'u1', 'u2', 'u1', 'u1', true, 'Has unblockself' ],
+                       [ 'nonsysop', 'u2', 'nonsysop', 'nonsysop', 'ipbnounblockself', 'no unblockself' ],
+                       [ 'nonsysop', 'nonsysop', 'nonsysop', 'nonsysop', true, 'no unblockself but can de-selfblock' ],
+                       [ 'u1', 'u2', 'u1', 'u2', true, 'Can block user who blocked' ],
+               ];
+       }
+
        protected function insertBlock() {
                $badActor = $this->getTestUser()->getUser();
                $sysop = $this->getTestSysop()->getUser();