Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / tests / phpunit / includes / TitlePermissionTest.php
1 <?php
2
3 /**
4 * @group Database
5 *
6 * @covers Title::getUserPermissionsErrors
7 * @covers Title::getUserPermissionsErrorsInternal
8 */
9 class TitlePermissionTest extends MediaWikiLangTestCase {
10
11 /**
12 * @var string
13 */
14 protected $userName, $altUserName;
15
16 /**
17 * @var Title
18 */
19 protected $title;
20
21 /**
22 * @var User
23 */
24 protected $user, $anonUser, $userUser, $altUser;
25
26 protected function setUp() {
27 parent::setUp();
28
29 $langObj = Language::factory( 'en' );
30 $localZone = 'UTC';
31 $localOffset = date( 'Z' ) / 60;
32
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',
42 ),
43 ) );
44 // Without this testUserBlock will use a non-English context on non-English MediaWiki
45 // installations (because of how Title::checkUserBlock is implemented) and fail.
46 RequestContext::resetMain();
47
48 $this->userName = 'Useruser';
49 $this->altUserName = 'Altuseruser';
50 date_default_timezone_set( $localZone );
51
52 $this->title = Title::makeTitle( NS_MAIN, "Main Page" );
53 if ( !isset( $this->userUser ) || !( $this->userUser instanceof User ) ) {
54 $this->userUser = User::newFromName( $this->userName );
55
56 if ( !$this->userUser->getID() ) {
57 $this->userUser = User::createNew( $this->userName, array(
58 "email" => "test@example.com",
59 "real_name" => "Test User" ) );
60 $this->userUser->load();
61 }
62
63 $this->altUser = User::newFromName( $this->altUserName );
64 if ( !$this->altUser->getID() ) {
65 $this->altUser = User::createNew( $this->altUserName, array(
66 "email" => "alttest@example.com",
67 "real_name" => "Test User Alt" ) );
68 $this->altUser->load();
69 }
70
71 $this->anonUser = User::newFromId( 0 );
72
73 $this->user = $this->userUser;
74 }
75 }
76
77 protected function setUserPerm( $perm ) {
78 // Setting member variables is evil!!!
79
80 if ( is_array( $perm ) ) {
81 $this->user->mRights = $perm;
82 } else {
83 $this->user->mRights = array( $perm );
84 }
85 }
86
87 protected function setTitle( $ns, $title = "Main_Page" ) {
88 $this->title = Title::makeTitle( $ns, $title );
89 }
90
91 protected function setUser( $userName = null ) {
92 if ( $userName === 'anon' ) {
93 $this->user = $this->anonUser;
94 } elseif ( $userName === null || $userName === $this->userName ) {
95 $this->user = $this->userUser;
96 } else {
97 $this->user = $this->altUser;
98 }
99 }
100
101 /**
102 * @todo This test method should be split up into separate test methods and
103 * data providers
104 */
105 public function testQuickPermissions() {
106 global $wgContLang;
107 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
108
109 $this->setUser( 'anon' );
110 $this->setTitle( NS_TALK );
111 $this->setUserPerm( "createtalk" );
112 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
113 $this->assertEquals( array(), $res );
114
115 $this->setTitle( NS_TALK );
116 $this->setUserPerm( "createpage" );
117 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
118 $this->assertEquals( array( array( "nocreatetext" ) ), $res );
119
120 $this->setTitle( NS_TALK );
121 $this->setUserPerm( "" );
122 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
123 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
124
125 $this->setTitle( NS_MAIN );
126 $this->setUserPerm( "createpage" );
127 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
128 $this->assertEquals( array(), $res );
129
130 $this->setTitle( NS_MAIN );
131 $this->setUserPerm( "createtalk" );
132 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
133 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
134
135 $this->setUser( $this->userName );
136 $this->setTitle( NS_TALK );
137 $this->setUserPerm( "createtalk" );
138 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
139 $this->assertEquals( array(), $res );
140
141 $this->setTitle( NS_TALK );
142 $this->setUserPerm( "createpage" );
143 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
144 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
145
146 $this->setTitle( NS_TALK );
147 $this->setUserPerm( "" );
148 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
149 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
150
151 $this->setTitle( NS_MAIN );
152 $this->setUserPerm( "createpage" );
153 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
154 $this->assertEquals( array(), $res );
155
156 $this->setTitle( NS_MAIN );
157 $this->setUserPerm( "createtalk" );
158 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
159 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
160
161 $this->setTitle( NS_MAIN );
162 $this->setUserPerm( "" );
163 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
164 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
165
166 $this->setUser( 'anon' );
167 $this->setTitle( NS_USER, $this->userName . '' );
168 $this->setUserPerm( "" );
169 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
170 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
171
172 $this->setTitle( NS_USER, $this->userName . '/subpage' );
173 $this->setUserPerm( "" );
174 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
175 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
176
177 $this->setTitle( NS_USER, $this->userName . '' );
178 $this->setUserPerm( "move-rootuserpages" );
179 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
180 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
181
182 $this->setTitle( NS_USER, $this->userName . '/subpage' );
183 $this->setUserPerm( "move-rootuserpages" );
184 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
185 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
186
187 $this->setTitle( NS_USER, $this->userName . '' );
188 $this->setUserPerm( "" );
189 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
190 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
191
192 $this->setTitle( NS_USER, $this->userName . '/subpage' );
193 $this->setUserPerm( "" );
194 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
195 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
196
197 $this->setTitle( NS_USER, $this->userName . '' );
198 $this->setUserPerm( "move-rootuserpages" );
199 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
200 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
201
202 $this->setTitle( NS_USER, $this->userName . '/subpage' );
203 $this->setUserPerm( "move-rootuserpages" );
204 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
205 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
206
207 $this->setUser( $this->userName );
208 $this->setTitle( NS_FILE, "img.png" );
209 $this->setUserPerm( "" );
210 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
211 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ), $res );
212
213 $this->setTitle( NS_FILE, "img.png" );
214 $this->setUserPerm( "movefile" );
215 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
216 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
217
218 $this->setUser( 'anon' );
219 $this->setTitle( NS_FILE, "img.png" );
220 $this->setUserPerm( "" );
221 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
222 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ), $res );
223
224 $this->setTitle( NS_FILE, "img.png" );
225 $this->setUserPerm( "movefile" );
226 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
227 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
228
229 $this->setUser( $this->userName );
230 $this->setUserPerm( "move" );
231 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
232
233 $this->setUserPerm( "" );
234 $this->runGroupPermissions(
235 'move',
236 array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) )
237 );
238
239 $this->setUser( 'anon' );
240 $this->setUserPerm( "move" );
241 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
242
243 $this->setUserPerm( "" );
244 $this->runGroupPermissions(
245 'move',
246 array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
247 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) )
248 );
249
250 if ( $this->isWikitextNS( NS_MAIN ) ) {
251 //NOTE: some content models don't allow moving
252 // @todo find a Wikitext namespace for testing
253
254 $this->setTitle( NS_MAIN );
255 $this->setUser( 'anon' );
256 $this->setUserPerm( "move" );
257 $this->runGroupPermissions( 'move', array() );
258
259 $this->setUserPerm( "" );
260 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
261 array( array( 'movenologintext' ) ) );
262
263 $this->setUser( $this->userName );
264 $this->setUserPerm( "" );
265 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
266
267 $this->setUserPerm( "move" );
268 $this->runGroupPermissions( 'move', array() );
269
270 $this->setUser( 'anon' );
271 $this->setUserPerm( 'move' );
272 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
273 $this->assertEquals( array(), $res );
274
275 $this->setUserPerm( '' );
276 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
277 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
278 }
279
280 $this->setTitle( NS_USER );
281 $this->setUser( $this->userName );
282 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
283 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
284 $this->assertEquals( array(), $res );
285
286 $this->setUserPerm( "move" );
287 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
288 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
289
290 $this->setUser( 'anon' );
291 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
292 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
293 $this->assertEquals( array(), $res );
294
295 $this->setTitle( NS_USER, "User/subpage" );
296 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
297 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
298 $this->assertEquals( array(), $res );
299
300 $this->setUserPerm( "move" );
301 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
302 $this->assertEquals( array(), $res );
303
304 $this->setUser( 'anon' );
305 $check = array(
306 'edit' => array(
307 array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ) ),
308 array( array( 'badaccess-group0' ) ),
309 array(),
310 true
311 ),
312 'protect' => array(
313 array( array(
314 'badaccess-groups',
315 "[[$prefix:Administrators|Administrators]]", 1 ),
316 array( 'protect-cantedit'
317 ) ),
318 array( array( 'badaccess-group0' ), array( 'protect-cantedit' ) ),
319 array( array( 'protect-cantedit' ) ),
320 false
321 ),
322 '' => array( array(), array(), array(), true )
323 );
324
325 foreach ( array( "edit", "protect", "" ) as $action ) {
326 $this->setUserPerm( null );
327 $this->assertEquals( $check[$action][0],
328 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
329 $this->assertEquals( $check[$action][0],
330 $this->title->getUserPermissionsErrors( $action, $this->user, 'full' ) );
331 $this->assertEquals( $check[$action][0],
332 $this->title->getUserPermissionsErrors( $action, $this->user, 'secure' ) );
333
334 global $wgGroupPermissions;
335 $old = $wgGroupPermissions;
336 $wgGroupPermissions = array();
337
338 $this->assertEquals( $check[$action][1],
339 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
340 $this->assertEquals( $check[$action][1],
341 $this->title->getUserPermissionsErrors( $action, $this->user, 'full' ) );
342 $this->assertEquals( $check[$action][1],
343 $this->title->getUserPermissionsErrors( $action, $this->user, 'secure' ) );
344 $wgGroupPermissions = $old;
345
346 $this->setUserPerm( $action );
347 $this->assertEquals( $check[$action][2],
348 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
349 $this->assertEquals( $check[$action][2],
350 $this->title->getUserPermissionsErrors( $action, $this->user, 'full' ) );
351 $this->assertEquals( $check[$action][2],
352 $this->title->getUserPermissionsErrors( $action, $this->user, 'secure' ) );
353
354 $this->setUserPerm( $action );
355 $this->assertEquals( $check[$action][3],
356 $this->title->userCan( $action, $this->user, true ) );
357 $this->assertEquals( $check[$action][3],
358 $this->title->quickUserCan( $action, $this->user ) );
359 # count( User::getGroupsWithPermissions( $action ) ) < 1
360 }
361 }
362
363 protected function runGroupPermissions( $action, $result, $result2 = null ) {
364 global $wgGroupPermissions;
365
366 if ( $result2 === null ) {
367 $result2 = $result;
368 }
369
370 $wgGroupPermissions['autoconfirmed']['move'] = false;
371 $wgGroupPermissions['user']['move'] = false;
372 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
373 $this->assertEquals( $result, $res );
374
375 $wgGroupPermissions['autoconfirmed']['move'] = true;
376 $wgGroupPermissions['user']['move'] = false;
377 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
378 $this->assertEquals( $result2, $res );
379
380 $wgGroupPermissions['autoconfirmed']['move'] = true;
381 $wgGroupPermissions['user']['move'] = true;
382 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
383 $this->assertEquals( $result2, $res );
384
385 $wgGroupPermissions['autoconfirmed']['move'] = false;
386 $wgGroupPermissions['user']['move'] = true;
387 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
388 $this->assertEquals( $result2, $res );
389 }
390
391 /**
392 * @todo This test method should be split up into separate test methods and
393 * data providers
394 */
395 public function testSpecialsAndNSPermissions() {
396 global $wgNamespaceProtection;
397 $this->setUser( $this->userName );
398
399 $this->setTitle( NS_SPECIAL );
400
401 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
402 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
403
404 $this->setTitle( NS_MAIN );
405 $this->setUserPerm( 'bogus' );
406 $this->assertEquals( array(),
407 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
408
409 $this->setTitle( NS_MAIN );
410 $this->setUserPerm( '' );
411 $this->assertEquals( array( array( 'badaccess-group0' ) ),
412 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
413
414 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
415
416 $this->setTitle( NS_USER );
417 $this->setUserPerm( '' );
418 $this->assertEquals( array( array( 'badaccess-group0' ),
419 array( 'namespaceprotected', 'User', 'bogus' ) ),
420 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
421
422 $this->setTitle( NS_MEDIAWIKI );
423 $this->setUserPerm( 'bogus' );
424 $this->assertEquals( array( array( 'protectedinterface', 'bogus' ) ),
425 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
426
427 $this->setTitle( NS_MEDIAWIKI );
428 $this->setUserPerm( 'bogus' );
429 $this->assertEquals( array( array( 'protectedinterface', 'bogus' ) ),
430 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
431
432 $wgNamespaceProtection = null;
433
434 $this->setUserPerm( 'bogus' );
435 $this->assertEquals( array(),
436 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
437 $this->assertEquals( true,
438 $this->title->userCan( 'bogus', $this->user ) );
439
440 $this->setUserPerm( '' );
441 $this->assertEquals( array( array( 'badaccess-group0' ) ),
442 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
443 $this->assertEquals( false,
444 $this->title->userCan( 'bogus', $this->user ) );
445 }
446
447 /**
448 * @todo This test method should be split up into separate test methods and
449 * data providers
450 */
451 public function testCssAndJavascriptPermissions() {
452 $this->setUser( $this->userName );
453
454 $this->setTitle( NS_USER, $this->userName . '/test.js' );
455 $this->runCSSandJSPermissions(
456 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected', 'bogus' ) ),
457 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected', 'bogus' ) ),
458 array( array( 'badaccess-group0' ) ),
459 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected', 'bogus' ) ),
460 array( array( 'badaccess-group0' ) )
461 );
462
463 $this->setTitle( NS_USER, $this->userName . '/test.css' );
464 $this->runCSSandJSPermissions(
465 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected', 'bogus' ) ),
466 array( array( 'badaccess-group0' ) ),
467 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected', 'bogus' ) ),
468 array( array( 'badaccess-group0' ) ),
469 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected', 'bogus' ) )
470 );
471
472 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
473 $this->runCSSandJSPermissions(
474 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
475 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
476 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
477 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
478 array( array( 'badaccess-group0' ) )
479 );
480
481 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
482 $this->runCSSandJSPermissions(
483 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) ),
484 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) ),
485 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) ),
486 array( array( 'badaccess-group0' ) ),
487 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) )
488 );
489
490 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
491 $this->runCSSandJSPermissions(
492 array( array( 'badaccess-group0' ) ),
493 array( array( 'badaccess-group0' ) ),
494 array( array( 'badaccess-group0' ) ),
495 array( array( 'badaccess-group0' ) ),
496 array( array( 'badaccess-group0' ) )
497 );
498 }
499
500 protected function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
501 $this->setUserPerm( '' );
502 $this->assertEquals( $result0,
503 $this->title->getUserPermissionsErrors( 'bogus',
504 $this->user ) );
505
506 $this->setUserPerm( 'editmyusercss' );
507 $this->assertEquals( $result1,
508 $this->title->getUserPermissionsErrors( 'bogus',
509 $this->user ) );
510
511 $this->setUserPerm( 'editmyuserjs' );
512 $this->assertEquals( $result2,
513 $this->title->getUserPermissionsErrors( 'bogus',
514 $this->user ) );
515
516 $this->setUserPerm( 'editusercss' );
517 $this->assertEquals( $result3,
518 $this->title->getUserPermissionsErrors( 'bogus',
519 $this->user ) );
520
521 $this->setUserPerm( 'edituserjs' );
522 $this->assertEquals( $result4,
523 $this->title->getUserPermissionsErrors( 'bogus',
524 $this->user ) );
525
526 $this->setUserPerm( 'editusercssjs' );
527 $this->assertEquals( array( array( 'badaccess-group0' ) ),
528 $this->title->getUserPermissionsErrors( 'bogus',
529 $this->user ) );
530
531 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
532 $this->assertEquals( array( array( 'badaccess-group0' ) ),
533 $this->title->getUserPermissionsErrors( 'bogus',
534 $this->user ) );
535 }
536
537 /**
538 * @todo This test method should be split up into separate test methods and
539 * data providers
540 */
541 public function testPageRestrictions() {
542 global $wgContLang;
543
544 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
545
546 $this->setTitle( NS_MAIN );
547 $this->title->mRestrictionsLoaded = true;
548 $this->setUserPerm( "edit" );
549 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
550
551 $this->assertEquals( array(),
552 $this->title->getUserPermissionsErrors( 'edit',
553 $this->user ) );
554
555 $this->assertEquals( true,
556 $this->title->quickUserCan( 'edit', $this->user ) );
557 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
558 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
559
560 $this->assertEquals( array( array( 'badaccess-group0' ),
561 array( 'protectedpagetext', 'bogus', 'bogus' ),
562 array( 'protectedpagetext', 'editprotected', 'bogus' ),
563 array( 'protectedpagetext', 'protect', 'bogus' ) ),
564 $this->title->getUserPermissionsErrors( 'bogus',
565 $this->user ) );
566 $this->assertEquals( array( array( 'protectedpagetext', 'bogus', 'edit' ),
567 array( 'protectedpagetext', 'editprotected', 'edit' ),
568 array( 'protectedpagetext', 'protect', 'edit' ) ),
569 $this->title->getUserPermissionsErrors( 'edit',
570 $this->user ) );
571 $this->setUserPerm( "" );
572 $this->assertEquals( array( array( 'badaccess-group0' ),
573 array( 'protectedpagetext', 'bogus', 'bogus' ),
574 array( 'protectedpagetext', 'editprotected', 'bogus' ),
575 array( 'protectedpagetext', 'protect', 'bogus' ) ),
576 $this->title->getUserPermissionsErrors( 'bogus',
577 $this->user ) );
578 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
579 array( 'protectedpagetext', 'bogus', 'edit' ),
580 array( 'protectedpagetext', 'editprotected', 'edit' ),
581 array( 'protectedpagetext', 'protect', 'edit' ) ),
582 $this->title->getUserPermissionsErrors( 'edit',
583 $this->user ) );
584 $this->setUserPerm( array( "edit", "editprotected" ) );
585 $this->assertEquals( array( array( 'badaccess-group0' ),
586 array( 'protectedpagetext', 'bogus', 'bogus' ),
587 array( 'protectedpagetext', 'protect', 'bogus' ) ),
588 $this->title->getUserPermissionsErrors( 'bogus',
589 $this->user ) );
590 $this->assertEquals( array(
591 array( 'protectedpagetext', 'bogus', 'edit' ),
592 array( 'protectedpagetext', 'protect', 'edit' ) ),
593 $this->title->getUserPermissionsErrors( 'edit',
594 $this->user ) );
595
596 $this->title->mCascadeRestriction = true;
597 $this->setUserPerm( "edit" );
598 $this->assertEquals( false,
599 $this->title->quickUserCan( 'bogus', $this->user ) );
600 $this->assertEquals( false,
601 $this->title->quickUserCan( 'edit', $this->user ) );
602 $this->assertEquals( array( array( 'badaccess-group0' ),
603 array( 'protectedpagetext', 'bogus', 'bogus' ),
604 array( 'protectedpagetext', 'editprotected', 'bogus' ),
605 array( 'protectedpagetext', 'protect', 'bogus' ) ),
606 $this->title->getUserPermissionsErrors( 'bogus',
607 $this->user ) );
608 $this->assertEquals( array( array( 'protectedpagetext', 'bogus', 'edit' ),
609 array( 'protectedpagetext', 'editprotected', 'edit' ),
610 array( 'protectedpagetext', 'protect', 'edit' ) ),
611 $this->title->getUserPermissionsErrors( 'edit',
612 $this->user ) );
613
614 $this->setUserPerm( array( "edit", "editprotected" ) );
615 $this->assertEquals( false,
616 $this->title->quickUserCan( 'bogus', $this->user ) );
617 $this->assertEquals( false,
618 $this->title->quickUserCan( 'edit', $this->user ) );
619 $this->assertEquals( array( array( 'badaccess-group0' ),
620 array( 'protectedpagetext', 'bogus', 'bogus' ),
621 array( 'protectedpagetext', 'protect', 'bogus' ),
622 array( 'protectedpagetext', 'protect', 'bogus' ) ),
623 $this->title->getUserPermissionsErrors( 'bogus',
624 $this->user ) );
625 $this->assertEquals( array( array( 'protectedpagetext', 'bogus', 'edit' ),
626 array( 'protectedpagetext', 'protect', 'edit' ),
627 array( 'protectedpagetext', 'protect', 'edit' ) ),
628 $this->title->getUserPermissionsErrors( 'edit',
629 $this->user ) );
630 }
631
632 public function testCascadingSourcesRestrictions() {
633 $this->setTitle( NS_MAIN, "test page" );
634 $this->setUserPerm( array( "edit", "bogus" ) );
635
636 $this->title->mCascadeSources = array(
637 Title::makeTitle( NS_MAIN, "Bogus" ),
638 Title::makeTitle( NS_MAIN, "UnBogus" )
639 );
640 $this->title->mCascadingRestrictions = array(
641 "bogus" => array( 'bogus', "sysop", "protect", "" )
642 );
643
644 $this->assertEquals( false,
645 $this->title->userCan( 'bogus', $this->user ) );
646 $this->assertEquals( array(
647 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ),
648 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ),
649 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ) ),
650 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
651
652 $this->assertEquals( true,
653 $this->title->userCan( 'edit', $this->user ) );
654 $this->assertEquals( array(),
655 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
656 }
657
658 /**
659 * @todo This test method should be split up into separate test methods and
660 * data providers
661 */
662 public function testActionPermissions() {
663 $this->setUserPerm( array( "createpage" ) );
664 $this->setTitle( NS_MAIN, "test page" );
665 $this->title->mTitleProtection['permission'] = '';
666 $this->title->mTitleProtection['user'] = $this->user->getID();
667 $this->title->mTitleProtection['expiry'] = 'infinity';
668 $this->title->mTitleProtection['reason'] = 'test';
669 $this->title->mCascadeRestriction = false;
670
671 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
672 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
673 $this->assertEquals( false,
674 $this->title->userCan( 'create', $this->user ) );
675
676 $this->title->mTitleProtection['permission'] = 'editprotected';
677 $this->setUserPerm( array( 'createpage', 'protect' ) );
678 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
679 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
680 $this->assertEquals( false,
681 $this->title->userCan( 'create', $this->user ) );
682
683 $this->setUserPerm( array( 'createpage', 'editprotected' ) );
684 $this->assertEquals( array(),
685 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
686 $this->assertEquals( true,
687 $this->title->userCan( 'create', $this->user ) );
688
689 $this->setUserPerm( array( 'createpage' ) );
690 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
691 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
692 $this->assertEquals( false,
693 $this->title->userCan( 'create', $this->user ) );
694
695 $this->setTitle( NS_MEDIA, "test page" );
696 $this->setUserPerm( array( "move" ) );
697 $this->assertEquals( false,
698 $this->title->userCan( 'move', $this->user ) );
699 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
700 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
701
702 $this->setTitle( NS_HELP, "test page" );
703 $this->assertEquals( array(),
704 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
705 $this->assertEquals( true,
706 $this->title->userCan( 'move', $this->user ) );
707
708 $this->title->mInterwiki = "no";
709 $this->assertEquals( array( array( 'immobile-source-page' ) ),
710 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
711 $this->assertEquals( false,
712 $this->title->userCan( 'move', $this->user ) );
713
714 $this->setTitle( NS_MEDIA, "test page" );
715 $this->assertEquals( false,
716 $this->title->userCan( 'move-target', $this->user ) );
717 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
718 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
719
720 $this->setTitle( NS_HELP, "test page" );
721 $this->assertEquals( array(),
722 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
723 $this->assertEquals( true,
724 $this->title->userCan( 'move-target', $this->user ) );
725
726 $this->title->mInterwiki = "no";
727 $this->assertEquals( array( array( 'immobile-target-page' ) ),
728 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
729 $this->assertEquals( false,
730 $this->title->userCan( 'move-target', $this->user ) );
731 }
732
733 public function testUserBlock() {
734 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
735 $wgEmailConfirmToEdit = true;
736 $wgEmailAuthentication = true;
737
738 $this->setUserPerm( array( "createpage", "move" ) );
739 $this->setTitle( NS_HELP, "test page" );
740
741 # $short
742 $this->assertEquals( array( array( 'confirmedittext' ) ),
743 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
744 $wgEmailConfirmToEdit = false;
745 $this->assertEquals( true, $this->title->userCan( 'move-target', $this->user ) );
746
747 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
748 $this->assertEquals( array(),
749 $this->title->getUserPermissionsErrors( 'move-target',
750 $this->user ) );
751
752 global $wgLang;
753 $prev = time();
754 $now = time() + 120;
755 $this->user->mBlockedby = $this->user->getId();
756 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
757 'no reason given', $prev + 3600, 1, 0 );
758 $this->user->mBlock->mTimestamp = 0;
759 $this->assertEquals( array( array( 'autoblockedtext',
760 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
761 'Useruser', null, 'infinite', '127.0.8.1',
762 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
763 $this->title->getUserPermissionsErrors( 'move-target',
764 $this->user ) );
765
766 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
767 // quickUserCan should ignore user blocks
768 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
769
770 global $wgLocalTZoffset;
771 $wgLocalTZoffset = -60;
772 $this->user->mBlockedby = $this->user->getName();
773 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
774 'no reason given', $now, 0, 10 );
775 $this->assertEquals( array( array( 'blockedtext',
776 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
777 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
778 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
779 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
780 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
781 # $user->blockedFor() == ''
782 # $user->mBlock->mExpiry == 'infinity'
783 }
784 }