Merge "define RC_EXTERNAL type for recent changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitlePermissionTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class TitlePermissionTest extends MediaWikiLangTestCase {
7
8 /**
9 * @var string
10 */
11 protected $userName, $altUserName;
12
13 /**
14 * @var Title
15 */
16 protected $title;
17
18 /**
19 * @var User
20 */
21 protected $user, $anonUser, $userUser, $altUser;
22
23 protected function setUp() {
24 parent::setUp();
25
26 $langObj = Language::factory( 'en' );
27 $localZone = 'UTC';
28 $localOffset = date( 'Z' ) / 60;
29
30 $this->setMwGlobals( array(
31 'wgMemc' => new EmptyBagOStuff,
32 'wgContLang' => $langObj,
33 'wgLang' => $langObj,
34 'wgLocaltimezone' => $localZone,
35 'wgLocalTZoffset' => $localOffset,
36 'wgNamespaceProtection' => array(
37 NS_MEDIAWIKI => 'editinterface',
38 ),
39 'wgUser' => null,
40 ) );
41
42 $this->userName = 'Useruser';
43 $this->altUserName = 'Altuseruser';
44 date_default_timezone_set( $localZone );
45
46 $this->title = Title::makeTitle( NS_MAIN, "Main Page" );
47 if ( !isset( $this->userUser ) || !( $this->userUser instanceOf User ) ) {
48 $this->userUser = User::newFromName( $this->userName );
49
50 if ( !$this->userUser->getID() ) {
51 $this->userUser = User::createNew( $this->userName, array(
52 "email" => "test@example.com",
53 "real_name" => "Test User" ) );
54 $this->userUser->load();
55 }
56
57 $this->altUser = User::newFromName( $this->altUserName );
58 if ( !$this->altUser->getID() ) {
59 $this->altUser = User::createNew( $this->altUserName, array(
60 "email" => "alttest@example.com",
61 "real_name" => "Test User Alt" ) );
62 $this->altUser->load();
63 }
64
65 $this->anonUser = User::newFromId( 0 );
66
67 $this->user = $this->userUser;
68 }
69
70 }
71
72 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 = array( $perm );
79 }
80 }
81
82 function setTitle( $ns, $title = "Main_Page" ) {
83 $this->title = Title::makeTitle( $ns, $title );
84 }
85
86 function setUser( $userName = null ) {
87 global $wgUser;
88
89 if ( $userName === 'anon' ) {
90 $this->user = $this->anonUser;
91 } elseif ( $userName === null || $userName === $this->userName ) {
92 $this->user = $this->userUser;
93 } else {
94 $this->user = $this->altUser;
95 }
96
97 $wgUser = $this->user;
98 }
99
100 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( array(), $res );
109
110 $this->setTitle( NS_TALK );
111 $this->setUserPerm( "createpage" );
112 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
113 $this->assertEquals( array( array( "nocreatetext" ) ), $res );
114
115 $this->setTitle( NS_TALK );
116 $this->setUserPerm( "" );
117 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
118 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
119
120 $this->setTitle( NS_MAIN );
121 $this->setUserPerm( "createpage" );
122 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
123 $this->assertEquals( array( ), $res );
124
125 $this->setTitle( NS_MAIN );
126 $this->setUserPerm( "createtalk" );
127 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
128 $this->assertEquals( array( array( '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( array( ), $res );
135
136 $this->setTitle( NS_TALK );
137 $this->setUserPerm( "createpage" );
138 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
139 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
140
141 $this->setTitle( NS_TALK );
142 $this->setUserPerm( "" );
143 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
144 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
145
146 $this->setTitle( NS_MAIN );
147 $this->setUserPerm( "createpage" );
148 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
149 $this->assertEquals( array( ), $res );
150
151 $this->setTitle( NS_MAIN );
152 $this->setUserPerm( "createtalk" );
153 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
154 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
155
156 $this->setTitle( NS_MAIN );
157 $this->setUserPerm( "" );
158 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
159 $this->assertEquals( array( array( '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( array( array( 'cant-move-user-page' ), array( '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( array( array( '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( array( array( '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( array( array( 'movenologintext' ) ), $res );
181
182 $this->setTitle( NS_USER, $this->userName . '' );
183 $this->setUserPerm( "" );
184 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
185 $this->assertEquals( array( array( 'cant-move-user-page' ), array( '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( array( array( '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( array( array( '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( array( array( '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( array( array( 'movenotallowedfile' ), array( '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( array( array( '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( array( array( 'movenotallowedfile' ), array( '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( array( array( 'movenologintext' ) ), $res );
223
224 $this->setUser( $this->userName );
225 $this->setUserPerm( "move" );
226 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
227
228 $this->setUserPerm( "" );
229 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ) );
230
231 $this->setUser( 'anon' );
232 $this->setUserPerm( "move" );
233 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
234
235 $this->setUserPerm( "" );
236 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
237 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ) );
238
239 if ( $this->isWikitextNS( NS_MAIN ) ) {
240 //NOTE: some content models don't allow moving
241 //@todo: find a Wikitext namespace for testing
242
243 $this->setTitle( NS_MAIN );
244 $this->setUser( 'anon' );
245 $this->setUserPerm( "move" );
246 $this->runGroupPermissions( 'move', array( ) );
247
248 $this->setUserPerm( "" );
249 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
250 array( array( 'movenologintext' ) ) );
251
252 $this->setUser( $this->userName );
253 $this->setUserPerm( "" );
254 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
255
256 $this->setUserPerm( "move" );
257 $this->runGroupPermissions( 'move', array( ) );
258
259 $this->setUser( 'anon' );
260 $this->setUserPerm( 'move' );
261 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
262 $this->assertEquals( array( ), $res );
263
264 $this->setUserPerm( '' );
265 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
266 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
267 }
268
269 $this->setTitle( NS_USER );
270 $this->setUser( $this->userName );
271 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
272 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
273 $this->assertEquals( array( ), $res );
274
275 $this->setUserPerm( "move" );
276 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
277 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
278
279 $this->setUser( 'anon' );
280 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
281 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
282 $this->assertEquals( array( ), $res );
283
284 $this->setTitle( NS_USER, "User/subpage" );
285 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
286 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
287 $this->assertEquals( array( ), $res );
288
289 $this->setUserPerm( "move" );
290 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
291 $this->assertEquals( array( ), $res );
292
293 $this->setUser( 'anon' );
294 $check = array( 'edit' => array( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ) ),
295 array( array( 'badaccess-group0' ) ),
296 array( ), true ),
297 'protect' => array( array( array( 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ), array( 'protect-cantedit' ) ),
298 array( array( 'badaccess-group0' ), array( 'protect-cantedit' ) ),
299 array( array( 'protect-cantedit' ) ), false ),
300 '' => array( array( ), array( ), array( ), true ) );
301 global $wgUser;
302 $wgUser = $this->user;
303 foreach ( array( "edit", "protect", "" ) as $action ) {
304 $this->setUserPerm( null );
305 $this->assertEquals( $check[$action][0],
306 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
307
308 global $wgGroupPermissions;
309 $old = $wgGroupPermissions;
310 $wgGroupPermissions = array();
311
312 $this->assertEquals( $check[$action][1],
313 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
314 $wgGroupPermissions = $old;
315
316 $this->setUserPerm( $action );
317 $this->assertEquals( $check[$action][2],
318 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
319
320 $this->setUserPerm( $action );
321 $this->assertEquals( $check[$action][3],
322 $this->title->userCan( $action, true ) );
323 $this->assertEquals( $check[$action][3],
324 $this->title->quickUserCan( $action ) );
325
326 # count( User::getGroupsWithPermissions( $action ) ) < 1
327 }
328 }
329
330 function runGroupPermissions( $action, $result, $result2 = null ) {
331 global $wgGroupPermissions;
332
333 if ( $result2 === null ) $result2 = $result;
334
335 $wgGroupPermissions['autoconfirmed']['move'] = false;
336 $wgGroupPermissions['user']['move'] = false;
337 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
338 $this->assertEquals( $result, $res );
339
340 $wgGroupPermissions['autoconfirmed']['move'] = true;
341 $wgGroupPermissions['user']['move'] = false;
342 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
343 $this->assertEquals( $result2, $res );
344
345 $wgGroupPermissions['autoconfirmed']['move'] = true;
346 $wgGroupPermissions['user']['move'] = true;
347 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
348 $this->assertEquals( $result2, $res );
349
350 $wgGroupPermissions['autoconfirmed']['move'] = false;
351 $wgGroupPermissions['user']['move'] = true;
352 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
353 $this->assertEquals( $result2, $res );
354 }
355
356 function testSpecialsAndNSPermissions() {
357 global $wgNamespaceProtection;
358 $this->setUser( $this->userName );
359
360 $this->setTitle( NS_SPECIAL );
361
362 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
363 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
364 $this->assertEquals( array( array( 'badaccess-group0' ) ),
365 $this->title->getUserPermissionsErrors( 'execute', $this->user ) );
366
367 $this->setTitle( NS_MAIN );
368 $this->setUserPerm( 'bogus' );
369 $this->assertEquals( array( ),
370 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
371
372 $this->setTitle( NS_MAIN );
373 $this->setUserPerm( '' );
374 $this->assertEquals( array( array( 'badaccess-group0' ) ),
375 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
376
377 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
378
379 $this->setTitle( NS_USER );
380 $this->setUserPerm( '' );
381 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
382 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
383
384 $this->setTitle( NS_MEDIAWIKI );
385 $this->setUserPerm( 'bogus' );
386 $this->assertEquals( array( array( 'protectedinterface' ) ),
387 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
388
389 $this->setTitle( NS_MEDIAWIKI );
390 $this->setUserPerm( 'bogus' );
391 $this->assertEquals( array( array( 'protectedinterface' ) ),
392 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
393
394 $wgNamespaceProtection = null;
395
396 $this->setUserPerm( 'bogus' );
397 $this->assertEquals( array( ),
398 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
399 $this->assertEquals( true,
400 $this->title->userCan( 'bogus' ) );
401
402 $this->setUserPerm( '' );
403 $this->assertEquals( array( array( 'badaccess-group0' ) ),
404 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
405 $this->assertEquals( false,
406 $this->title->userCan( 'bogus' ) );
407 }
408
409 function testCssAndJavascriptPermissions() {
410 $this->setUser( $this->userName );
411
412 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
413 $this->runCSSandJSPermissions(
414 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
415 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
416 array( array( 'badaccess-group0' ) ) );
417
418 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
419 $this->runCSSandJSPermissions(
420 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
421 array( array( 'badaccess-group0' ) ),
422 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ) );
423
424 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
425 $this->runCSSandJSPermissions(
426 array( array( 'badaccess-group0' ) ),
427 array( array( 'badaccess-group0' ) ),
428 array( array( 'badaccess-group0' ) ) );
429 }
430
431 function runCSSandJSPermissions( $result0, $result1, $result2 ) {
432 $this->setUserPerm( '' );
433 $this->assertEquals( $result0,
434 $this->title->getUserPermissionsErrors( 'bogus',
435 $this->user ) );
436
437 $this->setUserPerm( 'editusercss' );
438 $this->assertEquals( $result1,
439 $this->title->getUserPermissionsErrors( 'bogus',
440 $this->user ) );
441
442 $this->setUserPerm( 'edituserjs' );
443 $this->assertEquals( $result2,
444 $this->title->getUserPermissionsErrors( 'bogus',
445 $this->user ) );
446
447 $this->setUserPerm( 'editusercssjs' );
448 $this->assertEquals( array( array( 'badaccess-group0' ) ),
449 $this->title->getUserPermissionsErrors( 'bogus',
450 $this->user ) );
451
452 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
453 $this->assertEquals( array( array( 'badaccess-group0' ) ),
454 $this->title->getUserPermissionsErrors( 'bogus',
455 $this->user ) );
456 }
457
458 function testPageRestrictions() {
459 global $wgUser, $wgContLang;
460
461 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
462
463 $wgUser = $this->user;
464 $this->setTitle( NS_MAIN );
465 $this->title->mRestrictionsLoaded = true;
466 $this->setUserPerm( "edit" );
467 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
468
469 $this->assertEquals( array( ),
470 $this->title->getUserPermissionsErrors( 'edit',
471 $this->user ) );
472
473 $this->assertEquals( true,
474 $this->title->quickUserCan( 'edit' ) );
475 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
476 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
477
478 $this->assertEquals( array( array( 'badaccess-group0' ),
479 array( 'protectedpagetext', 'bogus' ),
480 array( 'protectedpagetext', 'protect' ),
481 array( 'protectedpagetext', 'protect' ) ),
482 $this->title->getUserPermissionsErrors( 'bogus',
483 $this->user ) );
484 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
485 array( 'protectedpagetext', 'protect' ),
486 array( 'protectedpagetext', 'protect' ) ),
487 $this->title->getUserPermissionsErrors( 'edit',
488 $this->user ) );
489 $this->setUserPerm( "" );
490 $this->assertEquals( array( array( 'badaccess-group0' ),
491 array( 'protectedpagetext', 'bogus' ),
492 array( 'protectedpagetext', 'protect' ),
493 array( 'protectedpagetext', 'protect' ) ),
494 $this->title->getUserPermissionsErrors( 'bogus',
495 $this->user ) );
496 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
497 array( 'protectedpagetext', 'bogus' ),
498 array( 'protectedpagetext', 'protect' ),
499 array( 'protectedpagetext', 'protect' ) ),
500 $this->title->getUserPermissionsErrors( 'edit',
501 $this->user ) );
502 $this->setUserPerm( array( "edit", "editprotected" ) );
503 $this->assertEquals( array( array( 'badaccess-group0' ),
504 array( 'protectedpagetext', 'bogus' ),
505 array( 'protectedpagetext', 'protect' ),
506 array( 'protectedpagetext', 'protect' ) ),
507 $this->title->getUserPermissionsErrors( 'bogus',
508 $this->user ) );
509 $this->assertEquals( array( ),
510 $this->title->getUserPermissionsErrors( 'edit',
511 $this->user ) );
512 $this->title->mCascadeRestriction = true;
513 $this->assertEquals( false,
514 $this->title->quickUserCan( 'bogus' ) );
515 $this->assertEquals( false,
516 $this->title->quickUserCan( 'edit' ) );
517 $this->assertEquals( array( array( 'badaccess-group0' ),
518 array( 'protectedpagetext', 'bogus' ),
519 array( 'protectedpagetext', 'protect' ),
520 array( 'protectedpagetext', 'protect' ) ),
521 $this->title->getUserPermissionsErrors( 'bogus',
522 $this->user ) );
523 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
524 array( 'protectedpagetext', 'protect' ),
525 array( 'protectedpagetext', 'protect' ) ),
526 $this->title->getUserPermissionsErrors( 'edit',
527 $this->user ) );
528 }
529
530 function testCascadingSourcesRestrictions() {
531 global $wgUser;
532 $wgUser = $this->user;
533 $this->setTitle( NS_MAIN, "test page" );
534 $this->setUserPerm( array( "edit", "bogus" ) );
535
536 $this->title->mCascadeSources = array( Title::makeTitle( NS_MAIN, "Bogus" ), Title::makeTitle( NS_MAIN, "UnBogus" ) );
537 $this->title->mCascadingRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
538
539 $this->assertEquals( false,
540 $this->title->userCan( 'bogus' ) );
541 $this->assertEquals( array( array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
542 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ) ),
543 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
544
545 $this->assertEquals( true,
546 $this->title->userCan( 'edit' ) );
547 $this->assertEquals( array( ),
548 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
549
550 }
551
552 function testActionPermissions() {
553 global $wgUser;
554 $wgUser = $this->user;
555
556 $this->setUserPerm( array( "createpage" ) );
557 $this->setTitle( NS_MAIN, "test page" );
558 $this->title->mTitleProtection['pt_create_perm'] = '';
559 $this->title->mTitleProtection['pt_user'] = $this->user->getID();
560 $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
561 $this->title->mTitleProtection['pt_reason'] = 'test';
562 $this->title->mCascadeRestriction = false;
563
564 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
565 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
566 $this->assertEquals( false,
567 $this->title->userCan( 'create' ) );
568
569 $this->title->mTitleProtection['pt_create_perm'] = 'sysop';
570 $this->setUserPerm( array( 'createpage', 'protect' ) );
571 $this->assertEquals( array( ),
572 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
573 $this->assertEquals( true,
574 $this->title->userCan( 'create' ) );
575
576
577 $this->setUserPerm( array( 'createpage' ) );
578 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
579 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
580 $this->assertEquals( false,
581 $this->title->userCan( 'create' ) );
582
583 $this->setTitle( NS_MEDIA, "test page" );
584 $this->setUserPerm( array( "move" ) );
585 $this->assertEquals( false,
586 $this->title->userCan( 'move' ) );
587 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
588 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
589
590 $this->setTitle( NS_HELP, "test page" );
591 $this->assertEquals( array( ),
592 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
593 $this->assertEquals( true,
594 $this->title->userCan( 'move' ) );
595
596 $this->title->mInterwiki = "no";
597 $this->assertEquals( array( array( 'immobile-source-page' ) ),
598 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
599 $this->assertEquals( false,
600 $this->title->userCan( 'move' ) );
601
602 $this->setTitle( NS_MEDIA, "test page" );
603 $this->assertEquals( false,
604 $this->title->userCan( 'move-target' ) );
605 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
606 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
607
608 $this->setTitle( NS_HELP, "test page" );
609 $this->assertEquals( array( ),
610 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
611 $this->assertEquals( true,
612 $this->title->userCan( 'move-target' ) );
613
614 $this->title->mInterwiki = "no";
615 $this->assertEquals( array( array( 'immobile-target-page' ) ),
616 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
617 $this->assertEquals( false,
618 $this->title->userCan( 'move-target' ) );
619
620 }
621
622 function testUserBlock() {
623 global $wgUser, $wgEmailConfirmToEdit, $wgEmailAuthentication;
624 $wgEmailConfirmToEdit = true;
625 $wgEmailAuthentication = true;
626 $wgUser = $this->user;
627
628 $this->setUserPerm( array( "createpage", "move" ) );
629 $this->setTitle( NS_HELP, "test page" );
630
631 # $short
632 $this->assertEquals( array( array( 'confirmedittext' ) ),
633 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
634 $wgEmailConfirmToEdit = false;
635 $this->assertEquals( true, $this->title->userCan( 'move-target' ) );
636
637 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
638 $this->assertEquals( array( ),
639 $this->title->getUserPermissionsErrors( 'move-target',
640 $this->user ) );
641
642 global $wgLang;
643 $prev = time();
644 $now = time() + 120;
645 $this->user->mBlockedby = $this->user->getId();
646 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
647 'no reason given', $prev + 3600, 1, 0 );
648 $this->user->mBlock->mTimestamp = 0;
649 $this->assertEquals( array( array( 'autoblockedtext',
650 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
651 'Useruser', null, 'infinite', '127.0.8.1',
652 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
653 $this->title->getUserPermissionsErrors( 'move-target',
654 $this->user ) );
655
656 $this->assertEquals( false, $this->title->userCan( 'move-target' ) );
657 // quickUserCan should ignore user blocks
658 $this->assertEquals( true, $this->title->quickUserCan( 'move-target' ) );
659
660 global $wgLocalTZoffset;
661 $wgLocalTZoffset = -60;
662 $this->user->mBlockedby = $this->user->getName();
663 $this->user->mBlock = new Block( '127.0.8.1', 0, 1, 'no reason given', $now, 0, 10 );
664 $this->assertEquals( array( array( 'blockedtext',
665 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
666 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
667 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
668 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
669
670 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
671 # $user->blockedFor() == ''
672 # $user->mBlock->mExpiry == 'infinity'
673 }
674 }