7 class GenderCacheTest
extends MediaWikiLangTestCase
{
9 /** @var string[] User key => username */
10 private static $nameMap;
12 function addDBDataOnce() {
13 // ensure the correct default gender
14 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
16 $male = $this->getMutableTestUser()->getUser();
17 $male->setOption( 'gender', 'male' );
18 $male->saveSettings();
20 $female = $this->getMutableTestUser()->getUser();
21 $female->setOption( 'gender', 'female' );
22 $female->saveSettings();
24 $default = $this->getMutableTestUser()->getUser();
25 $default->setOption( 'gender', null );
26 $default->saveSettings();
29 'UTMale' => $male->getName(),
30 'UTFemale' => $female->getName(),
31 'UTDefaultGender' => $default->getName()
38 * @dataProvider provideUserGenders
39 * @covers GenderCache::getGenderOf
41 public function testUserName( $userKey, $expectedGender ) {
42 $genderCache = GenderCache
::singleton();
43 $username = isset( self
::$nameMap[$userKey] ) ? self
::$nameMap[$userKey] : $userKey;
44 $gender = $genderCache->getGenderOf( $username );
45 $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
49 * genderCache should work with user objects, too
51 * @dataProvider provideUserGenders
52 * @covers GenderCache::getGenderOf
54 public function testUserObjects( $userKey, $expectedGender ) {
55 $username = isset( self
::$nameMap[$userKey] ) ? self
::$nameMap[$userKey] : $userKey;
56 $genderCache = GenderCache
::singleton();
57 $gender = $genderCache->getGenderOf( $username );
58 $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
61 public static function provideUserGenders() {
64 [ 'UTFemale', 'female' ],
65 [ 'UTDefaultGender', 'unknown' ],
66 [ 'UTNotExist', 'unknown' ],
67 // some not valid user
68 [ '127.0.0.1', 'unknown' ],
69 [ 'user@test', 'unknown' ],
74 * test strip of subpages to avoid unnecessary queries
75 * against the never existing username
77 * @dataProvider provideUserGenders
78 * @covers GenderCache::getGenderOf
80 public function testStripSubpages( $userKey, $expectedGender ) {
81 $username = isset( self
::$nameMap[$userKey] ) ? self
::$nameMap[$userKey] : $userKey;
82 $genderCache = GenderCache
::singleton();
83 $gender = $genderCache->getGenderOf( "$username/subpage" );
84 $this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );