Fix permission check on protection log
authorAmmar <ammarpad@yahoo.com>
Fri, 27 Sep 2019 13:39:15 +0000 (14:39 +0100)
committerPetr Pchelko <ppchelko@wikimedia.org>
Fri, 27 Sep 2019 16:58:41 +0000 (09:58 -0700)
*I2341e6f inverted the permission check, such that now the link is only shown to unprivileged users

Bug: T234017
Change-Id: I0977f1ab1a72840303aeca2367a30546d83117d4

includes/logging/ProtectLogFormatter.php
tests/phpunit/includes/logging/ProtectLogFormatterTest.php

index 6e3b26b..81d9aa2 100644 (file)
@@ -101,7 +101,7 @@ class ProtectLogFormatter extends LogFormatter {
                ];
 
                // Show change protection link
-               if ( !MediaWikiServices::getInstance()
+               if ( MediaWikiServices::getInstance()
                                ->getPermissionManager()
                                ->userHasRight( $this->context->getUser(), 'protect' )
                ) {
index 1c076ca..7feaa93 100644 (file)
@@ -428,4 +428,46 @@ class ProtectLogFormatterTest extends LogFormatterTestCase {
        public function testMoveProtLogDatabaseRows( $row, $extra ) {
                $this->doTestLogFormatter( $row, $extra );
        }
+
+       public function provideGetActionLinks() {
+               yield [
+                       [ 'protect' ],
+                       true
+               ];
+               yield [
+                       [],
+                       false
+               ];
+       }
+
+       /**
+        * @param string[] $permissions
+        * @param bool $shouldMatch
+        * @dataProvider provideGetActionLinks
+        * @covers ProtectLogFormatter::getActionLinks
+        */
+       public function testGetActionLinks( array $permissions, $shouldMatch ) {
+               RequestContext::resetMain();
+               $user = $this->getTestUser()->getUser();
+               $this->overrideUserPermissions( $user, $permissions );
+               $row = $this->expandDatabaseRow( [
+                       'type' => 'protect',
+                       'action' => 'unprotect',
+                       'comment' => 'unprotect comment',
+                       'namespace' => NS_MAIN,
+                       'title' => 'ProtectPage',
+                       'params' => [],
+               ], false );
+               $context = new RequestContext();
+               $context->setUser( $user );
+               $formatter = LogFormatter::newFromRow( $row );
+               $formatter->setContext( $context );
+               if ( $shouldMatch ) {
+                       $this->assertStringMatchesFormat(
+                               '%Aaction=protect%A', $formatter->getActionLinks() );
+               } else {
+                       $this->assertStringNotMatchesFormat(
+                               '%Aaction=protect%A', $formatter->getActionLinks() );
+               }
+       }
 }