Merge "FormatJson: Remove whitespace from empty arrays and objects"
[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( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ) );
232
233 $this->setUser( 'anon' );
234 $this->setUserPerm( "move" );
235 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
236
237 $this->setUserPerm( "" );
238 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
239 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ) );
240
241 if ( $this->isWikitextNS( NS_MAIN ) ) {
242 //NOTE: some content models don't allow moving
243 // @todo find a Wikitext namespace for testing
244
245 $this->setTitle( NS_MAIN );
246 $this->setUser( 'anon' );
247 $this->setUserPerm( "move" );
248 $this->runGroupPermissions( 'move', array() );
249
250 $this->setUserPerm( "" );
251 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
252 array( array( 'movenologintext' ) ) );
253
254 $this->setUser( $this->userName );
255 $this->setUserPerm( "" );
256 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
257
258 $this->setUserPerm( "move" );
259 $this->runGroupPermissions( 'move', array() );
260
261 $this->setUser( 'anon' );
262 $this->setUserPerm( 'move' );
263 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
264 $this->assertEquals( array(), $res );
265
266 $this->setUserPerm( '' );
267 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
268 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
269 }
270
271 $this->setTitle( NS_USER );
272 $this->setUser( $this->userName );
273 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
274 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
275 $this->assertEquals( array(), $res );
276
277 $this->setUserPerm( "move" );
278 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
279 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
280
281 $this->setUser( 'anon' );
282 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
283 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
284 $this->assertEquals( array(), $res );
285
286 $this->setTitle( NS_USER, "User/subpage" );
287 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
288 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
289 $this->assertEquals( array(), $res );
290
291 $this->setUserPerm( "move" );
292 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
293 $this->assertEquals( array(), $res );
294
295 $this->setUser( 'anon' );
296 $check = array( 'edit' => array( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ) ),
297 array( array( 'badaccess-group0' ) ),
298 array(), true ),
299 'protect' => array( array( array( 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ), array( 'protect-cantedit' ) ),
300 array( array( 'badaccess-group0' ), array( 'protect-cantedit' ) ),
301 array( array( 'protect-cantedit' ) ), false ),
302 '' => array( array(), array(), array(), true ) );
303
304 foreach ( array( "edit", "protect", "" ) as $action ) {
305 $this->setUserPerm( null );
306 $this->assertEquals( $check[$action][0],
307 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
308
309 global $wgGroupPermissions;
310 $old = $wgGroupPermissions;
311 $wgGroupPermissions = array();
312
313 $this->assertEquals( $check[$action][1],
314 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
315 $wgGroupPermissions = $old;
316
317 $this->setUserPerm( $action );
318 $this->assertEquals( $check[$action][2],
319 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
320
321 $this->setUserPerm( $action );
322 $this->assertEquals( $check[$action][3],
323 $this->title->userCan( $action, $this->user, true ) );
324 $this->assertEquals( $check[$action][3],
325 $this->title->quickUserCan( $action, $this->user ) );
326 # count( User::getGroupsWithPermissions( $action ) ) < 1
327 }
328 }
329
330 protected function runGroupPermissions( $action, $result, $result2 = null ) {
331 global $wgGroupPermissions;
332
333 if ( $result2 === null ) {
334 $result2 = $result;
335 }
336
337 $wgGroupPermissions['autoconfirmed']['move'] = false;
338 $wgGroupPermissions['user']['move'] = false;
339 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
340 $this->assertEquals( $result, $res );
341
342 $wgGroupPermissions['autoconfirmed']['move'] = true;
343 $wgGroupPermissions['user']['move'] = false;
344 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
345 $this->assertEquals( $result2, $res );
346
347 $wgGroupPermissions['autoconfirmed']['move'] = true;
348 $wgGroupPermissions['user']['move'] = true;
349 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
350 $this->assertEquals( $result2, $res );
351
352 $wgGroupPermissions['autoconfirmed']['move'] = false;
353 $wgGroupPermissions['user']['move'] = true;
354 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
355 $this->assertEquals( $result2, $res );
356 }
357
358 /**
359 * @todo This test method should be split up into separate test methods and
360 * data providers
361 */
362 public function testSpecialsAndNSPermissions() {
363 global $wgNamespaceProtection;
364 $this->setUser( $this->userName );
365
366 $this->setTitle( NS_SPECIAL );
367
368 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
369 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
370
371 $this->setTitle( NS_MAIN );
372 $this->setUserPerm( 'bogus' );
373 $this->assertEquals( array(),
374 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
375
376 $this->setTitle( NS_MAIN );
377 $this->setUserPerm( '' );
378 $this->assertEquals( array( array( 'badaccess-group0' ) ),
379 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
380
381 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
382
383 $this->setTitle( NS_USER );
384 $this->setUserPerm( '' );
385 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
386 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
387
388 $this->setTitle( NS_MEDIAWIKI );
389 $this->setUserPerm( 'bogus' );
390 $this->assertEquals( array( array( 'protectedinterface' ) ),
391 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
392
393 $this->setTitle( NS_MEDIAWIKI );
394 $this->setUserPerm( 'bogus' );
395 $this->assertEquals( array( array( 'protectedinterface' ) ),
396 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
397
398 $wgNamespaceProtection = null;
399
400 $this->setUserPerm( 'bogus' );
401 $this->assertEquals( array(),
402 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
403 $this->assertEquals( true,
404 $this->title->userCan( 'bogus', $this->user ) );
405
406 $this->setUserPerm( '' );
407 $this->assertEquals( array( array( 'badaccess-group0' ) ),
408 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
409 $this->assertEquals( false,
410 $this->title->userCan( 'bogus', $this->user ) );
411 }
412
413 /**
414 * @todo This test method should be split up into separate test methods and
415 * data providers
416 */
417 public function testCssAndJavascriptPermissions() {
418 $this->setUser( $this->userName );
419
420 $this->setTitle( NS_USER, $this->userName . '/test.js' );
421 $this->runCSSandJSPermissions(
422 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
423 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
424 array( array( 'badaccess-group0' ) ),
425 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
426 array( array( 'badaccess-group0' ) )
427 );
428
429 $this->setTitle( NS_USER, $this->userName . '/test.css' );
430 $this->runCSSandJSPermissions(
431 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
432 array( array( 'badaccess-group0' ) ),
433 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
434 array( array( 'badaccess-group0' ) ),
435 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) )
436 );
437
438 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
439 $this->runCSSandJSPermissions(
440 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
441 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
442 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
443 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
444 array( array( 'badaccess-group0' ) )
445 );
446
447 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
448 $this->runCSSandJSPermissions(
449 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
450 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
451 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
452 array( array( 'badaccess-group0' ) ),
453 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) )
454 );
455
456 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
457 $this->runCSSandJSPermissions(
458 array( array( 'badaccess-group0' ) ),
459 array( array( 'badaccess-group0' ) ),
460 array( array( 'badaccess-group0' ) ),
461 array( array( 'badaccess-group0' ) ),
462 array( array( 'badaccess-group0' ) )
463 );
464 }
465
466 protected function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
467 $this->setUserPerm( '' );
468 $this->assertEquals( $result0,
469 $this->title->getUserPermissionsErrors( 'bogus',
470 $this->user ) );
471
472 $this->setUserPerm( 'editmyusercss' );
473 $this->assertEquals( $result1,
474 $this->title->getUserPermissionsErrors( 'bogus',
475 $this->user ) );
476
477 $this->setUserPerm( 'editmyuserjs' );
478 $this->assertEquals( $result2,
479 $this->title->getUserPermissionsErrors( 'bogus',
480 $this->user ) );
481
482 $this->setUserPerm( 'editusercss' );
483 $this->assertEquals( $result3,
484 $this->title->getUserPermissionsErrors( 'bogus',
485 $this->user ) );
486
487 $this->setUserPerm( 'edituserjs' );
488 $this->assertEquals( $result4,
489 $this->title->getUserPermissionsErrors( 'bogus',
490 $this->user ) );
491
492 $this->setUserPerm( 'editusercssjs' );
493 $this->assertEquals( array( array( 'badaccess-group0' ) ),
494 $this->title->getUserPermissionsErrors( 'bogus',
495 $this->user ) );
496
497 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
498 $this->assertEquals( array( array( 'badaccess-group0' ) ),
499 $this->title->getUserPermissionsErrors( 'bogus',
500 $this->user ) );
501 }
502
503 /**
504 * @todo This test method should be split up into separate test methods and
505 * data providers
506 */
507 public function testPageRestrictions() {
508 global $wgContLang;
509
510 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
511
512 $this->setTitle( NS_MAIN );
513 $this->title->mRestrictionsLoaded = true;
514 $this->setUserPerm( "edit" );
515 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
516
517 $this->assertEquals( array(),
518 $this->title->getUserPermissionsErrors( 'edit',
519 $this->user ) );
520
521 $this->assertEquals( true,
522 $this->title->quickUserCan( 'edit', $this->user ) );
523 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
524 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
525
526 $this->assertEquals( array( array( 'badaccess-group0' ),
527 array( 'protectedpagetext', 'bogus' ),
528 array( 'protectedpagetext', 'editprotected' ),
529 array( 'protectedpagetext', 'protect' ) ),
530 $this->title->getUserPermissionsErrors( 'bogus',
531 $this->user ) );
532 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
533 array( 'protectedpagetext', 'editprotected' ),
534 array( 'protectedpagetext', 'protect' ) ),
535 $this->title->getUserPermissionsErrors( 'edit',
536 $this->user ) );
537 $this->setUserPerm( "" );
538 $this->assertEquals( array( array( 'badaccess-group0' ),
539 array( 'protectedpagetext', 'bogus' ),
540 array( 'protectedpagetext', 'editprotected' ),
541 array( 'protectedpagetext', 'protect' ) ),
542 $this->title->getUserPermissionsErrors( 'bogus',
543 $this->user ) );
544 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
545 array( 'protectedpagetext', 'bogus' ),
546 array( 'protectedpagetext', 'editprotected' ),
547 array( 'protectedpagetext', 'protect' ) ),
548 $this->title->getUserPermissionsErrors( 'edit',
549 $this->user ) );
550 $this->setUserPerm( array( "edit", "editprotected" ) );
551 $this->assertEquals( array( array( 'badaccess-group0' ),
552 array( 'protectedpagetext', 'bogus' ),
553 array( 'protectedpagetext', 'protect' ) ),
554 $this->title->getUserPermissionsErrors( 'bogus',
555 $this->user ) );
556 $this->assertEquals( array(
557 array( 'protectedpagetext', 'bogus' ),
558 array( 'protectedpagetext', 'protect' ) ),
559 $this->title->getUserPermissionsErrors( 'edit',
560 $this->user ) );
561
562 $this->title->mCascadeRestriction = true;
563 $this->setUserPerm( "edit" );
564 $this->assertEquals( false,
565 $this->title->quickUserCan( 'bogus', $this->user ) );
566 $this->assertEquals( false,
567 $this->title->quickUserCan( 'edit', $this->user ) );
568 $this->assertEquals( array( array( 'badaccess-group0' ),
569 array( 'protectedpagetext', 'bogus' ),
570 array( 'protectedpagetext', 'editprotected' ),
571 array( 'protectedpagetext', 'protect' ) ),
572 $this->title->getUserPermissionsErrors( 'bogus',
573 $this->user ) );
574 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
575 array( 'protectedpagetext', 'editprotected' ),
576 array( 'protectedpagetext', 'protect' ) ),
577 $this->title->getUserPermissionsErrors( 'edit',
578 $this->user ) );
579
580 $this->setUserPerm( array( "edit", "editprotected" ) );
581 $this->assertEquals( false,
582 $this->title->quickUserCan( 'bogus', $this->user ) );
583 $this->assertEquals( false,
584 $this->title->quickUserCan( 'edit', $this->user ) );
585 $this->assertEquals( array( array( 'badaccess-group0' ),
586 array( 'protectedpagetext', 'bogus' ),
587 array( 'protectedpagetext', 'protect' ),
588 array( 'protectedpagetext', 'protect' ) ),
589 $this->title->getUserPermissionsErrors( 'bogus',
590 $this->user ) );
591 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
592 array( 'protectedpagetext', 'protect' ),
593 array( 'protectedpagetext', 'protect' ) ),
594 $this->title->getUserPermissionsErrors( 'edit',
595 $this->user ) );
596 }
597
598 public function testCascadingSourcesRestrictions() {
599 $this->setTitle( NS_MAIN, "test page" );
600 $this->setUserPerm( array( "edit", "bogus" ) );
601
602 $this->title->mCascadeSources = array( Title::makeTitle( NS_MAIN, "Bogus" ), Title::makeTitle( NS_MAIN, "UnBogus" ) );
603 $this->title->mCascadingRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
604
605 $this->assertEquals( false,
606 $this->title->userCan( 'bogus', $this->user ) );
607 $this->assertEquals( array( array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
608 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
609 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ) ),
610 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
611
612 $this->assertEquals( true,
613 $this->title->userCan( 'edit', $this->user ) );
614 $this->assertEquals( array(),
615 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
616 }
617
618 /**
619 * @todo This test method should be split up into separate test methods and
620 * data providers
621 */
622 public function testActionPermissions() {
623 $this->setUserPerm( array( "createpage" ) );
624 $this->setTitle( NS_MAIN, "test page" );
625 $this->title->mTitleProtection['pt_create_perm'] = '';
626 $this->title->mTitleProtection['pt_user'] = $this->user->getID();
627 $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
628 $this->title->mTitleProtection['pt_reason'] = 'test';
629 $this->title->mCascadeRestriction = false;
630
631 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
632 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
633 $this->assertEquals( false,
634 $this->title->userCan( 'create', $this->user ) );
635
636 $this->title->mTitleProtection['pt_create_perm'] = 'sysop';
637 $this->setUserPerm( array( 'createpage', 'protect' ) );
638 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
639 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
640 $this->assertEquals( false,
641 $this->title->userCan( 'create', $this->user ) );
642
643 $this->setUserPerm( array( 'createpage', 'editprotected' ) );
644 $this->assertEquals( array(),
645 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
646 $this->assertEquals( true,
647 $this->title->userCan( 'create', $this->user ) );
648
649 $this->setUserPerm( array( 'createpage' ) );
650 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
651 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
652 $this->assertEquals( false,
653 $this->title->userCan( 'create', $this->user ) );
654
655 $this->setTitle( NS_MEDIA, "test page" );
656 $this->setUserPerm( array( "move" ) );
657 $this->assertEquals( false,
658 $this->title->userCan( 'move', $this->user ) );
659 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
660 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
661
662 $this->setTitle( NS_HELP, "test page" );
663 $this->assertEquals( array(),
664 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
665 $this->assertEquals( true,
666 $this->title->userCan( 'move', $this->user ) );
667
668 $this->title->mInterwiki = "no";
669 $this->assertEquals( array( array( 'immobile-source-page' ) ),
670 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
671 $this->assertEquals( false,
672 $this->title->userCan( 'move', $this->user ) );
673
674 $this->setTitle( NS_MEDIA, "test page" );
675 $this->assertEquals( false,
676 $this->title->userCan( 'move-target', $this->user ) );
677 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
678 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
679
680 $this->setTitle( NS_HELP, "test page" );
681 $this->assertEquals( array(),
682 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
683 $this->assertEquals( true,
684 $this->title->userCan( 'move-target', $this->user ) );
685
686 $this->title->mInterwiki = "no";
687 $this->assertEquals( array( array( 'immobile-target-page' ) ),
688 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
689 $this->assertEquals( false,
690 $this->title->userCan( 'move-target', $this->user ) );
691 }
692
693 public function testUserBlock() {
694 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
695 $wgEmailConfirmToEdit = true;
696 $wgEmailAuthentication = true;
697
698 $this->setUserPerm( array( "createpage", "move" ) );
699 $this->setTitle( NS_HELP, "test page" );
700
701 # $short
702 $this->assertEquals( array( array( 'confirmedittext' ) ),
703 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
704 $wgEmailConfirmToEdit = false;
705 $this->assertEquals( true, $this->title->userCan( 'move-target', $this->user ) );
706
707 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
708 $this->assertEquals( array(),
709 $this->title->getUserPermissionsErrors( 'move-target',
710 $this->user ) );
711
712 global $wgLang;
713 $prev = time();
714 $now = time() + 120;
715 $this->user->mBlockedby = $this->user->getId();
716 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
717 'no reason given', $prev + 3600, 1, 0 );
718 $this->user->mBlock->mTimestamp = 0;
719 $this->assertEquals( array( array( 'autoblockedtext',
720 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
721 'Useruser', null, 'infinite', '127.0.8.1',
722 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
723 $this->title->getUserPermissionsErrors( 'move-target',
724 $this->user ) );
725
726 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
727 // quickUserCan should ignore user blocks
728 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
729
730 global $wgLocalTZoffset;
731 $wgLocalTZoffset = -60;
732 $this->user->mBlockedby = $this->user->getName();
733 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
734 'no reason given', $now, 0, 10 );
735 $this->assertEquals( array( array( 'blockedtext',
736 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
737 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
738 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
739 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
740 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
741 # $user->blockedFor() == ''
742 # $user->mBlock->mExpiry == 'infinity'
743 }
744 }