Add DROP INDEX support to DatabaseSqlite::replaceVars method
[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 'wgLanguageCode' => 'en',
34 'wgLang' => $langObj,
35 'wgLocaltimezone' => $localZone,
36 'wgLocalTZoffset' => $localOffset,
37 'wgNamespaceProtection' => array(
38 NS_MEDIAWIKI => 'editinterface',
39 ),
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 function setUserPerm( $perm ) {
72 // Setting member variables is evil!!!
73
74 if ( is_array( $perm ) ) {
75 $this->user->mRights = $perm;
76 } else {
77 $this->user->mRights = array( $perm );
78 }
79 }
80
81 function setTitle( $ns, $title = "Main_Page" ) {
82 $this->title = Title::makeTitle( $ns, $title );
83 }
84
85 function setUser( $userName = null ) {
86 if ( $userName === 'anon' ) {
87 $this->user = $this->anonUser;
88 } elseif ( $userName === null || $userName === $this->userName ) {
89 $this->user = $this->userUser;
90 } else {
91 $this->user = $this->altUser;
92 }
93 }
94
95 function testQuickPermissions() {
96 global $wgContLang;
97 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
98
99 $this->setUser( 'anon' );
100 $this->setTitle( NS_TALK );
101 $this->setUserPerm( "createtalk" );
102 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
103 $this->assertEquals( array(), $res );
104
105 $this->setTitle( NS_TALK );
106 $this->setUserPerm( "createpage" );
107 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
108 $this->assertEquals( array( array( "nocreatetext" ) ), $res );
109
110 $this->setTitle( NS_TALK );
111 $this->setUserPerm( "" );
112 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
113 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
114
115 $this->setTitle( NS_MAIN );
116 $this->setUserPerm( "createpage" );
117 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
118 $this->assertEquals( array(), $res );
119
120 $this->setTitle( NS_MAIN );
121 $this->setUserPerm( "createtalk" );
122 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
123 $this->assertEquals( array( array( 'nocreatetext' ) ), $res );
124
125 $this->setUser( $this->userName );
126 $this->setTitle( NS_TALK );
127 $this->setUserPerm( "createtalk" );
128 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
129 $this->assertEquals( array(), $res );
130
131 $this->setTitle( NS_TALK );
132 $this->setUserPerm( "createpage" );
133 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
134 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
135
136 $this->setTitle( NS_TALK );
137 $this->setUserPerm( "" );
138 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
139 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
140
141 $this->setTitle( NS_MAIN );
142 $this->setUserPerm( "createpage" );
143 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
144 $this->assertEquals( array(), $res );
145
146 $this->setTitle( NS_MAIN );
147 $this->setUserPerm( "createtalk" );
148 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
149 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
150
151 $this->setTitle( NS_MAIN );
152 $this->setUserPerm( "" );
153 $res = $this->title->getUserPermissionsErrors( 'create', $this->user );
154 $this->assertEquals( array( array( 'nocreate-loggedin' ) ), $res );
155
156 $this->setUser( 'anon' );
157 $this->setTitle( NS_USER, $this->userName . '' );
158 $this->setUserPerm( "" );
159 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
160 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
161
162 $this->setTitle( NS_USER, $this->userName . '/subpage' );
163 $this->setUserPerm( "" );
164 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
165 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
166
167 $this->setTitle( NS_USER, $this->userName . '' );
168 $this->setUserPerm( "move-rootuserpages" );
169 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
170 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
171
172 $this->setTitle( NS_USER, $this->userName . '/subpage' );
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 . '' );
178 $this->setUserPerm( "" );
179 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
180 $this->assertEquals( array( array( 'cant-move-user-page' ), array( 'movenologintext' ) ), $res );
181
182 $this->setTitle( NS_USER, $this->userName . '/subpage' );
183 $this->setUserPerm( "" );
184 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
185 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
186
187 $this->setTitle( NS_USER, $this->userName . '' );
188 $this->setUserPerm( "move-rootuserpages" );
189 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
190 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
191
192 $this->setTitle( NS_USER, $this->userName . '/subpage' );
193 $this->setUserPerm( "move-rootuserpages" );
194 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
195 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
196
197 $this->setUser( $this->userName );
198 $this->setTitle( NS_FILE, "img.png" );
199 $this->setUserPerm( "" );
200 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
201 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ), $res );
202
203 $this->setTitle( NS_FILE, "img.png" );
204 $this->setUserPerm( "movefile" );
205 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
206 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
207
208 $this->setUser( 'anon' );
209 $this->setTitle( NS_FILE, "img.png" );
210 $this->setUserPerm( "" );
211 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
212 $this->assertEquals( array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ), $res );
213
214 $this->setTitle( NS_FILE, "img.png" );
215 $this->setUserPerm( "movefile" );
216 $res = $this->title->getUserPermissionsErrors( 'move', $this->user );
217 $this->assertEquals( array( array( 'movenologintext' ) ), $res );
218
219 $this->setUser( $this->userName );
220 $this->setUserPerm( "move" );
221 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
222
223 $this->setUserPerm( "" );
224 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ) );
225
226 $this->setUser( 'anon' );
227 $this->setUserPerm( "move" );
228 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ) ) );
229
230 $this->setUserPerm( "" );
231 $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
232 array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ) );
233
234 if ( $this->isWikitextNS( NS_MAIN ) ) {
235 //NOTE: some content models don't allow moving
236 // @todo find a Wikitext namespace for testing
237
238 $this->setTitle( NS_MAIN );
239 $this->setUser( 'anon' );
240 $this->setUserPerm( "move" );
241 $this->runGroupPermissions( 'move', array() );
242
243 $this->setUserPerm( "" );
244 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
245 array( array( 'movenologintext' ) ) );
246
247 $this->setUser( $this->userName );
248 $this->setUserPerm( "" );
249 $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
250
251 $this->setUserPerm( "move" );
252 $this->runGroupPermissions( 'move', array() );
253
254 $this->setUser( 'anon' );
255 $this->setUserPerm( 'move' );
256 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
257 $this->assertEquals( array(), $res );
258
259 $this->setUserPerm( '' );
260 $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
261 $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
262 }
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
297 foreach ( array( "edit", "protect", "" ) as $action ) {
298 $this->setUserPerm( null );
299 $this->assertEquals( $check[$action][0],
300 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
301
302 global $wgGroupPermissions;
303 $old = $wgGroupPermissions;
304 $wgGroupPermissions = array();
305
306 $this->assertEquals( $check[$action][1],
307 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
308 $wgGroupPermissions = $old;
309
310 $this->setUserPerm( $action );
311 $this->assertEquals( $check[$action][2],
312 $this->title->getUserPermissionsErrors( $action, $this->user, true ) );
313
314 $this->setUserPerm( $action );
315 $this->assertEquals( $check[$action][3],
316 $this->title->userCan( $action, $this->user, true ) );
317 $this->assertEquals( $check[$action][3],
318 $this->title->quickUserCan( $action, $this->user ) );
319 # count( User::getGroupsWithPermissions( $action ) ) < 1
320 }
321 }
322
323 function runGroupPermissions( $action, $result, $result2 = null ) {
324 global $wgGroupPermissions;
325
326 if ( $result2 === null ) {
327 $result2 = $result;
328 }
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
360 $this->setTitle( NS_MAIN );
361 $this->setUserPerm( 'bogus' );
362 $this->assertEquals( array(),
363 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
364
365 $this->setTitle( NS_MAIN );
366 $this->setUserPerm( '' );
367 $this->assertEquals( array( array( 'badaccess-group0' ) ),
368 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
369
370 $wgNamespaceProtection[NS_USER] = array( 'bogus' );
371
372 $this->setTitle( NS_USER );
373 $this->setUserPerm( '' );
374 $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
375 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
376
377 $this->setTitle( NS_MEDIAWIKI );
378 $this->setUserPerm( 'bogus' );
379 $this->assertEquals( array( array( 'protectedinterface' ) ),
380 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
381
382 $this->setTitle( NS_MEDIAWIKI );
383 $this->setUserPerm( 'bogus' );
384 $this->assertEquals( array( array( 'protectedinterface' ) ),
385 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
386
387 $wgNamespaceProtection = null;
388
389 $this->setUserPerm( 'bogus' );
390 $this->assertEquals( array(),
391 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
392 $this->assertEquals( true,
393 $this->title->userCan( 'bogus', $this->user ) );
394
395 $this->setUserPerm( '' );
396 $this->assertEquals( array( array( 'badaccess-group0' ) ),
397 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
398 $this->assertEquals( false,
399 $this->title->userCan( 'bogus', $this->user ) );
400 }
401
402 function testCssAndJavascriptPermissions() {
403 $this->setUser( $this->userName );
404
405 $this->setTitle( NS_USER, $this->userName . '/test.js' );
406 $this->runCSSandJSPermissions(
407 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
408 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
409 array( array( 'badaccess-group0' ) ),
410 array( array( 'badaccess-group0' ), array( 'mycustomjsprotected' ) ),
411 array( array( 'badaccess-group0' ) )
412 );
413
414 $this->setTitle( NS_USER, $this->userName . '/test.css' );
415 $this->runCSSandJSPermissions(
416 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
417 array( array( 'badaccess-group0' ) ),
418 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) ),
419 array( array( 'badaccess-group0' ) ),
420 array( array( 'badaccess-group0' ), array( 'mycustomcssprotected' ) )
421 );
422
423 $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
424 $this->runCSSandJSPermissions(
425 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
426 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
427 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
428 array( array( 'badaccess-group0' ), array( 'customjsprotected' ) ),
429 array( array( 'badaccess-group0' ) )
430 );
431
432 $this->setTitle( NS_USER, $this->altUserName . '/test.css' );
433 $this->runCSSandJSPermissions(
434 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
435 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
436 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) ),
437 array( array( 'badaccess-group0' ) ),
438 array( array( 'badaccess-group0' ), array( 'customcssprotected' ) )
439 );
440
441 $this->setTitle( NS_USER, $this->altUserName . '/tempo' );
442 $this->runCSSandJSPermissions(
443 array( array( 'badaccess-group0' ) ),
444 array( array( 'badaccess-group0' ) ),
445 array( array( 'badaccess-group0' ) ),
446 array( array( 'badaccess-group0' ) ),
447 array( array( 'badaccess-group0' ) )
448 );
449 }
450
451 function runCSSandJSPermissions( $result0, $result1, $result2, $result3, $result4 ) {
452 $this->setUserPerm( '' );
453 $this->assertEquals( $result0,
454 $this->title->getUserPermissionsErrors( 'bogus',
455 $this->user ) );
456
457 $this->setUserPerm( 'editmyusercss' );
458 $this->assertEquals( $result1,
459 $this->title->getUserPermissionsErrors( 'bogus',
460 $this->user ) );
461
462 $this->setUserPerm( 'editmyuserjs' );
463 $this->assertEquals( $result2,
464 $this->title->getUserPermissionsErrors( 'bogus',
465 $this->user ) );
466
467 $this->setUserPerm( 'editusercss' );
468 $this->assertEquals( $result3,
469 $this->title->getUserPermissionsErrors( 'bogus',
470 $this->user ) );
471
472 $this->setUserPerm( 'edituserjs' );
473 $this->assertEquals( $result4,
474 $this->title->getUserPermissionsErrors( 'bogus',
475 $this->user ) );
476
477 $this->setUserPerm( 'editusercssjs' );
478 $this->assertEquals( array( array( 'badaccess-group0' ) ),
479 $this->title->getUserPermissionsErrors( 'bogus',
480 $this->user ) );
481
482 $this->setUserPerm( array( 'edituserjs', 'editusercss' ) );
483 $this->assertEquals( array( array( 'badaccess-group0' ) ),
484 $this->title->getUserPermissionsErrors( 'bogus',
485 $this->user ) );
486 }
487
488 function testPageRestrictions() {
489 global $wgContLang;
490
491 $prefix = $wgContLang->getFormattedNsText( NS_PROJECT );
492
493 $this->setTitle( NS_MAIN );
494 $this->title->mRestrictionsLoaded = true;
495 $this->setUserPerm( "edit" );
496 $this->title->mRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
497
498 $this->assertEquals( array(),
499 $this->title->getUserPermissionsErrors( 'edit',
500 $this->user ) );
501
502 $this->assertEquals( true,
503 $this->title->quickUserCan( 'edit', $this->user ) );
504 $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
505 "bogus" => array( 'bogus', "sysop", "protect", "" ) );
506
507 $this->assertEquals( array( array( 'badaccess-group0' ),
508 array( 'protectedpagetext', 'bogus' ),
509 array( 'protectedpagetext', 'editprotected' ),
510 array( 'protectedpagetext', 'protect' ) ),
511 $this->title->getUserPermissionsErrors( 'bogus',
512 $this->user ) );
513 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
514 array( 'protectedpagetext', 'editprotected' ),
515 array( 'protectedpagetext', 'protect' ) ),
516 $this->title->getUserPermissionsErrors( 'edit',
517 $this->user ) );
518 $this->setUserPerm( "" );
519 $this->assertEquals( array( array( 'badaccess-group0' ),
520 array( 'protectedpagetext', 'bogus' ),
521 array( 'protectedpagetext', 'editprotected' ),
522 array( 'protectedpagetext', 'protect' ) ),
523 $this->title->getUserPermissionsErrors( 'bogus',
524 $this->user ) );
525 $this->assertEquals( array( array( 'badaccess-groups', "*, [[$prefix:Users|Users]]", 2 ),
526 array( 'protectedpagetext', 'bogus' ),
527 array( 'protectedpagetext', 'editprotected' ),
528 array( 'protectedpagetext', 'protect' ) ),
529 $this->title->getUserPermissionsErrors( 'edit',
530 $this->user ) );
531 $this->setUserPerm( array( "edit", "editprotected" ) );
532 $this->assertEquals( array( array( 'badaccess-group0' ),
533 array( 'protectedpagetext', 'bogus' ),
534 array( 'protectedpagetext', 'protect' ) ),
535 $this->title->getUserPermissionsErrors( 'bogus',
536 $this->user ) );
537 $this->assertEquals( array(
538 array( 'protectedpagetext', 'bogus' ),
539 array( 'protectedpagetext', 'protect' ) ),
540 $this->title->getUserPermissionsErrors( 'edit',
541 $this->user ) );
542
543 $this->title->mCascadeRestriction = true;
544 $this->setUserPerm( "edit" );
545 $this->assertEquals( false,
546 $this->title->quickUserCan( 'bogus', $this->user ) );
547 $this->assertEquals( false,
548 $this->title->quickUserCan( 'edit', $this->user ) );
549 $this->assertEquals( array( array( 'badaccess-group0' ),
550 array( 'protectedpagetext', 'bogus' ),
551 array( 'protectedpagetext', 'editprotected' ),
552 array( 'protectedpagetext', 'protect' ) ),
553 $this->title->getUserPermissionsErrors( 'bogus',
554 $this->user ) );
555 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
556 array( 'protectedpagetext', 'editprotected' ),
557 array( 'protectedpagetext', 'protect' ) ),
558 $this->title->getUserPermissionsErrors( 'edit',
559 $this->user ) );
560
561 $this->setUserPerm( array( "edit", "editprotected" ) );
562 $this->assertEquals( false,
563 $this->title->quickUserCan( 'bogus', $this->user ) );
564 $this->assertEquals( false,
565 $this->title->quickUserCan( 'edit', $this->user ) );
566 $this->assertEquals( array( array( 'badaccess-group0' ),
567 array( 'protectedpagetext', 'bogus' ),
568 array( 'protectedpagetext', 'protect' ),
569 array( 'protectedpagetext', 'protect' ) ),
570 $this->title->getUserPermissionsErrors( 'bogus',
571 $this->user ) );
572 $this->assertEquals( array( array( 'protectedpagetext', 'bogus' ),
573 array( 'protectedpagetext', 'protect' ),
574 array( 'protectedpagetext', 'protect' ) ),
575 $this->title->getUserPermissionsErrors( 'edit',
576 $this->user ) );
577 }
578
579 function testCascadingSourcesRestrictions() {
580 $this->setTitle( NS_MAIN, "test page" );
581 $this->setUserPerm( array( "edit", "bogus" ) );
582
583 $this->title->mCascadeSources = array( Title::makeTitle( NS_MAIN, "Bogus" ), Title::makeTitle( NS_MAIN, "UnBogus" ) );
584 $this->title->mCascadingRestrictions = array( "bogus" => array( 'bogus', "sysop", "protect", "" ) );
585
586 $this->assertEquals( false,
587 $this->title->userCan( 'bogus', $this->user ) );
588 $this->assertEquals( array( array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
589 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ),
590 array( "cascadeprotected", 2, "* [[:Bogus]]\n* [[:UnBogus]]\n" ) ),
591 $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
592
593 $this->assertEquals( true,
594 $this->title->userCan( 'edit', $this->user ) );
595 $this->assertEquals( array(),
596 $this->title->getUserPermissionsErrors( 'edit', $this->user ) );
597 }
598
599 function testActionPermissions() {
600 $this->setUserPerm( array( "createpage" ) );
601 $this->setTitle( NS_MAIN, "test page" );
602 $this->title->mTitleProtection['pt_create_perm'] = '';
603 $this->title->mTitleProtection['pt_user'] = $this->user->getID();
604 $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
605 $this->title->mTitleProtection['pt_reason'] = 'test';
606 $this->title->mCascadeRestriction = false;
607
608 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
609 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
610 $this->assertEquals( false,
611 $this->title->userCan( 'create', $this->user ) );
612
613 $this->title->mTitleProtection['pt_create_perm'] = 'sysop';
614 $this->setUserPerm( array( 'createpage', 'protect' ) );
615 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
616 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
617 $this->assertEquals( false,
618 $this->title->userCan( 'create', $this->user ) );
619
620 $this->setUserPerm( array( 'createpage', 'editprotected' ) );
621 $this->assertEquals( array(),
622 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
623 $this->assertEquals( true,
624 $this->title->userCan( 'create', $this->user ) );
625
626 $this->setUserPerm( array( 'createpage' ) );
627 $this->assertEquals( array( array( 'titleprotected', 'Useruser', 'test' ) ),
628 $this->title->getUserPermissionsErrors( 'create', $this->user ) );
629 $this->assertEquals( false,
630 $this->title->userCan( 'create', $this->user ) );
631
632 $this->setTitle( NS_MEDIA, "test page" );
633 $this->setUserPerm( array( "move" ) );
634 $this->assertEquals( false,
635 $this->title->userCan( 'move', $this->user ) );
636 $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
637 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
638
639 $this->setTitle( NS_HELP, "test page" );
640 $this->assertEquals( array(),
641 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
642 $this->assertEquals( true,
643 $this->title->userCan( 'move', $this->user ) );
644
645 $this->title->mInterwiki = "no";
646 $this->assertEquals( array( array( 'immobile-source-page' ) ),
647 $this->title->getUserPermissionsErrors( 'move', $this->user ) );
648 $this->assertEquals( false,
649 $this->title->userCan( 'move', $this->user ) );
650
651 $this->setTitle( NS_MEDIA, "test page" );
652 $this->assertEquals( false,
653 $this->title->userCan( 'move-target', $this->user ) );
654 $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
655 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
656
657 $this->setTitle( NS_HELP, "test page" );
658 $this->assertEquals( array(),
659 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
660 $this->assertEquals( true,
661 $this->title->userCan( 'move-target', $this->user ) );
662
663 $this->title->mInterwiki = "no";
664 $this->assertEquals( array( array( 'immobile-target-page' ) ),
665 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
666 $this->assertEquals( false,
667 $this->title->userCan( 'move-target', $this->user ) );
668 }
669
670 function testUserBlock() {
671 global $wgEmailConfirmToEdit, $wgEmailAuthentication;
672 $wgEmailConfirmToEdit = true;
673 $wgEmailAuthentication = true;
674
675 $this->setUserPerm( array( "createpage", "move" ) );
676 $this->setTitle( NS_HELP, "test page" );
677
678 # $short
679 $this->assertEquals( array( array( 'confirmedittext' ) ),
680 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
681 $wgEmailConfirmToEdit = false;
682 $this->assertEquals( true, $this->title->userCan( 'move-target', $this->user ) );
683
684 # $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount'
685 $this->assertEquals( array(),
686 $this->title->getUserPermissionsErrors( 'move-target',
687 $this->user ) );
688
689 global $wgLang;
690 $prev = time();
691 $now = time() + 120;
692 $this->user->mBlockedby = $this->user->getId();
693 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
694 'no reason given', $prev + 3600, 1, 0 );
695 $this->user->mBlock->mTimestamp = 0;
696 $this->assertEquals( array( array( 'autoblockedtext',
697 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
698 'Useruser', null, 'infinite', '127.0.8.1',
699 $wgLang->timeanddate( wfTimestamp( TS_MW, $prev ), true ) ) ),
700 $this->title->getUserPermissionsErrors( 'move-target',
701 $this->user ) );
702
703 $this->assertEquals( false, $this->title->userCan( 'move-target', $this->user ) );
704 // quickUserCan should ignore user blocks
705 $this->assertEquals( true, $this->title->quickUserCan( 'move-target', $this->user ) );
706
707 global $wgLocalTZoffset;
708 $wgLocalTZoffset = -60;
709 $this->user->mBlockedby = $this->user->getName();
710 $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
711 'no reason given', $now, 0, 10 );
712 $this->assertEquals( array( array( 'blockedtext',
713 '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
714 'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',
715 $wgLang->timeanddate( wfTimestamp( TS_MW, $now ), true ) ) ),
716 $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
717 # $action != 'read' && $action != 'createaccount' && $user->isBlockedFrom( $this )
718 # $user->blockedFor() == ''
719 # $user->mBlock->mExpiry == 'infinity'
720 }
721 }