Debian Codegraph @

tests/phpunit/includes/api/ApiUserrightsTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use MediaWiki\Block\DatabaseBlock;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\MainConfigSchema;
8 use MediaWiki\Title\Title;
9 use MediaWiki\User\User;
10 use TestUserRegistry;
11
12 /**
13 * @group API
14 * @group Database
15 * @group medium
16 *
17 * @covers MediaWiki\Api\ApiUserrights
18 */
19 class ApiUserrightsTest extends ApiTestCase {
20
21 protected function setUp(): void {
22 parent::setUp();
23
24 $this->overrideConfigValues( [
25 MainConfigNames::AddGroups => [],
26 MainConfigNames::RemoveGroups => [],
27 ] );
28 }
29
30 /**
31 * Unsets $wgGroupPermissions['bureaucrat']['userrights'], and sets
32 * $wgAddGroups['bureaucrat'] and $wgRemoveGroups['bureaucrat'] to the
33 * specified values.
34 *
35 * @param array|bool $add Groups bureaucrats should be allowed to add, true for all
36 * @param array|bool $remove Groups bureaucrats should be allowed to remove, true for all
37 */
38 protected function setPermissions( $add = [], $remove = [] ) {
39 $this->setGroupPermissions( 'bureaucrat', 'userrights', false );
40
41 if ( $add ) {
42 $this->overrideConfigValue(
43 MainConfigNames::AddGroups,
44 [ 'bureaucrat' => $add ] + MainConfigSchema::getDefaultValue( MainConfigNames::AddGroups )
45 );
46 }
47 if ( $remove ) {
48 $this->overrideConfigValue(
49 MainConfigNames::RemoveGroups,
50 [ 'bureaucrat' => $remove ] + MainConfigSchema::getDefaultValue( MainConfigNames::RemoveGroups )
51 );
52 }
53 }
54
55 /**
56 * Perform an API userrights request that's expected to be successful.
57 *
58 * @param array|string $expectedGroups Group(s) that the user is expected
59 * to have after the API request
60 * @param array $params Array to pass to doApiRequestWithToken(). 'action'
61 * => 'userrights' is implicit. If no 'user' or 'userid' is specified,
62 * we add a 'user' parameter. If no 'add' or 'remove' is specified, we
63 * add 'add' => 'sysop'.
64 * @param User|null $user The user that we're modifying. The user must be
65 * mutable, because we're going to change its groups! null means that
66 * we'll make up our own user to modify, and doesn't make sense if 'user'
67 * or 'userid' is specified in $params.
68 */
69 protected function doSuccessfulRightsChange(
70 $expectedGroups = 'sysop', array $params = [], ?User $user = null
71 ) {
72 $expectedGroups = (array)$expectedGroups;
73 $params['action'] = 'userrights';
74
75 if ( !$user ) {
76 $user = $this->getMutableTestUser()->getUser();
77 }
78
79 $this->assertTrue( TestUserRegistry::isMutable( $user ),
80 'Immutable user passed to doSuccessfulRightsChange!' );
81
82 if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
83 $params['user'] = $user->getName();
84 }
85 if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
86 $params['add'] = 'sysop';
87 }
88
89 $res = $this->doApiRequestWithToken( $params );
90
91 $user->clearInstanceCache();
92 $this->getServiceContainer()->getPermissionManager()->invalidateUsersRightsCache();
93 $this->assertSame(
94 $expectedGroups, $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user )
95 );
96
97 $this->assertArrayNotHasKey( 'warnings', $res[0] );
98 }
99
100 /**
101 * Perform an API userrights request that's expected to fail.
102 *
103 * @param string $expectedCode Expected API error code
104 * @param array $params As for doSuccessfulRightsChange()
105 * @param User|null $user As for doSuccessfulRightsChange(). If there's no
106 * user who will possibly be affected (such as if an invalid username is
107 * provided in $params), pass null.
108 */
109 private function doFailedRightsChange(
110 $expectedCode, array $params = [], ?User $user = null
111 ) {
112 $params['action'] = 'userrights';
113 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
114
115 $this->expectApiErrorCode( $expectedCode );
116
117 if ( !$user ) {
118 // If 'user' or 'userid' is specified and $user was not specified,
119 // the user we're creating now will have nothing to do with the API
120 // request, but that's okay, since we're just testing that it has
121 // no groups.
122 $user = $this->getMutableTestUser()->getUser();
123 }
124
125 $this->assertTrue( TestUserRegistry::isMutable( $user ),
126 'Immutable user passed to doFailedRightsChange!' );
127
128 if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
129 $params['user'] = $user->getName();
130 }
131 if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
132 $params['add'] = 'sysop';
133 }
134 $expectedGroups = $userGroupManager->getUserGroups( $user );
135
136 try {
137 $this->doApiRequestWithToken( $params );
138 } finally {
139 $user->clearInstanceCache();
140 $this->assertSame( $expectedGroups, $userGroupManager->getUserGroups( $user ) );
141 }
142 }
143
144 public function testAdd() {
145 $this->doSuccessfulRightsChange();
146 }
147
148 public function testBlockedWithUserrights() {
149 $user = $this->getTestSysop()->getUser();
150
151 $block = new DatabaseBlock( [ 'address' => $user, 'by' => $user, ] );
152 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
153 $blockStore->insertBlock( $block );
154
155 $this->doSuccessfulRightsChange();
156 }
157
158 public function testBlockedWithoutUserrights() {
159 $user = $this->getTestSysop()->getUser();
160
161 $this->setPermissions( true, true );
162
163 $block = new DatabaseBlock( [ 'address' => $user, 'by' => $user ] );
164 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
165 $blockStore->insertBlock( $block );
166
167 $this->doFailedRightsChange( 'blocked' );
168 }
169
170 public function testAddMultiple() {
171 $this->doSuccessfulRightsChange(
172 [ 'bureaucrat', 'sysop' ],
173 [ 'add' => 'bureaucrat|sysop' ]
174 );
175 }
176
177 public function testTooFewExpiries() {
178 $this->doFailedRightsChange(
179 'toofewexpiries',
180 [ 'add' => 'sysop|bureaucrat|bot', 'expiry' => 'infinity|tomorrow' ]
181 );
182 }
183
184 public function testTooManyExpiries() {
185 $this->doFailedRightsChange(
186 'toofewexpiries',
187 [ 'add' => 'sysop|bureaucrat', 'expiry' => 'infinity|tomorrow|never' ]
188 );
189 }
190
191 public function testInvalidExpiry() {
192 $this->doFailedRightsChange( 'invalidexpiry', [ 'expiry' => 'yummy lollipops!' ] );
193 }
194
195 public function testMultipleInvalidExpiries() {
196 $this->doFailedRightsChange(
197 'invalidexpiry',
198 [ 'add' => 'sysop|bureaucrat', 'expiry' => 'foo|bar' ]
199 );
200 }
201
202 public function testWithTag() {
203 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
204
205 $user = $this->getMutableTestUser()->getUser();
206
207 $this->doSuccessfulRightsChange( 'sysop', [ 'tags' => 'custom tag' ], $user );
208
209 $this->assertSame(
210 'custom tag',
211 $this->getDb()->newSelectQueryBuilder()
212 ->select( 'ctd_name' )
213 ->from( 'logging' )
214 ->join( 'change_tag', null, 'ct_log_id = log_id' )
215 ->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
216 ->where( [ 'log_namespace' => NS_USER, 'log_title' => strtr( $user->getName(), ' ', '_' ) ] )
217 ->caller( __METHOD__ )->fetchField() );
218 }
219
220 public function testWithoutTagPermission() {
221 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
222
223 $this->setGroupPermissions( 'user', 'applychangetags', false );
224
225 $this->doFailedRightsChange(
226 'tags-apply-no-permission',
227 [ 'tags' => 'custom tag' ]
228 );
229 }
230
231 public function testNonexistentUser() {
232 $this->doFailedRightsChange(
233 'nosuchuser',
234 [ 'user' => 'Nonexistent user' ]
235 );
236 }
237
238 public function testWebToken() {
239 $sysop = $this->getTestSysop()->getUser();
240 $user = $this->getMutableTestUser()->getUser();
241
242 $token = $sysop->getEditToken( $user->getName() );
243
244 $res = $this->doApiRequest( [
245 'action' => 'userrights',
246 'user' => $user->getName(),
247 'add' => 'sysop',
248 'token' => $token,
249 ] );
250
251 $user->clearInstanceCache();
252 $this->assertSame( [ 'sysop' ], $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user ) );
253
254 $this->assertArrayNotHasKey( 'warnings', $res[0] );
255 }
256
257 /**
258 * Tests adding and removing various groups with various permissions.
259 *
260 * @dataProvider addAndRemoveGroupsProvider
261 * @param array|null $permissions [ [ $wgAddGroups, $wgRemoveGroups ] ] or null for 'userrights'
262 * to be set in $wgGroupPermissions
263 * @param array $groupsToChange [ [ groups to add ], [ groups to remove ] ]
264 * @param array $expectedGroups Array of expected groups
265 */
266 public function testAddAndRemoveGroups(
267 ?array $permissions, array $groupsToChange, array $expectedGroups
268 ) {
269 if ( $permissions !== null ) {
270 $this->setPermissions( $permissions[0], $permissions[1] );
271 }
272
273 $params = [
274 'add' => implode( '|', $groupsToChange[0] ),
275 'remove' => implode( '|', $groupsToChange[1] ),
276 ];
277
278 // We'll take a bot so we have a group to remove
279 $user = $this->getMutableTestUser( [ 'bot' ] )->getUser();
280
281 $this->doSuccessfulRightsChange( $expectedGroups, $params, $user );
282 }
283
284 public static function addAndRemoveGroupsProvider() {
285 return [
286 'Simple add' => [
287 [ [ 'sysop' ], [] ],
288 [ [ 'sysop' ], [] ],
289 [ 'bot', 'sysop' ]
290 ], 'Add with only remove permission' => [
291 [ [], [ 'sysop' ] ],
292 [ [ 'sysop' ], [] ],
293 [ 'bot' ],
294 ], 'Add with global remove permission' => [
295 [ [], true ],
296 [ [ 'sysop' ], [] ],
297 [ 'bot' ],
298 ], 'Simple remove' => [
299 [ [], [ 'bot' ] ],
300 [ [], [ 'bot' ] ],
301 [],
302 ], 'Remove with only add permission' => [
303 [ [ 'bot' ], [] ],
304 [ [], [ 'bot' ] ],
305 [ 'bot' ],
306 ], 'Remove with global add permission' => [
307 [ true, [] ],
308 [ [], [ 'bot' ] ],
309 [ 'bot' ],
310 ], 'Add and remove same new group' => [
311 null,
312 [ [ 'sysop' ], [ 'sysop' ] ],
313 // The userrights code does removals before adds, so it doesn't remove the sysop
314 // group here and only adds it.
315 [ 'bot', 'sysop' ],
316 ], 'Add and remove same existing group' => [
317 null,
318 [ [ 'bot' ], [ 'bot' ] ],
319 // But here it first removes the existing group and then re-adds it.
320 [ 'bot' ],
321 ],
322 ];
323 }
324
325 public function testWatched() {
326 $user = $this->getMutableTestUser()->getUser();
327 $userPage = Title::makeTitle( NS_USER, $user->getName() );
328 $this->doSuccessfulRightsChange( 'sysop', [ 'watchuser' => true ], $user );
329 $this->assertTrue( $this->getServiceContainer()->getWatchlistManager()
330 ->isWatched( $this->getTestSysop()->getUser(), $userPage ) );
331 }
332
333 /**
334 * T422085: Verify that cross-wiki user lookup is blocked
335 * when the performer lacks the 'userrights-interwiki' permission.
336 */
337 public function testInterwikiLookupWithoutPermission() {
338 $this->setGroupPermissions( 'sysop', 'userrights-interwiki', false );
339
340 $this->doFailedRightsChange(
341 'permissiondenied',
342 [ 'user' => 'SomeUser@remotewiki' ]
343 );
344 }
345 }