This reduces the acceptable forms for hook handlers to three things: * a callable (in the form of a string, an array, or a closure) * an object, which is expected to have a public "on" method that matches the hook name. * an array containing an object spec in the "handler" key, for use with ExtensionRegistry. All other forms will trigger a deprecation warning. Bug: T339167 Depends-On: I980f2d45e6bb8c6a04058e68c758f71bbcf709de Depends-On: Ieae405f70caa01d84602583cc214b0ee3fadc796 Depends-On: If15df4b598c02ed9bda5eea0ae89a16ebbf4f2e2 Depends-On: Id70276fa1e1821bd400dc0ae5cea722a21d524d5 Change-Id: I83bc81d1b3033c38b9313884a9c70a187fdde227
123 lines
3.2 KiB
PHP
123 lines
3.2 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
|
use Psr\Log\NullLogger;
|
|
|
|
/**
|
|
* @group Database
|
|
* @group Cache
|
|
* @covers LocalisationCache
|
|
* @author Niklas Laxström
|
|
*/
|
|
class LocalisationCacheTest extends MediaWikiIntegrationTestCase {
|
|
use DummyServicesTrait;
|
|
|
|
/**
|
|
* @param array $hooks Hook overrides
|
|
* @return LocalisationCache
|
|
*/
|
|
protected function getMockLocalisationCache( $hooks = [] ) {
|
|
global $IP;
|
|
|
|
$hookContainer = $this->createHookContainer( $hooks );
|
|
|
|
// in case any of the LanguageNameUtils hooks are being used
|
|
$langNameUtils = $this->getDummyLanguageNameUtils(
|
|
[ 'hookContainer' => $hookContainer ]
|
|
);
|
|
|
|
$lc = $this->getMockBuilder( LocalisationCache::class )
|
|
->setConstructorArgs( [
|
|
new ServiceOptions( LocalisationCache::CONSTRUCTOR_OPTIONS, [
|
|
'forceRecache' => false,
|
|
'manualRecache' => false,
|
|
'ExtensionMessagesFiles' => [],
|
|
'MessagesDirs' => [],
|
|
] ),
|
|
new LCStoreDB( [] ),
|
|
new NullLogger,
|
|
[],
|
|
$langNameUtils,
|
|
$hookContainer
|
|
] )
|
|
->onlyMethods( [ 'getMessagesDirs' ] )
|
|
->getMock();
|
|
$lc->method( 'getMessagesDirs' )
|
|
->willReturn( [ "$IP/tests/phpunit/data/localisationcache" ] );
|
|
|
|
return $lc;
|
|
}
|
|
|
|
public function testPluralRulesFallback() {
|
|
$cache = $this->getMockLocalisationCache();
|
|
|
|
$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 = $this->getMockLocalisationCache();
|
|
$lc->recache( 'ba' );
|
|
$this->assertEquals(
|
|
[
|
|
'present-ba' => 'ba',
|
|
'present-ru' => 'ru',
|
|
'present-en' => 'en',
|
|
],
|
|
$lc->getItem( 'ba', 'messages' ),
|
|
'Fallbacks are only used to fill missing data'
|
|
);
|
|
}
|
|
|
|
public function testRecacheFallbacksWithHooks() {
|
|
// Use hook to provide updates for messages. This is what the
|
|
// LocalisationUpdate extension does. See T70781.
|
|
|
|
$lc = $this->getMockLocalisationCache( [
|
|
'LocalisationCacheRecacheFallback' =>
|
|
static function (
|
|
LocalisationCache $lc,
|
|
$code,
|
|
array &$cache
|
|
) {
|
|
if ( $code === 'ru' ) {
|
|
$cache['messages']['present-ba'] = 'ru-override';
|
|
$cache['messages']['present-ru'] = 'ru-override';
|
|
$cache['messages']['present-en'] = 'ru-override';
|
|
}
|
|
}
|
|
] );
|
|
$lc->recache( 'ba' );
|
|
$this->assertEquals(
|
|
[
|
|
'present-ba' => 'ba',
|
|
'present-ru' => 'ru-override',
|
|
'present-en' => 'ru-override',
|
|
],
|
|
$lc->getItem( 'ba', 'messages' ),
|
|
'Updates provided by hooks follow the normal fallback order.'
|
|
);
|
|
}
|
|
}
|