The unit tests spend nearly half of their run time resetting the user table for each test. But the majority of tests do not depend on the user table having the exact value that the setup code resets it to, and do not need to modify the user objects they require to run. Fix that by providing an API for tests to get User objects, and to indicate whether the User object will be subject to destructive modification or not. This allows User objects to be reused across multiple unit tests. Change-Id: I17ef1f519759c5e7796c259282afe730ef722e96
86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group Database
|
|
* @group Cache
|
|
*/
|
|
class GenderCacheTest extends MediaWikiLangTestCase {
|
|
|
|
/** @var string[] User key => username */
|
|
private static $nameMap;
|
|
|
|
function addDBDataOnce() {
|
|
// ensure the correct default gender
|
|
$this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
|
|
|
|
$male = $this->getMutableTestUser()->getUser();
|
|
$male->setOption( 'gender', 'male' );
|
|
$male->saveSettings();
|
|
|
|
$female = $this->getMutableTestUser()->getUser();
|
|
$female->setOption( 'gender', 'female' );
|
|
$female->saveSettings();
|
|
|
|
$default = $this->getMutableTestUser()->getUser();
|
|
$default->setOption( 'gender', null );
|
|
$default->saveSettings();
|
|
|
|
self::$nameMap = [
|
|
'UTMale' => $male->getName(),
|
|
'UTFemale' => $female->getName(),
|
|
'UTDefaultGender' => $default->getName()
|
|
];
|
|
}
|
|
|
|
/**
|
|
* test usernames
|
|
*
|
|
* @dataProvider provideUserGenders
|
|
* @covers GenderCache::getGenderOf
|
|
*/
|
|
public function testUserName( $userKey, $expectedGender ) {
|
|
$genderCache = GenderCache::singleton();
|
|
$username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey;
|
|
$gender = $genderCache->getGenderOf( $username );
|
|
$this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
|
|
}
|
|
|
|
/**
|
|
* genderCache should work with user objects, too
|
|
*
|
|
* @dataProvider provideUserGenders
|
|
* @covers GenderCache::getGenderOf
|
|
*/
|
|
public function testUserObjects( $userKey, $expectedGender ) {
|
|
$username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey;
|
|
$genderCache = GenderCache::singleton();
|
|
$gender = $genderCache->getGenderOf( $username );
|
|
$this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
|
|
}
|
|
|
|
public static function provideUserGenders() {
|
|
return [
|
|
[ 'UTMale', 'male' ],
|
|
[ 'UTFemale', 'female' ],
|
|
[ 'UTDefaultGender', 'unknown' ],
|
|
[ 'UTNotExist', 'unknown' ],
|
|
// some not valid user
|
|
[ '127.0.0.1', 'unknown' ],
|
|
[ 'user@test', 'unknown' ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* test strip of subpages to avoid unnecessary queries
|
|
* against the never existing username
|
|
*
|
|
* @dataProvider provideUserGenders
|
|
* @covers GenderCache::getGenderOf
|
|
*/
|
|
public function testStripSubpages( $userKey, $expectedGender ) {
|
|
$username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey;
|
|
$genderCache = GenderCache::singleton();
|
|
$gender = $genderCache->getGenderOf( "$username/subpage" );
|
|
$this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );
|
|
}
|
|
}
|