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