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