3 use MediaWiki\Block\Restriction\PageRestriction
;
4 use MediaWiki\MediaWikiServices
;
9 * @covers Title::getUserPermissionsErrors
10 * @covers Title::getUserPermissionsErrorsInternal
12 class TitlePermissionTest
extends MediaWikiLangTestCase
{
17 protected $userName, $altUserName;
27 protected $user, $anonUser, $userUser, $altUser;
29 protected function setUp() {
33 $localOffset = date( 'Z' ) / 60;
35 $this->setMwGlobals( [
36 'wgLocaltimezone' => $localZone,
37 'wgLocalTZoffset' => $localOffset,
38 'wgNamespaceProtection' => [
39 NS_MEDIAWIKI
=> 'editinterface',
42 // Without this testUserBlock will use a non-English context on non-English MediaWiki
43 // installations (because of how Title::checkUserBlock is implemented) and fail.
44 RequestContext
::resetMain();
46 $this->userName
= 'Useruser';
47 $this->altUserName
= 'Altuseruser';
48 date_default_timezone_set( $localZone );
50 $this->title
= Title
::makeTitle( NS_MAIN
, "Main Page" );
51 if ( !isset( $this->userUser
) ||
!( $this->userUser
instanceof User
) ) {
52 $this->userUser
= User
::newFromName( $this->userName
);
54 if ( !$this->userUser
->getId() ) {
55 $this->userUser
= User
::createNew( $this->userName
, [
56 "email" => "test@example.com",
57 "real_name" => "Test User" ] );
58 $this->userUser
->load();
61 $this->altUser
= User
::newFromName( $this->altUserName
);
62 if ( !$this->altUser
->getId() ) {
63 $this->altUser
= User
::createNew( $this->altUserName
, [
64 "email" => "alttest@example.com",
65 "real_name" => "Test User Alt" ] );
66 $this->altUser
->load();
69 $this->anonUser
= User
::newFromId( 0 );
71 $this->user
= $this->userUser
;
73 $this->overrideMwServices();
76 protected function setUserPerm( $perm ) {
77 // Setting member variables is evil!!!
79 if ( is_array( $perm ) ) {
80 $this->user
->mRights
= $perm;
82 $this->user
->mRights
= [ $perm ];
86 protected function setTitle( $ns, $title = "Main_Page" ) {
87 $this->title
= Title
::makeTitle( $ns, $title );
90 protected function setUser( $userName = null ) {
91 if ( $userName === 'anon' ) {
92 $this->user
= $this->anonUser
;
93 } elseif ( $userName === null ||
$userName === $this->userName
) {
94 $this->user
= $this->userUser
;
96 $this->user
= $this->altUser
;
101 * @todo This test method should be split up into separate test methods and
104 * This test is failing per T201776.
107 * @covers Title::checkQuickPermissions
109 public function testQuickPermissions() {
110 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
111 getFormattedNsText( NS_PROJECT
);
113 $this->setUser( 'anon' );
114 $this->setTitle( NS_TALK
);
115 $this->setUserPerm( "createtalk" );
116 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
117 $this->assertEquals( [], $res );
119 $this->setTitle( NS_TALK
);
120 $this->setUserPerm( "createpage" );
121 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
122 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
124 $this->setTitle( NS_TALK
);
125 $this->setUserPerm( "" );
126 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
127 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
129 $this->setTitle( NS_MAIN
);
130 $this->setUserPerm( "createpage" );
131 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
132 $this->assertEquals( [], $res );
134 $this->setTitle( NS_MAIN
);
135 $this->setUserPerm( "createtalk" );
136 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
137 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
139 $this->setUser( $this->userName
);
140 $this->setTitle( NS_TALK
);
141 $this->setUserPerm( "createtalk" );
142 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
143 $this->assertEquals( [], $res );
145 $this->setTitle( NS_TALK
);
146 $this->setUserPerm( "createpage" );
147 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
148 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
150 $this->setTitle( NS_TALK
);
151 $this->setUserPerm( "" );
152 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
153 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
155 $this->setTitle( NS_MAIN
);
156 $this->setUserPerm( "createpage" );
157 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
158 $this->assertEquals( [], $res );
160 $this->setTitle( NS_MAIN
);
161 $this->setUserPerm( "createtalk" );
162 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
163 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
165 $this->setTitle( NS_MAIN
);
166 $this->setUserPerm( "" );
167 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
168 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
170 $this->setUser( 'anon' );
171 $this->setTitle( NS_USER
, $this->userName
. '' );
172 $this->setUserPerm( "" );
173 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
174 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
176 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
177 $this->setUserPerm( "" );
178 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
179 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
181 $this->setTitle( NS_USER
, $this->userName
. '' );
182 $this->setUserPerm( "move-rootuserpages" );
183 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
184 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
186 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
187 $this->setUserPerm( "move-rootuserpages" );
188 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
189 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
191 $this->setTitle( NS_USER
, $this->userName
. '' );
192 $this->setUserPerm( "" );
193 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
194 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
196 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
197 $this->setUserPerm( "" );
198 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
199 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
201 $this->setTitle( NS_USER
, $this->userName
. '' );
202 $this->setUserPerm( "move-rootuserpages" );
203 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
204 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
206 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
207 $this->setUserPerm( "move-rootuserpages" );
208 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
209 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
211 $this->setUser( $this->userName
);
212 $this->setTitle( NS_FILE
, "img.png" );
213 $this->setUserPerm( "" );
214 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
215 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
217 $this->setTitle( NS_FILE
, "img.png" );
218 $this->setUserPerm( "movefile" );
219 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
220 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
222 $this->setUser( 'anon' );
223 $this->setTitle( NS_FILE
, "img.png" );
224 $this->setUserPerm( "" );
225 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
226 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
228 $this->setTitle( NS_FILE
, "img.png" );
229 $this->setUserPerm( "movefile" );
230 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
231 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
233 $this->setUser( $this->userName
);
234 $this->setUserPerm( "move" );
235 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
237 $this->setUserPerm( "" );
238 $this->runGroupPermissions(
240 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
243 $this->setUser( 'anon' );
244 $this->setUserPerm( "move" );
245 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
247 $this->setUserPerm( "" );
248 $this->runGroupPermissions(
250 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
251 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
254 if ( $this->isWikitextNS( NS_MAIN
) ) {
255 // NOTE: some content models don't allow moving
256 // @todo find a Wikitext namespace for testing
258 $this->setTitle( NS_MAIN
);
259 $this->setUser( 'anon' );
260 $this->setUserPerm( "move" );
261 $this->runGroupPermissions( 'move', [] );
263 $this->setUserPerm( "" );
264 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
265 [ [ 'movenologintext' ] ] );
267 $this->setUser( $this->userName
);
268 $this->setUserPerm( "" );
269 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
271 $this->setUserPerm( "move" );
272 $this->runGroupPermissions( 'move', [] );
274 $this->setUser( 'anon' );
275 $this->setUserPerm( 'move' );
276 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
277 $this->assertEquals( [], $res );
279 $this->setUserPerm( '' );
280 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
281 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
284 $this->setTitle( NS_USER
);
285 $this->setUser( $this->userName
);
286 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
287 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
288 $this->assertEquals( [], $res );
290 $this->setUserPerm( "move" );
291 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
292 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
294 $this->setUser( 'anon' );
295 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
296 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
297 $this->assertEquals( [], $res );
299 $this->setTitle( NS_USER
, "User/subpage" );
300 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
301 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
302 $this->assertEquals( [], $res );
304 $this->setUserPerm( "move" );
305 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
306 $this->assertEquals( [], $res );
308 $this->setUser( 'anon' );
311 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
312 [ [ 'badaccess-group0' ] ],
319 "[[$prefix:Administrators|Administrators]]", 1 ],
322 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
323 [ [ 'protect-cantedit' ] ],
326 '' => [ [], [], [], true ]
329 foreach ( [ "edit", "protect", "" ] as $action ) {
330 $this->setUserPerm( null );
331 $this->assertEquals( $check[$action][0],
332 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
333 $this->assertEquals( $check[$action][0],
334 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
335 $this->assertEquals( $check[$action][0],
336 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
338 global $wgGroupPermissions;
339 $old = $wgGroupPermissions;
340 $wgGroupPermissions = [];
342 $this->assertEquals( $check[$action][1],
343 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
344 $this->assertEquals( $check[$action][1],
345 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
346 $this->assertEquals( $check[$action][1],
347 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
348 $wgGroupPermissions = $old;
350 $this->setUserPerm( $action );
351 $this->assertEquals( $check[$action][2],
352 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
353 $this->assertEquals( $check[$action][2],
354 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
355 $this->assertEquals( $check[$action][2],
356 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
358 $this->setUserPerm( $action );
359 $this->assertEquals( $check[$action][3],
360 $this->title
->userCan( $action, $this->user
, true ) );
361 $this->assertEquals( $check[$action][3],
362 $this->title
->quickUserCan( $action, $this->user
) );
363 # count( User::getGroupsWithPermissions( $action ) ) < 1
367 protected function runGroupPermissions( $action, $result, $result2 = null ) {
368 global $wgGroupPermissions;
370 if ( $result2 === null ) {
374 $wgGroupPermissions['autoconfirmed']['move'] = false;
375 $wgGroupPermissions['user']['move'] = false;
376 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
377 $this->assertEquals( $result, $res );
379 $wgGroupPermissions['autoconfirmed']['move'] = true;
380 $wgGroupPermissions['user']['move'] = false;
381 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
382 $this->assertEquals( $result2, $res );
384 $wgGroupPermissions['autoconfirmed']['move'] = true;
385 $wgGroupPermissions['user']['move'] = true;
386 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
387 $this->assertEquals( $result2, $res );
389 $wgGroupPermissions['autoconfirmed']['move'] = false;
390 $wgGroupPermissions['user']['move'] = true;
391 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
392 $this->assertEquals( $result2, $res );
396 * @todo This test method should be split up into separate test methods and
398 * @covers Title::checkSpecialsAndNSPermissions
400 public function testSpecialsAndNSPermissions() {
401 global $wgNamespaceProtection;
402 $this->setUser( $this->userName
);
404 $this->setTitle( NS_SPECIAL
);
406 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
407 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
409 $this->setTitle( NS_MAIN
);
410 $this->setUserPerm( 'bogus' );
411 $this->assertEquals( [],
412 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
414 $this->setTitle( NS_MAIN
);
415 $this->setUserPerm( '' );
416 $this->assertEquals( [ [ 'badaccess-group0' ] ],
417 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
419 $wgNamespaceProtection[NS_USER
] = [ 'bogus' ];
421 $this->setTitle( NS_USER
);
422 $this->setUserPerm( '' );
423 $this->assertEquals( [ [ 'badaccess-group0' ],
424 [ 'namespaceprotected', 'User', 'bogus' ] ],
425 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
427 $this->setTitle( NS_MEDIAWIKI
);
428 $this->setUserPerm( 'bogus' );
429 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
430 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
432 $this->setTitle( NS_MEDIAWIKI
);
433 $this->setUserPerm( 'bogus' );
434 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
435 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
437 $wgNamespaceProtection = null;
439 $this->setUserPerm( 'bogus' );
440 $this->assertEquals( [],
441 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
442 $this->assertEquals( true,
443 $this->title
->userCan( 'bogus', $this->user
) );
445 $this->setUserPerm( '' );
446 $this->assertEquals( [ [ 'badaccess-group0' ] ],
447 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
448 $this->assertEquals( false,
449 $this->title
->userCan( 'bogus', $this->user
) );
453 * @todo This test method should be split up into separate test methods and
455 * @covers Title::checkUserConfigPermissions
457 public function testJsConfigEditPermissions() {
458 $this->setUser( $this->userName
);
460 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
461 $this->runConfigEditPermissions(
462 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
464 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
465 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
466 [ [ 'badaccess-group0' ] ],
468 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
469 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
470 [ [ 'badaccess-group0' ] ],
471 [ [ 'badaccess-groups' ] ]
476 * @todo This test method should be split up into separate test methods and
478 * @covers Title::checkUserConfigPermissions
480 public function testJsonConfigEditPermissions() {
481 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
482 getFormattedNsText( NS_PROJECT
);
483 $this->setUser( $this->userName
);
485 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
486 $this->runConfigEditPermissions(
487 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
489 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
490 [ [ 'badaccess-group0' ] ],
491 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
493 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
494 [ [ 'badaccess-group0' ] ],
495 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
496 [ [ 'badaccess-groups' ] ]
501 * @todo This test method should be split up into separate test methods and
503 * @covers Title::checkUserConfigPermissions
505 public function testCssConfigEditPermissions() {
506 $this->setUser( $this->userName
);
508 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
509 $this->runConfigEditPermissions(
510 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
512 [ [ 'badaccess-group0' ] ],
513 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
514 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
516 [ [ 'badaccess-group0' ] ],
517 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
518 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
519 [ [ 'badaccess-groups' ] ]
524 * @todo This test method should be split up into separate test methods and
526 * @covers Title::checkUserConfigPermissions
528 public function testOtherJsConfigEditPermissions() {
529 $this->setUser( $this->userName
);
531 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
532 $this->runConfigEditPermissions(
533 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
535 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
536 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
537 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
539 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
540 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
541 [ [ 'badaccess-group0' ] ],
542 [ [ 'badaccess-groups' ] ]
547 * @todo This test method should be split up into separate test methods and
549 * @covers Title::checkUserConfigPermissions
551 public function testOtherJsonConfigEditPermissions() {
552 $this->setUser( $this->userName
);
554 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
555 $this->runConfigEditPermissions(
556 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
558 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
559 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
560 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
562 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
563 [ [ 'badaccess-group0' ] ],
564 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
565 [ [ 'badaccess-groups' ] ]
570 * @todo This test method should be split up into separate test methods and
572 * @covers Title::checkUserConfigPermissions
574 public function testOtherCssConfigEditPermissions() {
575 $this->setUser( $this->userName
);
577 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
578 $this->runConfigEditPermissions(
579 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
581 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
582 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
583 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
585 [ [ 'badaccess-group0' ] ],
586 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
587 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
588 [ [ 'badaccess-groups' ] ]
593 * @todo This test method should be split up into separate test methods and
595 * @covers Title::checkUserConfigPermissions
597 public function testOtherNonConfigEditPermissions() {
598 $this->setUser( $this->userName
);
600 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
601 $this->runConfigEditPermissions(
602 [ [ 'badaccess-group0' ] ],
604 [ [ 'badaccess-group0' ] ],
605 [ [ 'badaccess-group0' ] ],
606 [ [ 'badaccess-group0' ] ],
608 [ [ 'badaccess-group0' ] ],
609 [ [ 'badaccess-group0' ] ],
610 [ [ 'badaccess-group0' ] ],
611 [ [ 'badaccess-groups' ] ]
616 * @todo This should use data providers like the other methods here.
617 * @covers Title::checkUserConfigPermissions
619 public function testPatrolActionConfigEditPermissions() {
620 $this->setUser( 'anon' );
621 $this->setTitle( NS_USER
, 'ToPatrolOrNotToPatrol' );
622 $this->runConfigEditPermissions(
623 [ [ 'badaccess-group0' ] ],
625 [ [ 'badaccess-group0' ] ],
626 [ [ 'badaccess-group0' ] ],
627 [ [ 'badaccess-group0' ] ],
629 [ [ 'badaccess-group0' ] ],
630 [ [ 'badaccess-group0' ] ],
631 [ [ 'badaccess-group0' ] ],
632 [ [ 'badaccess-groups' ] ]
636 protected function runConfigEditPermissions(
646 $this->setUserPerm( '' );
647 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
648 $this->assertEquals( $resultNone, $result );
650 $this->setUserPerm( 'editmyusercss' );
651 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
652 $this->assertEquals( $resultMyCss, $result );
654 $this->setUserPerm( 'editmyuserjson' );
655 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
656 $this->assertEquals( $resultMyJson, $result );
658 $this->setUserPerm( 'editmyuserjs' );
659 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
660 $this->assertEquals( $resultMyJs, $result );
662 $this->setUserPerm( 'editusercss' );
663 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
664 $this->assertEquals( $resultUserCss, $result );
666 $this->setUserPerm( 'edituserjson' );
667 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
668 $this->assertEquals( $resultUserJson, $result );
670 $this->setUserPerm( 'edituserjs' );
671 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
672 $this->assertEquals( $resultUserJs, $result );
674 $this->setUserPerm( '' );
675 $result = $this->title
->getUserPermissionsErrors( 'patrol', $this->user
);
676 $this->assertEquals( reset( $resultPatrol[0] ), reset( $result[0] ) );
678 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
679 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
680 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
684 * @todo This test method should be split up into separate test methods and
687 * This test is failing per T201776.
690 * @covers Title::checkPageRestrictions
692 public function testPageRestrictions() {
693 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
694 getFormattedNsText( NS_PROJECT
);
696 $this->setTitle( NS_MAIN
);
697 $this->title
->mRestrictionsLoaded
= true;
698 $this->setUserPerm( "edit" );
699 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
701 $this->assertEquals( [],
702 $this->title
->getUserPermissionsErrors( 'edit',
705 $this->assertEquals( true,
706 $this->title
->quickUserCan( 'edit', $this->user
) );
707 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
708 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
710 $this->assertEquals( [ [ 'badaccess-group0' ],
711 [ 'protectedpagetext', 'bogus', 'bogus' ],
712 [ 'protectedpagetext', 'editprotected', 'bogus' ],
713 [ 'protectedpagetext', 'protect', 'bogus' ] ],
714 $this->title
->getUserPermissionsErrors( 'bogus',
716 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
717 [ 'protectedpagetext', 'editprotected', 'edit' ],
718 [ 'protectedpagetext', 'protect', 'edit' ] ],
719 $this->title
->getUserPermissionsErrors( 'edit',
721 $this->setUserPerm( "" );
722 $this->assertEquals( [ [ 'badaccess-group0' ],
723 [ 'protectedpagetext', 'bogus', 'bogus' ],
724 [ 'protectedpagetext', 'editprotected', 'bogus' ],
725 [ 'protectedpagetext', 'protect', 'bogus' ] ],
726 $this->title
->getUserPermissionsErrors( 'bogus',
728 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
729 [ 'protectedpagetext', 'bogus', 'edit' ],
730 [ 'protectedpagetext', 'editprotected', 'edit' ],
731 [ 'protectedpagetext', 'protect', 'edit' ] ],
732 $this->title
->getUserPermissionsErrors( 'edit',
734 $this->setUserPerm( [ "edit", "editprotected" ] );
735 $this->assertEquals( [ [ 'badaccess-group0' ],
736 [ 'protectedpagetext', 'bogus', 'bogus' ],
737 [ 'protectedpagetext', 'protect', 'bogus' ] ],
738 $this->title
->getUserPermissionsErrors( 'bogus',
740 $this->assertEquals( [
741 [ 'protectedpagetext', 'bogus', 'edit' ],
742 [ 'protectedpagetext', 'protect', 'edit' ] ],
743 $this->title
->getUserPermissionsErrors( 'edit',
746 $this->title
->mCascadeRestriction
= true;
747 $this->setUserPerm( "edit" );
748 $this->assertEquals( false,
749 $this->title
->quickUserCan( 'bogus', $this->user
) );
750 $this->assertEquals( false,
751 $this->title
->quickUserCan( 'edit', $this->user
) );
752 $this->assertEquals( [ [ 'badaccess-group0' ],
753 [ 'protectedpagetext', 'bogus', 'bogus' ],
754 [ 'protectedpagetext', 'editprotected', 'bogus' ],
755 [ 'protectedpagetext', 'protect', 'bogus' ] ],
756 $this->title
->getUserPermissionsErrors( 'bogus',
758 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
759 [ 'protectedpagetext', 'editprotected', 'edit' ],
760 [ 'protectedpagetext', 'protect', 'edit' ] ],
761 $this->title
->getUserPermissionsErrors( 'edit',
764 $this->setUserPerm( [ "edit", "editprotected" ] );
765 $this->assertEquals( false,
766 $this->title
->quickUserCan( 'bogus', $this->user
) );
767 $this->assertEquals( false,
768 $this->title
->quickUserCan( 'edit', $this->user
) );
769 $this->assertEquals( [ [ 'badaccess-group0' ],
770 [ 'protectedpagetext', 'bogus', 'bogus' ],
771 [ 'protectedpagetext', 'protect', 'bogus' ],
772 [ 'protectedpagetext', 'protect', 'bogus' ] ],
773 $this->title
->getUserPermissionsErrors( 'bogus',
775 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
776 [ 'protectedpagetext', 'protect', 'edit' ],
777 [ 'protectedpagetext', 'protect', 'edit' ] ],
778 $this->title
->getUserPermissionsErrors( 'edit',
783 * @covers Title::checkCascadingSourcesRestrictions
785 public function testCascadingSourcesRestrictions() {
786 $this->setTitle( NS_MAIN
, "test page" );
787 $this->setUserPerm( [ "edit", "bogus" ] );
789 $this->title
->mCascadeSources
= [
790 Title
::makeTitle( NS_MAIN
, "Bogus" ),
791 Title
::makeTitle( NS_MAIN
, "UnBogus" )
793 $this->title
->mCascadingRestrictions
= [
794 "bogus" => [ 'bogus', "sysop", "protect", "" ]
797 $this->assertEquals( false,
798 $this->title
->userCan( 'bogus', $this->user
) );
799 $this->assertEquals( [
800 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
801 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
802 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
803 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
805 $this->assertEquals( true,
806 $this->title
->userCan( 'edit', $this->user
) );
807 $this->assertEquals( [],
808 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
812 * @todo This test method should be split up into separate test methods and
814 * @covers Title::checkActionPermissions
816 public function testActionPermissions() {
817 $this->setUserPerm( [ "createpage" ] );
818 $this->setTitle( NS_MAIN
, "test page" );
819 $this->title
->mTitleProtection
['permission'] = '';
820 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
821 $this->title
->mTitleProtection
['expiry'] = 'infinity';
822 $this->title
->mTitleProtection
['reason'] = 'test';
823 $this->title
->mCascadeRestriction
= false;
825 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
826 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
827 $this->assertEquals( false,
828 $this->title
->userCan( 'create', $this->user
) );
830 $this->title
->mTitleProtection
['permission'] = 'editprotected';
831 $this->setUserPerm( [ 'createpage', 'protect' ] );
832 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
833 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
834 $this->assertEquals( false,
835 $this->title
->userCan( 'create', $this->user
) );
837 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
838 $this->assertEquals( [],
839 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
840 $this->assertEquals( true,
841 $this->title
->userCan( 'create', $this->user
) );
843 $this->setUserPerm( [ 'createpage' ] );
844 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
845 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
846 $this->assertEquals( false,
847 $this->title
->userCan( 'create', $this->user
) );
849 $this->setTitle( NS_MEDIA
, "test page" );
850 $this->setUserPerm( [ "move" ] );
851 $this->assertEquals( false,
852 $this->title
->userCan( 'move', $this->user
) );
853 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
854 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
856 $this->setTitle( NS_HELP
, "test page" );
857 $this->assertEquals( [],
858 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
859 $this->assertEquals( true,
860 $this->title
->userCan( 'move', $this->user
) );
862 $this->title
->mInterwiki
= "no";
863 $this->assertEquals( [ [ 'immobile-source-page' ] ],
864 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
865 $this->assertEquals( false,
866 $this->title
->userCan( 'move', $this->user
) );
868 $this->setTitle( NS_MEDIA
, "test page" );
869 $this->assertEquals( false,
870 $this->title
->userCan( 'move-target', $this->user
) );
871 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
872 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
874 $this->setTitle( NS_HELP
, "test page" );
875 $this->assertEquals( [],
876 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
877 $this->assertEquals( true,
878 $this->title
->userCan( 'move-target', $this->user
) );
880 $this->title
->mInterwiki
= "no";
881 $this->assertEquals( [ [ 'immobile-target-page' ] ],
882 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
883 $this->assertEquals( false,
884 $this->title
->userCan( 'move-target', $this->user
) );
888 * @covers Title::checkUserBlock
890 public function testUserBlock() {
891 $this->setMwGlobals( [
892 'wgEmailConfirmToEdit' => true,
893 'wgEmailAuthentication' => true,
896 $this->setUserPerm( [ 'createpage', 'edit', 'move', 'rollback', 'patrol', 'upload', 'purge' ] );
897 $this->setTitle( NS_HELP
, "test page" );
899 # $wgEmailConfirmToEdit only applies to 'edit' action
900 $this->assertEquals( [],
901 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
902 $this->assertContains( [ 'confirmedittext' ],
903 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
905 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
906 $this->assertNotContains( [ 'confirmedittext' ],
907 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
909 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
910 $this->assertEquals( [],
911 $this->title
->getUserPermissionsErrors( 'move-target',
917 $this->user
->mBlockedby
= $this->user
->getId();
918 $this->user
->mBlock
= new Block( [
919 'address' => '127.0.8.1',
920 'by' => $this->user
->getId(),
921 'reason' => 'no reason given',
922 'timestamp' => $prev +
3600,
926 $this->user
->mBlock
->mTimestamp
= 0;
927 $this->assertEquals( [ [ 'autoblockedtext',
928 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
929 'Useruser', null, 'infinite', '127.0.8.1',
930 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
931 $this->title
->getUserPermissionsErrors( 'move-target',
934 $this->assertEquals( false, $this->title
->userCan( 'move-target', $this->user
) );
935 // quickUserCan should ignore user blocks
936 $this->assertEquals( true, $this->title
->quickUserCan( 'move-target', $this->user
) );
938 global $wgLocalTZoffset;
939 $wgLocalTZoffset = -60;
940 $this->user
->mBlockedby
= $this->user
->getName();
941 $this->user
->mBlock
= new Block( [
942 'address' => '127.0.8.1',
943 'by' => $this->user
->getId(),
944 'reason' => 'no reason given',
949 $this->assertEquals( [ [ 'blockedtext',
950 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
951 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
952 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
953 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
954 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
955 # $user->blockedFor() == ''
956 # $user->mBlock->mExpiry == 'infinity'
958 $this->user
->mBlockedby
= $this->user
->getName();
959 $this->user
->mBlock
= new Block( [
960 'address' => '127.0.8.1',
961 'by' => $this->user
->getId(),
962 'reason' => 'no reason given',
966 'systemBlock' => 'test',
969 $errors = [ [ 'systemblockedtext',
970 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
971 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
972 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
974 $this->assertEquals( $errors,
975 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
976 $this->assertEquals( $errors,
977 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
978 $this->assertEquals( $errors,
979 $this->title
->getUserPermissionsErrors( 'rollback', $this->user
) );
980 $this->assertEquals( $errors,
981 $this->title
->getUserPermissionsErrors( 'patrol', $this->user
) );
982 $this->assertEquals( $errors,
983 $this->title
->getUserPermissionsErrors( 'upload', $this->user
) );
984 $this->assertEquals( [],
985 $this->title
->getUserPermissionsErrors( 'purge', $this->user
) );
987 // partial block message test
988 $this->user
->mBlockedby
= $this->user
->getName();
989 $this->user
->mBlock
= new Block( [
990 'address' => '127.0.8.1',
991 'by' => $this->user
->getId(),
992 'reason' => 'no reason given',
998 $this->assertEquals( [],
999 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
1000 $this->assertEquals( [],
1001 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
1002 $this->assertEquals( [],
1003 $this->title
->getUserPermissionsErrors( 'rollback', $this->user
) );
1004 $this->assertEquals( [],
1005 $this->title
->getUserPermissionsErrors( 'patrol', $this->user
) );
1006 $this->assertEquals( [],
1007 $this->title
->getUserPermissionsErrors( 'upload', $this->user
) );
1008 $this->assertEquals( [],
1009 $this->title
->getUserPermissionsErrors( 'purge', $this->user
) );
1011 $this->user
->mBlock
->setRestrictions( [
1012 ( new PageRestriction( 0, $this->title
->getArticleID() ) )->setTitle( $this->title
),
1015 $errors = [ [ 'blockedtext-partial',
1016 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1017 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1018 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ];
1020 $this->assertEquals( $errors,
1021 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
1022 $this->assertEquals( $errors,
1023 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
1024 $this->assertEquals( $errors,
1025 $this->title
->getUserPermissionsErrors( 'rollback', $this->user
) );
1026 $this->assertEquals( $errors,
1027 $this->title
->getUserPermissionsErrors( 'patrol', $this->user
) );
1028 $this->assertEquals( [],
1029 $this->title
->getUserPermissionsErrors( 'upload', $this->user
) );
1030 $this->assertEquals( [],
1031 $this->title
->getUserPermissionsErrors( 'purge', $this->user
) );