3 use MediaWiki\MediaWikiServices
;
10 class MessageCacheTest
extends MediaWikiLangTestCase
{
12 protected function setUp() {
14 $this->configureLanguages();
15 MessageCache
::destroyInstance();
16 MessageCache
::singleton()->enable();
20 * Helper function -- setup site language for testing
22 protected function configureLanguages() {
23 // for the test, we need the content language to be anything but English,
24 // let's choose e.g. German (de)
25 $this->setUserLang( 'de' );
26 $this->setContentLang( 'de' );
29 function addDBDataOnce() {
30 $this->configureLanguages();
32 // Set up messages and fallbacks ab -> ru -> de
33 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
34 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
35 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
37 // Fallbacks where ab does not exist
38 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
39 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
41 // Fallback to the content language
42 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
44 // Add customizations for an existing message.
45 $this->makePage( 'sunday', 'ru' );
47 // Full key tests -- always want russian
48 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
49 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
51 // In content language -- get base if no derivative
52 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
56 * Helper function for addDBData -- adds a simple page to the database
58 * @param string $title Title of page to be created
59 * @param string $lang Language and content of the created page
60 * @param string|null $content Content of the created page, or null for a generic string
62 protected function makePage( $title, $lang, $content = null ) {
63 if ( $content === null ) {
66 if ( $lang !== MediaWikiServices
::getInstance()->getContentLanguage()->getCode() ) {
67 $title = "$title/$lang";
70 $title = Title
::newFromText( $title, NS_MEDIAWIKI
);
71 $wikiPage = new WikiPage( $title );
72 $contentHandler = ContentHandler
::makeContent( $content, $title );
73 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
77 * Test message fallbacks, T3495
79 * @dataProvider provideMessagesForFallback
81 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
82 $result = MessageCache
::singleton()->get( $message, true, $lang );
83 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
86 function provideMessagesForFallback() {
88 [ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
89 [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
90 [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
91 [ 'FallbackLanguageTest-None', 'ab', false ],
93 // Existing message with customizations on the fallbacks
94 [ 'sunday', 'ab', 'амҽыш' ],
97 [ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
98 // UI language different from content language should only use de/none as last option
99 [ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
103 public function testReplaceMsg() {
104 $messageCache = MessageCache
::singleton();
106 $uckey = MediaWikiServices
::getInstance()->getContentLanguage()->ucfirst( $message );
107 $oldText = $messageCache->get( $message ); // "Ausführen"
109 $dbw = wfGetDB( DB_MASTER
);
110 $dbw->startAtomic( __METHOD__
); // simulate request and block deferred updates
111 $messageCache->replace( $uckey, 'Allez!' );
112 $this->assertEquals( 'Allez!',
113 $messageCache->getMsgFromNamespace( $uckey, 'de' ),
114 'Updates are reflected in-process immediately' );
115 $this->assertEquals( 'Allez!',
116 $messageCache->get( $message ),
117 'Updates are reflected in-process immediately' );
118 $this->makePage( 'Go', 'de', 'Race!' );
119 $dbw->endAtomic( __METHOD__
);
121 $this->assertEquals( 0,
122 DeferredUpdates
::pendingUpdatesCount(),
123 'Post-commit deferred update triggers a run of all updates' );
125 $this->assertEquals( 'Race!', $messageCache->get( $message ), 'Correct final contents' );
127 $this->makePage( 'Go', 'de', $oldText );
128 $messageCache->replace( $uckey, $oldText ); // deferred update runs immediately
129 $this->assertEquals( $oldText, $messageCache->get( $message ), 'Content restored' );
132 public function testReplaceCache() {
133 global $wgWANObjectCaches;
135 // We need a WAN cache for this.
136 $this->setMwGlobals( [
137 'wgMainWANCache' => 'hash',
138 'wgWANObjectCaches' => $wgWANObjectCaches +
[
140 'class' => WANObjectCache
::class,
146 $this->overrideMwServices();
148 MessageCache
::destroyInstance();
149 $messageCache = MessageCache
::singleton();
150 $messageCache->enable();
153 $this->makePage( 'Key1', 'de', 'Value1' );
154 $this->assertEquals( 0,
155 DeferredUpdates
::pendingUpdatesCount(),
156 'Post-commit deferred update triggers a run of all updates' );
157 $this->assertEquals( 'Value1', $messageCache->get( 'Key1' ), 'Key1 was successfully edited' );
159 // Screw up the database so MessageCache::loadFromDB() will
160 // produce the wrong result for reloading Key1
162 'page', [ 'page_namespace' => NS_MEDIAWIKI
, 'page_title' => 'Key1' ], __METHOD__
165 // Populate the second key
166 $this->makePage( 'Key2', 'de', 'Value2' );
167 $this->assertEquals( 0,
168 DeferredUpdates
::pendingUpdatesCount(),
169 'Post-commit deferred update triggers a run of all updates' );
170 $this->assertEquals( 'Value2', $messageCache->get( 'Key2' ), 'Key2 was successfully edited' );
172 // Now test that the second edit didn't reload Key1
173 $this->assertEquals( 'Value1', $messageCache->get( 'Key1' ),
174 'Key1 wasn\'t reloaded by edit of Key2' );
178 * @dataProvider provideNormalizeKey
180 public function testNormalizeKey( $key, $expected ) {
181 $actual = MessageCache
::normalizeKey( $key );
182 $this->assertEquals( $expected, $actual );
185 public function provideNormalizeKey() {
191 [ 'Foo bar', 'foo_bar' ],
193 [ 'Ćab_e 3', 'ćab_e_3' ],
200 public function testNoDBAccess() {
201 global $wgContLanguageCode;
203 $dbr = wfGetDB( DB_REPLICA
);
205 MessageCache
::singleton()->getMsgFromNamespace( 'allpages', $wgContLanguageCode );
207 $this->assertEquals( 0, $dbr->trxLevel() );
208 $dbr->setFlag( DBO_TRX
, $dbr::REMEMBER_PRIOR
); // make queries trigger TRX
210 MessageCache
::singleton()->getMsgFromNamespace( 'go', $wgContLanguageCode );
212 $dbr->restoreFlags();
214 $this->assertEquals( 0, $dbr->trxLevel(), "No DB read queries" );