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
103 * This test is failing per T201776.
106 * @covers Title::checkQuickPermissions
108 public function testQuickPermissions() {
109 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
110 getFormattedNsText( NS_PROJECT
);
112 $this->setUser( 'anon' );
113 $this->setTitle( NS_TALK
);
114 $this->setUserPerm( "createtalk" );
115 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
116 $this->assertEquals( [], $res );
118 $this->setTitle( NS_TALK
);
119 $this->setUserPerm( "createpage" );
120 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
121 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
123 $this->setTitle( NS_TALK
);
124 $this->setUserPerm( "" );
125 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
126 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
128 $this->setTitle( NS_MAIN
);
129 $this->setUserPerm( "createpage" );
130 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
131 $this->assertEquals( [], $res );
133 $this->setTitle( NS_MAIN
);
134 $this->setUserPerm( "createtalk" );
135 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
136 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
138 $this->setUser( $this->userName
);
139 $this->setTitle( NS_TALK
);
140 $this->setUserPerm( "createtalk" );
141 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
142 $this->assertEquals( [], $res );
144 $this->setTitle( NS_TALK
);
145 $this->setUserPerm( "createpage" );
146 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
147 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
149 $this->setTitle( NS_TALK
);
150 $this->setUserPerm( "" );
151 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
152 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
154 $this->setTitle( NS_MAIN
);
155 $this->setUserPerm( "createpage" );
156 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
157 $this->assertEquals( [], $res );
159 $this->setTitle( NS_MAIN
);
160 $this->setUserPerm( "createtalk" );
161 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
162 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
164 $this->setTitle( NS_MAIN
);
165 $this->setUserPerm( "" );
166 $res = $this->title
->getUserPermissionsErrors( 'create', $this->user
);
167 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
169 $this->setUser( 'anon' );
170 $this->setTitle( NS_USER
, $this->userName
. '' );
171 $this->setUserPerm( "" );
172 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
173 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
175 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
176 $this->setUserPerm( "" );
177 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
178 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
180 $this->setTitle( NS_USER
, $this->userName
. '' );
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
. '/subpage' );
186 $this->setUserPerm( "move-rootuserpages" );
187 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
188 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
190 $this->setTitle( NS_USER
, $this->userName
. '' );
191 $this->setUserPerm( "" );
192 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
193 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
195 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
196 $this->setUserPerm( "" );
197 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
198 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
200 $this->setTitle( NS_USER
, $this->userName
. '' );
201 $this->setUserPerm( "move-rootuserpages" );
202 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
203 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
205 $this->setTitle( NS_USER
, $this->userName
. '/subpage' );
206 $this->setUserPerm( "move-rootuserpages" );
207 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
208 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
210 $this->setUser( $this->userName
);
211 $this->setTitle( NS_FILE
, "img.png" );
212 $this->setUserPerm( "" );
213 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
214 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
216 $this->setTitle( NS_FILE
, "img.png" );
217 $this->setUserPerm( "movefile" );
218 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
219 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
221 $this->setUser( 'anon' );
222 $this->setTitle( NS_FILE
, "img.png" );
223 $this->setUserPerm( "" );
224 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
225 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
227 $this->setTitle( NS_FILE
, "img.png" );
228 $this->setUserPerm( "movefile" );
229 $res = $this->title
->getUserPermissionsErrors( 'move', $this->user
);
230 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
232 $this->setUser( $this->userName
);
233 $this->setUserPerm( "move" );
234 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
236 $this->setUserPerm( "" );
237 $this->runGroupPermissions(
239 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
242 $this->setUser( 'anon' );
243 $this->setUserPerm( "move" );
244 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
246 $this->setUserPerm( "" );
247 $this->runGroupPermissions(
249 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
250 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
253 if ( $this->isWikitextNS( NS_MAIN
) ) {
254 // NOTE: some content models don't allow moving
255 // @todo find a Wikitext namespace for testing
257 $this->setTitle( NS_MAIN
);
258 $this->setUser( 'anon' );
259 $this->setUserPerm( "move" );
260 $this->runGroupPermissions( 'move', [] );
262 $this->setUserPerm( "" );
263 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
264 [ [ 'movenologintext' ] ] );
266 $this->setUser( $this->userName
);
267 $this->setUserPerm( "" );
268 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
270 $this->setUserPerm( "move" );
271 $this->runGroupPermissions( 'move', [] );
273 $this->setUser( 'anon' );
274 $this->setUserPerm( 'move' );
275 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
276 $this->assertEquals( [], $res );
278 $this->setUserPerm( '' );
279 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
280 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
283 $this->setTitle( NS_USER
);
284 $this->setUser( $this->userName
);
285 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
286 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
287 $this->assertEquals( [], $res );
289 $this->setUserPerm( "move" );
290 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
291 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
293 $this->setUser( 'anon' );
294 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
295 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
296 $this->assertEquals( [], $res );
298 $this->setTitle( NS_USER
, "User/subpage" );
299 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
300 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
301 $this->assertEquals( [], $res );
303 $this->setUserPerm( "move" );
304 $res = $this->title
->getUserPermissionsErrors( 'move-target', $this->user
);
305 $this->assertEquals( [], $res );
307 $this->setUser( 'anon' );
310 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
311 [ [ 'badaccess-group0' ] ],
318 "[[$prefix:Administrators|Administrators]]", 1 ],
321 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
322 [ [ 'protect-cantedit' ] ],
325 '' => [ [], [], [], true ]
328 foreach ( [ "edit", "protect", "" ] as $action ) {
329 $this->setUserPerm( null );
330 $this->assertEquals( $check[$action][0],
331 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
332 $this->assertEquals( $check[$action][0],
333 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
334 $this->assertEquals( $check[$action][0],
335 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
337 global $wgGroupPermissions;
338 $old = $wgGroupPermissions;
339 $wgGroupPermissions = [];
341 $this->assertEquals( $check[$action][1],
342 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
343 $this->assertEquals( $check[$action][1],
344 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
345 $this->assertEquals( $check[$action][1],
346 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
347 $wgGroupPermissions = $old;
349 $this->setUserPerm( $action );
350 $this->assertEquals( $check[$action][2],
351 $this->title
->getUserPermissionsErrors( $action, $this->user
, true ) );
352 $this->assertEquals( $check[$action][2],
353 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'full' ) );
354 $this->assertEquals( $check[$action][2],
355 $this->title
->getUserPermissionsErrors( $action, $this->user
, 'secure' ) );
357 $this->setUserPerm( $action );
358 $this->assertEquals( $check[$action][3],
359 $this->title
->userCan( $action, $this->user
, true ) );
360 $this->assertEquals( $check[$action][3],
361 $this->title
->quickUserCan( $action, $this->user
) );
362 # count( User::getGroupsWithPermissions( $action ) ) < 1
366 protected function runGroupPermissions( $action, $result, $result2 = null ) {
367 global $wgGroupPermissions;
369 if ( $result2 === null ) {
373 $wgGroupPermissions['autoconfirmed']['move'] = false;
374 $wgGroupPermissions['user']['move'] = false;
375 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
376 $this->assertEquals( $result, $res );
378 $wgGroupPermissions['autoconfirmed']['move'] = true;
379 $wgGroupPermissions['user']['move'] = false;
380 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
381 $this->assertEquals( $result2, $res );
383 $wgGroupPermissions['autoconfirmed']['move'] = true;
384 $wgGroupPermissions['user']['move'] = true;
385 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
386 $this->assertEquals( $result2, $res );
388 $wgGroupPermissions['autoconfirmed']['move'] = false;
389 $wgGroupPermissions['user']['move'] = true;
390 $res = $this->title
->getUserPermissionsErrors( $action, $this->user
);
391 $this->assertEquals( $result2, $res );
395 * @todo This test method should be split up into separate test methods and
397 * @covers Title::checkSpecialsAndNSPermissions
399 public function testSpecialsAndNSPermissions() {
400 global $wgNamespaceProtection;
401 $this->setUser( $this->userName
);
403 $this->setTitle( NS_SPECIAL
);
405 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
406 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
408 $this->setTitle( NS_MAIN
);
409 $this->setUserPerm( 'bogus' );
410 $this->assertEquals( [],
411 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
413 $this->setTitle( NS_MAIN
);
414 $this->setUserPerm( '' );
415 $this->assertEquals( [ [ 'badaccess-group0' ] ],
416 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
418 $wgNamespaceProtection[NS_USER
] = [ 'bogus' ];
420 $this->setTitle( NS_USER
);
421 $this->setUserPerm( '' );
422 $this->assertEquals( [ [ 'badaccess-group0' ],
423 [ 'namespaceprotected', 'User', '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 $this->setTitle( NS_MEDIAWIKI
);
432 $this->setUserPerm( 'bogus' );
433 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
434 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
436 $wgNamespaceProtection = null;
438 $this->setUserPerm( 'bogus' );
439 $this->assertEquals( [],
440 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
441 $this->assertEquals( true,
442 $this->title
->userCan( 'bogus', $this->user
) );
444 $this->setUserPerm( '' );
445 $this->assertEquals( [ [ 'badaccess-group0' ] ],
446 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
447 $this->assertEquals( false,
448 $this->title
->userCan( 'bogus', $this->user
) );
452 * @todo This test method should be split up into separate test methods and
454 * @covers Title::checkUserConfigPermissions
456 public function testJsConfigEditPermissions() {
457 $this->setUser( $this->userName
);
459 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
460 $this->runConfigEditPermissions(
461 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
463 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
464 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
465 [ [ 'badaccess-group0' ] ],
467 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
468 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
469 [ [ 'badaccess-group0' ] ]
474 * @todo This test method should be split up into separate test methods and
476 * @covers Title::checkUserConfigPermissions
478 public function testJsonConfigEditPermissions() {
479 $this->setUser( $this->userName
);
481 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
482 $this->runConfigEditPermissions(
483 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
485 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
486 [ [ 'badaccess-group0' ] ],
487 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
489 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
490 [ [ 'badaccess-group0' ] ],
491 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ]
496 * @todo This test method should be split up into separate test methods and
498 * @covers Title::checkUserConfigPermissions
500 public function testCssConfigEditPermissions() {
501 $this->setUser( $this->userName
);
503 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
504 $this->runConfigEditPermissions(
505 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
507 [ [ 'badaccess-group0' ] ],
508 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
509 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
511 [ [ 'badaccess-group0' ] ],
512 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
513 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ]
518 * @todo This test method should be split up into separate test methods and
520 * @covers Title::checkUserConfigPermissions
522 public function testOtherJsConfigEditPermissions() {
523 $this->setUser( $this->userName
);
525 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
526 $this->runConfigEditPermissions(
527 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
529 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
530 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
531 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
533 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
534 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
535 [ [ 'badaccess-group0' ] ]
540 * @todo This test method should be split up into separate test methods and
542 * @covers Title::checkUserConfigPermissions
544 public function testOtherJsonConfigEditPermissions() {
545 $this->setUser( $this->userName
);
547 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
548 $this->runConfigEditPermissions(
549 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
551 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
552 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
553 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
555 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
556 [ [ 'badaccess-group0' ] ],
557 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ]
562 * @todo This test method should be split up into separate test methods and
564 * @covers Title::checkUserConfigPermissions
566 public function testOtherCssConfigEditPermissions() {
567 $this->setUser( $this->userName
);
569 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
570 $this->runConfigEditPermissions(
571 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
573 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
574 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
575 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
577 [ [ 'badaccess-group0' ] ],
578 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
579 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ]
584 * @todo This test method should be split up into separate test methods and
586 * @covers Title::checkUserConfigPermissions
588 public function testOtherNonConfigEditPermissions() {
589 $this->setUser( $this->userName
);
591 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
592 $this->runConfigEditPermissions(
593 [ [ 'badaccess-group0' ] ],
595 [ [ 'badaccess-group0' ] ],
596 [ [ 'badaccess-group0' ] ],
597 [ [ 'badaccess-group0' ] ],
599 [ [ 'badaccess-group0' ] ],
600 [ [ 'badaccess-group0' ] ],
601 [ [ 'badaccess-group0' ] ]
605 protected function runConfigEditPermissions(
614 $this->setUserPerm( '' );
615 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
616 $this->assertEquals( $resultNone, $result );
618 $this->setUserPerm( 'editmyusercss' );
619 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
620 $this->assertEquals( $resultMyCss, $result );
622 $this->setUserPerm( 'editmyuserjson' );
623 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
624 $this->assertEquals( $resultMyJson, $result );
626 $this->setUserPerm( 'editmyuserjs' );
627 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
628 $this->assertEquals( $resultMyJs, $result );
630 $this->setUserPerm( 'editusercss' );
631 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
632 $this->assertEquals( $resultUserCss, $result );
634 $this->setUserPerm( 'edituserjson' );
635 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
636 $this->assertEquals( $resultUserJson, $result );
638 $this->setUserPerm( 'edituserjs' );
639 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
640 $this->assertEquals( $resultUserJs, $result );
642 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
643 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
644 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
648 * @todo This test method should be split up into separate test methods and
651 * This test is failing per T201776.
654 * @covers Title::checkPageRestrictions
656 public function testPageRestrictions() {
657 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
658 getFormattedNsText( NS_PROJECT
);
660 $this->setTitle( NS_MAIN
);
661 $this->title
->mRestrictionsLoaded
= true;
662 $this->setUserPerm( "edit" );
663 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
665 $this->assertEquals( [],
666 $this->title
->getUserPermissionsErrors( 'edit',
669 $this->assertEquals( true,
670 $this->title
->quickUserCan( 'edit', $this->user
) );
671 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
672 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
674 $this->assertEquals( [ [ 'badaccess-group0' ],
675 [ 'protectedpagetext', 'bogus', 'bogus' ],
676 [ 'protectedpagetext', 'editprotected', 'bogus' ],
677 [ 'protectedpagetext', 'protect', 'bogus' ] ],
678 $this->title
->getUserPermissionsErrors( 'bogus',
680 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
681 [ 'protectedpagetext', 'editprotected', 'edit' ],
682 [ 'protectedpagetext', 'protect', 'edit' ] ],
683 $this->title
->getUserPermissionsErrors( 'edit',
685 $this->setUserPerm( "" );
686 $this->assertEquals( [ [ 'badaccess-group0' ],
687 [ 'protectedpagetext', 'bogus', 'bogus' ],
688 [ 'protectedpagetext', 'editprotected', 'bogus' ],
689 [ 'protectedpagetext', 'protect', 'bogus' ] ],
690 $this->title
->getUserPermissionsErrors( 'bogus',
692 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
693 [ 'protectedpagetext', 'bogus', 'edit' ],
694 [ 'protectedpagetext', 'editprotected', 'edit' ],
695 [ 'protectedpagetext', 'protect', 'edit' ] ],
696 $this->title
->getUserPermissionsErrors( 'edit',
698 $this->setUserPerm( [ "edit", "editprotected" ] );
699 $this->assertEquals( [ [ 'badaccess-group0' ],
700 [ 'protectedpagetext', 'bogus', 'bogus' ],
701 [ 'protectedpagetext', 'protect', 'bogus' ] ],
702 $this->title
->getUserPermissionsErrors( 'bogus',
704 $this->assertEquals( [
705 [ 'protectedpagetext', 'bogus', 'edit' ],
706 [ 'protectedpagetext', 'protect', 'edit' ] ],
707 $this->title
->getUserPermissionsErrors( 'edit',
710 $this->title
->mCascadeRestriction
= true;
711 $this->setUserPerm( "edit" );
712 $this->assertEquals( false,
713 $this->title
->quickUserCan( 'bogus', $this->user
) );
714 $this->assertEquals( false,
715 $this->title
->quickUserCan( 'edit', $this->user
) );
716 $this->assertEquals( [ [ 'badaccess-group0' ],
717 [ 'protectedpagetext', 'bogus', 'bogus' ],
718 [ 'protectedpagetext', 'editprotected', 'bogus' ],
719 [ 'protectedpagetext', 'protect', 'bogus' ] ],
720 $this->title
->getUserPermissionsErrors( 'bogus',
722 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
723 [ 'protectedpagetext', 'editprotected', 'edit' ],
724 [ 'protectedpagetext', 'protect', 'edit' ] ],
725 $this->title
->getUserPermissionsErrors( 'edit',
728 $this->setUserPerm( [ "edit", "editprotected" ] );
729 $this->assertEquals( false,
730 $this->title
->quickUserCan( 'bogus', $this->user
) );
731 $this->assertEquals( false,
732 $this->title
->quickUserCan( 'edit', $this->user
) );
733 $this->assertEquals( [ [ 'badaccess-group0' ],
734 [ 'protectedpagetext', 'bogus', 'bogus' ],
735 [ 'protectedpagetext', 'protect', 'bogus' ],
736 [ 'protectedpagetext', 'protect', 'bogus' ] ],
737 $this->title
->getUserPermissionsErrors( 'bogus',
739 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
740 [ 'protectedpagetext', 'protect', 'edit' ],
741 [ 'protectedpagetext', 'protect', 'edit' ] ],
742 $this->title
->getUserPermissionsErrors( 'edit',
747 * @covers Title::checkCascadingSourcesRestrictions
749 public function testCascadingSourcesRestrictions() {
750 $this->setTitle( NS_MAIN
, "test page" );
751 $this->setUserPerm( [ "edit", "bogus" ] );
753 $this->title
->mCascadeSources
= [
754 Title
::makeTitle( NS_MAIN
, "Bogus" ),
755 Title
::makeTitle( NS_MAIN
, "UnBogus" )
757 $this->title
->mCascadingRestrictions
= [
758 "bogus" => [ 'bogus', "sysop", "protect", "" ]
761 $this->assertEquals( false,
762 $this->title
->userCan( 'bogus', $this->user
) );
763 $this->assertEquals( [
764 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
765 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
766 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
767 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
769 $this->assertEquals( true,
770 $this->title
->userCan( 'edit', $this->user
) );
771 $this->assertEquals( [],
772 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
776 * @todo This test method should be split up into separate test methods and
778 * @covers Title::checkActionPermissions
780 public function testActionPermissions() {
781 $this->setUserPerm( [ "createpage" ] );
782 $this->setTitle( NS_MAIN
, "test page" );
783 $this->title
->mTitleProtection
['permission'] = '';
784 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
785 $this->title
->mTitleProtection
['expiry'] = 'infinity';
786 $this->title
->mTitleProtection
['reason'] = 'test';
787 $this->title
->mCascadeRestriction
= false;
789 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
790 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
791 $this->assertEquals( false,
792 $this->title
->userCan( 'create', $this->user
) );
794 $this->title
->mTitleProtection
['permission'] = 'editprotected';
795 $this->setUserPerm( [ 'createpage', 'protect' ] );
796 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
797 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
798 $this->assertEquals( false,
799 $this->title
->userCan( 'create', $this->user
) );
801 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
802 $this->assertEquals( [],
803 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
804 $this->assertEquals( true,
805 $this->title
->userCan( 'create', $this->user
) );
807 $this->setUserPerm( [ 'createpage' ] );
808 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
809 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
810 $this->assertEquals( false,
811 $this->title
->userCan( 'create', $this->user
) );
813 $this->setTitle( NS_MEDIA
, "test page" );
814 $this->setUserPerm( [ "move" ] );
815 $this->assertEquals( false,
816 $this->title
->userCan( 'move', $this->user
) );
817 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
818 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
820 $this->setTitle( NS_HELP
, "test page" );
821 $this->assertEquals( [],
822 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
823 $this->assertEquals( true,
824 $this->title
->userCan( 'move', $this->user
) );
826 $this->title
->mInterwiki
= "no";
827 $this->assertEquals( [ [ 'immobile-source-page' ] ],
828 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
829 $this->assertEquals( false,
830 $this->title
->userCan( 'move', $this->user
) );
832 $this->setTitle( NS_MEDIA
, "test page" );
833 $this->assertEquals( false,
834 $this->title
->userCan( 'move-target', $this->user
) );
835 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
836 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
838 $this->setTitle( NS_HELP
, "test page" );
839 $this->assertEquals( [],
840 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
841 $this->assertEquals( true,
842 $this->title
->userCan( 'move-target', $this->user
) );
844 $this->title
->mInterwiki
= "no";
845 $this->assertEquals( [ [ 'immobile-target-page' ] ],
846 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
847 $this->assertEquals( false,
848 $this->title
->userCan( 'move-target', $this->user
) );
852 * @covers Title::checkUserBlock
854 public function testUserBlock() {
855 $this->setMwGlobals( [
856 'wgEmailConfirmToEdit' => true,
857 'wgEmailAuthentication' => true,
860 $this->setUserPerm( [ "createpage", "move" ] );
861 $this->setTitle( NS_HELP
, "test page" );
863 # $wgEmailConfirmToEdit only applies to 'edit' action
864 $this->assertEquals( [],
865 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
866 $this->assertContains( [ 'confirmedittext' ],
867 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
869 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
870 $this->assertNotContains( [ 'confirmedittext' ],
871 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
873 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
874 $this->assertEquals( [],
875 $this->title
->getUserPermissionsErrors( 'move-target',
881 $this->user
->mBlockedby
= $this->user
->getId();
882 $this->user
->mBlock
= new Block( [
883 'address' => '127.0.8.1',
884 'by' => $this->user
->getId(),
885 'reason' => 'no reason given',
886 'timestamp' => $prev +
3600,
890 $this->user
->mBlock
->mTimestamp
= 0;
891 $this->assertEquals( [ [ 'autoblockedtext',
892 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
893 'Useruser', null, 'infinite', '127.0.8.1',
894 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
895 $this->title
->getUserPermissionsErrors( 'move-target',
898 $this->assertEquals( false, $this->title
->userCan( 'move-target', $this->user
) );
899 // quickUserCan should ignore user blocks
900 $this->assertEquals( true, $this->title
->quickUserCan( 'move-target', $this->user
) );
902 global $wgLocalTZoffset;
903 $wgLocalTZoffset = -60;
904 $this->user
->mBlockedby
= $this->user
->getName();
905 $this->user
->mBlock
= new Block( [
906 'address' => '127.0.8.1',
907 'by' => $this->user
->getId(),
908 'reason' => 'no reason given',
913 $this->assertEquals( [ [ 'blockedtext',
914 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
915 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
916 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
917 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
918 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
919 # $user->blockedFor() == ''
920 # $user->mBlock->mExpiry == 'infinity'
922 $this->user
->mBlockedby
= $this->user
->getName();
923 $this->user
->mBlock
= new Block( [
924 'address' => '127.0.8.1',
925 'by' => $this->user
->getId(),
926 'reason' => 'no reason given',
930 'systemBlock' => 'test',
932 $this->assertEquals( [ [ 'systemblockedtext',
933 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
934 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
935 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
936 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );