6 * @covers Title::getUserPermissionsErrors
7 * @covers Title::getUserPermissionsErrorsInternal
9 class TitlePermissionTest
extends MediaWikiLangTestCase
{
14 protected $userName, $altUserName;
24 protected $user, $anonUser, $userUser, $altUser;
26 protected function setUp() {
29 $langObj = Language
::factory( 'en' );
31 $localOffset = date( 'Z' ) / 60;
33 $this->setMwGlobals( [
34 'wgContLang' => $langObj,
35 'wgLanguageCode' => 'en',
37 'wgLocaltimezone' => $localZone,
38 'wgLocalTZoffset' => $localOffset,
39 'wgNamespaceProtection' => [
40 NS_MEDIAWIKI
=> 'editinterface',
43 // Without this testUserBlock will use a non-English context on non-English MediaWiki
44 // installations (because of how Title::checkUserBlock is implemented) and fail.
45 RequestContext
::resetMain();
47 $this->userName
= 'Useruser';
48 $this->altUserName
= 'Altuseruser';
49 date_default_timezone_set( $localZone );
51 $this->title
= Title
::makeTitle( NS_MAIN
, "Main Page" );
52 if ( !isset( $this->userUser
) ||
!( $this->userUser
instanceof User
) ) {
53 $this->userUser
= User
::newFromName( $this->userName
);
55 if ( !$this->userUser
->getID() ) {
56 $this->userUser
= User
::createNew( $this->userName
, [
57 "email" => "test@example.com",
58 "real_name" => "Test User" ] );
59 $this->userUser
->load();
62 $this->altUser
= User
::newFromName( $this->altUserName
);
63 if ( !$this->altUser
->getID() ) {
64 $this->altUser
= User
::createNew( $this->altUserName
, [
65 "email" => "alttest@example.com",
66 "real_name" => "Test User Alt" ] );
67 $this->altUser
->load();
70 $this->anonUser
= User
::newFromId( 0 );
72 $this->user
= $this->userUser
;
76 protected function setUserPerm( $perm ) {
77 // Setting member variables is evil!!!
79 if ( is_array( $perm ) ) {
80 $this->user
->mRights
= $perm;
82 $this->user
->mRights
= [ $perm ];
86 protected function setTitle( $ns, $title = "Main_Page" ) {
87 $this->title
= Title
::makeTitle( $ns, $title );
90 protected function setUser( $userName = null ) {
91 if ( $userName === 'anon' ) {
92 $this->user
= $this->anonUser
;
93 } elseif ( $userName === null ||
$userName === $this->userName
) {
94 $this->user
= $this->userUser
;
96 $this->user
= $this->altUser
;
101 * @todo This test method should be split up into separate test methods and
104 public function testQuickPermissions() {
106 $prefix = $wgContLang->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
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
450 public function testCssAndJavascriptPermissions() {
451 $this->setUser( $this->userName
);
453 $this->setTitle( NS_USER
, $this->userName
. '/test.js' );
454 $this->runCSSandJSPermissions(
455 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
456 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
457 [ [ 'badaccess-group0' ] ],
458 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
459 [ [ 'badaccess-group0' ] ]
462 $this->setTitle( NS_USER
, $this->userName
. '/test.css' );
463 $this->runCSSandJSPermissions(
464 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
465 [ [ 'badaccess-group0' ] ],
466 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
467 [ [ 'badaccess-group0' ] ],
468 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ]
471 $this->setTitle( NS_USER
, $this->altUserName
. '/test.js' );
472 $this->runCSSandJSPermissions(
473 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
474 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
475 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
476 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
477 [ [ 'badaccess-group0' ] ]
480 $this->setTitle( NS_USER
, $this->altUserName
. '/test.css' );
481 $this->runCSSandJSPermissions(
482 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
483 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
484 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
485 [ [ 'badaccess-group0' ] ],
486 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ]
489 $this->setTitle( NS_USER
, $this->altUserName
. '/tempo' );
490 $this->runCSSandJSPermissions(
491 [ [ 'badaccess-group0' ] ],
492 [ [ 'badaccess-group0' ] ],
493 [ [ 'badaccess-group0' ] ],
494 [ [ 'badaccess-group0' ] ],
495 [ [ 'badaccess-group0' ] ]
499 protected function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
500 $this->setUserPerm( '' );
501 $this->assertEquals( $result0,
502 $this->title
->getUserPermissionsErrors( 'bogus',
505 $this->setUserPerm( 'editmyusercss' );
506 $this->assertEquals( $result1,
507 $this->title
->getUserPermissionsErrors( 'bogus',
510 $this->setUserPerm( 'editmyuserjs' );
511 $this->assertEquals( $result2,
512 $this->title
->getUserPermissionsErrors( 'bogus',
515 $this->setUserPerm( 'editusercss' );
516 $this->assertEquals( $result3,
517 $this->title
->getUserPermissionsErrors( 'bogus',
520 $this->setUserPerm( 'edituserjs' );
521 $this->assertEquals( $result4,
522 $this->title
->getUserPermissionsErrors( 'bogus',
525 $this->setUserPerm( 'editusercssjs' );
526 $this->assertEquals( [ [ 'badaccess-group0' ] ],
527 $this->title
->getUserPermissionsErrors( 'bogus',
530 $this->setUserPerm( [ 'edituserjs', 'editusercss' ] );
531 $this->assertEquals( [ [ 'badaccess-group0' ] ],
532 $this->title
->getUserPermissionsErrors( 'bogus',
537 * @todo This test method should be split up into separate test methods and
540 public function testPageRestrictions() {
543 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT
);
545 $this->setTitle( NS_MAIN
);
546 $this->title
->mRestrictionsLoaded
= true;
547 $this->setUserPerm( "edit" );
548 $this->title
->mRestrictions
= [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
550 $this->assertEquals( [],
551 $this->title
->getUserPermissionsErrors( 'edit',
554 $this->assertEquals( true,
555 $this->title
->quickUserCan( 'edit', $this->user
) );
556 $this->title
->mRestrictions
= [ "edit" => [ 'bogus', "sysop", "protect", "" ],
557 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
559 $this->assertEquals( [ [ 'badaccess-group0' ],
560 [ 'protectedpagetext', 'bogus', 'bogus' ],
561 [ 'protectedpagetext', 'editprotected', 'bogus' ],
562 [ 'protectedpagetext', 'protect', 'bogus' ] ],
563 $this->title
->getUserPermissionsErrors( 'bogus',
565 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
566 [ 'protectedpagetext', 'editprotected', 'edit' ],
567 [ 'protectedpagetext', 'protect', 'edit' ] ],
568 $this->title
->getUserPermissionsErrors( 'edit',
570 $this->setUserPerm( "" );
571 $this->assertEquals( [ [ 'badaccess-group0' ],
572 [ 'protectedpagetext', 'bogus', 'bogus' ],
573 [ 'protectedpagetext', 'editprotected', 'bogus' ],
574 [ 'protectedpagetext', 'protect', 'bogus' ] ],
575 $this->title
->getUserPermissionsErrors( 'bogus',
577 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
578 [ 'protectedpagetext', 'bogus', 'edit' ],
579 [ 'protectedpagetext', 'editprotected', 'edit' ],
580 [ 'protectedpagetext', 'protect', 'edit' ] ],
581 $this->title
->getUserPermissionsErrors( 'edit',
583 $this->setUserPerm( [ "edit", "editprotected" ] );
584 $this->assertEquals( [ [ 'badaccess-group0' ],
585 [ 'protectedpagetext', 'bogus', 'bogus' ],
586 [ 'protectedpagetext', 'protect', 'bogus' ] ],
587 $this->title
->getUserPermissionsErrors( 'bogus',
589 $this->assertEquals( [
590 [ 'protectedpagetext', 'bogus', 'edit' ],
591 [ 'protectedpagetext', 'protect', 'edit' ] ],
592 $this->title
->getUserPermissionsErrors( 'edit',
595 $this->title
->mCascadeRestriction
= true;
596 $this->setUserPerm( "edit" );
597 $this->assertEquals( false,
598 $this->title
->quickUserCan( 'bogus', $this->user
) );
599 $this->assertEquals( false,
600 $this->title
->quickUserCan( 'edit', $this->user
) );
601 $this->assertEquals( [ [ 'badaccess-group0' ],
602 [ 'protectedpagetext', 'bogus', 'bogus' ],
603 [ 'protectedpagetext', 'editprotected', 'bogus' ],
604 [ 'protectedpagetext', 'protect', 'bogus' ] ],
605 $this->title
->getUserPermissionsErrors( 'bogus',
607 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
608 [ 'protectedpagetext', 'editprotected', 'edit' ],
609 [ 'protectedpagetext', 'protect', 'edit' ] ],
610 $this->title
->getUserPermissionsErrors( 'edit',
613 $this->setUserPerm( [ "edit", "editprotected" ] );
614 $this->assertEquals( false,
615 $this->title
->quickUserCan( 'bogus', $this->user
) );
616 $this->assertEquals( false,
617 $this->title
->quickUserCan( 'edit', $this->user
) );
618 $this->assertEquals( [ [ 'badaccess-group0' ],
619 [ 'protectedpagetext', 'bogus', 'bogus' ],
620 [ 'protectedpagetext', 'protect', 'bogus' ],
621 [ 'protectedpagetext', 'protect', 'bogus' ] ],
622 $this->title
->getUserPermissionsErrors( 'bogus',
624 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
625 [ 'protectedpagetext', 'protect', 'edit' ],
626 [ 'protectedpagetext', 'protect', 'edit' ] ],
627 $this->title
->getUserPermissionsErrors( 'edit',
631 public function testCascadingSourcesRestrictions() {
632 $this->setTitle( NS_MAIN
, "test page" );
633 $this->setUserPerm( [ "edit", "bogus" ] );
635 $this->title
->mCascadeSources
= [
636 Title
::makeTitle( NS_MAIN
, "Bogus" ),
637 Title
::makeTitle( NS_MAIN
, "UnBogus" )
639 $this->title
->mCascadingRestrictions
= [
640 "bogus" => [ 'bogus', "sysop", "protect", "" ]
643 $this->assertEquals( false,
644 $this->title
->userCan( 'bogus', $this->user
) );
645 $this->assertEquals( [
646 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
647 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
648 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
649 $this->title
->getUserPermissionsErrors( 'bogus', $this->user
) );
651 $this->assertEquals( true,
652 $this->title
->userCan( 'edit', $this->user
) );
653 $this->assertEquals( [],
654 $this->title
->getUserPermissionsErrors( 'edit', $this->user
) );
658 * @todo This test method should be split up into separate test methods and
661 public function testActionPermissions() {
662 $this->setUserPerm( [ "createpage" ] );
663 $this->setTitle( NS_MAIN
, "test page" );
664 $this->title
->mTitleProtection
['permission'] = '';
665 $this->title
->mTitleProtection
['user'] = $this->user
->getID();
666 $this->title
->mTitleProtection
['expiry'] = 'infinity';
667 $this->title
->mTitleProtection
['reason'] = 'test';
668 $this->title
->mCascadeRestriction
= false;
670 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
671 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
672 $this->assertEquals( false,
673 $this->title
->userCan( 'create', $this->user
) );
675 $this->title
->mTitleProtection
['permission'] = 'editprotected';
676 $this->setUserPerm( [ 'createpage', 'protect' ] );
677 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
678 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
679 $this->assertEquals( false,
680 $this->title
->userCan( 'create', $this->user
) );
682 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
683 $this->assertEquals( [],
684 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
685 $this->assertEquals( true,
686 $this->title
->userCan( 'create', $this->user
) );
688 $this->setUserPerm( [ 'createpage' ] );
689 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
690 $this->title
->getUserPermissionsErrors( 'create', $this->user
) );
691 $this->assertEquals( false,
692 $this->title
->userCan( 'create', $this->user
) );
694 $this->setTitle( NS_MEDIA
, "test page" );
695 $this->setUserPerm( [ "move" ] );
696 $this->assertEquals( false,
697 $this->title
->userCan( 'move', $this->user
) );
698 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
699 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
701 $this->setTitle( NS_HELP
, "test page" );
702 $this->assertEquals( [],
703 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
704 $this->assertEquals( true,
705 $this->title
->userCan( 'move', $this->user
) );
707 $this->title
->mInterwiki
= "no";
708 $this->assertEquals( [ [ 'immobile-source-page' ] ],
709 $this->title
->getUserPermissionsErrors( 'move', $this->user
) );
710 $this->assertEquals( false,
711 $this->title
->userCan( 'move', $this->user
) );
713 $this->setTitle( NS_MEDIA
, "test page" );
714 $this->assertEquals( false,
715 $this->title
->userCan( 'move-target', $this->user
) );
716 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
717 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
719 $this->setTitle( NS_HELP
, "test page" );
720 $this->assertEquals( [],
721 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
722 $this->assertEquals( true,
723 $this->title
->userCan( 'move-target', $this->user
) );
725 $this->title
->mInterwiki
= "no";
726 $this->assertEquals( [ [ 'immobile-target-page' ] ],
727 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
728 $this->assertEquals( false,
729 $this->title
->userCan( 'move-target', $this->user
) );
732 public function testUserBlock() {
733 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
734 $wgEmailConfirmToEdit = true;
735 $wgEmailAuthentication = true;
737 $this->setUserPerm( [ "createpage", "move" ] );
738 $this->setTitle( NS_HELP
, "test page" );
741 $this->assertEquals( [ [ 'confirmedittext' ] ],
742 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
743 $wgEmailConfirmToEdit = false;
744 $this->assertEquals( true, $this->title
->userCan( 'move-target', $this->user
) );
746 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
747 $this->assertEquals( [],
748 $this->title
->getUserPermissionsErrors( 'move-target',
754 $this->user
->mBlockedby
= $this->user
->getId();
755 $this->user
->mBlock
= new Block( [
756 'address' => '127.0.8.1',
757 'by' => $this->user
->getId(),
758 'reason' => 'no reason given',
759 'timestamp' => $prev +
3600,
763 $this->user
->mBlock
->mTimestamp
= 0;
764 $this->assertEquals( [ [ 'autoblockedtext',
765 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
766 'Useruser', null, 'infinite', '127.0.8.1',
767 $wgLang->timeanddate( wfTimestamp( TS_MW
, $prev ), true ) ] ],
768 $this->title
->getUserPermissionsErrors( 'move-target',
771 $this->assertEquals( false, $this->title
->userCan( 'move-target', $this->user
) );
772 // quickUserCan should ignore user blocks
773 $this->assertEquals( true, $this->title
->quickUserCan( 'move-target', $this->user
) );
775 global $wgLocalTZoffset;
776 $wgLocalTZoffset = -60;
777 $this->user
->mBlockedby
= $this->user
->getName();
778 $this->user
->mBlock
= new Block( [
779 'address' => '127.0.8.1',
780 'by' => $this->user
->getId(),
781 'reason' => 'no reason given',
786 $this->assertEquals( [ [ 'blockedtext',
787 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
788 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
789 $wgLang->timeanddate( wfTimestamp( TS_MW
, $now ), true ) ] ],
790 $this->title
->getUserPermissionsErrors( 'move-target', $this->user
) );
791 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
792 # $user->blockedFor() == ''
793 # $user->mBlock->mExpiry == 'infinity'