Use mergeMwGlobalArrayValue in LocalisationCacheTest
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / LocalisationCacheTest.php
1 <?php
2 /**
3 * @group Database
4 * @group Cache
5 * @covers LocalisationCache
6 * @author Niklas Laxström
7 */
8 class LocalisationCacheTest extends MediaWikiTestCase {
9 protected function setUp() {
10 global $IP;
11
12 parent::setUp();
13 $this->setMwGlobals( array(
14 'wgMessagesDirs' => array( "$IP/tests/phpunit/data/localisationcache" ),
15 'wgExtensionMessagesFiles' => array(),
16 'wgHooks' => array(),
17 ) );
18 }
19
20 public function testPuralRulesFallback() {
21 $cache = new LocalisationCache( array( 'store' => 'detect' ) );
22
23 $this->assertEquals(
24 $cache->getItem( 'ar', 'pluralRules' ),
25 $cache->getItem( 'arz', 'pluralRules' ),
26 'arz plural rules (undefined) fallback to ar (defined)'
27 );
28
29 $this->assertEquals(
30 $cache->getItem( 'ar', 'compiledPluralRules' ),
31 $cache->getItem( 'arz', 'compiledPluralRules' ),
32 'arz compiled plural rules (undefined) fallback to ar (defined)'
33 );
34
35 $this->assertNotEquals(
36 $cache->getItem( 'ksh', 'pluralRules' ),
37 $cache->getItem( 'de', 'pluralRules' ),
38 'ksh plural rules (defined) dont fallback to de (defined)'
39 );
40
41 $this->assertNotEquals(
42 $cache->getItem( 'ksh', 'compiledPluralRules' ),
43 $cache->getItem( 'de', 'compiledPluralRules' ),
44 'ksh compiled plural rules (defined) dont fallback to de (defined)'
45 );
46 }
47
48 public function testRecacheFallbacks() {
49 $lc = new LocalisationCache( array( 'store' => 'detect' ) );
50 $lc->recache( 'uk' );
51 $this->assertEquals(
52 array(
53 'present-uk' => 'uk',
54 'present-ru' => 'ru',
55 'present-en' => 'en',
56 ),
57 $lc->getItem( 'uk', 'messages' ),
58 'Fallbacks are only used to fill missing data'
59 );
60 }
61
62 public function testRecacheFallbacksWithHooks() {
63 // Use hook to provide updates for messages. This is what the
64 // LocalisationUpdate extension does. See bug 68781.
65 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
66 'LocalisationCacheRecacheFallback' => array(
67 function (
68 LocalisationCache $lc,
69 $code,
70 array &$cache
71 ) {
72 if ( $code === 'ru' ) {
73 $cache['messages']['present-uk'] = 'ru-override';
74 $cache['messages']['present-ru'] = 'ru-override';
75 $cache['messages']['present-en'] = 'ru-override';
76 }
77 }
78 )
79 ) );
80
81 $lc = new LocalisationCache( array( 'store' => 'detect' ) );
82 $lc->recache( 'uk' );
83 $this->assertEquals(
84 array(
85 'present-uk' => 'uk',
86 'present-ru' => 'ru-override',
87 'present-en' => 'ru-override',
88 ),
89 $lc->getItem( 'uk', 'messages' ),
90 'Updates provided by hooks follow the normal fallback order.'
91 );
92 }
93 }