Merge "docs: Fix typo"
[mediawiki.git] / tests / phpunit / includes / language / LocalisationCacheTest.php
blob2e033124c049fbb20918d42e08f4b8d20df3f681
1 <?php
3 use MediaWiki\Tests\Language\MockLocalisationCacheTrait;
4 use MediaWiki\Tests\Unit\DummyServicesTrait;
5 use Wikimedia\TestingAccessWrapper;
7 /**
8 * @group Language
9 * @group Database
10 * @covers \LocalisationCache
11 * @author Niklas Laxström
13 class LocalisationCacheTest extends MediaWikiIntegrationTestCase {
14 use DummyServicesTrait;
15 use MockLocalisationCacheTrait;
17 public function testPluralRulesFallback() {
18 $cache = $this->getMockLocalisationCache();
20 $this->assertEquals(
21 $cache->getItem( 'ar', 'pluralRules' ),
22 $cache->getItem( 'arz', 'pluralRules' ),
23 'arz plural rules (undefined) fallback to ar (defined)'
26 $this->assertEquals(
27 $cache->getItem( 'ar', 'compiledPluralRules' ),
28 $cache->getItem( 'arz', 'compiledPluralRules' ),
29 'arz compiled plural rules (undefined) fallback to ar (defined)'
32 $this->assertNotEquals(
33 $cache->getItem( 'ksh', 'pluralRules' ),
34 $cache->getItem( 'de', 'pluralRules' ),
35 'ksh plural rules (defined) dont fallback to de (defined)'
38 $this->assertNotEquals(
39 $cache->getItem( 'ksh', 'compiledPluralRules' ),
40 $cache->getItem( 'de', 'compiledPluralRules' ),
41 'ksh compiled plural rules (defined) dont fallback to de (defined)'
45 public function testRecacheFallbacks() {
46 $lc = $this->getMockLocalisationCache();
47 $lc->recache( 'ba' );
48 $messages = $lc->getItem( 'ba', 'messages' );
50 // Fallbacks are only used to fill missing data
51 $this->assertSame( 'ba', $messages['present-ba'] );
52 $this->assertSame( 'ru', $messages['present-ru'] );
53 $this->assertSame( 'en', $messages['present-en'] );
56 public function testRecacheFallbacksWithHooks() {
57 // Use hook to provide updates for messages. This is what the
58 // LocalisationUpdate extension does. See T70781.
60 $lc = $this->getMockLocalisationCache( [
61 'LocalisationCacheRecacheFallback' =>
62 static function (
63 LocalisationCache $lc,
64 $code,
65 array &$cache
66 ) {
67 if ( $code === 'ru' ) {
68 $cache['messages']['present-ba'] = 'ru-override';
69 $cache['messages']['present-ru'] = 'ru-override';
70 $cache['messages']['present-en'] = 'ru-override';
73 ] );
74 $lc->recache( 'ba' );
75 $messages = $lc->getItem( 'ba', 'messages' );
77 // Updates provided by hooks follow the normal fallback order.
78 $this->assertSame( 'ba', $messages['present-ba'] );
79 $this->assertSame( 'ru-override', $messages['present-ru'] );
80 $this->assertSame( 'ru-override', $messages['present-en'] );
83 public function testRecacheExtensionMessagesFiles(): void {
84 global $IP;
86 // first, recache the l10n cache and test it
87 $lc = $this->getMockLocalisationCache( [], [
88 'ExtensionMessagesFiles' => [
89 __METHOD__ => "$IP/tests/phpunit/data/localisationcache/ExtensionMessagesFiles.php",
91 ] );
92 $lc->recache( 'de' );
93 $this->assertExtensionMessagesFiles( $lc );
95 // then, make another l10n cache sharing the first one’s LCStore and test that (T343375)
96 $lc = $this->getMockLocalisationCache( [], [
97 'ExtensionMessagesFiles' => [
98 __METHOD__ => "$IP/tests/phpunit/data/localisationcache/ExtensionMessagesFiles.php",
100 ] );
101 // no recache this time, but load only the core data first by getting the fallbackSequence
102 $lc->getItem( 'de', 'fallbackSequence' );
103 $this->assertExtensionMessagesFiles( $lc );
106 public function testRecacheTranslationAliasesDirs(): void {
107 global $IP;
109 $lc = $this->getMockLocalisationCache( [], [
110 'TranslationAliasesDirs' => [
111 __METHOD__ => "$IP/tests/phpunit/data/localisationcache/translation-alias/"
113 ] );
115 $lc->recache( 'nl' );
116 $specialPageAliases = $lc->getItem( 'nl', 'specialPageAliases' );
117 $this->assertSame(
118 [ "Vertalersmeldingen(TEST)", "NotifyTranslators(TEST)" ],
119 $specialPageAliases['NotifyTranslators'],
120 'specialPageAliases can be set in TranslationAliasesDirs'
122 $this->assertSame(
123 [ 'ActieveGebruikers(TEST)', 'ActieveGebruikers', 'ActiveUsers' ],
124 $specialPageAliases['Activeusers'],
125 'specialPageAliases from extension/core files are merged'
128 $lc->recache( 'pt' );
129 $specialPageAliases = $lc->getItem( 'pt', 'specialPageAliases' );
130 $this->assertSame(
131 [ 'Utilizadores_activos(TEST)', 'Utilizadores_activos', 'Usuários_ativos', 'ActiveUsers' ],
132 $specialPageAliases['Activeusers'],
133 'specialPageAliases from extension/core files and fallback languages are merged'
136 $this->expectException( UnexpectedValueException::class );
137 $this->expectExceptionMessageMatches( '/invalid key:/i' );
138 $lc->recache( 'fr' );
142 * Assert that the given LocalisationCache, which should be configured with
143 * ExtensionMessagesFiles containing the ExtensionMessagesFiles.php test fixture file,
144 * contains the expected data.
146 private function assertExtensionMessagesFiles( LocalisationCache $lc ): void {
147 $specialPageAliases = $lc->getItem( 'de', 'specialPageAliases' );
148 $this->assertSame(
149 [ 'LokalisierungsPufferTest' ],
150 $specialPageAliases['LocalisationCacheTest'],
151 'specialPageAliases can be set in ExtensionMessagesFiles'
153 $this->assertSame(
154 [ 'Aktive_Benutzer*innen', 'Aktive_Benutzer', 'ActiveFolx', 'ActiveUsers' ],
155 $specialPageAliases['Activeusers'],
156 'specialPageAliases from extension/core files and fallback languages are merged'
158 $namespaceNames = $lc->getItem( 'de', 'namespaceNames' );
159 $this->assertSame(
160 'LokalisierungsPufferTest',
161 $namespaceNames[98]
163 $this->assertFalse(
164 $lc->getItem( 'de', 'rtl' ),
165 'rtl cannot be set in ExtensionMessagesFiles'
169 public function testLoadCoreDataAvoidsInitLanguage(): void {
170 $lc = $this->getMockLocalisationCache();
172 $lc->getItem( 'de', 'fallback' );
173 $lc->getItem( 'de', 'rtl' );
174 $lc->getItem( 'de', 'fallbackSequence' );
175 $lc->getItem( 'de', 'originalFallbackSequence' );
177 $this->assertArrayNotHasKey( 'de',
178 TestingAccessWrapper::newFromObject( $lc )->initialisedLangs );
181 public function testShallowFallbackForInvalidCode(): void {
182 $lc = $this->getMockLocalisationCache();
183 $invalidCode = '!invalid!';
185 $this->assertSame( false, $lc->getItem( $invalidCode, 'rtl' ) );
186 $this->assertSame( 'windows-1252', $lc->getItem( $invalidCode, 'fallback8bitEncoding' ) );