API: Fixes for AuthManager
[mediawiki.git] / tests / phpunit / includes / cache / GenderCacheTest.php
blobe329f8d8c5e299d6772c338f9a1773abc051a8b2
1 <?php
3 /**
4 * @group Database
5 * @group Cache
6 */
7 class GenderCacheTest extends MediaWikiLangTestCase {
9 function addDBDataOnce() {
10 // ensure the correct default gender
11 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
13 $user = User::newFromName( 'UTMale' );
14 if ( $user->getId() == 0 ) {
15 $user->addToDatabase();
16 TestUser::setPasswordForUser( $user, 'UTMalePassword' );
18 // ensure the right gender
19 $user->setOption( 'gender', 'male' );
20 $user->saveSettings();
22 $user = User::newFromName( 'UTFemale' );
23 if ( $user->getId() == 0 ) {
24 $user->addToDatabase();
25 TestUser::setPasswordForUser( $user, 'UTFemalePassword' );
27 // ensure the right gender
28 $user->setOption( 'gender', 'female' );
29 $user->saveSettings();
31 $user = User::newFromName( 'UTDefaultGender' );
32 if ( $user->getId() == 0 ) {
33 $user->addToDatabase();
34 TestUser::setPasswordForUser( $user, 'UTDefaultGenderPassword' );
36 // ensure the default gender
37 $user->setOption( 'gender', null );
38 $user->saveSettings();
41 /**
42 * test usernames
44 * @dataProvider provideUserGenders
45 * @covers GenderCache::getGenderOf
47 public function testUserName( $username, $expectedGender ) {
48 $genderCache = GenderCache::singleton();
49 $gender = $genderCache->getGenderOf( $username );
50 $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
53 /**
54 * genderCache should work with user objects, too
56 * @dataProvider provideUserGenders
57 * @covers GenderCache::getGenderOf
59 public function testUserObjects( $username, $expectedGender ) {
60 $genderCache = GenderCache::singleton();
61 $user = User::newFromName( $username );
62 $gender = $genderCache->getGenderOf( $user );
63 $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
66 public static function provideUserGenders() {
67 return [
68 [ 'UTMale', 'male' ],
69 [ 'UTFemale', 'female' ],
70 [ 'UTDefaultGender', 'unknown' ],
71 [ 'UTNotExist', 'unknown' ],
72 // some not valid user
73 [ '127.0.0.1', 'unknown' ],
74 [ 'user@test', 'unknown' ],
78 /**
79 * test strip of subpages to avoid unnecessary queries
80 * against the never existing username
82 * @dataProvider provideStripSubpages
83 * @covers GenderCache::getGenderOf
85 public function testStripSubpages( $pageWithSubpage, $expectedGender ) {
86 $genderCache = GenderCache::singleton();
87 $gender = $genderCache->getGenderOf( $pageWithSubpage );
88 $this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );
91 public static function provideStripSubpages() {
92 return [
93 [ 'UTMale/subpage', 'male' ],
94 [ 'UTFemale/subpage', 'female' ],
95 [ 'UTDefaultGender/subpage', 'unknown' ],
96 [ 'UTNotExist/subpage', 'unknown' ],
97 [ '127.0.0.1/subpage', 'unknown' ],