Debian Codegraph @

tests/phpunit/unit/includes/Permissions/PermissionManagerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Permissions;
4
5 use MediaWiki\Actions\ActionFactory;
6 use MediaWiki\Block\BlockErrorFormatter;
7 use MediaWiki\Block\BlockManager;
8 use MediaWiki\Config\ServiceOptions;
9 use MediaWiki\HookContainer\HookContainer;
10 use MediaWiki\MainConfigNames;
11 use MediaWiki\Page\RedirectLookup;
12 use MediaWiki\Permissions\GroupPermissionsLookup;
13 use MediaWiki\Permissions\PermissionManager;
14 use MediaWiki\Permissions\PermissionStatus;
15 use MediaWiki\Permissions\RestrictionStore;
16 use MediaWiki\SpecialPage\SpecialPageFactory;
17 use MediaWiki\Tests\Unit\DummyServicesTrait;
18 use MediaWiki\Title\Title;
19 use MediaWiki\Title\TitleFormatter;
20 use MediaWiki\User\TempUser\RealTempUserConfig;
21 use MediaWiki\User\User;
22 use MediaWiki\User\UserFactory;
23 use MediaWiki\User\UserGroupManager;
24 use MediaWiki\User\UserIdentityLookup;
25 use MediaWikiUnitTestCase;
26 use Wikimedia\TestingAccessWrapper;
27
28 /**
29 * For the integration tests, see \MediaWiki\Tests\Integration\Permissions\PermissionManagerTest.
30 *
31 * @author DannyS712
32 * @covers \MediaWiki\Permissions\PermissionManager
33 */
34 class PermissionManagerTest extends MediaWikiUnitTestCase {
35 use DummyServicesTrait;
36
37 private function getPermissionManager( $options = [] ) {
38 $overrideConfig = $options['config'] ?? [];
39 $baseConfig = [
40 MainConfigNames::WhitelistRead => false,
41 MainConfigNames::WhitelistReadRegexp => false,
42 MainConfigNames::EmailConfirmToEdit => false,
43 MainConfigNames::BlockDisablesLogin => false,
44 MainConfigNames::EnablePartialActionBlocks => false,
45 MainConfigNames::GroupPermissions => [],
46 MainConfigNames::RevokePermissions => [],
47 MainConfigNames::GroupInheritsPermissions => [],
48 MainConfigNames::AvailableRights => [],
49 MainConfigNames::NamespaceProtection => [ NS_MEDIAWIKI => 'editinterface' ],
50 MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop' ],
51 MainConfigNames::DeleteRevisionsLimit => false,
52 MainConfigNames::RateLimits => [],
53 MainConfigNames::ImplicitRights => [],
54 ];
55 $config = $overrideConfig + $baseConfig;
56
57 $hookContainer = $options['hookContainer'] ??
58 $this->createMock( HookContainer::class );
59 $redirectLookup = $options['redirectLookup'] ??
60 $this->createMock( RedirectLookup::class );
61 $restrictionStore = $options['restrictionStore'] ??
62 $this->createMock( RestrictionStore::class );
63
64 $permissionManager = new PermissionManager(
65 new ServiceOptions( PermissionManager::CONSTRUCTOR_OPTIONS, $config ),
66 $this->createMock( SpecialPageFactory::class ),
67 $this->getDummyNamespaceInfo(),
68 new GroupPermissionsLookup(
69 new ServiceOptions( GroupPermissionsLookup::CONSTRUCTOR_OPTIONS, $config )
70 ),
71 $this->createMock( UserGroupManager::class ),
72 $this->createMock( BlockManager::class ),
73 $this->createMock( BlockErrorFormatter::class ),
74 $hookContainer,
75 $this->createMock( UserIdentityLookup::class ),
76 $redirectLookup,
77 $restrictionStore,
78 $this->createMock( TitleFormatter::class ),
79 new RealTempUserConfig( [] ),
80 $this->createMock( UserFactory::class ),
81 $this->createMock( ActionFactory::class )
82 );
83
84 return TestingAccessWrapper::newFromObject( $permissionManager );
85 }
86
87 /**
88 * Does not cover the `editmyuserjsredirect` functionality, which is covered
89 * in testCheckUserConfigPermissionsForRedirect
90 *
91 * @dataProvider provideTestCheckUserConfigPermissions
92 * @param string $pageTitle Does not include the namespace prefix
93 * @param array $rights What rights the user should be given
94 * @param string $action
95 * @param string|bool $pageType 'css', 'js', 'json', or false for none of those
96 * @param array $expectedErrors
97 */
98 public function testCheckUserConfigPermissions(
99 string $pageTitle,
100 array $rights,
101 string $action,
102 $pageType,
103 array $expectedErrors
104 ) {
105 $user = $this->createMock( User::class );
106 $user->method( 'getId' )->willReturn( 123 );
107 $user->method( 'getName' )->willReturn( 'NameOfActingUser' );
108
109 $title = $this->createMock( Title::class );
110 $title->method( 'getText' )->willReturn( $pageTitle );
111 $title->method( 'isUserCssConfigPage' )->willReturn( $pageType === 'css' );
112 $title->method( 'isUserJsonConfigPage' )->willReturn( $pageType === 'json' );
113 $title->method( 'isUserJsConfigPage' )->willReturn( $pageType === 'js' );
114
115 $permissionManager = $this->getPermissionManager();
116 // Override user rights
117 $permissionManager->overrideUserRightsForTesting( $user, $rights );
118
119 $result = PermissionStatus::newEmpty();
120 $permissionManager->checkUserConfigPermissions(
121 $action,
122 $user,
123 $result,
124 PermissionManager::RIGOR_QUICK, // unused
125 true, // $short, unused
126 $title
127 );
128 $this->assertEquals( $expectedErrors, $result->toLegacyErrorArray() );
129 }
130
131 public static function provideTestCheckUserConfigPermissions() {
132 yield 'Patrol ignored' => [ 'NameOfActingUser/subpage', [], 'patrol', false, [] ];
133 yield 'Own non-config' => [ 'NameOfActingUser/subpage', [], 'edit', false, [] ];
134 yield 'Other non-config' => [ 'NameOfAnotherUser/subpage', [], 'edit', false, [] ];
135 yield 'Delete other subpage' => [ 'NameOfAnotherUser/subpage', [], 'delete', false, [] ];
136
137 foreach ( [ 'css', 'json', 'js' ] as $type ) {
138 // User editing their own subpages, everything okay
139 // ensure that we don't run the checks for redirects now, those are done separately
140 yield "Own $type with editmyuser*" => [
141 'NameOfActingUser/subpage.' . $type,
142 [ "editmyuser{$type}", 'editmyuserjsredirect' ],
143 'edit',
144 $type,
145 []
146 ];
147
148 // Interface admin editing own subpages, everything okay
149 yield "Own $type with edituser*" => [
150 'NameOfActingUser/subpage.' . $type,
151 [ "edituser{$type}" ],
152 'edit',
153 $type,
154 []
155 ];
156
157 // User with no rights editing own subpages, problematic
158 yield "Own $type with no rights" => [
159 'NameOfActingUser/subpage.' . $type,
160 [],
161 'edit',
162 $type,
163 [ [ "mycustom{$type}protected", 'edit' ] ]
164 ];
165
166 // Interface admin editing other user's subpages, everything okay
167 yield "Other $type with edituser*" => [
168 'NameOfAnotherUser/subpage.' . $type,
169 [ "edituser{$type}" ],
170 'edit',
171 $type,
172 []
173 ];
174
175 // Normal user editing other user's subpages, problematic
176 yield "Other $type with no rights" => [
177 'NameOfAnotherUser/subpage.' . $type,
178 [],
179 'edit',
180 $type,
181 [ [ "custom{$type}protected", 'edit' ] ]
182 ];
183 }
184 }
185
186 /**
187 * @dataProvider provideTestCheckUserConfigPermissionsForRedirect
188 */
189 public function testCheckUserConfigPermissionsForRedirect(
190 bool $canEditOwnRedirect,
191 bool $isRedirect,
192 int $targetNamespace,
193 string $targetText,
194 bool $expectErrors
195 ) {
196 $user = $this->createMock( User::class );
197 $user->method( 'getId' )->willReturn( 123 );
198 $user->method( 'getName' )->willReturn( 'NameOfActingUser' );
199
200 $title = $this->createMock( Title::class );
201 $title->method( 'getText' )->willReturn( 'NameOfActingUser/common.js' );
202 $title->method( 'isUserCssConfigPage' )->willReturn( false );
203 $title->method( 'isUserJsonConfigPage' )->willReturn( false );
204 $title->method( 'isUserJsConfigPage' )->willReturn( true );
205
206 if ( $isRedirect ) {
207 $target = $this->createMock( Title::class );
208 $target->method( 'inNamespace' )
209 ->with( NS_USER )
210 ->willReturn( $targetNamespace === NS_USER );
211
212 $target->method( 'getText' )->willReturn( $targetText );
213 } else {
214 $target = null;
215 }
216
217 $redirectLookup = $this->createMock( RedirectLookup::class );
218 $redirectLookup->method( 'getRedirectTarget' )->with( $title )
219 ->willReturn( $target );
220
221 $permissionManager = $this->getPermissionManager( [
222 'redirectLookup' => $redirectLookup,
223 ] );
224
225 // Override user rights
226 $rights = [ 'editmyuserjs' ];
227 if ( $canEditOwnRedirect ) {
228 $rights[] = 'editmyuserjsredirect';
229 }
230 $permissionManager->overrideUserRightsForTesting( $user, $rights );
231
232 $result = PermissionStatus::newEmpty();
233 $permissionManager->checkUserConfigPermissions(
234 'edit',
235 $user,
236 $result,
237 PermissionManager::RIGOR_QUICK, // unused
238 true, // $short, unused
239 $title
240 );
241 $this->assertEquals(
242 $expectErrors ? [ [ 'mycustomjsredirectprotected', 'edit' ] ] : [],
243 $result->toLegacyErrorArray()
244 );
245 }
246
247 public static function provideTestCheckUserConfigPermissionsForRedirect() {
248 yield 'With `editmyuserjsredirect`' => [ true, true, NS_USER, 'NameOfActingUser/other.js', false ];
249 yield 'Not a redirect' => [ false, false, NS_USER, 'NameOfActingUser/other.js', false ];
250 yield 'Redirect out of user space' => [ false, true, NS_MAIN, 'MainPage.js', true ];
251 yield 'Redirect to different user' => [ false, true, NS_USER, 'NameOfAnotherUser/other.js', true ];
252 yield 'Redirect to own subpage' => [ false, true, NS_USER, 'NameOfActingUser/other.js', false ];
253 }
254
255 /**
256 * @dataProvider provideTestCheckPageRestrictions
257 */
258 public function testCheckPageRestrictions(
259 string $action,
260 array $restrictions,
261 array $rights,
262 bool $cascading,
263 array $expectedErrors
264 ) {
265 $user = $this->createMock( User::class );
266 $user->method( 'getId' )->willReturn( 123 );
267 $user->method( 'getName' )->willReturn( 'NameOfActingUser' );
268
269 $title = $this->createMock( Title::class );
270
271 $restrictionStore = $this->createMock( RestrictionStore::class );
272 $restrictionStore->expects( $this->any() )
273 ->method( 'listApplicableRestrictionTypes' )
274 ->with( $title )
275 ->willReturn( [ 'other-action', $action ] );
276 $restrictionStore->expects( $this->once() )
277 ->method( 'getRestrictions' )
278 ->with( $title, $action )
279 ->willReturn( $restrictions );
280 $restrictionStore->method( 'areRestrictionsCascading' )->willReturn( $cascading );
281
282 $permissionManager = $this->getPermissionManager( [
283 'restrictionStore' => $restrictionStore,
284 ] );
285 $permissionManager->overrideUserRightsForTesting( $user, $rights );
286
287 $result = PermissionStatus::newEmpty();
288 $permissionManager->checkPageRestrictions(
289 $action,
290 $user,
291 $result,
292 PermissionManager::RIGOR_QUICK, // unused
293 true, // $short, unused
294 $title
295 );
296 $this->assertEquals( $expectedErrors, $result->toLegacyErrorArray() );
297 }
298
299 public static function provideTestCheckPageRestrictions() {
300 yield 'No restrictions' => [ 'move', [], [], true, [] ];
301 yield 'Empty string' => [ 'edit', [ '' ], [], true, [] ];
302 yield 'Semi-protected, with rights' => [
303 'edit',
304 [ 'autoconfirmed' ],
305 [ 'editsemiprotected' ],
306 false,
307 []
308 ];
309 yield 'Sysop protected, no rights' => [
310 'edit',
311 [ 'sysop', ],
312 [],
313 false,
314 [ [ 'protectedpagetext', 'editprotected', 'edit' ] ]
315 ];
316 yield 'Sysop protected and cascading, no protect' => [
317 'edit',
318 [ 'editprotected' ],
319 [ 'editprotected' ],
320 true,
321 [ [ 'protectedpagetext', 'protect', 'edit' ] ]
322 ];
323 yield 'Sysop protected and cascading, with rights' => [
324 'edit',
325 [ 'editprotected' ],
326 [ 'editprotected', 'protect' ],
327 true,
328 []
329 ];
330 }
331
332 /**
333 * @dataProvider provideTestCheckQuickPermissions
334 */
335 public function testCheckQuickPermissions(
336 int $namespace,
337 string $pageTitle,
338 string $userType,
339 string $action,
340 array $rights,
341 string $expectedError
342 ) {
343 // Convert string single error to the array of errors PermissionManager uses
344 $expectedErrors = ( $expectedError === '' ? [] : [ [ $expectedError ] ] );
345
346 $userIsAnon = $userType === 'anon';
347 $userIsTemp = $userType === 'temp';
348 $userIsNamed = $userType === 'user';
349 $user = $this->createMock( User::class );
350 $user->method( 'getId' )->willReturn( $userIsAnon ? 0 : 123 );
351 $user->method( 'getName' )->willReturn( $userIsAnon ? '1.1.1.1' : 'NameOfActingUser' );
352 $user->method( 'isAnon' )->willReturn( $userIsAnon );
353 $user->method( 'isNamed' )->willReturn( $userIsNamed );
354 $user->method( 'isTemp' )->willReturn( $userIsTemp );
355
356 // HookContainer - always return true (false tested separately)
357 $hookContainer = $this->createMock( HookContainer::class );
358 $hookContainer->method( 'run' )
359 ->willReturn( true );
360
361 // Overrides needed in case `GroupPermissionsLookup::groupHasPermission` is called
362 $config = [
363 MainConfigNames::GroupPermissions => [
364 'autoconfirmed' => [
365 'move' => true
366 ]
367 ]
368 ];
369
370 $permissionManager = $this->getPermissionManager( [
371 'config' => $config,
372 'hookContainer' => $hookContainer,
373 ] );
374 $permissionManager->overrideUserRightsForTesting( $user, $rights );
375
376 $title = $this->createMock( Title::class );
377 $title->method( 'getNamespace' )->willReturn( $namespace );
378 $title->method( 'getText' )->willReturn( $pageTitle );
379
380 // Ensure that `missingPermissionError` doesn't call User::newFatalPermissionDeniedStatus
381 // which uses the global state
382 $short = true;
383
384 $result = PermissionStatus::newEmpty();
385 $permissionManager->checkQuickPermissions(
386 $action,
387 $user,
388 $result,
389 PermissionManager::RIGOR_QUICK, // unused
390 $short,
391 $title
392 );
393 $this->assertEquals( $expectedErrors, $result->toLegacyErrorArray() );
394 }
395
396 public static function provideTestCheckQuickPermissions() {
397 // $namespace, $pageTitle, $userIsAnon, $action, $rights, $expectedError
398
399 // Four different possible errors when trying to create
400 yield 'Anon createtalk fail' => [
401 NS_TALK, 'Example', 'anon', 'create', [], 'nocreatetext'
402 ];
403 yield 'Anon createpage fail' => [
404 NS_MAIN, 'Example', 'anon', 'create', [], 'nocreatetext'
405 ];
406 yield 'User createtalk fail' => [
407 NS_TALK, 'Example', 'user', 'create', [], 'nocreate-loggedin'
408 ];
409 yield 'User createpage fail' => [
410 NS_MAIN, 'Example', 'user', 'create', [], 'nocreate-loggedin'
411 ];
412 yield 'Temp user createpage fail' => [
413 NS_MAIN, 'Example', 'temp', 'create', [], 'nocreatetext'
414 ];
415
416 yield 'Createpage pass' => [
417 NS_MAIN, 'Example', 'anon', 'create', [ 'createpage' ], ''
418 ];
419
420 // Three different namespace specific move failures, even if user has `move` rights
421 yield 'Move root user page fail' => [
422 NS_USER, 'Example', 'anon', 'move', [ 'move' ], 'cant-move-user-page'
423 ];
424 yield 'Move file fail' => [
425 NS_FILE, 'Example', 'anon', 'move', [ 'move' ], 'movenotallowedfile'
426 ];
427 yield 'Move category fail' => [
428 NS_CATEGORY, 'Example', 'anon', 'move', [ 'move' ], 'cant-move-category-page'
429 ];
430
431 // No move rights at all. Different failures depending on who is allowed to move.
432 // Test method sets group permissions to [ 'autoconfirmed' => [ 'move' => true ] ]
433 yield 'Anon move fail, autoconfirmed can move' => [
434 NS_TALK, 'Example', 'anon', 'move', [], 'movenologintext'
435 ];
436 yield 'User move fail, autoconfirmed can move' => [
437 NS_TALK, 'Example', 'user', 'move', [], 'movenotallowed'
438 ];
439 yield 'Temp user move fail, autoconfirmed can move' => [
440 NS_TALK, 'Example', 'temp', 'move', [], 'movenologintext'
441 ];
442 yield 'Move pass' => [ NS_MAIN, 'Example', 'anon', 'move', [ 'move' ], '' ];
443
444 // Three different possible failures for move target
445 yield 'Move-target no rights' => [
446 NS_MAIN, 'Example', 'user', 'move-target', [], 'movenotallowed'
447 ];
448 yield 'Move-target to user root' => [
449 NS_USER, 'Example', 'user', 'move-target', [ 'move' ], 'cant-move-to-user-page'
450 ];
451 yield 'Move-target to category' => [
452 NS_CATEGORY, 'Example', 'user', 'move-target', [ 'move' ], 'cant-move-to-category-page'
453 ];
454 yield 'Move-target pass' => [
455 NS_MAIN, 'Example', 'user', 'move-target', [ 'move' ], ''
456 ];
457
458 // Other actions without special handling
459 yield 'Missing rights for edit' => [
460 NS_MAIN, 'Example', 'user', 'edit', [], 'badaccess-group0'
461 ];
462 yield 'Having rights for edit' => [
463 NS_MAIN, 'Example', 'user', 'edit', [ 'edit', ], ''
464 ];
465 }
466
467 /**
468 * @dataProvider provideTestCheckQuickPermissionsHook
469 */
470 public function testCheckQuickPermissionsHook( array $hookErrors, array $expectedResult ) {
471 $title = $this->createMock( Title::class );
472 $user = $this->createMock( User::class );
473 $action = 'FakeActionGoesHere';
474
475 $hookCallback = function ( $hookTitle, $hookUser, $hookAction, &$errors, $doExpensiveQueries, $short )
476 use ( $user, $title, $action, $hookErrors )
477 {
478 $this->assertSame( $title, $hookTitle );
479 $this->assertSame( $user, $hookUser );
480 $this->assertSame( $action, $hookAction );
481 $errors = $hookErrors;
482 return false;
483 };
484
485 $hookContainer = $this->createHookContainer( [ 'TitleQuickPermissions' => $hookCallback ] );
486
487 $permissionManager = $this->getPermissionManager( [
488 'hookContainer' => $hookContainer,
489 ] );
490 $result = PermissionStatus::newEmpty();
491 $permissionManager->checkQuickPermissions(
492 $action,
493 $user,
494 $result,
495 PermissionManager::RIGOR_QUICK, // unused
496 true, // $short, unused,
497 $title
498 );
499 $this->assertEquals(
500 $expectedResult,
501 $result->toLegacyErrorArray()
502 );
503 }
504
505 public function provideTestCheckQuickPermissionsHook() {
506 // test name => [ $result / $errors, $status->toLegacyErrorArray() ]
507 yield 'Hook returns false but no errors' => [ [], [] ];
508 yield 'One error' => [ [ 'error-key' ], [ [ 'error-key' ] ] ];
509 yield 'One error with params as array' => [ [ 'error-key', 'param' ], [ [ 'error-key', 'param' ] ] ];
510 yield 'Multiple errors' => [ [ [ 'error-key' ], [ 'error-key-2' ] ], [ [ 'error-key' ], [ 'error-key-2' ] ] ];
511 }
512
513 }