3 namespace MediaWiki\Tests\Permissions
;
7 use MediaWikiLangTestCase
;
11 use MediaWiki\Block\Restriction\NamespaceRestriction
;
12 use MediaWiki\Block\Restriction\PageRestriction
;
13 use MediaWiki\MediaWikiServices
;
14 use MediaWiki\Permissions\PermissionManager
;
19 * @covers \MediaWiki\Permissions\PermissionManager
21 class PermissionManagerTest
extends MediaWikiLangTestCase
{
26 protected $userName, $altUserName;
36 protected $user, $anonUser, $userUser, $altUser;
39 * @var PermissionManager
41 protected $permissionManager;
43 /** Constant for self::testIsBlockedFrom */
44 const USER_TALK_PAGE
= '<user talk page>';
46 protected function setUp() {
50 $localOffset = date( 'Z' ) / 60;
52 $this->setMwGlobals( [
53 'wgLocaltimezone' => $localZone,
54 'wgLocalTZoffset' => $localOffset,
55 'wgNamespaceProtection' => [
56 NS_MEDIAWIKI
=> 'editinterface',
59 // Without this testUserBlock will use a non-English context on non-English MediaWiki
60 // installations (because of how Title::checkUserBlock is implemented) and fail.
61 RequestContext
::resetMain();
63 $this->userName
= 'Useruser';
64 $this->altUserName
= 'Altuseruser';
65 date_default_timezone_set( $localZone );
67 $this->title
= Title
::makeTitle( NS_MAIN
, "Main Page" );
68 if ( !isset( $this->userUser
) ||
!( $this->userUser
instanceof User
) ) {
69 $this->userUser
= User
::newFromName( $this->userName
);
71 if ( !$this->userUser
->getId() ) {
72 $this->userUser
= User
::createNew( $this->userName
, [
73 "email" => "test@example.com",
74 "real_name" => "Test User" ] );
75 $this->userUser
->load();
78 $this->altUser
= User
::newFromName( $this->altUserName
);
79 if ( !$this->altUser
->getId() ) {
80 $this->altUser
= User
::createNew( $this->altUserName
, [
81 "email" => "alttest@example.com",
82 "real_name" => "Test User Alt" ] );
83 $this->altUser
->load();
86 $this->anonUser
= User
::newFromId( 0 );
88 $this->user
= $this->userUser
;
91 $this->permissionManager
= MediaWikiServices
::getInstance()->getPermissionManager();
93 $this->overrideMwServices();
96 protected function setUserPerm( $perm ) {
97 // Setting member variables is evil!!!
99 if ( is_array( $perm ) ) {
100 $this->user
->mRights
= $perm;
102 $this->user
->mRights
= [ $perm ];
106 protected function setTitle( $ns, $title = "Main_Page" ) {
107 $this->title
= Title
::makeTitle( $ns, $title );
110 protected function setUser( $userName = null ) {
111 if ( $userName === 'anon' ) {
112 $this->user
= $this->anonUser
;
113 } elseif ( $userName === null ||
$userName === $this->userName
) {
114 $this->user
= $this->userUser
;
116 $this->user
= $this->altUser
;
121 * @todo This test method should be split up into separate test methods and
124 * This test is failing per T201776.
127 * @covers \MediaWiki\Permissions\PermissionManager::checkQuickPermissions
129 public function testQuickPermissions() {
130 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
131 getFormattedNsText( NS_PROJECT
);
133 $this->setUser( 'anon' );
134 $this->setTitle( NS_TALK
);
135 $this->setUserPerm( "createtalk" );
136 $res = $this->permissionManager
137 ->getPermissionErrors( 'create', $this->user
, $this->title
);
138 $this->assertEquals( [], $res );
140 $this->setTitle( NS_TALK
);
141 $this->setUserPerm( "createpage" );
142 $res = $this->permissionManager
143 ->getPermissionErrors( 'create', $this->user
, $this->title
);
144 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
146 $this->setTitle( NS_TALK
);
147 $this->setUserPerm( "" );
148 $res = $this->permissionManager
149 ->getPermissionErrors( 'create', $this->user
, $this->title
);
150 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
152 $this->setTitle( NS_MAIN
);
153 $this->setUserPerm( "createpage" );
154 $res = $this->permissionManager
155 ->getPermissionErrors( 'create', $this->user
, $this->title
);
156 $this->assertEquals( [], $res );
158 $this->setTitle( NS_MAIN
);
159 $this->setUserPerm( "createtalk" );
160 $res = $this->permissionManager
161 ->getPermissionErrors( 'create', $this->user
, $this->title
);
162 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
164 $this->setUser( $this->userName
);
165 $this->setTitle( NS_TALK
);
166 $this->setUserPerm( "createtalk" );
167 $res = $this->permissionManager
168 ->getPermissionErrors( 'create', $this->user
, $this->title
);
169 $this->assertEquals( [], $res );
171 $this->setTitle( NS_TALK
);
172 $this->setUserPerm( "createpage" );
173 $res = $this->permissionManager
174 ->getPermissionErrors( 'create', $this->user
, $this->title
);
175 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
177 $this->setTitle( NS_TALK
);
178 $this->setUserPerm( "" );
179 $res = $this->permissionManager
180 ->getPermissionErrors( 'create', $this->user
, $this->title
);
181 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
183 $this->setTitle( NS_MAIN
);
184 $this->setUserPerm( "createpage" );
185 $res = $this->permissionManager
186 ->getPermissionErrors( 'create', $this->user
, $this->title
);
187 $this->assertEquals( [], $res );
189 $this->setTitle( NS_MAIN
);
190 $this->setUserPerm( "createtalk" );
191 $res = $this->permissionManager
192 ->getPermissionErrors( 'create', $this->user
, $this->title
);
193 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
195 $this->setTitle( NS_MAIN
);
196 $this->setUserPerm( "" );
197 $res = $this->permissionManager
198 ->getPermissionErrors( 'create', $this->user
, $this->title
);
199 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
201 $this->setUser( 'anon' );
202 $this->setTitle( NS_USER
, $this->userName
. '' );
203 $this->setUserPerm( "" );
204 $res = $this->permissionManager
205 ->getPermissionErrors( 'move', $this->user
, $this->title
);
206 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
208 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
209 $this->setUserPerm( "" );
210 $res = $this->permissionManager
211 ->getPermissionErrors( 'move', $this->user
, $this->title
);
212 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
214 $this->setTitle( NS_USER
, $this->userName
. '' );
215 $this->setUserPerm( "move-rootuserpages" );
216 $res = $this->permissionManager
217 ->getPermissionErrors( 'move', $this->user
, $this->title
);
218 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
220 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
221 $this->setUserPerm( "move-rootuserpages" );
222 $res = $this->permissionManager
223 ->getPermissionErrors( 'move', $this->user
, $this->title
);
224 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
226 $this->setTitle( NS_USER
, $this->userName
. '' );
227 $this->setUserPerm( "" );
228 $res = $this->permissionManager
229 ->getPermissionErrors( 'move', $this->user
, $this->title
);
230 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
232 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
233 $this->setUserPerm( "" );
234 $res = $this->permissionManager
235 ->getPermissionErrors( 'move', $this->user
, $this->title
);
236 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
238 $this->setTitle( NS_USER
, $this->userName
. '' );
239 $this->setUserPerm( "move-rootuserpages" );
240 $res = $this->permissionManager
241 ->getPermissionErrors( 'move', $this->user
, $this->title
);
242 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
244 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
245 $this->setUserPerm( "move-rootuserpages" );
246 $res = $this->permissionManager
247 ->getPermissionErrors( 'move', $this->user
, $this->title
);
248 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
250 $this->setUser( $this->userName
);
251 $this->setTitle( NS_FILE
, "img.png" );
252 $this->setUserPerm( "" );
253 $res = $this->permissionManager
254 ->getPermissionErrors( 'move', $this->user
, $this->title
);
255 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
257 $this->setTitle( NS_FILE
, "img.png" );
258 $this->setUserPerm( "movefile" );
259 $res = $this->permissionManager
260 ->getPermissionErrors( 'move', $this->user
, $this->title
);
261 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
263 $this->setUser( 'anon' );
264 $this->setTitle( NS_FILE
, "img.png" );
265 $this->setUserPerm( "" );
266 $res = $this->permissionManager
267 ->getPermissionErrors( 'move', $this->user
, $this->title
);
268 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
270 $this->setTitle( NS_FILE
, "img.png" );
271 $this->setUserPerm( "movefile" );
272 $res = $this->permissionManager
273 ->getPermissionErrors( 'move', $this->user
, $this->title
);
274 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
276 $this->setUser( $this->userName
);
277 $this->setUserPerm( "move" );
278 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
280 $this->setUserPerm( "" );
281 $this->runGroupPermissions(
283 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
286 $this->setUser( 'anon' );
287 $this->setUserPerm( "move" );
288 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
290 $this->setUserPerm( "" );
291 $this->runGroupPermissions(
293 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
294 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
297 if ( $this->isWikitextNS( NS_MAIN
) ) {
298 // NOTE: some content models don't allow moving
299 // @todo find a Wikitext namespace for testing
301 $this->setTitle( NS_MAIN
);
302 $this->setUser( 'anon' );
303 $this->setUserPerm( "move" );
304 $this->runGroupPermissions( 'move', [] );
306 $this->setUserPerm( "" );
307 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
308 [ [ 'movenologintext' ] ] );
310 $this->setUser( $this->userName
);
311 $this->setUserPerm( "" );
312 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
314 $this->setUserPerm( "move" );
315 $this->runGroupPermissions( 'move', [] );
317 $this->setUser( 'anon' );
318 $this->setUserPerm( 'move' );
319 $res = $this->permissionManager
320 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
321 $this->assertEquals( [], $res );
323 $this->setUserPerm( '' );
324 $res = $this->permissionManager
325 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
326 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
329 $this->setTitle( NS_USER
);
330 $this->setUser( $this->userName
);
331 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
332 $res = $this->permissionManager
333 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
334 $this->assertEquals( [], $res );
336 $this->setUserPerm( "move" );
337 $res = $this->permissionManager
338 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
339 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
341 $this->setUser( 'anon' );
342 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
343 $res = $this->permissionManager
344 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
345 $this->assertEquals( [], $res );
347 $this->setTitle( NS_USER
, "User/subpage" );
348 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
349 $res = $this->permissionManager
350 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
351 $this->assertEquals( [], $res );
353 $this->setUserPerm( "move" );
354 $res = $this->permissionManager
355 ->getPermissionErrors( 'move-target', $this->user
, $this->title
);
356 $this->assertEquals( [], $res );
358 $this->setUser( 'anon' );
361 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
362 [ [ 'badaccess-group0' ] ],
369 "[[$prefix:Administrators|Administrators]]", 1 ],
372 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
373 [ [ 'protect-cantedit' ] ],
376 '' => [ [], [], [], true ]
379 foreach ( [ "edit", "protect", "" ] as $action ) {
380 $this->setUserPerm( null );
381 $this->assertEquals( $check[$action][0],
382 $this->permissionManager
383 ->getPermissionErrors( $action, $this->user
, $this->title
, true ) );
384 $this->assertEquals( $check[$action][0],
385 $this->permissionManager
386 ->getPermissionErrors( $action, $this->user
, $this->title
, 'full' ) );
387 $this->assertEquals( $check[$action][0],
388 $this->permissionManager
389 ->getPermissionErrors( $action, $this->user
, $this->title
, 'secure' ) );
391 global $wgGroupPermissions;
392 $old = $wgGroupPermissions;
393 $wgGroupPermissions = [];
395 $this->assertEquals( $check[$action][1],
396 $this->permissionManager
397 ->getPermissionErrors( $action, $this->user
, $this->title
, true ) );
398 $this->assertEquals( $check[$action][1],
399 $this->permissionManager
400 ->getPermissionErrors( $action, $this->user
, $this->title
, 'full' ) );
401 $this->assertEquals( $check[$action][1],
402 $this->permissionManager
403 ->getPermissionErrors( $action, $this->user
, $this->title
, 'secure' ) );
404 $wgGroupPermissions = $old;
406 $this->setUserPerm( $action );
407 $this->assertEquals( $check[$action][2],
408 $this->permissionManager
409 ->getPermissionErrors( $action, $this->user
, $this->title
, true ) );
410 $this->assertEquals( $check[$action][2],
411 $this->permissionManager
412 ->getPermissionErrors( $action, $this->user
, $this->title
, 'full' ) );
413 $this->assertEquals( $check[$action][2],
414 $this->permissionManager
415 ->getPermissionErrors( $action, $this->user
, $this->title
, 'secure' ) );
417 $this->setUserPerm( $action );
418 $this->assertEquals( $check[$action][3],
419 $this->permissionManager
->userCan( $action, $this->user
, $this->title
, true ) );
420 $this->assertEquals( $check[$action][3],
421 $this->permissionManager
->userCan( $action, $this->user
, $this->title
,
422 PermissionManager
::RIGOR_QUICK
) );
423 # count( User::getGroupsWithPermissions( $action ) ) < 1
427 protected function runGroupPermissions( $action, $result, $result2 = null ) {
428 global $wgGroupPermissions;
430 if ( $result2 === null ) {
434 $wgGroupPermissions['autoconfirmed']['move'] = false;
435 $wgGroupPermissions['user']['move'] = false;
436 $res = $this->permissionManager
437 ->getPermissionErrors( $action, $this->user
, $this->title
);
438 $this->assertEquals( $result, $res );
440 $wgGroupPermissions['autoconfirmed']['move'] = true;
441 $wgGroupPermissions['user']['move'] = false;
442 $res = $this->permissionManager
443 ->getPermissionErrors( $action, $this->user
, $this->title
);
444 $this->assertEquals( $result2, $res );
446 $wgGroupPermissions['autoconfirmed']['move'] = true;
447 $wgGroupPermissions['user']['move'] = true;
448 $res = $this->permissionManager
449 ->getPermissionErrors( $action, $this->user
, $this->title
);
450 $this->assertEquals( $result2, $res );
452 $wgGroupPermissions['autoconfirmed']['move'] = false;
453 $wgGroupPermissions['user']['move'] = true;
454 $res = $this->permissionManager
455 ->getPermissionErrors( $action, $this->user
, $this->title
);
456 $this->assertEquals( $result2, $res );
460 * @todo This test method should be split up into separate test methods and
462 * @covers MediaWiki\Permissions\PermissionManager::checkSpecialsAndNSPermissions
464 public function testSpecialsAndNSPermissions() {
465 global $wgNamespaceProtection;
466 $this->setUser( $this->userName
);
468 $this->setTitle( NS_SPECIAL
);
470 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
471 $this->permissionManager
472 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
474 $this->setTitle( NS_MAIN
);
475 $this->setUserPerm( 'bogus' );
476 $this->assertEquals( [],
477 $this->permissionManager
478 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
480 $this->setTitle( NS_MAIN
);
481 $this->setUserPerm( '' );
482 $this->assertEquals( [ [ 'badaccess-group0' ] ],
483 $this->permissionManager
484 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
486 $wgNamespaceProtection[NS_USER
] = [ 'bogus' ];
488 $this->setTitle( NS_USER
);
489 $this->setUserPerm( '' );
490 $this->assertEquals( [ [ 'badaccess-group0' ],
491 [ 'namespaceprotected', 'User', 'bogus' ] ],
492 $this->permissionManager
493 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
495 $this->setTitle( NS_MEDIAWIKI
);
496 $this->setUserPerm( 'bogus' );
497 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
498 $this->permissionManager
499 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
501 $this->setTitle( NS_MEDIAWIKI
);
502 $this->setUserPerm( 'bogus' );
503 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
504 $this->permissionManager
505 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
507 $wgNamespaceProtection = null;
509 $this->setUserPerm( 'bogus' );
510 $this->assertEquals( [],
511 $this->permissionManager
512 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
513 $this->assertEquals( true,
514 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
) );
516 $this->setUserPerm( '' );
517 $this->assertEquals( [ [ 'badaccess-group0' ] ],
518 $this->permissionManager
519 ->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
520 $this->assertEquals( false,
521 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
) );
525 * @todo This test method should be split up into separate test methods and
527 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
529 public function testJsConfigEditPermissions() {
530 $this->setUser( $this->userName
);
532 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
533 $this->runConfigEditPermissions(
534 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
536 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
537 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
538 [ [ 'badaccess-group0' ] ],
540 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
541 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
542 [ [ 'badaccess-group0' ] ],
543 [ [ 'badaccess-groups' ] ]
548 * @todo This test method should be split up into separate test methods and
550 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
552 public function testJsonConfigEditPermissions() {
553 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
554 getFormattedNsText( NS_PROJECT
);
555 $this->setUser( $this->userName
);
557 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
558 $this->runConfigEditPermissions(
559 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
561 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
562 [ [ 'badaccess-group0' ] ],
563 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
565 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
566 [ [ 'badaccess-group0' ] ],
567 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
568 [ [ 'badaccess-groups' ] ]
573 * @todo This test method should be split up into separate test methods and
575 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
577 public function testCssConfigEditPermissions() {
578 $this->setUser( $this->userName
);
580 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
581 $this->runConfigEditPermissions(
582 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
584 [ [ 'badaccess-group0' ] ],
585 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
586 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
588 [ [ 'badaccess-group0' ] ],
589 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
590 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
591 [ [ 'badaccess-groups' ] ]
596 * @todo This test method should be split up into separate test methods and
598 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
600 public function testOtherJsConfigEditPermissions() {
601 $this->setUser( $this->userName
);
603 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
604 $this->runConfigEditPermissions(
605 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
607 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
608 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
609 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
611 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
612 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
613 [ [ 'badaccess-group0' ] ],
614 [ [ 'badaccess-groups' ] ]
619 * @todo This test method should be split up into separate test methods and
621 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
623 public function testOtherJsonConfigEditPermissions() {
624 $this->setUser( $this->userName
);
626 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
627 $this->runConfigEditPermissions(
628 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
630 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
631 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
632 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
634 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
635 [ [ 'badaccess-group0' ] ],
636 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
637 [ [ 'badaccess-groups' ] ]
642 * @todo This test method should be split up into separate test methods and
644 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
646 public function testOtherCssConfigEditPermissions() {
647 $this->setUser( $this->userName
);
649 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
650 $this->runConfigEditPermissions(
651 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
653 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
654 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
655 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
657 [ [ 'badaccess-group0' ] ],
658 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
659 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
660 [ [ 'badaccess-groups' ] ]
665 * @todo This test method should be split up into separate test methods and
667 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
669 public function testOtherNonConfigEditPermissions() {
670 $this->setUser( $this->userName
);
672 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
673 $this->runConfigEditPermissions(
674 [ [ 'badaccess-group0' ] ],
676 [ [ 'badaccess-group0' ] ],
677 [ [ 'badaccess-group0' ] ],
678 [ [ 'badaccess-group0' ] ],
680 [ [ 'badaccess-group0' ] ],
681 [ [ 'badaccess-group0' ] ],
682 [ [ 'badaccess-group0' ] ],
683 [ [ 'badaccess-groups' ] ]
688 * @todo This should use data providers like the other methods here.
689 * @covers \MediaWiki\Permissions\PermissionManager::checkUserConfigPermissions
691 public function testPatrolActionConfigEditPermissions() {
692 $this->setUser( 'anon' );
693 $this->setTitle( NS_USER
, 'ToPatrolOrNotToPatrol' );
694 $this->runConfigEditPermissions(
695 [ [ 'badaccess-group0' ] ],
697 [ [ 'badaccess-group0' ] ],
698 [ [ 'badaccess-group0' ] ],
699 [ [ 'badaccess-group0' ] ],
701 [ [ 'badaccess-group0' ] ],
702 [ [ 'badaccess-group0' ] ],
703 [ [ 'badaccess-group0' ] ],
704 [ [ 'badaccess-groups' ] ]
708 protected function runConfigEditPermissions(
718 $this->setUserPerm( '' );
719 $result = $this->permissionManager
720 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
721 $this->assertEquals( $resultNone, $result );
723 $this->setUserPerm( 'editmyusercss' );
724 $result = $this->permissionManager
725 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
726 $this->assertEquals( $resultMyCss, $result );
728 $this->setUserPerm( 'editmyuserjson' );
729 $result = $this->permissionManager
730 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
731 $this->assertEquals( $resultMyJson, $result );
733 $this->setUserPerm( 'editmyuserjs' );
734 $result = $this->permissionManager
735 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
736 $this->assertEquals( $resultMyJs, $result );
738 $this->setUserPerm( 'editusercss' );
739 $result = $this->permissionManager
740 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
741 $this->assertEquals( $resultUserCss, $result );
743 $this->setUserPerm( 'edituserjson' );
744 $result = $this->permissionManager
745 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
746 $this->assertEquals( $resultUserJson, $result );
748 $this->setUserPerm( 'edituserjs' );
749 $result = $this->permissionManager
750 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
751 $this->assertEquals( $resultUserJs, $result );
753 $this->setUserPerm( '' );
754 $result = $this->permissionManager
755 ->getPermissionErrors( 'patrol', $this->user
, $this->title
);
756 $this->assertEquals( reset( $resultPatrol[0] ), reset( $result[0] ) );
758 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
759 $result = $this->permissionManager
760 ->getPermissionErrors( 'bogus', $this->user
, $this->title
);
761 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
765 * @todo This test method should be split up into separate test methods and
768 * This test is failing per T201776.
771 * @covers \MediaWiki\Permissions\PermissionManager::checkPageRestrictions
773 public function testPageRestrictions() {
774 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
775 getFormattedNsText( NS_PROJECT
);
777 $this->setTitle( NS_MAIN
);
778 $this->title
->mRestrictionsLoaded
= true;
779 $this->setUserPerm( "edit" );
780 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
782 $this->assertEquals( [],
783 $this->permissionManager
->getPermissionErrors( 'edit',
784 $this->user
, $this->title
) );
786 $this->assertEquals( true,
787 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
,
788 PermissionManager
::RIGOR_QUICK
) );
790 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
791 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
793 $this->assertEquals( [ [ 'badaccess-group0' ],
794 [ 'protectedpagetext', 'bogus', 'bogus' ],
795 [ 'protectedpagetext', 'editprotected', 'bogus' ],
796 [ 'protectedpagetext', 'protect', 'bogus' ] ],
797 $this->permissionManager
->getPermissionErrors( 'bogus',
798 $this->user
, $this->title
) );
799 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
800 [ 'protectedpagetext', 'editprotected', 'edit' ],
801 [ 'protectedpagetext', 'protect', 'edit' ] ],
802 $this->permissionManager
->getPermissionErrors( 'edit',
803 $this->user
, $this->title
) );
804 $this->setUserPerm( "" );
805 $this->assertEquals( [ [ 'badaccess-group0' ],
806 [ 'protectedpagetext', 'bogus', 'bogus' ],
807 [ 'protectedpagetext', 'editprotected', 'bogus' ],
808 [ 'protectedpagetext', 'protect', 'bogus' ] ],
809 $this->permissionManager
->getPermissionErrors( 'bogus',
810 $this->user
, $this->title
) );
811 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
812 [ 'protectedpagetext', 'bogus', 'edit' ],
813 [ 'protectedpagetext', 'editprotected', 'edit' ],
814 [ 'protectedpagetext', 'protect', 'edit' ] ],
815 $this->permissionManager
->getPermissionErrors( 'edit',
816 $this->user
, $this->title
) );
817 $this->setUserPerm( [ "edit", "editprotected" ] );
818 $this->assertEquals( [ [ 'badaccess-group0' ],
819 [ 'protectedpagetext', 'bogus', 'bogus' ],
820 [ 'protectedpagetext', 'protect', 'bogus' ] ],
821 $this->permissionManager
->getPermissionErrors( 'bogus',
822 $this->user
, $this->title
) );
823 $this->assertEquals( [
824 [ 'protectedpagetext', 'bogus', 'edit' ],
825 [ 'protectedpagetext', 'protect', 'edit' ] ],
826 $this->permissionManager
->getPermissionErrors( 'edit',
827 $this->user
, $this->title
) );
829 $this->title
->mCascadeRestriction
= true;
830 $this->setUserPerm( "edit" );
832 $this->assertEquals( false,
833 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
,
834 PermissionManager
::RIGOR_QUICK
) );
836 $this->assertEquals( false,
837 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
,
838 PermissionManager
::RIGOR_QUICK
) );
840 $this->assertEquals( [ [ 'badaccess-group0' ],
841 [ 'protectedpagetext', 'bogus', 'bogus' ],
842 [ 'protectedpagetext', 'editprotected', 'bogus' ],
843 [ 'protectedpagetext', 'protect', 'bogus' ] ],
844 $this->permissionManager
->getPermissionErrors( 'bogus',
845 $this->user
, $this->title
) );
846 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
847 [ 'protectedpagetext', 'editprotected', 'edit' ],
848 [ 'protectedpagetext', 'protect', 'edit' ] ],
849 $this->permissionManager
->getPermissionErrors( 'edit',
850 $this->user
, $this->title
) );
852 $this->setUserPerm( [ "edit", "editprotected" ] );
853 $this->assertEquals( false,
854 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
,
855 PermissionManager
::RIGOR_QUICK
) );
857 $this->assertEquals( false,
858 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
,
859 PermissionManager
::RIGOR_QUICK
) );
861 $this->assertEquals( [ [ 'badaccess-group0' ],
862 [ 'protectedpagetext', 'bogus', 'bogus' ],
863 [ 'protectedpagetext', 'protect', 'bogus' ],
864 [ 'protectedpagetext', 'protect', 'bogus' ] ],
865 $this->permissionManager
->getPermissionErrors( 'bogus',
866 $this->user
, $this->title
) );
867 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
868 [ 'protectedpagetext', 'protect', 'edit' ],
869 [ 'protectedpagetext', 'protect', 'edit' ] ],
870 $this->permissionManager
->getPermissionErrors( 'edit',
871 $this->user
, $this->title
) );
875 * @covers \MediaWiki\Permissions\PermissionManager::checkCascadingSourcesRestrictions
877 public function testCascadingSourcesRestrictions() {
878 $this->setTitle( NS_MAIN
, "test page" );
879 $this->setUserPerm( [ "edit", "bogus" ] );
881 $this->title
->mCascadeSources
= [
882 Title
::makeTitle( NS_MAIN
, "Bogus" ),
883 Title
::makeTitle( NS_MAIN
, "UnBogus" )
885 $this->title
->mCascadingRestrictions
= [
886 "bogus" => [ 'bogus', "sysop", "protect", "" ]
889 $this->assertEquals( false,
890 $this->permissionManager
->userCan( 'bogus', $this->user
, $this->title
) );
891 $this->assertEquals( [
892 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
893 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
894 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
895 $this->permissionManager
->getPermissionErrors( 'bogus', $this->user
, $this->title
) );
897 $this->assertEquals( true,
898 $this->permissionManager
->userCan( 'edit', $this->user
, $this->title
) );
899 $this->assertEquals( [],
900 $this->permissionManager
->getPermissionErrors( 'edit', $this->user
, $this->title
) );
904 * @todo This test method should be split up into separate test methods and
906 * @covers \MediaWiki\Permissions\PermissionManager::checkActionPermissions
908 public function testActionPermissions() {
909 $this->setUserPerm( [ "createpage" ] );
910 $this->setTitle( NS_MAIN
, "test page" );
911 $this->title
->mTitleProtection
['permission'] = '';
912 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
913 $this->title
->mTitleProtection
['expiry'] = 'infinity';
914 $this->title
->mTitleProtection
['reason'] = 'test';
915 $this->title
->mCascadeRestriction
= false;
917 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
918 $this->permissionManager
919 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
920 $this->assertEquals( false,
921 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
923 $this->title
->mTitleProtection
['permission'] = 'editprotected';
924 $this->setUserPerm( [ 'createpage', 'protect' ] );
925 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
926 $this->permissionManager
927 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
928 $this->assertEquals( false,
929 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
931 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
932 $this->assertEquals( [],
933 $this->permissionManager
934 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
935 $this->assertEquals( true,
936 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
938 $this->setUserPerm( [ 'createpage' ] );
939 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
940 $this->permissionManager
941 ->getPermissionErrors( 'create', $this->user
, $this->title
) );
942 $this->assertEquals( false,
943 $this->permissionManager
->userCan( 'create', $this->user
, $this->title
) );
945 $this->setTitle( NS_MEDIA
, "test page" );
946 $this->setUserPerm( [ "move" ] );
947 $this->assertEquals( false,
948 $this->permissionManager
->userCan( 'move', $this->user
, $this->title
) );
949 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
950 $this->permissionManager
951 ->getPermissionErrors( 'move', $this->user
, $this->title
) );
953 $this->setTitle( NS_HELP
, "test page" );
954 $this->assertEquals( [],
955 $this->permissionManager
956 ->getPermissionErrors( 'move', $this->user
, $this->title
) );
957 $this->assertEquals( true,
958 $this->permissionManager
->userCan( 'move', $this->user
, $this->title
) );
960 $this->title
->mInterwiki
= "no";
961 $this->assertEquals( [ [ 'immobile-source-page' ] ],
962 $this->permissionManager
963 ->getPermissionErrors( 'move', $this->user
, $this->title
) );
964 $this->assertEquals( false,
965 $this->permissionManager
->userCan( 'move', $this->user
, $this->title
) );
967 $this->setTitle( NS_MEDIA
, "test page" );
968 $this->assertEquals( false,
969 $this->permissionManager
->userCan( 'move-target', $this->user
, $this->title
) );
970 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
971 $this->permissionManager
972 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
974 $this->setTitle( NS_HELP
, "test page" );
975 $this->assertEquals( [],
976 $this->permissionManager
977 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
978 $this->assertEquals( true,
979 $this->permissionManager
->userCan( 'move-target', $this->user
, $this->title
) );
981 $this->title
->mInterwiki
= "no";
982 $this->assertEquals( [ [ 'immobile-target-page' ] ],
983 $this->permissionManager
984 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
985 $this->assertEquals( false,
986 $this->permissionManager
->userCan( 'move-target', $this->user
, $this->title
) );
990 * @covers \MediaWiki\Permissions\PermissionManager::checkUserBlock
992 public function testUserBlock() {
993 $this->setMwGlobals( [
994 'wgEmailConfirmToEdit' => true,
995 'wgEmailAuthentication' => true,
998 $this->overrideMwServices();
999 $this->permissionManager
= MediaWikiServices
::getInstance()->getPermissionManager();
1001 $this->setUserPerm( [
1010 $this->setTitle( NS_HELP
, "test page" );
1012 # $wgEmailConfirmToEdit only applies to 'edit' action
1013 $this->assertEquals( [],
1014 $this->permissionManager
->getPermissionErrors( 'move-target',
1015 $this->user
, $this->title
) );
1016 $this->assertContains( [ 'confirmedittext' ],
1017 $this->permissionManager
1018 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1020 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
1021 $this->overrideMwServices();
1022 $this->permissionManager
= MediaWikiServices
::getInstance()->getPermissionManager();
1024 $this->assertNotContains( [ 'confirmedittext' ],
1025 $this->permissionManager
1026 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1028 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
1029 $this->assertEquals( [],
1030 $this->permissionManager
->getPermissionErrors( 'move-target',
1031 $this->user
, $this->title
) );
1035 $now = time() +
120;
1036 $this->user
->mBlockedby
= $this->user
->getId();
1037 $this->user
->mBlock
= new Block( [
1038 'address' => '127.0.8.1',
1039 'by' => $this->user
->getId(),
1040 'reason' => 'no reason given',
1041 'timestamp' => $prev +
3600,
1045 $this->user
->mBlock
->mTimestamp
= 0;
1046 $this->assertEquals( [ [ 'autoblockedtext',
1047 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1048 'Useruser', null, 'infinite', '127.0.8.1',
1049 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
1050 $this->permissionManager
->getPermissionErrors( 'move-target',
1051 $this->user
, $this->title
) );
1053 $this->assertEquals( false, $this->permissionManager
1054 ->userCan( 'move-target', $this->user
, $this->title
) );
1055 // quickUserCan should ignore user blocks
1056 $this->assertEquals( true, $this->permissionManager
1057 ->userCan( 'move-target', $this->user
, $this->title
,
1058 PermissionManager
::RIGOR_QUICK
) );
1060 global $wgLocalTZoffset;
1061 $wgLocalTZoffset = -60;
1062 $this->user
->mBlockedby
= $this->user
->getName();
1063 $this->user
->mBlock
= new Block( [
1064 'address' => '127.0.8.1',
1065 'by' => $this->user
->getId(),
1066 'reason' => 'no reason given',
1067 'timestamp' => $now,
1071 $this->assertEquals( [ [ 'blockedtext',
1072 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1073 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1074 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
1075 $this->permissionManager
1076 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1077 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
1078 # $user->blockedFor() == ''
1079 # $user->mBlock->mExpiry == 'infinity'
1081 $this->user
->mBlockedby
= $this->user
->getName();
1082 $this->user
->mBlock
= new Block( [
1083 'address' => '127.0.8.1',
1084 'by' => $this->user
->getId(),
1085 'reason' => 'no reason given',
1086 'timestamp' => $now,
1089 'systemBlock' => 'test',
1092 $errors = [ [ 'systemblockedtext',
1093 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1094 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
1095 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1097 $this->assertEquals( $errors,
1098 $this->permissionManager
1099 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1100 $this->assertEquals( $errors,
1101 $this->permissionManager
1102 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1103 $this->assertEquals( $errors,
1104 $this->permissionManager
1105 ->getPermissionErrors( 'rollback', $this->user
, $this->title
) );
1106 $this->assertEquals( $errors,
1107 $this->permissionManager
1108 ->getPermissionErrors( 'patrol', $this->user
, $this->title
) );
1109 $this->assertEquals( $errors,
1110 $this->permissionManager
1111 ->getPermissionErrors( 'upload', $this->user
, $this->title
) );
1112 $this->assertEquals( [],
1113 $this->permissionManager
1114 ->getPermissionErrors( 'purge', $this->user
, $this->title
) );
1116 // partial block message test
1117 $this->user
->mBlockedby
= $this->user
->getName();
1118 $this->user
->mBlock
= new Block( [
1119 'address' => '127.0.8.1',
1120 'by' => $this->user
->getId(),
1121 'reason' => 'no reason given',
1122 'timestamp' => $now,
1123 'sitewide' => false,
1127 $this->assertEquals( [],
1128 $this->permissionManager
1129 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1130 $this->assertEquals( [],
1131 $this->permissionManager
1132 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1133 $this->assertEquals( [],
1134 $this->permissionManager
1135 ->getPermissionErrors( 'rollback', $this->user
, $this->title
) );
1136 $this->assertEquals( [],
1137 $this->permissionManager
1138 ->getPermissionErrors( 'patrol', $this->user
, $this->title
) );
1139 $this->assertEquals( [],
1140 $this->permissionManager
1141 ->getPermissionErrors( 'upload', $this->user
, $this->title
) );
1142 $this->assertEquals( [],
1143 $this->permissionManager
1144 ->getPermissionErrors( 'purge', $this->user
, $this->title
) );
1146 $this->user
->mBlock
->setRestrictions( [
1147 ( new PageRestriction( 0, $this->title
->getArticleID() ) )->setTitle( $this->title
),
1150 $errors = [ [ 'blockedtext-partial',
1151 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1152 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1153 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1155 $this->assertEquals( $errors,
1156 $this->permissionManager
1157 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1158 $this->assertEquals( $errors,
1159 $this->permissionManager
1160 ->getPermissionErrors( 'move-target', $this->user
, $this->title
) );
1161 $this->assertEquals( $errors,
1162 $this->permissionManager
1163 ->getPermissionErrors( 'rollback', $this->user
, $this->title
) );
1164 $this->assertEquals( $errors,
1165 $this->permissionManager
1166 ->getPermissionErrors( 'patrol', $this->user
, $this->title
) );
1167 $this->assertEquals( [],
1168 $this->permissionManager
1169 ->getPermissionErrors( 'upload', $this->user
, $this->title
) );
1170 $this->assertEquals( [],
1171 $this->permissionManager
1172 ->getPermissionErrors( 'purge', $this->user
, $this->title
) );
1175 $this->user
->mBlockedby
= null;
1176 $this->user
->mBlock
= null;
1178 $this->assertEquals( [],
1179 $this->permissionManager
1180 ->getPermissionErrors( 'edit', $this->user
, $this->title
) );
1184 * @covers \MediaWiki\Permissions\PermissionManager::checkUserBlock
1186 * Tests to determine that the passed in permission does not get mixed up with
1187 * an action of the same name.
1189 public function testUserBlockAction() {
1192 $tester = $this->getMockBuilder( Action
::class )
1193 ->disableOriginalConstructor()
1195 $tester->method( 'getName' )
1196 ->willReturn( 'tester' );
1197 $tester->method( 'getRestriction' )
1198 ->willReturn( 'test' );
1199 $tester->method( 'requiresUnblock' )
1200 ->willReturn( false );
1202 $this->setMwGlobals( [
1204 'tester' => $tester,
1206 'wgGroupPermissions' => [
1214 $this->user
->mBlockedby
= $this->user
->getName();
1215 $this->user
->mBlock
= new Block( [
1216 'address' => '127.0.8.1',
1217 'by' => $this->user
->getId(),
1218 'reason' => 'no reason given',
1219 'timestamp' => $now,
1221 'expiry' => 'infinity',
1224 $errors = [ [ 'blockedtext',
1225 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1226 'Useruser', null, 'infinite', '127.0.8.1',
1227 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1229 $this->assertEquals( $errors,
1230 $this->permissionManager
1231 ->getPermissionErrors( 'tester', $this->user
, $this->title
) );
1235 * @covers \MediaWiki\Permissions\PermissionManager::isBlockedFrom
1237 public function testBlockInstanceCache() {
1238 // First, check the user isn't blocked
1239 $user = $this->getMutableTestUser()->getUser();
1240 $ut = Title
::makeTitle( NS_USER_TALK
, $user->getName() );
1241 $this->assertNull( $user->getBlock( false ), 'sanity check' );
1242 //$this->assertSame( '', $user->blockedBy(), 'sanity check' );
1243 //$this->assertSame( '', $user->blockedFor(), 'sanity check' );
1244 //$this->assertFalse( (bool)$user->isHidden(), 'sanity check' );
1245 $this->assertFalse( $this->permissionManager
1246 ->isBlockedFrom( $user, $ut ), 'sanity check' );
1249 $blocker = $this->getTestSysop()->getUser();
1250 $block = new Block( [
1252 'allowUsertalk' => false,
1253 'reason' => 'Because',
1255 $block->setTarget( $user );
1256 $block->setBlocker( $blocker );
1257 $res = $block->insert();
1258 $this->assertTrue( (bool)$res['id'], 'sanity check: Failed to insert block' );
1260 // Clear cache and confirm it loaded the block properly
1261 $user->clearInstanceCache();
1262 $this->assertInstanceOf( Block
::class, $user->getBlock( false ) );
1263 //$this->assertSame( $blocker->getName(), $user->blockedBy() );
1264 //$this->assertSame( 'Because', $user->blockedFor() );
1265 //$this->assertTrue( (bool)$user->isHidden() );
1266 $this->assertTrue( $this->permissionManager
->isBlockedFrom( $user, $ut ) );
1271 // Clear cache and confirm it loaded the not-blocked properly
1272 $user->clearInstanceCache();
1273 $this->assertNull( $user->getBlock( false ) );
1274 //$this->assertSame( '', $user->blockedBy() );
1275 //$this->assertSame( '', $user->blockedFor() );
1276 //$this->assertFalse( (bool)$user->isHidden() );
1277 $this->assertFalse( $this->permissionManager
->isBlockedFrom( $user, $ut ) );
1281 * @covers \MediaWiki\Permissions\PermissionManager::isBlockedFrom
1282 * @dataProvider provideIsBlockedFrom
1283 * @param string|null $title Title to test.
1284 * @param bool $expect Expected result from User::isBlockedFrom()
1285 * @param array $options Additional test options:
1286 * - 'blockAllowsUTEdit': (bool, default true) Value for $wgBlockAllowsUTEdit
1287 * - 'allowUsertalk': (bool, default false) Passed to Block::__construct()
1288 * - 'pageRestrictions': (array|null) If non-empty, page restriction titles for the block.
1290 public function testIsBlockedFrom( $title, $expect, array $options = [] ) {
1291 $this->setMwGlobals( [
1292 'wgBlockAllowsUTEdit' => $options['blockAllowsUTEdit'] ??
true,
1295 $user = $this->getTestUser()->getUser();
1297 if ( $title === self
::USER_TALK_PAGE
) {
1298 $title = $user->getTalkPage();
1300 $title = Title
::newFromText( $title );
1304 foreach ( $options['pageRestrictions'] ??
[] as $pagestr ) {
1305 $page = $this->getExistingTestPage(
1306 $pagestr === self
::USER_TALK_PAGE ?
$user->getTalkPage() : $pagestr
1308 $restrictions[] = new PageRestriction( 0, $page->getId() );
1310 foreach ( $options['namespaceRestrictions'] ??
[] as $ns ) {
1311 $restrictions[] = new NamespaceRestriction( 0, $ns );
1314 $block = new Block( [
1315 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
1316 'allowUsertalk' => $options['allowUsertalk'] ??
false,
1317 'sitewide' => !$restrictions,
1319 $block->setTarget( $user );
1320 $block->setBlocker( $this->getTestSysop()->getUser() );
1321 if ( $restrictions ) {
1322 $block->setRestrictions( $restrictions );
1327 $this->assertSame( $expect, $this->permissionManager
->isBlockedFrom( $user, $title ) );
1333 public static function provideIsBlockedFrom() {
1335 'Sitewide block, basic operation' => [ 'Test page', true ],
1336 'Sitewide block, not allowing user talk' => [
1337 self
::USER_TALK_PAGE
, true, [
1338 'allowUsertalk' => false,
1341 'Sitewide block, allowing user talk' => [
1342 self
::USER_TALK_PAGE
, false, [
1343 'allowUsertalk' => true,
1346 'Sitewide block, allowing user talk but $wgBlockAllowsUTEdit is false' => [
1347 self
::USER_TALK_PAGE
, true, [
1348 'allowUsertalk' => true,
1349 'blockAllowsUTEdit' => false,
1352 'Partial block, blocking the page' => [
1353 'Test page', true, [
1354 'pageRestrictions' => [ 'Test page' ],
1357 'Partial block, not blocking the page' => [
1358 'Test page 2', false, [
1359 'pageRestrictions' => [ 'Test page' ],
1362 'Partial block, not allowing user talk but user talk page is not blocked' => [
1363 self
::USER_TALK_PAGE
, false, [
1364 'allowUsertalk' => false,
1365 'pageRestrictions' => [ 'Test page' ],
1368 'Partial block, allowing user talk but user talk page is blocked' => [
1369 self
::USER_TALK_PAGE
, true, [
1370 'allowUsertalk' => true,
1371 'pageRestrictions' => [ self
::USER_TALK_PAGE
],
1374 'Partial block, user talk page is not blocked but $wgBlockAllowsUTEdit is false' => [
1375 self
::USER_TALK_PAGE
, false, [
1376 'allowUsertalk' => false,
1377 'pageRestrictions' => [ 'Test page' ],
1378 'blockAllowsUTEdit' => false,
1381 'Partial block, user talk page is blocked and $wgBlockAllowsUTEdit is false' => [
1382 self
::USER_TALK_PAGE
, true, [
1383 'allowUsertalk' => true,
1384 'pageRestrictions' => [ self
::USER_TALK_PAGE
],
1385 'blockAllowsUTEdit' => false,
1388 'Partial user talk namespace block, not allowing user talk' => [
1389 self
::USER_TALK_PAGE
, true, [
1390 'allowUsertalk' => false,
1391 'namespaceRestrictions' => [ NS_USER_TALK
],
1394 'Partial user talk namespace block, allowing user talk' => [
1395 self
::USER_TALK_PAGE
, false, [
1396 'allowUsertalk' => true,
1397 'namespaceRestrictions' => [ NS_USER_TALK
],
1400 'Partial user talk namespace block, where $wgBlockAllowsUTEdit is false' => [
1401 self
::USER_TALK_PAGE
, true, [
1402 'allowUsertalk' => true,
1403 'namespaceRestrictions' => [ NS_USER_TALK
],
1404 'blockAllowsUTEdit' => false,