Merge "API: Avoid duplicate logs to api-feature-usage"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 5 Mar 2019 23:14:19 +0000 (23:14 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 5 Mar 2019 23:14:19 +0000 (23:14 +0000)
includes/specialpage/FormSpecialPage.php
tests/common/TestsAutoLoader.php
tests/phpunit/includes/specialpage/FormSpecialPageTestCase.php [new file with mode: 0644]
tests/phpunit/includes/specials/SpecialPasswordResetTest.php [new file with mode: 0644]

index 81a0036..d1c6aea 100644 (file)
@@ -203,9 +203,11 @@ abstract class FormSpecialPage extends SpecialPage {
        protected function checkExecutePermissions( User $user ) {
                $this->checkPermissions();
 
-               if ( $this->requiresUnblock() && $user->isBlocked() ) {
+               if ( $this->requiresUnblock() ) {
                        $block = $user->getBlock();
-                       throw new UserBlockedError( $block );
+                       if ( $block && $block->isSitewide() ) {
+                               throw new UserBlockedError( $block );
+                       }
                }
 
                if ( $this->requiresWrite() ) {
index f742a1b..153af50 100644 (file)
@@ -156,6 +156,7 @@ $wgAutoloadClasses += [
        # tests/phpunit/includes/specialpage
        'SpecialPageTestHelper' => "$testDir/phpunit/includes/specialpage/SpecialPageTestHelper.php",
        'AbstractChangesListSpecialPageTestCase' => "$testDir/phpunit/includes/specialpage/AbstractChangesListSpecialPageTestCase.php",
+       'FormSpecialPageTestCase' => "$testDir/phpunit/includes/specialpage/FormSpecialPageTestCase.php",
 
        # tests/phpunit/includes/specials
        'SpecialPageTestBase' => "$testDir/phpunit/includes/specials/SpecialPageTestBase.php",
diff --git a/tests/phpunit/includes/specialpage/FormSpecialPageTestCase.php b/tests/phpunit/includes/specialpage/FormSpecialPageTestCase.php
new file mode 100644 (file)
index 0000000..a3b5adb
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+/**
+ * Factory for handling the special page list and generating SpecialPage objects.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @group SpecialPage
+ */
+abstract class FormSpecialPageTestCase extends SpecialPageTestBase {
+
+       /**
+        * @covers FormSpecialPage::checkExecutePermissions
+        */
+       public function testCheckExecutePermissionsSitewideBlock() {
+               $special = $this->newSpecialPage();
+               $checkExecutePermissions = $this->getMethod( $special, 'checkExecutePermissions' );
+
+               $user = clone $this->getTestUser()->getUser();
+               $user->mBlockedby = $user->getName();
+               $user->mBlock = new Block( [
+                       'address' => '127.0.8.1',
+                       'by' => $user->getId(),
+                       'reason' => 'sitewide block',
+                       'timestamp' => time(),
+                       'sitewide' => true,
+                       'expiry' => 10,
+               ] );
+
+               $this->expectException( UserBlockedError::class );
+               $checkExecutePermissions( $user );
+       }
+
+       /**
+        * @covers FormSpecialPage::checkExecutePermissions
+        */
+       public function testCheckExecutePermissionsPartialBlock() {
+               $special = $this->newSpecialPage();
+               $checkExecutePermissions = $this->getMethod( $special, 'checkExecutePermissions' );
+
+               $user = clone $this->getTestUser()->getUser();
+               $user->mBlockedby = $user->getName();
+               $user->mBlock = new Block( [
+                       'address' => '127.0.8.1',
+                       'by' => $user->getId(),
+                       'reason' => 'partial block',
+                       'timestamp' => time(),
+                       'sitewide' => false,
+                       'expiry' => 10,
+               ] );
+
+               $this->assertNull( $checkExecutePermissions( $user ) );
+       }
+
+       /**
+        * Get a protected/private method.
+        *
+        * @param object $obj
+        * @param string $name
+        * @return callable
+        */
+       protected function getMethod( $obj, $name ) {
+               $method = new ReflectionMethod( $obj, $name );
+               $method->setAccessible( true );
+               return $method->getClosure( $obj );
+       }
+}
diff --git a/tests/phpunit/includes/specials/SpecialPasswordResetTest.php b/tests/phpunit/includes/specials/SpecialPasswordResetTest.php
new file mode 100644 (file)
index 0000000..273b428
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+class SpecialPasswordResetTest extends FormSpecialPageTestCase {
+       /**
+        * {@inheritdoc}
+        */
+       protected function newSpecialPage() {
+               return new SpecialPasswordReset();
+       }
+}