MediaWiki REL1_33
PermissionManagerTest.php
Go to the documentation of this file.
1<?php
2
4
10use User;
15
22
27
31 protected $title;
32
37
42
44 const USER_TALK_PAGE = '<user talk page>';
45
46 protected function setUp() {
47 parent::setUp();
48
49 $localZone = 'UTC';
50 $localOffset = date( 'Z' ) / 60;
51
52 $this->setMwGlobals( [
53 'wgLocaltimezone' => $localZone,
54 'wgLocalTZoffset' => $localOffset,
55 'wgNamespaceProtection' => [
56 NS_MEDIAWIKI => 'editinterface',
57 ],
58 ] );
59 // Without this testUserBlock will use a non-English context on non-English MediaWiki
60 // installations (because of how Title::checkUserBlock is implemented) and fail.
61 RequestContext::resetMain();
62
63 $this->userName = 'Useruser';
64 $this->altUserName = 'Altuseruser';
65 date_default_timezone_set( $localZone );
66
67 $this->title = Title::makeTitle( NS_MAIN, "Main Page" );
68 if ( !isset( $this->userUser ) || !( $this->userUser instanceof User ) ) {
69 $this->userUser = User::newFromName( $this->userName );
70
71 if ( !$this->userUser->getId() ) {
72 $this->userUser = User::createNew( $this->userName, [
73 "email" => "test@example.com",
74 "real_name" => "Test User" ] );
75 $this->userUser->load();
76 }
77
78 $this->altUser = User::newFromName( $this->altUserName );
79 if ( !$this->altUser->getId() ) {
80 $this->altUser = User::createNew( $this->altUserName, [
81 "email" => "alttest@example.com",
82 "real_name" => "Test User Alt" ] );
83 $this->altUser->load();
84 }
85
86 $this->anonUser = User::newFromId( 0 );
87
88 $this->user = $this->userUser;
89 }
90
91 $this->permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
92
93 $this->overrideMwServices();
94 }
95
96 protected function setUserPerm( $perm ) {
97 // Setting member variables is evil!!!
98
99 if ( is_array( $perm ) ) {
100 $this->user->mRights = $perm;
101 } else {
102 $this->user->mRights = [ $perm ];
103 }
104 }
105
106 protected function setTitle( $ns, $title = "Main_Page" ) {
107 $this->title = Title::makeTitle( $ns, $title );
108 }
109
110 protected function setUser( $userName = null ) {
111 if ( $userName === 'anon' ) {
112 $this->user = $this->anonUser;
113 } elseif ( $userName === null || $userName === $this->userName ) {
114 $this->user = $this->userUser;
115 } else {
116 $this->user = $this->altUser;
117 }
118 }
119
129 public function testQuickPermissions() {
130 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
131 getFormattedNsText( NS_PROJECT );
132
133 $this->setUser( 'anon' );
134 $this->setTitle( NS_TALK );
135 $this->setUserPerm( "createtalk" );
136 $res = $this->permissionManager
137 ->getPermissionErrors( 'create', $this->user, $this->title );
138 $this->assertEquals( [], $res );
139
140 $this->setTitle( NS_TALK );
141 $this->setUserPerm( "createpage" );
142 $res = $this->permissionManager
143 ->getPermissionErrors( 'create', $this->user, $this->title );
144 $this->assertEquals( [ [ "nocreatetext" ] ], $res );
145
146 $this->setTitle( NS_TALK );
147 $this->setUserPerm( "" );
148 $res = $this->permissionManager
149 ->getPermissionErrors( 'create', $this->user, $this->title );
150 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
151
152 $this->setTitle( NS_MAIN );
153 $this->setUserPerm( "createpage" );
154 $res = $this->permissionManager
155 ->getPermissionErrors( 'create', $this->user, $this->title );
156 $this->assertEquals( [], $res );
157
158 $this->setTitle( NS_MAIN );
159 $this->setUserPerm( "createtalk" );
160 $res = $this->permissionManager
161 ->getPermissionErrors( 'create', $this->user, $this->title );
162 $this->assertEquals( [ [ 'nocreatetext' ] ], $res );
163
164 $this->setUser( $this->userName );
165 $this->setTitle( NS_TALK );
166 $this->setUserPerm( "createtalk" );
167 $res = $this->permissionManager
168 ->getPermissionErrors( 'create', $this->user, $this->title );
169 $this->assertEquals( [], $res );
170
171 $this->setTitle( NS_TALK );
172 $this->setUserPerm( "createpage" );
173 $res = $this->permissionManager
174 ->getPermissionErrors( 'create', $this->user, $this->title );
175 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
176
177 $this->setTitle( NS_TALK );
178 $this->setUserPerm( "" );
179 $res = $this->permissionManager
180 ->getPermissionErrors( 'create', $this->user, $this->title );
181 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
182
183 $this->setTitle( NS_MAIN );
184 $this->setUserPerm( "createpage" );
185 $res = $this->permissionManager
186 ->getPermissionErrors( 'create', $this->user, $this->title );
187 $this->assertEquals( [], $res );
188
189 $this->setTitle( NS_MAIN );
190 $this->setUserPerm( "createtalk" );
191 $res = $this->permissionManager
192 ->getPermissionErrors( 'create', $this->user, $this->title );
193 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
194
195 $this->setTitle( NS_MAIN );
196 $this->setUserPerm( "" );
197 $res = $this->permissionManager
198 ->getPermissionErrors( 'create', $this->user, $this->title );
199 $this->assertEquals( [ [ 'nocreate-loggedin' ] ], $res );
200
201 $this->setUser( 'anon' );
202 $this->setTitle( NS_USER, $this->userName . '' );
203 $this->setUserPerm( "" );
204 $res = $this->permissionManager
205 ->getPermissionErrors( 'move', $this->user, $this->title );
206 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
207
208 $this->setTitle( NS_USER, $this->userName . '/subpage' );
209 $this->setUserPerm( "" );
210 $res = $this->permissionManager
211 ->getPermissionErrors( 'move', $this->user, $this->title );
212 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
213
214 $this->setTitle( NS_USER, $this->userName . '' );
215 $this->setUserPerm( "move-rootuserpages" );
216 $res = $this->permissionManager
217 ->getPermissionErrors( 'move', $this->user, $this->title );
218 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
219
220 $this->setTitle( NS_USER, $this->userName . '/subpage' );
221 $this->setUserPerm( "move-rootuserpages" );
222 $res = $this->permissionManager
223 ->getPermissionErrors( 'move', $this->user, $this->title );
224 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
225
226 $this->setTitle( NS_USER, $this->userName . '' );
227 $this->setUserPerm( "" );
228 $res = $this->permissionManager
229 ->getPermissionErrors( 'move', $this->user, $this->title );
230 $this->assertEquals( [ [ 'cant-move-user-page' ], [ 'movenologintext' ] ], $res );
231
232 $this->setTitle( NS_USER, $this->userName . '/subpage' );
233 $this->setUserPerm( "" );
234 $res = $this->permissionManager
235 ->getPermissionErrors( 'move', $this->user, $this->title );
236 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
237
238 $this->setTitle( NS_USER, $this->userName . '' );
239 $this->setUserPerm( "move-rootuserpages" );
240 $res = $this->permissionManager
241 ->getPermissionErrors( 'move', $this->user, $this->title );
242 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
243
244 $this->setTitle( NS_USER, $this->userName . '/subpage' );
245 $this->setUserPerm( "move-rootuserpages" );
246 $res = $this->permissionManager
247 ->getPermissionErrors( 'move', $this->user, $this->title );
248 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
249
250 $this->setUser( $this->userName );
251 $this->setTitle( NS_FILE, "img.png" );
252 $this->setUserPerm( "" );
253 $res = $this->permissionManager
254 ->getPermissionErrors( 'move', $this->user, $this->title );
255 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ], $res );
256
257 $this->setTitle( NS_FILE, "img.png" );
258 $this->setUserPerm( "movefile" );
259 $res = $this->permissionManager
260 ->getPermissionErrors( 'move', $this->user, $this->title );
261 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
262
263 $this->setUser( 'anon' );
264 $this->setTitle( NS_FILE, "img.png" );
265 $this->setUserPerm( "" );
266 $res = $this->permissionManager
267 ->getPermissionErrors( 'move', $this->user, $this->title );
268 $this->assertEquals( [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ], $res );
269
270 $this->setTitle( NS_FILE, "img.png" );
271 $this->setUserPerm( "movefile" );
272 $res = $this->permissionManager
273 ->getPermissionErrors( 'move', $this->user, $this->title );
274 $this->assertEquals( [ [ 'movenologintext' ] ], $res );
275
276 $this->setUser( $this->userName );
277 $this->setUserPerm( "move" );
278 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
279
280 $this->setUserPerm( "" );
281 $this->runGroupPermissions(
282 'move',
283 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ]
284 );
285
286 $this->setUser( 'anon' );
287 $this->setUserPerm( "move" );
288 $this->runGroupPermissions( 'move', [ [ 'movenotallowedfile' ] ] );
289
290 $this->setUserPerm( "" );
291 $this->runGroupPermissions(
292 'move',
293 [ [ 'movenotallowedfile' ], [ 'movenotallowed' ] ],
294 [ [ 'movenotallowedfile' ], [ 'movenologintext' ] ]
295 );
296
297 if ( $this->isWikitextNS( NS_MAIN ) ) {
298 // NOTE: some content models don't allow moving
299 // @todo find a Wikitext namespace for testing
300
301 $this->setTitle( NS_MAIN );
302 $this->setUser( 'anon' );
303 $this->setUserPerm( "move" );
304 $this->runGroupPermissions( 'move', [] );
305
306 $this->setUserPerm( "" );
307 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ],
308 [ [ 'movenologintext' ] ] );
309
310 $this->setUser( $this->userName );
311 $this->setUserPerm( "" );
312 $this->runGroupPermissions( 'move', [ [ 'movenotallowed' ] ] );
313
314 $this->setUserPerm( "move" );
315 $this->runGroupPermissions( 'move', [] );
316
317 $this->setUser( 'anon' );
318 $this->setUserPerm( 'move' );
319 $res = $this->permissionManager
320 ->getPermissionErrors( 'move-target', $this->user, $this->title );
321 $this->assertEquals( [], $res );
322
323 $this->setUserPerm( '' );
324 $res = $this->permissionManager
325 ->getPermissionErrors( 'move-target', $this->user, $this->title );
326 $this->assertEquals( [ [ 'movenotallowed' ] ], $res );
327 }
328
329 $this->setTitle( NS_USER );
330 $this->setUser( $this->userName );
331 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
332 $res = $this->permissionManager
333 ->getPermissionErrors( 'move-target', $this->user, $this->title );
334 $this->assertEquals( [], $res );
335
336 $this->setUserPerm( "move" );
337 $res = $this->permissionManager
338 ->getPermissionErrors( 'move-target', $this->user, $this->title );
339 $this->assertEquals( [ [ 'cant-move-to-user-page' ] ], $res );
340
341 $this->setUser( 'anon' );
342 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
343 $res = $this->permissionManager
344 ->getPermissionErrors( 'move-target', $this->user, $this->title );
345 $this->assertEquals( [], $res );
346
347 $this->setTitle( NS_USER, "User/subpage" );
348 $this->setUserPerm( [ "move", "move-rootuserpages" ] );
349 $res = $this->permissionManager
350 ->getPermissionErrors( 'move-target', $this->user, $this->title );
351 $this->assertEquals( [], $res );
352
353 $this->setUserPerm( "move" );
354 $res = $this->permissionManager
355 ->getPermissionErrors( 'move-target', $this->user, $this->title );
356 $this->assertEquals( [], $res );
357
358 $this->setUser( 'anon' );
359 $check = [
360 'edit' => [
361 [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ] ],
362 [ [ 'badaccess-group0' ] ],
363 [],
364 true
365 ],
366 'protect' => [
367 [ [
368 'badaccess-groups',
369 "[[$prefix:Administrators|Administrators]]", 1 ],
370 [ 'protect-cantedit'
371 ] ],
372 [ [ 'badaccess-group0' ], [ 'protect-cantedit' ] ],
373 [ [ 'protect-cantedit' ] ],
374 false
375 ],
376 '' => [ [], [], [], true ]
377 ];
378
379 foreach ( [ "edit", "protect", "" ] as $action ) {
380 $this->setUserPerm( null );
381 $this->assertEquals( $check[$action][0],
382 $this->permissionManager
383 ->getPermissionErrors( $action, $this->user, $this->title, true ) );
384 $this->assertEquals( $check[$action][0],
385 $this->permissionManager
386 ->getPermissionErrors( $action, $this->user, $this->title, 'full' ) );
387 $this->assertEquals( $check[$action][0],
388 $this->permissionManager
389 ->getPermissionErrors( $action, $this->user, $this->title, 'secure' ) );
390
391 global $wgGroupPermissions;
392 $old = $wgGroupPermissions;
394
395 $this->assertEquals( $check[$action][1],
396 $this->permissionManager
397 ->getPermissionErrors( $action, $this->user, $this->title, true ) );
398 $this->assertEquals( $check[$action][1],
399 $this->permissionManager
400 ->getPermissionErrors( $action, $this->user, $this->title, 'full' ) );
401 $this->assertEquals( $check[$action][1],
402 $this->permissionManager
403 ->getPermissionErrors( $action, $this->user, $this->title, 'secure' ) );
404 $wgGroupPermissions = $old;
405
406 $this->setUserPerm( $action );
407 $this->assertEquals( $check[$action][2],
408 $this->permissionManager
409 ->getPermissionErrors( $action, $this->user, $this->title, true ) );
410 $this->assertEquals( $check[$action][2],
411 $this->permissionManager
412 ->getPermissionErrors( $action, $this->user, $this->title, 'full' ) );
413 $this->assertEquals( $check[$action][2],
414 $this->permissionManager
415 ->getPermissionErrors( $action, $this->user, $this->title, 'secure' ) );
416
417 $this->setUserPerm( $action );
418 $this->assertEquals( $check[$action][3],
419 $this->permissionManager->userCan( $action, $this->user, $this->title, true ) );
420 $this->assertEquals( $check[$action][3],
421 $this->permissionManager->userCan( $action, $this->user, $this->title,
422 PermissionManager::RIGOR_QUICK ) );
423 # count( User::getGroupsWithPermissions( $action ) ) < 1
424 }
425 }
426
427 protected function runGroupPermissions( $action, $result, $result2 = null ) {
428 global $wgGroupPermissions;
429
430 if ( $result2 === null ) {
431 $result2 = $result;
432 }
433
434 $wgGroupPermissions['autoconfirmed']['move'] = false;
435 $wgGroupPermissions['user']['move'] = false;
436 $res = $this->permissionManager
437 ->getPermissionErrors( $action, $this->user, $this->title );
438 $this->assertEquals( $result, $res );
439
440 $wgGroupPermissions['autoconfirmed']['move'] = true;
441 $wgGroupPermissions['user']['move'] = false;
442 $res = $this->permissionManager
443 ->getPermissionErrors( $action, $this->user, $this->title );
444 $this->assertEquals( $result2, $res );
445
446 $wgGroupPermissions['autoconfirmed']['move'] = true;
447 $wgGroupPermissions['user']['move'] = true;
448 $res = $this->permissionManager
449 ->getPermissionErrors( $action, $this->user, $this->title );
450 $this->assertEquals( $result2, $res );
451
452 $wgGroupPermissions['autoconfirmed']['move'] = false;
453 $wgGroupPermissions['user']['move'] = true;
454 $res = $this->permissionManager
455 ->getPermissionErrors( $action, $this->user, $this->title );
456 $this->assertEquals( $result2, $res );
457 }
458
466 $this->setUser( $this->userName );
467
468 $this->setTitle( NS_SPECIAL );
469
470 $this->assertEquals( [ [ 'badaccess-group0' ], [ 'ns-specialprotected' ] ],
471 $this->permissionManager
472 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
473
474 $this->setTitle( NS_MAIN );
475 $this->setUserPerm( 'bogus' );
476 $this->assertEquals( [],
477 $this->permissionManager
478 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
479
480 $this->setTitle( NS_MAIN );
481 $this->setUserPerm( '' );
482 $this->assertEquals( [ [ 'badaccess-group0' ] ],
483 $this->permissionManager
484 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
485
486 $wgNamespaceProtection[NS_USER] = [ 'bogus' ];
487
488 $this->setTitle( NS_USER );
489 $this->setUserPerm( '' );
490 $this->assertEquals( [ [ 'badaccess-group0' ],
491 [ 'namespaceprotected', 'User', 'bogus' ] ],
492 $this->permissionManager
493 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
494
495 $this->setTitle( NS_MEDIAWIKI );
496 $this->setUserPerm( 'bogus' );
497 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
498 $this->permissionManager
499 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
500
501 $this->setTitle( NS_MEDIAWIKI );
502 $this->setUserPerm( 'bogus' );
503 $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
504 $this->permissionManager
505 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
506
507 $wgNamespaceProtection = null;
508
509 $this->setUserPerm( 'bogus' );
510 $this->assertEquals( [],
511 $this->permissionManager
512 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
513 $this->assertEquals( true,
514 $this->permissionManager->userCan( 'bogus', $this->user, $this->title ) );
515
516 $this->setUserPerm( '' );
517 $this->assertEquals( [ [ 'badaccess-group0' ] ],
518 $this->permissionManager
519 ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
520 $this->assertEquals( false,
521 $this->permissionManager->userCan( 'bogus', $this->user, $this->title ) );
522 }
523
529 public function testJsConfigEditPermissions() {
530 $this->setUser( $this->userName );
531
532 $this->setTitle( NS_USER, $this->userName . '/test.js' );
534 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
535
536 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
537 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
538 [ [ 'badaccess-group0' ] ],
539
540 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
541 [ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
542 [ [ 'badaccess-group0' ] ],
543 [ [ 'badaccess-groups' ] ]
544 );
545 }
546
553 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
554 getFormattedNsText( NS_PROJECT );
555 $this->setUser( $this->userName );
556
557 $this->setTitle( NS_USER, $this->userName . '/test.json' );
559 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
560
561 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
562 [ [ 'badaccess-group0' ] ],
563 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
564
565 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
566 [ [ 'badaccess-group0' ] ],
567 [ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
568 [ [ 'badaccess-groups' ] ]
569 );
570 }
571
578 $this->setUser( $this->userName );
579
580 $this->setTitle( NS_USER, $this->userName . '/test.css' );
582 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
583
584 [ [ 'badaccess-group0' ] ],
585 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
586 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
587
588 [ [ 'badaccess-group0' ] ],
589 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
590 [ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
591 [ [ 'badaccess-groups' ] ]
592 );
593 }
594
601 $this->setUser( $this->userName );
602
603 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
605 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
606
607 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
608 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
609 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
610
611 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
612 [ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
613 [ [ 'badaccess-group0' ] ],
614 [ [ 'badaccess-groups' ] ]
615 );
616 }
617
624 $this->setUser( $this->userName );
625
626 $this->setTitle( NS_USER, $this->altUserName . '/test.json' );
628 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
629
630 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
631 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
632 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
633
634 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
635 [ [ 'badaccess-group0' ] ],
636 [ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
637 [ [ 'badaccess-groups' ] ]
638 );
639 }
640
647 $this->setUser( $this->userName );
648
649 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
651 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
652
653 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
654 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
655 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
656
657 [ [ 'badaccess-group0' ] ],
658 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
659 [ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
660 [ [ 'badaccess-groups' ] ]
661 );
662 }
663
670 $this->setUser( $this->userName );
671
672 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
674 [ [ 'badaccess-group0' ] ],
675
676 [ [ 'badaccess-group0' ] ],
677 [ [ 'badaccess-group0' ] ],
678 [ [ 'badaccess-group0' ] ],
679
680 [ [ 'badaccess-group0' ] ],
681 [ [ 'badaccess-group0' ] ],
682 [ [ 'badaccess-group0' ] ],
683 [ [ 'badaccess-groups' ] ]
684 );
685 }
686
692 $this->setUser( 'anon' );
693 $this->setTitle( NS_USER, 'ToPatrolOrNotToPatrol' );
695 [ [ 'badaccess-group0' ] ],
696
697 [ [ 'badaccess-group0' ] ],
698 [ [ 'badaccess-group0' ] ],
699 [ [ 'badaccess-group0' ] ],
700
701 [ [ 'badaccess-group0' ] ],
702 [ [ 'badaccess-group0' ] ],
703 [ [ 'badaccess-group0' ] ],
704 [ [ 'badaccess-groups' ] ]
705 );
706 }
707
708 protected function runConfigEditPermissions(
709 $resultNone,
710 $resultMyCss,
711 $resultMyJson,
712 $resultMyJs,
713 $resultUserCss,
714 $resultUserJson,
715 $resultUserJs,
716 $resultPatrol
717 ) {
718 $this->setUserPerm( '' );
719 $result = $this->permissionManager
720 ->getPermissionErrors( 'bogus', $this->user, $this->title );
721 $this->assertEquals( $resultNone, $result );
722
723 $this->setUserPerm( 'editmyusercss' );
724 $result = $this->permissionManager
725 ->getPermissionErrors( 'bogus', $this->user, $this->title );
726 $this->assertEquals( $resultMyCss, $result );
727
728 $this->setUserPerm( 'editmyuserjson' );
729 $result = $this->permissionManager
730 ->getPermissionErrors( 'bogus', $this->user, $this->title );
731 $this->assertEquals( $resultMyJson, $result );
732
733 $this->setUserPerm( 'editmyuserjs' );
734 $result = $this->permissionManager
735 ->getPermissionErrors( 'bogus', $this->user, $this->title );
736 $this->assertEquals( $resultMyJs, $result );
737
738 $this->setUserPerm( 'editusercss' );
739 $result = $this->permissionManager
740 ->getPermissionErrors( 'bogus', $this->user, $this->title );
741 $this->assertEquals( $resultUserCss, $result );
742
743 $this->setUserPerm( 'edituserjson' );
744 $result = $this->permissionManager
745 ->getPermissionErrors( 'bogus', $this->user, $this->title );
746 $this->assertEquals( $resultUserJson, $result );
747
748 $this->setUserPerm( 'edituserjs' );
749 $result = $this->permissionManager
750 ->getPermissionErrors( 'bogus', $this->user, $this->title );
751 $this->assertEquals( $resultUserJs, $result );
752
753 $this->setUserPerm( '' );
754 $result = $this->permissionManager
755 ->getPermissionErrors( 'patrol', $this->user, $this->title );
756 $this->assertEquals( reset( $resultPatrol[0] ), reset( $result[0] ) );
757
758 $this->setUserPerm( [ 'edituserjs', 'edituserjson', 'editusercss' ] );
759 $result = $this->permissionManager
760 ->getPermissionErrors( 'bogus', $this->user, $this->title );
761 $this->assertEquals( [ [ 'badaccess-group0' ] ], $result );
762 }
763
773 public function testPageRestrictions() {
774 $prefix = MediaWikiServices::getInstance()->getContentLanguage()->
775 getFormattedNsText( NS_PROJECT );
776
777 $this->setTitle( NS_MAIN );
778 $this->title->mRestrictionsLoaded = true;
779 $this->setUserPerm( "edit" );
780 $this->title->mRestrictions = [ "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
781
782 $this->assertEquals( [],
783 $this->permissionManager->getPermissionErrors( 'edit',
784 $this->user, $this->title ) );
785
786 $this->assertEquals( true,
787 $this->permissionManager->userCan( 'edit', $this->user, $this->title,
788 PermissionManager::RIGOR_QUICK ) );
789
790 $this->title->mRestrictions = [ "edit" => [ 'bogus', "sysop", "protect", "" ],
791 "bogus" => [ 'bogus', "sysop", "protect", "" ] ];
792
793 $this->assertEquals( [ [ 'badaccess-group0' ],
794 [ 'protectedpagetext', 'bogus', 'bogus' ],
795 [ 'protectedpagetext', 'editprotected', 'bogus' ],
796 [ 'protectedpagetext', 'protect', 'bogus' ] ],
797 $this->permissionManager->getPermissionErrors( 'bogus',
798 $this->user, $this->title ) );
799 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
800 [ 'protectedpagetext', 'editprotected', 'edit' ],
801 [ 'protectedpagetext', 'protect', 'edit' ] ],
802 $this->permissionManager->getPermissionErrors( 'edit',
803 $this->user, $this->title ) );
804 $this->setUserPerm( "" );
805 $this->assertEquals( [ [ 'badaccess-group0' ],
806 [ 'protectedpagetext', 'bogus', 'bogus' ],
807 [ 'protectedpagetext', 'editprotected', 'bogus' ],
808 [ 'protectedpagetext', 'protect', 'bogus' ] ],
809 $this->permissionManager->getPermissionErrors( 'bogus',
810 $this->user, $this->title ) );
811 $this->assertEquals( [ [ 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ],
812 [ 'protectedpagetext', 'bogus', 'edit' ],
813 [ 'protectedpagetext', 'editprotected', 'edit' ],
814 [ 'protectedpagetext', 'protect', 'edit' ] ],
815 $this->permissionManager->getPermissionErrors( 'edit',
816 $this->user, $this->title ) );
817 $this->setUserPerm( [ "edit", "editprotected" ] );
818 $this->assertEquals( [ [ 'badaccess-group0' ],
819 [ 'protectedpagetext', 'bogus', 'bogus' ],
820 [ 'protectedpagetext', 'protect', 'bogus' ] ],
821 $this->permissionManager->getPermissionErrors( 'bogus',
822 $this->user, $this->title ) );
823 $this->assertEquals( [
824 [ 'protectedpagetext', 'bogus', 'edit' ],
825 [ 'protectedpagetext', 'protect', 'edit' ] ],
826 $this->permissionManager->getPermissionErrors( 'edit',
827 $this->user, $this->title ) );
828
829 $this->title->mCascadeRestriction = true;
830 $this->setUserPerm( "edit" );
831
832 $this->assertEquals( false,
833 $this->permissionManager->userCan( 'bogus', $this->user, $this->title,
834 PermissionManager::RIGOR_QUICK ) );
835
836 $this->assertEquals( false,
837 $this->permissionManager->userCan( 'edit', $this->user, $this->title,
838 PermissionManager::RIGOR_QUICK ) );
839
840 $this->assertEquals( [ [ 'badaccess-group0' ],
841 [ 'protectedpagetext', 'bogus', 'bogus' ],
842 [ 'protectedpagetext', 'editprotected', 'bogus' ],
843 [ 'protectedpagetext', 'protect', 'bogus' ] ],
844 $this->permissionManager->getPermissionErrors( 'bogus',
845 $this->user, $this->title ) );
846 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
847 [ 'protectedpagetext', 'editprotected', 'edit' ],
848 [ 'protectedpagetext', 'protect', 'edit' ] ],
849 $this->permissionManager->getPermissionErrors( 'edit',
850 $this->user, $this->title ) );
851
852 $this->setUserPerm( [ "edit", "editprotected" ] );
853 $this->assertEquals( false,
854 $this->permissionManager->userCan( 'bogus', $this->user, $this->title,
855 PermissionManager::RIGOR_QUICK ) );
856
857 $this->assertEquals( false,
858 $this->permissionManager->userCan( 'edit', $this->user, $this->title,
859 PermissionManager::RIGOR_QUICK ) );
860
861 $this->assertEquals( [ [ 'badaccess-group0' ],
862 [ 'protectedpagetext', 'bogus', 'bogus' ],
863 [ 'protectedpagetext', 'protect', 'bogus' ],
864 [ 'protectedpagetext', 'protect', 'bogus' ] ],
865 $this->permissionManager->getPermissionErrors( 'bogus',
866 $this->user, $this->title ) );
867 $this->assertEquals( [ [ 'protectedpagetext', 'bogus', 'edit' ],
868 [ 'protectedpagetext', 'protect', 'edit' ],
869 [ 'protectedpagetext', 'protect', 'edit' ] ],
870 $this->permissionManager->getPermissionErrors( 'edit',
871 $this->user, $this->title ) );
872 }
873
878 $this->setTitle( NS_MAIN, "test page" );
879 $this->setUserPerm( [ "edit", "bogus" ] );
880
881 $this->title->mCascadeSources = [
882 Title::makeTitle( NS_MAIN, "Bogus" ),
883 Title::makeTitle( NS_MAIN, "UnBogus" )
884 ];
885 $this->title->mCascadingRestrictions = [
886 "bogus" => [ 'bogus', "sysop", "protect", "" ]
887 ];
888
889 $this->assertEquals( false,
890 $this->permissionManager->userCan( 'bogus', $this->user, $this->title ) );
891 $this->assertEquals( [
892 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
893 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ],
894 [ "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n", 'bogus' ] ],
895 $this->permissionManager->getPermissionErrors( 'bogus', $this->user, $this->title ) );
896
897 $this->assertEquals( true,
898 $this->permissionManager->userCan( 'edit', $this->user, $this->title ) );
899 $this->assertEquals( [],
900 $this->permissionManager->getPermissionErrors( 'edit', $this->user, $this->title ) );
901 }
902
908 public function testActionPermissions() {
909 $this->setUserPerm( [ "createpage" ] );
910 $this->setTitle( NS_MAIN, "test page" );
911 $this->title->mTitleProtection['permission'] = '';
912 $this->title->mTitleProtection['user'] = $this->user->getId();
913 $this->title->mTitleProtection['expiry'] = 'infinity';
914 $this->title->mTitleProtection['reason'] = 'test';
915 $this->title->mCascadeRestriction = false;
916
917 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
918 $this->permissionManager
919 ->getPermissionErrors( 'create', $this->user, $this->title ) );
920 $this->assertEquals( false,
921 $this->permissionManager->userCan( 'create', $this->user, $this->title ) );
922
923 $this->title->mTitleProtection['permission'] = 'editprotected';
924 $this->setUserPerm( [ 'createpage', 'protect' ] );
925 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
926 $this->permissionManager
927 ->getPermissionErrors( 'create', $this->user, $this->title ) );
928 $this->assertEquals( false,
929 $this->permissionManager->userCan( 'create', $this->user, $this->title ) );
930
931 $this->setUserPerm( [ 'createpage', 'editprotected' ] );
932 $this->assertEquals( [],
933 $this->permissionManager
934 ->getPermissionErrors( 'create', $this->user, $this->title ) );
935 $this->assertEquals( true,
936 $this->permissionManager->userCan( 'create', $this->user, $this->title ) );
937
938 $this->setUserPerm( [ 'createpage' ] );
939 $this->assertEquals( [ [ 'titleprotected', 'Useruser', 'test' ] ],
940 $this->permissionManager
941 ->getPermissionErrors( 'create', $this->user, $this->title ) );
942 $this->assertEquals( false,
943 $this->permissionManager->userCan( 'create', $this->user, $this->title ) );
944
945 $this->setTitle( NS_MEDIA, "test page" );
946 $this->setUserPerm( [ "move" ] );
947 $this->assertEquals( false,
948 $this->permissionManager->userCan( 'move', $this->user, $this->title ) );
949 $this->assertEquals( [ [ 'immobile-source-namespace', 'Media' ] ],
950 $this->permissionManager
951 ->getPermissionErrors( 'move', $this->user, $this->title ) );
952
953 $this->setTitle( NS_HELP, "test page" );
954 $this->assertEquals( [],
955 $this->permissionManager
956 ->getPermissionErrors( 'move', $this->user, $this->title ) );
957 $this->assertEquals( true,
958 $this->permissionManager->userCan( 'move', $this->user, $this->title ) );
959
960 $this->title->mInterwiki = "no";
961 $this->assertEquals( [ [ 'immobile-source-page' ] ],
962 $this->permissionManager
963 ->getPermissionErrors( 'move', $this->user, $this->title ) );
964 $this->assertEquals( false,
965 $this->permissionManager->userCan( 'move', $this->user, $this->title ) );
966
967 $this->setTitle( NS_MEDIA, "test page" );
968 $this->assertEquals( false,
969 $this->permissionManager->userCan( 'move-target', $this->user, $this->title ) );
970 $this->assertEquals( [ [ 'immobile-target-namespace', 'Media' ] ],
971 $this->permissionManager
972 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
973
974 $this->setTitle( NS_HELP, "test page" );
975 $this->assertEquals( [],
976 $this->permissionManager
977 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
978 $this->assertEquals( true,
979 $this->permissionManager->userCan( 'move-target', $this->user, $this->title ) );
980
981 $this->title->mInterwiki = "no";
982 $this->assertEquals( [ [ 'immobile-target-page' ] ],
983 $this->permissionManager
984 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
985 $this->assertEquals( false,
986 $this->permissionManager->userCan( 'move-target', $this->user, $this->title ) );
987 }
988
992 public function testUserBlock() {
993 $this->setMwGlobals( [
994 'wgEmailConfirmToEdit' => true,
995 'wgEmailAuthentication' => true,
996 ] );
997
998 $this->overrideMwServices();
999 $this->permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
1000
1001 $this->setUserPerm( [
1002 'createpage',
1003 'edit',
1004 'move',
1005 'rollback',
1006 'patrol',
1007 'upload',
1008 'purge'
1009 ] );
1010 $this->setTitle( NS_HELP, "test page" );
1011
1012 # $wgEmailConfirmToEdit only applies to 'edit' action
1013 $this->assertEquals( [],
1014 $this->permissionManager->getPermissionErrors( 'move-target',
1015 $this->user, $this->title ) );
1016 $this->assertContains( [ 'confirmedittext' ],
1017 $this->permissionManager
1018 ->getPermissionErrors( 'edit', $this->user, $this->title ) );
1019
1020 $this->setMwGlobals( 'wgEmailConfirmToEdit', false );
1021 $this->overrideMwServices();
1022 $this->permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
1023
1024 $this->assertNotContains( [ 'confirmedittext' ],
1025 $this->permissionManager
1026 ->getPermissionErrors( 'edit', $this->user, $this->title ) );
1027
1028 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
1029 $this->assertEquals( [],
1030 $this->permissionManager->getPermissionErrors( 'move-target',
1031 $this->user, $this->title ) );
1032
1033 global $wgLang;
1034 $prev = time();
1035 $now = time() + 120;
1036 $this->user->mBlockedby = $this->user->getId();
1037 $this->user->mBlock = new Block( [
1038 'address' => '127.0.8.1',
1039 'by' => $this->user->getId(),
1040 'reason' => 'no reason given',
1041 'timestamp' => $prev + 3600,
1042 'auto' => true,
1043 'expiry' => 0
1044 ] );
1045 $this->user->mBlock->mTimestamp = 0;
1046 $this->assertEquals( [ [ 'autoblockedtext',
1047 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1048 'Useruser', null, 'infinite', '127.0.8.1',
1049 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ] ],
1050 $this->permissionManager->getPermissionErrors( 'move-target',
1051 $this->user, $this->title ) );
1052
1053 $this->assertEquals( false, $this->permissionManager
1054 ->userCan( 'move-target', $this->user, $this->title ) );
1055 // quickUserCan should ignore user blocks
1056 $this->assertEquals( true, $this->permissionManager
1057 ->userCan( 'move-target', $this->user, $this->title,
1058 PermissionManager::RIGOR_QUICK ) );
1059
1060 global $wgLocalTZoffset;
1061 $wgLocalTZoffset = -60;
1062 $this->user->mBlockedby = $this->user->getName();
1063 $this->user->mBlock = new Block( [
1064 'address' => '127.0.8.1',
1065 'by' => $this->user->getId(),
1066 'reason' => 'no reason given',
1067 'timestamp' => $now,
1068 'auto' => false,
1069 'expiry' => 10,
1070 ] );
1071 $this->assertEquals( [ [ 'blockedtext',
1072 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1073 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1074 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ],
1075 $this->permissionManager
1076 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
1077 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
1078 # $user->blockedFor() == ''
1079 # $user->mBlock->mExpiry == 'infinity'
1080
1081 $this->user->mBlockedby = $this->user->getName();
1082 $this->user->mBlock = new Block( [
1083 'address' => '127.0.8.1',
1084 'by' => $this->user->getId(),
1085 'reason' => 'no reason given',
1086 'timestamp' => $now,
1087 'auto' => false,
1088 'expiry' => 10,
1089 'systemBlock' => 'test',
1090 ] );
1091
1092 $errors = [ [ 'systemblockedtext',
1093 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1094 'Useruser', 'test', '23:00, 31 December 1969', '127.0.8.1',
1095 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ];
1096
1097 $this->assertEquals( $errors,
1098 $this->permissionManager
1099 ->getPermissionErrors( 'edit', $this->user, $this->title ) );
1100 $this->assertEquals( $errors,
1101 $this->permissionManager
1102 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
1103 $this->assertEquals( $errors,
1104 $this->permissionManager
1105 ->getPermissionErrors( 'rollback', $this->user, $this->title ) );
1106 $this->assertEquals( $errors,
1107 $this->permissionManager
1108 ->getPermissionErrors( 'patrol', $this->user, $this->title ) );
1109 $this->assertEquals( $errors,
1110 $this->permissionManager
1111 ->getPermissionErrors( 'upload', $this->user, $this->title ) );
1112 $this->assertEquals( [],
1113 $this->permissionManager
1114 ->getPermissionErrors( 'purge', $this->user, $this->title ) );
1115
1116 // partial block message test
1117 $this->user->mBlockedby = $this->user->getName();
1118 $this->user->mBlock = new Block( [
1119 'address' => '127.0.8.1',
1120 'by' => $this->user->getId(),
1121 'reason' => 'no reason given',
1122 'timestamp' => $now,
1123 'sitewide' => false,
1124 'expiry' => 10,
1125 ] );
1126
1127 $this->assertEquals( [],
1128 $this->permissionManager
1129 ->getPermissionErrors( 'edit', $this->user, $this->title ) );
1130 $this->assertEquals( [],
1131 $this->permissionManager
1132 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
1133 $this->assertEquals( [],
1134 $this->permissionManager
1135 ->getPermissionErrors( 'rollback', $this->user, $this->title ) );
1136 $this->assertEquals( [],
1137 $this->permissionManager
1138 ->getPermissionErrors( 'patrol', $this->user, $this->title ) );
1139 $this->assertEquals( [],
1140 $this->permissionManager
1141 ->getPermissionErrors( 'upload', $this->user, $this->title ) );
1142 $this->assertEquals( [],
1143 $this->permissionManager
1144 ->getPermissionErrors( 'purge', $this->user, $this->title ) );
1145
1146 $this->user->mBlock->setRestrictions( [
1147 ( new PageRestriction( 0, $this->title->getArticleID() ) )->setTitle( $this->title ),
1148 ] );
1149
1150 $errors = [ [ 'blockedtext-partial',
1151 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1152 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
1153 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ];
1154
1155 $this->assertEquals( $errors,
1156 $this->permissionManager
1157 ->getPermissionErrors( 'edit', $this->user, $this->title ) );
1158 $this->assertEquals( $errors,
1159 $this->permissionManager
1160 ->getPermissionErrors( 'move-target', $this->user, $this->title ) );
1161 $this->assertEquals( $errors,
1162 $this->permissionManager
1163 ->getPermissionErrors( 'rollback', $this->user, $this->title ) );
1164 $this->assertEquals( $errors,
1165 $this->permissionManager
1166 ->getPermissionErrors( 'patrol', $this->user, $this->title ) );
1167 $this->assertEquals( [],
1168 $this->permissionManager
1169 ->getPermissionErrors( 'upload', $this->user, $this->title ) );
1170 $this->assertEquals( [],
1171 $this->permissionManager
1172 ->getPermissionErrors( 'purge', $this->user, $this->title ) );
1173
1174 // Test no block.
1175 $this->user->mBlockedby = null;
1176 $this->user->mBlock = null;
1177
1178 $this->assertEquals( [],
1179 $this->permissionManager
1180 ->getPermissionErrors( 'edit', $this->user, $this->title ) );
1181 }
1182
1189 public function testUserBlockAction() {
1190 global $wgLang;
1191
1192 $tester = $this->getMockBuilder( Action::class )
1193 ->disableOriginalConstructor()
1194 ->getMock();
1195 $tester->method( 'getName' )
1196 ->willReturn( 'tester' );
1197 $tester->method( 'getRestriction' )
1198 ->willReturn( 'test' );
1199 $tester->method( 'requiresUnblock' )
1200 ->willReturn( false );
1201
1202 $this->setMwGlobals( [
1203 'wgActions' => [
1204 'tester' => $tester,
1205 ],
1206 'wgGroupPermissions' => [
1207 '*' => [
1208 'tester' => true,
1209 ],
1210 ],
1211 ] );
1212
1213 $now = time();
1214 $this->user->mBlockedby = $this->user->getName();
1215 $this->user->mBlock = new Block( [
1216 'address' => '127.0.8.1',
1217 'by' => $this->user->getId(),
1218 'reason' => 'no reason given',
1219 'timestamp' => $now,
1220 'auto' => false,
1221 'expiry' => 'infinity',
1222 ] );
1223
1224 $errors = [ [ 'blockedtext',
1225 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
1226 'Useruser', null, 'infinite', '127.0.8.1',
1227 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ] ];
1228
1229 $this->assertEquals( $errors,
1230 $this->permissionManager
1231 ->getPermissionErrors( 'tester', $this->user, $this->title ) );
1232 }
1233
1237 public function testBlockInstanceCache() {
1238 // First, check the user isn't blocked
1239 $user = $this->getMutableTestUser()->getUser();
1240 $ut = Title::makeTitle( NS_USER_TALK, $user->getName() );
1241 $this->assertNull( $user->getBlock( false ), 'sanity check' );
1242 //$this->assertSame( '', $user->blockedBy(), 'sanity check' );
1243 //$this->assertSame( '', $user->blockedFor(), 'sanity check' );
1244 //$this->assertFalse( (bool)$user->isHidden(), 'sanity check' );
1245 $this->assertFalse( $this->permissionManager
1246 ->isBlockedFrom( $user, $ut ), 'sanity check' );
1247
1248 // Block the user
1249 $blocker = $this->getTestSysop()->getUser();
1250 $block = new Block( [
1251 'hideName' => true,
1252 'allowUsertalk' => false,
1253 'reason' => 'Because',
1254 ] );
1255 $block->setTarget( $user );
1256 $block->setBlocker( $blocker );
1257 $res = $block->insert();
1258 $this->assertTrue( (bool)$res['id'], 'sanity check: Failed to insert block' );
1259
1260 // Clear cache and confirm it loaded the block properly
1262 $this->assertInstanceOf( Block::class, $user->getBlock( false ) );
1263 //$this->assertSame( $blocker->getName(), $user->blockedBy() );
1264 //$this->assertSame( 'Because', $user->blockedFor() );
1265 //$this->assertTrue( (bool)$user->isHidden() );
1266 $this->assertTrue( $this->permissionManager->isBlockedFrom( $user, $ut ) );
1267
1268 // Unblock
1269 $block->delete();
1270
1271 // Clear cache and confirm it loaded the not-blocked properly
1273 $this->assertNull( $user->getBlock( false ) );
1274 //$this->assertSame( '', $user->blockedBy() );
1275 //$this->assertSame( '', $user->blockedFor() );
1276 //$this->assertFalse( (bool)$user->isHidden() );
1277 $this->assertFalse( $this->permissionManager->isBlockedFrom( $user, $ut ) );
1278 }
1279
1290 public function testIsBlockedFrom( $title, $expect, array $options = [] ) {
1291 $this->setMwGlobals( [
1292 'wgBlockAllowsUTEdit' => $options['blockAllowsUTEdit'] ?? true,
1293 ] );
1294
1295 $user = $this->getTestUser()->getUser();
1296
1297 if ( $title === self::USER_TALK_PAGE ) {
1299 } else {
1300 $title = Title::newFromText( $title );
1301 }
1302
1303 $restrictions = [];
1304 foreach ( $options['pageRestrictions'] ?? [] as $pagestr ) {
1305 $page = $this->getExistingTestPage(
1306 $pagestr === self::USER_TALK_PAGE ? $user->getTalkPage() : $pagestr
1307 );
1308 $restrictions[] = new PageRestriction( 0, $page->getId() );
1309 }
1310 foreach ( $options['namespaceRestrictions'] ?? [] as $ns ) {
1311 $restrictions[] = new NamespaceRestriction( 0, $ns );
1312 }
1313
1314 $block = new Block( [
1315 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
1316 'allowUsertalk' => $options['allowUsertalk'] ?? false,
1317 'sitewide' => !$restrictions,
1318 ] );
1319 $block->setTarget( $user );
1320 $block->setBlocker( $this->getTestSysop()->getUser() );
1321 if ( $restrictions ) {
1322 $block->setRestrictions( $restrictions );
1323 }
1324 $block->insert();
1325
1326 try {
1327 $this->assertSame( $expect, $this->permissionManager->isBlockedFrom( $user, $title ) );
1328 } finally {
1329 $block->delete();
1330 }
1331 }
1332
1333 public static function provideIsBlockedFrom() {
1334 return [
1335 'Sitewide block, basic operation' => [ 'Test page', true ],
1336 'Sitewide block, not allowing user talk' => [
1337 self::USER_TALK_PAGE, true, [
1338 'allowUsertalk' => false,
1339 ]
1340 ],
1341 'Sitewide block, allowing user talk' => [
1342 self::USER_TALK_PAGE, false, [
1343 'allowUsertalk' => true,
1344 ]
1345 ],
1346 'Sitewide block, allowing user talk but $wgBlockAllowsUTEdit is false' => [
1347 self::USER_TALK_PAGE, true, [
1348 'allowUsertalk' => true,
1349 'blockAllowsUTEdit' => false,
1350 ]
1351 ],
1352 'Partial block, blocking the page' => [
1353 'Test page', true, [
1354 'pageRestrictions' => [ 'Test page' ],
1355 ]
1356 ],
1357 'Partial block, not blocking the page' => [
1358 'Test page 2', false, [
1359 'pageRestrictions' => [ 'Test page' ],
1360 ]
1361 ],
1362 'Partial block, not allowing user talk but user talk page is not blocked' => [
1363 self::USER_TALK_PAGE, false, [
1364 'allowUsertalk' => false,
1365 'pageRestrictions' => [ 'Test page' ],
1366 ]
1367 ],
1368 'Partial block, allowing user talk but user talk page is blocked' => [
1369 self::USER_TALK_PAGE, true, [
1370 'allowUsertalk' => true,
1371 'pageRestrictions' => [ self::USER_TALK_PAGE ],
1372 ]
1373 ],
1374 'Partial block, user talk page is not blocked but $wgBlockAllowsUTEdit is false' => [
1375 self::USER_TALK_PAGE, false, [
1376 'allowUsertalk' => false,
1377 'pageRestrictions' => [ 'Test page' ],
1378 'blockAllowsUTEdit' => false,
1379 ]
1380 ],
1381 'Partial block, user talk page is blocked and $wgBlockAllowsUTEdit is false' => [
1382 self::USER_TALK_PAGE, true, [
1383 'allowUsertalk' => true,
1384 'pageRestrictions' => [ self::USER_TALK_PAGE ],
1385 'blockAllowsUTEdit' => false,
1386 ]
1387 ],
1388 'Partial user talk namespace block, not allowing user talk' => [
1389 self::USER_TALK_PAGE, true, [
1390 'allowUsertalk' => false,
1391 'namespaceRestrictions' => [ NS_USER_TALK ],
1392 ]
1393 ],
1394 'Partial user talk namespace block, allowing user talk' => [
1395 self::USER_TALK_PAGE, false, [
1396 'allowUsertalk' => true,
1397 'namespaceRestrictions' => [ NS_USER_TALK ],
1398 ]
1399 ],
1400 'Partial user talk namespace block, where $wgBlockAllowsUTEdit is false' => [
1401 self::USER_TALK_PAGE, true, [
1402 'allowUsertalk' => true,
1403 'namespaceRestrictions' => [ NS_USER_TALK ],
1404 'blockAllowsUTEdit' => false,
1405 ]
1406 ],
1407 ];
1408 }
1409
1410}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgNamespaceProtection
Set the minimum permissions required to edit pages in each namespace.
$wgGroupPermissions
Permission keys given to users in each group.
$wgLocalTZoffset
Set an offset from UTC in minutes to use for the default timezone setting for anonymous users and new...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
$wgLang
Definition Setup.php:875
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:39
Base class that store and restore the Language objects.
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
static getTestSysop()
Convenience method for getting an immutable admin test user.
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
isWikitextNS( $ns)
Returns true if the given namespace defaults to Wikitext according to $wgNamespaceContentModels.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
runConfigEditPermissions( $resultNone, $resultMyCss, $resultMyJson, $resultMyJs, $resultUserCss, $resultUserJson, $resultUserJs, $resultPatrol)
testUserBlock()
\MediaWiki\Permissions\PermissionManager::checkUserBlock
testIsBlockedFrom( $title, $expect, array $options=[])
\MediaWiki\Permissions\PermissionManager::isBlockedFrom provideIsBlockedFrom
testCascadingSourcesRestrictions()
\MediaWiki\Permissions\PermissionManager::checkCascadingSourcesRestrictions
testBlockInstanceCache()
\MediaWiki\Permissions\PermissionManager::isBlockedFrom
testUserBlockAction()
\MediaWiki\Permissions\PermissionManager::checkUserBlock
const USER_TALK_PAGE
Constant for self::testIsBlockedFrom.
Group all the pieces relevant to the context of a request into one instance.
Represents a title within MediaWiki.
Definition Title.php:40
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2452
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
clearInstanceCache( $reloadFrom=false)
Clear various cached data stored in this object.
Definition User.php:1734
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:609
getTalkPage()
Get this user's talk page title.
Definition User.php:4556
static createNew( $name, $params=[])
Add a user to the database, return the user object.
Definition User.php:4300
getBlock( $fromReplica=true)
Get the block affecting the user, or null if the user is not blocked.
Definition User.php:2289
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
const NS_HELP
Definition Defines.php:85
const NS_USER
Definition Defines.php:75
const NS_FILE
Definition Defines.php:79
const NS_MAIN
Definition Defines.php:73
const NS_MEDIAWIKI
Definition Defines.php:81
const NS_SPECIAL
Definition Defines.php:62
const NS_MEDIA
Definition Defines.php:61
const NS_TALK
Definition Defines.php:74
const NS_USER_TALK
Definition Defines.php:76
const NS_PROJECT
Definition Defines.php:77
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\d*-\d*)") will be honored when streaming the file. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition hooks.txt:1991
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2004
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
title