diff options
author | Gergő Tisza <tgr.huwiki@gmail.com> | 2019-01-07 00:01:00 -0800 |
---|---|---|
committer | Reedy <reedy@wikimedia.org> | 2023-09-02 11:59:25 +0000 |
commit | 7a21b9a03271d5857f2f059adcaf2b9df4a8355c (patch) | |
tree | 283e8c1dd552f3890b81118c8d58e7c07b1c3249 /tests/phpunit/includes/user/UserGroupManagerTest.php | |
parent | b71cf7de9eb3466a026f8d80f47559d2ba054c16 (diff) | |
download | mediawikicore-7a21b9a03271d5857f2f059adcaf2b9df4a8355c.tar.gz mediawikicore-7a21b9a03271d5857f2f059adcaf2b9df4a8355c.zip |
Add UserGroupManager::getUserPrivilegedGroups()
This moves the core part of wfGetPrivilegedGroups() out of Wikimedia
config and makes it possible to move functionality built on it into
core.
Bug: T208477
Change-Id: I6536ef2909caeed047447e8b6a25831d6f00d827
Diffstat (limited to 'tests/phpunit/includes/user/UserGroupManagerTest.php')
-rw-r--r-- | tests/phpunit/includes/user/UserGroupManagerTest.php | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/phpunit/includes/user/UserGroupManagerTest.php b/tests/phpunit/includes/user/UserGroupManagerTest.php index afb547ab650f..a19fe086e11d 100644 --- a/tests/phpunit/includes/user/UserGroupManagerTest.php +++ b/tests/phpunit/includes/user/UserGroupManagerTest.php @@ -35,6 +35,8 @@ use MediaWiki\User\UserIdentity; use MediaWiki\User\UserIdentityValue; use MediaWiki\Utils\MWTimestamp; use MediaWikiIntegrationTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Rule\InvokedCount; use RequestContext; use SiteConfiguration; use TestLogger; @@ -120,6 +122,20 @@ class UserGroupManagerTest extends MediaWikiIntegrationTestCase { } /** + * Returns a callable that must be called exactly $invokedCount times. + * @param InvokedCount $invokedCount + * @return callable|MockObject + */ + private function countPromise( $invokedCount ) { + $mockHandler = $this->getMockBuilder( \stdClass::class ) + ->addMethods( [ '__invoke' ] ) + ->getMock(); + $mockHandler->expects( $invokedCount ) + ->method( '__invoke' ); + return $mockHandler; + } + + /** * @param UserGroupManager $manager * @param UserIdentity $user * @param string $group @@ -1223,4 +1239,70 @@ class UserGroupManagerTest extends MediaWikiIntegrationTestCase { $manager = $this->getManager( self::CHANGEABLE_GROUPS_TEST_CONFIG ); $this->assertGroupsEquals( $expected, $manager->getGroupsChangeableByGroup( $group ) ); } + + /** + * @covers \MediaWiki\User\UserGroupManager::getUserPrivilegedGroups() + */ + public function testGetUserPrivilegedGroups() { + $this->setMwGlobals( 'wgPrivilegedGroups', [ 'sysop', 'interface-admin', 'bar', 'baz' ] ); + $makeHook = function ( $invocationCount, User $userToMatch, array $groupsToAdd ) { + return function ( $u, &$groups ) use ( $userToMatch, $invocationCount, $groupsToAdd ) { + $invocationCount(); + $this->assertTrue( $userToMatch->equals( $u ) ); + $groups = array_merge( $groups, $groupsToAdd ); + }; + }; + + $manager = $this->getManager(); + + $user = new User; + $user->setName( '*Unregistered 1234' ); + + $this->assertArrayEquals( + [], + $manager->getUserPrivilegedGroups( $user ) + ); + + $user = $this->getTestUser( [ 'sysop', 'bot', 'interface-admin' ] )->getUser(); + + $this->setTemporaryHook( 'UserPrivilegedGroups', + $makeHook( $this->countPromise( $this->once() ), $user, [ 'foo' ] ) ); + $this->setTemporaryHook( 'UserEffectiveGroups', + $makeHook( $this->countPromise( $this->once() ), $user, [ 'bar', 'boom' ] ) ); + $this->assertArrayEquals( + [ 'sysop', 'interface-admin', 'foo', 'bar' ], + $manager->getUserPrivilegedGroups( $user ) + ); + $this->assertArrayEquals( + [ 'sysop', 'interface-admin', 'foo', 'bar' ], + $manager->getUserPrivilegedGroups( $user ) + ); + + $this->setTemporaryHook( 'UserPrivilegedGroups', + $makeHook( $this->countPromise( $this->once() ), $user, [ 'baz' ] ) ); + $this->setTemporaryHook( 'UserEffectiveGroups', + $makeHook( $this->countPromise( $this->once() ), $user, [ 'baz' ] ) ); + $this->assertArrayEquals( + [ 'sysop', 'interface-admin', 'foo', 'bar' ], + $manager->getUserPrivilegedGroups( $user ) + ); + $this->assertArrayEquals( + [ 'sysop', 'interface-admin', 'baz' ], + $manager->getUserPrivilegedGroups( $user, UserGroupManager::READ_NORMAL, true ) + ); + $this->assertArrayEquals( + [ 'sysop', 'interface-admin', 'baz' ], + $manager->getUserPrivilegedGroups( $user ) + ); + + $this->setTemporaryHook( 'UserPrivilegedGroups', static function () { + } ); + $this->setTemporaryHook( 'UserEffectiveGroups', static function () { + } ); + $user = $this->getTestUser( [] )->getUser(); + $this->assertArrayEquals( + [], + $manager->getUserPrivilegedGroups( $user, UserGroupManager::READ_NORMAL, true ) + ); + } } |