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