3 use MediaWiki\MediaWikiServices
;
8 * @covers Title::getUserPermissionsErrors
9 * @covers Title::getUserPermissionsErrorsInternal
11 class TitlePermissionTest
extends MediaWikiLangTestCase
{
16 protected $userName, $altUserName;
26 protected $user, $anonUser, $userUser, $altUser;
28 protected function setUp() {
32 $localOffset = date( 'Z' ) / 60;
34 $this->setMwGlobals( [
35 'wgLocaltimezone' => $localZone,
36 'wgLocalTZoffset' => $localOffset,
37 'wgNamespaceProtection' => [
38 NS_MEDIAWIKI
=> 'editinterface',
41 // Without this testUserBlock will use a non-English context on non-English MediaWiki
42 // installations (because of how Title::checkUserBlock is implemented) and fail.
43 RequestContext
::resetMain();
45 $this->userName
= 'Useruser';
46 $this->altUserName
= 'Altuseruser';
47 date_default_timezone_set( $localZone );
49 $this->title
= Title
::makeTitle( NS_MAIN
, "Main Page" );
50 if ( !isset( $this->userUser
) ||
!( $this->userUser
instanceof User
) ) {
51 $this->userUser
= User
::newFromName( $this->userName
);
53 if ( !$this->userUser
->getId() ) {
54 $this->userUser
= User
::createNew( $this->userName
, [
55 "email" => "test@example.com",
56 "real_name" => "Test User" ] );
57 $this->userUser
->load();
60 $this->altUser
= User
::newFromName( $this->altUserName
);
61 if ( !$this->altUser
->getId() ) {
62 $this->altUser
= User
::createNew( $this->altUserName
, [
63 "email" => "alttest@example.com",
64 "real_name" => "Test User Alt" ] );
65 $this->altUser
->load();
68 $this->anonUser
= User
::newFromId( 0 );
70 $this->user
= $this->userUser
;
72 $this->overrideMwServices();
75 protected function setUserPerm( $perm ) {
76 // Setting member variables is evil!!!
78 if ( is_array( $perm ) ) {
79 $this->user
->mRights
= $perm;
81 $this->user
->mRights
= [ $perm ];
85 protected function setTitle( $ns, $title = "Main_Page" ) {
86 $this->title
= Title
::makeTitle( $ns, $title );
89 protected function setUser( $userName = null ) {
90 if ( $userName === 'anon' ) {
91 $this->user
= $this->anonUser
;
92 } elseif ( $userName === null ||
$userName === $this->userName
) {
93 $this->user
= $this->userUser
;
95 $this->user
= $this->altUser
;
100 * @todo This test method should be split up into separate test methods and
102 * @covers Title::checkQuickPermissions
104 public function testQuickPermissions() {
105 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
106 getFormattedNsText( NS_PROJECT
);
108 $this->setUser( 'anon' );
109 $this->setTitle( NS_TALK
);
110 $this->setUserPerm( "createtalk" );
111 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
112 $this->assertEquals( [], $res );
114 $this->setTitle( NS_TALK
);
115 $this->setUserPerm( "createpage" );
116 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
117 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
119 $this->setTitle( NS_TALK
);
120 $this->setUserPerm( "" );
121 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
122 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
124 $this->setTitle( NS_MAIN
);
125 $this->setUserPerm( "createpage" );
126 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
127 $this->assertEquals( [], $res );
129 $this->setTitle( NS_MAIN
);
130 $this->setUserPerm( "createtalk" );
131 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
132 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
134 $this->setUser( $this->userName
);
135 $this->setTitle( NS_TALK
);
136 $this->setUserPerm( "createtalk" );
137 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
138 $this->assertEquals( [], $res );
140 $this->setTitle( NS_TALK
);
141 $this->setUserPerm( "createpage" );
142 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
143 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
145 $this->setTitle( NS_TALK
);
146 $this->setUserPerm( "" );
147 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
148 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
150 $this->setTitle( NS_MAIN
);
151 $this->setUserPerm( "createpage" );
152 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
153 $this->assertEquals( [], $res );
155 $this->setTitle( NS_MAIN
);
156 $this->setUserPerm( "createtalk" );
157 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
158 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
160 $this->setTitle( NS_MAIN
);
161 $this->setUserPerm( "" );
162 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
163 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
165 $this->setUser( 'anon' );
166 $this->setTitle( NS_USER
, $this->userName
. '' );
167 $this->setUserPerm( "" );
168 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
169 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
171 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
172 $this->setUserPerm( "" );
173 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
174 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
176 $this->setTitle( NS_USER
, $this->userName
. '' );
177 $this->setUserPerm( "move-rootuserpages" );
178 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
179 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
181 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
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
. '' );
187 $this->setUserPerm( "" );
188 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
189 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
191 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
192 $this->setUserPerm( "" );
193 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
194 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
196 $this->setTitle( NS_USER
, $this->userName
. '' );
197 $this->setUserPerm( "move-rootuserpages" );
198 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
199 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
201 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
202 $this->setUserPerm( "move-rootuserpages" );
203 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
204 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
206 $this->setUser( $this->userName
);
207 $this->setTitle( NS_FILE
, "img.png" );
208 $this->setUserPerm( "" );
209 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
210 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
212 $this->setTitle( NS_FILE
, "img.png" );
213 $this->setUserPerm( "movefile" );
214 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
215 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
217 $this->setUser( 'anon' );
218 $this->setTitle( NS_FILE
, "img.png" );
219 $this->setUserPerm( "" );
220 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
221 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
223 $this->setTitle( NS_FILE
, "img.png" );
224 $this->setUserPerm( "movefile" );
225 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
226 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
228 $this->setUser( $this->userName
);
229 $this->setUserPerm( "move" );
230 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
232 $this->setUserPerm( "" );
233 $this->runGroupPermissions(
235 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
238 $this->setUser( 'anon' );
239 $this->setUserPerm( "move" );
240 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
242 $this->setUserPerm( "" );
243 $this->runGroupPermissions(
245 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
246 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
249 if ( $this->isWikitextNS( NS_MAIN
) ) {
250 // NOTE: some content models don't allow moving
251 // @todo find a Wikitext namespace for testing
253 $this->setTitle( NS_MAIN
);
254 $this->setUser( 'anon' );
255 $this->setUserPerm( "move" );
256 $this->runGroupPermissions( 'move', [] );
258 $this->setUserPerm( "" );
259 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
260 [ [ 'movenologintext' ] ] );
262 $this->setUser( $this->userName
);
263 $this->setUserPerm( "" );
264 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
266 $this->setUserPerm( "move" );
267 $this->runGroupPermissions( 'move', [] );
269 $this->setUser( 'anon' );
270 $this->setUserPerm( 'move' );
271 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
272 $this->assertEquals( [], $res );
274 $this->setUserPerm( '' );
275 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
276 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
279 $this->setTitle( NS_USER
);
280 $this->setUser( $this->userName
);
281 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
282 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
283 $this->assertEquals( [], $res );
285 $this->setUserPerm( "move" );
286 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
287 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
289 $this->setUser( 'anon' );
290 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
291 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
292 $this->assertEquals( [], $res );
294 $this->setTitle( NS_USER
, "User/subpage" );
295 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
296 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
297 $this->assertEquals( [], $res );
299 $this->setUserPerm( "move" );
300 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
301 $this->assertEquals( [], $res );
303 $this->setUser( 'anon' );
306 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
307 [ [ 'badaccess-group0' ] ],
314 "[[$prefix:Administrators|Administrators]]", 1 ],
317 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
318 [ [ 'protect-cantedit' ] ],
321 '' => [ [], [], [], true ]
324 foreach ( [ "edit", "protect", "" ] as $action ) {
325 $this->setUserPerm( null );
326 $this->assertEquals( $check[$action][0],
327 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
328 $this->assertEquals( $check[$action][0],
329 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
330 $this->assertEquals( $check[$action][0],
331 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
333 global $wgGroupPermissions;
334 $old = $wgGroupPermissions;
335 $wgGroupPermissions = [];
337 $this->assertEquals( $check[$action][1],
338 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
339 $this->assertEquals( $check[$action][1],
340 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
341 $this->assertEquals( $check[$action][1],
342 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
343 $wgGroupPermissions = $old;
345 $this->setUserPerm( $action );
346 $this->assertEquals( $check[$action][2],
347 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
348 $this->assertEquals( $check[$action][2],
349 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
350 $this->assertEquals( $check[$action][2],
351 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
353 $this->setUserPerm( $action );
354 $this->assertEquals( $check[$action][3],
355 $this->title
->userCan( $action, $this->user
, true ) );
356 $this->assertEquals( $check[$action][3],
357 $this->title
->quickUserCan( $action, $this->user
) );
358 # count( User::getGroupsWithPermissions( $action ) ) < 1
362 protected function runGroupPermissions( $action, $result, $result2 = null ) {
363 global $wgGroupPermissions;
365 if ( $result2 === null ) {
369 $wgGroupPermissions['autoconfirmed']['move'] = false;
370 $wgGroupPermissions['user']['move'] = false;
371 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
372 $this->assertEquals( $result, $res );
374 $wgGroupPermissions['autoconfirmed']['move'] = true;
375 $wgGroupPermissions['user']['move'] = false;
376 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
377 $this->assertEquals( $result2, $res );
379 $wgGroupPermissions['autoconfirmed']['move'] = true;
380 $wgGroupPermissions['user']['move'] = true;
381 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
382 $this->assertEquals( $result2, $res );
384 $wgGroupPermissions['autoconfirmed']['move'] = false;
385 $wgGroupPermissions['user']['move'] = true;
386 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
387 $this->assertEquals( $result2, $res );
391 * @todo This test method should be split up into separate test methods and
393 * @covers Title::checkSpecialsAndNSPermissions
395 public function testSpecialsAndNSPermissions() {
396 global $wgNamespaceProtection;
397 $this->setUser( $this->userName
);
399 $this->setTitle( NS_SPECIAL
);
401 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
402 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
404 $this->setTitle( NS_MAIN
);
405 $this->setUserPerm( 'bogus' );
406 $this->assertEquals( [],
407 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
409 $this->setTitle( NS_MAIN
);
410 $this->setUserPerm( '' );
411 $this->assertEquals( [ [ 'badaccess-group0' ] ],
412 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
414 $wgNamespaceProtection[NS_USER
] = [ 'bogus' ];
416 $this->setTitle( NS_USER
);
417 $this->setUserPerm( '' );
418 $this->assertEquals( [ [ 'badaccess-group0' ],
419 [ 'namespaceprotected', 'User', 'bogus' ] ],
420 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
422 $this->setTitle( NS_MEDIAWIKI
);
423 $this->setUserPerm( 'bogus' );
424 $this->assertEquals( [ [ 'protectedinterface', '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 $wgNamespaceProtection = null;
434 $this->setUserPerm( 'bogus' );
435 $this->assertEquals( [],
436 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
437 $this->assertEquals( true,
438 $this->title
->userCan( 'bogus', $this->user
) );
440 $this->setUserPerm( '' );
441 $this->assertEquals( [ [ 'badaccess-group0' ] ],
442 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
443 $this->assertEquals( false,
444 $this->title
->userCan( 'bogus', $this->user
) );
448 * @todo This test method should be split up into separate test methods and
450 * @covers Title::checkUserConfigPermissions
452 public function testJsConfigEditPermissions() {
453 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
454 getFormattedNsText( NS_PROJECT
);
455 $this->setUser( $this->userName
);
457 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
458 $this->runConfigEditPermissions(
459 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
461 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
462 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
463 [ [ 'badaccess-group0' ] ],
465 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
466 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
467 [ [ 'badaccess-group0' ] ],
468 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
473 * @todo This test method should be split up into separate test methods and
475 * @covers Title::checkUserConfigPermissions
477 public function testJsonConfigEditPermissions() {
478 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
479 getFormattedNsText( NS_PROJECT
);
480 $this->setUser( $this->userName
);
482 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
483 $this->runConfigEditPermissions(
484 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
486 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
487 [ [ 'badaccess-group0' ] ],
488 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
490 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
491 [ [ 'badaccess-group0' ] ],
492 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
493 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
498 * @todo This test method should be split up into separate test methods and
500 * @covers Title::checkUserConfigPermissions
502 public function testCssConfigEditPermissions() {
503 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
504 getFormattedNsText( NS_PROJECT
);
505 $this->setUser( $this->userName
);
507 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
508 $this->runConfigEditPermissions(
509 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
511 [ [ 'badaccess-group0' ] ],
512 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
513 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
515 [ [ 'badaccess-group0' ] ],
516 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
517 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
518 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
523 * @todo This test method should be split up into separate test methods and
525 * @covers Title::checkUserConfigPermissions
527 public function testOtherJsConfigEditPermissions() {
528 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
529 getFormattedNsText( NS_PROJECT
);
530 $this->setUser( $this->userName
);
532 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
533 $this->runConfigEditPermissions(
534 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
536 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
537 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
538 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
540 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
541 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
542 [ [ 'badaccess-group0' ] ],
543 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
548 * @todo This test method should be split up into separate test methods and
550 * @covers Title::checkUserConfigPermissions
552 public function testOtherJsonConfigEditPermissions() {
553 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
554 getFormattedNsText( NS_PROJECT
);
555 $this->setUser( $this->userName
);
557 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
558 $this->runConfigEditPermissions(
559 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
561 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
562 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
563 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
565 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
566 [ [ 'badaccess-group0' ] ],
567 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
568 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
573 * @todo This test method should be split up into separate test methods and
575 * @covers Title::checkUserConfigPermissions
577 public function testOtherCssConfigEditPermissions() {
578 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
579 getFormattedNsText( NS_PROJECT
);
580 $this->setUser( $this->userName
);
582 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
583 $this->runConfigEditPermissions(
584 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
586 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
587 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
588 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
590 [ [ 'badaccess-group0' ] ],
591 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
592 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
593 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
598 * @todo This test method should be split up into separate test methods and
600 * @covers Title::checkUserConfigPermissions
602 public function testOtherNonConfigEditPermissions() {
603 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
604 getFormattedNsText( NS_PROJECT
);
605 $this->setUser( $this->userName
);
607 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
608 $this->runConfigEditPermissions(
609 [ [ 'badaccess-group0' ] ],
611 [ [ 'badaccess-group0' ] ],
612 [ [ 'badaccess-group0' ] ],
613 [ [ 'badaccess-group0' ] ],
615 [ [ 'badaccess-group0' ] ],
616 [ [ 'badaccess-group0' ] ],
617 [ [ 'badaccess-group0' ] ],
618 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
623 * @todo This should use data providers like the other methods here.
624 * @covers Title::checkUserConfigPermissions
626 public function testPatrolActionConfigEditPermissions() {
627 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
628 getFormattedNsText( NS_PROJECT
);
629 $this->setUser( 'anon' );
630 $this->setTitle( NS_USER
, 'ToPatrolOrNotToPatrol' );
631 $this->runConfigEditPermissions(
632 [ [ 'badaccess-group0' ] ],
634 [ [ 'badaccess-group0' ] ],
635 [ [ 'badaccess-group0' ] ],
636 [ [ 'badaccess-group0' ] ],
638 [ [ 'badaccess-group0' ] ],
639 [ [ 'badaccess-group0' ] ],
640 [ [ 'badaccess-group0' ] ],
641 [ [ 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ] ]
645 protected function runConfigEditPermissions(
655 $this->setUserPerm( '' );
656 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
657 $this->assertEquals( $resultNone, $result );
659 $this->setUserPerm( 'editmyusercss' );
660 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
661 $this->assertEquals( $resultMyCss, $result );
663 $this->setUserPerm( 'editmyuserjson' );
664 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
665 $this->assertEquals( $resultMyJson, $result );
667 $this->setUserPerm( 'editmyuserjs' );
668 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
669 $this->assertEquals( $resultMyJs, $result );
671 $this->setUserPerm( 'editusercss' );
672 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
673 $this->assertEquals( $resultUserCss, $result );
675 $this->setUserPerm( 'edituserjson' );
676 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
677 $this->assertEquals( $resultUserJson, $result );
679 $this->setUserPerm( 'edituserjs' );
680 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
681 $this->assertEquals( $resultUserJs, $result );
683 $this->setUserPerm( '' );
684 $result = $this->title
->getUserPermissionsErrors( 'patrol', $this->user
);
685 $this->assertEquals( $resultPatrol, $result );
687 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
688 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
689 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
693 * @todo This test method should be split up into separate test methods and
695 * @covers Title::checkPageRestrictions
697 public function testPageRestrictions() {
698 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
699 getFormattedNsText( NS_PROJECT
);
701 $this->setTitle( NS_MAIN
);
702 $this->title
->mRestrictionsLoaded
= true;
703 $this->setUserPerm( "edit" );
704 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
706 $this->assertEquals( [],
707 $this->title
->getUserPermissionsErrors( 'edit',
710 $this->assertEquals( true,
711 $this->title
->quickUserCan( 'edit', $this->user
) );
712 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
713 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
715 $this->assertEquals( [ [ 'badaccess-group0' ],
716 [ 'protectedpagetext', 'bogus', 'bogus' ],
717 [ 'protectedpagetext', 'editprotected', 'bogus' ],
718 [ 'protectedpagetext', 'protect', 'bogus' ] ],
719 $this->title
->getUserPermissionsErrors( 'bogus',
721 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
722 [ 'protectedpagetext', 'editprotected', 'edit' ],
723 [ 'protectedpagetext', 'protect', 'edit' ] ],
724 $this->title
->getUserPermissionsErrors( 'edit',
726 $this->setUserPerm( "" );
727 $this->assertEquals( [ [ 'badaccess-group0' ],
728 [ 'protectedpagetext', 'bogus', 'bogus' ],
729 [ 'protectedpagetext', 'editprotected', 'bogus' ],
730 [ 'protectedpagetext', 'protect', 'bogus' ] ],
731 $this->title
->getUserPermissionsErrors( 'bogus',
733 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
734 [ 'protectedpagetext', 'bogus', 'edit' ],
735 [ 'protectedpagetext', 'editprotected', 'edit' ],
736 [ 'protectedpagetext', 'protect', 'edit' ] ],
737 $this->title
->getUserPermissionsErrors( 'edit',
739 $this->setUserPerm( [ "edit", "editprotected" ] );
740 $this->assertEquals( [ [ 'badaccess-group0' ],
741 [ 'protectedpagetext', 'bogus', 'bogus' ],
742 [ 'protectedpagetext', 'protect', 'bogus' ] ],
743 $this->title
->getUserPermissionsErrors( 'bogus',
745 $this->assertEquals( [
746 [ 'protectedpagetext', 'bogus', 'edit' ],
747 [ 'protectedpagetext', 'protect', 'edit' ] ],
748 $this->title
->getUserPermissionsErrors( 'edit',
751 $this->title
->mCascadeRestriction
= true;
752 $this->setUserPerm( "edit" );
753 $this->assertEquals( false,
754 $this->title
->quickUserCan( 'bogus', $this->user
) );
755 $this->assertEquals( false,
756 $this->title
->quickUserCan( 'edit', $this->user
) );
757 $this->assertEquals( [ [ 'badaccess-group0' ],
758 [ 'protectedpagetext', 'bogus', 'bogus' ],
759 [ 'protectedpagetext', 'editprotected', 'bogus' ],
760 [ 'protectedpagetext', 'protect', 'bogus' ] ],
761 $this->title
->getUserPermissionsErrors( 'bogus',
763 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
764 [ 'protectedpagetext', 'editprotected', 'edit' ],
765 [ 'protectedpagetext', 'protect', 'edit' ] ],
766 $this->title
->getUserPermissionsErrors( 'edit',
769 $this->setUserPerm( [ "edit", "editprotected" ] );
770 $this->assertEquals( false,
771 $this->title
->quickUserCan( 'bogus', $this->user
) );
772 $this->assertEquals( false,
773 $this->title
->quickUserCan( 'edit', $this->user
) );
774 $this->assertEquals( [ [ 'badaccess-group0' ],
775 [ 'protectedpagetext', 'bogus', 'bogus' ],
776 [ 'protectedpagetext', 'protect', 'bogus' ],
777 [ 'protectedpagetext', 'protect', 'bogus' ] ],
778 $this->title
->getUserPermissionsErrors( 'bogus',
780 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
781 [ 'protectedpagetext', 'protect', 'edit' ],
782 [ 'protectedpagetext', 'protect', 'edit' ] ],
783 $this->title
->getUserPermissionsErrors( 'edit',
788 * @covers Title::checkCascadingSourcesRestrictions
790 public function testCascadingSourcesRestrictions() {
791 $this->setTitle( NS_MAIN
, "test page" );
792 $this->setUserPerm( [ "edit", "bogus" ] );
794 $this->title
->mCascadeSources
= [
795 Title
::makeTitle( NS_MAIN
, "Bogus" ),
796 Title
::makeTitle( NS_MAIN
, "UnBogus" )
798 $this->title
->mCascadingRestrictions
= [
799 "bogus" => [ 'bogus', "sysop", "protect", "" ]
802 $this->assertEquals( false,
803 $this->title
->userCan( 'bogus', $this->user
) );
804 $this->assertEquals( [
805 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
806 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
807 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
808 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
810 $this->assertEquals( true,
811 $this->title
->userCan( 'edit', $this->user
) );
812 $this->assertEquals( [],
813 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
817 * @todo This test method should be split up into separate test methods and
819 * @covers Title::checkActionPermissions
821 public function testActionPermissions() {
822 $this->setUserPerm( [ "createpage" ] );
823 $this->setTitle( NS_MAIN
, "test page" );
824 $this->title
->mTitleProtection
['permission'] = '';
825 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
826 $this->title
->mTitleProtection
['expiry'] = 'infinity';
827 $this->title
->mTitleProtection
['reason'] = 'test';
828 $this->title
->mCascadeRestriction
= false;
830 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
831 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
832 $this->assertEquals( false,
833 $this->title
->userCan( 'create', $this->user
) );
835 $this->title
->mTitleProtection
['permission'] = 'editprotected';
836 $this->setUserPerm( [ 'createpage', 'protect' ] );
837 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
838 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
839 $this->assertEquals( false,
840 $this->title
->userCan( 'create', $this->user
) );
842 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
843 $this->assertEquals( [],
844 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
845 $this->assertEquals( true,
846 $this->title
->userCan( 'create', $this->user
) );
848 $this->setUserPerm( [ 'createpage' ] );
849 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
850 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
851 $this->assertEquals( false,
852 $this->title
->userCan( 'create', $this->user
) );
854 $this->setTitle( NS_MEDIA
, "test page" );
855 $this->setUserPerm( [ "move" ] );
856 $this->assertEquals( false,
857 $this->title
->userCan( 'move', $this->user
) );
858 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
859 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
861 $this->setTitle( NS_HELP
, "test page" );
862 $this->assertEquals( [],
863 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
864 $this->assertEquals( true,
865 $this->title
->userCan( 'move', $this->user
) );
867 $this->title
->mInterwiki
= "no";
868 $this->assertEquals( [ [ 'immobile-source-page' ] ],
869 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
870 $this->assertEquals( false,
871 $this->title
->userCan( 'move', $this->user
) );
873 $this->setTitle( NS_MEDIA
, "test page" );
874 $this->assertEquals( false,
875 $this->title
->userCan( 'move-target', $this->user
) );
876 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
877 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
879 $this->setTitle( NS_HELP
, "test page" );
880 $this->assertEquals( [],
881 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
882 $this->assertEquals( true,
883 $this->title
->userCan( 'move-target', $this->user
) );
885 $this->title
->mInterwiki
= "no";
886 $this->assertEquals( [ [ 'immobile-target-page' ] ],
887 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
888 $this->assertEquals( false,
889 $this->title
->userCan( 'move-target', $this->user
) );
893 * @covers Title::checkUserBlock
895 public function testUserBlock() {
896 $this->setMwGlobals( [
897 'wgEmailConfirmToEdit' => true,
898 'wgEmailAuthentication' => true,
901 $this->setUserPerm( [ "createpage", "move" ] );
902 $this->setTitle( NS_HELP
, "test page" );
904 # $wgEmailConfirmToEdit only applies to 'edit' action
905 $this->assertEquals( [],
906 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
907 $this->assertContains( [ 'confirmedittext' ],
908 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
910 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
911 $this->assertNotContains( [ 'confirmedittext' ],
912 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
914 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
915 $this->assertEquals( [],
916 $this->title
->getUserPermissionsErrors( 'move-target',
922 $this->user
->mBlockedby
= $this->user
->getId();
923 $this->user
->mBlock
= new Block( [
924 'address' => '127.0.8.1',
925 'by' => $this->user
->getId(),
926 'reason' => 'no reason given',
927 'timestamp' => $prev +
3600,
931 $this->user
->mBlock
->mTimestamp
= 0;
932 $this->assertEquals( [ [ 'autoblockedtext',
933 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
934 'Useruser', null, 'infinite', '127.0.8.1',
935 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
936 $this->title
->getUserPermissionsErrors( 'move-target',
939 $this->assertEquals( false, $this->title
->userCan( 'move-target', $this->user
) );
940 // quickUserCan should ignore user blocks
941 $this->assertEquals( true, $this->title
->quickUserCan( 'move-target', $this->user
) );
943 global $wgLocalTZoffset;
944 $wgLocalTZoffset = -60;
945 $this->user
->mBlockedby
= $this->user
->getName();
946 $this->user
->mBlock
= new Block( [
947 'address' => '127.0.8.1',
948 'by' => $this->user
->getId(),
949 'reason' => 'no reason given',
954 $this->assertEquals( [ [ 'blockedtext',
955 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
956 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
957 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
958 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
959 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
960 # $user->blockedFor() == ''
961 # $user->mBlock->mExpiry == 'infinity'
963 $this->user
->mBlockedby
= $this->user
->getName();
964 $this->user
->mBlock
= new Block( [
965 'address' => '127.0.8.1',
966 'by' => $this->user
->getId(),
967 'reason' => 'no reason given',
971 'systemBlock' => 'test',
973 $this->assertEquals( [ [ 'systemblockedtext',
974 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
975 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
976 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
977 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );