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