Merge "Make LocalisationCache a service"
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / LocalisationCacheTest.php
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use Psr\Log\NullLogger;
5
6 /**
7 * @group Database
8 * @group Cache
9 * @covers LocalisationCache
10 * @author Niklas Laxström
11 */
12 class LocalisationCacheTest extends MediaWikiTestCase {
13 protected function setUp() {
14 parent::setUp();
15 $this->setMwGlobals( [
16 'wgExtensionMessagesFiles' => [],
17 'wgHooks' => [],
18 ] );
19 }
20
21 /**
22 * @return LocalisationCache
23 */
24 protected function getMockLocalisationCache() {
25 global $IP;
26
27 $lc = $this->getMockBuilder( LocalisationCache::class )
28 ->setConstructorArgs( [
29 new ServiceOptions( LocalisationCache::$constructorOptions, [
30 'forceRecache' => false,
31 'manualRecache' => false,
32 'ExtensionMessagesFiles' => [],
33 'MessagesDirs' => [],
34 ] ),
35 new LCStoreDB( [] ),
36 new NullLogger
37 ] )
38 ->setMethods( [ 'getMessagesDirs' ] )
39 ->getMock();
40 $lc->expects( $this->any() )->method( 'getMessagesDirs' )
41 ->will( $this->returnValue(
42 [ "$IP/tests/phpunit/data/localisationcache" ]
43 ) );
44
45 return $lc;
46 }
47
48 public function testPluralRulesFallback() {
49 $cache = $this->getMockLocalisationCache();
50
51 $this->assertEquals(
52 $cache->getItem( 'ar', 'pluralRules' ),
53 $cache->getItem( 'arz', 'pluralRules' ),
54 'arz plural rules (undefined) fallback to ar (defined)'
55 );
56
57 $this->assertEquals(
58 $cache->getItem( 'ar', 'compiledPluralRules' ),
59 $cache->getItem( 'arz', 'compiledPluralRules' ),
60 'arz compiled plural rules (undefined) fallback to ar (defined)'
61 );
62
63 $this->assertNotEquals(
64 $cache->getItem( 'ksh', 'pluralRules' ),
65 $cache->getItem( 'de', 'pluralRules' ),
66 'ksh plural rules (defined) dont fallback to de (defined)'
67 );
68
69 $this->assertNotEquals(
70 $cache->getItem( 'ksh', 'compiledPluralRules' ),
71 $cache->getItem( 'de', 'compiledPluralRules' ),
72 'ksh compiled plural rules (defined) dont fallback to de (defined)'
73 );
74 }
75
76 public function testRecacheFallbacks() {
77 $lc = $this->getMockLocalisationCache();
78 $lc->recache( 'ba' );
79 $this->assertEquals(
80 [
81 'present-ba' => 'ba',
82 'present-ru' => 'ru',
83 'present-en' => 'en',
84 ],
85 $lc->getItem( 'ba', 'messages' ),
86 'Fallbacks are only used to fill missing data'
87 );
88 }
89
90 public function testRecacheFallbacksWithHooks() {
91 // Use hook to provide updates for messages. This is what the
92 // LocalisationUpdate extension does. See T70781.
93 $this->mergeMwGlobalArrayValue( 'wgHooks', [
94 'LocalisationCacheRecacheFallback' => [
95 function (
96 LocalisationCache $lc,
97 $code,
98 array &$cache
99 ) {
100 if ( $code === 'ru' ) {
101 $cache['messages']['present-ba'] = 'ru-override';
102 $cache['messages']['present-ru'] = 'ru-override';
103 $cache['messages']['present-en'] = 'ru-override';
104 }
105 }
106 ]
107 ] );
108
109 $lc = $this->getMockLocalisationCache();
110 $lc->recache( 'ba' );
111 $this->assertEquals(
112 [
113 'present-ba' => 'ba',
114 'present-ru' => 'ru-override',
115 'present-en' => 'ru-override',
116 ],
117 $lc->getItem( 'ba', 'messages' ),
118 'Updates provided by hooks follow the normal fallback order.'
119 );
120 }
121 }