MessagesGom_deva: Correct syntax in namespace alias
[lhc/web/wiklou.git] / tests / phpunit / includes / user / UserTest.php
1 <?php
2
3 define( 'NS_UNITTEST', 5600 );
4 define( 'NS_UNITTEST_TALK', 5601 );
5
6 /**
7 * @group Database
8 */
9 class UserTest extends MediaWikiTestCase {
10 /**
11 * @var User
12 */
13 protected $user;
14
15 protected function setUp() {
16 parent::setUp();
17
18 $this->setMwGlobals( [
19 'wgGroupPermissions' => [],
20 'wgRevokePermissions' => [],
21 ] );
22
23 $this->setUpPermissionGlobals();
24
25 $this->user = new User;
26 $this->user->addGroup( 'unittesters' );
27 }
28
29 private function setUpPermissionGlobals() {
30 global $wgGroupPermissions, $wgRevokePermissions;
31
32 # Data for regular $wgGroupPermissions test
33 $wgGroupPermissions['unittesters'] = [
34 'test' => true,
35 'runtest' => true,
36 'writetest' => false,
37 'nukeworld' => false,
38 ];
39 $wgGroupPermissions['testwriters'] = [
40 'test' => true,
41 'writetest' => true,
42 'modifytest' => true,
43 ];
44
45 # Data for regular $wgRevokePermissions test
46 $wgRevokePermissions['formertesters'] = [
47 'runtest' => true,
48 ];
49
50 # For the options test
51 $wgGroupPermissions['*'] = [
52 'editmyoptions' => true,
53 ];
54 }
55
56 /**
57 * @covers User::getGroupPermissions
58 */
59 public function testGroupPermissions() {
60 $rights = User::getGroupPermissions( [ 'unittesters' ] );
61 $this->assertContains( 'runtest', $rights );
62 $this->assertNotContains( 'writetest', $rights );
63 $this->assertNotContains( 'modifytest', $rights );
64 $this->assertNotContains( 'nukeworld', $rights );
65
66 $rights = User::getGroupPermissions( [ 'unittesters', 'testwriters' ] );
67 $this->assertContains( 'runtest', $rights );
68 $this->assertContains( 'writetest', $rights );
69 $this->assertContains( 'modifytest', $rights );
70 $this->assertNotContains( 'nukeworld', $rights );
71 }
72
73 /**
74 * @covers User::getGroupPermissions
75 */
76 public function testRevokePermissions() {
77 $rights = User::getGroupPermissions( [ 'unittesters', 'formertesters' ] );
78 $this->assertNotContains( 'runtest', $rights );
79 $this->assertNotContains( 'writetest', $rights );
80 $this->assertNotContains( 'modifytest', $rights );
81 $this->assertNotContains( 'nukeworld', $rights );
82 }
83
84 /**
85 * @covers User::getRights
86 */
87 public function testUserPermissions() {
88 $rights = $this->user->getRights();
89 $this->assertContains( 'runtest', $rights );
90 $this->assertNotContains( 'writetest', $rights );
91 $this->assertNotContains( 'modifytest', $rights );
92 $this->assertNotContains( 'nukeworld', $rights );
93 }
94
95 /**
96 * @dataProvider provideGetGroupsWithPermission
97 * @covers User::getGroupsWithPermission
98 */
99 public function testGetGroupsWithPermission( $expected, $right ) {
100 $result = User::getGroupsWithPermission( $right );
101 sort( $result );
102 sort( $expected );
103
104 $this->assertEquals( $expected, $result, "Groups with permission $right" );
105 }
106
107 public static function provideGetGroupsWithPermission() {
108 return [
109 [
110 [ 'unittesters', 'testwriters' ],
111 'test'
112 ],
113 [
114 [ 'unittesters' ],
115 'runtest'
116 ],
117 [
118 [ 'testwriters' ],
119 'writetest'
120 ],
121 [
122 [ 'testwriters' ],
123 'modifytest'
124 ],
125 ];
126 }
127
128 /**
129 * @dataProvider provideIPs
130 * @covers User::isIP
131 */
132 public function testIsIP( $value, $result, $message ) {
133 $this->assertEquals( $this->user->isIP( $value ), $result, $message );
134 }
135
136 public static function provideIPs() {
137 return [
138 [ '', false, 'Empty string' ],
139 [ ' ', false, 'Blank space' ],
140 [ '10.0.0.0', true, 'IPv4 private 10/8' ],
141 [ '10.255.255.255', true, 'IPv4 private 10/8' ],
142 [ '192.168.1.1', true, 'IPv4 private 192.168/16' ],
143 [ '203.0.113.0', true, 'IPv4 example' ],
144 [ '2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff', true, 'IPv6 example' ],
145 // Not valid IPs but classified as such by MediaWiki for negated asserting
146 // of whether this might be the identifier of a logged-out user or whether
147 // to allow usernames like it.
148 [ '300.300.300.300', true, 'Looks too much like an IPv4 address' ],
149 [ '203.0.113.xxx', true, 'Assigned by UseMod to cloaked logged-out users' ],
150 ];
151 }
152
153 /**
154 * @dataProvider provideUserNames
155 * @covers User::isValidUserName
156 */
157 public function testIsValidUserName( $username, $result, $message ) {
158 $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
159 }
160
161 public static function provideUserNames() {
162 return [
163 [ '', false, 'Empty string' ],
164 [ ' ', false, 'Blank space' ],
165 [ 'abcd', false, 'Starts with small letter' ],
166 [ 'Ab/cd', false, 'Contains slash' ],
167 [ 'Ab cd', true, 'Whitespace' ],
168 [ '192.168.1.1', false, 'IP' ],
169 [ 'User:Abcd', false, 'Reserved Namespace' ],
170 [ '12abcd232', true, 'Starts with Numbers' ],
171 [ '?abcd', true, 'Start with ? mark' ],
172 [ '#abcd', false, 'Start with #' ],
173 [ 'Abcdകഖഗഘ', true, ' Mixed scripts' ],
174 [ 'ജോസ്‌തോമസ്', false, 'ZWNJ- Format control character' ],
175 [ 'Ab cd', false, ' Ideographic space' ],
176 [ '300.300.300.300', false, 'Looks too much like an IPv4 address' ],
177 [ '302.113.311.900', false, 'Looks too much like an IPv4 address' ],
178 [ '203.0.113.xxx', false, 'Reserved for usage by UseMod for cloaked logged-out users' ],
179 ];
180 }
181
182 /**
183 * Test, if for all rights a right- message exist,
184 * which is used on Special:ListGroupRights as help text
185 * Extensions and core
186 */
187 public function testAllRightsWithMessage() {
188 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
189 $allRights = User::getAllRights();
190 $allMessageKeys = Language::getMessageKeysFor( 'en' );
191
192 $rightsWithMessage = [];
193 foreach ( $allMessageKeys as $message ) {
194 // === 0: must be at beginning of string (position 0)
195 if ( strpos( $message, 'right-' ) === 0 ) {
196 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
197 }
198 }
199
200 sort( $allRights );
201 sort( $rightsWithMessage );
202
203 $this->assertEquals(
204 $allRights,
205 $rightsWithMessage,
206 'Each user rights (core/extensions) has a corresponding right- message.'
207 );
208 }
209
210 /**
211 * Test User::editCount
212 * @group medium
213 * @covers User::getEditCount
214 */
215 public function testEditCount() {
216 $user = $this->getMutableTestUser()->getUser();
217
218 // let the user have a few (3) edits
219 $page = WikiPage::factory( Title::newFromText( 'Help:UserTest_EditCount' ) );
220 for ( $i = 0; $i < 3; $i++ ) {
221 $page->doEdit( (string)$i, 'test', 0, false, $user );
222 }
223
224 $user->clearInstanceCache();
225 $this->assertEquals(
226 3,
227 $user->getEditCount(),
228 'After three edits, the user edit count should be 3'
229 );
230
231 // increase the edit count and clear the cache
232 $user->incEditCount();
233
234 $user->clearInstanceCache();
235 $this->assertEquals(
236 4,
237 $user->getEditCount(),
238 'After increasing the edit count manually, the user edit count should be 4'
239 );
240 }
241
242 /**
243 * Test changing user options.
244 * @covers User::setOption
245 * @covers User::getOption
246 */
247 public function testOptions() {
248 $user = $this->getMutableTestUser()->getUser();
249
250 $user->setOption( 'userjs-someoption', 'test' );
251 $user->setOption( 'cols', 200 );
252 $user->saveSettings();
253
254 $user = User::newFromName( $user->getName() );
255 $this->assertEquals( 'test', $user->getOption( 'userjs-someoption' ) );
256 $this->assertEquals( 200, $user->getOption( 'cols' ) );
257 }
258
259 /**
260 * Bug 37963
261 * Make sure defaults are loaded when setOption is called.
262 * @covers User::loadOptions
263 */
264 public function testAnonOptions() {
265 global $wgDefaultUserOptions;
266 $this->user->setOption( 'userjs-someoption', 'test' );
267 $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user->getOption( 'cols' ) );
268 $this->assertEquals( 'test', $this->user->getOption( 'userjs-someoption' ) );
269 }
270
271 /**
272 * Test password validity checks. There are 3 checks in core,
273 * - ensure the password meets the minimal length
274 * - ensure the password is not the same as the username
275 * - ensure the username/password combo isn't forbidden
276 * @covers User::checkPasswordValidity()
277 * @covers User::getPasswordValidity()
278 * @covers User::isValidPassword()
279 */
280 public function testCheckPasswordValidity() {
281 $this->setMwGlobals( [
282 'wgPasswordPolicy' => [
283 'policies' => [
284 'sysop' => [
285 'MinimalPasswordLength' => 8,
286 'MinimumPasswordLengthToLogin' => 1,
287 'PasswordCannotMatchUsername' => 1,
288 ],
289 'default' => [
290 'MinimalPasswordLength' => 6,
291 'PasswordCannotMatchUsername' => true,
292 'PasswordCannotMatchBlacklist' => true,
293 'MaximalPasswordLength' => 40,
294 ],
295 ],
296 'checks' => [
297 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
298 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
299 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
300 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
301 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
302 ],
303 ],
304 ] );
305
306 $user = static::getTestUser()->getUser();
307
308 // Sanity
309 $this->assertTrue( $user->isValidPassword( 'Password1234' ) );
310
311 // Minimum length
312 $this->assertFalse( $user->isValidPassword( 'a' ) );
313 $this->assertFalse( $user->checkPasswordValidity( 'a' )->isGood() );
314 $this->assertTrue( $user->checkPasswordValidity( 'a' )->isOK() );
315 $this->assertEquals( 'passwordtooshort', $user->getPasswordValidity( 'a' ) );
316
317 // Maximum length
318 $longPass = str_repeat( 'a', 41 );
319 $this->assertFalse( $user->isValidPassword( $longPass ) );
320 $this->assertFalse( $user->checkPasswordValidity( $longPass )->isGood() );
321 $this->assertFalse( $user->checkPasswordValidity( $longPass )->isOK() );
322 $this->assertEquals( 'passwordtoolong', $user->getPasswordValidity( $longPass ) );
323
324 // Matches username
325 $this->assertFalse( $user->checkPasswordValidity( $user->getName() )->isGood() );
326 $this->assertTrue( $user->checkPasswordValidity( $user->getName() )->isOK() );
327 $this->assertEquals( 'password-name-match', $user->getPasswordValidity( $user->getName() ) );
328
329 // On the forbidden list
330 $user = User::newFromName( 'Useruser' );
331 $this->assertFalse( $user->checkPasswordValidity( 'Passpass' )->isGood() );
332 $this->assertEquals( 'password-login-forbidden', $user->getPasswordValidity( 'Passpass' ) );
333 }
334
335 /**
336 * @covers User::getCanonicalName()
337 * @dataProvider provideGetCanonicalName
338 */
339 public function testGetCanonicalName( $name, $expectedArray ) {
340 // fake interwiki map for the 'Interwiki prefix' testcase
341 $this->mergeMwGlobalArrayValue( 'wgHooks', [
342 'InterwikiLoadPrefix' => [
343 function ( $prefix, &$iwdata ) {
344 if ( $prefix === 'interwiki' ) {
345 $iwdata = [
346 'iw_url' => 'http://example.com/',
347 'iw_local' => 0,
348 'iw_trans' => 0,
349 ];
350 return false;
351 }
352 },
353 ],
354 ] );
355
356 foreach ( $expectedArray as $validate => $expected ) {
357 $this->assertEquals(
358 $expected,
359 User::getCanonicalName( $name, $validate === 'false' ? false : $validate ), $validate );
360 }
361 }
362
363 public static function provideGetCanonicalName() {
364 return [
365 'Leading space' => [ ' Leading space', [ 'creatable' => 'Leading space' ] ],
366 'Trailing space ' => [ 'Trailing space ', [ 'creatable' => 'Trailing space' ] ],
367 'Namespace prefix' => [ 'Talk:Username', [ 'creatable' => false, 'usable' => false,
368 'valid' => false, 'false' => 'Talk:Username' ] ],
369 'Interwiki prefix' => [ 'interwiki:Username', [ 'creatable' => false, 'usable' => false,
370 'valid' => false, 'false' => 'Interwiki:Username' ] ],
371 'With hash' => [ 'name with # hash', [ 'creatable' => false, 'usable' => false ] ],
372 'Multi spaces' => [ 'Multi spaces', [ 'creatable' => 'Multi spaces',
373 'usable' => 'Multi spaces' ] ],
374 'Lowercase' => [ 'lowercase', [ 'creatable' => 'Lowercase' ] ],
375 'Invalid character' => [ 'in[]valid', [ 'creatable' => false, 'usable' => false,
376 'valid' => false, 'false' => 'In[]valid' ] ],
377 'With slash' => [ 'with / slash', [ 'creatable' => false, 'usable' => false, 'valid' => false,
378 'false' => 'With / slash' ] ],
379 ];
380 }
381
382 /**
383 * @covers User::equals
384 */
385 public function testEquals() {
386 $first = $this->getMutableTestUser()->getUser();
387 $second = User::newFromName( $first->getName() );
388
389 $this->assertTrue( $first->equals( $first ) );
390 $this->assertTrue( $first->equals( $second ) );
391 $this->assertTrue( $second->equals( $first ) );
392
393 $third = $this->getMutableTestUser()->getUser();
394 $fourth = $this->getMutableTestUser()->getUser();
395
396 $this->assertFalse( $third->equals( $fourth ) );
397 $this->assertFalse( $fourth->equals( $third ) );
398
399 // Test users loaded from db with id
400 $user = $this->getMutableTestUser()->getUser();
401 $fifth = User::newFromId( $user->getId() );
402 $sixth = User::newFromName( $user->getName() );
403 $this->assertTrue( $fifth->equals( $sixth ) );
404 }
405
406 /**
407 * @covers User::getId
408 */
409 public function testGetId() {
410 $user = static::getTestUser()->getUser();
411 $this->assertTrue( $user->getId() > 0 );
412
413 }
414
415 /**
416 * @covers User::isLoggedIn
417 * @covers User::isAnon
418 */
419 public function testLoggedIn() {
420 $user = $this->getMutableTestUser()->getUser();
421 $this->assertTrue( $user->isLoggedIn() );
422 $this->assertFalse( $user->isAnon() );
423
424 // Non-existent users are perceived as anonymous
425 $user = User::newFromName( 'UTNonexistent' );
426 $this->assertFalse( $user->isLoggedIn() );
427 $this->assertTrue( $user->isAnon() );
428
429 $user = new User;
430 $this->assertFalse( $user->isLoggedIn() );
431 $this->assertTrue( $user->isAnon() );
432 }
433
434 /**
435 * @covers User::checkAndSetTouched
436 */
437 public function testCheckAndSetTouched() {
438 $user = $this->getMutableTestUser()->getUser();
439 $user = TestingAccessWrapper::newFromObject( $user );
440 $this->assertTrue( $user->isLoggedIn() );
441
442 $touched = $user->getDBTouched();
443 $this->assertTrue(
444 $user->checkAndSetTouched(), "checkAndSetTouched() succeded" );
445 $this->assertGreaterThan(
446 $touched, $user->getDBTouched(), "user_touched increased with casOnTouched()" );
447
448 $touched = $user->getDBTouched();
449 $this->assertTrue(
450 $user->checkAndSetTouched(), "checkAndSetTouched() succeded #2" );
451 $this->assertGreaterThan(
452 $touched, $user->getDBTouched(), "user_touched increased with casOnTouched() #2" );
453 }
454 }