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