wiki.techinc.nl/tests/phpunit/includes/cache/LocalisationCacheTest.php
Aryeh Gregor 043d88f680 Make LocalisationCache a service
This removes Language::$dataCache without deprecation, because 1) I
don't know of a way to properly simulate it in the new paradigm, and 2)
I found no direct access to the member outside of the Language and
LanguageTest classes.

An earlier version of this patch (e4468a1d6b) had to be reverted
because of a massive slowdown on test runs. Based on some local testing,
this should fix the problem. Running all tests in languages is slowed
down by only around 20% instead of a factor of five, and memory usage is
actually reduced greatly (~350 MB -> ~200 MB). The slowdown is still not
great, but I assume it's par for the course for converting things to
services and is acceptable. If not, I can try to optimize further.

Bug: T231220
Bug: T231198
Bug: T231200
Bug: T201405
Change-Id: Ieadbd820379a006d8ad2d2e4a1e96241e172ec5a
2019-10-07 13:18:47 -07:00

121 lines
3 KiB
PHP

<?php
use MediaWiki\Config\ServiceOptions;
use Psr\Log\NullLogger;
/**
* @group Database
* @group Cache
* @covers LocalisationCache
* @author Niklas Laxström
*/
class LocalisationCacheTest extends MediaWikiTestCase {
protected function setUp() {
parent::setUp();
$this->setMwGlobals( [
'wgExtensionMessagesFiles' => [],
'wgHooks' => [],
] );
}
/**
* @return LocalisationCache
*/
protected function getMockLocalisationCache() {
global $IP;
$lc = $this->getMockBuilder( LocalisationCache::class )
->setConstructorArgs( [
new ServiceOptions( LocalisationCache::$constructorOptions, [
'forceRecache' => false,
'manualRecache' => false,
'ExtensionMessagesFiles' => [],
'MessagesDirs' => [],
] ),
new LCStoreDB( [] ),
new NullLogger
] )
->setMethods( [ 'getMessagesDirs' ] )
->getMock();
$lc->expects( $this->any() )->method( 'getMessagesDirs' )
->will( $this->returnValue(
[ "$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.
$this->mergeMwGlobalArrayValue( 'wgHooks', [
'LocalisationCacheRecacheFallback' => [
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 = $this->getMockLocalisationCache();
$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.'
);
}
}