wiki.techinc.nl/tests/phpunit/includes/cache/LocalisationCacheTest.php
Brad Jorsch b6fc9067b0 LocalisationCache: Process one fallback at a time
Currently LocalisationCache merges the core data for all languages in
the fallback chain, then the extension data, then merges those two, and
then gives extensions like LocalisationUpdate a chance to make final
overrides with the LocalisationCacheRecache hook.

But if LocalisationUpdate doesn't want to locally duplicate all the
messages for every language (e.g. r104041), LocalisationCacheRecache is
too late: the information as to whether a message came from the primary
language or a fallback has been lost, so when LU itself has an override
for a fallback language it can't know whether or not the existing
message should be overridden or not.

The solution is for LocalisationCache to gather the data for each
fallback language separately, call a new hook for LU to affect just that
language (LocalisationCacheRecacheFallback), and only then merge the
fallback languages together.

Bug: 68781
Change-Id: Iacfe96063fcc66c1f97ca5e5292a8fc70af988cf
2014-09-04 16:56:31 +02:00

91 lines
2.4 KiB
PHP

<?php
/**
* @group Database
* @group Cache
* @covers LocalisationCache
* @author Niklas Laxström
*/
class LocalisationCacheTest extends MediaWikiTestCase {
protected function setUp() {
global $IP;
parent::setUp();
$this->setMwGlobals( array(
'wgMessagesDirs' => array( "$IP/tests/phpunit/data/localisationcache" ),
'wgExtensionMessagesFiles' => array(),
'wgHooks' => array(),
) );
}
public function testPuralRulesFallback() {
$cache = new LocalisationCache( array( 'store' => 'detect' ) );
$this->assertEquals(
$cache->getItem( 'ar', 'pluralRules' ),
$cache->getItem( 'arz', 'pluralRules' ),
'arz plural rules (undefined) fallback to ar (defined)'
);
$this->assertEquals(
$cache->getItem( 'ar', 'compiledPluralRules' ),
$cache->getItem( 'arz', 'compiledPluralRules' ),
'arz compiled plural rules (undefined) fallback to ar (defined)'
);
$this->assertNotEquals(
$cache->getItem( 'ksh', 'pluralRules' ),
$cache->getItem( 'de', 'pluralRules' ),
'ksh plural rules (defined) dont fallback to de (defined)'
);
$this->assertNotEquals(
$cache->getItem( 'ksh', 'compiledPluralRules' ),
$cache->getItem( 'de', 'compiledPluralRules' ),
'ksh compiled plural rules (defined) dont fallback to de (defined)'
);
}
public function testRecacheFallbacks() {
$lc = new LocalisationCache( array( 'store' => 'detect' ) );
$lc->recache( 'uk' );
$this->assertEquals(
array(
'present-uk' => 'uk',
'present-ru' => 'ru',
'present-en' => 'en',
),
$lc->getItem( 'uk', 'messages' ),
'Fallbacks are only used to fill missing data'
);
}
public function testRecacheFallbacksWithHooks() {
global $wgHooks;
// Use hook to provide updates for messages. This is what the
// LocalisationUpdate extension does. See bug 68781.
$wgHooks['LocalisationCacheRecacheFallback'][] = function (
LocalisationCache $lc,
$code,
array &$cache
) {
if ( $code === 'ru' ) {
$cache['messages']['present-uk'] = 'ru-override';
$cache['messages']['present-ru'] = 'ru-override';
$cache['messages']['present-en'] = 'ru-override';
}
};
$lc = new LocalisationCache( array( 'store' => 'detect' ) );
$lc->recache( 'uk' );
$this->assertEquals(
array(
'present-uk' => 'uk',
'present-ru' => 'ru-override',
'present-en' => 'ru-override',
),
$lc->getItem( 'uk', 'messages' ),
'Updates provided by hooks follow the normal fallback order.'
);
}
}