3 namespace MediaWiki\Tests\Permissions
;
7 use MediaWikiLangTestCase
;
11 use MediaWiki\Block\Restriction\NamespaceRestriction
;
12 use MediaWiki\Block\Restriction\PageRestriction
;
13 use MediaWiki\Block\SystemBlock
;
14 use MediaWiki\MediaWikiServices
;
15 use MediaWiki\Permissions\PermissionManager
;
20 * @covers \MediaWiki\Permissions\PermissionManager
22 class PermissionManagerTest
extends MediaWikiLangTestCase
{
27 protected $userName, $altUserName;
37 protected $user, $anonUser, $userUser, $altUser;
40 * @var PermissionManager
42 protected $permissionManager;
44 /** Constant for self::testIsBlockedFrom */
45 const USER_TALK_PAGE
= '<user talk page>';
47 protected function setUp() {
51 $localOffset = date( 'Z' ) / 60;
53 $this->setMwGlobals( [
54 'wgLocaltimezone' => $localZone,
55 'wgLocalTZoffset' => $localOffset,
56 'wgNamespaceProtection' => [
57 NS_MEDIAWIKI
=> 'editinterface',
60 // Without this testUserBlock will use a non-English context on non-English MediaWiki
61 // installations (because of how Title::checkUserBlock is implemented) and fail.
62 RequestContext
::resetMain();
64 $this->userName
= 'Useruser';
65 $this->altUserName
= 'Altuseruser';
66 date_default_timezone_set( $localZone );
68 $this->title
= Title
::makeTitle( NS_MAIN
, "Main Page" );
69 if ( !isset( $this->userUser
) ||
!( $this->userUser
instanceof User
) ) {
70 $this->userUser
= User
::newFromName( $this->userName
);
72 if ( !$this->userUser
->getId() ) {
73 $this->userUser
= User
::createNew( $this->userName
, [
74 "email" => "test@example.com",
75 "real_name" => "Test User" ] );
76 $this->userUser
->load();
79 $this->altUser
= User
::newFromName( $this->altUserName
);
80 if ( !$this->altUser
->getId() ) {
81 $this->altUser
= User
::createNew( $this->altUserName
, [
82 "email" => "alttest@example.com",
83 "real_name" => "Test User Alt" ] );
84 $this->altUser
->load();
87 $this->anonUser
= User
::newFromId( 0 );
89 $this->user
= $this->userUser
;
92 $this->permissionManager
= MediaWikiServices
::getInstance()->getPermissionManager();
94 $this->overrideMwServices();
97 protected function setUserPerm( $perm ) {
98 // Setting member variables is evil!!!
100 if ( is_array( $perm ) ) {
101 $this->user
->mRights
= $perm;
103 $this->user
->mRights
= [ $perm ];
107 protected function setTitle( $ns, $title = "Main_Page" ) {
108 $this->title
= Title
::makeTitle( $ns, $title );
111 protected function setUser( $userName = null ) {
112 if ( $userName === 'anon' ) {
113 $this->user
= $this->anonUser
;
114 } elseif ( $userName === null ||
$userName === $this->userName
) {
115 $this->user
= $this->userUser
;
117 $this->user
= $this->altUser
;
122 * @todo This test method should be split up into separate test methods and
125 * This test is failing per T201776.
128 * @covers \MediaWiki\Permissions\PermissionManager::checkQuickPermissions
130 public function testQuickPermissions() {
131 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
132 getFormattedNsText( NS_PROJECT
);
134 $this->setUser( 'anon' );
135 $this->setTitle( NS_TALK
);
136 $this->setUserPerm( "createtalk" );
137 $res = $this->permissionManager
138 ->getPermissionErrors( 'create', $this->user
, $this->title
);
139 $this->assertEquals( [], $res );
141 $this->setTitle( NS_TALK
);
142 $this->setUserPerm( "createpage" );
143 $res = $this->permissionManager
144 ->getPermissionErrors( 'create', $this->user
, $this->title
);
145 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
147 $this->setTitle( NS_TALK
);
148 $this->setUserPerm( "" );
149 $res = $this->permissionManager
150 ->getPermissionErrors( 'create', $this->user
, $this->title
);
151 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
153 $this->setTitle( NS_MAIN
);
154 $this->setUserPerm( "createpage" );
155 $res = $this->permissionManager
156 ->getPermissionErrors( 'create', $this->user
, $this->title
);
157 $this->assertEquals( [], $res );
159 $this->setTitle( NS_MAIN
);
160 $this->setUserPerm( "createtalk" );
161 $res = $this->permissionManager
162 ->getPermissionErrors( 'create', $this->user
, $this->title
);
163 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
165 $this->setUser( $this->userName
);
166 $this->setTitle( NS_TALK
);
167 $this->setUserPerm( "createtalk" );
168 $res = $this->permissionManager
169 ->getPermissionErrors( 'create', $this->user
, $this->title
);
170 $this->assertEquals( [], $res );
172 $this->setTitle( NS_TALK
);
173 $this->setUserPerm( "createpage" );
174 $res = $this->permissionManager
175 ->getPermissionErrors( 'create', $this->user
, $this->title
);
176 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
178 $this->setTitle( NS_TALK
);
179 $this->setUserPerm( "" );
180 $res = $this->permissionManager
181 ->getPermissionErrors( 'create', $this->user
, $this->title
);
182 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
184 $this->setTitle( NS_MAIN
);
185 $this->setUserPerm( "createpage" );
186 $res = $this->permissionManager
187 ->getPermissionErrors( 'create', $this->user
, $this->title
);
188 $this->assertEquals( [], $res );
190 $this->setTitle( NS_MAIN
);
191 $this->setUserPerm( "createtalk" );
192 $res = $this->permissionManager
193 ->getPermissionErrors( 'create', $this->user
, $this->title
);
194 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
196 $this->setTitle( NS_MAIN
);
197 $this->setUserPerm( "" );
198 $res = $this->permissionManager
199 ->getPermissionErrors( 'create', $this->user
, $this->title
);
200 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
202 $this->setUser( 'anon' );
203 $this->setTitle( NS_USER
, $this->userName
. '' );
204 $this->setUserPerm( "" );
205 $res = $this->permissionManager
206 ->getPermissionErrors( 'move', $this->user
, $this->title
);
207 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
209 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
210 $this->setUserPerm( "" );
211 $res = $this->permissionManager
212 ->getPermissionErrors( 'move', $this->user
, $this->title
);
213 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
215 $this->setTitle( NS_USER
, $this->userName
. '' );
216 $this->setUserPerm( "move-rootuserpages" );
217 $res = $this->permissionManager
218 ->getPermissionErrors( 'move', $this->user
, $this->title
);
219 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
221 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
222 $this->setUserPerm( "move-rootuserpages" );
223 $res = $this->permissionManager
224 ->getPermissionErrors( 'move', $this->user
, $this->title
);
225 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
227 $this->setTitle( NS_USER
, $this->userName
. '' );
228 $this->setUserPerm( "" );
229 $res = $this->permissionManager
230 ->getPermissionErrors( 'move', $this->user
, $this->title
);
231 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
233 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
234 $this->setUserPerm( "" );
235 $res = $this->permissionManager
236 ->getPermissionErrors( 'move', $this->user
, $this->title
);
237 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
239 $this->setTitle( NS_USER
, $this->userName
. '' );
240 $this->setUserPerm( "move-rootuserpages" );
241 $res = $this->permissionManager
242 ->getPermissionErrors( 'move', $this->user
, $this->title
);
243 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
245 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
246 $this->setUserPerm( "move-rootuserpages" );
247 $res = $this->permissionManager
248 ->getPermissionErrors( 'move', $this->user
, $this->title
);
249 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
251 $this->setUser( $this->userName
);
252 $this->setTitle( NS_FILE
, "img.png" );
253 $this->setUserPerm( "" );
254 $res = $this->permissionManager
255 ->getPermissionErrors( 'move', $this->user
, $this->title
);
256 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
258 $this->setTitle( NS_FILE
, "img.png" );
259 $this->setUserPerm( "movefile" );
260 $res = $this->permissionManager
261 ->getPermissionErrors( 'move', $this->user
, $this->title
);
262 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
264 $this->setUser( 'anon' );
265 $this->setTitle( NS_FILE
, "img.png" );
266 $this->setUserPerm( "" );
267 $res = $this->permissionManager
268 ->getPermissionErrors( 'move', $this->user
, $this->title
);
269 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
271 $this->setTitle( NS_FILE
, "img.png" );
272 $this->setUserPerm( "movefile" );
273 $res = $this->permissionManager
274 ->getPermissionErrors( 'move', $this->user
, $this->title
);
275 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
277 $this->setUser( $this->userName
);
278 $this->setUserPerm( "move" );
279 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
281 $this->setUserPerm( "" );
282 $this->runGroupPermissions(
284 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
287 $this->setUser( 'anon' );
288 $this->setUserPerm( "move" );
289 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
291 $this->setUserPerm( "" );
292 $this->runGroupPermissions(
294 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
295 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
298 if ( $this->isWikitextNS( NS_MAIN
) ) {
299 // NOTE: some content models don't allow moving
300 // @todo find a Wikitext namespace for testing
302 $this->setTitle( NS_MAIN
);
303 $this->setUser( 'anon' );
304 $this->setUserPerm( "move" );
305 $this->runGroupPermissions( 'move', [] );
307 $this->setUserPerm( "" );
308 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
309 [ [ 'movenologintext' ] ] );
311 $this->setUser( $this->userName
);
312 $this->setUserPerm( "" );
313 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
315 $this->setUserPerm( "move" );
316 $this->runGroupPermissions( 'move', [] );
318 $this->setUser( 'anon' );
319 $this->setUserPerm( 'move' );
320 $res = $this->permissionManager
321 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
322 $this->assertEquals( [], $res );
324 $this->setUserPerm( '' );
325 $res = $this->permissionManager
326 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
327 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
330 $this->setTitle( NS_USER
);
331 $this->setUser( $this->userName
);
332 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
333 $res = $this->permissionManager
334 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
335 $this->assertEquals( [], $res );
337 $this->setUserPerm( "move" );
338 $res = $this->permissionManager
339 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
340 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
342 $this->setUser( 'anon' );
343 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
344 $res = $this->permissionManager
345 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
346 $this->assertEquals( [], $res );
348 $this->setTitle( NS_USER
, "User/subpage" );
349 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
350 $res = $this->permissionManager
351 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
352 $this->assertEquals( [], $res );
354 $this->setUserPerm( "move" );
355 $res = $this->permissionManager
356 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
357 $this->assertEquals( [], $res );
359 $this->setUser( 'anon' );
362 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
363 [ [ 'badaccess-group0' ] ],
370 "[[$prefix:Administrators|Administrators]]", 1 ],
373 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
374 [ [ 'protect-cantedit' ] ],
377 '' => [ [], [], [], true ]
380 foreach ( [ "edit", "protect", "" ] as $action ) {
381 $this->setUserPerm( null );
382 $this->assertEquals( $check[$action][0],
383 $this->permissionManager
384 ->getPermissionErrors( $action, $this->user
, $this->title
, true ) );
385 $this->assertEquals( $check[$action][0],
386 $this->permissionManager
387 ->getPermissionErrors( $action, $this->user
, $this->title
, 'full' ) );
388 $this->assertEquals( $check[$action][0],
389 $this->permissionManager
390 ->getPermissionErrors( $action, $this->user
, $this->title
, 'secure' ) );
392 global $wgGroupPermissions;
393 $old = $wgGroupPermissions;
394 $wgGroupPermissions = [];
396 $this->assertEquals( $check[$action][1],
397 $this->permissionManager
398 ->getPermissionErrors( $action, $this->user
, $this->title
, true ) );
399 $this->assertEquals( $check[$action][1],
400 $this->permissionManager
401 ->getPermissionErrors( $action, $this->user
, $this->title
, 'full' ) );
402 $this->assertEquals( $check[$action][1],
403 $this->permissionManager
404 ->getPermissionErrors( $action, $this->user
, $this->title
, 'secure' ) );
405 $wgGroupPermissions = $old;
407 $this->setUserPerm( $action );
408 $this->assertEquals( $check[$action][2],
409 $this->permissionManager
410 ->getPermissionErrors( $action, $this->user
, $this->title
, true ) );
411 $this->assertEquals( $check[$action][2],
412 $this->permissionManager
413 ->getPermissionErrors( $action, $this->user
, $this->title
, 'full' ) );
414 $this->assertEquals( $check[$action][2],
415 $this->permissionManager
416 ->getPermissionErrors( $action, $this->user
, $this->title
, 'secure' ) );
418 $this->setUserPerm( $action );
419 $this->assertEquals( $check[$action][3],
420 $this->permissionManager
->userCan( $action, $this->user
, $this->title
, true ) );
421 $this->assertEquals( $check[$action][3],
422 $this->permissionManager
->userCan( $action, $this->user
, $this->title
,
423 PermissionManager
::RIGOR_QUICK
) );
424 # count( User::getGroupsWithPermissions( $action ) ) < 1
428 protected function runGroupPermissions( $action, $result, $result2 = null ) {
429 global $wgGroupPermissions;
431 if ( $result2 === null ) {
435 $wgGroupPermissions['autoconfirmed']['move'] = false;
436 $wgGroupPermissions['user']['move'] = false;
437 $res = $this->permissionManager
438 ->getPermissionErrors( $action, $this->user
, $this->title
);
439 $this->assertEquals( $result, $res );
441 $wgGroupPermissions['autoconfirmed']['move'] = true;
442 $wgGroupPermissions['user']['move'] = false;
443 $res = $this->permissionManager
444 ->getPermissionErrors( $action, $this->user
, $this->title
);
445 $this->assertEquals( $result2, $res );
447 $wgGroupPermissions['autoconfirmed']['move'] = true;
448 $wgGroupPermissions['user']['move'] = true;
449 $res = $this->permissionManager
450 ->getPermissionErrors( $action, $this->user
, $this->title
);
451 $this->assertEquals( $result2, $res );
453 $wgGroupPermissions['autoconfirmed']['move'] = false;
454 $wgGroupPermissions['user']['move'] = true;
455 $res = $this->permissionManager
456 ->getPermissionErrors( $action, $this->user
, $this->title
);
457 $this->assertEquals( $result2, $res );
461 * @todo This test method should be split up into separate test methods and
463 * @covers MediaWiki\Permissions\PermissionManager::checkSpecialsAndNSPermissions
465 public function testSpecialsAndNSPermissions() {
466 global $wgNamespaceProtection;
467 $this->setUser( $this->userName
);
469 $this->setTitle( NS_SPECIAL
);
471 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
472 $this->permissionManager
473 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
475 $this->setTitle( NS_MAIN
);
476 $this->setUserPerm( 'bogus' );
477 $this->assertEquals( [],
478 $this->permissionManager
479 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
481 $this->setTitle( NS_MAIN
);
482 $this->setUserPerm( '' );
483 $this->assertEquals( [ [ 'badaccess-group0' ] ],
484 $this->permissionManager
485 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
487 $wgNamespaceProtection[NS_USER
] = [ 'bogus' ];
489 $this->setTitle( NS_USER
);
490 $this->setUserPerm( '' );
491 $this->assertEquals( [ [ 'badaccess-group0' ],
492 [ 'namespaceprotected', 'User', 'bogus' ] ],
493 $this->permissionManager
494 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
496 $this->setTitle( NS_MEDIAWIKI
);
497 $this->setUserPerm( 'bogus' );
498 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
499 $this->permissionManager
500 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
502 $this->setTitle( NS_MEDIAWIKI
);
503 $this->setUserPerm( 'bogus' );
504 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
505 $this->permissionManager
506 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
508 $wgNamespaceProtection = null;
510 $this->setUserPerm( 'bogus' );
511 $this->assertEquals( [],
512 $this->permissionManager
513 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
514 $this->assertEquals( true,
515 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
) );
517 $this->setUserPerm( '' );
518 $this->assertEquals( [ [ 'badaccess-group0' ] ],
519 $this->permissionManager
520 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
521 $this->assertEquals( false,
522 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
) );
526 * @todo This test method should be split up into separate test methods and
528 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
530 public function testJsConfigEditPermissions() {
531 $this->setUser( $this->userName
);
533 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
534 $this->runConfigEditPermissions(
535 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
537 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
538 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
539 [ [ 'badaccess-group0' ] ],
541 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
542 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
543 [ [ 'badaccess-group0' ] ],
544 [ [ 'badaccess-groups' ] ]
549 * @todo This test method should be split up into separate test methods and
551 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
553 public function testJsonConfigEditPermissions() {
554 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
555 getFormattedNsText( NS_PROJECT
);
556 $this->setUser( $this->userName
);
558 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
559 $this->runConfigEditPermissions(
560 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
562 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
563 [ [ 'badaccess-group0' ] ],
564 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
566 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
567 [ [ 'badaccess-group0' ] ],
568 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
569 [ [ 'badaccess-groups' ] ]
574 * @todo This test method should be split up into separate test methods and
576 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
578 public function testCssConfigEditPermissions() {
579 $this->setUser( $this->userName
);
581 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
582 $this->runConfigEditPermissions(
583 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
585 [ [ 'badaccess-group0' ] ],
586 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
587 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
589 [ [ 'badaccess-group0' ] ],
590 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
591 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
592 [ [ 'badaccess-groups' ] ]
597 * @todo This test method should be split up into separate test methods and
599 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
601 public function testOtherJsConfigEditPermissions() {
602 $this->setUser( $this->userName
);
604 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
605 $this->runConfigEditPermissions(
606 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
608 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
609 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
610 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
612 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
613 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
614 [ [ 'badaccess-group0' ] ],
615 [ [ 'badaccess-groups' ] ]
620 * @todo This test method should be split up into separate test methods and
622 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
624 public function testOtherJsonConfigEditPermissions() {
625 $this->setUser( $this->userName
);
627 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
628 $this->runConfigEditPermissions(
629 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
631 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
632 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
633 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
635 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
636 [ [ 'badaccess-group0' ] ],
637 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
638 [ [ 'badaccess-groups' ] ]
643 * @todo This test method should be split up into separate test methods and
645 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
647 public function testOtherCssConfigEditPermissions() {
648 $this->setUser( $this->userName
);
650 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
651 $this->runConfigEditPermissions(
652 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
654 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
655 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
656 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
658 [ [ 'badaccess-group0' ] ],
659 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
660 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
661 [ [ 'badaccess-groups' ] ]
666 * @todo This test method should be split up into separate test methods and
668 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
670 public function testOtherNonConfigEditPermissions() {
671 $this->setUser( $this->userName
);
673 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
674 $this->runConfigEditPermissions(
675 [ [ 'badaccess-group0' ] ],
677 [ [ 'badaccess-group0' ] ],
678 [ [ 'badaccess-group0' ] ],
679 [ [ 'badaccess-group0' ] ],
681 [ [ 'badaccess-group0' ] ],
682 [ [ 'badaccess-group0' ] ],
683 [ [ 'badaccess-group0' ] ],
684 [ [ 'badaccess-groups' ] ]
689 * @todo This should use data providers like the other methods here.
690 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
692 public function testPatrolActionConfigEditPermissions() {
693 $this->setUser( 'anon' );
694 $this->setTitle( NS_USER
, 'ToPatrolOrNotToPatrol' );
695 $this->runConfigEditPermissions(
696 [ [ 'badaccess-group0' ] ],
698 [ [ 'badaccess-group0' ] ],
699 [ [ 'badaccess-group0' ] ],
700 [ [ 'badaccess-group0' ] ],
702 [ [ 'badaccess-group0' ] ],
703 [ [ 'badaccess-group0' ] ],
704 [ [ 'badaccess-group0' ] ],
705 [ [ 'badaccess-groups' ] ]
709 protected function runConfigEditPermissions(
719 $this->setUserPerm( '' );
720 $result = $this->permissionManager
721 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
722 $this->assertEquals( $resultNone, $result );
724 $this->setUserPerm( 'editmyusercss' );
725 $result = $this->permissionManager
726 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
727 $this->assertEquals( $resultMyCss, $result );
729 $this->setUserPerm( 'editmyuserjson' );
730 $result = $this->permissionManager
731 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
732 $this->assertEquals( $resultMyJson, $result );
734 $this->setUserPerm( 'editmyuserjs' );
735 $result = $this->permissionManager
736 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
737 $this->assertEquals( $resultMyJs, $result );
739 $this->setUserPerm( 'editusercss' );
740 $result = $this->permissionManager
741 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
742 $this->assertEquals( $resultUserCss, $result );
744 $this->setUserPerm( 'edituserjson' );
745 $result = $this->permissionManager
746 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
747 $this->assertEquals( $resultUserJson, $result );
749 $this->setUserPerm( 'edituserjs' );
750 $result = $this->permissionManager
751 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
752 $this->assertEquals( $resultUserJs, $result );
754 $this->setUserPerm( '' );
755 $result = $this->permissionManager
756 ->getPermissionErrors( 'patrol', $this->user
, $this->title
);
757 $this->assertEquals( reset( $resultPatrol[0] ), reset( $result[0] ) );
759 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
760 $result = $this->permissionManager
761 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
762 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
766 * @todo This test method should be split up into separate test methods and
769 * This test is failing per T201776.
772 * @covers \MediaWiki\Permissions\PermissionManager::checkPageRestrictions
774 public function testPageRestrictions() {
775 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
776 getFormattedNsText( NS_PROJECT
);
778 $this->setTitle( NS_MAIN
);
779 $this->title
->mRestrictionsLoaded
= true;
780 $this->setUserPerm( "edit" );
781 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
783 $this->assertEquals( [],
784 $this->permissionManager
->getPermissionErrors( 'edit',
785 $this->user
, $this->title
) );
787 $this->assertEquals( true,
788 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
,
789 PermissionManager
::RIGOR_QUICK
) );
791 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
792 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
794 $this->assertEquals( [ [ 'badaccess-group0' ],
795 [ 'protectedpagetext', 'bogus', 'bogus' ],
796 [ 'protectedpagetext', 'editprotected', 'bogus' ],
797 [ 'protectedpagetext', 'protect', 'bogus' ] ],
798 $this->permissionManager
->getPermissionErrors( 'bogus',
799 $this->user
, $this->title
) );
800 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
801 [ 'protectedpagetext', 'editprotected', 'edit' ],
802 [ 'protectedpagetext', 'protect', 'edit' ] ],
803 $this->permissionManager
->getPermissionErrors( 'edit',
804 $this->user
, $this->title
) );
805 $this->setUserPerm( "" );
806 $this->assertEquals( [ [ 'badaccess-group0' ],
807 [ 'protectedpagetext', 'bogus', 'bogus' ],
808 [ 'protectedpagetext', 'editprotected', 'bogus' ],
809 [ 'protectedpagetext', 'protect', 'bogus' ] ],
810 $this->permissionManager
->getPermissionErrors( 'bogus',
811 $this->user
, $this->title
) );
812 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
813 [ 'protectedpagetext', 'bogus', 'edit' ],
814 [ 'protectedpagetext', 'editprotected', 'edit' ],
815 [ 'protectedpagetext', 'protect', 'edit' ] ],
816 $this->permissionManager
->getPermissionErrors( 'edit',
817 $this->user
, $this->title
) );
818 $this->setUserPerm( [ "edit", "editprotected" ] );
819 $this->assertEquals( [ [ 'badaccess-group0' ],
820 [ 'protectedpagetext', 'bogus', 'bogus' ],
821 [ 'protectedpagetext', 'protect', 'bogus' ] ],
822 $this->permissionManager
->getPermissionErrors( 'bogus',
823 $this->user
, $this->title
) );
824 $this->assertEquals( [
825 [ 'protectedpagetext', 'bogus', 'edit' ],
826 [ 'protectedpagetext', 'protect', 'edit' ] ],
827 $this->permissionManager
->getPermissionErrors( 'edit',
828 $this->user
, $this->title
) );
830 $this->title
->mCascadeRestriction
= true;
831 $this->setUserPerm( "edit" );
833 $this->assertEquals( false,
834 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
,
835 PermissionManager
::RIGOR_QUICK
) );
837 $this->assertEquals( false,
838 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
,
839 PermissionManager
::RIGOR_QUICK
) );
841 $this->assertEquals( [ [ 'badaccess-group0' ],
842 [ 'protectedpagetext', 'bogus', 'bogus' ],
843 [ 'protectedpagetext', 'editprotected', 'bogus' ],
844 [ 'protectedpagetext', 'protect', 'bogus' ] ],
845 $this->permissionManager
->getPermissionErrors( 'bogus',
846 $this->user
, $this->title
) );
847 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
848 [ 'protectedpagetext', 'editprotected', 'edit' ],
849 [ 'protectedpagetext', 'protect', 'edit' ] ],
850 $this->permissionManager
->getPermissionErrors( 'edit',
851 $this->user
, $this->title
) );
853 $this->setUserPerm( [ "edit", "editprotected" ] );
854 $this->assertEquals( false,
855 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
,
856 PermissionManager
::RIGOR_QUICK
) );
858 $this->assertEquals( false,
859 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
,
860 PermissionManager
::RIGOR_QUICK
) );
862 $this->assertEquals( [ [ 'badaccess-group0' ],
863 [ 'protectedpagetext', 'bogus', 'bogus' ],
864 [ 'protectedpagetext', 'protect', 'bogus' ],
865 [ 'protectedpagetext', 'protect', 'bogus' ] ],
866 $this->permissionManager
->getPermissionErrors( 'bogus',
867 $this->user
, $this->title
) );
868 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
869 [ 'protectedpagetext', 'protect', 'edit' ],
870 [ 'protectedpagetext', 'protect', 'edit' ] ],
871 $this->permissionManager
->getPermissionErrors( 'edit',
872 $this->user
, $this->title
) );
876 * @covers \MediaWiki\Permissions\PermissionManager::checkCascadingSourcesRestrictions
878 public function testCascadingSourcesRestrictions() {
879 $this->setTitle( NS_MAIN
, "test page" );
880 $this->setUserPerm( [ "edit", "bogus" ] );
882 $this->title
->mCascadeSources
= [
883 Title
::makeTitle( NS_MAIN
, "Bogus" ),
884 Title
::makeTitle( NS_MAIN
, "UnBogus" )
886 $this->title
->mCascadingRestrictions
= [
887 "bogus" => [ 'bogus', "sysop", "protect", "" ]
890 $this->assertEquals( false,
891 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
) );
892 $this->assertEquals( [
893 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
894 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
895 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
896 $this->permissionManager
->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
898 $this->assertEquals( true,
899 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
) );
900 $this->assertEquals( [],
901 $this->permissionManager
->getPermissionErrors( 'edit', $this->user
, $this->title
) );
905 * @todo This test method should be split up into separate test methods and
907 * @covers \MediaWiki\Permissions\PermissionManager::checkActionPermissions
909 public function testActionPermissions() {
910 $this->setUserPerm( [ "createpage" ] );
911 $this->setTitle( NS_MAIN
, "test page" );
912 $this->title
->mTitleProtection
['permission'] = '';
913 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
914 $this->title
->mTitleProtection
['expiry'] = 'infinity';
915 $this->title
->mTitleProtection
['reason'] = 'test';
916 $this->title
->mCascadeRestriction
= false;
918 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
919 $this->permissionManager
920 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
921 $this->assertEquals( false,
922 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
924 $this->title
->mTitleProtection
['permission'] = 'editprotected';
925 $this->setUserPerm( [ 'createpage', 'protect' ] );
926 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
927 $this->permissionManager
928 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
929 $this->assertEquals( false,
930 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
932 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
933 $this->assertEquals( [],
934 $this->permissionManager
935 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
936 $this->assertEquals( true,
937 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
939 $this->setUserPerm( [ 'createpage' ] );
940 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
941 $this->permissionManager
942 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
943 $this->assertEquals( false,
944 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
946 $this->setTitle( NS_MEDIA
, "test page" );
947 $this->setUserPerm( [ "move" ] );
948 $this->assertEquals( false,
949 $this->permissionManager
->userCan( 'move', $this->user
, $this->title
) );
950 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
951 $this->permissionManager
952 ->getPermissionErrors( 'move', $this->user
, $this->title
) );
954 $this->setTitle( NS_HELP
, "test page" );
955 $this->assertEquals( [],
956 $this->permissionManager
957 ->getPermissionErrors( 'move', $this->user
, $this->title
) );
958 $this->assertEquals( true,
959 $this->permissionManager
->userCan( 'move', $this->user
, $this->title
) );
961 $this->title
->mInterwiki
= "no";
962 $this->assertEquals( [ [ 'immobile-source-page' ] ],
963 $this->permissionManager
964 ->getPermissionErrors( 'move', $this->user
, $this->title
) );
965 $this->assertEquals( false,
966 $this->permissionManager
->userCan( 'move', $this->user
, $this->title
) );
968 $this->setTitle( NS_MEDIA
, "test page" );
969 $this->assertEquals( false,
970 $this->permissionManager
->userCan( 'move-target', $this->user
, $this->title
) );
971 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
972 $this->permissionManager
973 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
975 $this->setTitle( NS_HELP
, "test page" );
976 $this->assertEquals( [],
977 $this->permissionManager
978 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
979 $this->assertEquals( true,
980 $this->permissionManager
->userCan( 'move-target', $this->user
, $this->title
) );
982 $this->title
->mInterwiki
= "no";
983 $this->assertEquals( [ [ 'immobile-target-page' ] ],
984 $this->permissionManager
985 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
986 $this->assertEquals( false,
987 $this->permissionManager
->userCan( 'move-target', $this->user
, $this->title
) );
991 * @covers \MediaWiki\Permissions\PermissionManager::checkUserBlock
993 public function testUserBlock() {
994 $this->setMwGlobals( [
995 'wgEmailConfirmToEdit' => true,
996 'wgEmailAuthentication' => true,
997 'wgBlockDisablesLogin' => false,
1000 $this->overrideMwServices();
1001 $this->permissionManager
= MediaWikiServices
::getInstance()->getPermissionManager();
1003 $this->setUserPerm( [
1012 $this->setTitle( NS_HELP
, "test page" );
1014 # $wgEmailConfirmToEdit only applies to 'edit' action
1015 $this->assertEquals( [],
1016 $this->permissionManager
->getPermissionErrors( 'move-target',
1017 $this->user
, $this->title
) );
1018 $this->assertContains( [ 'confirmedittext' ],
1019 $this->permissionManager
1020 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1022 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
1023 $this->overrideMwServices();
1024 $this->permissionManager
= MediaWikiServices
::getInstance()->getPermissionManager();
1026 $this->assertNotContains( [ 'confirmedittext' ],
1027 $this->permissionManager
1028 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1030 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
1031 $this->assertEquals( [],
1032 $this->permissionManager
->getPermissionErrors( 'move-target',
1033 $this->user
, $this->title
) );
1037 $now = time() +
120;
1038 $this->user
->mBlockedby
= $this->user
->getId();
1039 $this->user
->mBlock
= new Block( [
1040 'address' => '127.0.8.1',
1041 'by' => $this->user
->getId(),
1042 'reason' => 'no reason given',
1043 'timestamp' => $prev +
3600,
1047 $this->user
->mBlock
->mTimestamp
= 0;
1048 $this->assertEquals( [ [ 'autoblockedtext',
1049 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1050 'Useruser', null, 'infinite', '127.0.8.1',
1051 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
1052 $this->permissionManager
->getPermissionErrors( 'move-target',
1053 $this->user
, $this->title
) );
1055 $this->assertEquals( false, $this->permissionManager
1056 ->userCan( 'move-target', $this->user
, $this->title
) );
1057 // quickUserCan should ignore user blocks
1058 $this->assertEquals( true, $this->permissionManager
1059 ->userCan( 'move-target', $this->user
, $this->title
,
1060 PermissionManager
::RIGOR_QUICK
) );
1062 global $wgLocalTZoffset;
1063 $wgLocalTZoffset = -60;
1064 $this->user
->mBlockedby
= $this->user
->getName();
1065 $this->user
->mBlock
= new Block( [
1066 'address' => '127.0.8.1',
1067 'by' => $this->user
->getId(),
1068 'reason' => 'no reason given',
1069 'timestamp' => $now,
1073 $this->assertEquals( [ [ 'blockedtext',
1074 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1075 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1076 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
1077 $this->permissionManager
1078 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1079 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
1080 # $user->blockedFor() == ''
1081 # $user->mBlock->mExpiry == 'infinity'
1083 $this->user
->mBlockedby
= $this->user
->getName();
1084 $this->user
->mBlock
= new SystemBlock( [
1085 'address' => '127.0.8.1',
1086 'by' => $this->user
->getId(),
1087 'reason' => 'no reason given',
1088 'timestamp' => $now,
1090 'systemBlock' => 'test',
1093 $errors = [ [ 'systemblockedtext',
1094 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1095 'Useruser', 'test', 'infinite', '127.0.8.1',
1096 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1098 $this->assertEquals( $errors,
1099 $this->permissionManager
1100 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1101 $this->assertEquals( $errors,
1102 $this->permissionManager
1103 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1104 $this->assertEquals( $errors,
1105 $this->permissionManager
1106 ->getPermissionErrors( 'rollback', $this->user
, $this->title
) );
1107 $this->assertEquals( $errors,
1108 $this->permissionManager
1109 ->getPermissionErrors( 'patrol', $this->user
, $this->title
) );
1110 $this->assertEquals( $errors,
1111 $this->permissionManager
1112 ->getPermissionErrors( 'upload', $this->user
, $this->title
) );
1113 $this->assertEquals( [],
1114 $this->permissionManager
1115 ->getPermissionErrors( 'purge', $this->user
, $this->title
) );
1117 // partial block message test
1118 $this->user
->mBlockedby
= $this->user
->getName();
1119 $this->user
->mBlock
= new Block( [
1120 'address' => '127.0.8.1',
1121 'by' => $this->user
->getId(),
1122 'reason' => 'no reason given',
1123 'timestamp' => $now,
1124 'sitewide' => false,
1128 $this->assertEquals( [],
1129 $this->permissionManager
1130 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1131 $this->assertEquals( [],
1132 $this->permissionManager
1133 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1134 $this->assertEquals( [],
1135 $this->permissionManager
1136 ->getPermissionErrors( 'rollback', $this->user
, $this->title
) );
1137 $this->assertEquals( [],
1138 $this->permissionManager
1139 ->getPermissionErrors( 'patrol', $this->user
, $this->title
) );
1140 $this->assertEquals( [],
1141 $this->permissionManager
1142 ->getPermissionErrors( 'upload', $this->user
, $this->title
) );
1143 $this->assertEquals( [],
1144 $this->permissionManager
1145 ->getPermissionErrors( 'purge', $this->user
, $this->title
) );
1147 $this->user
->mBlock
->setRestrictions( [
1148 ( new PageRestriction( 0, $this->title
->getArticleID() ) )->setTitle( $this->title
),
1151 $errors = [ [ 'blockedtext-partial',
1152 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1153 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1154 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1156 $this->assertEquals( $errors,
1157 $this->permissionManager
1158 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1159 $this->assertEquals( $errors,
1160 $this->permissionManager
1161 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1162 $this->assertEquals( $errors,
1163 $this->permissionManager
1164 ->getPermissionErrors( 'rollback', $this->user
, $this->title
) );
1165 $this->assertEquals( $errors,
1166 $this->permissionManager
1167 ->getPermissionErrors( 'patrol', $this->user
, $this->title
) );
1168 $this->assertEquals( [],
1169 $this->permissionManager
1170 ->getPermissionErrors( 'upload', $this->user
, $this->title
) );
1171 $this->assertEquals( [],
1172 $this->permissionManager
1173 ->getPermissionErrors( 'purge', $this->user
, $this->title
) );
1176 $this->user
->mBlockedby
= null;
1177 $this->user
->mBlock
= null;
1179 $this->assertEquals( [],
1180 $this->permissionManager
1181 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1185 * @covers \MediaWiki\Permissions\PermissionManager::checkUserBlock
1187 * Tests to determine that the passed in permission does not get mixed up with
1188 * an action of the same name.
1190 public function testUserBlockAction() {
1193 $tester = $this->getMockBuilder( Action
::class )
1194 ->disableOriginalConstructor()
1196 $tester->method( 'getName' )
1197 ->willReturn( 'tester' );
1198 $tester->method( 'getRestriction' )
1199 ->willReturn( 'test' );
1200 $tester->method( 'requiresUnblock' )
1201 ->willReturn( false );
1203 $this->setMwGlobals( [
1205 'tester' => $tester,
1207 'wgGroupPermissions' => [
1215 $this->user
->mBlockedby
= $this->user
->getName();
1216 $this->user
->mBlock
= new Block( [
1217 'address' => '127.0.8.1',
1218 'by' => $this->user
->getId(),
1219 'reason' => 'no reason given',
1220 'timestamp' => $now,
1222 'expiry' => 'infinity',
1225 $errors = [ [ 'blockedtext',
1226 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1227 'Useruser', null, 'infinite', '127.0.8.1',
1228 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1230 $this->assertEquals( $errors,
1231 $this->permissionManager
1232 ->getPermissionErrors( 'tester', $this->user
, $this->title
) );
1236 * @covers \MediaWiki\Permissions\PermissionManager::isBlockedFrom
1238 public function testBlockInstanceCache() {
1239 // First, check the user isn't blocked
1240 $user = $this->getMutableTestUser()->getUser();
1241 $ut = Title
::makeTitle( NS_USER_TALK
, $user->getName() );
1242 $this->assertNull( $user->getBlock( false ), 'sanity check' );
1243 //$this->assertSame( '', $user->blockedBy(), 'sanity check' );
1244 //$this->assertSame( '', $user->blockedFor(), 'sanity check' );
1245 //$this->assertFalse( (bool)$user->isHidden(), 'sanity check' );
1246 $this->assertFalse( $this->permissionManager
1247 ->isBlockedFrom( $user, $ut ), 'sanity check' );
1250 $blocker = $this->getTestSysop()->getUser();
1251 $block = new Block( [
1253 'allowUsertalk' => false,
1254 'reason' => 'Because',
1256 $block->setTarget( $user );
1257 $block->setBlocker( $blocker );
1258 $res = $block->insert();
1259 $this->assertTrue( (bool)$res['id'], 'sanity check: Failed to insert block' );
1261 // Clear cache and confirm it loaded the block properly
1262 $user->clearInstanceCache();
1263 $this->assertInstanceOf( Block
::class, $user->getBlock( false ) );
1264 //$this->assertSame( $blocker->getName(), $user->blockedBy() );
1265 //$this->assertSame( 'Because', $user->blockedFor() );
1266 //$this->assertTrue( (bool)$user->isHidden() );
1267 $this->assertTrue( $this->permissionManager
->isBlockedFrom( $user, $ut ) );
1272 // Clear cache and confirm it loaded the not-blocked properly
1273 $user->clearInstanceCache();
1274 $this->assertNull( $user->getBlock( false ) );
1275 //$this->assertSame( '', $user->blockedBy() );
1276 //$this->assertSame( '', $user->blockedFor() );
1277 //$this->assertFalse( (bool)$user->isHidden() );
1278 $this->assertFalse( $this->permissionManager
->isBlockedFrom( $user, $ut ) );
1282 * @covers \MediaWiki\Permissions\PermissionManager::isBlockedFrom
1283 * @dataProvider provideIsBlockedFrom
1284 * @param string|null $title Title to test.
1285 * @param bool $expect Expected result from User::isBlockedFrom()
1286 * @param array $options Additional test options:
1287 * - 'blockAllowsUTEdit': (bool, default true) Value for $wgBlockAllowsUTEdit
1288 * - 'allowUsertalk': (bool, default false) Passed to Block::__construct()
1289 * - 'pageRestrictions': (array|null) If non-empty, page restriction titles for the block.
1291 public function testIsBlockedFrom( $title, $expect, array $options = [] ) {
1292 $this->setMwGlobals( [
1293 'wgBlockAllowsUTEdit' => $options['blockAllowsUTEdit'] ??
true,
1296 $user = $this->getTestUser()->getUser();
1298 if ( $title === self
::USER_TALK_PAGE
) {
1299 $title = $user->getTalkPage();
1301 $title = Title
::newFromText( $title );
1305 foreach ( $options['pageRestrictions'] ??
[] as $pagestr ) {
1306 $page = $this->getExistingTestPage(
1307 $pagestr === self
::USER_TALK_PAGE ?
$user->getTalkPage() : $pagestr
1309 $restrictions[] = new PageRestriction( 0, $page->getId() );
1311 foreach ( $options['namespaceRestrictions'] ??
[] as $ns ) {
1312 $restrictions[] = new NamespaceRestriction( 0, $ns );
1315 $block = new Block( [
1316 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
1317 'allowUsertalk' => $options['allowUsertalk'] ??
false,
1318 'sitewide' => !$restrictions,
1320 $block->setTarget( $user );
1321 $block->setBlocker( $this->getTestSysop()->getUser() );
1322 if ( $restrictions ) {
1323 $block->setRestrictions( $restrictions );
1328 $this->assertSame( $expect, $this->permissionManager
->isBlockedFrom( $user, $title ) );
1334 public static function provideIsBlockedFrom() {
1336 'Sitewide block, basic operation' => [ 'Test page', true ],
1337 'Sitewide block, not allowing user talk' => [
1338 self
::USER_TALK_PAGE
, true, [
1339 'allowUsertalk' => false,
1342 'Sitewide block, allowing user talk' => [
1343 self
::USER_TALK_PAGE
, false, [
1344 'allowUsertalk' => true,
1347 'Sitewide block, allowing user talk but $wgBlockAllowsUTEdit is false' => [
1348 self
::USER_TALK_PAGE
, true, [
1349 'allowUsertalk' => true,
1350 'blockAllowsUTEdit' => false,
1353 'Partial block, blocking the page' => [
1354 'Test page', true, [
1355 'pageRestrictions' => [ 'Test page' ],
1358 'Partial block, not blocking the page' => [
1359 'Test page 2', false, [
1360 'pageRestrictions' => [ 'Test page' ],
1363 'Partial block, not allowing user talk but user talk page is not blocked' => [
1364 self
::USER_TALK_PAGE
, false, [
1365 'allowUsertalk' => false,
1366 'pageRestrictions' => [ 'Test page' ],
1369 'Partial block, allowing user talk but user talk page is blocked' => [
1370 self
::USER_TALK_PAGE
, true, [
1371 'allowUsertalk' => true,
1372 'pageRestrictions' => [ self
::USER_TALK_PAGE
],
1375 'Partial block, user talk page is not blocked but $wgBlockAllowsUTEdit is false' => [
1376 self
::USER_TALK_PAGE
, false, [
1377 'allowUsertalk' => false,
1378 'pageRestrictions' => [ 'Test page' ],
1379 'blockAllowsUTEdit' => false,
1382 'Partial block, user talk page is blocked and $wgBlockAllowsUTEdit is false' => [
1383 self
::USER_TALK_PAGE
, true, [
1384 'allowUsertalk' => true,
1385 'pageRestrictions' => [ self
::USER_TALK_PAGE
],
1386 'blockAllowsUTEdit' => false,
1389 'Partial user talk namespace block, not allowing user talk' => [
1390 self
::USER_TALK_PAGE
, true, [
1391 'allowUsertalk' => false,
1392 'namespaceRestrictions' => [ NS_USER_TALK
],
1395 'Partial user talk namespace block, allowing user talk' => [
1396 self
::USER_TALK_PAGE
, false, [
1397 'allowUsertalk' => true,
1398 'namespaceRestrictions' => [ NS_USER_TALK
],
1401 'Partial user talk namespace block, where $wgBlockAllowsUTEdit is false' => [
1402 self
::USER_TALK_PAGE
, true, [
1403 'allowUsertalk' => true,
1404 'namespaceRestrictions' => [ NS_USER_TALK
],
1405 'blockAllowsUTEdit' => false,