<?php
use MediaWiki\MediaWikiServices;
+use Wikimedia\TestingAccessWrapper;
/**
* @group 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
+ *
+ * @return Revision
*/
protected function makePage( $title, $lang, $content = null ) {
if ( $content === null ) {
$title = Title::newFromText( $title, NS_MEDIAWIKI );
$wikiPage = new WikiPage( $title );
$contentHandler = ContentHandler::makeContent( $content, $title );
- $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
+ $status = $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
+
+ // sanity
+ $this->assertTrue( $status->isOK(), 'Create page ' . $title->getPrefixedDBkey() );
+ return $status->value['revision'];
}
/**
$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
*/
[ 'ćaB', 'ćaB' ],
];
}
+
+ public function testNoDBAccess() {
+ global $wgContLanguageCode;
+
+ $dbr = wfGetDB( DB_REPLICA );
+
+ MessageCache::singleton()->getMsgFromNamespace( 'allpages', $wgContLanguageCode );
+
+ $this->assertEquals( 0, $dbr->trxLevel() );
+ $dbr->setFlag( DBO_TRX, $dbr::REMEMBER_PRIOR ); // make queries trigger TRX
+
+ MessageCache::singleton()->getMsgFromNamespace( 'go', $wgContLanguageCode );
+
+ $dbr->restoreFlags();
+
+ $this->assertEquals( 0, $dbr->trxLevel(), "No DB read queries" );
+ }
+
+ /**
+ * 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.
+ $importRevision = new WikiRevision( new HashConfig() );
+ $importRevision->setTitle( $r3->getTitle() );
+ $importRevision->setComment( 'Imported edit' );
+ $importRevision->setTimestamp( '19991122334455' );
+ $importRevision->setText( 'IMPORTED OLD TEST' );
+ $importRevision->setUsername( 'Alan Smithee' );
+
+ $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
+ $importer->import( $importRevision );
+
+ // Now, load the message from the wiki page
+ MessageCache::destroyInstance();
+ $messageCache = MessageCache::singleton();
+ $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] );
+ }
+
}