Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / TitlePermissionTest.php
blobac80a9aa3771f3cd6585d882a6f735242faa4623
1 <?php
3 /**
4 * @group Database
6 * @covers Title::getUserPermissionsErrors
7 * @covers Title::getUserPermissionsErrorsInternal
8 */
9 class TitlePermissionTest extends MediaWikiLangTestCase {
11 /**
12 * @var string
14 protected $userName, $altUserName;
16 /**
17 * @var Title
19 protected $title;
21 /**
22 * @var User
24 protected $user, $anonUser, $userUser, $altUser;
26 protected function setUp() {
27 parent::setUp();
29 $langObj = Language::factory( 'en' );
30 $localZone = 'UTC';
31 $localOffset = date( 'Z' ) / 60;
33 $this->setMwGlobals( array(
34 'wgMemc' => new EmptyBagOStuff,
35 'wgContLang' => $langObj,
36 'wgLanguageCode' => 'en',
37 'wgLang' => $langObj,
38 'wgLocaltimezone' => $localZone,
39 'wgLocalTZoffset' => $localOffset,
40 'wgNamespaceProtection' => array(
41 NS_MEDIAWIKI => 'editinterface',
43 ) );
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, array(
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, array(
63 "email" => "alttest@example.com",
64 "real_name" => "Test User Alt" ) );
65 $this->altUser->load();
68 $this->anonUser = User::newFromId( 0 );
70 $this->user = $this->userUser;
74 protected function setUserPerm( $perm ) {
75 // Setting member variables is evil!!!
77 if ( is_array( $perm ) ) {
78 $this->user->mRights = $perm;
79 } else {
80 $this->user->mRights = array( $perm );
84 protected function setTitle( $ns, $title = "Main_Page" ) {
85 $this->title = Title::makeTitle( $ns, $title );
88 protected function setUser( $userName = null ) {
89 if ( $userName === 'anon' ) {
90 $this->user = $this->anonUser;
91 } elseif ( $userName === null || $userName === $this->userName ) {
92 $this->user = $this->userUser;
93 } else {
94 $this->user = $this->altUser;
98 /**
99 * @todo This test method should be split up into separate test methods and
100 * data providers
102 public function testQuickPermissions() {
103 global $wgContLang;
104 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
106 $this->setUser( 'anon' );
107 $this->setTitle( NS_TALK );
108 $this->setUserPerm( "createtalk" );
109 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
110 $this->assertEquals( array(), $res );
112 $this->setTitle( NS_TALK );
113 $this->setUserPerm( "createpage" );
114 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
115 $this->assertEquals( array( array( "nocreatetext" ) ), $res );
117 $this->setTitle( NS_TALK );
118 $this->setUserPerm( "" );
119 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
120 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
122 $this->setTitle( NS_MAIN );
123 $this->setUserPerm( "createpage" );
124 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
125 $this->assertEquals( array(), $res );
127 $this->setTitle( NS_MAIN );
128 $this->setUserPerm( "createtalk" );
129 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
130 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
132 $this->setUser( $this->userName );
133 $this->setTitle( NS_TALK );
134 $this->setUserPerm( "createtalk" );
135 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
136 $this->assertEquals( array(), $res );
138 $this->setTitle( NS_TALK );
139 $this->setUserPerm( "createpage" );
140 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
141 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
143 $this->setTitle( NS_TALK );
144 $this->setUserPerm( "" );
145 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
146 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
148 $this->setTitle( NS_MAIN );
149 $this->setUserPerm( "createpage" );
150 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
151 $this->assertEquals( array(), $res );
153 $this->setTitle( NS_MAIN );
154 $this->setUserPerm( "createtalk" );
155 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
156 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
158 $this->setTitle( NS_MAIN );
159 $this->setUserPerm( "" );
160 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
161 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
163 $this->setUser( 'anon' );
164 $this->setTitle( NS_USER, $this->userName . '' );
165 $this->setUserPerm( "" );
166 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
167 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
169 $this->setTitle( NS_USER, $this->userName . '/subpage' );
170 $this->setUserPerm( "" );
171 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
172 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
174 $this->setTitle( NS_USER, $this->userName . '' );
175 $this->setUserPerm( "move-rootuserpages" );
176 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
177 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
179 $this->setTitle( NS_USER, $this->userName . '/subpage' );
180 $this->setUserPerm( "move-rootuserpages" );
181 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
182 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
184 $this->setTitle( NS_USER, $this->userName . '' );
185 $this->setUserPerm( "" );
186 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
187 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
189 $this->setTitle( NS_USER, $this->userName . '/subpage' );
190 $this->setUserPerm( "" );
191 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
192 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
194 $this->setTitle( NS_USER, $this->userName . '' );
195 $this->setUserPerm( "move-rootuserpages" );
196 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
197 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
199 $this->setTitle( NS_USER, $this->userName . '/subpage' );
200 $this->setUserPerm( "move-rootuserpages" );
201 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
202 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
204 $this->setUser( $this->userName );
205 $this->setTitle( NS_FILE, "img.png" );
206 $this->setUserPerm( "" );
207 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
208 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ), $res );
210 $this->setTitle( NS_FILE, "img.png" );
211 $this->setUserPerm( "movefile" );
212 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
213 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
215 $this->setUser( 'anon' );
216 $this->setTitle( NS_FILE, "img.png" );
217 $this->setUserPerm( "" );
218 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
219 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ), $res );
221 $this->setTitle( NS_FILE, "img.png" );
222 $this->setUserPerm( "movefile" );
223 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
224 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
226 $this->setUser( $this->userName );
227 $this->setUserPerm( "move" );
228 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
230 $this->setUserPerm( "" );
231 $this->runGroupPermissions(
232 'move',
233 array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) )
236 $this->setUser( 'anon' );
237 $this->setUserPerm( "move" );
238 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
240 $this->setUserPerm( "" );
241 $this->runGroupPermissions(
242 'move',
243 array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
244 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) )
247 if ( $this->isWikitextNS( NS_MAIN ) ) {
248 //NOTE: some content models don't allow moving
249 // @todo find a Wikitext namespace for testing
251 $this->setTitle( NS_MAIN );
252 $this->setUser( 'anon' );
253 $this->setUserPerm( "move" );
254 $this->runGroupPermissions( 'move', array() );
256 $this->setUserPerm( "" );
257 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
258 array( array( 'movenologintext' ) ) );
260 $this->setUser( $this->userName );
261 $this->setUserPerm( "" );
262 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
264 $this->setUserPerm( "move" );
265 $this->runGroupPermissions( 'move', array() );
267 $this->setUser( 'anon' );
268 $this->setUserPerm( 'move' );
269 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
270 $this->assertEquals( array(), $res );
272 $this->setUserPerm( '' );
273 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
274 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
277 $this->setTitle( NS_USER );
278 $this->setUser( $this->userName );
279 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
280 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
281 $this->assertEquals( array(), $res );
283 $this->setUserPerm( "move" );
284 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
285 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
287 $this->setUser( 'anon' );
288 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
289 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
290 $this->assertEquals( array(), $res );
292 $this->setTitle( NS_USER, "User/subpage" );
293 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
294 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
295 $this->assertEquals( array(), $res );
297 $this->setUserPerm( "move" );
298 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
299 $this->assertEquals( array(), $res );
301 $this->setUser( 'anon' );
302 $check = array(
303 'edit' => array(
304 array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ) ),
305 array( array( 'badaccess-group0' ) ),
306 array(),
307 true
309 'protect' => array(
310 array( array(
311 'badaccess-groups',
312 "[[$prefix:Administrators|Administrators]]", 1 ),
313 array( 'protect-cantedit'
314 ) ),
315 array( array( 'badaccess-group0' ), array( 'protect-cantedit' ) ),
316 array( array( 'protect-cantedit' ) ),
317 false
319 '' => array( array(), array(), array(), true )
322 foreach ( array( "edit", "protect", "" ) as $action ) {
323 $this->setUserPerm( null );
324 $this->assertEquals( $check[$action][0],
325 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
327 global $wgGroupPermissions;
328 $old = $wgGroupPermissions;
329 $wgGroupPermissions = array();
331 $this->assertEquals( $check[$action][1],
332 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
333 $wgGroupPermissions = $old;
335 $this->setUserPerm( $action );
336 $this->assertEquals( $check[$action][2],
337 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
339 $this->setUserPerm( $action );
340 $this->assertEquals( $check[$action][3],
341 $this->title->userCan( $action, $this->user, true ) );
342 $this->assertEquals( $check[$action][3],
343 $this->title->quickUserCan( $action, $this->user ) );
344 # count( User::getGroupsWithPermissions( $action ) ) < 1
348 protected function runGroupPermissions( $action, $result, $result2 = null ) {
349 global $wgGroupPermissions;
351 if ( $result2 === null ) {
352 $result2 = $result;
355 $wgGroupPermissions['autoconfirmed']['move'] = false;
356 $wgGroupPermissions['user']['move'] = false;
357 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
358 $this->assertEquals( $result, $res );
360 $wgGroupPermissions['autoconfirmed']['move'] = true;
361 $wgGroupPermissions['user']['move'] = false;
362 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
363 $this->assertEquals( $result2, $res );
365 $wgGroupPermissions['autoconfirmed']['move'] = true;
366 $wgGroupPermissions['user']['move'] = true;
367 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
368 $this->assertEquals( $result2, $res );
370 $wgGroupPermissions['autoconfirmed']['move'] = false;
371 $wgGroupPermissions['user']['move'] = true;
372 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
373 $this->assertEquals( $result2, $res );
377 * @todo This test method should be split up into separate test methods and
378 * data providers
380 public function testSpecialsAndNSPermissions() {
381 global $wgNamespaceProtection;
382 $this->setUser( $this->userName );
384 $this->setTitle( NS_SPECIAL );
386 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
387 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
389 $this->setTitle( NS_MAIN );
390 $this->setUserPerm( 'bogus' );
391 $this->assertEquals( array(),
392 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
394 $this->setTitle( NS_MAIN );
395 $this->setUserPerm( '' );
396 $this->assertEquals( array( array( 'badaccess-group0' ) ),
397 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
399 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
401 $this->setTitle( NS_USER );
402 $this->setUserPerm( '' );
403 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
404 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
406 $this->setTitle( NS_MEDIAWIKI );
407 $this->setUserPerm( 'bogus' );
408 $this->assertEquals( array( array( 'protectedinterface' ) ),
409 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
411 $this->setTitle( NS_MEDIAWIKI );
412 $this->setUserPerm( 'bogus' );
413 $this->assertEquals( array( array( 'protectedinterface' ) ),
414 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
416 $wgNamespaceProtection = null;
418 $this->setUserPerm( 'bogus' );
419 $this->assertEquals( array(),
420 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
421 $this->assertEquals( true,
422 $this->title->userCan( 'bogus', $this->user ) );
424 $this->setUserPerm( '' );
425 $this->assertEquals( array( array( 'badaccess-group0' ) ),
426 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
427 $this->assertEquals( false,
428 $this->title->userCan( 'bogus', $this->user ) );
432 * @todo This test method should be split up into separate test methods and
433 * data providers
435 public function testCssAndJavascriptPermissions() {
436 $this->setUser( $this->userName );
438 $this->setTitle( NS_USER, $this->userName . '/test.js' );
439 $this->runCSSandJSPermissions(
440 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
441 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
442 array( array( 'badaccess-group0' ) ),
443 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
444 array( array( 'badaccess-group0' ) )
447 $this->setTitle( NS_USER, $this->userName . '/test.css' );
448 $this->runCSSandJSPermissions(
449 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
450 array( array( 'badaccess-group0' ) ),
451 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
452 array( array( 'badaccess-group0' ) ),
453 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) )
456 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
457 $this->runCSSandJSPermissions(
458 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
459 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
460 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
461 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
462 array( array( 'badaccess-group0' ) )
465 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
466 $this->runCSSandJSPermissions(
467 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
468 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
469 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
470 array( array( 'badaccess-group0' ) ),
471 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) )
474 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
475 $this->runCSSandJSPermissions(
476 array( array( 'badaccess-group0' ) ),
477 array( array( 'badaccess-group0' ) ),
478 array( array( 'badaccess-group0' ) ),
479 array( array( 'badaccess-group0' ) ),
480 array( array( 'badaccess-group0' ) )
484 protected function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
485 $this->setUserPerm( '' );
486 $this->assertEquals( $result0,
487 $this->title->getUserPermissionsErrors( 'bogus',
488 $this->user ) );
490 $this->setUserPerm( 'editmyusercss' );
491 $this->assertEquals( $result1,
492 $this->title->getUserPermissionsErrors( 'bogus',
493 $this->user ) );
495 $this->setUserPerm( 'editmyuserjs' );
496 $this->assertEquals( $result2,
497 $this->title->getUserPermissionsErrors( 'bogus',
498 $this->user ) );
500 $this->setUserPerm( 'editusercss' );
501 $this->assertEquals( $result3,
502 $this->title->getUserPermissionsErrors( 'bogus',
503 $this->user ) );
505 $this->setUserPerm( 'edituserjs' );
506 $this->assertEquals( $result4,
507 $this->title->getUserPermissionsErrors( 'bogus',
508 $this->user ) );
510 $this->setUserPerm( 'editusercssjs' );
511 $this->assertEquals( array( array( 'badaccess-group0' ) ),
512 $this->title->getUserPermissionsErrors( 'bogus',
513 $this->user ) );
515 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
516 $this->assertEquals( array( array( 'badaccess-group0' ) ),
517 $this->title->getUserPermissionsErrors( 'bogus',
518 $this->user ) );
522 * @todo This test method should be split up into separate test methods and
523 * data providers
525 public function testPageRestrictions() {
526 global $wgContLang;
528 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
530 $this->setTitle( NS_MAIN );
531 $this->title->mRestrictionsLoaded = true;
532 $this->setUserPerm( "edit" );
533 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
535 $this->assertEquals( array(),
536 $this->title->getUserPermissionsErrors( 'edit',
537 $this->user ) );
539 $this->assertEquals( true,
540 $this->title->quickUserCan( 'edit', $this->user ) );
541 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
542 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
544 $this->assertEquals( array( array( 'badaccess-group0' ),
545 array( 'protectedpagetext', 'bogus' ),
546 array( 'protectedpagetext', 'editprotected' ),
547 array( 'protectedpagetext', 'protect' ) ),
548 $this->title->getUserPermissionsErrors( 'bogus',
549 $this->user ) );
550 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
551 array( 'protectedpagetext', 'editprotected' ),
552 array( 'protectedpagetext', 'protect' ) ),
553 $this->title->getUserPermissionsErrors( 'edit',
554 $this->user ) );
555 $this->setUserPerm( "" );
556 $this->assertEquals( array( array( 'badaccess-group0' ),
557 array( 'protectedpagetext', 'bogus' ),
558 array( 'protectedpagetext', 'editprotected' ),
559 array( 'protectedpagetext', 'protect' ) ),
560 $this->title->getUserPermissionsErrors( 'bogus',
561 $this->user ) );
562 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
563 array( 'protectedpagetext', 'bogus' ),
564 array( 'protectedpagetext', 'editprotected' ),
565 array( 'protectedpagetext', 'protect' ) ),
566 $this->title->getUserPermissionsErrors( 'edit',
567 $this->user ) );
568 $this->setUserPerm( array( "edit", "editprotected" ) );
569 $this->assertEquals( array( array( 'badaccess-group0' ),
570 array( 'protectedpagetext', 'bogus' ),
571 array( 'protectedpagetext', 'protect' ) ),
572 $this->title->getUserPermissionsErrors( 'bogus',
573 $this->user ) );
574 $this->assertEquals( array(
575 array( 'protectedpagetext', 'bogus' ),
576 array( 'protectedpagetext', 'protect' ) ),
577 $this->title->getUserPermissionsErrors( 'edit',
578 $this->user ) );
580 $this->title->mCascadeRestriction = true;
581 $this->setUserPerm( "edit" );
582 $this->assertEquals( false,
583 $this->title->quickUserCan( 'bogus', $this->user ) );
584 $this->assertEquals( false,
585 $this->title->quickUserCan( 'edit', $this->user ) );
586 $this->assertEquals( array( array( 'badaccess-group0' ),
587 array( 'protectedpagetext', 'bogus' ),
588 array( 'protectedpagetext', 'editprotected' ),
589 array( 'protectedpagetext', 'protect' ) ),
590 $this->title->getUserPermissionsErrors( 'bogus',
591 $this->user ) );
592 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
593 array( 'protectedpagetext', 'editprotected' ),
594 array( 'protectedpagetext', 'protect' ) ),
595 $this->title->getUserPermissionsErrors( 'edit',
596 $this->user ) );
598 $this->setUserPerm( array( "edit", "editprotected" ) );
599 $this->assertEquals( false,
600 $this->title->quickUserCan( 'bogus', $this->user ) );
601 $this->assertEquals( false,
602 $this->title->quickUserCan( 'edit', $this->user ) );
603 $this->assertEquals( array( array( 'badaccess-group0' ),
604 array( 'protectedpagetext', 'bogus' ),
605 array( 'protectedpagetext', 'protect' ),
606 array( 'protectedpagetext', 'protect' ) ),
607 $this->title->getUserPermissionsErrors( 'bogus',
608 $this->user ) );
609 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
610 array( 'protectedpagetext', 'protect' ),
611 array( 'protectedpagetext', 'protect' ) ),
612 $this->title->getUserPermissionsErrors( 'edit',
613 $this->user ) );
616 public function testCascadingSourcesRestrictions() {
617 $this->setTitle( NS_MAIN, "test page" );
618 $this->setUserPerm( array( "edit", "bogus" ) );
620 $this->title->mCascadeSources = array(
621 Title::makeTitle( NS_MAIN, "Bogus" ),
622 Title::makeTitle( NS_MAIN, "UnBogus" )
624 $this->title->mCascadingRestrictions = array(
625 "bogus" => array( 'bogus', "sysop", "protect", "" )
628 $this->assertEquals( false,
629 $this->title->userCan( 'bogus', $this->user ) );
630 $this->assertEquals( array( array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
631 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
632 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ) ),
633 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
635 $this->assertEquals( true,
636 $this->title->userCan( 'edit', $this->user ) );
637 $this->assertEquals( array(),
638 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
642 * @todo This test method should be split up into separate test methods and
643 * data providers
645 public function testActionPermissions() {
646 $this->setUserPerm( array( "createpage" ) );
647 $this->setTitle( NS_MAIN, "test page" );
648 $this->title->mTitleProtection['pt_create_perm'] = '';
649 $this->title->mTitleProtection['pt_user'] = $this->user->getID();
650 $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
651 $this->title->mTitleProtection['pt_reason'] = 'test';
652 $this->title->mCascadeRestriction = false;
654 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
655 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
656 $this->assertEquals( false,
657 $this->title->userCan( 'create', $this->user ) );
659 $this->title->mTitleProtection['pt_create_perm'] = 'sysop';
660 $this->setUserPerm( array( 'createpage', 'protect' ) );
661 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
662 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
663 $this->assertEquals( false,
664 $this->title->userCan( 'create', $this->user ) );
666 $this->setUserPerm( array( 'createpage', 'editprotected' ) );
667 $this->assertEquals( array(),
668 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
669 $this->assertEquals( true,
670 $this->title->userCan( 'create', $this->user ) );
672 $this->setUserPerm( array( 'createpage' ) );
673 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
674 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
675 $this->assertEquals( false,
676 $this->title->userCan( 'create', $this->user ) );
678 $this->setTitle( NS_MEDIA, "test page" );
679 $this->setUserPerm( array( "move" ) );
680 $this->assertEquals( false,
681 $this->title->userCan( 'move', $this->user ) );
682 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
683 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
685 $this->setTitle( NS_HELP, "test page" );
686 $this->assertEquals( array(),
687 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
688 $this->assertEquals( true,
689 $this->title->userCan( 'move', $this->user ) );
691 $this->title->mInterwiki = "no";
692 $this->assertEquals( array( array( 'immobile-source-page' ) ),
693 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
694 $this->assertEquals( false,
695 $this->title->userCan( 'move', $this->user ) );
697 $this->setTitle( NS_MEDIA, "test page" );
698 $this->assertEquals( false,
699 $this->title->userCan( 'move-target', $this->user ) );
700 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
701 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
703 $this->setTitle( NS_HELP, "test page" );
704 $this->assertEquals( array(),
705 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
706 $this->assertEquals( true,
707 $this->title->userCan( 'move-target', $this->user ) );
709 $this->title->mInterwiki = "no";
710 $this->assertEquals( array( array( 'immobile-target-page' ) ),
711 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
712 $this->assertEquals( false,
713 $this->title->userCan( 'move-target', $this->user ) );
716 public function testUserBlock() {
717 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
718 $wgEmailConfirmToEdit = true;
719 $wgEmailAuthentication = true;
721 $this->setUserPerm( array( "createpage", "move" ) );
722 $this->setTitle( NS_HELP, "test page" );
724 # $short
725 $this->assertEquals( array( array( 'confirmedittext' ) ),
726 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
727 $wgEmailConfirmToEdit = false;
728 $this->assertEquals( true, $this->title->userCan( 'move-target', $this->user ) );
730 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
731 $this->assertEquals( array(),
732 $this->title->getUserPermissionsErrors( 'move-target',
733 $this->user ) );
735 global $wgLang;
736 $prev = time();
737 $now = time() + 120;
738 $this->user->mBlockedby = $this->user->getId();
739 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
740 'no reason given', $prev + 3600, 1, 0 );
741 $this->user->mBlock->mTimestamp = 0;
742 $this->assertEquals( array( array( 'autoblockedtext',
743 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
744 'Useruser', null, 'infinite', '127.0.8.1',
745 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
746 $this->title->getUserPermissionsErrors( 'move-target',
747 $this->user ) );
749 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
750 // quickUserCan should ignore user blocks
751 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
753 global $wgLocalTZoffset;
754 $wgLocalTZoffset = -60;
755 $this->user->mBlockedby = $this->user->getName();
756 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
757 'no reason given', $now, 0, 10 );
758 $this->assertEquals( array( array( 'blockedtext',
759 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
760 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
761 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
762 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
763 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
764 # $user->blockedFor() == ''
765 # $user->mBlock->mExpiry == 'infinity'