2 use MediaWiki\MediaWikiServices
;
8 class GenderCacheTest
extends MediaWikiLangTestCase
{
10 /** @var string[] User key => username */
11 private static $nameMap;
13 function addDBDataOnce() {
14 // ensure the correct default gender
15 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
17 $male = $this->getMutableTestUser()->getUser();
18 $male->setOption( 'gender', 'male' );
19 $male->saveSettings();
21 $female = $this->getMutableTestUser()->getUser();
22 $female->setOption( 'gender', 'female' );
23 $female->saveSettings();
25 $default = $this->getMutableTestUser()->getUser();
26 $default->setOption( 'gender', null );
27 $default->saveSettings();
30 'UTMale' => $male->getName(),
31 'UTFemale' => $female->getName(),
32 'UTDefaultGender' => $default->getName()
39 * @dataProvider provideUserGenders
40 * @covers GenderCache::getGenderOf
42 public function testUserName( $userKey, $expectedGender ) {
43 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
44 $username = isset( self
::$nameMap[$userKey] ) ? self
::$nameMap[$userKey] : $userKey;
45 $gender = $genderCache->getGenderOf( $username );
46 $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
50 * genderCache should work with user objects, too
52 * @dataProvider provideUserGenders
53 * @covers GenderCache::getGenderOf
55 public function testUserObjects( $userKey, $expectedGender ) {
56 $username = isset( self
::$nameMap[$userKey] ) ? self
::$nameMap[$userKey] : $userKey;
57 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
58 $gender = $genderCache->getGenderOf( $username );
59 $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
62 public static function provideUserGenders() {
65 [ 'UTFemale', 'female' ],
66 [ 'UTDefaultGender', 'unknown' ],
67 [ 'UTNotExist', 'unknown' ],
68 // some not valid user
69 [ '127.0.0.1', 'unknown' ],
70 [ 'user@test', 'unknown' ],
75 * test strip of subpages to avoid unnecessary queries
76 * against the never existing username
78 * @dataProvider provideUserGenders
79 * @covers GenderCache::getGenderOf
81 public function testStripSubpages( $userKey, $expectedGender ) {
82 $username = isset( self
::$nameMap[$userKey] ) ? self
::$nameMap[$userKey] : $userKey;
83 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
84 $gender = $genderCache->getGenderOf( "$username/subpage" );
85 $this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );