From 1d59041fc86195feaca29d6d263f440a6f17d9b3 Mon Sep 17 00:00:00 2001 From: Ammar Date: Fri, 27 Sep 2019 14:39:15 +0100 Subject: [PATCH] Fix permission check on protection log *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 | 2 +- .../logging/ProtectLogFormatterTest.php | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/includes/logging/ProtectLogFormatter.php b/includes/logging/ProtectLogFormatter.php index 6e3b26b17f..81d9aa2e45 100644 --- a/includes/logging/ProtectLogFormatter.php +++ b/includes/logging/ProtectLogFormatter.php @@ -101,7 +101,7 @@ class ProtectLogFormatter extends LogFormatter { ]; // Show change protection link - if ( !MediaWikiServices::getInstance() + if ( MediaWikiServices::getInstance() ->getPermissionManager() ->userHasRight( $this->context->getUser(), 'protect' ) ) { diff --git a/tests/phpunit/includes/logging/ProtectLogFormatterTest.php b/tests/phpunit/includes/logging/ProtectLogFormatterTest.php index 1c076cab04..7feaa93022 100644 --- a/tests/phpunit/includes/logging/ProtectLogFormatterTest.php +++ b/tests/phpunit/includes/logging/ProtectLogFormatterTest.php @@ -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() ); + } + } } -- 2.20.1