<?php
+
+use MediaWiki\Config\ServiceOptions;
+use Psr\Log\NullLogger;
+
/**
* @group Database
* @group Cache
*/
class LocalisationCacheTest extends MediaWikiTestCase {
protected function setUp() {
+ parent::setUp();
+ $this->setMwGlobals( [
+ 'wgExtensionMessagesFiles' => [],
+ 'wgHooks' => [],
+ ] );
+ }
+
+ /**
+ * @return LocalisationCache
+ */
+ protected function getMockLocalisationCache() {
global $IP;
- parent::setUp();
- $this->setMwGlobals( array(
- 'wgMessagesDirs' => array( "$IP/tests/phpunit/data/localisationcache" ),
- 'wgExtensionMessagesFiles' => array(),
- 'wgHooks' => array(),
- ) );
+ $lc = $this->getMockBuilder( LocalisationCache::class )
+ ->setConstructorArgs( [
+ new ServiceOptions( LocalisationCache::CONSTRUCTOR_OPTIONS, [
+ '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 testPuralRulesFallback() {
- $cache = new LocalisationCache( array( 'store' => 'detect' ) );
+ public function testPluralRulesFallback() {
+ $cache = $this->getMockLocalisationCache();
$this->assertEquals(
$cache->getItem( 'ar', 'pluralRules' ),
}
public function testRecacheFallbacks() {
- $lc = new LocalisationCache( array( 'store' => 'detect' ) );
- $lc->recache( 'uk' );
+ $lc = $this->getMockLocalisationCache();
+ $lc->recache( 'ba' );
$this->assertEquals(
- array(
- 'present-uk' => 'uk',
+ [
+ 'present-ba' => 'ba',
'present-ru' => 'ru',
'present-en' => 'en',
- ),
- $lc->getItem( 'uk', 'messages' ),
+ ],
+ $lc->getItem( 'ba', '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';
- }
- };
+ // 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 = new LocalisationCache( array( 'store' => 'detect' ) );
- $lc->recache( 'uk' );
+ $lc = $this->getMockLocalisationCache();
+ $lc->recache( 'ba' );
$this->assertEquals(
- array(
- 'present-uk' => 'uk',
+ [
+ 'present-ba' => 'ba',
'present-ru' => 'ru-override',
'present-en' => 'ru-override',
- ),
- $lc->getItem( 'uk', 'messages' ),
+ ],
+ $lc->getItem( 'ba', 'messages' ),
'Updates provided by hooks follow the normal fallback order.'
);
}