test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / TitlePermissionTest.php
blob6ae995e19c064e9af78f06933bb6006bdb544e55
1 <?php
3 /**
4 * @group Database
5 */
6 class TitlePermissionTest extends MediaWikiLangTestCase {
8 /**
9 * @var string
11 protected $userName, $altUserName;
13 /**
14 * @var Title
16 protected $title;
18 /**
19 * @var User
21 protected $user, $anonUser, $userUser, $altUser;
23 protected function setUp() {
24 parent::setUp();
26 $langObj = Language::factory( 'en' );
27 $localZone = 'UTC';
28 $localOffset = date( 'Z' ) / 60;
30 $this->setMwGlobals( array(
31 'wgMemc' => new EmptyBagOStuff,
32 'wgContLang' => $langObj,
33 'wgLanguageCode' => 'en',
34 'wgLang' => $langObj,
35 'wgLocaltimezone' => $localZone,
36 'wgLocalTZoffset' => $localOffset,
37 'wgNamespaceProtection' => array(
38 NS_MEDIAWIKI => 'editinterface',
40 ) );
42 $this->userName = 'Useruser';
43 $this->altUserName = 'Altuseruser';
44 date_default_timezone_set( $localZone );
46 $this->title = Title::makeTitle( NS_MAIN, "Main Page" );
47 if ( !isset( $this->userUser ) || !( $this->userUser instanceOf User ) ) {
48 $this->userUser = User::newFromName( $this->userName );
50 if ( !$this->userUser->getID() ) {
51 $this->userUser = User::createNew( $this->userName, array(
52 "email" => "test@example.com",
53 "real_name" => "Test User" ) );
54 $this->userUser->load();
57 $this->altUser = User::newFromName( $this->altUserName );
58 if ( !$this->altUser->getID() ) {
59 $this->altUser = User::createNew( $this->altUserName, array(
60 "email" => "alttest@example.com",
61 "real_name" => "Test User Alt" ) );
62 $this->altUser->load();
65 $this->anonUser = User::newFromId( 0 );
67 $this->user = $this->userUser;
71 function setUserPerm( $perm ) {
72 // Setting member variables is evil!!!
74 if ( is_array( $perm ) ) {
75 $this->user->mRights = $perm;
76 } else {
77 $this->user->mRights = array( $perm );
81 function setTitle( $ns, $title = "Main_Page" ) {
82 $this->title = Title::makeTitle( $ns, $title );
85 function setUser( $userName = null ) {
86 if ( $userName === 'anon' ) {
87 $this->user = $this->anonUser;
88 } elseif ( $userName === null || $userName === $this->userName ) {
89 $this->user = $this->userUser;
90 } else {
91 $this->user = $this->altUser;
95 function testQuickPermissions() {
96 global $wgContLang;
97 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
99 $this->setUser( 'anon' );
100 $this->setTitle( NS_TALK );
101 $this->setUserPerm( "createtalk" );
102 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
103 $this->assertEquals( array(), $res );
105 $this->setTitle( NS_TALK );
106 $this->setUserPerm( "createpage" );
107 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
108 $this->assertEquals( array( array( "nocreatetext" ) ), $res );
110 $this->setTitle( NS_TALK );
111 $this->setUserPerm( "" );
112 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
113 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
115 $this->setTitle( NS_MAIN );
116 $this->setUserPerm( "createpage" );
117 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
118 $this->assertEquals( array(), $res );
120 $this->setTitle( NS_MAIN );
121 $this->setUserPerm( "createtalk" );
122 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
123 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
125 $this->setUser( $this->userName );
126 $this->setTitle( NS_TALK );
127 $this->setUserPerm( "createtalk" );
128 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
129 $this->assertEquals( array(), $res );
131 $this->setTitle( NS_TALK );
132 $this->setUserPerm( "createpage" );
133 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
134 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
136 $this->setTitle( NS_TALK );
137 $this->setUserPerm( "" );
138 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
139 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
141 $this->setTitle( NS_MAIN );
142 $this->setUserPerm( "createpage" );
143 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
144 $this->assertEquals( array(), $res );
146 $this->setTitle( NS_MAIN );
147 $this->setUserPerm( "createtalk" );
148 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
149 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
151 $this->setTitle( NS_MAIN );
152 $this->setUserPerm( "" );
153 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
154 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
156 $this->setUser( 'anon' );
157 $this->setTitle( NS_USER, $this->userName . '' );
158 $this->setUserPerm( "" );
159 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
160 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
162 $this->setTitle( NS_USER, $this->userName . '/subpage' );
163 $this->setUserPerm( "" );
164 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
165 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
167 $this->setTitle( NS_USER, $this->userName . '' );
168 $this->setUserPerm( "move-rootuserpages" );
169 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
170 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
172 $this->setTitle( NS_USER, $this->userName . '/subpage' );
173 $this->setUserPerm( "move-rootuserpages" );
174 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
175 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
177 $this->setTitle( NS_USER, $this->userName . '' );
178 $this->setUserPerm( "" );
179 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
180 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
182 $this->setTitle( NS_USER, $this->userName . '/subpage' );
183 $this->setUserPerm( "" );
184 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
185 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
187 $this->setTitle( NS_USER, $this->userName . '' );
188 $this->setUserPerm( "move-rootuserpages" );
189 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
190 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
192 $this->setTitle( NS_USER, $this->userName . '/subpage' );
193 $this->setUserPerm( "move-rootuserpages" );
194 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
195 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
197 $this->setUser( $this->userName );
198 $this->setTitle( NS_FILE, "img.png" );
199 $this->setUserPerm( "" );
200 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
201 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ), $res );
203 $this->setTitle( NS_FILE, "img.png" );
204 $this->setUserPerm( "movefile" );
205 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
206 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
208 $this->setUser( 'anon' );
209 $this->setTitle( NS_FILE, "img.png" );
210 $this->setUserPerm( "" );
211 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
212 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ), $res );
214 $this->setTitle( NS_FILE, "img.png" );
215 $this->setUserPerm( "movefile" );
216 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
217 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
219 $this->setUser( $this->userName );
220 $this->setUserPerm( "move" );
221 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
223 $this->setUserPerm( "" );
224 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ) );
226 $this->setUser( 'anon' );
227 $this->setUserPerm( "move" );
228 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
230 $this->setUserPerm( "" );
231 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
232 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ) );
234 if ( $this->isWikitextNS( NS_MAIN ) ) {
235 //NOTE: some content models don't allow moving
236 // @todo find a Wikitext namespace for testing
238 $this->setTitle( NS_MAIN );
239 $this->setUser( 'anon' );
240 $this->setUserPerm( "move" );
241 $this->runGroupPermissions( 'move', array() );
243 $this->setUserPerm( "" );
244 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
245 array( array( 'movenologintext' ) ) );
247 $this->setUser( $this->userName );
248 $this->setUserPerm( "" );
249 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
251 $this->setUserPerm( "move" );
252 $this->runGroupPermissions( 'move', array() );
254 $this->setUser( 'anon' );
255 $this->setUserPerm( 'move' );
256 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
257 $this->assertEquals( array(), $res );
259 $this->setUserPerm( '' );
260 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
261 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
264 $this->setTitle( NS_USER );
265 $this->setUser( $this->userName );
266 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
267 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
268 $this->assertEquals( array(), $res );
270 $this->setUserPerm( "move" );
271 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
272 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
274 $this->setUser( 'anon' );
275 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
276 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
277 $this->assertEquals( array(), $res );
279 $this->setTitle( NS_USER, "User/subpage" );
280 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
281 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
282 $this->assertEquals( array(), $res );
284 $this->setUserPerm( "move" );
285 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
286 $this->assertEquals( array(), $res );
288 $this->setUser( 'anon' );
289 $check = array( 'edit' => array( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ) ),
290 array( array( 'badaccess-group0' ) ),
291 array(), true ),
292 'protect' => array( array( array( 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ), array( 'protect-cantedit' ) ),
293 array( array( 'badaccess-group0' ), array( 'protect-cantedit' ) ),
294 array( array( 'protect-cantedit' ) ), false ),
295 '' => array( array(), array(), array(), true ) );
297 foreach ( array( "edit", "protect", "" ) as $action ) {
298 $this->setUserPerm( null );
299 $this->assertEquals( $check[$action][0],
300 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
302 global $wgGroupPermissions;
303 $old = $wgGroupPermissions;
304 $wgGroupPermissions = array();
306 $this->assertEquals( $check[$action][1],
307 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
308 $wgGroupPermissions = $old;
310 $this->setUserPerm( $action );
311 $this->assertEquals( $check[$action][2],
312 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
314 $this->setUserPerm( $action );
315 $this->assertEquals( $check[$action][3],
316 $this->title->userCan( $action, $this->user, true ) );
317 $this->assertEquals( $check[$action][3],
318 $this->title->quickUserCan( $action, $this->user ) );
319 # count( User::getGroupsWithPermissions( $action ) ) < 1
323 function runGroupPermissions( $action, $result, $result2 = null ) {
324 global $wgGroupPermissions;
326 if ( $result2 === null ) {
327 $result2 = $result;
330 $wgGroupPermissions['autoconfirmed']['move'] = false;
331 $wgGroupPermissions['user']['move'] = false;
332 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
333 $this->assertEquals( $result, $res );
335 $wgGroupPermissions['autoconfirmed']['move'] = true;
336 $wgGroupPermissions['user']['move'] = false;
337 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
338 $this->assertEquals( $result2, $res );
340 $wgGroupPermissions['autoconfirmed']['move'] = true;
341 $wgGroupPermissions['user']['move'] = true;
342 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
343 $this->assertEquals( $result2, $res );
345 $wgGroupPermissions['autoconfirmed']['move'] = false;
346 $wgGroupPermissions['user']['move'] = true;
347 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
348 $this->assertEquals( $result2, $res );
351 function testSpecialsAndNSPermissions() {
352 global $wgNamespaceProtection;
353 $this->setUser( $this->userName );
355 $this->setTitle( NS_SPECIAL );
357 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
358 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
360 $this->setTitle( NS_MAIN );
361 $this->setUserPerm( 'bogus' );
362 $this->assertEquals( array(),
363 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
365 $this->setTitle( NS_MAIN );
366 $this->setUserPerm( '' );
367 $this->assertEquals( array( array( 'badaccess-group0' ) ),
368 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
370 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
372 $this->setTitle( NS_USER );
373 $this->setUserPerm( '' );
374 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
375 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
377 $this->setTitle( NS_MEDIAWIKI );
378 $this->setUserPerm( 'bogus' );
379 $this->assertEquals( array( array( 'protectedinterface' ) ),
380 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
382 $this->setTitle( NS_MEDIAWIKI );
383 $this->setUserPerm( 'bogus' );
384 $this->assertEquals( array( array( 'protectedinterface' ) ),
385 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
387 $wgNamespaceProtection = null;
389 $this->setUserPerm( 'bogus' );
390 $this->assertEquals( array(),
391 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
392 $this->assertEquals( true,
393 $this->title->userCan( 'bogus', $this->user ) );
395 $this->setUserPerm( '' );
396 $this->assertEquals( array( array( 'badaccess-group0' ) ),
397 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
398 $this->assertEquals( false,
399 $this->title->userCan( 'bogus', $this->user ) );
402 function testCssAndJavascriptPermissions() {
403 $this->setUser( $this->userName );
405 $this->setTitle( NS_USER, $this->userName . '/test.js' );
406 $this->runCSSandJSPermissions(
407 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
408 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
409 array( array( 'badaccess-group0' ) ),
410 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
411 array( array( 'badaccess-group0' ) )
414 $this->setTitle( NS_USER, $this->userName . '/test.css' );
415 $this->runCSSandJSPermissions(
416 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
417 array( array( 'badaccess-group0' ) ),
418 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
419 array( array( 'badaccess-group0' ) ),
420 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) )
423 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
424 $this->runCSSandJSPermissions(
425 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
426 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
427 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
428 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
429 array( array( 'badaccess-group0' ) )
432 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
433 $this->runCSSandJSPermissions(
434 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
435 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
436 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
437 array( array( 'badaccess-group0' ) ),
438 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) )
441 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
442 $this->runCSSandJSPermissions(
443 array( array( 'badaccess-group0' ) ),
444 array( array( 'badaccess-group0' ) ),
445 array( array( 'badaccess-group0' ) ),
446 array( array( 'badaccess-group0' ) ),
447 array( array( 'badaccess-group0' ) )
451 function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
452 $this->setUserPerm( '' );
453 $this->assertEquals( $result0,
454 $this->title->getUserPermissionsErrors( 'bogus',
455 $this->user ) );
457 $this->setUserPerm( 'editmyusercss' );
458 $this->assertEquals( $result1,
459 $this->title->getUserPermissionsErrors( 'bogus',
460 $this->user ) );
462 $this->setUserPerm( 'editmyuserjs' );
463 $this->assertEquals( $result2,
464 $this->title->getUserPermissionsErrors( 'bogus',
465 $this->user ) );
467 $this->setUserPerm( 'editusercss' );
468 $this->assertEquals( $result3,
469 $this->title->getUserPermissionsErrors( 'bogus',
470 $this->user ) );
472 $this->setUserPerm( 'edituserjs' );
473 $this->assertEquals( $result4,
474 $this->title->getUserPermissionsErrors( 'bogus',
475 $this->user ) );
477 $this->setUserPerm( 'editusercssjs' );
478 $this->assertEquals( array( array( 'badaccess-group0' ) ),
479 $this->title->getUserPermissionsErrors( 'bogus',
480 $this->user ) );
482 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
483 $this->assertEquals( array( array( 'badaccess-group0' ) ),
484 $this->title->getUserPermissionsErrors( 'bogus',
485 $this->user ) );
488 function testPageRestrictions() {
489 global $wgContLang;
491 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
493 $this->setTitle( NS_MAIN );
494 $this->title->mRestrictionsLoaded = true;
495 $this->setUserPerm( "edit" );
496 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
498 $this->assertEquals( array(),
499 $this->title->getUserPermissionsErrors( 'edit',
500 $this->user ) );
502 $this->assertEquals( true,
503 $this->title->quickUserCan( 'edit', $this->user ) );
504 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
505 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
507 $this->assertEquals( array( array( 'badaccess-group0' ),
508 array( 'protectedpagetext', 'bogus' ),
509 array( 'protectedpagetext', 'protect' ),
510 array( 'protectedpagetext', 'protect' ) ),
511 $this->title->getUserPermissionsErrors( 'bogus',
512 $this->user ) );
513 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
514 array( 'protectedpagetext', 'protect' ),
515 array( 'protectedpagetext', 'protect' ) ),
516 $this->title->getUserPermissionsErrors( 'edit',
517 $this->user ) );
518 $this->setUserPerm( "" );
519 $this->assertEquals( array( array( 'badaccess-group0' ),
520 array( 'protectedpagetext', 'bogus' ),
521 array( 'protectedpagetext', 'protect' ),
522 array( 'protectedpagetext', 'protect' ) ),
523 $this->title->getUserPermissionsErrors( 'bogus',
524 $this->user ) );
525 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
526 array( 'protectedpagetext', 'bogus' ),
527 array( 'protectedpagetext', 'protect' ),
528 array( 'protectedpagetext', 'protect' ) ),
529 $this->title->getUserPermissionsErrors( 'edit',
530 $this->user ) );
531 $this->setUserPerm( array( "edit", "editprotected" ) );
532 $this->assertEquals( array( array( 'badaccess-group0' ),
533 array( 'protectedpagetext', 'bogus' ),
534 array( 'protectedpagetext', 'protect' ),
535 array( 'protectedpagetext', 'protect' ) ),
536 $this->title->getUserPermissionsErrors( 'bogus',
537 $this->user ) );
538 $this->assertEquals( array(),
539 $this->title->getUserPermissionsErrors( 'edit',
540 $this->user ) );
541 $this->title->mCascadeRestriction = true;
542 $this->assertEquals( false,
543 $this->title->quickUserCan( 'bogus', $this->user ) );
544 $this->assertEquals( false,
545 $this->title->quickUserCan( 'edit', $this->user ) );
546 $this->assertEquals( array( array( 'badaccess-group0' ),
547 array( 'protectedpagetext', 'bogus' ),
548 array( 'protectedpagetext', 'protect' ),
549 array( 'protectedpagetext', 'protect' ) ),
550 $this->title->getUserPermissionsErrors( 'bogus',
551 $this->user ) );
552 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
553 array( 'protectedpagetext', 'protect' ),
554 array( 'protectedpagetext', 'protect' ) ),
555 $this->title->getUserPermissionsErrors( 'edit',
556 $this->user ) );
559 function testCascadingSourcesRestrictions() {
560 $this->setTitle( NS_MAIN, "test page" );
561 $this->setUserPerm( array( "edit", "bogus" ) );
563 $this->title->mCascadeSources = array( Title::makeTitle( NS_MAIN, "Bogus" ), Title::makeTitle( NS_MAIN, "UnBogus" ) );
564 $this->title->mCascadingRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
566 $this->assertEquals( false,
567 $this->title->userCan( 'bogus', $this->user ) );
568 $this->assertEquals( array( array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
569 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ) ),
570 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
572 $this->assertEquals( true,
573 $this->title->userCan( 'edit', $this->user ) );
574 $this->assertEquals( array(),
575 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
578 function testActionPermissions() {
579 $this->setUserPerm( array( "createpage" ) );
580 $this->setTitle( NS_MAIN, "test page" );
581 $this->title->mTitleProtection['pt_create_perm'] = '';
582 $this->title->mTitleProtection['pt_user'] = $this->user->getID();
583 $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
584 $this->title->mTitleProtection['pt_reason'] = 'test';
585 $this->title->mCascadeRestriction = false;
587 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
588 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
589 $this->assertEquals( false,
590 $this->title->userCan( 'create', $this->user ) );
592 $this->title->mTitleProtection['pt_create_perm'] = 'sysop';
593 $this->setUserPerm( array( 'createpage', 'protect' ) );
594 $this->assertEquals( array(),
595 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
596 $this->assertEquals( true,
597 $this->title->userCan( 'create', $this->user ) );
599 $this->setUserPerm( array( 'createpage' ) );
600 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
601 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
602 $this->assertEquals( false,
603 $this->title->userCan( 'create', $this->user ) );
605 $this->setTitle( NS_MEDIA, "test page" );
606 $this->setUserPerm( array( "move" ) );
607 $this->assertEquals( false,
608 $this->title->userCan( 'move', $this->user ) );
609 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
610 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
612 $this->setTitle( NS_HELP, "test page" );
613 $this->assertEquals( array(),
614 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
615 $this->assertEquals( true,
616 $this->title->userCan( 'move', $this->user ) );
618 $this->title->mInterwiki = "no";
619 $this->assertEquals( array( array( 'immobile-source-page' ) ),
620 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
621 $this->assertEquals( false,
622 $this->title->userCan( 'move', $this->user ) );
624 $this->setTitle( NS_MEDIA, "test page" );
625 $this->assertEquals( false,
626 $this->title->userCan( 'move-target', $this->user ) );
627 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
628 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
630 $this->setTitle( NS_HELP, "test page" );
631 $this->assertEquals( array(),
632 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
633 $this->assertEquals( true,
634 $this->title->userCan( 'move-target', $this->user ) );
636 $this->title->mInterwiki = "no";
637 $this->assertEquals( array( array( 'immobile-target-page' ) ),
638 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
639 $this->assertEquals( false,
640 $this->title->userCan( 'move-target', $this->user ) );
643 function testUserBlock() {
644 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
645 $wgEmailConfirmToEdit = true;
646 $wgEmailAuthentication = true;
648 $this->setUserPerm( array( "createpage", "move" ) );
649 $this->setTitle( NS_HELP, "test page" );
651 # $short
652 $this->assertEquals( array( array( 'confirmedittext' ) ),
653 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
654 $wgEmailConfirmToEdit = false;
655 $this->assertEquals( true, $this->title->userCan( 'move-target', $this->user ) );
657 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
658 $this->assertEquals( array(),
659 $this->title->getUserPermissionsErrors( 'move-target',
660 $this->user ) );
662 global $wgLang;
663 $prev = time();
664 $now = time() + 120;
665 $this->user->mBlockedby = $this->user->getId();
666 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
667 'no reason given', $prev + 3600, 1, 0 );
668 $this->user->mBlock->mTimestamp = 0;
669 $this->assertEquals( array( array( 'autoblockedtext',
670 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
671 'Useruser', null, 'infinite', '127.0.8.1',
672 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
673 $this->title->getUserPermissionsErrors( 'move-target',
674 $this->user ) );
676 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
677 // quickUserCan should ignore user blocks
678 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
680 global $wgLocalTZoffset;
681 $wgLocalTZoffset = -60;
682 $this->user->mBlockedby = $this->user->getName();
683 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
684 'no reason given', $now, 0, 10 );
685 $this->assertEquals( array( array( 'blockedtext',
686 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
687 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
688 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
689 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
690 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
691 # $user->blockedFor() == ''
692 # $user->mBlock->mExpiry == 'infinity'