Fix SpecialBlock validation for ipb_allow_usertalk
authorDayllan Maza <dmaza@wikimedia.org>
Tue, 30 Jul 2019 02:15:56 +0000 (22:15 -0400)
committerDayllan Maza <dmaza@wikimedia.org>
Tue, 30 Jul 2019 19:34:14 +0000 (15:34 -0400)
ipb_allow_usertalk should be saved as false only when:
    A block is sitewide
    A block is partial and there is a restriction on the User_talk namespace

Bug: T224468
Change-Id: Ic85368991b5905af1bab99b0cb5fcba0e993405e

includes/specials/SpecialBlock.php
languages/i18n/en.json
languages/i18n/qqq.json
tests/phpunit/includes/api/ApiBlockTest.php
tests/phpunit/includes/specials/SpecialBlockTest.php

index ea4f18d..a55a9c8 100644 (file)
@@ -754,8 +754,6 @@ class SpecialBlock extends FormSpecialPage {
         * @return bool|array
         */
        public static function processForm( array $data, IContextSource $context ) {
-               global $wgBlockAllowsUTEdit, $wgHideUserContribLimit;
-
                $performer = $context->getUser();
                $enablePartialBlocks = $context->getConfig()->get( 'EnablePartialBlocks' );
                $isPartialBlock = $enablePartialBlocks &&
@@ -843,23 +841,33 @@ class SpecialBlock extends FormSpecialPage {
                        }
 
                        # Recheck params here...
+                       $hideUserContribLimit = $context->getConfig()->get( 'HideUserContribLimit' );
                        if ( $type != DatabaseBlock::TYPE_USER ) {
                                $data['HideUser'] = false; # IP users should not be hidden
                        } elseif ( !wfIsInfinity( $data['Expiry'] ) ) {
                                # Bad expiry.
                                return [ 'ipb_expiry_temp' ];
-                       } elseif ( $wgHideUserContribLimit !== false
-                               && $user->getEditCount() > $wgHideUserContribLimit
+                       } elseif ( $hideUserContribLimit !== false
+                               && $user->getEditCount() > $hideUserContribLimit
                        ) {
                                # Typically, the user should have a handful of edits.
                                # Disallow hiding users with many edits for performance.
                                return [ [ 'ipb_hide_invalid',
-                                       Message::numParam( $wgHideUserContribLimit ) ] ];
+                                       Message::numParam( $hideUserContribLimit ) ] ];
                        } elseif ( !$data['Confirm'] ) {
                                return [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
                        }
                }
 
+               $blockAllowsUTEdit = $context->getConfig()->get( 'BlockAllowsUTEdit' );
+               $userTalkEditAllowed = !$blockAllowsUTEdit || !$data['DisableUTEdit'];
+               if ( !$userTalkEditAllowed &&
+                       $isPartialBlock &&
+                       !in_array( NS_USER_TALK, explode( "\n", $data['NamespaceRestrictions'] ) )
+               ) {
+                       return [ 'ipb-prevent-user-talk-edit' ];
+               }
+
                # Create block object.
                $block = new DatabaseBlock();
                $block->setTarget( $target );
@@ -867,7 +875,7 @@ class SpecialBlock extends FormSpecialPage {
                $block->setReason( $data['Reason'][0] );
                $block->setExpiry( $expiryTime );
                $block->isCreateAccountBlocked( $data['CreateAccount'] );
-               $block->isUsertalkEditAllowed( !$wgBlockAllowsUTEdit || !$data['DisableUTEdit'] );
+               $block->isUsertalkEditAllowed( $userTalkEditAllowed );
                $block->isEmailBlocked( $data['DisableEmail'] );
                $block->isHardblock( $data['HardBlock'] );
                $block->isAutoblocking( $data['AutoBlock'] );
index 01e1007..51b9846 100644 (file)
        "block-log-flags-angry-autoblock": "enhanced autoblock enabled",
        "block-log-flags-hiddenname": "username hidden",
        "range_block_disabled": "The administrator ability to create range blocks is disabled.",
+       "ipb-prevent-user-talk-edit": "Editing their own talk page must be allowed for a partial block, unless it includes a restriction on the User Talk namespace.",
        "ipb_expiry_invalid": "Expiry time invalid.",
        "ipb_expiry_old": "Expiry time is in the past.",
        "ipb_expiry_temp": "Hidden username blocks must be permanent.",
index 081bb66..6c8942c 100644 (file)
        "block-log-flags-angry-autoblock": "Used as a block log flag in [[Special:Log/block]].\n{{Related|Block-log-flags}}",
        "block-log-flags-hiddenname": "Used as a block log flag in [[Special:Log/block]] and in [[Special:Block]].\n\n{{Related|Block-log-flags}}",
        "range_block_disabled": "Used as error message in [[Special:Block]].\n\nSee also:\n* {{msg-mw|Range block disabled}}\n* {{msg-mw|Ip range invalid}}\n* {{msg-mw|Ip range toolarge}}",
+       "ipb-prevent-user-talk-edit": "Used as error message in [[Special:Block]] if invalid options are selected regarding \"Edit own user talk\".\n\nSee also:\n{{msg-mw|ipb-disableusertalk}}",
        "ipb_expiry_invalid": "Used as error message in [[Special:Block]].",
        "ipb_expiry_old": "Used as error message in [[Special:Block]], if the expiry time is in the past.\n{{Identical|protect_expiry_old}}",
        "ipb_expiry_temp": "Warning message displayed on [[Special:Block]] if the option \"hide username\" is selected but the expiry time is not infinite.",
index b29d333..c2917b6 100644 (file)
@@ -265,6 +265,7 @@ class ApiBlockTest extends ApiTestCase {
                        'partial' => true,
                        'pagerestrictions' => $title,
                        'namespacerestrictions' => $namespace,
+                       'allowusertalk' => true,
                ] );
 
                $block = DatabaseBlock::newFromTarget( $this->mUser->getName() );
index 86e3295..5f11a01 100644 (file)
@@ -391,6 +391,78 @@ class SpecialBlockTest extends SpecialPageTestBase {
                $this->assertSame( 0, $count );
        }
 
+       /**
+        * @dataProvider provideProcessFormErrors
+        * @covers ::processForm()
+        */
+       public function testProcessFormErrors( $data, $expected, $config = [] ) {
+               $defaultConfig = [
+                       'wgEnablePartialBlocks' => true,
+                       'wgBlockAllowsUTEdit' => true,
+               ];
+
+               $this->setMwGlobals( array_merge( $defaultConfig, $config ) );
+
+               $defaultData = [
+                       'Target' => '1.2.3.4',
+                       'Expiry' => 'infinity',
+                       'Reason' => [ 'bad reason' ],
+                       'Confirm' => false,
+                       'PageRestrictions' => '',
+                       'NamespaceRestrictions' => '',
+               ];
+
+               $context = RequestContext::getMain();
+               $page = $this->newSpecialPage();
+               $result = $page->processForm( array_merge( $defaultData, $data ), $context );
+
+               $this->assertEquals( $result[0], $expected );
+       }
+
+       public function provideProcessFormErrors() {
+               return [
+                       'Invalid expiry' => [
+                               [
+                                       'Expiry' => 'invalid',
+                               ],
+                               'ipb_expiry_invalid',
+                       ],
+                       'Expiry is in the past' => [
+                               [
+                                       'Expiry' => 'yesterday',
+                               ],
+                               'ipb_expiry_old',
+                       ],
+                       'HideUser with wrong permissions' => [
+                               [
+                                       'HideUser' => 1,
+                               ],
+                               'badaccess-group0',
+                       ],
+                       'Bad ip address' => [
+                               [
+                                       'Target' => '1.2.3.4/1234',
+                               ],
+                               'badipaddress',
+                       ],
+                       'Edit user talk page invalid with no restrictions' => [
+                               [
+                                       'EditingRestriction' => 'partial',
+                                       'DisableUTEdit' => 1,
+                               ],
+                               'ipb-prevent-user-talk-edit',
+                       ],
+                       'Edit user talk page invalid with namespace restriction != NS_USER_TALK ' => [
+                               [
+                                       'EditingRestriction' => 'partial',
+                                       'DisableUTEdit' => 1,
+                                       'NamespaceRestrictions' => NS_USER
+                               ],
+                               'ipb-prevent-user-talk-edit',
+                       ],
+               ];
+       }
+
        /**
         * @dataProvider provideCheckUnblockSelf
         * @covers ::checkUnblockSelf