Merge "Additional tests to keep Parsoid on track."
[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 $this->setTitle( NS_MAIN );
240 $this->setUser( 'anon' );
241 $this->setUserPerm( "move" );
242 $this->runGroupPermissions( 'move', array( ) );
243
244 $this->setUserPerm( "" );
245 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
246 array( array( 'movenologintext' ) ) );
247
248 $this->setUser( $this->userName );
249 $this->setUserPerm( "" );
250 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
251
252 $this->setUserPerm( "move" );
253 $this->runGroupPermissions( 'move', array( ) );
254
255 $this->setUser( 'anon' );
256 $this->setUserPerm( 'move' );
257 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
258 $this->assertEquals( array( ), $res );
259
260 $this->setUserPerm( '' );
261 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
262 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
263
264 $this->setTitle( NS_USER );
265 $this->setUser( $this->userName );
266 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
267 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
268 $this->assertEquals( array( ), $res );
269
270 $this->setUserPerm( "move" );
271 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
272 $this->assertEquals( array( array( 'cant-move-to-user-page' ) ), $res );
273
274 $this->setUser( 'anon' );
275 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
276 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
277 $this->assertEquals( array( ), $res );
278
279 $this->setTitle( NS_USER, "User/subpage" );
280 $this->setUserPerm( array( "move", "move-rootuserpages" ) );
281 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
282 $this->assertEquals( array( ), $res );
283
284 $this->setUserPerm( "move" );
285 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
286 $this->assertEquals( array( ), $res );
287
288 $this->setUser( 'anon' );
289 $check = array( 'edit' => array( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ) ),
290 array( array( 'badaccess-group0' ) ),
291 array( ), true ),
292 'protect' => array( array( array( 'badaccess-groups', "[[$prefix:Administrators|Administrators]]", 1 ), array( 'protect-cantedit' ) ),
293 array( array( 'badaccess-group0' ), array( 'protect-cantedit' ) ),
294 array( array( 'protect-cantedit' ) ), false ),
295 '' => array( array( ), array( ), array( ), true ) );
296 global $wgUser;
297 $wgUser = $this->user;
298 foreach ( array( "edit", "protect", "" ) as $action ) {
299 $this->setUserPerm( null );
300 $this->assertEquals( $check[$action][0],
301 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
302
303 global $wgGroupPermissions;
304 $old = $wgGroupPermissions;
305 $wgGroupPermissions = array();
306
307 $this->assertEquals( $check[$action][1],
308 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
309 $wgGroupPermissions = $old;
310
311 $this->setUserPerm( $action );
312 $this->assertEquals( $check[$action][2],
313 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
314
315 $this->setUserPerm( $action );
316 $this->assertEquals( $check[$action][3],
317 $this->title->userCan( $action, true ) );
318 $this->assertEquals( $check[$action][3],
319 $this->title->quickUserCan( $action ) );
320
321 # count( User::getGroupsWithPermissions( $action ) ) < 1
322 }
323 }
324
325 function runGroupPermissions( $action, $result, $result2 = null ) {
326 global $wgGroupPermissions;
327
328 if ( $result2 === null ) $result2 = $result;
329
330 $wgGroupPermissions['autoconfirmed']['move'] = false;
331 $wgGroupPermissions['user']['move'] = false;
332 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
333 $this->assertEquals( $result, $res );
334
335 $wgGroupPermissions['autoconfirmed']['move'] = true;
336 $wgGroupPermissions['user']['move'] = false;
337 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
338 $this->assertEquals( $result2, $res );
339
340 $wgGroupPermissions['autoconfirmed']['move'] = true;
341 $wgGroupPermissions['user']['move'] = true;
342 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
343 $this->assertEquals( $result2, $res );
344
345 $wgGroupPermissions['autoconfirmed']['move'] = false;
346 $wgGroupPermissions['user']['move'] = true;
347 $res = $this->title->getUserPermissionsErrors( $action, $this->user );
348 $this->assertEquals( $result2, $res );
349 }
350
351 function testSpecialsAndNSPermissions() {
352 global $wgNamespaceProtection;
353 $this->setUser( $this->userName );
354
355 $this->setTitle( NS_SPECIAL );
356
357 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'ns-specialprotected' ) ),
358 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
359 $this->assertEquals( array( array( 'badaccess-group0' ) ),
360 $this->title->getUserPermissionsErrors( 'execute', $this->user ) );
361
362 $this->setTitle( NS_MAIN );
363 $this->setUserPerm( 'bogus' );
364 $this->assertEquals( array( ),
365 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
366
367 $this->setTitle( NS_MAIN );
368 $this->setUserPerm( '' );
369 $this->assertEquals( array( array( 'badaccess-group0' ) ),
370 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
371
372 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
373
374 $this->setTitle( NS_USER );
375 $this->setUserPerm( '' );
376 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
377 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
378
379 $this->setTitle( NS_MEDIAWIKI );
380 $this->setUserPerm( 'bogus' );
381 $this->assertEquals( array( array( 'protectedinterface' ) ),
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 $wgNamespaceProtection = null;
390
391 $this->setUserPerm( 'bogus' );
392 $this->assertEquals( array( ),
393 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
394 $this->assertEquals( true,
395 $this->title->userCan( 'bogus' ) );
396
397 $this->setUserPerm( '' );
398 $this->assertEquals( array( array( 'badaccess-group0' ) ),
399 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
400 $this->assertEquals( false,
401 $this->title->userCan( 'bogus' ) );
402 }
403
404 function testCssAndJavascriptPermissions() {
405 $this->setUser( $this->userName );
406
407 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
408 $this->runCSSandJSPermissions(
409 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
410 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
411 array( array( 'badaccess-group0' ) ) );
412
413 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
414 $this->runCSSandJSPermissions(
415 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
416 array( array( 'badaccess-group0' ) ),
417 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ) );
418
419 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
420 $this->runCSSandJSPermissions(
421 array( array( 'badaccess-group0' ) ),
422 array( array( 'badaccess-group0' ) ),
423 array( array( 'badaccess-group0' ) ) );
424 }
425
426 function runCSSandJSPermissions( $result0, $result1, $result2 ) {
427 $this->setUserPerm( '' );
428 $this->assertEquals( $result0,
429 $this->title->getUserPermissionsErrors( 'bogus',
430 $this->user ) );
431
432 $this->setUserPerm( 'editusercss' );
433 $this->assertEquals( $result1,
434 $this->title->getUserPermissionsErrors( 'bogus',
435 $this->user ) );
436
437 $this->setUserPerm( 'edituserjs' );
438 $this->assertEquals( $result2,
439 $this->title->getUserPermissionsErrors( 'bogus',
440 $this->user ) );
441
442 $this->setUserPerm( 'editusercssjs' );
443 $this->assertEquals( array( array( 'badaccess-group0' ) ),
444 $this->title->getUserPermissionsErrors( 'bogus',
445 $this->user ) );
446
447 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
448 $this->assertEquals( array( array( 'badaccess-group0' ) ),
449 $this->title->getUserPermissionsErrors( 'bogus',
450 $this->user ) );
451 }
452
453 function testPageRestrictions() {
454 global $wgUser, $wgContLang;
455
456 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
457
458 $wgUser = $this->user;
459 $this->setTitle( NS_MAIN );
460 $this->title->mRestrictionsLoaded = true;
461 $this->setUserPerm( "edit" );
462 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
463
464 $this->assertEquals( array( ),
465 $this->title->getUserPermissionsErrors( 'edit',
466 $this->user ) );
467
468 $this->assertEquals( true,
469 $this->title->quickUserCan( 'edit' ) );
470 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
471 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
472
473 $this->assertEquals( array( array( 'badaccess-group0' ),
474 array( 'protectedpagetext', 'bogus' ),
475 array( 'protectedpagetext', 'protect' ),
476 array( 'protectedpagetext', 'protect' ) ),
477 $this->title->getUserPermissionsErrors( 'bogus',
478 $this->user ) );
479 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
480 array( 'protectedpagetext', 'protect' ),
481 array( 'protectedpagetext', 'protect' ) ),
482 $this->title->getUserPermissionsErrors( 'edit',
483 $this->user ) );
484 $this->setUserPerm( "" );
485 $this->assertEquals( array( array( 'badaccess-group0' ),
486 array( 'protectedpagetext', 'bogus' ),
487 array( 'protectedpagetext', 'protect' ),
488 array( 'protectedpagetext', 'protect' ) ),
489 $this->title->getUserPermissionsErrors( 'bogus',
490 $this->user ) );
491 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
492 array( 'protectedpagetext', 'bogus' ),
493 array( 'protectedpagetext', 'protect' ),
494 array( 'protectedpagetext', 'protect' ) ),
495 $this->title->getUserPermissionsErrors( 'edit',
496 $this->user ) );
497 $this->setUserPerm( array( "edit", "editprotected" ) );
498 $this->assertEquals( array( array( 'badaccess-group0' ),
499 array( 'protectedpagetext', 'bogus' ),
500 array( 'protectedpagetext', 'protect' ),
501 array( 'protectedpagetext', 'protect' ) ),
502 $this->title->getUserPermissionsErrors( 'bogus',
503 $this->user ) );
504 $this->assertEquals( array( ),
505 $this->title->getUserPermissionsErrors( 'edit',
506 $this->user ) );
507 $this->title->mCascadeRestriction = true;
508 $this->assertEquals( false,
509 $this->title->quickUserCan( 'bogus' ) );
510 $this->assertEquals( false,
511 $this->title->quickUserCan( 'edit' ) );
512 $this->assertEquals( array( array( 'badaccess-group0' ),
513 array( 'protectedpagetext', 'bogus' ),
514 array( 'protectedpagetext', 'protect' ),
515 array( 'protectedpagetext', 'protect' ) ),
516 $this->title->getUserPermissionsErrors( 'bogus',
517 $this->user ) );
518 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
519 array( 'protectedpagetext', 'protect' ),
520 array( 'protectedpagetext', 'protect' ) ),
521 $this->title->getUserPermissionsErrors( 'edit',
522 $this->user ) );
523 }
524
525 function testCascadingSourcesRestrictions() {
526 global $wgUser;
527 $wgUser = $this->user;
528 $this->setTitle( NS_MAIN, "test page" );
529 $this->setUserPerm( array( "edit", "bogus" ) );
530
531 $this->title->mCascadeSources = array( Title::makeTitle( NS_MAIN, "Bogus" ), Title::makeTitle( NS_MAIN, "UnBogus" ) );
532 $this->title->mCascadingRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
533
534 $this->assertEquals( false,
535 $this->title->userCan( 'bogus' ) );
536 $this->assertEquals( array( array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
537 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ) ),
538 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
539
540 $this->assertEquals( true,
541 $this->title->userCan( 'edit' ) );
542 $this->assertEquals( array( ),
543 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
544
545 }
546
547 function testActionPermissions() {
548 global $wgUser;
549 $wgUser = $this->user;
550
551 $this->setUserPerm( array( "createpage" ) );
552 $this->setTitle( NS_MAIN, "test page" );
553 $this->title->mTitleProtection['pt_create_perm'] = '';
554 $this->title->mTitleProtection['pt_user'] = $this->user->getID();
555 $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
556 $this->title->mTitleProtection['pt_reason'] = 'test';
557 $this->title->mCascadeRestriction = false;
558
559 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
560 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
561 $this->assertEquals( false,
562 $this->title->userCan( 'create' ) );
563
564 $this->title->mTitleProtection['pt_create_perm'] = 'sysop';
565 $this->setUserPerm( array( 'createpage', 'protect' ) );
566 $this->assertEquals( array( ),
567 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
568 $this->assertEquals( true,
569 $this->title->userCan( 'create' ) );
570
571
572 $this->setUserPerm( array( 'createpage' ) );
573 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
574 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
575 $this->assertEquals( false,
576 $this->title->userCan( 'create' ) );
577
578 $this->setTitle( NS_MEDIA, "test page" );
579 $this->setUserPerm( array( "move" ) );
580 $this->assertEquals( false,
581 $this->title->userCan( 'move' ) );
582 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
583 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
584
585 $this->setTitle( NS_MAIN, "test page" );
586 $this->assertEquals( array( ),
587 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
588 $this->assertEquals( true,
589 $this->title->userCan( 'move' ) );
590
591 $this->title->mInterwiki = "no";
592 $this->assertEquals( array( array( 'immobile-source-page' ) ),
593 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
594 $this->assertEquals( false,
595 $this->title->userCan( 'move' ) );
596
597 $this->setTitle( NS_MEDIA, "test page" );
598 $this->assertEquals( false,
599 $this->title->userCan( 'move-target' ) );
600 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
601 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
602
603 $this->setTitle( NS_MAIN, "test page" );
604 $this->assertEquals( array( ),
605 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
606 $this->assertEquals( true,
607 $this->title->userCan( 'move-target' ) );
608
609 $this->title->mInterwiki = "no";
610 $this->assertEquals( array( array( 'immobile-target-page' ) ),
611 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
612 $this->assertEquals( false,
613 $this->title->userCan( 'move-target' ) );
614
615 }
616
617 function testUserBlock() {
618 global $wgUser, $wgEmailConfirmToEdit, $wgEmailAuthentication;
619 $wgEmailConfirmToEdit = true;
620 $wgEmailAuthentication = true;
621 $wgUser = $this->user;
622
623 $this->setUserPerm( array( "createpage", "move" ) );
624 $this->setTitle( NS_MAIN, "test page" );
625
626 # $short
627 $this->assertEquals( array( array( 'confirmedittext' ) ),
628 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
629 $wgEmailConfirmToEdit = false;
630 $this->assertEquals( true, $this->title->userCan( 'move-target' ) );
631
632 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
633 $this->assertEquals( array( ),
634 $this->title->getUserPermissionsErrors( 'move-target',
635 $this->user ) );
636
637 global $wgLang;
638 $prev = time();
639 $now = time() + 120;
640 $this->user->mBlockedby = $this->user->getId();
641 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
642 'no reason given', $prev + 3600, 1, 0 );
643 $this->user->mBlock->mTimestamp = 0;
644 $this->assertEquals( array( array( 'autoblockedtext',
645 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
646 'Useruser', null, 'infinite', '127.0.8.1',
647 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
648 $this->title->getUserPermissionsErrors( 'move-target',
649 $this->user ) );
650
651 $this->assertEquals( false, $this->title->userCan( 'move-target' ) );
652 // quickUserCan should ignore user blocks
653 $this->assertEquals( true, $this->title->quickUserCan( 'move-target' ) );
654
655 global $wgLocalTZoffset;
656 $wgLocalTZoffset = -60;
657 $this->user->mBlockedby = $this->user->getName();
658 $this->user->mBlock = new Block( '127.0.8.1', 0, 1, 'no reason given', $now, 0, 10 );
659 $this->assertEquals( array( array( 'blockedtext',
660 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
661 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
662 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
663 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
664
665 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
666 # $user->blockedFor() == ''
667 # $user->mBlock->mExpiry == 'infinity'
668 }
669 }