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