MediaWiki  REL1_31
LocalisationCacheTest.php
Go to the documentation of this file.
1 <?php
9  protected function setUp() {
10  parent::setUp();
11  $this->setMwGlobals( [
12  'wgExtensionMessagesFiles' => [],
13  'wgHooks' => [],
14  ] );
15  }
16 
20  protected function getMockLocalisationCache() {
21  global $IP;
22  $lc = $this->getMockBuilder( \LocalisationCache::class )
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  ) );
30 
31  return $lc;
32  }
33 
34  public function testPuralRulesFallback() {
36 
37  $this->assertEquals(
38  $cache->getItem( 'ar', 'pluralRules' ),
39  $cache->getItem( 'arz', 'pluralRules' ),
40  'arz plural rules (undefined) fallback to ar (defined)'
41  );
42 
43  $this->assertEquals(
44  $cache->getItem( 'ar', 'compiledPluralRules' ),
45  $cache->getItem( 'arz', 'compiledPluralRules' ),
46  'arz compiled plural rules (undefined) fallback to ar (defined)'
47  );
48 
49  $this->assertNotEquals(
50  $cache->getItem( 'ksh', 'pluralRules' ),
51  $cache->getItem( 'de', 'pluralRules' ),
52  'ksh plural rules (defined) dont fallback to de (defined)'
53  );
54 
55  $this->assertNotEquals(
56  $cache->getItem( 'ksh', 'compiledPluralRules' ),
57  $cache->getItem( 'de', 'compiledPluralRules' ),
58  'ksh compiled plural rules (defined) dont fallback to de (defined)'
59  );
60  }
61 
62  public function testRecacheFallbacks() {
63  $lc = $this->getMockLocalisationCache();
64  $lc->recache( 'ba' );
65  $this->assertEquals(
66  [
67  'present-ba' => 'ba',
68  'present-ru' => 'ru',
69  'present-en' => 'en',
70  ],
71  $lc->getItem( 'ba', 'messages' ),
72  'Fallbacks are only used to fill missing data'
73  );
74  }
75 
76  public function testRecacheFallbacksWithHooks() {
77  // Use hook to provide updates for messages. This is what the
78  // LocalisationUpdate extension does. See T70781.
79  $this->mergeMwGlobalArrayValue( 'wgHooks', [
80  'LocalisationCacheRecacheFallback' => [
81  function (
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';
90  }
91  }
92  ]
93  ] );
94 
95  $lc = $this->getMockLocalisationCache();
96  $lc->recache( 'ba' );
97  $this->assertEquals(
98  [
99  'present-ba' => 'ba',
100  'present-ru' => 'ru-override',
101  'present-en' => 'ru-override',
102  ],
103  $lc->getItem( 'ba', 'messages' ),
104  'Updates provided by hooks follow the normal fallback order.'
105  );
106  }
107 }
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:813
array
the array() calling protocol came about after MediaWiki 1.4rc1.
LocalisationCacheTest
Database Cache LocalisationCache.
Definition: LocalisationCacheTest.php:8
LocalisationCacheTest\testPuralRulesFallback
testPuralRulesFallback()
Definition: LocalisationCacheTest.php:34
LocalisationCacheTest\getMockLocalisationCache
getMockLocalisationCache()
Definition: LocalisationCacheTest.php:20
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
LocalisationCacheTest\testRecacheFallbacks
testRecacheFallbacks()
Definition: LocalisationCacheTest.php:62
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
LocalisationCacheTest\testRecacheFallbacksWithHooks
testRecacheFallbacksWithHooks()
Definition: LocalisationCacheTest.php:76
LocalisationCache
Class for caching the contents of localisation files, Messages*.php and *.i18n.php.
Definition: LocalisationCache.php:39
$cache
$cache
Definition: mcc.php:33
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:865
LocalisationCacheTest\setUp
setUp()
Definition: LocalisationCacheTest.php:9
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
$IP
$IP
Definition: WebStart.php:52