3 use MediaWiki\Cache\GenderCache
;
9 class GenderCacheTest
extends MediaWikiLangTestCase
{
11 /** @var string[] User key => username */
12 private static $nameMap;
14 public function addDBDataOnce() {
15 // ensure the correct default gender
16 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
18 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
20 $male = $this->getMutableTestUser()->getUser();
21 $userOptionsManager->setOption( $male, 'gender', 'male' );
22 $male->saveSettings();
24 $female = $this->getMutableTestUser()->getUser();
25 $userOptionsManager->setOption( $female, 'gender', 'female' );
26 $female->saveSettings();
28 $default = $this->getMutableTestUser()->getUser();
29 $userOptionsManager->setOption( $default, 'gender', null );
30 $default->saveSettings();
33 'UTMale' => $male->getName(),
34 'UTFemale' => $female->getName(),
35 'UTDefaultGender' => $default->getName()
42 * @dataProvider provideUserGenders
43 * @covers \MediaWiki\Cache\GenderCache::getGenderOf
45 public function testUserName( $userKey, $expectedGender ) {
46 $genderCache = $this->getServiceContainer()->getGenderCache();
47 $username = self
::$nameMap[$userKey] ??
$userKey;
48 $gender = $genderCache->getGenderOf( $username );
49 $this->assertEquals( $expectedGender, $gender, "GenderCache normal" );
53 * genderCache should work with user objects, too
55 * @dataProvider provideUserGenders
56 * @covers \MediaWiki\Cache\GenderCache::getGenderOf
58 public function testUserObjects( $userKey, $expectedGender ) {
59 $username = self
::$nameMap[$userKey] ??
$userKey;
60 $genderCache = $this->getServiceContainer()->getGenderCache();
61 $gender = $genderCache->getGenderOf( $username );
62 $this->assertEquals( $expectedGender, $gender, "GenderCache normal" );
65 public static function provideUserGenders() {
68 [ 'UTFemale', 'female' ],
69 [ 'UTDefaultGender', 'unknown' ],
70 [ 'UTNotExist', 'unknown' ],
71 // some not valid user
72 [ '127.0.0.1', 'unknown' ],
73 [ 'user@test', 'unknown' ],
78 * test strip of subpages to avoid unnecessary queries
79 * against the never existing username
81 * @dataProvider provideUserGenders
82 * @covers \MediaWiki\Cache\GenderCache::getGenderOf
84 public function testStripSubpages( $userKey, $expectedGender ) {
85 $username = self
::$nameMap[$userKey] ??
$userKey;
86 $genderCache = $this->getServiceContainer()->getGenderCache();
87 $gender = $genderCache->getGenderOf( "$username/subpage" );
88 $this->assertEquals( $expectedGender, $gender, "GenderCache must strip of subpages" );
92 * GenderCache must work without database (like Installer)
95 public function testWithoutDB() {
96 $this->overrideMwServices();
98 $services = $this->getServiceContainer();
99 $services->disableService( 'DBLoadBalancer' );
100 $services->disableService( 'DBLoadBalancerFactory' );
102 // Make sure the disable works
103 $this->assertTrue( $services->isServiceDisabled( 'DBLoadBalancer' ) );
105 // Test, if it is possible to create the gender cache
106 $genderCache = $services->getGenderCache();
107 $this->assertInstanceOf( GenderCache
::class, $genderCache );