Fix typo in tests/phpunit/includes/Revision/MainSlotRoleHandlerTest.php
[lhc/web/wiklou.git] / tests / phpunit / includes / TitlePermissionTest.php
1 <?php
2
3 use MediaWiki\Block\Restriction\PageRestriction;
4 use MediaWiki\MediaWikiServices;
5
6 /**
7 * @group Database
8 *
9 * @covers Title::getUserPermissionsErrors
10 * @covers Title::getUserPermissionsErrorsInternal
11 */
12 class TitlePermissionTest extends MediaWikiLangTestCase {
13
14 /**
15 * @var string
16 */
17 protected $userName, $altUserName;
18
19 /**
20 * @var Title
21 */
22 protected $title;
23
24 /**
25 * @var User
26 */
27 protected $user, $anonUser, $userUser, $altUser;
28
29 protected function setUp() {
30 parent::setUp();
31
32 $localZone = 'UTC';
33 $localOffset = date( 'Z' ) / 60;
34
35 $this->setMwGlobals( [
36 'wgLocaltimezone' => $localZone,
37 'wgLocalTZoffset' => $localOffset,
38 'wgNamespaceProtection' => [
39 NS_MEDIAWIKI => 'editinterface',
40 ],
41 ] );
42 // Without this testUserBlock will use a non-English context on non-English MediaWiki
43 // installations (because of how Title::checkUserBlock is implemented) and fail.
44 RequestContext::resetMain();
45
46 $this->userName = 'Useruser';
47 $this->altUserName = 'Altuseruser';
48 date_default_timezone_set( $localZone );
49
50 $this->title = Title::makeTitle( NS_MAIN, "Main Page" );
51 if ( !isset( $this->userUser ) || !( $this->userUser instanceof User ) ) {
52 $this->userUser = User::newFromName( $this->userName );
53
54 if ( !$this->userUser->getId() ) {
55 $this->userUser = User::createNew( $this->userName, [
56 "email" => "test@example.com",
57 "real_name" => "Test User" ] );
58 $this->userUser->load();
59 }
60
61 $this->altUser = User::newFromName( $this->altUserName );
62 if ( !$this->altUser->getId() ) {
63 $this->altUser = User::createNew( $this->altUserName, [
64 "email" => "alttest@example.com",
65 "real_name" => "Test User Alt" ] );
66 $this->altUser->load();
67 }
68
69 $this->anonUser = User::newFromId( 0 );
70
71 $this->user = $this->userUser;
72 }
73 $this->overrideMwServices();
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 = [ $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 * This test is failing per T201776.
105 *
106 * @group Broken
107 * @covers Title::checkQuickPermissions
108 */
109 public function testQuickPermissions() {
110 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
111 getFormattedNsText( NS_PROJECT );
112
113 $this->setUser( 'anon' );
114 $this->setTitle( NS_TALK );
115 $this->setUserPerm( "createtalk" );
116 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
117 $this->assertEquals( [], $res );
118
119 $this->setTitle( NS_TALK );
120 $this->setUserPerm( "createpage" );
121 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
122 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
123
124 $this->setTitle( NS_TALK );
125 $this->setUserPerm( "" );
126 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
127 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
128
129 $this->setTitle( NS_MAIN );
130 $this->setUserPerm( "createpage" );
131 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
132 $this->assertEquals( [], $res );
133
134 $this->setTitle( NS_MAIN );
135 $this->setUserPerm( "createtalk" );
136 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
137 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
138
139 $this->setUser( $this->userName );
140 $this->setTitle( NS_TALK );
141 $this->setUserPerm( "createtalk" );
142 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
143 $this->assertEquals( [], $res );
144
145 $this->setTitle( NS_TALK );
146 $this->setUserPerm( "createpage" );
147 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
148 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
149
150 $this->setTitle( NS_TALK );
151 $this->setUserPerm( "" );
152 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
153 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
154
155 $this->setTitle( NS_MAIN );
156 $this->setUserPerm( "createpage" );
157 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
158 $this->assertEquals( [], $res );
159
160 $this->setTitle( NS_MAIN );
161 $this->setUserPerm( "createtalk" );
162 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
163 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
164
165 $this->setTitle( NS_MAIN );
166 $this->setUserPerm( "" );
167 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
168 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
169
170 $this->setUser( 'anon' );
171 $this->setTitle( NS_USER, $this->userName . '' );
172 $this->setUserPerm( "" );
173 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
174 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
175
176 $this->setTitle( NS_USER, $this->userName . '/subpage' );
177 $this->setUserPerm( "" );
178 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
179 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
180
181 $this->setTitle( NS_USER, $this->userName . '' );
182 $this->setUserPerm( "move-rootuserpages" );
183 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
184 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
185
186 $this->setTitle( NS_USER, $this->userName . '/subpage' );
187 $this->setUserPerm( "move-rootuserpages" );
188 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
189 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
190
191 $this->setTitle( NS_USER, $this->userName . '' );
192 $this->setUserPerm( "" );
193 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
194 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
195
196 $this->setTitle( NS_USER, $this->userName . '/subpage' );
197 $this->setUserPerm( "" );
198 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
199 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
200
201 $this->setTitle( NS_USER, $this->userName . '' );
202 $this->setUserPerm( "move-rootuserpages" );
203 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
204 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
205
206 $this->setTitle( NS_USER, $this->userName . '/subpage' );
207 $this->setUserPerm( "move-rootuserpages" );
208 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
209 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
210
211 $this->setUser( $this->userName );
212 $this->setTitle( NS_FILE, "img.png" );
213 $this->setUserPerm( "" );
214 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
215 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
216
217 $this->setTitle( NS_FILE, "img.png" );
218 $this->setUserPerm( "movefile" );
219 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
220 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
221
222 $this->setUser( 'anon' );
223 $this->setTitle( NS_FILE, "img.png" );
224 $this->setUserPerm( "" );
225 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
226 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
227
228 $this->setTitle( NS_FILE, "img.png" );
229 $this->setUserPerm( "movefile" );
230 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
231 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
232
233 $this->setUser( $this->userName );
234 $this->setUserPerm( "move" );
235 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
236
237 $this->setUserPerm( "" );
238 $this->runGroupPermissions(
239 'move',
240 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
241 );
242
243 $this->setUser( 'anon' );
244 $this->setUserPerm( "move" );
245 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
246
247 $this->setUserPerm( "" );
248 $this->runGroupPermissions(
249 'move',
250 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
251 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
252 );
253
254 if ( $this->isWikitextNS( NS_MAIN ) ) {
255 // NOTE: some content models don't allow moving
256 // @todo find a Wikitext namespace for testing
257
258 $this->setTitle( NS_MAIN );
259 $this->setUser( 'anon' );
260 $this->setUserPerm( "move" );
261 $this->runGroupPermissions( 'move', [] );
262
263 $this->setUserPerm( "" );
264 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
265 [ [ 'movenologintext' ] ] );
266
267 $this->setUser( $this->userName );
268 $this->setUserPerm( "" );
269 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
270
271 $this->setUserPerm( "move" );
272 $this->runGroupPermissions( 'move', [] );
273
274 $this->setUser( 'anon' );
275 $this->setUserPerm( 'move' );
276 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
277 $this->assertEquals( [], $res );
278
279 $this->setUserPerm( '' );
280 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
281 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
282 }
283
284 $this->setTitle( NS_USER );
285 $this->setUser( $this->userName );
286 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
287 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
288 $this->assertEquals( [], $res );
289
290 $this->setUserPerm( "move" );
291 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
292 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
293
294 $this->setUser( 'anon' );
295 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
296 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
297 $this->assertEquals( [], $res );
298
299 $this->setTitle( NS_USER, "User/subpage" );
300 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
301 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
302 $this->assertEquals( [], $res );
303
304 $this->setUserPerm( "move" );
305 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
306 $this->assertEquals( [], $res );
307
308 $this->setUser( 'anon' );
309 $check = [
310 'edit' => [
311 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
312 [ [ 'badaccess-group0' ] ],
313 [],
314 true
315 ],
316 'protect' => [
317 [ [
318 'badaccess-groups',
319 "[[$prefix:Administrators|Administrators]]", 1 ],
320 [ 'protect-cantedit'
321 ] ],
322 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
323 [ [ 'protect-cantedit' ] ],
324 false
325 ],
326 '' => [ [], [], [], true ]
327 ];
328
329 foreach ( [ "edit", "protect", "" ] as $action ) {
330 $this->setUserPerm( null );
331 $this->assertEquals( $check[$action][0],
332 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
333 $this->assertEquals( $check[$action][0],
334 $this->title->getUserPermissionsErrors( $action, $this->user, 'full' ) );
335 $this->assertEquals( $check[$action][0],
336 $this->title->getUserPermissionsErrors( $action, $this->user, 'secure' ) );
337
338 global $wgGroupPermissions;
339 $old = $wgGroupPermissions;
340 $wgGroupPermissions = [];
341
342 $this->assertEquals( $check[$action][1],
343 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
344 $this->assertEquals( $check[$action][1],
345 $this->title->getUserPermissionsErrors( $action, $this->user, 'full' ) );
346 $this->assertEquals( $check[$action][1],
347 $this->title->getUserPermissionsErrors( $action, $this->user, 'secure' ) );
348 $wgGroupPermissions = $old;
349
350 $this->setUserPerm( $action );
351 $this->assertEquals( $check[$action][2],
352 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
353 $this->assertEquals( $check[$action][2],
354 $this->title->getUserPermissionsErrors( $action, $this->user, 'full' ) );
355 $this->assertEquals( $check[$action][2],
356 $this->title->getUserPermissionsErrors( $action, $this->user, 'secure' ) );
357
358 $this->setUserPerm( $action );
359 $this->assertEquals( $check[$action][3],
360 $this->title->userCan( $action, $this->user, true ) );
361 $this->assertEquals( $check[$action][3],
362 $this->title->quickUserCan( $action, $this->user ) );
363 # count( User::getGroupsWithPermissions( $action ) ) < 1
364 }
365 }
366
367 protected function runGroupPermissions( $action, $result, $result2 = null ) {
368 global $wgGroupPermissions;
369
370 if ( $result2 === null ) {
371 $result2 = $result;
372 }
373
374 $wgGroupPermissions['autoconfirmed']['move'] = false;
375 $wgGroupPermissions['user']['move'] = false;
376 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
377 $this->assertEquals( $result, $res );
378
379 $wgGroupPermissions['autoconfirmed']['move'] = true;
380 $wgGroupPermissions['user']['move'] = false;
381 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
382 $this->assertEquals( $result2, $res );
383
384 $wgGroupPermissions['autoconfirmed']['move'] = true;
385 $wgGroupPermissions['user']['move'] = true;
386 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
387 $this->assertEquals( $result2, $res );
388
389 $wgGroupPermissions['autoconfirmed']['move'] = false;
390 $wgGroupPermissions['user']['move'] = true;
391 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
392 $this->assertEquals( $result2, $res );
393 }
394
395 /**
396 * @todo This test method should be split up into separate test methods and
397 * data providers
398 * @covers Title::checkSpecialsAndNSPermissions
399 */
400 public function testSpecialsAndNSPermissions() {
401 global $wgNamespaceProtection;
402 $this->setUser( $this->userName );
403
404 $this->setTitle( NS_SPECIAL );
405
406 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
407 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
408
409 $this->setTitle( NS_MAIN );
410 $this->setUserPerm( 'bogus' );
411 $this->assertEquals( [],
412 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
413
414 $this->setTitle( NS_MAIN );
415 $this->setUserPerm( '' );
416 $this->assertEquals( [ [ 'badaccess-group0' ] ],
417 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
418
419 $wgNamespaceProtection[NS_USER] = [ 'bogus' ];
420
421 $this->setTitle( NS_USER );
422 $this->setUserPerm( '' );
423 $this->assertEquals( [ [ 'badaccess-group0' ],
424 [ 'namespaceprotected', 'User', 'bogus' ] ],
425 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
426
427 $this->setTitle( NS_MEDIAWIKI );
428 $this->setUserPerm( 'bogus' );
429 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
430 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
431
432 $this->setTitle( NS_MEDIAWIKI );
433 $this->setUserPerm( 'bogus' );
434 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
435 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
436
437 $wgNamespaceProtection = null;
438
439 $this->setUserPerm( 'bogus' );
440 $this->assertEquals( [],
441 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
442 $this->assertEquals( true,
443 $this->title->userCan( 'bogus', $this->user ) );
444
445 $this->setUserPerm( '' );
446 $this->assertEquals( [ [ 'badaccess-group0' ] ],
447 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
448 $this->assertEquals( false,
449 $this->title->userCan( 'bogus', $this->user ) );
450 }
451
452 /**
453 * @todo This test method should be split up into separate test methods and
454 * data providers
455 * @covers Title::checkUserConfigPermissions
456 */
457 public function testJsConfigEditPermissions() {
458 $this->setUser( $this->userName );
459
460 $this->setTitle( NS_USER, $this->userName . '/test.js' );
461 $this->runConfigEditPermissions(
462 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
463
464 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
465 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
466 [ [ 'badaccess-group0' ] ],
467
468 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
469 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
470 [ [ 'badaccess-group0' ] ],
471 [ [ 'badaccess-groups' ] ]
472 );
473 }
474
475 /**
476 * @todo This test method should be split up into separate test methods and
477 * data providers
478 * @covers Title::checkUserConfigPermissions
479 */
480 public function testJsonConfigEditPermissions() {
481 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
482 getFormattedNsText( NS_PROJECT );
483 $this->setUser( $this->userName );
484
485 $this->setTitle( NS_USER, $this->userName . '/test.json' );
486 $this->runConfigEditPermissions(
487 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
488
489 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
490 [ [ 'badaccess-group0' ] ],
491 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
492
493 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
494 [ [ 'badaccess-group0' ] ],
495 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
496 [ [ 'badaccess-groups' ] ]
497 );
498 }
499
500 /**
501 * @todo This test method should be split up into separate test methods and
502 * data providers
503 * @covers Title::checkUserConfigPermissions
504 */
505 public function testCssConfigEditPermissions() {
506 $this->setUser( $this->userName );
507
508 $this->setTitle( NS_USER, $this->userName . '/test.css' );
509 $this->runConfigEditPermissions(
510 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
511
512 [ [ 'badaccess-group0' ] ],
513 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
514 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
515
516 [ [ 'badaccess-group0' ] ],
517 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
518 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
519 [ [ 'badaccess-groups' ] ]
520 );
521 }
522
523 /**
524 * @todo This test method should be split up into separate test methods and
525 * data providers
526 * @covers Title::checkUserConfigPermissions
527 */
528 public function testOtherJsConfigEditPermissions() {
529 $this->setUser( $this->userName );
530
531 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
532 $this->runConfigEditPermissions(
533 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
534
535 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
536 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
537 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
538
539 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
540 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
541 [ [ 'badaccess-group0' ] ],
542 [ [ 'badaccess-groups' ] ]
543 );
544 }
545
546 /**
547 * @todo This test method should be split up into separate test methods and
548 * data providers
549 * @covers Title::checkUserConfigPermissions
550 */
551 public function testOtherJsonConfigEditPermissions() {
552 $this->setUser( $this->userName );
553
554 $this->setTitle( NS_USER, $this->altUserName . '/test.json' );
555 $this->runConfigEditPermissions(
556 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
557
558 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
559 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
560 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
561
562 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
563 [ [ 'badaccess-group0' ] ],
564 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
565 [ [ 'badaccess-groups' ] ]
566 );
567 }
568
569 /**
570 * @todo This test method should be split up into separate test methods and
571 * data providers
572 * @covers Title::checkUserConfigPermissions
573 */
574 public function testOtherCssConfigEditPermissions() {
575 $this->setUser( $this->userName );
576
577 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
578 $this->runConfigEditPermissions(
579 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
580
581 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
582 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
583 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
584
585 [ [ 'badaccess-group0' ] ],
586 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
587 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
588 [ [ 'badaccess-groups' ] ]
589 );
590 }
591
592 /**
593 * @todo This test method should be split up into separate test methods and
594 * data providers
595 * @covers Title::checkUserConfigPermissions
596 */
597 public function testOtherNonConfigEditPermissions() {
598 $this->setUser( $this->userName );
599
600 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
601 $this->runConfigEditPermissions(
602 [ [ 'badaccess-group0' ] ],
603
604 [ [ 'badaccess-group0' ] ],
605 [ [ 'badaccess-group0' ] ],
606 [ [ 'badaccess-group0' ] ],
607
608 [ [ 'badaccess-group0' ] ],
609 [ [ 'badaccess-group0' ] ],
610 [ [ 'badaccess-group0' ] ],
611 [ [ 'badaccess-groups' ] ]
612 );
613 }
614
615 /**
616 * @todo This should use data providers like the other methods here.
617 * @covers Title::checkUserConfigPermissions
618 */
619 public function testPatrolActionConfigEditPermissions() {
620 $this->setUser( 'anon' );
621 $this->setTitle( NS_USER, 'ToPatrolOrNotToPatrol' );
622 $this->runConfigEditPermissions(
623 [ [ 'badaccess-group0' ] ],
624
625 [ [ 'badaccess-group0' ] ],
626 [ [ 'badaccess-group0' ] ],
627 [ [ 'badaccess-group0' ] ],
628
629 [ [ 'badaccess-group0' ] ],
630 [ [ 'badaccess-group0' ] ],
631 [ [ 'badaccess-group0' ] ],
632 [ [ 'badaccess-groups' ] ]
633 );
634 }
635
636 protected function runConfigEditPermissions(
637 $resultNone,
638 $resultMyCss,
639 $resultMyJson,
640 $resultMyJs,
641 $resultUserCss,
642 $resultUserJson,
643 $resultUserJs,
644 $resultPatrol
645 ) {
646 $this->setUserPerm( '' );
647 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
648 $this->assertEquals( $resultNone, $result );
649
650 $this->setUserPerm( 'editmyusercss' );
651 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
652 $this->assertEquals( $resultMyCss, $result );
653
654 $this->setUserPerm( 'editmyuserjson' );
655 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
656 $this->assertEquals( $resultMyJson, $result );
657
658 $this->setUserPerm( 'editmyuserjs' );
659 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
660 $this->assertEquals( $resultMyJs, $result );
661
662 $this->setUserPerm( 'editusercss' );
663 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
664 $this->assertEquals( $resultUserCss, $result );
665
666 $this->setUserPerm( 'edituserjson' );
667 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
668 $this->assertEquals( $resultUserJson, $result );
669
670 $this->setUserPerm( 'edituserjs' );
671 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
672 $this->assertEquals( $resultUserJs, $result );
673
674 $this->setUserPerm( '' );
675 $result = $this->title->getUserPermissionsErrors( 'patrol', $this->user );
676 $this->assertEquals( reset( $resultPatrol[0] ), reset( $result[0] ) );
677
678 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
679 $result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
680 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
681 }
682
683 /**
684 * @todo This test method should be split up into separate test methods and
685 * data providers
686 *
687 * This test is failing per T201776.
688 *
689 * @group Broken
690 * @covers Title::checkPageRestrictions
691 */
692 public function testPageRestrictions() {
693 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
694 getFormattedNsText( NS_PROJECT );
695
696 $this->setTitle( NS_MAIN );
697 $this->title->mRestrictionsLoaded = true;
698 $this->setUserPerm( "edit" );
699 $this->title->mRestrictions = [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
700
701 $this->assertEquals( [],
702 $this->title->getUserPermissionsErrors( 'edit',
703 $this->user ) );
704
705 $this->assertEquals( true,
706 $this->title->quickUserCan( 'edit', $this->user ) );
707 $this->title->mRestrictions = [ "edit" => [ 'bogus', "sysop", "protect", "" ],
708 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
709
710 $this->assertEquals( [ [ 'badaccess-group0' ],
711 [ 'protectedpagetext', 'bogus', 'bogus' ],
712 [ 'protectedpagetext', 'editprotected', 'bogus' ],
713 [ 'protectedpagetext', 'protect', 'bogus' ] ],
714 $this->title->getUserPermissionsErrors( 'bogus',
715 $this->user ) );
716 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
717 [ 'protectedpagetext', 'editprotected', 'edit' ],
718 [ 'protectedpagetext', 'protect', 'edit' ] ],
719 $this->title->getUserPermissionsErrors( 'edit',
720 $this->user ) );
721 $this->setUserPerm( "" );
722 $this->assertEquals( [ [ 'badaccess-group0' ],
723 [ 'protectedpagetext', 'bogus', 'bogus' ],
724 [ 'protectedpagetext', 'editprotected', 'bogus' ],
725 [ 'protectedpagetext', 'protect', 'bogus' ] ],
726 $this->title->getUserPermissionsErrors( 'bogus',
727 $this->user ) );
728 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
729 [ 'protectedpagetext', 'bogus', 'edit' ],
730 [ 'protectedpagetext', 'editprotected', 'edit' ],
731 [ 'protectedpagetext', 'protect', 'edit' ] ],
732 $this->title->getUserPermissionsErrors( 'edit',
733 $this->user ) );
734 $this->setUserPerm( [ "edit", "editprotected" ] );
735 $this->assertEquals( [ [ 'badaccess-group0' ],
736 [ 'protectedpagetext', 'bogus', 'bogus' ],
737 [ 'protectedpagetext', 'protect', 'bogus' ] ],
738 $this->title->getUserPermissionsErrors( 'bogus',
739 $this->user ) );
740 $this->assertEquals( [
741 [ 'protectedpagetext', 'bogus', 'edit' ],
742 [ 'protectedpagetext', 'protect', 'edit' ] ],
743 $this->title->getUserPermissionsErrors( 'edit',
744 $this->user ) );
745
746 $this->title->mCascadeRestriction = true;
747 $this->setUserPerm( "edit" );
748 $this->assertEquals( false,
749 $this->title->quickUserCan( 'bogus', $this->user ) );
750 $this->assertEquals( false,
751 $this->title->quickUserCan( 'edit', $this->user ) );
752 $this->assertEquals( [ [ 'badaccess-group0' ],
753 [ 'protectedpagetext', 'bogus', 'bogus' ],
754 [ 'protectedpagetext', 'editprotected', 'bogus' ],
755 [ 'protectedpagetext', 'protect', 'bogus' ] ],
756 $this->title->getUserPermissionsErrors( 'bogus',
757 $this->user ) );
758 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
759 [ 'protectedpagetext', 'editprotected', 'edit' ],
760 [ 'protectedpagetext', 'protect', 'edit' ] ],
761 $this->title->getUserPermissionsErrors( 'edit',
762 $this->user ) );
763
764 $this->setUserPerm( [ "edit", "editprotected" ] );
765 $this->assertEquals( false,
766 $this->title->quickUserCan( 'bogus', $this->user ) );
767 $this->assertEquals( false,
768 $this->title->quickUserCan( 'edit', $this->user ) );
769 $this->assertEquals( [ [ 'badaccess-group0' ],
770 [ 'protectedpagetext', 'bogus', 'bogus' ],
771 [ 'protectedpagetext', 'protect', 'bogus' ],
772 [ 'protectedpagetext', 'protect', 'bogus' ] ],
773 $this->title->getUserPermissionsErrors( 'bogus',
774 $this->user ) );
775 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
776 [ 'protectedpagetext', 'protect', 'edit' ],
777 [ 'protectedpagetext', 'protect', 'edit' ] ],
778 $this->title->getUserPermissionsErrors( 'edit',
779 $this->user ) );
780 }
781
782 /**
783 * @covers Title::checkCascadingSourcesRestrictions
784 */
785 public function testCascadingSourcesRestrictions() {
786 $this->setTitle( NS_MAIN, "test page" );
787 $this->setUserPerm( [ "edit", "bogus" ] );
788
789 $this->title->mCascadeSources = [
790 Title::makeTitle( NS_MAIN, "Bogus" ),
791 Title::makeTitle( NS_MAIN, "UnBogus" )
792 ];
793 $this->title->mCascadingRestrictions = [
794 "bogus" => [ 'bogus', "sysop", "protect", "" ]
795 ];
796
797 $this->assertEquals( false,
798 $this->title->userCan( 'bogus', $this->user ) );
799 $this->assertEquals( [
800 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
801 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
802 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
803 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
804
805 $this->assertEquals( true,
806 $this->title->userCan( 'edit', $this->user ) );
807 $this->assertEquals( [],
808 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
809 }
810
811 /**
812 * @todo This test method should be split up into separate test methods and
813 * data providers
814 * @covers Title::checkActionPermissions
815 */
816 public function testActionPermissions() {
817 $this->setUserPerm( [ "createpage" ] );
818 $this->setTitle( NS_MAIN, "test page" );
819 $this->title->mTitleProtection['permission'] = '';
820 $this->title->mTitleProtection['user'] = $this->user->getId();
821 $this->title->mTitleProtection['expiry'] = 'infinity';
822 $this->title->mTitleProtection['reason'] = 'test';
823 $this->title->mCascadeRestriction = false;
824
825 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
826 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
827 $this->assertEquals( false,
828 $this->title->userCan( 'create', $this->user ) );
829
830 $this->title->mTitleProtection['permission'] = 'editprotected';
831 $this->setUserPerm( [ 'createpage', 'protect' ] );
832 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
833 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
834 $this->assertEquals( false,
835 $this->title->userCan( 'create', $this->user ) );
836
837 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
838 $this->assertEquals( [],
839 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
840 $this->assertEquals( true,
841 $this->title->userCan( 'create', $this->user ) );
842
843 $this->setUserPerm( [ 'createpage' ] );
844 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
845 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
846 $this->assertEquals( false,
847 $this->title->userCan( 'create', $this->user ) );
848
849 $this->setTitle( NS_MEDIA, "test page" );
850 $this->setUserPerm( [ "move" ] );
851 $this->assertEquals( false,
852 $this->title->userCan( 'move', $this->user ) );
853 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
854 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
855
856 $this->setTitle( NS_HELP, "test page" );
857 $this->assertEquals( [],
858 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
859 $this->assertEquals( true,
860 $this->title->userCan( 'move', $this->user ) );
861
862 $this->title->mInterwiki = "no";
863 $this->assertEquals( [ [ 'immobile-source-page' ] ],
864 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
865 $this->assertEquals( false,
866 $this->title->userCan( 'move', $this->user ) );
867
868 $this->setTitle( NS_MEDIA, "test page" );
869 $this->assertEquals( false,
870 $this->title->userCan( 'move-target', $this->user ) );
871 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
872 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
873
874 $this->setTitle( NS_HELP, "test page" );
875 $this->assertEquals( [],
876 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
877 $this->assertEquals( true,
878 $this->title->userCan( 'move-target', $this->user ) );
879
880 $this->title->mInterwiki = "no";
881 $this->assertEquals( [ [ 'immobile-target-page' ] ],
882 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
883 $this->assertEquals( false,
884 $this->title->userCan( 'move-target', $this->user ) );
885 }
886
887 /**
888 * @covers Title::checkUserBlock
889 */
890 public function testUserBlock() {
891 $this->setMwGlobals( [
892 'wgEmailConfirmToEdit' => true,
893 'wgEmailAuthentication' => true,
894 ] );
895
896 $this->setUserPerm( [ 'createpage', 'edit', 'move', 'rollback', 'patrol', 'upload', 'purge' ] );
897 $this->setTitle( NS_HELP, "test page" );
898
899 # $wgEmailConfirmToEdit only applies to 'edit' action
900 $this->assertEquals( [],
901 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
902 $this->assertContains( [ 'confirmedittext' ],
903 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
904
905 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
906 $this->assertNotContains( [ 'confirmedittext' ],
907 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
908
909 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
910 $this->assertEquals( [],
911 $this->title->getUserPermissionsErrors( 'move-target',
912 $this->user ) );
913
914 global $wgLang;
915 $prev = time();
916 $now = time() + 120;
917 $this->user->mBlockedby = $this->user->getId();
918 $this->user->mBlock = new Block( [
919 'address' => '127.0.8.1',
920 'by' => $this->user->getId(),
921 'reason' => 'no reason given',
922 'timestamp' => $prev + 3600,
923 'auto' => true,
924 'expiry' => 0
925 ] );
926 $this->user->mBlock->mTimestamp = 0;
927 $this->assertEquals( [ [ 'autoblockedtext',
928 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
929 'Useruser', null, 'infinite', '127.0.8.1',
930 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ] ],
931 $this->title->getUserPermissionsErrors( 'move-target',
932 $this->user ) );
933
934 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
935 // quickUserCan should ignore user blocks
936 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
937
938 global $wgLocalTZoffset;
939 $wgLocalTZoffset = -60;
940 $this->user->mBlockedby = $this->user->getName();
941 $this->user->mBlock = new Block( [
942 'address' => '127.0.8.1',
943 'by' => $this->user->getId(),
944 'reason' => 'no reason given',
945 'timestamp' => $now,
946 'auto' => false,
947 'expiry' => 10,
948 ] );
949 $this->assertEquals( [ [ 'blockedtext',
950 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
951 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
952 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ],
953 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
954 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
955 # $user->blockedFor() == ''
956 # $user->mBlock->mExpiry == 'infinity'
957
958 $this->user->mBlockedby = $this->user->getName();
959 $this->user->mBlock = new Block( [
960 'address' => '127.0.8.1',
961 'by' => $this->user->getId(),
962 'reason' => 'no reason given',
963 'timestamp' => $now,
964 'auto' => false,
965 'expiry' => 10,
966 'systemBlock' => 'test',
967 ] );
968
969 $errors = [ [ 'systemblockedtext',
970 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
971 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
972 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ];
973
974 $this->assertEquals( $errors,
975 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
976 $this->assertEquals( $errors,
977 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
978 $this->assertEquals( $errors,
979 $this->title->getUserPermissionsErrors( 'rollback', $this->user ) );
980 $this->assertEquals( $errors,
981 $this->title->getUserPermissionsErrors( 'patrol', $this->user ) );
982 $this->assertEquals( $errors,
983 $this->title->getUserPermissionsErrors( 'upload', $this->user ) );
984 $this->assertEquals( [],
985 $this->title->getUserPermissionsErrors( 'purge', $this->user ) );
986
987 // partial block message test
988 $this->user->mBlockedby = $this->user->getName();
989 $this->user->mBlock = new Block( [
990 'address' => '127.0.8.1',
991 'by' => $this->user->getId(),
992 'reason' => 'no reason given',
993 'timestamp' => $now,
994 'sitewide' => false,
995 'expiry' => 10,
996 ] );
997
998 $this->assertEquals( [],
999 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
1000 $this->assertEquals( [],
1001 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
1002 $this->assertEquals( [],
1003 $this->title->getUserPermissionsErrors( 'rollback', $this->user ) );
1004 $this->assertEquals( [],
1005 $this->title->getUserPermissionsErrors( 'patrol', $this->user ) );
1006 $this->assertEquals( [],
1007 $this->title->getUserPermissionsErrors( 'upload', $this->user ) );
1008 $this->assertEquals( [],
1009 $this->title->getUserPermissionsErrors( 'purge', $this->user ) );
1010
1011 $this->user->mBlock->setRestrictions( [
1012 ( new PageRestriction( 0, $this->title->getArticleID() ) )->setTitle( $this->title ),
1013 ] );
1014
1015 $errors = [ [ 'blockedtext-partial',
1016 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1017 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1018 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ];
1019
1020 $this->assertEquals( $errors,
1021 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
1022 $this->assertEquals( $errors,
1023 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
1024 $this->assertEquals( $errors,
1025 $this->title->getUserPermissionsErrors( 'rollback', $this->user ) );
1026 $this->assertEquals( $errors,
1027 $this->title->getUserPermissionsErrors( 'patrol', $this->user ) );
1028 $this->assertEquals( [],
1029 $this->title->getUserPermissionsErrors( 'upload', $this->user ) );
1030 $this->assertEquals( [],
1031 $this->title->getUserPermissionsErrors( 'purge', $this->user ) );
1032 }
1033 }