3 use MediaWiki\Config\ServiceOptions
;
4 use MediaWiki\Languages\LanguageNameUtils
;
5 use Psr\Log\NullLogger
;
10 * @covers LocalisationCache
11 * @author Niklas Laxström
13 class LocalisationCacheTest
extends MediaWikiTestCase
{
14 protected function setUp() {
16 $this->setMwGlobals( [
17 'wgExtensionMessagesFiles' => [],
23 * @return LocalisationCache
25 protected function getMockLocalisationCache() {
28 $mockLangNameUtils = $this->createMock( LanguageNameUtils
::class );
29 $mockLangNameUtils->method( 'isValidBuiltInCode' )->will( $this->returnCallback(
31 // Copy-paste, but it's only one line
32 return (bool)preg_match( '/^[a-z0-9-]{2,}$/', $code );
35 $mockLangNameUtils->method( 'isSupportedLanguage' )->will( $this->returnCallback(
37 return in_array( $code, [
48 $mockLangNameUtils->method( 'getMessagesFileName' )->will( $this->returnCallback(
51 $code = str_replace( '-', '_', ucfirst( $code ) );
52 return "$IP/languages/messages/Messages$code.php";
55 $mockLangNameUtils->expects( $this->never() )->method( $this->anythingBut(
56 'isValidBuiltInCode', 'isSupportedLanguage', 'getMessagesFileName'
59 $lc = $this->getMockBuilder( LocalisationCache
::class )
60 ->setConstructorArgs( [
61 new ServiceOptions( LocalisationCache
::$constructorOptions, [
62 'forceRecache' => false,
63 'manualRecache' => false,
64 'ExtensionMessagesFiles' => [],
72 ->setMethods( [ 'getMessagesDirs' ] )
74 $lc->expects( $this->any() )->method( 'getMessagesDirs' )
75 ->will( $this->returnValue(
76 [ "$IP/tests/phpunit/data/localisationcache" ]
82 public function testPluralRulesFallback() {
83 $cache = $this->getMockLocalisationCache();
86 $cache->getItem( 'ar', 'pluralRules' ),
87 $cache->getItem( 'arz', 'pluralRules' ),
88 'arz plural rules (undefined) fallback to ar (defined)'
92 $cache->getItem( 'ar', 'compiledPluralRules' ),
93 $cache->getItem( 'arz', 'compiledPluralRules' ),
94 'arz compiled plural rules (undefined) fallback to ar (defined)'
97 $this->assertNotEquals(
98 $cache->getItem( 'ksh', 'pluralRules' ),
99 $cache->getItem( 'de', 'pluralRules' ),
100 'ksh plural rules (defined) dont fallback to de (defined)'
103 $this->assertNotEquals(
104 $cache->getItem( 'ksh', 'compiledPluralRules' ),
105 $cache->getItem( 'de', 'compiledPluralRules' ),
106 'ksh compiled plural rules (defined) dont fallback to de (defined)'
110 public function testRecacheFallbacks() {
111 $lc = $this->getMockLocalisationCache();
112 $lc->recache( 'ba' );
115 'present-ba' => 'ba',
116 'present-ru' => 'ru',
117 'present-en' => 'en',
119 $lc->getItem( 'ba', 'messages' ),
120 'Fallbacks are only used to fill missing data'
124 public function testRecacheFallbacksWithHooks() {
125 // Use hook to provide updates for messages. This is what the
126 // LocalisationUpdate extension does. See T70781.
127 $this->mergeMwGlobalArrayValue( 'wgHooks', [
128 'LocalisationCacheRecacheFallback' => [
130 LocalisationCache
$lc,
134 if ( $code === 'ru' ) {
135 $cache['messages']['present-ba'] = 'ru-override';
136 $cache['messages']['present-ru'] = 'ru-override';
137 $cache['messages']['present-en'] = 'ru-override';
143 $lc = $this->getMockLocalisationCache();
144 $lc->recache( 'ba' );
147 'present-ba' => 'ba',
148 'present-ru' => 'ru-override',
149 'present-en' => 'ru-override',
151 $lc->getItem( 'ba', 'messages' ),
152 'Updates provided by hooks follow the normal fallback order.'