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 $this->setUser( $this->userName
);
455 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
456 $this->runConfigEditPermissions(
457 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
459 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
460 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
461 [ [ 'badaccess-group0' ] ],
463 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
464 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
465 [ [ 'badaccess-group0' ] ]
470 * @todo This test method should be split up into separate test methods and
472 * @covers Title::checkUserConfigPermissions
474 public function testJsonConfigEditPermissions() {
475 $this->setUser( $this->userName
);
477 $this->setTitle( NS_USER
, $this->userName
. '/test.json' );
478 $this->runConfigEditPermissions(
479 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
481 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
482 [ [ 'badaccess-group0' ] ],
483 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
485 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
486 [ [ 'badaccess-group0' ] ],
487 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ]
492 * @todo This test method should be split up into separate test methods and
494 * @covers Title::checkUserConfigPermissions
496 public function testCssConfigEditPermissions() {
497 $this->setUser( $this->userName
);
499 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
500 $this->runConfigEditPermissions(
501 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
503 [ [ 'badaccess-group0' ] ],
504 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
505 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
507 [ [ 'badaccess-group0' ] ],
508 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
509 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ]
514 * @todo This test method should be split up into separate test methods and
516 * @covers Title::checkUserConfigPermissions
518 public function testOtherJsConfigEditPermissions() {
519 $this->setUser( $this->userName
);
521 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
522 $this->runConfigEditPermissions(
523 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
525 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
526 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
527 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
529 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
530 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
531 [ [ 'badaccess-group0' ] ]
536 * @todo This test method should be split up into separate test methods and
538 * @covers Title::checkUserConfigPermissions
540 public function testOtherJsonConfigEditPermissions() {
541 $this->setUser( $this->userName
);
543 $this->setTitle( NS_USER
, $this->altUserName
. '/test.json' );
544 $this->runConfigEditPermissions(
545 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
547 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
548 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
549 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
551 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
552 [ [ 'badaccess-group0' ] ],
553 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ]
558 * @todo This test method should be split up into separate test methods and
560 * @covers Title::checkUserConfigPermissions
562 public function testOtherCssConfigEditPermissions() {
563 $this->setUser( $this->userName
);
565 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
566 $this->runConfigEditPermissions(
567 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
569 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
570 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
571 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
573 [ [ 'badaccess-group0' ] ],
574 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
575 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ]
580 * @todo This test method should be split up into separate test methods and
582 * @covers Title::checkUserConfigPermissions
584 public function testOtherNonConfigEditPermissions() {
585 $this->setUser( $this->userName
);
587 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
588 $this->runConfigEditPermissions(
589 [ [ 'badaccess-group0' ] ],
591 [ [ 'badaccess-group0' ] ],
592 [ [ 'badaccess-group0' ] ],
593 [ [ 'badaccess-group0' ] ],
595 [ [ 'badaccess-group0' ] ],
596 [ [ 'badaccess-group0' ] ],
597 [ [ 'badaccess-group0' ] ]
601 protected function runConfigEditPermissions(
610 $this->setUserPerm( '' );
611 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
612 $this->assertEquals( $resultNone, $result );
614 $this->setUserPerm( 'editmyusercss' );
615 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
616 $this->assertEquals( $resultMyCss, $result );
618 $this->setUserPerm( 'editmyuserjson' );
619 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
620 $this->assertEquals( $resultMyJson, $result );
622 $this->setUserPerm( 'editmyuserjs' );
623 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
624 $this->assertEquals( $resultMyJs, $result );
626 $this->setUserPerm( 'editusercss' );
627 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
628 $this->assertEquals( $resultUserCss, $result );
630 $this->setUserPerm( 'edituserjson' );
631 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
632 $this->assertEquals( $resultUserJson, $result );
634 $this->setUserPerm( 'edituserjs' );
635 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
636 $this->assertEquals( $resultUserJs, $result );
638 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
639 $result = $this->title
->getUserPermissionsErrors( 'bogus', $this->user
);
640 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
644 * @todo This test method should be split up into separate test methods and
646 * @covers Title::checkPageRestrictions
648 public function testPageRestrictions() {
649 $prefix = MediaWikiServices
::getInstance()->getContentLanguage()->
650 getFormattedNsText( NS_PROJECT
);
652 $this->setTitle( NS_MAIN
);
653 $this->title
->mRestrictionsLoaded
= true;
654 $this->setUserPerm( "edit" );
655 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
657 $this->assertEquals( [],
658 $this->title
->getUserPermissionsErrors( 'edit',
661 $this->assertEquals( true,
662 $this->title
->quickUserCan( 'edit', $this->user
) );
663 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
664 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
666 $this->assertEquals( [ [ 'badaccess-group0' ],
667 [ 'protectedpagetext', 'bogus', 'bogus' ],
668 [ 'protectedpagetext', 'editprotected', 'bogus' ],
669 [ 'protectedpagetext', 'protect', 'bogus' ] ],
670 $this->title
->getUserPermissionsErrors( 'bogus',
672 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
673 [ 'protectedpagetext', 'editprotected', 'edit' ],
674 [ 'protectedpagetext', 'protect', 'edit' ] ],
675 $this->title
->getUserPermissionsErrors( 'edit',
677 $this->setUserPerm( "" );
678 $this->assertEquals( [ [ 'badaccess-group0' ],
679 [ 'protectedpagetext', 'bogus', 'bogus' ],
680 [ 'protectedpagetext', 'editprotected', 'bogus' ],
681 [ 'protectedpagetext', 'protect', 'bogus' ] ],
682 $this->title
->getUserPermissionsErrors( 'bogus',
684 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
685 [ 'protectedpagetext', 'bogus', 'edit' ],
686 [ 'protectedpagetext', 'editprotected', 'edit' ],
687 [ 'protectedpagetext', 'protect', 'edit' ] ],
688 $this->title
->getUserPermissionsErrors( 'edit',
690 $this->setUserPerm( [ "edit", "editprotected" ] );
691 $this->assertEquals( [ [ 'badaccess-group0' ],
692 [ 'protectedpagetext', 'bogus', 'bogus' ],
693 [ 'protectedpagetext', 'protect', 'bogus' ] ],
694 $this->title
->getUserPermissionsErrors( 'bogus',
696 $this->assertEquals( [
697 [ 'protectedpagetext', 'bogus', 'edit' ],
698 [ 'protectedpagetext', 'protect', 'edit' ] ],
699 $this->title
->getUserPermissionsErrors( 'edit',
702 $this->title
->mCascadeRestriction
= true;
703 $this->setUserPerm( "edit" );
704 $this->assertEquals( false,
705 $this->title
->quickUserCan( 'bogus', $this->user
) );
706 $this->assertEquals( false,
707 $this->title
->quickUserCan( 'edit', $this->user
) );
708 $this->assertEquals( [ [ 'badaccess-group0' ],
709 [ 'protectedpagetext', 'bogus', 'bogus' ],
710 [ 'protectedpagetext', 'editprotected', 'bogus' ],
711 [ 'protectedpagetext', 'protect', 'bogus' ] ],
712 $this->title
->getUserPermissionsErrors( 'bogus',
714 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
715 [ 'protectedpagetext', 'editprotected', 'edit' ],
716 [ 'protectedpagetext', 'protect', 'edit' ] ],
717 $this->title
->getUserPermissionsErrors( 'edit',
720 $this->setUserPerm( [ "edit", "editprotected" ] );
721 $this->assertEquals( false,
722 $this->title
->quickUserCan( 'bogus', $this->user
) );
723 $this->assertEquals( false,
724 $this->title
->quickUserCan( 'edit', $this->user
) );
725 $this->assertEquals( [ [ 'badaccess-group0' ],
726 [ 'protectedpagetext', 'bogus', 'bogus' ],
727 [ 'protectedpagetext', 'protect', 'bogus' ],
728 [ 'protectedpagetext', 'protect', 'bogus' ] ],
729 $this->title
->getUserPermissionsErrors( 'bogus',
731 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
732 [ 'protectedpagetext', 'protect', 'edit' ],
733 [ 'protectedpagetext', 'protect', 'edit' ] ],
734 $this->title
->getUserPermissionsErrors( 'edit',
739 * @covers Title::checkCascadingSourcesRestrictions
741 public function testCascadingSourcesRestrictions() {
742 $this->setTitle( NS_MAIN
, "test page" );
743 $this->setUserPerm( [ "edit", "bogus" ] );
745 $this->title
->mCascadeSources
= [
746 Title
::makeTitle( NS_MAIN
, "Bogus" ),
747 Title
::makeTitle( NS_MAIN
, "UnBogus" )
749 $this->title
->mCascadingRestrictions
= [
750 "bogus" => [ 'bogus', "sysop", "protect", "" ]
753 $this->assertEquals( false,
754 $this->title
->userCan( 'bogus', $this->user
) );
755 $this->assertEquals( [
756 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
757 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
758 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
759 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
761 $this->assertEquals( true,
762 $this->title
->userCan( 'edit', $this->user
) );
763 $this->assertEquals( [],
764 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
768 * @todo This test method should be split up into separate test methods and
770 * @covers Title::checkActionPermissions
772 public function testActionPermissions() {
773 $this->setUserPerm( [ "createpage" ] );
774 $this->setTitle( NS_MAIN
, "test page" );
775 $this->title
->mTitleProtection
['permission'] = '';
776 $this->title
->mTitleProtection
['user'] = $this->user
->getId();
777 $this->title
->mTitleProtection
['expiry'] = 'infinity';
778 $this->title
->mTitleProtection
['reason'] = 'test';
779 $this->title
->mCascadeRestriction
= false;
781 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
782 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
783 $this->assertEquals( false,
784 $this->title
->userCan( 'create', $this->user
) );
786 $this->title
->mTitleProtection
['permission'] = 'editprotected';
787 $this->setUserPerm( [ 'createpage', 'protect' ] );
788 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
789 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
790 $this->assertEquals( false,
791 $this->title
->userCan( 'create', $this->user
) );
793 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
794 $this->assertEquals( [],
795 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
796 $this->assertEquals( true,
797 $this->title
->userCan( 'create', $this->user
) );
799 $this->setUserPerm( [ 'createpage' ] );
800 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
801 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
802 $this->assertEquals( false,
803 $this->title
->userCan( 'create', $this->user
) );
805 $this->setTitle( NS_MEDIA
, "test page" );
806 $this->setUserPerm( [ "move" ] );
807 $this->assertEquals( false,
808 $this->title
->userCan( 'move', $this->user
) );
809 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
810 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
812 $this->setTitle( NS_HELP
, "test page" );
813 $this->assertEquals( [],
814 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
815 $this->assertEquals( true,
816 $this->title
->userCan( 'move', $this->user
) );
818 $this->title
->mInterwiki
= "no";
819 $this->assertEquals( [ [ 'immobile-source-page' ] ],
820 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
821 $this->assertEquals( false,
822 $this->title
->userCan( 'move', $this->user
) );
824 $this->setTitle( NS_MEDIA
, "test page" );
825 $this->assertEquals( false,
826 $this->title
->userCan( 'move-target', $this->user
) );
827 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
828 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
830 $this->setTitle( NS_HELP
, "test page" );
831 $this->assertEquals( [],
832 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
833 $this->assertEquals( true,
834 $this->title
->userCan( 'move-target', $this->user
) );
836 $this->title
->mInterwiki
= "no";
837 $this->assertEquals( [ [ 'immobile-target-page' ] ],
838 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
839 $this->assertEquals( false,
840 $this->title
->userCan( 'move-target', $this->user
) );
844 * @covers Title::checkUserBlock
846 public function testUserBlock() {
847 $this->setMwGlobals( [
848 'wgEmailConfirmToEdit' => true,
849 'wgEmailAuthentication' => true,
852 $this->setUserPerm( [ "createpage", "move" ] );
853 $this->setTitle( NS_HELP
, "test page" );
855 # $wgEmailConfirmToEdit only applies to 'edit' action
856 $this->assertEquals( [],
857 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
858 $this->assertContains( [ 'confirmedittext' ],
859 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
861 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
862 $this->assertNotContains( [ 'confirmedittext' ],
863 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
865 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
866 $this->assertEquals( [],
867 $this->title
->getUserPermissionsErrors( 'move-target',
873 $this->user
->mBlockedby
= $this->user
->getId();
874 $this->user
->mBlock
= new Block( [
875 'address' => '127.0.8.1',
876 'by' => $this->user
->getId(),
877 'reason' => 'no reason given',
878 'timestamp' => $prev +
3600,
882 $this->user
->mBlock
->mTimestamp
= 0;
883 $this->assertEquals( [ [ 'autoblockedtext',
884 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
885 'Useruser', null, 'infinite', '127.0.8.1',
886 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
887 $this->title
->getUserPermissionsErrors( 'move-target',
890 $this->assertEquals( false, $this->title
->userCan( 'move-target', $this->user
) );
891 // quickUserCan should ignore user blocks
892 $this->assertEquals( true, $this->title
->quickUserCan( 'move-target', $this->user
) );
894 global $wgLocalTZoffset;
895 $wgLocalTZoffset = -60;
896 $this->user
->mBlockedby
= $this->user
->getName();
897 $this->user
->mBlock
= new Block( [
898 'address' => '127.0.8.1',
899 'by' => $this->user
->getId(),
900 'reason' => 'no reason given',
905 $this->assertEquals( [ [ 'blockedtext',
906 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
907 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
908 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
909 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
910 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
911 # $user->blockedFor() == ''
912 # $user->mBlock->mExpiry == 'infinity'
914 $this->user
->mBlockedby
= $this->user
->getName();
915 $this->user
->mBlock
= new Block( [
916 'address' => '127.0.8.1',
917 'by' => $this->user
->getId(),
918 'reason' => 'no reason given',
922 'systemBlock' => 'test',
924 $this->assertEquals( [ [ 'systemblockedtext',
925 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
926 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
927 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
928 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );