Destroy session after running api tests
[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
330 global $wgGroupPermissions;
331 $old = $wgGroupPermissions;
332 $wgGroupPermissions = array();
333
334 $this->assertEquals( $check[$action][1],
335 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
336 $wgGroupPermissions = $old;
337
338 $this->setUserPerm( $action );
339 $this->assertEquals( $check[$action][2],
340 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
341
342 $this->setUserPerm( $action );
343 $this->assertEquals( $check[$action][3],
344 $this->title->userCan( $action, $this->user, true ) );
345 $this->assertEquals( $check[$action][3],
346 $this->title->quickUserCan( $action, $this->user ) );
347 # count( User::getGroupsWithPermissions( $action ) ) < 1
348 }
349 }
350
351 protected function runGroupPermissions( $action, $result, $result2 = null ) {
352 global $wgGroupPermissions;
353
354 if ( $result2 === null ) {
355 $result2 = $result;
356 }
357
358 $wgGroupPermissions['autoconfirmed']['move'] = false;
359 $wgGroupPermissions['user']['move'] = false;
360 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
361 $this->assertEquals( $result, $res );
362
363 $wgGroupPermissions['autoconfirmed']['move'] = true;
364 $wgGroupPermissions['user']['move'] = false;
365 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
366 $this->assertEquals( $result2, $res );
367
368 $wgGroupPermissions['autoconfirmed']['move'] = true;
369 $wgGroupPermissions['user']['move'] = true;
370 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
371 $this->assertEquals( $result2, $res );
372
373 $wgGroupPermissions['autoconfirmed']['move'] = false;
374 $wgGroupPermissions['user']['move'] = true;
375 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
376 $this->assertEquals( $result2, $res );
377 }
378
379 /**
380 * @todo This test method should be split up into separate test methods and
381 * data providers
382 */
383 public function testSpecialsAndNSPermissions() {
384 global $wgNamespaceProtection;
385 $this->setUser( $this->userName );
386
387 $this->setTitle( NS_SPECIAL );
388
389 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
390 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
391
392 $this->setTitle( NS_MAIN );
393 $this->setUserPerm( 'bogus' );
394 $this->assertEquals( array(),
395 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
396
397 $this->setTitle( NS_MAIN );
398 $this->setUserPerm( '' );
399 $this->assertEquals( array( array( 'badaccess-group0' ) ),
400 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
401
402 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
403
404 $this->setTitle( NS_USER );
405 $this->setUserPerm( '' );
406 $this->assertEquals( array( array( 'badaccess-group0' ),
407 array( 'namespaceprotected', 'User', 'bogus' ) ),
408 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
409
410 $this->setTitle( NS_MEDIAWIKI );
411 $this->setUserPerm( 'bogus' );
412 $this->assertEquals( array( array( 'protectedinterface', 'bogus' ) ),
413 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
414
415 $this->setTitle( NS_MEDIAWIKI );
416 $this->setUserPerm( 'bogus' );
417 $this->assertEquals( array( array( 'protectedinterface', 'bogus' ) ),
418 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
419
420 $wgNamespaceProtection = null;
421
422 $this->setUserPerm( 'bogus' );
423 $this->assertEquals( array(),
424 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
425 $this->assertEquals( true,
426 $this->title->userCan( 'bogus', $this->user ) );
427
428 $this->setUserPerm( '' );
429 $this->assertEquals( array( array( 'badaccess-group0' ) ),
430 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
431 $this->assertEquals( false,
432 $this->title->userCan( 'bogus', $this->user ) );
433 }
434
435 /**
436 * @todo This test method should be split up into separate test methods and
437 * data providers
438 */
439 public function testCssAndJavascriptPermissions() {
440 $this->setUser( $this->userName );
441
442 $this->setTitle( NS_USER, $this->userName . '/test.js' );
443 $this->runCSSandJSPermissions(
444 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected', 'bogus' ) ),
445 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected', 'bogus' ) ),
446 array( array( 'badaccess-group0' ) ),
447 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected', 'bogus' ) ),
448 array( array( 'badaccess-group0' ) )
449 );
450
451 $this->setTitle( NS_USER, $this->userName . '/test.css' );
452 $this->runCSSandJSPermissions(
453 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected', 'bogus' ) ),
454 array( array( 'badaccess-group0' ) ),
455 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected', 'bogus' ) ),
456 array( array( 'badaccess-group0' ) ),
457 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected', 'bogus' ) )
458 );
459
460 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
461 $this->runCSSandJSPermissions(
462 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
463 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
464 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
465 array( array( 'badaccess-group0' ), array( 'customjsprotected', 'bogus' ) ),
466 array( array( 'badaccess-group0' ) )
467 );
468
469 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
470 $this->runCSSandJSPermissions(
471 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) ),
472 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) ),
473 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) ),
474 array( array( 'badaccess-group0' ) ),
475 array( array( 'badaccess-group0' ), array( 'customcssprotected', 'bogus' ) )
476 );
477
478 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
479 $this->runCSSandJSPermissions(
480 array( array( 'badaccess-group0' ) ),
481 array( array( 'badaccess-group0' ) ),
482 array( array( 'badaccess-group0' ) ),
483 array( array( 'badaccess-group0' ) ),
484 array( array( 'badaccess-group0' ) )
485 );
486 }
487
488 protected function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
489 $this->setUserPerm( '' );
490 $this->assertEquals( $result0,
491 $this->title->getUserPermissionsErrors( 'bogus',
492 $this->user ) );
493
494 $this->setUserPerm( 'editmyusercss' );
495 $this->assertEquals( $result1,
496 $this->title->getUserPermissionsErrors( 'bogus',
497 $this->user ) );
498
499 $this->setUserPerm( 'editmyuserjs' );
500 $this->assertEquals( $result2,
501 $this->title->getUserPermissionsErrors( 'bogus',
502 $this->user ) );
503
504 $this->setUserPerm( 'editusercss' );
505 $this->assertEquals( $result3,
506 $this->title->getUserPermissionsErrors( 'bogus',
507 $this->user ) );
508
509 $this->setUserPerm( 'edituserjs' );
510 $this->assertEquals( $result4,
511 $this->title->getUserPermissionsErrors( 'bogus',
512 $this->user ) );
513
514 $this->setUserPerm( 'editusercssjs' );
515 $this->assertEquals( array( array( 'badaccess-group0' ) ),
516 $this->title->getUserPermissionsErrors( 'bogus',
517 $this->user ) );
518
519 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
520 $this->assertEquals( array( array( 'badaccess-group0' ) ),
521 $this->title->getUserPermissionsErrors( 'bogus',
522 $this->user ) );
523 }
524
525 /**
526 * @todo This test method should be split up into separate test methods and
527 * data providers
528 */
529 public function testPageRestrictions() {
530 global $wgContLang;
531
532 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
533
534 $this->setTitle( NS_MAIN );
535 $this->title->mRestrictionsLoaded = true;
536 $this->setUserPerm( "edit" );
537 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
538
539 $this->assertEquals( array(),
540 $this->title->getUserPermissionsErrors( 'edit',
541 $this->user ) );
542
543 $this->assertEquals( true,
544 $this->title->quickUserCan( 'edit', $this->user ) );
545 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
546 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
547
548 $this->assertEquals( array( array( 'badaccess-group0' ),
549 array( 'protectedpagetext', 'bogus', 'bogus' ),
550 array( 'protectedpagetext', 'editprotected', 'bogus' ),
551 array( 'protectedpagetext', 'protect', 'bogus' ) ),
552 $this->title->getUserPermissionsErrors( 'bogus',
553 $this->user ) );
554 $this->assertEquals( array( array( 'protectedpagetext', 'bogus', 'edit' ),
555 array( 'protectedpagetext', 'editprotected', 'edit' ),
556 array( 'protectedpagetext', 'protect', 'edit' ) ),
557 $this->title->getUserPermissionsErrors( 'edit',
558 $this->user ) );
559 $this->setUserPerm( "" );
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( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
567 array( 'protectedpagetext', 'bogus', 'edit' ),
568 array( 'protectedpagetext', 'editprotected', 'edit' ),
569 array( 'protectedpagetext', 'protect', 'edit' ) ),
570 $this->title->getUserPermissionsErrors( 'edit',
571 $this->user ) );
572 $this->setUserPerm( array( "edit", "editprotected" ) );
573 $this->assertEquals( array( array( 'badaccess-group0' ),
574 array( 'protectedpagetext', 'bogus', 'bogus' ),
575 array( 'protectedpagetext', 'protect', 'bogus' ) ),
576 $this->title->getUserPermissionsErrors( 'bogus',
577 $this->user ) );
578 $this->assertEquals( array(
579 array( 'protectedpagetext', 'bogus', 'edit' ),
580 array( 'protectedpagetext', 'protect', 'edit' ) ),
581 $this->title->getUserPermissionsErrors( 'edit',
582 $this->user ) );
583
584 $this->title->mCascadeRestriction = true;
585 $this->setUserPerm( "edit" );
586 $this->assertEquals( false,
587 $this->title->quickUserCan( 'bogus', $this->user ) );
588 $this->assertEquals( false,
589 $this->title->quickUserCan( 'edit', $this->user ) );
590 $this->assertEquals( array( array( 'badaccess-group0' ),
591 array( 'protectedpagetext', 'bogus', 'bogus' ),
592 array( 'protectedpagetext', 'editprotected', 'bogus' ),
593 array( 'protectedpagetext', 'protect', 'bogus' ) ),
594 $this->title->getUserPermissionsErrors( 'bogus',
595 $this->user ) );
596 $this->assertEquals( array( array( 'protectedpagetext', 'bogus', 'edit' ),
597 array( 'protectedpagetext', 'editprotected', 'edit' ),
598 array( 'protectedpagetext', 'protect', 'edit' ) ),
599 $this->title->getUserPermissionsErrors( 'edit',
600 $this->user ) );
601
602 $this->setUserPerm( array( "edit", "editprotected" ) );
603 $this->assertEquals( false,
604 $this->title->quickUserCan( 'bogus', $this->user ) );
605 $this->assertEquals( false,
606 $this->title->quickUserCan( 'edit', $this->user ) );
607 $this->assertEquals( array( array( 'badaccess-group0' ),
608 array( 'protectedpagetext', 'bogus', 'bogus' ),
609 array( 'protectedpagetext', 'protect', 'bogus' ),
610 array( 'protectedpagetext', 'protect', 'bogus' ) ),
611 $this->title->getUserPermissionsErrors( 'bogus',
612 $this->user ) );
613 $this->assertEquals( array( array( 'protectedpagetext', 'bogus', 'edit' ),
614 array( 'protectedpagetext', 'protect', 'edit' ),
615 array( 'protectedpagetext', 'protect', 'edit' ) ),
616 $this->title->getUserPermissionsErrors( 'edit',
617 $this->user ) );
618 }
619
620 public function testCascadingSourcesRestrictions() {
621 $this->setTitle( NS_MAIN, "test page" );
622 $this->setUserPerm( array( "edit", "bogus" ) );
623
624 $this->title->mCascadeSources = array(
625 Title::makeTitle( NS_MAIN, "Bogus" ),
626 Title::makeTitle( NS_MAIN, "UnBogus" )
627 );
628 $this->title->mCascadingRestrictions = array(
629 "bogus" => array( 'bogus', "sysop", "protect", "" )
630 );
631
632 $this->assertEquals( false,
633 $this->title->userCan( 'bogus', $this->user ) );
634 $this->assertEquals( array(
635 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ),
636 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ),
637 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ) ),
638 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
639
640 $this->assertEquals( true,
641 $this->title->userCan( 'edit', $this->user ) );
642 $this->assertEquals( array(),
643 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
644 }
645
646 /**
647 * @todo This test method should be split up into separate test methods and
648 * data providers
649 */
650 public function testActionPermissions() {
651 $this->setUserPerm( array( "createpage" ) );
652 $this->setTitle( NS_MAIN, "test page" );
653 $this->title->mTitleProtection['permission'] = '';
654 $this->title->mTitleProtection['user'] = $this->user->getID();
655 $this->title->mTitleProtection['expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
656 $this->title->mTitleProtection['reason'] = 'test';
657 $this->title->mCascadeRestriction = false;
658
659 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
660 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
661 $this->assertEquals( false,
662 $this->title->userCan( 'create', $this->user ) );
663
664 $this->title->mTitleProtection['permission'] = 'editprotected';
665 $this->setUserPerm( array( 'createpage', 'protect' ) );
666 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
667 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
668 $this->assertEquals( false,
669 $this->title->userCan( 'create', $this->user ) );
670
671 $this->setUserPerm( array( 'createpage', 'editprotected' ) );
672 $this->assertEquals( array(),
673 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
674 $this->assertEquals( true,
675 $this->title->userCan( 'create', $this->user ) );
676
677 $this->setUserPerm( array( 'createpage' ) );
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->setTitle( NS_MEDIA, "test page" );
684 $this->setUserPerm( array( "move" ) );
685 $this->assertEquals( false,
686 $this->title->userCan( 'move', $this->user ) );
687 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
688 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
689
690 $this->setTitle( NS_HELP, "test page" );
691 $this->assertEquals( array(),
692 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
693 $this->assertEquals( true,
694 $this->title->userCan( 'move', $this->user ) );
695
696 $this->title->mInterwiki = "no";
697 $this->assertEquals( array( array( 'immobile-source-page' ) ),
698 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
699 $this->assertEquals( false,
700 $this->title->userCan( 'move', $this->user ) );
701
702 $this->setTitle( NS_MEDIA, "test page" );
703 $this->assertEquals( false,
704 $this->title->userCan( 'move-target', $this->user ) );
705 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
706 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
707
708 $this->setTitle( NS_HELP, "test page" );
709 $this->assertEquals( array(),
710 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
711 $this->assertEquals( true,
712 $this->title->userCan( 'move-target', $this->user ) );
713
714 $this->title->mInterwiki = "no";
715 $this->assertEquals( array( array( 'immobile-target-page' ) ),
716 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
717 $this->assertEquals( false,
718 $this->title->userCan( 'move-target', $this->user ) );
719 }
720
721 public function testUserBlock() {
722 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
723 $wgEmailConfirmToEdit = true;
724 $wgEmailAuthentication = true;
725
726 $this->setUserPerm( array( "createpage", "move" ) );
727 $this->setTitle( NS_HELP, "test page" );
728
729 # $short
730 $this->assertEquals( array( array( 'confirmedittext' ) ),
731 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
732 $wgEmailConfirmToEdit = false;
733 $this->assertEquals( true, $this->title->userCan( 'move-target', $this->user ) );
734
735 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
736 $this->assertEquals( array(),
737 $this->title->getUserPermissionsErrors( 'move-target',
738 $this->user ) );
739
740 global $wgLang;
741 $prev = time();
742 $now = time() + 120;
743 $this->user->mBlockedby = $this->user->getId();
744 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
745 'no reason given', $prev + 3600, 1, 0 );
746 $this->user->mBlock->mTimestamp = 0;
747 $this->assertEquals( array( array( 'autoblockedtext',
748 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
749 'Useruser', null, 'infinite', '127.0.8.1',
750 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
751 $this->title->getUserPermissionsErrors( 'move-target',
752 $this->user ) );
753
754 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
755 // quickUserCan should ignore user blocks
756 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
757
758 global $wgLocalTZoffset;
759 $wgLocalTZoffset = -60;
760 $this->user->mBlockedby = $this->user->getName();
761 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
762 'no reason given', $now, 0, 10 );
763 $this->assertEquals( array( array( 'blockedtext',
764 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
765 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
766 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
767 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
768 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
769 # $user->blockedFor() == ''
770 # $user->mBlock->mExpiry == 'infinity'
771 }
772 }