wiki.techinc.nl/tests/phpunit/includes/cache/MessageCacheTest.php
Brad Jorsch 08e9eb1fef MessageCache: replace should actually replace, not reload
Prior to I462554b30, MessageCache::replace() did just that: it took the
existing cache and updated the one entry.

In I462554b30, the rearrangement of work into a DeferredUpdate
introduced a bug: the in-process cache was updated, but when the shared
cache was loaded later the entry was never updated in there so the
shared caches kept the old value. This was found in code review and
worked around by reloading all the messages from the database instead of
updating the existing cache.

But all that extra work reloading everything from the database causes
major slowness saving any MediaWiki-namespace page when the wiki has
many such small pages. Let's go back and fix the bug so replace() can
again replace instead of reloading everything.

Bug: T203925
Change-Id: Ife8e1bd6f143f480eb8da09b817c85aadf33a923
2018-10-11 11:20:51 -04:00

199 lines
6.2 KiB
PHP

<?php
use MediaWiki\MediaWikiServices;
/**
* @group Database
* @group Cache
* @covers MessageCache
*/
class MessageCacheTest extends MediaWikiLangTestCase {
protected function setUp() {
parent::setUp();
$this->configureLanguages();
MessageCache::destroyInstance();
MessageCache::singleton()->enable();
}
/**
* 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)
$this->setUserLang( 'de' );
$this->setContentLang( 'de' );
}
function addDBDataOnce() {
$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' );
// Add customizations for an existing message.
$this->makePage( 'sunday', 'ru' );
// Full key tests -- always want russian
$this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
$this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
// In content language -- get base if no derivative
$this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
}
/**
* Helper function for addDBData -- adds a simple page to the database
*
* @param string $title Title of page to be created
* @param string $lang Language and content of the created page
* @param string|null $content Content of the created page, or null for a generic string
*/
protected function makePage( $title, $lang, $content = null ) {
if ( $content === null ) {
$content = $lang;
}
if ( $lang !== MediaWikiServices::getInstance()->getContentLanguage()->getCode() ) {
$title = "$title/$lang";
}
$title = Title::newFromText( $title, NS_MEDIAWIKI );
$wikiPage = new WikiPage( $title );
$contentHandler = ContentHandler::makeContent( $content, $title );
$wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
}
/**
* Test message fallbacks, T3495
*
* @dataProvider provideMessagesForFallback
*/
public function testMessageFallbacks( $message, $lang, $expectedContent ) {
$result = MessageCache::singleton()->get( $message, true, $lang );
$this->assertEquals( $expectedContent, $result, "Message fallback failed." );
}
function provideMessagesForFallback() {
return [
[ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
[ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
[ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
[ 'FallbackLanguageTest-None', 'ab', false ],
// Existing message with customizations on the fallbacks
[ 'sunday', 'ab', 'амҽыш' ],
// T48579
[ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
// UI language different from content language should only use de/none as last option
[ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
];
}
public function testReplaceMsg() {
$messageCache = MessageCache::singleton();
$message = 'go';
$uckey = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $message );
$oldText = $messageCache->get( $message ); // "Ausführen"
$dbw = wfGetDB( DB_MASTER );
$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__ );
$this->assertEquals( 0,
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' );
}
public function testReplaceCache() {
global $wgWANObjectCaches;
// We need a WAN cache for this.
$this->setMwGlobals( [
'wgMainWANCache' => 'hash',
'wgWANObjectCaches' => $wgWANObjectCaches + [
'hash' => [
'class' => WANObjectCache::class,
'cacheId' => 'hash',
'channels' => []
]
]
] );
$this->overrideMwServices();
MessageCache::destroyInstance();
$messageCache = MessageCache::singleton();
$messageCache->enable();
// Populate one key
$this->makePage( 'Key1', 'de', 'Value1' );
$this->assertEquals( 0,
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
$this->db->delete(
'page', [ 'page_namespace' => NS_MEDIAWIKI, 'page_title' => 'Key1' ], __METHOD__
);
// Populate the second key
$this->makePage( 'Key2', 'de', 'Value2' );
$this->assertEquals( 0,
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' );
}
/**
* @dataProvider provideNormalizeKey
*/
public function testNormalizeKey( $key, $expected ) {
$actual = MessageCache::normalizeKey( $key );
$this->assertEquals( $expected, $actual );
}
public function provideNormalizeKey() {
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' ],
];
}
}