Move DatabaseDomain to Rdbms namespace
[mediawiki.git] / tests / phpunit / includes / cache / LocalisationCacheTest.php
blobed821530a2b5b90219ebc587c6f7a993b0aa4e76
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 parent::setUp();
11 $this->setMwGlobals( [
12 'wgExtensionMessagesFiles' => [],
13 'wgHooks' => [],
14 ] );
17 /**
18 * @return LocalisationCache
20 protected function getMockLocalisationCache() {
21 global $IP;
22 $lc = $this->getMockBuilder( 'LocalisationCache' )
23 ->setConstructorArgs( [ [ 'store' => 'detect' ] ] )
24 ->setMethods( [ 'getMessagesDirs' ] )
25 ->getMock();
26 $lc->expects( $this->any() )->method( 'getMessagesDirs' )
27 ->will( $this->returnValue(
28 [ "$IP/tests/phpunit/data/localisationcache" ]
29 ) );
31 return $lc;
34 public function testPuralRulesFallback() {
35 $cache = $this->getMockLocalisationCache();
37 $this->assertEquals(
38 $cache->getItem( 'ar', 'pluralRules' ),
39 $cache->getItem( 'arz', 'pluralRules' ),
40 'arz plural rules (undefined) fallback to ar (defined)'
43 $this->assertEquals(
44 $cache->getItem( 'ar', 'compiledPluralRules' ),
45 $cache->getItem( 'arz', 'compiledPluralRules' ),
46 'arz compiled plural rules (undefined) fallback to ar (defined)'
49 $this->assertNotEquals(
50 $cache->getItem( 'ksh', 'pluralRules' ),
51 $cache->getItem( 'de', 'pluralRules' ),
52 'ksh plural rules (defined) dont fallback to de (defined)'
55 $this->assertNotEquals(
56 $cache->getItem( 'ksh', 'compiledPluralRules' ),
57 $cache->getItem( 'de', 'compiledPluralRules' ),
58 'ksh compiled plural rules (defined) dont fallback to de (defined)'
62 public function testRecacheFallbacks() {
63 $lc = $this->getMockLocalisationCache();
64 $lc->recache( 'ba' );
65 $this->assertEquals(
67 'present-ba' => 'ba',
68 'present-ru' => 'ru',
69 'present-en' => 'en',
71 $lc->getItem( 'ba', 'messages' ),
72 'Fallbacks are only used to fill missing data'
76 public function testRecacheFallbacksWithHooks() {
77 // Use hook to provide updates for messages. This is what the
78 // LocalisationUpdate extension does. See bug 68781.
79 $this->mergeMwGlobalArrayValue( 'wgHooks', [
80 'LocalisationCacheRecacheFallback' => [
81 function (
82 LocalisationCache $lc,
83 $code,
84 array &$cache
85 ) {
86 if ( $code === 'ru' ) {
87 $cache['messages']['present-ba'] = 'ru-override';
88 $cache['messages']['present-ru'] = 'ru-override';
89 $cache['messages']['present-en'] = 'ru-override';
93 ] );
95 $lc = $this->getMockLocalisationCache();
96 $lc->recache( 'ba' );
97 $this->assertEquals(
99 'present-ba' => 'ba',
100 'present-ru' => 'ru-override',
101 'present-en' => 'ru-override',
103 $lc->getItem( 'ba', 'messages' ),
104 'Updates provided by hooks follow the normal fallback order.'