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
;
74 protected function setUserPerm( $perm ) {
75 // Setting member variables is evil!!!
77 if ( is_array( $perm ) ) {
78 $this->user
->mRights
= $perm;
80 $this->user
->mRights
= [ $perm ];
84 protected function setTitle( $ns, $title = "Main_Page" ) {
85 $this->title
= Title
::makeTitle( $ns, $title );
88 protected function setUser( $userName = null ) {
89 if ( $userName === 'anon' ) {
90 $this->user
= $this->anonUser
;
91 } elseif ( $userName === null ||
$userName === $this->userName
) {
92 $this->user
= $this->userUser
;
94 $this->user
= $this->altUser
;
99 * @todo This test method should be split up into separate test methods and
101 * @covers Title::checkQuickPermissions
103 public function testQuickPermissions() {
104 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
105 getFormattedNsText( NS_PROJECT
);
107 $this->setUser( 'anon' );
108 $this->setTitle( NS_TALK
);
109 $this->setUserPerm( "createtalk" );
110 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
111 $this->assertEquals( [], $res );
113 $this->setTitle( NS_TALK
);
114 $this->setUserPerm( "createpage" );
115 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
116 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
118 $this->setTitle( NS_TALK
);
119 $this->setUserPerm( "" );
120 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
121 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
123 $this->setTitle( NS_MAIN
);
124 $this->setUserPerm( "createpage" );
125 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
126 $this->assertEquals( [], $res );
128 $this->setTitle( NS_MAIN
);
129 $this->setUserPerm( "createtalk" );
130 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
131 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
133 $this->setUser( $this->userName
);
134 $this->setTitle( NS_TALK
);
135 $this->setUserPerm( "createtalk" );
136 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
137 $this->assertEquals( [], $res );
139 $this->setTitle( NS_TALK
);
140 $this->setUserPerm( "createpage" );
141 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
142 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
144 $this->setTitle( NS_TALK
);
145 $this->setUserPerm( "" );
146 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
147 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
149 $this->setTitle( NS_MAIN
);
150 $this->setUserPerm( "createpage" );
151 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
152 $this->assertEquals( [], $res );
154 $this->setTitle( NS_MAIN
);
155 $this->setUserPerm( "createtalk" );
156 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
157 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
159 $this->setTitle( NS_MAIN
);
160 $this->setUserPerm( "" );
161 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
162 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
164 $this->setUser( 'anon' );
165 $this->setTitle( NS_USER
, $this->userName
. '' );
166 $this->setUserPerm( "" );
167 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
168 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
170 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
171 $this->setUserPerm( "" );
172 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
173 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
175 $this->setTitle( NS_USER
, $this->userName
. '' );
176 $this->setUserPerm( "move-rootuserpages" );
177 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
178 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
180 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
181 $this->setUserPerm( "move-rootuserpages" );
182 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
183 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
185 $this->setTitle( NS_USER
, $this->userName
. '' );
186 $this->setUserPerm( "" );
187 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
188 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
190 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
191 $this->setUserPerm( "" );
192 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
193 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
195 $this->setTitle( NS_USER
, $this->userName
. '' );
196 $this->setUserPerm( "move-rootuserpages" );
197 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
198 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
200 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
201 $this->setUserPerm( "move-rootuserpages" );
202 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
203 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
205 $this->setUser( $this->userName
);
206 $this->setTitle( NS_FILE
, "img.png" );
207 $this->setUserPerm( "" );
208 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
209 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
211 $this->setTitle( NS_FILE
, "img.png" );
212 $this->setUserPerm( "movefile" );
213 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
214 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
216 $this->setUser( 'anon' );
217 $this->setTitle( NS_FILE
, "img.png" );
218 $this->setUserPerm( "" );
219 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
220 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
222 $this->setTitle( NS_FILE
, "img.png" );
223 $this->setUserPerm( "movefile" );
224 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
225 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
227 $this->setUser( $this->userName
);
228 $this->setUserPerm( "move" );
229 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
231 $this->setUserPerm( "" );
232 $this->runGroupPermissions(
234 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
237 $this->setUser( 'anon' );
238 $this->setUserPerm( "move" );
239 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
241 $this->setUserPerm( "" );
242 $this->runGroupPermissions(
244 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
245 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
248 if ( $this->isWikitextNS( NS_MAIN
) ) {
249 // NOTE: some content models don't allow moving
250 // @todo find a Wikitext namespace for testing
252 $this->setTitle( NS_MAIN
);
253 $this->setUser( 'anon' );
254 $this->setUserPerm( "move" );
255 $this->runGroupPermissions( 'move', [] );
257 $this->setUserPerm( "" );
258 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
259 [ [ 'movenologintext' ] ] );
261 $this->setUser( $this->userName
);
262 $this->setUserPerm( "" );
263 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
265 $this->setUserPerm( "move" );
266 $this->runGroupPermissions( 'move', [] );
268 $this->setUser( 'anon' );
269 $this->setUserPerm( 'move' );
270 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
271 $this->assertEquals( [], $res );
273 $this->setUserPerm( '' );
274 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
275 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
278 $this->setTitle( NS_USER
);
279 $this->setUser( $this->userName
);
280 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
281 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
282 $this->assertEquals( [], $res );
284 $this->setUserPerm( "move" );
285 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
286 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
288 $this->setUser( 'anon' );
289 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
290 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
291 $this->assertEquals( [], $res );
293 $this->setTitle( NS_USER
, "User/subpage" );
294 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
295 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
296 $this->assertEquals( [], $res );
298 $this->setUserPerm( "move" );
299 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
300 $this->assertEquals( [], $res );
302 $this->setUser( 'anon' );
305 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
306 [ [ 'badaccess-group0' ] ],
313 "[[$prefix:Administrators|Administrators]]", 1 ],
316 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
317 [ [ 'protect-cantedit' ] ],
320 '' => [ [], [], [], true ]
323 foreach ( [ "edit", "protect", "" ] as $action ) {
324 $this->setUserPerm( null );
325 $this->assertEquals( $check[$action][0],
326 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
327 $this->assertEquals( $check[$action][0],
328 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
329 $this->assertEquals( $check[$action][0],
330 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
332 global $wgGroupPermissions;
333 $old = $wgGroupPermissions;
334 $wgGroupPermissions = [];
336 $this->assertEquals( $check[$action][1],
337 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
338 $this->assertEquals( $check[$action][1],
339 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
340 $this->assertEquals( $check[$action][1],
341 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
342 $wgGroupPermissions = $old;
344 $this->setUserPerm( $action );
345 $this->assertEquals( $check[$action][2],
346 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
347 $this->assertEquals( $check[$action][2],
348 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
349 $this->assertEquals( $check[$action][2],
350 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
352 $this->setUserPerm( $action );
353 $this->assertEquals( $check[$action][3],
354 $this->title
->userCan( $action, $this->user
, true ) );
355 $this->assertEquals( $check[$action][3],
356 $this->title
->quickUserCan( $action, $this->user
) );
357 # count( User::getGroupsWithPermissions( $action ) ) < 1
361 protected function runGroupPermissions( $action, $result, $result2 = null ) {
362 global $wgGroupPermissions;
364 if ( $result2 === null ) {
368 $wgGroupPermissions['autoconfirmed']['move'] = false;
369 $wgGroupPermissions['user']['move'] = false;
370 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
371 $this->assertEquals( $result, $res );
373 $wgGroupPermissions['autoconfirmed']['move'] = true;
374 $wgGroupPermissions['user']['move'] = false;
375 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
376 $this->assertEquals( $result2, $res );
378 $wgGroupPermissions['autoconfirmed']['move'] = true;
379 $wgGroupPermissions['user']['move'] = true;
380 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
381 $this->assertEquals( $result2, $res );
383 $wgGroupPermissions['autoconfirmed']['move'] = false;
384 $wgGroupPermissions['user']['move'] = true;
385 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
386 $this->assertEquals( $result2, $res );
390 * @todo This test method should be split up into separate test methods and
392 * @covers Title::checkSpecialsAndNSPermissions
394 public function testSpecialsAndNSPermissions() {
395 global $wgNamespaceProtection;
396 $this->setUser( $this->userName
);
398 $this->setTitle( NS_SPECIAL
);
400 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
401 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
403 $this->setTitle( NS_MAIN
);
404 $this->setUserPerm( 'bogus' );
405 $this->assertEquals( [],
406 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
408 $this->setTitle( NS_MAIN
);
409 $this->setUserPerm( '' );
410 $this->assertEquals( [ [ 'badaccess-group0' ] ],
411 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
413 $wgNamespaceProtection[NS_USER
] = [ 'bogus' ];
415 $this->setTitle( NS_USER
);
416 $this->setUserPerm( '' );
417 $this->assertEquals( [ [ 'badaccess-group0' ],
418 [ 'namespaceprotected', 'User', 'bogus' ] ],
419 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
421 $this->setTitle( NS_MEDIAWIKI
);
422 $this->setUserPerm( 'bogus' );
423 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
424 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
426 $this->setTitle( NS_MEDIAWIKI
);
427 $this->setUserPerm( 'bogus' );
428 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
429 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
431 $wgNamespaceProtection = null;
433 $this->setUserPerm( 'bogus' );
434 $this->assertEquals( [],
435 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
436 $this->assertEquals( true,
437 $this->title
->userCan( 'bogus', $this->user
) );
439 $this->setUserPerm( '' );
440 $this->assertEquals( [ [ 'badaccess-group0' ] ],
441 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
442 $this->assertEquals( false,
443 $this->title
->userCan( 'bogus', $this->user
) );
447 * @todo This test method should be split up into separate test methods and
449 * @covers Title::checkUserConfigPermissions
451 public function testJsConfigEditPermissions() {
452 $this->setUser( $this->userName
);
454 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
455 $this->runConfigEditPermissions(
456 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
458 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
459 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
460 [ [ 'badaccess-group0' ] ],
462 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
463 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
464 [ [ 'badaccess-group0' ] ]
469 * @todo This test method should be split up into separate test methods and
471 * @covers Title::checkUserConfigPermissions
473 public function testJsonConfigEditPermissions() {
474 $this->setUser( $this->userName
);
476 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
477 $this->runConfigEditPermissions(
478 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
480 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
481 [ [ 'badaccess-group0' ] ],
482 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
484 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
485 [ [ 'badaccess-group0' ] ],
486 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ]
491 * @todo This test method should be split up into separate test methods and
493 * @covers Title::checkUserConfigPermissions
495 public function testCssConfigEditPermissions() {
496 $this->setUser( $this->userName
);
498 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
499 $this->runConfigEditPermissions(
500 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
502 [ [ 'badaccess-group0' ] ],
503 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
504 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
506 [ [ 'badaccess-group0' ] ],
507 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
508 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ]
513 * @todo This test method should be split up into separate test methods and
515 * @covers Title::checkUserConfigPermissions
517 public function testOtherJsConfigEditPermissions() {
518 $this->setUser( $this->userName
);
520 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
521 $this->runConfigEditPermissions(
522 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
524 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
525 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
526 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
528 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
529 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
530 [ [ 'badaccess-group0' ] ]
535 * @todo This test method should be split up into separate test methods and
537 * @covers Title::checkUserConfigPermissions
539 public function testOtherJsonConfigEditPermissions() {
540 $this->setUser( $this->userName
);
542 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
543 $this->runConfigEditPermissions(
544 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
546 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
547 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
548 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
550 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
551 [ [ 'badaccess-group0' ] ],
552 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ]
557 * @todo This test method should be split up into separate test methods and
559 * @covers Title::checkUserConfigPermissions
561 public function testOtherCssConfigEditPermissions() {
562 $this->setUser( $this->userName
);
564 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
565 $this->runConfigEditPermissions(
566 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
568 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
569 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
570 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
572 [ [ 'badaccess-group0' ] ],
573 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
574 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ]
579 * @todo This test method should be split up into separate test methods and
581 * @covers Title::checkUserConfigPermissions
583 public function testOtherNonConfigEditPermissions() {
584 $this->setUser( $this->userName
);
586 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
587 $this->runConfigEditPermissions(
588 [ [ 'badaccess-group0' ] ],
590 [ [ 'badaccess-group0' ] ],
591 [ [ 'badaccess-group0' ] ],
592 [ [ 'badaccess-group0' ] ],
594 [ [ 'badaccess-group0' ] ],
595 [ [ 'badaccess-group0' ] ],
596 [ [ 'badaccess-group0' ] ]
600 protected function runConfigEditPermissions(
609 $this->setUserPerm( '' );
610 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
611 $this->assertEquals( $resultNone, $result );
613 $this->setUserPerm( 'editmyusercss' );
614 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
615 $this->assertEquals( $resultMyCss, $result );
617 $this->setUserPerm( 'editmyuserjson' );
618 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
619 $this->assertEquals( $resultMyJson, $result );
621 $this->setUserPerm( 'editmyuserjs' );
622 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
623 $this->assertEquals( $resultMyJs, $result );
625 $this->setUserPerm( 'editusercss' );
626 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
627 $this->assertEquals( $resultUserCss, $result );
629 $this->setUserPerm( 'edituserjson' );
630 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
631 $this->assertEquals( $resultUserJson, $result );
633 $this->setUserPerm( 'edituserjs' );
634 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
635 $this->assertEquals( $resultUserJs, $result );
637 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
638 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
639 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
643 * @todo This test method should be split up into separate test methods and
645 * @covers Title::checkPageRestrictions
647 public function testPageRestrictions() {
648 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
649 getFormattedNsText( NS_PROJECT
);
651 $this->setTitle( NS_MAIN
);
652 $this->title
->mRestrictionsLoaded
= true;
653 $this->setUserPerm( "edit" );
654 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
656 $this->assertEquals( [],
657 $this->title
->getUserPermissionsErrors( 'edit',
660 $this->assertEquals( true,
661 $this->title
->quickUserCan( 'edit', $this->user
) );
662 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
663 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
665 $this->assertEquals( [ [ 'badaccess-group0' ],
666 [ 'protectedpagetext', 'bogus', 'bogus' ],
667 [ 'protectedpagetext', 'editprotected', 'bogus' ],
668 [ 'protectedpagetext', 'protect', 'bogus' ] ],
669 $this->title
->getUserPermissionsErrors( 'bogus',
671 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
672 [ 'protectedpagetext', 'editprotected', 'edit' ],
673 [ 'protectedpagetext', 'protect', 'edit' ] ],
674 $this->title
->getUserPermissionsErrors( 'edit',
676 $this->setUserPerm( "" );
677 $this->assertEquals( [ [ 'badaccess-group0' ],
678 [ 'protectedpagetext', 'bogus', 'bogus' ],
679 [ 'protectedpagetext', 'editprotected', 'bogus' ],
680 [ 'protectedpagetext', 'protect', 'bogus' ] ],
681 $this->title
->getUserPermissionsErrors( 'bogus',
683 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
684 [ 'protectedpagetext', 'bogus', 'edit' ],
685 [ 'protectedpagetext', 'editprotected', 'edit' ],
686 [ 'protectedpagetext', 'protect', 'edit' ] ],
687 $this->title
->getUserPermissionsErrors( 'edit',
689 $this->setUserPerm( [ "edit", "editprotected" ] );
690 $this->assertEquals( [ [ 'badaccess-group0' ],
691 [ 'protectedpagetext', 'bogus', 'bogus' ],
692 [ 'protectedpagetext', 'protect', 'bogus' ] ],
693 $this->title
->getUserPermissionsErrors( 'bogus',
695 $this->assertEquals( [
696 [ 'protectedpagetext', 'bogus', 'edit' ],
697 [ 'protectedpagetext', 'protect', 'edit' ] ],
698 $this->title
->getUserPermissionsErrors( 'edit',
701 $this->title
->mCascadeRestriction
= true;
702 $this->setUserPerm( "edit" );
703 $this->assertEquals( false,
704 $this->title
->quickUserCan( 'bogus', $this->user
) );
705 $this->assertEquals( false,
706 $this->title
->quickUserCan( 'edit', $this->user
) );
707 $this->assertEquals( [ [ 'badaccess-group0' ],
708 [ 'protectedpagetext', 'bogus', 'bogus' ],
709 [ 'protectedpagetext', 'editprotected', 'bogus' ],
710 [ 'protectedpagetext', 'protect', 'bogus' ] ],
711 $this->title
->getUserPermissionsErrors( 'bogus',
713 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
714 [ 'protectedpagetext', 'editprotected', 'edit' ],
715 [ 'protectedpagetext', 'protect', 'edit' ] ],
716 $this->title
->getUserPermissionsErrors( 'edit',
719 $this->setUserPerm( [ "edit", "editprotected" ] );
720 $this->assertEquals( false,
721 $this->title
->quickUserCan( 'bogus', $this->user
) );
722 $this->assertEquals( false,
723 $this->title
->quickUserCan( 'edit', $this->user
) );
724 $this->assertEquals( [ [ 'badaccess-group0' ],
725 [ 'protectedpagetext', 'bogus', 'bogus' ],
726 [ 'protectedpagetext', 'protect', 'bogus' ],
727 [ 'protectedpagetext', 'protect', 'bogus' ] ],
728 $this->title
->getUserPermissionsErrors( 'bogus',
730 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
731 [ 'protectedpagetext', 'protect', 'edit' ],
732 [ 'protectedpagetext', 'protect', 'edit' ] ],
733 $this->title
->getUserPermissionsErrors( 'edit',
738 * @covers Title::checkCascadingSourcesRestrictions
740 public function testCascadingSourcesRestrictions() {
741 $this->setTitle( NS_MAIN
, "test page" );
742 $this->setUserPerm( [ "edit", "bogus" ] );
744 $this->title
->mCascadeSources
= [
745 Title
::makeTitle( NS_MAIN
, "Bogus" ),
746 Title
::makeTitle( NS_MAIN
, "UnBogus" )
748 $this->title
->mCascadingRestrictions
= [
749 "bogus" => [ 'bogus', "sysop", "protect", "" ]
752 $this->assertEquals( false,
753 $this->title
->userCan( 'bogus', $this->user
) );
754 $this->assertEquals( [
755 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
756 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
757 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
758 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
760 $this->assertEquals( true,
761 $this->title
->userCan( 'edit', $this->user
) );
762 $this->assertEquals( [],
763 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
767 * @todo This test method should be split up into separate test methods and
769 * @covers Title::checkActionPermissions
771 public function testActionPermissions() {
772 $this->setUserPerm( [ "createpage" ] );
773 $this->setTitle( NS_MAIN
, "test page" );
774 $this->title
->mTitleProtection
['permission'] = '';
775 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
776 $this->title
->mTitleProtection
['expiry'] = 'infinity';
777 $this->title
->mTitleProtection
['reason'] = 'test';
778 $this->title
->mCascadeRestriction
= false;
780 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
781 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
782 $this->assertEquals( false,
783 $this->title
->userCan( 'create', $this->user
) );
785 $this->title
->mTitleProtection
['permission'] = 'editprotected';
786 $this->setUserPerm( [ 'createpage', 'protect' ] );
787 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
788 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
789 $this->assertEquals( false,
790 $this->title
->userCan( 'create', $this->user
) );
792 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
793 $this->assertEquals( [],
794 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
795 $this->assertEquals( true,
796 $this->title
->userCan( 'create', $this->user
) );
798 $this->setUserPerm( [ 'createpage' ] );
799 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
800 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
801 $this->assertEquals( false,
802 $this->title
->userCan( 'create', $this->user
) );
804 $this->setTitle( NS_MEDIA
, "test page" );
805 $this->setUserPerm( [ "move" ] );
806 $this->assertEquals( false,
807 $this->title
->userCan( 'move', $this->user
) );
808 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
809 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
811 $this->setTitle( NS_HELP
, "test page" );
812 $this->assertEquals( [],
813 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
814 $this->assertEquals( true,
815 $this->title
->userCan( 'move', $this->user
) );
817 $this->title
->mInterwiki
= "no";
818 $this->assertEquals( [ [ 'immobile-source-page' ] ],
819 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
820 $this->assertEquals( false,
821 $this->title
->userCan( 'move', $this->user
) );
823 $this->setTitle( NS_MEDIA
, "test page" );
824 $this->assertEquals( false,
825 $this->title
->userCan( 'move-target', $this->user
) );
826 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
827 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
829 $this->setTitle( NS_HELP
, "test page" );
830 $this->assertEquals( [],
831 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
832 $this->assertEquals( true,
833 $this->title
->userCan( 'move-target', $this->user
) );
835 $this->title
->mInterwiki
= "no";
836 $this->assertEquals( [ [ 'immobile-target-page' ] ],
837 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
838 $this->assertEquals( false,
839 $this->title
->userCan( 'move-target', $this->user
) );
843 * @covers Title::checkUserBlock
845 public function testUserBlock() {
846 $this->setMwGlobals( [
847 'wgEmailConfirmToEdit' => true,
848 'wgEmailAuthentication' => true,
851 $this->setUserPerm( [ "createpage", "move" ] );
852 $this->setTitle( NS_HELP
, "test page" );
854 # $wgEmailConfirmToEdit only applies to 'edit' action
855 $this->assertEquals( [],
856 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
857 $this->assertContains( [ 'confirmedittext' ],
858 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
860 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
861 $this->assertNotContains( [ 'confirmedittext' ],
862 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
864 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
865 $this->assertEquals( [],
866 $this->title
->getUserPermissionsErrors( 'move-target',
872 $this->user
->mBlockedby
= $this->user
->getId();
873 $this->user
->mBlock
= new Block( [
874 'address' => '127.0.8.1',
875 'by' => $this->user
->getId(),
876 'reason' => 'no reason given',
877 'timestamp' => $prev +
3600,
881 $this->user
->mBlock
->mTimestamp
= 0;
882 $this->assertEquals( [ [ 'autoblockedtext',
883 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
884 'Useruser', null, 'infinite', '127.0.8.1',
885 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
886 $this->title
->getUserPermissionsErrors( 'move-target',
889 $this->assertEquals( false, $this->title
->userCan( 'move-target', $this->user
) );
890 // quickUserCan should ignore user blocks
891 $this->assertEquals( true, $this->title
->quickUserCan( 'move-target', $this->user
) );
893 global $wgLocalTZoffset;
894 $wgLocalTZoffset = -60;
895 $this->user
->mBlockedby
= $this->user
->getName();
896 $this->user
->mBlock
= new Block( [
897 'address' => '127.0.8.1',
898 'by' => $this->user
->getId(),
899 'reason' => 'no reason given',
904 $this->assertEquals( [ [ 'blockedtext',
905 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
906 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
907 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
908 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
909 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
910 # $user->blockedFor() == ''
911 # $user->mBlock->mExpiry == 'infinity'
913 $this->user
->mBlockedby
= $this->user
->getName();
914 $this->user
->mBlock
= new Block( [
915 'address' => '127.0.8.1',
916 'by' => $this->user
->getId(),
917 'reason' => 'no reason given',
921 'systemBlock' => 'test',
923 $this->assertEquals( [ [ 'systemblockedtext',
924 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
925 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
926 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
927 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );