Merge "Improve rendering of Living Style Guide sections, examples, and modifiers."
[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
45 $this->userName = 'Useruser';
46 $this->altUserName = 'Altuseruser';
47 date_default_timezone_set( $localZone );
48
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 );
52
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();
58 }
59
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();
66 }
67
68 $this->anonUser = User::newFromId( 0 );
69
70 $this->user = $this->userUser;
71 }
72 }
73
74 protected function setUserPerm( $perm ) {
75 // Setting member variables is evil!!!
76
77 if ( is_array( $perm ) ) {
78 $this->user->mRights = $perm;
79 } else {
80 $this->user->mRights = array( $perm );
81 }
82 }
83
84 protected function setTitle( $ns, $title = "Main_Page" ) {
85 $this->title = Title::makeTitle( $ns, $title );
86 }
87
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;
95 }
96 }
97
98 /**
99 * @todo This test method should be split up into separate test methods and
100 * data providers
101 */
102 public function testQuickPermissions() {
103 global $wgContLang;
104 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
105
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 );
111
112 $this->setTitle( NS_TALK );
113 $this->setUserPerm( "createpage" );
114 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
115 $this->assertEquals( array( array( "nocreatetext" ) ), $res );
116
117 $this->setTitle( NS_TALK );
118 $this->setUserPerm( "" );
119 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
120 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
121
122 $this->setTitle( NS_MAIN );
123 $this->setUserPerm( "createpage" );
124 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
125 $this->assertEquals( array(), $res );
126
127 $this->setTitle( NS_MAIN );
128 $this->setUserPerm( "createtalk" );
129 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
130 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
131
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 );
137
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 );
142
143 $this->setTitle( NS_TALK );
144 $this->setUserPerm( "" );
145 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
146 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
147
148 $this->setTitle( NS_MAIN );
149 $this->setUserPerm( "createpage" );
150 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
151 $this->assertEquals( array(), $res );
152
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 );
157
158 $this->setTitle( NS_MAIN );
159 $this->setUserPerm( "" );
160 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
161 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
162
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 );
168
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 );
173
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 );
178
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 );
183
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 );
188
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 );
193
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 );
198
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 );
203
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 );
209
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 );
214
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 );
220
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 );
225
226 $this->setUser( $this->userName );
227 $this->setUserPerm( "move" );
228 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
229
230 $this->setUserPerm( "" );
231 $this->runGroupPermissions(
232 'move',
233 array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) )
234 );
235
236 $this->setUser( 'anon' );
237 $this->setUserPerm( "move" );
238 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
239
240 $this->setUserPerm( "" );
241 $this->runGroupPermissions(
242 'move',
243 array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
244 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) )
245 );
246
247 if ( $this->isWikitextNS( NS_MAIN ) ) {
248 //NOTE: some content models don't allow moving
249 // @todo find a Wikitext namespace for testing
250
251 $this->setTitle( NS_MAIN );
252 $this->setUser( 'anon' );
253 $this->setUserPerm( "move" );
254 $this->runGroupPermissions( 'move', array() );
255
256 $this->setUserPerm( "" );
257 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
258 array( array( 'movenologintext' ) ) );
259
260 $this->setUser( $this->userName );
261 $this->setUserPerm( "" );
262 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
263
264 $this->setUserPerm( "move" );
265 $this->runGroupPermissions( 'move', array() );
266
267 $this->setUser( 'anon' );
268 $this->setUserPerm( 'move' );
269 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
270 $this->assertEquals( array(), $res );
271
272 $this->setUserPerm( '' );
273 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
274 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
275 }
276
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 );
282
283 $this->setUserPerm( "move" );
284 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
285 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
286
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 );
291
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 );
296
297 $this->setUserPerm( "move" );
298 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
299 $this->assertEquals( array(), $res );
300
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
308 ),
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
318 ),
319 '' => array( array(), array(), array(), true )
320 );
321
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 ) );
326
327 global $wgGroupPermissions;
328 $old = $wgGroupPermissions;
329 $wgGroupPermissions = array();
330
331 $this->assertEquals( $check[$action][1],
332 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
333 $wgGroupPermissions = $old;
334
335 $this->setUserPerm( $action );
336 $this->assertEquals( $check[$action][2],
337 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
338
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
345 }
346 }
347
348 protected function runGroupPermissions( $action, $result, $result2 = null ) {
349 global $wgGroupPermissions;
350
351 if ( $result2 === null ) {
352 $result2 = $result;
353 }
354
355 $wgGroupPermissions['autoconfirmed']['move'] = false;
356 $wgGroupPermissions['user']['move'] = false;
357 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
358 $this->assertEquals( $result, $res );
359
360 $wgGroupPermissions['autoconfirmed']['move'] = true;
361 $wgGroupPermissions['user']['move'] = false;
362 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
363 $this->assertEquals( $result2, $res );
364
365 $wgGroupPermissions['autoconfirmed']['move'] = true;
366 $wgGroupPermissions['user']['move'] = true;
367 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
368 $this->assertEquals( $result2, $res );
369
370 $wgGroupPermissions['autoconfirmed']['move'] = false;
371 $wgGroupPermissions['user']['move'] = true;
372 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
373 $this->assertEquals( $result2, $res );
374 }
375
376 /**
377 * @todo This test method should be split up into separate test methods and
378 * data providers
379 */
380 public function testSpecialsAndNSPermissions() {
381 global $wgNamespaceProtection;
382 $this->setUser( $this->userName );
383
384 $this->setTitle( NS_SPECIAL );
385
386 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
387 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
388
389 $this->setTitle( NS_MAIN );
390 $this->setUserPerm( 'bogus' );
391 $this->assertEquals( array(),
392 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
393
394 $this->setTitle( NS_MAIN );
395 $this->setUserPerm( '' );
396 $this->assertEquals( array( array( 'badaccess-group0' ) ),
397 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
398
399 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
400
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 ) );
405
406 $this->setTitle( NS_MEDIAWIKI );
407 $this->setUserPerm( 'bogus' );
408 $this->assertEquals( array( array( 'protectedinterface' ) ),
409 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
410
411 $this->setTitle( NS_MEDIAWIKI );
412 $this->setUserPerm( 'bogus' );
413 $this->assertEquals( array( array( 'protectedinterface' ) ),
414 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
415
416 $wgNamespaceProtection = null;
417
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 ) );
423
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 ) );
429 }
430
431 /**
432 * @todo This test method should be split up into separate test methods and
433 * data providers
434 */
435 public function testCssAndJavascriptPermissions() {
436 $this->setUser( $this->userName );
437
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' ) )
445 );
446
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' ) )
454 );
455
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' ) )
463 );
464
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' ) )
472 );
473
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' ) )
481 );
482 }
483
484 protected function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
485 $this->setUserPerm( '' );
486 $this->assertEquals( $result0,
487 $this->title->getUserPermissionsErrors( 'bogus',
488 $this->user ) );
489
490 $this->setUserPerm( 'editmyusercss' );
491 $this->assertEquals( $result1,
492 $this->title->getUserPermissionsErrors( 'bogus',
493 $this->user ) );
494
495 $this->setUserPerm( 'editmyuserjs' );
496 $this->assertEquals( $result2,
497 $this->title->getUserPermissionsErrors( 'bogus',
498 $this->user ) );
499
500 $this->setUserPerm( 'editusercss' );
501 $this->assertEquals( $result3,
502 $this->title->getUserPermissionsErrors( 'bogus',
503 $this->user ) );
504
505 $this->setUserPerm( 'edituserjs' );
506 $this->assertEquals( $result4,
507 $this->title->getUserPermissionsErrors( 'bogus',
508 $this->user ) );
509
510 $this->setUserPerm( 'editusercssjs' );
511 $this->assertEquals( array( array( 'badaccess-group0' ) ),
512 $this->title->getUserPermissionsErrors( 'bogus',
513 $this->user ) );
514
515 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
516 $this->assertEquals( array( array( 'badaccess-group0' ) ),
517 $this->title->getUserPermissionsErrors( 'bogus',
518 $this->user ) );
519 }
520
521 /**
522 * @todo This test method should be split up into separate test methods and
523 * data providers
524 */
525 public function testPageRestrictions() {
526 global $wgContLang;
527
528 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
529
530 $this->setTitle( NS_MAIN );
531 $this->title->mRestrictionsLoaded = true;
532 $this->setUserPerm( "edit" );
533 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
534
535 $this->assertEquals( array(),
536 $this->title->getUserPermissionsErrors( 'edit',
537 $this->user ) );
538
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", "" ) );
543
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 ) );
579
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 ) );
597
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 ) );
614 }
615
616 public function testCascadingSourcesRestrictions() {
617 $this->setTitle( NS_MAIN, "test page" );
618 $this->setUserPerm( array( "edit", "bogus" ) );
619
620 $this->title->mCascadeSources = array(
621 Title::makeTitle( NS_MAIN, "Bogus" ),
622 Title::makeTitle( NS_MAIN, "UnBogus" )
623 );
624 $this->title->mCascadingRestrictions = array(
625 "bogus" => array( 'bogus', "sysop", "protect", "" )
626 );
627
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 ) );
634
635 $this->assertEquals( true,
636 $this->title->userCan( 'edit', $this->user ) );
637 $this->assertEquals( array(),
638 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
639 }
640
641 /**
642 * @todo This test method should be split up into separate test methods and
643 * data providers
644 */
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;
653
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 ) );
658
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 ) );
665
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 ) );
671
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 ) );
677
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 ) );
684
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 ) );
690
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 ) );
696
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 ) );
702
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 ) );
708
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 ) );
714 }
715
716 public function testUserBlock() {
717 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
718 $wgEmailConfirmToEdit = true;
719 $wgEmailAuthentication = true;
720
721 $this->setUserPerm( array( "createpage", "move" ) );
722 $this->setTitle( NS_HELP, "test page" );
723
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 ) );
729
730 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
731 $this->assertEquals( array(),
732 $this->title->getUserPermissionsErrors( 'move-target',
733 $this->user ) );
734
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 ) );
748
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 ) );
752
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'
766 }
767 }