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 * @covers User::getRights
98 public function testUserGetRightsHooks() {
100 $user->addGroup( 'unittesters' );
101 $user->addGroup( 'testwriters' );
102 $userWrapper = TestingAccessWrapper
::newFromObject( $user );
104 $rights = $user->getRights();
105 $this->assertContains( 'test', $rights, 'sanity check' );
106 $this->assertContains( 'runtest', $rights, 'sanity check' );
107 $this->assertContains( 'writetest', $rights, 'sanity check' );
108 $this->assertNotContains( 'nukeworld', $rights, 'sanity check' );
110 // Add a hook manipluating the rights
111 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserGetRights' => [ function ( $user, &$rights ) {
112 $rights[] = 'nukeworld';
113 $rights = array_diff( $rights, [ 'writetest' ] );
116 $userWrapper->mRights
= null;
117 $rights = $user->getRights();
118 $this->assertContains( 'test', $rights );
119 $this->assertContains( 'runtest', $rights );
120 $this->assertNotContains( 'writetest', $rights );
121 $this->assertContains( 'nukeworld', $rights );
123 // Add a Session that limits rights
124 $mock = $this->getMockBuilder( stdclass
::class )
125 ->setMethods( [ 'getAllowedUserRights', 'deregisterSession', 'getSessionId' ] )
127 $mock->method( 'getAllowedUserRights' )->willReturn( [ 'test', 'writetest' ] );
128 $mock->method( 'getSessionId' )->willReturn(
129 new MediaWiki\Session\
SessionId( str_repeat( 'X', 32 ) )
131 $session = MediaWiki\Session\TestUtils
::getDummySession( $mock );
132 $mockRequest = $this->getMockBuilder( FauxRequest
::class )
133 ->setMethods( [ 'getSession' ] )
135 $mockRequest->method( 'getSession' )->willReturn( $session );
136 $userWrapper->mRequest
= $mockRequest;
138 $userWrapper->mRights
= null;
139 $rights = $user->getRights();
140 $this->assertContains( 'test', $rights );
141 $this->assertNotContains( 'runtest', $rights );
142 $this->assertNotContains( 'writetest', $rights );
143 $this->assertNotContains( 'nukeworld', $rights );
147 * @dataProvider provideGetGroupsWithPermission
148 * @covers User::getGroupsWithPermission
150 public function testGetGroupsWithPermission( $expected, $right ) {
151 $result = User
::getGroupsWithPermission( $right );
155 $this->assertEquals( $expected, $result, "Groups with permission $right" );
158 public static function provideGetGroupsWithPermission() {
161 [ 'unittesters', 'testwriters' ],
180 * @dataProvider provideIPs
183 public function testIsIP( $value, $result, $message ) {
184 $this->assertEquals( $this->user
->isIP( $value ), $result, $message );
187 public static function provideIPs() {
189 [ '', false, 'Empty string' ],
190 [ ' ', false, 'Blank space' ],
191 [ '10.0.0.0', true, 'IPv4 private 10/8' ],
192 [ '10.255.255.255', true, 'IPv4 private 10/8' ],
193 [ '192.168.1.1', true, 'IPv4 private 192.168/16' ],
194 [ '203.0.113.0', true, 'IPv4 example' ],
195 [ '2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff', true, 'IPv6 example' ],
196 // Not valid IPs but classified as such by MediaWiki for negated asserting
197 // of whether this might be the identifier of a logged-out user or whether
198 // to allow usernames like it.
199 [ '300.300.300.300', true, 'Looks too much like an IPv4 address' ],
200 [ '203.0.113.xxx', true, 'Assigned by UseMod to cloaked logged-out users' ],
205 * @dataProvider provideUserNames
206 * @covers User::isValidUserName
208 public function testIsValidUserName( $username, $result, $message ) {
209 $this->assertEquals( $this->user
->isValidUserName( $username ), $result, $message );
212 public static function provideUserNames() {
214 [ '', false, 'Empty string' ],
215 [ ' ', false, 'Blank space' ],
216 [ 'abcd', false, 'Starts with small letter' ],
217 [ 'Ab/cd', false, 'Contains slash' ],
218 [ 'Ab cd', true, 'Whitespace' ],
219 [ '192.168.1.1', false, 'IP' ],
220 [ 'User:Abcd', false, 'Reserved Namespace' ],
221 [ '12abcd232', true, 'Starts with Numbers' ],
222 [ '?abcd', true, 'Start with ? mark' ],
223 [ '#abcd', false, 'Start with #' ],
224 [ 'Abcdകഖഗഘ', true, ' Mixed scripts' ],
225 [ 'ജോസ്തോമസ്', false, 'ZWNJ- Format control character' ],
226 [ 'Ab cd', false, ' Ideographic space' ],
227 [ '300.300.300.300', false, 'Looks too much like an IPv4 address' ],
228 [ '302.113.311.900', false, 'Looks too much like an IPv4 address' ],
229 [ '203.0.113.xxx', false, 'Reserved for usage by UseMod for cloaked logged-out users' ],
234 * Test, if for all rights a right- message exist,
235 * which is used on Special:ListGroupRights as help text
236 * Extensions and core
238 public function testAllRightsWithMessage() {
239 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
240 $allRights = User
::getAllRights();
241 $allMessageKeys = Language
::getMessageKeysFor( 'en' );
243 $rightsWithMessage = [];
244 foreach ( $allMessageKeys as $message ) {
245 // === 0: must be at beginning of string (position 0)
246 if ( strpos( $message, 'right-' ) === 0 ) {
247 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
252 sort( $rightsWithMessage );
257 'Each user rights (core/extensions) has a corresponding right- message.'
262 * Test User::editCount
264 * @covers User::getEditCount
266 public function testGetEditCount() {
267 $user = $this->getMutableTestUser()->getUser();
269 // let the user have a few (3) edits
270 $page = WikiPage
::factory( Title
::newFromText( 'Help:UserTest_EditCount' ) );
271 for ( $i = 0; $i < 3; $i++
) {
273 $page->doEditContent(
274 ContentHandler
::makeContent( (string)$i, $page->getTitle() ),
284 $user->getEditCount(),
285 'After three edits, the user edit count should be 3'
288 // increase the edit count
289 $user->incEditCount();
293 $user->getEditCount(),
294 'After increasing the edit count manually, the user edit count should be 4'
299 * Test User::editCount
301 * @covers User::getEditCount
303 public function testGetEditCountForAnons() {
304 $user = User
::newFromName( 'Anonymous' );
307 $user->getEditCount(),
308 'Edit count starts null for anonymous users.'
311 $user->incEditCount();
314 $user->getEditCount(),
315 'Edit count remains null for anonymous users despite calls to increase it.'
320 * Test User::editCount
322 * @covers User::incEditCount
324 public function testIncEditCount() {
325 $user = $this->getMutableTestUser()->getUser();
326 $user->incEditCount();
328 $reloadedUser = User
::newFromId( $user->getId() );
329 $reloadedUser->incEditCount();
333 $reloadedUser->getEditCount(),
334 'Increasing the edit count after a fresh load leaves the object up to date.'
339 * Test changing user options.
340 * @covers User::setOption
341 * @covers User::getOption
343 public function testOptions() {
344 $user = $this->getMutableTestUser()->getUser();
346 $user->setOption( 'userjs-someoption', 'test' );
347 $user->setOption( 'cols', 200 );
348 $user->saveSettings();
350 $user = User
::newFromName( $user->getName() );
351 $this->assertEquals( 'test', $user->getOption( 'userjs-someoption' ) );
352 $this->assertEquals( 200, $user->getOption( 'cols' ) );
357 * Make sure defaults are loaded when setOption is called.
358 * @covers User::loadOptions
360 public function testAnonOptions() {
361 global $wgDefaultUserOptions;
362 $this->user
->setOption( 'userjs-someoption', 'test' );
363 $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user
->getOption( 'cols' ) );
364 $this->assertEquals( 'test', $this->user
->getOption( 'userjs-someoption' ) );
368 * Test password validity checks. There are 3 checks in core,
369 * - ensure the password meets the minimal length
370 * - ensure the password is not the same as the username
371 * - ensure the username/password combo isn't forbidden
372 * @covers User::checkPasswordValidity()
373 * @covers User::getPasswordValidity()
374 * @covers User::isValidPassword()
376 public function testCheckPasswordValidity() {
377 $this->setMwGlobals( [
378 'wgPasswordPolicy' => [
381 'MinimalPasswordLength' => 8,
382 'MinimumPasswordLengthToLogin' => 1,
383 'PasswordCannotMatchUsername' => 1,
386 'MinimalPasswordLength' => 6,
387 'PasswordCannotMatchUsername' => true,
388 'PasswordCannotMatchBlacklist' => true,
389 'MaximalPasswordLength' => 40,
393 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
394 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
395 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
396 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
397 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
402 $user = static::getTestUser()->getUser();
405 $this->assertTrue( $user->isValidPassword( 'Password1234' ) );
408 $this->assertFalse( $user->isValidPassword( 'a' ) );
409 $this->assertFalse( $user->checkPasswordValidity( 'a' )->isGood() );
410 $this->assertTrue( $user->checkPasswordValidity( 'a' )->isOK() );
411 $this->assertEquals( 'passwordtooshort', $user->getPasswordValidity( 'a' ) );
414 $longPass = str_repeat( 'a', 41 );
415 $this->assertFalse( $user->isValidPassword( $longPass ) );
416 $this->assertFalse( $user->checkPasswordValidity( $longPass )->isGood() );
417 $this->assertFalse( $user->checkPasswordValidity( $longPass )->isOK() );
418 $this->assertEquals( 'passwordtoolong', $user->getPasswordValidity( $longPass ) );
421 $this->assertFalse( $user->checkPasswordValidity( $user->getName() )->isGood() );
422 $this->assertTrue( $user->checkPasswordValidity( $user->getName() )->isOK() );
423 $this->assertEquals( 'password-name-match', $user->getPasswordValidity( $user->getName() ) );
425 // On the forbidden list
426 $user = User
::newFromName( 'Useruser' );
427 $this->assertFalse( $user->checkPasswordValidity( 'Passpass' )->isGood() );
428 $this->assertEquals( 'password-login-forbidden', $user->getPasswordValidity( 'Passpass' ) );
432 * @covers User::getCanonicalName()
433 * @dataProvider provideGetCanonicalName
435 public function testGetCanonicalName( $name, $expectedArray ) {
436 // fake interwiki map for the 'Interwiki prefix' testcase
437 $this->mergeMwGlobalArrayValue( 'wgHooks', [
438 'InterwikiLoadPrefix' => [
439 function ( $prefix, &$iwdata ) {
440 if ( $prefix === 'interwiki' ) {
442 'iw_url' => 'http://example.com/',
452 foreach ( $expectedArray as $validate => $expected ) {
455 User
::getCanonicalName( $name, $validate === 'false' ?
false : $validate ), $validate );
459 public static function provideGetCanonicalName() {
461 'Leading space' => [ ' Leading space', [ 'creatable' => 'Leading space' ] ],
462 'Trailing space ' => [ 'Trailing space ', [ 'creatable' => 'Trailing space' ] ],
463 'Namespace prefix' => [ 'Talk:Username', [ 'creatable' => false, 'usable' => false,
464 'valid' => false, 'false' => 'Talk:Username' ] ],
465 'Interwiki prefix' => [ 'interwiki:Username', [ 'creatable' => false, 'usable' => false,
466 'valid' => false, 'false' => 'Interwiki:Username' ] ],
467 'With hash' => [ 'name with # hash', [ 'creatable' => false, 'usable' => false ] ],
468 'Multi spaces' => [ 'Multi spaces', [ 'creatable' => 'Multi spaces',
469 'usable' => 'Multi spaces' ] ],
470 'Lowercase' => [ 'lowercase', [ 'creatable' => 'Lowercase' ] ],
471 'Invalid character' => [ 'in[]valid', [ 'creatable' => false, 'usable' => false,
472 'valid' => false, 'false' => 'In[]valid' ] ],
473 'With slash' => [ 'with / slash', [ 'creatable' => false, 'usable' => false, 'valid' => false,
474 'false' => 'With / slash' ] ],
479 * @covers User::equals
481 public function testEquals() {
482 $first = $this->getMutableTestUser()->getUser();
483 $second = User
::newFromName( $first->getName() );
485 $this->assertTrue( $first->equals( $first ) );
486 $this->assertTrue( $first->equals( $second ) );
487 $this->assertTrue( $second->equals( $first ) );
489 $third = $this->getMutableTestUser()->getUser();
490 $fourth = $this->getMutableTestUser()->getUser();
492 $this->assertFalse( $third->equals( $fourth ) );
493 $this->assertFalse( $fourth->equals( $third ) );
495 // Test users loaded from db with id
496 $user = $this->getMutableTestUser()->getUser();
497 $fifth = User
::newFromId( $user->getId() );
498 $sixth = User
::newFromName( $user->getName() );
499 $this->assertTrue( $fifth->equals( $sixth ) );
503 * @covers User::getId
505 public function testGetId() {
506 $user = static::getTestUser()->getUser();
507 $this->assertTrue( $user->getId() > 0 );
512 * @covers User::isLoggedIn
513 * @covers User::isAnon
515 public function testLoggedIn() {
516 $user = $this->getMutableTestUser()->getUser();
517 $this->assertTrue( $user->isLoggedIn() );
518 $this->assertFalse( $user->isAnon() );
520 // Non-existent users are perceived as anonymous
521 $user = User
::newFromName( 'UTNonexistent' );
522 $this->assertFalse( $user->isLoggedIn() );
523 $this->assertTrue( $user->isAnon() );
526 $this->assertFalse( $user->isLoggedIn() );
527 $this->assertTrue( $user->isAnon() );
531 * @covers User::checkAndSetTouched
533 public function testCheckAndSetTouched() {
534 $user = $this->getMutableTestUser()->getUser();
535 $user = TestingAccessWrapper
::newFromObject( $user );
536 $this->assertTrue( $user->isLoggedIn() );
538 $touched = $user->getDBTouched();
540 $user->checkAndSetTouched(), "checkAndSetTouched() succeded" );
541 $this->assertGreaterThan(
542 $touched, $user->getDBTouched(), "user_touched increased with casOnTouched()" );
544 $touched = $user->getDBTouched();
546 $user->checkAndSetTouched(), "checkAndSetTouched() succeded #2" );
547 $this->assertGreaterThan(
548 $touched, $user->getDBTouched(), "user_touched increased with casOnTouched() #2" );
552 * @covers User::findUsersByGroup
554 public function testFindUsersByGroup() {
555 $users = User
::findUsersByGroup( [] );
556 $this->assertEquals( 0, iterator_count( $users ) );
558 $users = User
::findUsersByGroup( 'foo' );
559 $this->assertEquals( 0, iterator_count( $users ) );
561 $user = $this->getMutableTestUser( [ 'foo' ] )->getUser();
562 $users = User
::findUsersByGroup( 'foo' );
563 $this->assertEquals( 1, iterator_count( $users ) );
565 $this->assertTrue( $user->equals( $users->current() ) );
567 // arguments have OR relationship
568 $user2 = $this->getMutableTestUser( [ 'bar' ] )->getUser();
569 $users = User
::findUsersByGroup( [ 'foo', 'bar' ] );
570 $this->assertEquals( 2, iterator_count( $users ) );
572 $this->assertTrue( $user->equals( $users->current() ) );
574 $this->assertTrue( $user2->equals( $users->current() ) );
576 // users are not duplicated
577 $user = $this->getMutableTestUser( [ 'baz', 'boom' ] )->getUser();
578 $users = User
::findUsersByGroup( [ 'baz', 'boom' ] );
579 $this->assertEquals( 1, iterator_count( $users ) );
581 $this->assertTrue( $user->equals( $users->current() ) );