3 use MediaWiki\Config\ServiceOptions
;
4 use Psr\Log\NullLogger
;
9 * @covers LocalisationCache
10 * @author Niklas Laxström
12 class LocalisationCacheTest
extends MediaWikiTestCase
{
13 protected function setUp() {
15 $this->setMwGlobals( [
16 'wgExtensionMessagesFiles' => [],
22 * @return LocalisationCache
24 protected function getMockLocalisationCache() {
27 $lc = $this->getMockBuilder( LocalisationCache
::class )
28 ->setConstructorArgs( [
29 new ServiceOptions( LocalisationCache
::$constructorOptions, [
30 'forceRecache' => false,
31 'manualRecache' => false,
32 'ExtensionMessagesFiles' => [],
38 ->setMethods( [ 'getMessagesDirs' ] )
40 $lc->expects( $this->any() )->method( 'getMessagesDirs' )
41 ->will( $this->returnValue(
42 [ "$IP/tests/phpunit/data/localisationcache" ]
48 public function testPluralRulesFallback() {
49 $cache = $this->getMockLocalisationCache();
52 $cache->getItem( 'ar', 'pluralRules' ),
53 $cache->getItem( 'arz', 'pluralRules' ),
54 'arz plural rules (undefined) fallback to ar (defined)'
58 $cache->getItem( 'ar', 'compiledPluralRules' ),
59 $cache->getItem( 'arz', 'compiledPluralRules' ),
60 'arz compiled plural rules (undefined) fallback to ar (defined)'
63 $this->assertNotEquals(
64 $cache->getItem( 'ksh', 'pluralRules' ),
65 $cache->getItem( 'de', 'pluralRules' ),
66 'ksh plural rules (defined) dont fallback to de (defined)'
69 $this->assertNotEquals(
70 $cache->getItem( 'ksh', 'compiledPluralRules' ),
71 $cache->getItem( 'de', 'compiledPluralRules' ),
72 'ksh compiled plural rules (defined) dont fallback to de (defined)'
76 public function testRecacheFallbacks() {
77 $lc = $this->getMockLocalisationCache();
85 $lc->getItem( 'ba', 'messages' ),
86 'Fallbacks are only used to fill missing data'
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' => [
96 LocalisationCache
$lc,
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';
109 $lc = $this->getMockLocalisationCache();
110 $lc->recache( 'ba' );
113 'present-ba' => 'ba',
114 'present-ru' => 'ru-override',
115 'present-en' => 'ru-override',
117 $lc->getItem( 'ba', 'messages' ),
118 'Updates provided by hooks follow the normal fallback order.'