wiki.techinc.nl/tests/phpunit/includes/language/LocalisationCacheTest.php

174 lines
4.8 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Tests\Unit\DummyServicesTrait;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
* @group Database
* @group Cache
* @covers LocalisationCache
* @author Niklas Laxström
*/
class LocalisationCacheTest extends MediaWikiIntegrationTestCase {
use DummyServicesTrait;
/**
Hooks::run() call site migration Migrate all callers of Hooks::run() to use the new HookContainer/HookRunner system. General principles: * Use DI if it is already used. We're not changing the way state is managed in this patch. * HookContainer is always injected, not HookRunner. HookContainer is a service, it's a more generic interface, it is the only thing that provides isRegistered() which is needed in some cases, and a HookRunner can be efficiently constructed from it (confirmed by benchmark). Because HookContainer is needed for object construction, it is also needed by all factories. * "Ask your friendly local base class". Big hierarchies like SpecialPage and ApiBase have getHookContainer() and getHookRunner() methods in the base class, and classes that extend that base class are not expected to know or care where the base class gets its HookContainer from. * ProtectedHookAccessorTrait provides protected getHookContainer() and getHookRunner() methods, getting them from the global service container. The point of this is to ease migration to DI by ensuring that call sites ask their local friendly base class rather than getting a HookRunner from the service container directly. * Private $this->hookRunner. In some smaller classes where accessor methods did not seem warranted, there is a private HookRunner property which is accessed directly. Very rarely (two cases), there is a protected property, for consistency with code that conventionally assumes protected=private, but in cases where the class might actually be overridden, a protected accessor is preferred over a protected property. * The last resort: Hooks::runner(). Mostly for static, file-scope and global code. In a few cases it was used for objects with broken construction schemes, out of horror or laziness. Constructors with new required arguments: * AuthManager * BadFileLookup * BlockManager * ClassicInterwikiLookup * ContentHandlerFactory * ContentSecurityPolicy * DefaultOptionsManager * DerivedPageDataUpdater * FullSearchResultWidget * HtmlCacheUpdater * LanguageFactory * LanguageNameUtils * LinkRenderer * LinkRendererFactory * LocalisationCache * MagicWordFactory * MessageCache * NamespaceInfo * PageEditStash * PageHandlerFactory * PageUpdater * ParserFactory * PermissionManager * RevisionStore * RevisionStoreFactory * SearchEngineConfig * SearchEngineFactory * SearchFormWidget * SearchNearMatcher * SessionBackend * SpecialPageFactory * UserNameUtils * UserOptionsManager * WatchedItemQueryService * WatchedItemStore Constructors with new optional arguments: * DefaultPreferencesFactory * Language * LinkHolderArray * MovePage * Parser * ParserCache * PasswordReset * Router setHookContainer() now required after construction: * AuthenticationProvider * ResourceLoaderModule * SearchEngine Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
* @param array $hooks Hook overrides
* @param array $options Service options (see {@link LocalisationCache::CONSTRUCTOR_OPTIONS})
* @return LocalisationCache
*/
protected function getMockLocalisationCache( $hooks = [], $options = [] ) {
global $IP;
Hooks::run() call site migration Migrate all callers of Hooks::run() to use the new HookContainer/HookRunner system. General principles: * Use DI if it is already used. We're not changing the way state is managed in this patch. * HookContainer is always injected, not HookRunner. HookContainer is a service, it's a more generic interface, it is the only thing that provides isRegistered() which is needed in some cases, and a HookRunner can be efficiently constructed from it (confirmed by benchmark). Because HookContainer is needed for object construction, it is also needed by all factories. * "Ask your friendly local base class". Big hierarchies like SpecialPage and ApiBase have getHookContainer() and getHookRunner() methods in the base class, and classes that extend that base class are not expected to know or care where the base class gets its HookContainer from. * ProtectedHookAccessorTrait provides protected getHookContainer() and getHookRunner() methods, getting them from the global service container. The point of this is to ease migration to DI by ensuring that call sites ask their local friendly base class rather than getting a HookRunner from the service container directly. * Private $this->hookRunner. In some smaller classes where accessor methods did not seem warranted, there is a private HookRunner property which is accessed directly. Very rarely (two cases), there is a protected property, for consistency with code that conventionally assumes protected=private, but in cases where the class might actually be overridden, a protected accessor is preferred over a protected property. * The last resort: Hooks::runner(). Mostly for static, file-scope and global code. In a few cases it was used for objects with broken construction schemes, out of horror or laziness. Constructors with new required arguments: * AuthManager * BadFileLookup * BlockManager * ClassicInterwikiLookup * ContentHandlerFactory * ContentSecurityPolicy * DefaultOptionsManager * DerivedPageDataUpdater * FullSearchResultWidget * HtmlCacheUpdater * LanguageFactory * LanguageNameUtils * LinkRenderer * LinkRendererFactory * LocalisationCache * MagicWordFactory * MessageCache * NamespaceInfo * PageEditStash * PageHandlerFactory * PageUpdater * ParserFactory * PermissionManager * RevisionStore * RevisionStoreFactory * SearchEngineConfig * SearchEngineFactory * SearchFormWidget * SearchNearMatcher * SessionBackend * SpecialPageFactory * UserNameUtils * UserOptionsManager * WatchedItemQueryService * WatchedItemStore Constructors with new optional arguments: * DefaultPreferencesFactory * Language * LinkHolderArray * MovePage * Parser * ParserCache * PasswordReset * Router setHookContainer() now required after construction: * AuthenticationProvider * ResourceLoaderModule * SearchEngine Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
$hookContainer = $this->createHookContainer( $hooks );
// in case any of the LanguageNameUtils hooks are being used
$langNameUtils = $this->getDummyLanguageNameUtils(
[ 'hookContainer' => $hookContainer ]
);
$options += [
'forceRecache' => false,
'manualRecache' => false,
'ExtensionMessagesFiles' => [],
'MessagesDirs' => [],
];
$lc = $this->getMockBuilder( LocalisationCache::class )
->setConstructorArgs( [
new ServiceOptions( LocalisationCache::CONSTRUCTOR_OPTIONS, $options ),
new LCStoreDB( [] ),
new NullLogger,
[],
$langNameUtils,
Hooks::run() call site migration Migrate all callers of Hooks::run() to use the new HookContainer/HookRunner system. General principles: * Use DI if it is already used. We're not changing the way state is managed in this patch. * HookContainer is always injected, not HookRunner. HookContainer is a service, it's a more generic interface, it is the only thing that provides isRegistered() which is needed in some cases, and a HookRunner can be efficiently constructed from it (confirmed by benchmark). Because HookContainer is needed for object construction, it is also needed by all factories. * "Ask your friendly local base class". Big hierarchies like SpecialPage and ApiBase have getHookContainer() and getHookRunner() methods in the base class, and classes that extend that base class are not expected to know or care where the base class gets its HookContainer from. * ProtectedHookAccessorTrait provides protected getHookContainer() and getHookRunner() methods, getting them from the global service container. The point of this is to ease migration to DI by ensuring that call sites ask their local friendly base class rather than getting a HookRunner from the service container directly. * Private $this->hookRunner. In some smaller classes where accessor methods did not seem warranted, there is a private HookRunner property which is accessed directly. Very rarely (two cases), there is a protected property, for consistency with code that conventionally assumes protected=private, but in cases where the class might actually be overridden, a protected accessor is preferred over a protected property. * The last resort: Hooks::runner(). Mostly for static, file-scope and global code. In a few cases it was used for objects with broken construction schemes, out of horror or laziness. Constructors with new required arguments: * AuthManager * BadFileLookup * BlockManager * ClassicInterwikiLookup * ContentHandlerFactory * ContentSecurityPolicy * DefaultOptionsManager * DerivedPageDataUpdater * FullSearchResultWidget * HtmlCacheUpdater * LanguageFactory * LanguageNameUtils * LinkRenderer * LinkRendererFactory * LocalisationCache * MagicWordFactory * MessageCache * NamespaceInfo * PageEditStash * PageHandlerFactory * PageUpdater * ParserFactory * PermissionManager * RevisionStore * RevisionStoreFactory * SearchEngineConfig * SearchEngineFactory * SearchFormWidget * SearchNearMatcher * SessionBackend * SpecialPageFactory * UserNameUtils * UserOptionsManager * WatchedItemQueryService * WatchedItemStore Constructors with new optional arguments: * DefaultPreferencesFactory * Language * LinkHolderArray * MovePage * Parser * ParserCache * PasswordReset * Router setHookContainer() now required after construction: * AuthenticationProvider * ResourceLoaderModule * SearchEngine Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
$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.
Hooks::run() call site migration Migrate all callers of Hooks::run() to use the new HookContainer/HookRunner system. General principles: * Use DI if it is already used. We're not changing the way state is managed in this patch. * HookContainer is always injected, not HookRunner. HookContainer is a service, it's a more generic interface, it is the only thing that provides isRegistered() which is needed in some cases, and a HookRunner can be efficiently constructed from it (confirmed by benchmark). Because HookContainer is needed for object construction, it is also needed by all factories. * "Ask your friendly local base class". Big hierarchies like SpecialPage and ApiBase have getHookContainer() and getHookRunner() methods in the base class, and classes that extend that base class are not expected to know or care where the base class gets its HookContainer from. * ProtectedHookAccessorTrait provides protected getHookContainer() and getHookRunner() methods, getting them from the global service container. The point of this is to ease migration to DI by ensuring that call sites ask their local friendly base class rather than getting a HookRunner from the service container directly. * Private $this->hookRunner. In some smaller classes where accessor methods did not seem warranted, there is a private HookRunner property which is accessed directly. Very rarely (two cases), there is a protected property, for consistency with code that conventionally assumes protected=private, but in cases where the class might actually be overridden, a protected accessor is preferred over a protected property. * The last resort: Hooks::runner(). Mostly for static, file-scope and global code. In a few cases it was used for objects with broken construction schemes, out of horror or laziness. Constructors with new required arguments: * AuthManager * BadFileLookup * BlockManager * ClassicInterwikiLookup * ContentHandlerFactory * ContentSecurityPolicy * DefaultOptionsManager * DerivedPageDataUpdater * FullSearchResultWidget * HtmlCacheUpdater * LanguageFactory * LanguageNameUtils * LinkRenderer * LinkRendererFactory * LocalisationCache * MagicWordFactory * MessageCache * NamespaceInfo * PageEditStash * PageHandlerFactory * PageUpdater * ParserFactory * PermissionManager * RevisionStore * RevisionStoreFactory * SearchEngineConfig * SearchEngineFactory * SearchFormWidget * SearchNearMatcher * SessionBackend * SpecialPageFactory * UserNameUtils * UserOptionsManager * WatchedItemQueryService * WatchedItemStore Constructors with new optional arguments: * DefaultPreferencesFactory * Language * LinkHolderArray * MovePage * Parser * ParserCache * PasswordReset * Router setHookContainer() now required after construction: * AuthenticationProvider * ResourceLoaderModule * SearchEngine Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
$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.'
);
}
public function testRecacheExtensionMessagesFiles(): void {
global $IP;
$lc = $this->getMockLocalisationCache( [], [
'ExtensionMessagesFiles' => [
__METHOD__ => "$IP/tests/phpunit/data/localisationcache/ExtensionMessagesFiles.php",
]
] );
$lc->recache( 'de' );
$specialPageAliases = $lc->getItem( 'de', 'specialPageAliases' );
$this->assertSame(
[ 'LokalisierungsPufferTest' ],
$specialPageAliases['LocalisationCacheTest'],
'specialPageAliases can be set in ExtensionMessagesFiles'
);
$this->assertSame(
[ 'Aktive_Benutzer*innen', 'Aktive_Benutzer', 'ActiveFolx', 'ActiveUsers' ],
$specialPageAliases['Activeusers'],
'specialPageAliases from extension/core files and fallback languages are merged'
);
LocalisationCache: Add CORE_ONLY_KEYS, ALL_EXCEPT_CORE_ONLY_KEYS Previously, it was technically possible to set some keys in extension messages files that didn’t make much sense (e.g. $rtl['en'] = true;). This prevents optimizing language creation, so going forward, we will no longer support that; ALL_KEYS is now split into CORE_ONLY_KEYS and ALL_EXCEPT_CORE_ONLY_KEYS (with a test verifying that no key is missing or overlapping or anything), and ALL_EXCEPT_CORE_ONLY_KEYS are silently skipped when loading extension messages files. To demonstrate that it’s okay to silently skip these keys, patch set 1 of this change on Gerrit instead raised a deprecation warning; CI indicated that this warning was never hit. Codesearch [1] also suggests that no known extension was actually using any of these keys. (The DonationInterface [2] and LandingCheck [3] codesearch results can be ignored: both of these define es-419 as a new language, using the Language::getMessagesFileName hook to register their MessagesEs_419.php as a “core” message file, not an extension message file.) [1]: https://codesearch.wmcloud.org/search/?q=^\%24(fallback|rtl|(digit|separator)TransformTable|fallback8bitEncoding|link(PrefixExtension|Trail|PrefixCharset)|date(Formats|Preferences|PreferenceMigrationMap)|defaultDateFormat|digitGroupingPattern).*%3D&files=\.php%24 [2]: https://gerrit.wikimedia.org/g/mediawiki/extensions/DonationInterface/+/f8b5fe95f77c8a9bc516bc52084d001477c70e2d/gateway_common/messages/MessagesEs_419.php#11 [3]: https://gerrit.wikimedia.org/g/mediawiki/extensions/LandingCheck/+/2537439aeebaf6ccc71b897fa683cdc4d06ae9d3/messages/MessagesEs_419.php#11 Bug: T342418 Change-Id: Ia3dffea390d4efdfa3a3cea549d079507718ef48
2023-07-21 09:31:43 +00:00
$this->assertFalse(
$lc->getItem( 'de', 'rtl' ),
'rtl cannot be set in ExtensionMessagesFiles'
);
}
public function testLoadCoreDataAvoidsInitLanguage(): void {
$lc = $this->getMockLocalisationCache();
$lc->getItem( 'de', 'fallback' );
$lc->getItem( 'de', 'rtl' );
$lc->getItem( 'de', 'fallbackSequence' );
$lc->getItem( 'de', 'originalFallbackSequence' );
$this->assertArrayNotHasKey( 'de',
TestingAccessWrapper::newFromObject( $lc )->initialisedLangs );
}
public function testShallowFallbackForInvalidCode(): void {
$lc = $this->getMockLocalisationCache();
$invalidCode = '!invalid!';
$this->assertSame( false, $lc->getItem( $invalidCode, 'rtl' ) );
$this->assertSame( 'windows-1252', $lc->getItem( $invalidCode, 'fallback8bitEncoding' ) );
}
}