MediaWiki  1.30.0
GenderCacheTest.php
Go to the documentation of this file.
1 <?php
3 
9 
11  private static $nameMap;
12 
13  function addDBDataOnce() {
14  // ensure the correct default gender
15  $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
16 
17  $male = $this->getMutableTestUser()->getUser();
18  $male->setOption( 'gender', 'male' );
19  $male->saveSettings();
20 
21  $female = $this->getMutableTestUser()->getUser();
22  $female->setOption( 'gender', 'female' );
23  $female->saveSettings();
24 
25  $default = $this->getMutableTestUser()->getUser();
26  $default->setOption( 'gender', null );
27  $default->saveSettings();
28 
29  self::$nameMap = [
30  'UTMale' => $male->getName(),
31  'UTFemale' => $female->getName(),
32  'UTDefaultGender' => $default->getName()
33  ];
34  }
35 
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" );
47  }
48 
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" );
60  }
61 
62  public static function provideUserGenders() {
63  return [
64  [ 'UTMale', 'male' ],
65  [ 'UTFemale', 'female' ],
66  [ 'UTDefaultGender', 'unknown' ],
67  [ 'UTNotExist', 'unknown' ],
68  // some not valid user
69  [ '127.0.0.1', 'unknown' ],
70  [ 'user@test', 'unknown' ],
71  ];
72  }
73 
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" );
86  }
87 }
GenderCacheTest\testUserName
testUserName( $userKey, $expectedGender)
test usernames
Definition: GenderCacheTest.php:42
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:807
GenderCacheTest
Database Cache.
Definition: GenderCacheTest.php:8
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
GenderCacheTest\testStripSubpages
testStripSubpages( $userKey, $expectedGender)
test strip of subpages to avoid unnecessary queries against the never existing username
Definition: GenderCacheTest.php:81
GenderCacheTest\provideUserGenders
static provideUserGenders()
Definition: GenderCacheTest.php:62
MediaWikiTestCase\getMutableTestUser
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
Definition: MediaWikiTestCase.php:160
GenderCacheTest\$nameMap
static string[] $nameMap
User key => username.
Definition: GenderCacheTest.php:11
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
GenderCacheTest\testUserObjects
testUserObjects( $userKey, $expectedGender)
genderCache should work with user objects, too
Definition: GenderCacheTest.php:55
GenderCacheTest\addDBDataOnce
addDBDataOnce()
Stub.
Definition: GenderCacheTest.php:13
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:781