Merge "maintenance: Fix bad method call in RebuildTextIndex"
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / LocalisationCacheTest.php
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\Languages\LanguageNameUtils;
5 use Psr\Log\NullLogger;
6
7 /**
8 * @group Database
9 * @group Cache
10 * @covers LocalisationCache
11 * @author Niklas Laxström
12 */
13 class LocalisationCacheTest extends MediaWikiTestCase {
14 protected function setUp() {
15 parent::setUp();
16 $this->setMwGlobals( [
17 'wgExtensionMessagesFiles' => [],
18 'wgHooks' => [],
19 ] );
20 }
21
22 /**
23 * @return LocalisationCache
24 */
25 protected function getMockLocalisationCache() {
26 global $IP;
27
28 $mockLangNameUtils = $this->createMock( LanguageNameUtils::class );
29 $mockLangNameUtils->method( 'isValidBuiltInCode' )->will( $this->returnCallback(
30 function ( $code ) {
31 // Copy-paste, but it's only one line
32 return (bool)preg_match( '/^[a-z0-9-]{2,}$/', $code );
33 }
34 ) );
35 $mockLangNameUtils->method( 'isSupportedLanguage' )->will( $this->returnCallback(
36 function ( $code ) {
37 return in_array( $code, [
38 'ar',
39 'arz',
40 'ba',
41 'de',
42 'en',
43 'ksh',
44 'ru',
45 ] );
46 }
47 ) );
48 $mockLangNameUtils->method( 'getMessagesFileName' )->will( $this->returnCallback(
49 function ( $code ) {
50 global $IP;
51 $code = str_replace( '-', '_', ucfirst( $code ) );
52 return "$IP/languages/messages/Messages$code.php";
53 }
54 ) );
55 $mockLangNameUtils->expects( $this->never() )->method( $this->anythingBut(
56 'isValidBuiltInCode', 'isSupportedLanguage', 'getMessagesFileName'
57 ) );
58
59 $lc = $this->getMockBuilder( LocalisationCache::class )
60 ->setConstructorArgs( [
61 new ServiceOptions( LocalisationCache::$constructorOptions, [
62 'forceRecache' => false,
63 'manualRecache' => false,
64 'ExtensionMessagesFiles' => [],
65 'MessagesDirs' => [],
66 ] ),
67 new LCStoreDB( [] ),
68 new NullLogger,
69 [],
70 $mockLangNameUtils
71 ] )
72 ->setMethods( [ 'getMessagesDirs' ] )
73 ->getMock();
74 $lc->expects( $this->any() )->method( 'getMessagesDirs' )
75 ->will( $this->returnValue(
76 [ "$IP/tests/phpunit/data/localisationcache" ]
77 ) );
78
79 return $lc;
80 }
81
82 public function testPluralRulesFallback() {
83 $cache = $this->getMockLocalisationCache();
84
85 $this->assertEquals(
86 $cache->getItem( 'ar', 'pluralRules' ),
87 $cache->getItem( 'arz', 'pluralRules' ),
88 'arz plural rules (undefined) fallback to ar (defined)'
89 );
90
91 $this->assertEquals(
92 $cache->getItem( 'ar', 'compiledPluralRules' ),
93 $cache->getItem( 'arz', 'compiledPluralRules' ),
94 'arz compiled plural rules (undefined) fallback to ar (defined)'
95 );
96
97 $this->assertNotEquals(
98 $cache->getItem( 'ksh', 'pluralRules' ),
99 $cache->getItem( 'de', 'pluralRules' ),
100 'ksh plural rules (defined) dont fallback to de (defined)'
101 );
102
103 $this->assertNotEquals(
104 $cache->getItem( 'ksh', 'compiledPluralRules' ),
105 $cache->getItem( 'de', 'compiledPluralRules' ),
106 'ksh compiled plural rules (defined) dont fallback to de (defined)'
107 );
108 }
109
110 public function testRecacheFallbacks() {
111 $lc = $this->getMockLocalisationCache();
112 $lc->recache( 'ba' );
113 $this->assertEquals(
114 [
115 'present-ba' => 'ba',
116 'present-ru' => 'ru',
117 'present-en' => 'en',
118 ],
119 $lc->getItem( 'ba', 'messages' ),
120 'Fallbacks are only used to fill missing data'
121 );
122 }
123
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' => [
129 function (
130 LocalisationCache $lc,
131 $code,
132 array &$cache
133 ) {
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';
138 }
139 }
140 ]
141 ] );
142
143 $lc = $this->getMockLocalisationCache();
144 $lc->recache( 'ba' );
145 $this->assertEquals(
146 [
147 'present-ba' => 'ba',
148 'present-ru' => 'ru-override',
149 'present-en' => 'ru-override',
150 ],
151 $lc->getItem( 'ba', 'messages' ),
152 'Updates provided by hooks follow the normal fallback order.'
153 );
154 }
155 }