2013-01-16 07:28:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-11 14:59:55 +00:00
|
|
|
use MediaWiki\CommentStore\CommentStoreComment;
|
2023-11-21 21:08:14 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2022-07-20 12:42:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2021-10-04 17:10:37 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2020-04-29 02:53:38 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2019-03-22 12:26:21 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2018-07-29 12:24:54 +00:00
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
/**
|
language: Widen `@covers` tags in phpunit tests
Add a few missing `@group Language` tags as well.
Remove stray `@group Cache` from two classes since "Cache" is not a
MediaWiki core component (per T248519 and related tasks, we did long
ago in Bugzilla have a "MediaWiki-Cache" category, but that's since
been re-orged into BagOStuff, HTTP-Cache, and Parser/ParserCache,
and Internationalization/LocalisationCache, per the description at
<https://phabricator.wikimedia.org/project/profile/1329/>.)
Ref https://gerrit.wikimedia.org/r/q/owner:Krinkle+is:merged+message:Widen
> Given all called methods are de-facto and liberally claimed, and
> that we keep the coverage limited to the subject class, it maintains
> the spirit and intent by listing the class explicitly instead.
>
> PHPUnit offers a more precise tool when you need it (i.e. when testing
> legacy monster/god classes), but for well-written code, the
> class-wide tag is exactly what you want.
>
> We lose useful coverage and waste valuable time on keeping tags
> accurate through refactors (or worse, forget to do so).
> Tracking tiny per-method details wastes time in realizing (and
> fixing) when people inevitably don't keep them in sync, and time
> lost in finding uncovered code to write tests to realize it was
> already covered but "not yet claimed".
Bug: T364652
Change-Id: I9cfc4c210b90bfed6fd988a2525f80f5f5ee4870
2024-06-14 19:51:04 +00:00
|
|
|
* @group Language
|
2013-01-16 07:28:54 +00:00
|
|
|
* @group Database
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MessageCache
|
2013-01-16 07:28:54 +00:00
|
|
|
*/
|
|
|
|
|
class MessageCacheTest extends MediaWikiLangTestCase {
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2013-01-16 07:28:54 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->configureLanguages();
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getMessageCache()->enable();
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function -- setup site language for testing
|
|
|
|
|
*/
|
|
|
|
|
protected function configureLanguages() {
|
|
|
|
|
// for the test, we need the content language to be anything but English,
|
|
|
|
|
// let's choose e.g. German (de)
|
2016-03-09 16:47:58 +00:00
|
|
|
$this->setUserLang( 'de' );
|
|
|
|
|
$this->setContentLang( 'de' );
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function addDBDataOnce() {
|
2013-01-16 07:28:54 +00:00
|
|
|
$this->configureLanguages();
|
|
|
|
|
|
|
|
|
|
// Set up messages and fallbacks ab -> ru -> de
|
|
|
|
|
$this->makePage( 'FallbackLanguageTest-Full', 'ab' );
|
|
|
|
|
$this->makePage( 'FallbackLanguageTest-Full', 'ru' );
|
|
|
|
|
$this->makePage( 'FallbackLanguageTest-Full', 'de' );
|
|
|
|
|
|
|
|
|
|
// Fallbacks where ab does not exist
|
|
|
|
|
$this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
|
|
|
|
|
$this->makePage( 'FallbackLanguageTest-Partial', 'de' );
|
|
|
|
|
|
|
|
|
|
// Fallback to the content language
|
|
|
|
|
$this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
|
|
|
|
|
|
|
|
|
|
// Full key tests -- always want russian
|
|
|
|
|
$this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
|
|
|
|
|
$this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
|
|
|
|
|
|
|
|
|
|
// In content language -- get base if no derivative
|
2015-05-20 02:34:20 +00:00
|
|
|
$this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function for addDBData -- adds a simple page to the database
|
|
|
|
|
*
|
|
|
|
|
* @param string $title Title of page to be created
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $lang Language and content of the created page
|
2013-01-16 07:28:54 +00:00
|
|
|
* @param string|null $content Content of the created page, or null for a generic string
|
2019-03-22 12:26:21 +00:00
|
|
|
*
|
2020-04-29 02:53:38 +00:00
|
|
|
* @return RevisionRecord
|
2013-01-16 07:28:54 +00:00
|
|
|
*/
|
2020-04-29 02:53:38 +00:00
|
|
|
private function makePage( $title, $lang, $content = null ) {
|
2024-01-09 08:19:00 +00:00
|
|
|
$content ??= $lang;
|
2022-01-12 20:13:39 +00:00
|
|
|
if ( $lang !== $this->getServiceContainer()->getContentLanguage()->getCode() ) {
|
2013-01-16 07:28:54 +00:00
|
|
|
$title = "$title/$lang";
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 19:53:11 +00:00
|
|
|
$title = Title::makeTitle( NS_MEDIAWIKI, $title );
|
2022-06-26 21:21:02 +00:00
|
|
|
$wikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
|
2020-04-29 02:53:38 +00:00
|
|
|
$content = ContentHandler::makeContent( $content, $title );
|
|
|
|
|
$summary = CommentStoreComment::newUnsavedComment( "$lang translation test case" );
|
2021-10-04 17:10:37 +00:00
|
|
|
|
|
|
|
|
$newRevision = $wikiPage->newPageUpdater( $this->getTestSysop()->getUser() )
|
|
|
|
|
->setContent( SlotRecord::MAIN, $content )
|
|
|
|
|
->saveRevision( $summary );
|
2019-03-22 12:26:21 +00:00
|
|
|
|
2021-10-04 17:10:37 +00:00
|
|
|
$this->assertNotNull( $newRevision, 'Create page ' . $title->getPrefixedDBkey() );
|
|
|
|
|
return $newRevision;
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-04 07:06:00 +00:00
|
|
|
* Test message fallbacks, T3495
|
2013-01-16 07:28:54 +00:00
|
|
|
*
|
|
|
|
|
* @dataProvider provideMessagesForFallback
|
|
|
|
|
*/
|
2022-09-16 12:27:03 +00:00
|
|
|
public function testMessageFallbacks( $message, $langCode, $expectedContent ) {
|
|
|
|
|
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( $langCode );
|
2022-01-12 20:13:39 +00:00
|
|
|
$result = $this->getServiceContainer()->getMessageCache()->get( $message, true, $lang );
|
2013-01-16 07:28:54 +00:00
|
|
|
$this->assertEquals( $expectedContent, $result, "Message fallback failed." );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideMessagesForFallback() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
|
|
|
|
|
[ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
|
|
|
|
|
[ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
|
|
|
|
|
[ 'FallbackLanguageTest-None', 'ab', false ],
|
2013-01-16 07:28:54 +00:00
|
|
|
|
2017-02-20 23:45:58 +00:00
|
|
|
// T48579
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
|
2013-01-16 07:28:54 +00:00
|
|
|
// UI language different from content language should only use de/none as last option
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
|
|
|
|
|
];
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-28 05:53:51 +00:00
|
|
|
public function testReplaceMsg() {
|
2022-01-12 20:13:39 +00:00
|
|
|
$messageCache = $this->getServiceContainer()->getMessageCache();
|
2016-10-28 05:53:51 +00:00
|
|
|
$message = 'go';
|
2022-01-12 20:13:39 +00:00
|
|
|
$uckey = $this->getServiceContainer()->getContentLanguage()->ucfirst( $message );
|
2016-10-28 05:53:51 +00:00
|
|
|
$oldText = $messageCache->get( $message ); // "Ausführen"
|
|
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$dbw = $this->getDb();
|
2016-10-28 05:53:51 +00:00
|
|
|
$dbw->startAtomic( __METHOD__ ); // simulate request and block deferred updates
|
|
|
|
|
$messageCache->replace( $uckey, 'Allez!' );
|
|
|
|
|
$this->assertEquals( 'Allez!',
|
|
|
|
|
$messageCache->getMsgFromNamespace( $uckey, 'de' ),
|
|
|
|
|
'Updates are reflected in-process immediately' );
|
|
|
|
|
$this->assertEquals( 'Allez!',
|
|
|
|
|
$messageCache->get( $message ),
|
|
|
|
|
'Updates are reflected in-process immediately' );
|
|
|
|
|
$this->makePage( 'Go', 'de', 'Race!' );
|
|
|
|
|
$dbw->endAtomic( __METHOD__ );
|
|
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0,
|
2016-10-28 05:53:51 +00:00
|
|
|
DeferredUpdates::pendingUpdatesCount(),
|
|
|
|
|
'Post-commit deferred update triggers a run of all updates' );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 'Race!', $messageCache->get( $message ), 'Correct final contents' );
|
|
|
|
|
|
|
|
|
|
$this->makePage( 'Go', 'de', $oldText );
|
|
|
|
|
$messageCache->replace( $uckey, $oldText ); // deferred update runs immediately
|
|
|
|
|
$this->assertEquals( $oldText, $messageCache->get( $message ), 'Content restored' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 16:56:58 +00:00
|
|
|
public function testReplaceCache() {
|
2022-07-20 12:42:41 +00:00
|
|
|
$this->overrideConfigValues( [
|
objectcache: Remove $wgMainWANCache and $wgWANObjectCaches
We always wrap the local cluster cache, and there are no subclasses
of WANObjectCache. It was never documented or recommended how these
would be used. It is a left-over from the original 2015 Multi-DC plan
in which WANObjectCache would work differently. See task for details.
Note that this requires no configuration changes, even in the
theoretical case of these variables being used, as the only
option is to use the main cache, and that's also the default.
* Update WAN overrides to override the underlying main cache
instead.
* Fix EditPageTest which was previously implicitly using a 'hash'
as main cache but also relying on wan cache to be 'none'.
The part that it actually needs is the 'none'. When WAN cache is
enabled, testUpdateNoMinor fails due to an edit conflict because
one of the edits it makes is made with a current timestamp whereas
it expects to simulate wpEdittime in the year 2012 which, when
caching is enabled, is ignored and becomes the current time instead.
I don't understand exactly why, but I'm going to conserve that
behaviour for now.
* Fix TemplateCategoriesTest, which was failing due to an unexpected
cache hit:
> [objectcache] fetchOrRegenerate(…:page:10:…): volatile hit
This could be solved in a more realistic way by splitting the test,
or by explicitly resetting services half-way the test to clear
WikiPageFactory, PageStore and WANCache process state.
For now, keep the prior behaviour of no cache in this test.
Bug: T305093
Bug: T329680
Depends-On: If890622eed0d0f8b4bd73d36ba1815a3d760ea05
Depends-On: Ie1def75208822bdf19bb2cfd7e6edf32c2000e6b
Depends-On: I35cce61dc3ee90dcee3dd6f0b36f84133be029ed
Change-Id: I53781a8c06ebb2583f6ca83dd91bbfe8a5c88b13
2023-02-14 21:43:12 +00:00
|
|
|
MainConfigNames::MainCacheType => CACHE_HASH,
|
2018-09-26 16:56:58 +00:00
|
|
|
] );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$messageCache = $this->getServiceContainer()->getMessageCache();
|
2018-09-26 16:56:58 +00:00
|
|
|
$messageCache->enable();
|
|
|
|
|
|
|
|
|
|
// Populate one key
|
|
|
|
|
$this->makePage( 'Key1', 'de', 'Value1' );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0,
|
2018-09-26 16:56:58 +00:00
|
|
|
DeferredUpdates::pendingUpdatesCount(),
|
|
|
|
|
'Post-commit deferred update triggers a run of all updates' );
|
|
|
|
|
$this->assertEquals( 'Value1', $messageCache->get( 'Key1' ), 'Key1 was successfully edited' );
|
|
|
|
|
|
|
|
|
|
// Screw up the database so MessageCache::loadFromDB() will
|
|
|
|
|
// produce the wrong result for reloading Key1
|
2024-06-13 16:53:25 +00:00
|
|
|
$this->getDb()->newDeleteQueryBuilder()
|
2024-04-12 18:12:05 +00:00
|
|
|
->deleteFrom( 'page' )
|
|
|
|
|
->where( [ 'page_namespace' => NS_MEDIAWIKI, 'page_title' => 'Key1' ] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
2018-09-26 16:56:58 +00:00
|
|
|
|
|
|
|
|
// Populate the second key
|
|
|
|
|
$this->makePage( 'Key2', 'de', 'Value2' );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0,
|
2018-09-26 16:56:58 +00:00
|
|
|
DeferredUpdates::pendingUpdatesCount(),
|
|
|
|
|
'Post-commit deferred update triggers a run of all updates' );
|
|
|
|
|
$this->assertEquals( 'Value2', $messageCache->get( 'Key2' ), 'Key2 was successfully edited' );
|
|
|
|
|
|
|
|
|
|
// Now test that the second edit didn't reload Key1
|
|
|
|
|
$this->assertEquals( 'Value1', $messageCache->get( 'Key1' ),
|
|
|
|
|
'Key1 wasn\'t reloaded by edit of Key2' );
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 09:02:57 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNormalizeKey
|
|
|
|
|
*/
|
|
|
|
|
public function testNormalizeKey( $key, $expected ) {
|
|
|
|
|
$actual = MessageCache::normalizeKey( $key );
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideNormalizeKey() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'Foo', 'foo' ],
|
|
|
|
|
[ 'foo', 'foo' ],
|
|
|
|
|
[ 'fOo', 'fOo' ],
|
|
|
|
|
[ 'FOO', 'fOO' ],
|
|
|
|
|
[ 'Foo bar', 'foo_bar' ],
|
|
|
|
|
[ 'Ćab', 'ćab' ],
|
|
|
|
|
[ 'Ćab_e 3', 'ćab_e_3' ],
|
|
|
|
|
[ 'ĆAB', 'ćAB' ],
|
|
|
|
|
[ 'ćab', 'ćab' ],
|
|
|
|
|
[ 'ćaB', 'ćaB' ],
|
|
|
|
|
];
|
2015-07-19 09:02:57 +00:00
|
|
|
}
|
2018-10-15 15:08:24 +00:00
|
|
|
|
2019-07-19 23:51:01 +00:00
|
|
|
public function testNoDBAccessContentLanguage() {
|
2024-05-24 10:21:13 +00:00
|
|
|
$languageCode = $this->getServiceContainer()->getMainConfig()->get( MainConfigNames::LanguageCode );
|
2018-10-15 15:08:24 +00:00
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$dbr = $this->getDb();
|
2018-10-15 15:08:24 +00:00
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$messageCache = $this->getServiceContainer()->getMessageCache();
|
2024-05-24 10:21:13 +00:00
|
|
|
$messageCache->getMsgFromNamespace( 'allpages', $languageCode );
|
2018-10-15 15:08:24 +00:00
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $dbr->trxLevel() );
|
2018-10-15 15:08:24 +00:00
|
|
|
$dbr->setFlag( DBO_TRX, $dbr::REMEMBER_PRIOR ); // make queries trigger TRX
|
|
|
|
|
|
2024-05-24 10:21:13 +00:00
|
|
|
$messageCache->getMsgFromNamespace( 'go', $languageCode );
|
2018-10-15 15:08:24 +00:00
|
|
|
|
|
|
|
|
$dbr->restoreFlags();
|
|
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $dbr->trxLevel(), "No DB read queries (content language)" );
|
2019-07-19 23:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNoDBAccessNonContentLanguage() {
|
2023-09-25 18:49:16 +00:00
|
|
|
$dbr = $this->getDb();
|
2019-07-19 23:51:01 +00:00
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$messageCache = $this->getServiceContainer()->getMessageCache();
|
2020-03-14 12:58:53 +00:00
|
|
|
$messageCache->getMsgFromNamespace( 'allpages/nl', 'nl' );
|
2019-07-19 23:51:01 +00:00
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $dbr->trxLevel() );
|
2019-07-19 23:51:01 +00:00
|
|
|
$dbr->setFlag( DBO_TRX, $dbr::REMEMBER_PRIOR ); // make queries trigger TRX
|
|
|
|
|
|
2020-03-14 12:58:53 +00:00
|
|
|
$messageCache->getMsgFromNamespace( 'go/nl', 'nl' );
|
2019-07-19 23:51:01 +00:00
|
|
|
|
|
|
|
|
$dbr->restoreFlags();
|
|
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $dbr->trxLevel(), "No DB read queries (non-content language)" );
|
2018-10-15 15:08:24 +00:00
|
|
|
}
|
2019-03-22 12:26:21 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regression test for T218918
|
|
|
|
|
*/
|
|
|
|
|
public function testLoadFromDB_fetchLatestRevision() {
|
|
|
|
|
// Create three revisions of the same message page.
|
|
|
|
|
// Must be an existing message key.
|
|
|
|
|
$key = 'Log';
|
|
|
|
|
$this->makePage( $key, 'de', 'Test eins' );
|
|
|
|
|
$this->makePage( $key, 'de', 'Test zwei' );
|
|
|
|
|
$r3 = $this->makePage( $key, 'de', 'Test drei' );
|
|
|
|
|
|
|
|
|
|
// Create an out-of-sequence revision by importing a
|
|
|
|
|
// revision with an old timestamp. Hacky.
|
2022-10-30 10:08:25 +00:00
|
|
|
$importRevision = new WikiRevision();
|
2020-04-06 09:16:17 +00:00
|
|
|
$title = Title::newFromLinkTarget( $r3->getPageAsLinkTarget() );
|
|
|
|
|
$importRevision->setTitle( $title );
|
2019-03-22 12:26:21 +00:00
|
|
|
$importRevision->setComment( 'Imported edit' );
|
2019-03-25 10:47:53 +00:00
|
|
|
$importRevision->setTimestamp( '19991122001122' );
|
2020-04-06 09:16:17 +00:00
|
|
|
$content = ContentHandler::makeContent( 'IMPORTED OLD TEST', $title );
|
|
|
|
|
$importRevision->setContent( SlotRecord::MAIN, $content );
|
2019-04-03 17:11:46 +00:00
|
|
|
$importRevision->setUsername( 'ext>Alan Smithee' );
|
2019-03-22 12:26:21 +00:00
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$importer = $this->getServiceContainer()->getWikiRevisionOldRevisionImporterNoUpdates();
|
2019-03-22 12:26:21 +00:00
|
|
|
$importer->import( $importRevision );
|
|
|
|
|
|
|
|
|
|
// Now, load the message from the wiki page
|
2022-01-12 20:13:39 +00:00
|
|
|
$messageCache = $this->getServiceContainer()->getMessageCache();
|
2019-03-22 12:26:21 +00:00
|
|
|
$messageCache->enable();
|
|
|
|
|
$messageCache = TestingAccessWrapper::newFromObject( $messageCache );
|
|
|
|
|
|
|
|
|
|
$cache = $messageCache->loadFromDB( 'de' );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( $key, $cache );
|
|
|
|
|
|
|
|
|
|
// Text in the cache has an extra space in front!
|
|
|
|
|
$this->assertSame( ' ' . 'Test drei', $cache[$key] );
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 06:05:11 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsMainCacheable
|
|
|
|
|
* @param string|null $code The language code
|
|
|
|
|
* @param string $message The message key
|
|
|
|
|
* @param bool $expected
|
|
|
|
|
*/
|
|
|
|
|
public function testIsMainCacheable( $code, $message, $expected ) {
|
|
|
|
|
$messageCache = TestingAccessWrapper::newFromObject(
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getMessageCache() );
|
2021-05-11 06:05:11 +00:00
|
|
|
$this->assertSame( $expected, $messageCache->isMainCacheable( $message, $code ) );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideIsMainCacheable() {
|
2021-05-11 06:05:11 +00:00
|
|
|
$cases = [
|
2023-10-24 22:49:24 +00:00
|
|
|
[ 'allpages', true ],
|
|
|
|
|
[ 'Allpages', true ],
|
|
|
|
|
[ 'Allpages/bat', true ],
|
2021-05-11 06:05:11 +00:00
|
|
|
[ 'Conversiontable/zh-tw', true ],
|
2023-10-24 22:49:24 +00:00
|
|
|
[ 'My_special_message', false ],
|
2021-05-11 06:05:11 +00:00
|
|
|
];
|
|
|
|
|
foreach ( [ null, 'en', 'fr' ] as $code ) {
|
|
|
|
|
foreach ( $cases as $case ) {
|
|
|
|
|
yield array_merge( [ $code ], $case );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 17:00:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideLocalOverride
|
|
|
|
|
* @param string $messageKey
|
|
|
|
|
*/
|
|
|
|
|
public function testLocalOverride( $messageKey ) {
|
|
|
|
|
$messageCache = $this->getServiceContainer()->getMessageCache();
|
2022-09-16 12:27:03 +00:00
|
|
|
$languageFactory = $this->getServiceContainer()->getLanguageFactory();
|
|
|
|
|
$languageZh = $languageFactory->getLanguage( 'zh' );
|
|
|
|
|
$languageZh_tw = $languageFactory->getLanguage( 'zh-tw' );
|
|
|
|
|
$languageZh_hk = $languageFactory->getLanguage( 'zh-hk' );
|
|
|
|
|
$languageZh_mo = $languageFactory->getLanguage( 'zh-mo' );
|
|
|
|
|
$oldMessageZh = $messageCache->get( $messageKey, true, $languageZh );
|
|
|
|
|
$oldMessageZh_tw = $messageCache->get( $messageKey, true, $languageZh_tw );
|
2023-05-30 17:00:58 +00:00
|
|
|
|
|
|
|
|
$localOverrideHK = $messageKey . '_zh-hk';
|
|
|
|
|
$this->makePage( ucfirst( $messageKey ), 'zh-hk', $localOverrideHK );
|
2022-09-16 12:27:03 +00:00
|
|
|
$this->assertEquals( $oldMessageZh, $messageCache->get( $messageKey, true, $languageZh ), 'Local override overlapped (main code)' );
|
|
|
|
|
$this->assertEquals( $oldMessageZh_tw, $messageCache->get( $messageKey, true, $languageZh_tw ), 'Local override overlapped' );
|
|
|
|
|
$this->assertEquals( $localOverrideHK, $messageCache->get( $messageKey, true, $languageZh_hk ), 'Local override failed (self)' );
|
|
|
|
|
$this->assertEquals( $localOverrideHK, $messageCache->get( $messageKey, true, $languageZh_mo ), 'Local override failed (fallback)' );
|
2023-05-30 17:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideLocalOverride() {
|
|
|
|
|
return [
|
|
|
|
|
// Preloaded with preloadedMessages
|
|
|
|
|
[ 'nstab-main' ],
|
|
|
|
|
// Not preloaded
|
|
|
|
|
[ 'nstab-help' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
2023-07-12 15:44:30 +00:00
|
|
|
|
|
|
|
|
/** @dataProvider provideXssLanguage */
|
|
|
|
|
public function testXssLanguage( array $config, bool $expectXssMessage ): void {
|
|
|
|
|
$this->overrideConfigValues( $config + [
|
|
|
|
|
MainConfigNames::UseXssLanguage => false,
|
|
|
|
|
MainConfigNames::RawHtmlMessages => [],
|
|
|
|
|
] );
|
|
|
|
|
|
2022-09-16 12:27:03 +00:00
|
|
|
$xss = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'x-xss' );
|
2023-07-12 15:44:30 +00:00
|
|
|
$message = $this->getServiceContainer()->getMessageCache()
|
2022-09-16 12:27:03 +00:00
|
|
|
->get( 'key', true, $xss );
|
2023-07-12 15:44:30 +00:00
|
|
|
if ( $expectXssMessage ) {
|
|
|
|
|
$this->assertSame(
|
2023-09-22 05:49:04 +00:00
|
|
|
"<script>alert('key')</script>\"><script>alert('key')</script><x y=\"(\$*)",
|
2023-07-12 15:44:30 +00:00
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertFalse( $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideXssLanguage(): iterable {
|
|
|
|
|
yield 'default' => [
|
|
|
|
|
'config' => [],
|
|
|
|
|
'expectXssMessage' => false,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'enabled' => [
|
|
|
|
|
'config' => [
|
|
|
|
|
MainConfigNames::UseXssLanguage => true,
|
|
|
|
|
],
|
|
|
|
|
'expectXssMessage' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'enabled but message marked as raw' => [
|
|
|
|
|
'config' => [
|
|
|
|
|
MainConfigNames::UseXssLanguage => true,
|
|
|
|
|
MainConfigNames::RawHtmlMessages => [ 'key' ],
|
|
|
|
|
],
|
|
|
|
|
'expectXssMessage' => false,
|
|
|
|
|
];
|
|
|
|
|
}
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|