From aafae4614dc904ff5d73dd8797b756d6df19e451 Mon Sep 17 00:00:00 2001 From: Hamish Date: Mon, 27 Feb 2023 20:53:15 +0100 Subject: [PATCH] Replace removed User::getRights (#206) * Replace removed User::getRights Deprecated since 1.34 [master] * Replace removed User::getRights Deprecated since 1.34 [master] * Replace removed User::getRights Deprecated since 1.34 [master] * Replace removed User::getRights Deprecated since 1.34 [master] * Replace removed User::getRights Deprecated since 1.34 [master] --- .../PageContributorsPropertyAnnotator.php | 10 +++++++++- .../UserRightPropertyAnnotator.php | 10 +++++++++- .../PageContributorsPropertyAnnotatorTest.php | 15 +++++++++++---- .../UserRightPropertyAnnotatorTest.php | 15 +++++++++++---- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/PropertyAnnotators/PageContributorsPropertyAnnotator.php b/src/PropertyAnnotators/PageContributorsPropertyAnnotator.php index 9ea3d14b..ba114180 100644 --- a/src/PropertyAnnotators/PageContributorsPropertyAnnotator.php +++ b/src/PropertyAnnotators/PageContributorsPropertyAnnotator.php @@ -2,6 +2,8 @@ namespace SESP\PropertyAnnotators; +use MediaWiki\MediaWikiServices; +use MediaWiki\Permissions\PermissionManager; use SMW\DIProperty; use SMW\DIWikiPage; use SMW\SemanticData; @@ -30,6 +32,7 @@ class PageContributorsPropertyAnnotator implements PropertyAnnotator { * @var AppFactory */ private $appFactory; + private PermissionManager $permissionManager; /** * @since 2.0 @@ -38,6 +41,11 @@ class PageContributorsPropertyAnnotator implements PropertyAnnotator { */ public function __construct( AppFactory $appFactory ) { $this->appFactory = $appFactory; + $this->permissionManager = MediaWikiServices::getInstance()->getPermissionManager(); + } + + public function setPermissionManager( PermissionManager $permissionManager ) { + $this->permissionManager = $permissionManager; } /** @@ -87,7 +95,7 @@ public function addAnnotation( DIProperty $property, SemanticData $semanticData } private function isNotAnonymous( $user ) { - return !( in_array( 'bot', $user->getRights() ) && $this->appFactory->getOption( 'sespgExcludeBotEdits' ) ) && !$user->isAnon(); + return !( in_array( 'bot', $this->permissionManager->getUserPermissions( $user ) ) && $this->appFactory->getOption( 'sespgExcludeBotEdits' ) ) && !$user->isAnon(); } } diff --git a/src/PropertyAnnotators/UserRightPropertyAnnotator.php b/src/PropertyAnnotators/UserRightPropertyAnnotator.php index a9783121..ecbf4ff5 100644 --- a/src/PropertyAnnotators/UserRightPropertyAnnotator.php +++ b/src/PropertyAnnotators/UserRightPropertyAnnotator.php @@ -2,6 +2,8 @@ namespace SESP\PropertyAnnotators; +use MediaWiki\MediaWikiServices; +use MediaWiki\Permissions\PermissionManager; use SMW\DIProperty; use SMW\SemanticData; use SMWDataItem as DataItem; @@ -30,6 +32,7 @@ class UserRightPropertyAnnotator implements PropertyAnnotator { * @var AppFactory */ private $appFactory; + private PermissionManager $permissionManager; /** * @since 2.0 @@ -38,6 +41,11 @@ class UserRightPropertyAnnotator implements PropertyAnnotator { */ public function __construct( AppFactory $appFactory ) { $this->appFactory = $appFactory; + $this->permissionManager = MediaWikiServices::getInstance()->getPermissionManager(); + } + + public function setPermissionManager( PermissionManager $permissionManager ) { + $this->permissionManager = $permissionManager; } /** @@ -70,7 +78,7 @@ public function addAnnotation( DIProperty $property, SemanticData $semanticData return; } - foreach ( $user->getRights() as $right ) { + foreach ( $this->permissionManager->getUserPermissions( $user ) as $right ) { $semanticData->addPropertyObjectValue( $property, new DIBlob( $right ) ); } } diff --git a/tests/phpunit/Unit/PropertyAnnotators/PageContributorsPropertyAnnotatorTest.php b/tests/phpunit/Unit/PropertyAnnotators/PageContributorsPropertyAnnotatorTest.php index 4b3ca825..e3559617 100644 --- a/tests/phpunit/Unit/PropertyAnnotators/PageContributorsPropertyAnnotatorTest.php +++ b/tests/phpunit/Unit/PropertyAnnotators/PageContributorsPropertyAnnotatorTest.php @@ -3,6 +3,7 @@ namespace SESP\Tests\PropertyAnnotators; use ArrayIterator; +use MediaWiki\Permissions\PermissionManager; use SESP\AppFactory; use SESP\PropertyAnnotators\PageContributorsPropertyAnnotator; use SMW\DIProperty; @@ -74,10 +75,6 @@ public function testAddAnnotation() { ->method( 'isAnon' ) ->will( $this->returnValue( false ) ); - $user->expects( $this->once() ) - ->method( 'getRights' ) - ->will( $this->returnValue( [] ) ); - $wikiPage = $this->getMockBuilder( WikiPage::class ) ->disableOriginalConstructor() ->getMock(); @@ -109,10 +106,20 @@ public function testAddAnnotation() { $semanticData->expects( $this->once() ) ->method( 'addPropertyObjectValue' ); + $permissionManager = $this->getMockBuilder( PermissionManager::class ) + ->disableOriginalConstructor() + ->getMock(); + + $permissionManager->expects( $this->once() ) + ->method( 'getUserPermissions' ) + ->will( $this->returnValue( [] ) ); + $instance = new PageContributorsPropertyAnnotator( $this->appFactory ); + $instance->setPermissionManager( $permissionManager ); + $instance->addAnnotation( $this->property, $semanticData ); } diff --git a/tests/phpunit/Unit/PropertyAnnotators/UserRightPropertyAnnotatorTest.php b/tests/phpunit/Unit/PropertyAnnotators/UserRightPropertyAnnotatorTest.php index 7323d0aa..82f7a682 100644 --- a/tests/phpunit/Unit/PropertyAnnotators/UserRightPropertyAnnotatorTest.php +++ b/tests/phpunit/Unit/PropertyAnnotators/UserRightPropertyAnnotatorTest.php @@ -2,6 +2,7 @@ namespace SESP\Tests\PropertyAnnotators; +use MediaWiki\Permissions\PermissionManager; use SESP\PropertyAnnotators\UserRightPropertyAnnotator; use SMW\DIProperty; use SMW\DIWikiPage; @@ -58,10 +59,6 @@ public function testAddAnnotation( $rights, $expected ) { ->disableOriginalConstructor() ->getMock(); - $user->expects( $this->once() ) - ->method( 'getRights' ) - ->will( $this->returnValue( $rights ) ); - $this->appFactory->expects( $this->once() ) ->method( 'newUserFromTitle' ) ->will( $this->returnValue( $user ) ); @@ -93,10 +90,20 @@ public function testAddAnnotation( $rights, $expected ) { $semanticData->expects( $expected ) ->method( 'addPropertyObjectValue' ); + $permissionManager = $this->getMockBuilder( PermissionManager::class ) + ->disableOriginalConstructor() + ->getMock(); + + $permissionManager->expects( $this->once() ) + ->method( 'getUserPermissions' ) + ->will( $this->returnValue( $rights ) ); + $instance = new UserRightPropertyAnnotator( $this->appFactory ); + $instance->setPermissionManager( $permissionManager ); + $instance->addAnnotation( $this->property, $semanticData ); }