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