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