3 define( 'NS_UNITTEST', 5600 );
4 define( 'NS_UNITTEST_TALK', 5601 );
9 class UserTest
extends MediaWikiTestCase
{
15 protected function setUp() {
18 $this->setMwGlobals( [
19 'wgGroupPermissions' => [],
20 'wgRevokePermissions' => [],
23 $this->setUpPermissionGlobals();
25 $this->user
= new User
;
26 $this->user
->addGroup( 'unittesters' );
29 private function setUpPermissionGlobals() {
30 global $wgGroupPermissions, $wgRevokePermissions;
32 # Data for regular $wgGroupPermissions test
33 $wgGroupPermissions['unittesters'] = [
39 $wgGroupPermissions['testwriters'] = [
45 # Data for regular $wgRevokePermissions test
46 $wgRevokePermissions['formertesters'] = [
50 # For the options test
51 $wgGroupPermissions['*'] = [
52 'editmyoptions' => true,
57 * @covers User::getGroupPermissions
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 );
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 );
74 * @covers User::getGroupPermissions
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 );
85 * @covers User::getRights
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 );
96 * @dataProvider provideGetGroupsWithPermission
97 * @covers User::getGroupsWithPermission
99 public function testGetGroupsWithPermission( $expected, $right ) {
100 $result = User
::getGroupsWithPermission( $right );
104 $this->assertEquals( $expected, $result, "Groups with permission $right" );
107 public static function provideGetGroupsWithPermission() {
110 [ 'unittesters', 'testwriters' ],
129 * @dataProvider provideIPs
132 public function testIsIP( $value, $result, $message ) {
133 $this->assertEquals( $this->user
->isIP( $value ), $result, $message );
136 public static function provideIPs() {
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' ],
154 * @dataProvider provideUserNames
155 * @covers User::isValidUserName
157 public function testIsValidUserName( $username, $result, $message ) {
158 $this->assertEquals( $this->user
->isValidUserName( $username ), $result, $message );
161 public static function provideUserNames() {
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' ],
183 * Test, if for all rights a right- message exist,
184 * which is used on Special:ListGroupRights as help text
185 * Extensions and core
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' );
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-' ) );
201 sort( $rightsWithMessage );
206 'Each user rights (core/extensions) has a corresponding right- message.'
211 * Test User::editCount
213 * @covers User::getEditCount
215 public function testGetEditCount() {
216 $user = $this->getMutableTestUser()->getUser();
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 );
226 $user->getEditCount(),
227 'After three edits, the user edit count should be 3'
230 // increase the edit count
231 $user->incEditCount();
235 $user->getEditCount(),
236 'After increasing the edit count manually, the user edit count should be 4'
241 * Test User::editCount
243 * @covers User::getEditCount
245 public function testGetEditCountForAnons() {
246 $user = User
::newFromName( 'Anonymous' );
249 $user->getEditCount(),
250 'Edit count starts null for anonymous users.'
253 $user->incEditCount();
256 $user->getEditCount(),
257 'Edit count remains null for anonymous users despite calls to increase it.'
262 * Test User::editCount
264 * @covers User::incEditCount
266 public function testIncEditCount() {
267 $user = $this->getMutableTestUser()->getUser();
268 $user->incEditCount();
270 $reloadedUser = User
::newFromId( $user->getId() );
271 $reloadedUser->incEditCount();
275 $reloadedUser->getEditCount(),
276 'Increasing the edit count after a fresh load leaves the object up to date.'
281 * Test changing user options.
282 * @covers User::setOption
283 * @covers User::getOption
285 public function testOptions() {
286 $user = $this->getMutableTestUser()->getUser();
288 $user->setOption( 'userjs-someoption', 'test' );
289 $user->setOption( 'cols', 200 );
290 $user->saveSettings();
292 $user = User
::newFromName( $user->getName() );
293 $this->assertEquals( 'test', $user->getOption( 'userjs-someoption' ) );
294 $this->assertEquals( 200, $user->getOption( 'cols' ) );
299 * Make sure defaults are loaded when setOption is called.
300 * @covers User::loadOptions
302 public function testAnonOptions() {
303 global $wgDefaultUserOptions;
304 $this->user
->setOption( 'userjs-someoption', 'test' );
305 $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user
->getOption( 'cols' ) );
306 $this->assertEquals( 'test', $this->user
->getOption( 'userjs-someoption' ) );
310 * Test password validity checks. There are 3 checks in core,
311 * - ensure the password meets the minimal length
312 * - ensure the password is not the same as the username
313 * - ensure the username/password combo isn't forbidden
314 * @covers User::checkPasswordValidity()
315 * @covers User::getPasswordValidity()
316 * @covers User::isValidPassword()
318 public function testCheckPasswordValidity() {
319 $this->setMwGlobals( [
320 'wgPasswordPolicy' => [
323 'MinimalPasswordLength' => 8,
324 'MinimumPasswordLengthToLogin' => 1,
325 'PasswordCannotMatchUsername' => 1,
328 'MinimalPasswordLength' => 6,
329 'PasswordCannotMatchUsername' => true,
330 'PasswordCannotMatchBlacklist' => true,
331 'MaximalPasswordLength' => 40,
335 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
336 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
337 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
338 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
339 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
344 $user = static::getTestUser()->getUser();
347 $this->assertTrue( $user->isValidPassword( 'Password1234' ) );
350 $this->assertFalse( $user->isValidPassword( 'a' ) );
351 $this->assertFalse( $user->checkPasswordValidity( 'a' )->isGood() );
352 $this->assertTrue( $user->checkPasswordValidity( 'a' )->isOK() );
353 $this->assertEquals( 'passwordtooshort', $user->getPasswordValidity( 'a' ) );
356 $longPass = str_repeat( 'a', 41 );
357 $this->assertFalse( $user->isValidPassword( $longPass ) );
358 $this->assertFalse( $user->checkPasswordValidity( $longPass )->isGood() );
359 $this->assertFalse( $user->checkPasswordValidity( $longPass )->isOK() );
360 $this->assertEquals( 'passwordtoolong', $user->getPasswordValidity( $longPass ) );
363 $this->assertFalse( $user->checkPasswordValidity( $user->getName() )->isGood() );
364 $this->assertTrue( $user->checkPasswordValidity( $user->getName() )->isOK() );
365 $this->assertEquals( 'password-name-match', $user->getPasswordValidity( $user->getName() ) );
367 // On the forbidden list
368 $user = User
::newFromName( 'Useruser' );
369 $this->assertFalse( $user->checkPasswordValidity( 'Passpass' )->isGood() );
370 $this->assertEquals( 'password-login-forbidden', $user->getPasswordValidity( 'Passpass' ) );
374 * @covers User::getCanonicalName()
375 * @dataProvider provideGetCanonicalName
377 public function testGetCanonicalName( $name, $expectedArray ) {
378 // fake interwiki map for the 'Interwiki prefix' testcase
379 $this->mergeMwGlobalArrayValue( 'wgHooks', [
380 'InterwikiLoadPrefix' => [
381 function ( $prefix, &$iwdata ) {
382 if ( $prefix === 'interwiki' ) {
384 'iw_url' => 'http://example.com/',
394 foreach ( $expectedArray as $validate => $expected ) {
397 User
::getCanonicalName( $name, $validate === 'false' ?
false : $validate ), $validate );
401 public static function provideGetCanonicalName() {
403 'Leading space' => [ ' Leading space', [ 'creatable' => 'Leading space' ] ],
404 'Trailing space ' => [ 'Trailing space ', [ 'creatable' => 'Trailing space' ] ],
405 'Namespace prefix' => [ 'Talk:Username', [ 'creatable' => false, 'usable' => false,
406 'valid' => false, 'false' => 'Talk:Username' ] ],
407 'Interwiki prefix' => [ 'interwiki:Username', [ 'creatable' => false, 'usable' => false,
408 'valid' => false, 'false' => 'Interwiki:Username' ] ],
409 'With hash' => [ 'name with # hash', [ 'creatable' => false, 'usable' => false ] ],
410 'Multi spaces' => [ 'Multi spaces', [ 'creatable' => 'Multi spaces',
411 'usable' => 'Multi spaces' ] ],
412 'Lowercase' => [ 'lowercase', [ 'creatable' => 'Lowercase' ] ],
413 'Invalid character' => [ 'in[]valid', [ 'creatable' => false, 'usable' => false,
414 'valid' => false, 'false' => 'In[]valid' ] ],
415 'With slash' => [ 'with / slash', [ 'creatable' => false, 'usable' => false, 'valid' => false,
416 'false' => 'With / slash' ] ],
421 * @covers User::equals
423 public function testEquals() {
424 $first = $this->getMutableTestUser()->getUser();
425 $second = User
::newFromName( $first->getName() );
427 $this->assertTrue( $first->equals( $first ) );
428 $this->assertTrue( $first->equals( $second ) );
429 $this->assertTrue( $second->equals( $first ) );
431 $third = $this->getMutableTestUser()->getUser();
432 $fourth = $this->getMutableTestUser()->getUser();
434 $this->assertFalse( $third->equals( $fourth ) );
435 $this->assertFalse( $fourth->equals( $third ) );
437 // Test users loaded from db with id
438 $user = $this->getMutableTestUser()->getUser();
439 $fifth = User
::newFromId( $user->getId() );
440 $sixth = User
::newFromName( $user->getName() );
441 $this->assertTrue( $fifth->equals( $sixth ) );
445 * @covers User::getId
447 public function testGetId() {
448 $user = static::getTestUser()->getUser();
449 $this->assertTrue( $user->getId() > 0 );
454 * @covers User::isLoggedIn
455 * @covers User::isAnon
457 public function testLoggedIn() {
458 $user = $this->getMutableTestUser()->getUser();
459 $this->assertTrue( $user->isLoggedIn() );
460 $this->assertFalse( $user->isAnon() );
462 // Non-existent users are perceived as anonymous
463 $user = User
::newFromName( 'UTNonexistent' );
464 $this->assertFalse( $user->isLoggedIn() );
465 $this->assertTrue( $user->isAnon() );
468 $this->assertFalse( $user->isLoggedIn() );
469 $this->assertTrue( $user->isAnon() );
473 * @covers User::checkAndSetTouched
475 public function testCheckAndSetTouched() {
476 $user = $this->getMutableTestUser()->getUser();
477 $user = TestingAccessWrapper
::newFromObject( $user );
478 $this->assertTrue( $user->isLoggedIn() );
480 $touched = $user->getDBTouched();
482 $user->checkAndSetTouched(), "checkAndSetTouched() succeded" );
483 $this->assertGreaterThan(
484 $touched, $user->getDBTouched(), "user_touched increased with casOnTouched()" );
486 $touched = $user->getDBTouched();
488 $user->checkAndSetTouched(), "checkAndSetTouched() succeded #2" );
489 $this->assertGreaterThan(
490 $touched, $user->getDBTouched(), "user_touched increased with casOnTouched() #2" );
494 * @covers User::findUsersByGroup
496 public function testFindUsersByGroup() {
497 $users = User
::findUsersByGroup( [] );
498 $this->assertEquals( 0, iterator_count( $users ) );
500 $users = User
::findUsersByGroup( 'foo' );
501 $this->assertEquals( 0, iterator_count( $users ) );
503 $user = $this->getMutableTestUser( [ 'foo' ] )->getUser();
504 $users = User
::findUsersByGroup( 'foo' );
505 $this->assertEquals( 1, iterator_count( $users ) );
507 $this->assertTrue( $user->equals( $users->current() ) );
509 // arguments have OR relationship
510 $user2 = $this->getMutableTestUser( [ 'bar' ] )->getUser();
511 $users = User
::findUsersByGroup( [ 'foo', 'bar' ] );
512 $this->assertEquals( 2, iterator_count( $users ) );
514 $this->assertTrue( $user->equals( $users->current() ) );
516 $this->assertTrue( $user2->equals( $users->current() ) );
518 // users are not duplicated
519 $user = $this->getMutableTestUser( [ 'baz', 'boom' ] )->getUser();
520 $users = User
::findUsersByGroup( [ 'baz', 'boom' ] );
521 $this->assertEquals( 1, iterator_count( $users ) );
523 $this->assertTrue( $user->equals( $users->current() ) );