Tests: add LanguageNameUtils to DummyServicesTrait
Returns a real LanguageNameUtils object configured as needed. Change-Id: I50a8c6bc15665c454642a513f6669191456a3939
This commit is contained in:
parent
db9d388116
commit
a5293c323a
5 changed files with 46 additions and 88 deletions
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
|
|
@ -11,6 +11,7 @@ use Psr\Log\NullLogger;
|
|||
* @author Niklas Laxström
|
||||
*/
|
||||
class LocalisationCacheTest extends MediaWikiIntegrationTestCase {
|
||||
use DummyServicesTrait;
|
||||
|
||||
/**
|
||||
* @param array $hooks Hook overrides
|
||||
|
|
@ -19,37 +20,13 @@ class LocalisationCacheTest extends MediaWikiIntegrationTestCase {
|
|||
protected function getMockLocalisationCache( $hooks = [] ) {
|
||||
global $IP;
|
||||
|
||||
$mockLangNameUtils = $this->createNoOpMock( LanguageNameUtils::class,
|
||||
[ 'isValidBuiltInCode', 'isSupportedLanguage', 'getMessagesFileName' ] );
|
||||
$mockLangNameUtils->method( 'isValidBuiltInCode' )->willReturnCallback(
|
||||
static function ( $code ) {
|
||||
// Copy-paste, but it's only one line
|
||||
return (bool)preg_match( '/^[a-z0-9-]{2,}$/', $code );
|
||||
}
|
||||
);
|
||||
$mockLangNameUtils->method( 'isSupportedLanguage' )->willReturnCallback(
|
||||
static function ( $code ) {
|
||||
return in_array( $code, [
|
||||
'ar',
|
||||
'arz',
|
||||
'ba',
|
||||
'de',
|
||||
'en',
|
||||
'ksh',
|
||||
'ru',
|
||||
] );
|
||||
}
|
||||
);
|
||||
$mockLangNameUtils->method( 'getMessagesFileName' )->willReturnCallback(
|
||||
static function ( $code ) {
|
||||
global $IP;
|
||||
$code = str_replace( '-', '_', ucfirst( $code ) );
|
||||
return "$IP/languages/messages/Messages$code.php";
|
||||
}
|
||||
);
|
||||
|
||||
$hookContainer = $this->createHookContainer( $hooks );
|
||||
|
||||
// in case any of the LanguageNameUtils hooks are being used
|
||||
$langNameUtils = $this->getDummyLanguageNameUtils(
|
||||
[ 'hookContainer' => $hookContainer ]
|
||||
);
|
||||
|
||||
$lc = $this->getMockBuilder( LocalisationCache::class )
|
||||
->setConstructorArgs( [
|
||||
new ServiceOptions( LocalisationCache::CONSTRUCTOR_OPTIONS, [
|
||||
|
|
@ -61,7 +38,7 @@ class LocalisationCacheTest extends MediaWikiIntegrationTestCase {
|
|||
new LCStoreDB( [] ),
|
||||
new NullLogger,
|
||||
[],
|
||||
$mockLangNameUtils,
|
||||
$langNameUtils,
|
||||
$hookContainer
|
||||
] )
|
||||
->onlyMethods( [ 'getMessagesDirs' ] )
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use MediaWiki\Languages\LanguageFallback;
|
|||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\MainConfigNames;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
||||
use MediaWiki\User\UserIdentityValue;
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
||||
|
|
@ -14,6 +15,7 @@ use Wikimedia\TestingAccessWrapper;
|
|||
* @group Language
|
||||
*/
|
||||
class LanguageIntegrationTest extends LanguageClassesTestCase {
|
||||
use DummyServicesTrait;
|
||||
use LanguageNameUtilsTestTrait;
|
||||
|
||||
/** @var array Copy of $wgHooks from before we unset LanguageGetTranslatedLanguageNames */
|
||||
|
|
@ -1808,21 +1810,13 @@ class LanguageIntegrationTest extends LanguageClassesTestCase {
|
|||
* @covers Language::getNamespaceAliases
|
||||
*/
|
||||
public function testGetNamespaceAliasesFullLogic() {
|
||||
$langNameUtils = $this->getMockBuilder( LanguageNameUtils::class )
|
||||
->setConstructorArgs( [
|
||||
new ServiceOptions( LanguageNameUtils::CONSTRUCTOR_OPTIONS, [
|
||||
MainConfigNames::ExtraLanguageNames => [],
|
||||
MainConfigNames::UsePigLatinVariant => false,
|
||||
] ),
|
||||
$this->createHookContainer()
|
||||
] )
|
||||
->onlyMethods( [ 'getMessagesFileName' ] )
|
||||
->getMock();
|
||||
$langNameUtils->method( 'getMessagesFileName' )->will(
|
||||
$this->returnCallback( static function ( $code ) {
|
||||
return __DIR__ . '/../../data/messages/Messages_' . $code . '.php';
|
||||
} )
|
||||
);
|
||||
$hooks = $this->createHookContainer( [
|
||||
'Language::getMessagesFileName' => static function ( $code, &$file ) {
|
||||
$file = __DIR__ . '/../../data/messages/Messages_' . $code . '.php';
|
||||
}
|
||||
] );
|
||||
$langNameUtils = $this->getDummyLanguageNameUtils( [ 'hookContainer' => $hooks ] );
|
||||
|
||||
$this->overrideConfigValue( MainConfigNames::NamespaceAliases, [
|
||||
'Mouse' => NS_SPECIAL,
|
||||
] );
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ use MediaWiki\Cache\CacheKeyHelper;
|
|||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\Content\IContentHandlerFactory;
|
||||
use MediaWiki\Interwiki\InterwikiLookup;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Linker\LinkTarget;
|
||||
use MediaWiki\MainConfigSchema;
|
||||
use MediaWiki\Page\PageReference;
|
||||
|
|
@ -222,6 +223,27 @@ trait DummyServicesTrait {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options keys are
|
||||
* - anything in LanguageNameUtils::CONSTRUCTOR_OPTIONS, any missing options will default
|
||||
* to the MainConfigSchema defaults
|
||||
* - 'hookContainer' if specific hooks need to be registered, otherwise an empty
|
||||
* container will be used
|
||||
* @return LanguageNameUtils
|
||||
*/
|
||||
private function getDummyLanguageNameUtils( array $options = [] ): LanguageNameUtils {
|
||||
// configuration is based on the defaults in MainConfigSchema
|
||||
$serviceOptions = new ServiceOptions(
|
||||
LanguageNameUtils::CONSTRUCTOR_OPTIONS,
|
||||
$options, // caller can override the default config by specifying it here
|
||||
self::getDefaultSettings()
|
||||
);
|
||||
return new LanguageNameUtils(
|
||||
$serviceOptions,
|
||||
$options['hookContainer'] ?? $this->createHookContainer()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options see getDummyMediaWikiTitleCodec for supported options
|
||||
* @return TitleFormatter
|
||||
|
|
|
|||
|
|
@ -1,12 +1,7 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\HookContainer\HookContainer;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\MainConfigNames;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Wikimedia\ObjectFactory\ObjectFactory;
|
||||
use Wikimedia\Services\NoSuchServiceException;
|
||||
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
||||
|
||||
/**
|
||||
* @covers LanguageCode
|
||||
|
|
@ -15,6 +10,7 @@ use Wikimedia\Services\NoSuchServiceException;
|
|||
* @author Thiemo Kreuz
|
||||
*/
|
||||
class LanguageCodeTest extends MediaWikiUnitTestCase {
|
||||
use DummyServicesTrait;
|
||||
|
||||
public function testConstructor() {
|
||||
$instance = new LanguageCode();
|
||||
|
|
@ -226,29 +222,8 @@ class LanguageCodeTest extends MediaWikiUnitTestCase {
|
|||
$this->assertEquals( $internalCode, $result );
|
||||
}
|
||||
|
||||
public static function provideSupportedLanguageCodes() {
|
||||
$lnu = new LanguageNameUtils(
|
||||
new ServiceOptions(
|
||||
LanguageNameUtils::CONSTRUCTOR_OPTIONS,
|
||||
[
|
||||
MainConfigNames::ExtraLanguageNames => [],
|
||||
MainConfigNames::LanguageCode => 'en',
|
||||
MainConfigNames::UsePigLatinVariant => true,
|
||||
]
|
||||
),
|
||||
new HookContainer(
|
||||
new \MediaWiki\HookContainer\StaticHookRegistry(),
|
||||
new ObjectFactory( new class implements ContainerInterface {
|
||||
public function has( string $id ): bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function get( string $id ) {
|
||||
throw new NoSuchServiceException( $id );
|
||||
}
|
||||
} )
|
||||
)
|
||||
);
|
||||
public function provideSupportedLanguageCodes() {
|
||||
$lnu = $this->getDummyLanguageNameUtils();
|
||||
$languages = $lnu->getLanguageNames(
|
||||
LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Languages\LanguageFallback;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass MediaWiki\Languages\LanguageFallback
|
||||
* @covers ::__construct
|
||||
*/
|
||||
class LanguageFallbackTest extends MediaWikiUnitTestCase {
|
||||
use DummyServicesTrait;
|
||||
use LanguageFallbackTestTrait;
|
||||
|
||||
private const DATA = [
|
||||
|
|
@ -19,17 +20,6 @@ class LanguageFallbackTest extends MediaWikiUnitTestCase {
|
|||
'sh' => [ 'sh-latn', 'sh-cyrl', 'bs', 'sr-el', 'sr-latn', 'hr' ],
|
||||
];
|
||||
|
||||
private function getLanguageNameUtils() {
|
||||
$mockLangNameUtils = $this->createNoOpMock( LanguageNameUtils::class,
|
||||
[ 'isValidBuiltInCode' ] );
|
||||
$mockLangNameUtils->method( 'isValidBuiltInCode' )
|
||||
->willReturnCallback( static function ( $code ) {
|
||||
// One-line copy-paste
|
||||
return (bool)preg_match( '/^[a-z0-9-]{2,}$/', $code );
|
||||
} );
|
||||
return $mockLangNameUtils;
|
||||
}
|
||||
|
||||
private function getCallee( array $options = [] ): LanguageFallback {
|
||||
return new LanguageFallback(
|
||||
$options['siteLangCode'] ?? 'en',
|
||||
|
|
@ -37,7 +27,7 @@ class LanguageFallbackTest extends MediaWikiUnitTestCase {
|
|||
$options['expectedGets'] ?? 1,
|
||||
$options['fallbackMap'] ?? self::DATA
|
||||
),
|
||||
$this->getLanguageNameUtils()
|
||||
$this->getDummyLanguageNameUtils()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue