3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\MediaWikiServices
;
11 * @covers ApiUserrights
13 class ApiUserrightsTest
extends ApiTestCase
{
15 protected function setUp() {
17 $this->tablesUsed
= array_merge(
19 [ 'change_tag', 'change_tag_def', 'logging' ]
24 * Unsets $wgGroupPermissions['bureaucrat']['userrights'], and sets
25 * $wgAddGroups['bureaucrat'] and $wgRemoveGroups['bureaucrat'] to the
28 * @param array|bool $add Groups bureaucrats should be allowed to add, true for all
29 * @param array|bool $remove Groups bureaucrats should be allowed to remove, true for all
31 protected function setPermissions( $add = [], $remove = [] ) {
32 $this->setGroupPermissions( 'bureaucrat', 'userrights', false );
35 $this->mergeMwGlobalArrayValue( 'wgAddGroups', [ 'bureaucrat' => $add ] );
38 $this->mergeMwGlobalArrayValue( 'wgRemoveGroups', [ 'bureaucrat' => $remove ] );
41 $this->resetServices();
45 * Perform an API userrights request that's expected to be successful.
47 * @param array|string $expectedGroups Group(s) that the user is expected
48 * to have after the API request
49 * @param array $params Array to pass to doApiRequestWithToken(). 'action'
50 * => 'userrights' is implicit. If no 'user' or 'userid' is specified,
51 * we add a 'user' parameter. If no 'add' or 'remove' is specified, we
52 * add 'add' => 'sysop'.
53 * @param User|null $user The user that we're modifying. The user must be
54 * mutable, because we're going to change its groups! null means that
55 * we'll make up our own user to modify, and doesn't make sense if 'user'
56 * or 'userid' is specified in $params.
58 protected function doSuccessfulRightsChange(
59 $expectedGroups = 'sysop', array $params = [], User
$user = null
61 $expectedGroups = (array)$expectedGroups;
62 $params['action'] = 'userrights';
65 $user = $this->getMutableTestUser()->getUser();
68 $this->assertTrue( TestUserRegistry
::isMutable( $user ),
69 'Immutable user passed to doSuccessfulRightsChange!' );
71 if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
72 $params['user'] = $user->getName();
74 if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
75 $params['add'] = 'sysop';
78 $res = $this->doApiRequestWithToken( $params );
80 $user->clearInstanceCache();
81 MediaWikiServices
::getInstance()->getPermissionManager()->invalidateUsersRightsCache();
82 $this->assertSame( $expectedGroups, $user->getGroups() );
84 $this->assertArrayNotHasKey( 'warnings', $res[0] );
88 * Perform an API userrights request that's expected to fail.
90 * @param string $expectedException Expected exception text
91 * @param array $params As for doSuccessfulRightsChange()
92 * @param User|null $user As for doSuccessfulRightsChange(). If there's no
93 * user who will possibly be affected (such as if an invalid username is
94 * provided in $params), pass null.
96 protected function doFailedRightsChange(
97 $expectedException, array $params = [], User
$user = null
99 $params['action'] = 'userrights';
101 $this->setExpectedException( ApiUsageException
::class, $expectedException );
104 // If 'user' or 'userid' is specified and $user was not specified,
105 // the user we're creating now will have nothing to do with the API
106 // request, but that's okay, since we're just testing that it has
108 $user = $this->getMutableTestUser()->getUser();
111 $this->assertTrue( TestUserRegistry
::isMutable( $user ),
112 'Immutable user passed to doFailedRightsChange!' );
114 if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
115 $params['user'] = $user->getName();
117 if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
118 $params['add'] = 'sysop';
120 $expectedGroups = $user->getGroups();
123 $this->doApiRequestWithToken( $params );
125 $user->clearInstanceCache();
126 $this->assertSame( $expectedGroups, $user->getGroups() );
130 public function testAdd() {
131 $this->doSuccessfulRightsChange();
134 public function testBlockedWithUserrights() {
137 $block = new DatabaseBlock( [ 'address' => $wgUser, 'by' => $wgUser->getId(), ] );
141 $this->doSuccessfulRightsChange();
144 $wgUser->clearInstanceCache();
148 public function testBlockedWithoutUserrights() {
149 $user = $this->getTestSysop()->getUser();
151 $this->setPermissions( true, true );
153 $block = new DatabaseBlock( [ 'address' => $user, 'by' => $user->getId() ] );
157 $this->doFailedRightsChange( 'You have been blocked from editing.' );
160 $user->clearInstanceCache();
164 public function testAddMultiple() {
165 $this->doSuccessfulRightsChange(
166 [ 'bureaucrat', 'sysop' ],
167 [ 'add' => 'bureaucrat|sysop' ]
171 public function testTooFewExpiries() {
172 $this->doFailedRightsChange(
173 '2 expiry timestamps were provided where 3 were needed.',
174 [ 'add' => 'sysop|bureaucrat|bot', 'expiry' => 'infinity|tomorrow' ]
178 public function testTooManyExpiries() {
179 $this->doFailedRightsChange(
180 '3 expiry timestamps were provided where 2 were needed.',
181 [ 'add' => 'sysop|bureaucrat', 'expiry' => 'infinity|tomorrow|never' ]
185 public function testInvalidExpiry() {
186 $this->doFailedRightsChange( 'Invalid expiry time', [ 'expiry' => 'yummy lollipops!' ] );
189 public function testMultipleInvalidExpiries() {
190 $this->doFailedRightsChange(
191 'Invalid expiry time "foo".',
192 [ 'add' => 'sysop|bureaucrat', 'expiry' => 'foo|bar' ]
196 public function testWithTag() {
197 ChangeTags
::defineTag( 'custom tag' );
199 $user = $this->getMutableTestUser()->getUser();
201 $this->doSuccessfulRightsChange( 'sysop', [ 'tags' => 'custom tag' ], $user );
203 $dbr = wfGetDB( DB_REPLICA
);
207 [ 'change_tag', 'logging', 'change_tag_def' ],
210 'ct_log_id = log_id',
211 'log_namespace' => NS_USER
,
212 'log_title' => strtr( $user->getName(), ' ', '_' )
215 [ 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ] ]
220 public function testWithoutTagPermission() {
221 ChangeTags
::defineTag( 'custom tag' );
223 $this->setGroupPermissions( 'user', 'applychangetags', false );
224 $this->resetServices();
226 $this->doFailedRightsChange(
227 'You do not have permission to apply change tags along with your changes.',
228 [ 'tags' => 'custom tag' ]
232 public function testNonexistentUser() {
233 $this->doFailedRightsChange(
234 'There is no user by the name "Nonexistent user". Check your spelling.',
235 [ 'user' => 'Nonexistent user' ]
239 public function testWebToken() {
240 $sysop = $this->getTestSysop()->getUser();
241 $user = $this->getMutableTestUser()->getUser();
243 $token = $sysop->getEditToken( $user->getName() );
245 $res = $this->doApiRequest( [
246 'action' => 'userrights',
247 'user' => $user->getName(),
252 $user->clearInstanceCache();
253 $this->assertSame( [ 'sysop' ], $user->getGroups() );
255 $this->assertArrayNotHasKey( 'warnings', $res[0] );
259 * Helper for testCanProcessExpiries that returns a mock ApiUserrights that either can or cannot
260 * process expiries. Although the regular page can process expiries, we use a mock here to
261 * ensure that it's the result of canProcessExpiries() that makes a difference, and not some
262 * error in the way we construct the mock.
264 * @param bool $canProcessExpiries
266 private function getMockForProcessingExpiries( $canProcessExpiries ) {
267 $sysop = $this->getTestSysop()->getUser();
268 $user = $this->getMutableTestUser()->getUser();
270 $token = $sysop->getEditToken( 'userrights' );
272 $main = new ApiMain( new FauxRequest( [
273 'action' => 'userrights',
274 'user' => $user->getName(),
279 $mockUserRightsPage = $this->getMockBuilder( UserrightsPage
::class )
280 ->setMethods( [ 'canProcessExpiries' ] )
282 $mockUserRightsPage->method( 'canProcessExpiries' )->willReturn( $canProcessExpiries );
284 $mockApi = $this->getMockBuilder( ApiUserrights
::class )
285 ->setConstructorArgs( [ $main, 'userrights' ] )
286 ->setMethods( [ 'getUserRightsPage' ] )
288 $mockApi->method( 'getUserRightsPage' )->willReturn( $mockUserRightsPage );
293 public function testCanProcessExpiries() {
294 $mock1 = $this->getMockForProcessingExpiries( true );
295 $this->assertArrayHasKey( 'expiry', $mock1->getAllowedParams() );
297 $mock2 = $this->getMockForProcessingExpiries( false );
298 $this->assertArrayNotHasKey( 'expiry', $mock2->getAllowedParams() );
302 * Tests adding and removing various groups with various permissions.
304 * @dataProvider addAndRemoveGroupsProvider
305 * @param array|null $permissions [ [ $wgAddGroups, $wgRemoveGroups ] ] or null for 'userrights'
306 * to be set in $wgGroupPermissions
307 * @param array $groupsToChange [ [ groups to add ], [ groups to remove ] ]
308 * @param array $expectedGroups Array of expected groups
310 public function testAddAndRemoveGroups(
311 array $permissions = null, array $groupsToChange, array $expectedGroups
313 if ( $permissions !== null ) {
314 $this->setPermissions( $permissions[0], $permissions[1] );
318 'add' => implode( '|', $groupsToChange[0] ),
319 'remove' => implode( '|', $groupsToChange[1] ),
322 // We'll take a bot so we have a group to remove
323 $user = $this->getMutableTestUser( [ 'bot' ] )->getUser();
325 $this->doSuccessfulRightsChange( $expectedGroups, $params, $user );
328 public function addAndRemoveGroupsProvider() {
334 ], 'Add with only remove permission' => [
338 ], 'Add with global remove permission' => [
342 ], 'Simple remove' => [
346 ], 'Remove with only add permission' => [
350 ], 'Remove with global add permission' => [
354 ], 'Add and remove same new group' => [
356 [ [ 'sysop' ], [ 'sysop' ] ],
357 // The userrights code does removals before adds, so it doesn't remove the sysop
358 // group here and only adds it.
360 ], 'Add and remove same existing group' => [
362 [ [ 'bot' ], [ 'bot' ] ],
363 // But here it first removes the existing group and then re-adds it.