MediaWiki REL1_32
MessageCacheTest.php
Go to the documentation of this file.
1<?php
2
4
11
12 protected function setUp() {
13 parent::setUp();
14 $this->configureLanguages();
15 MessageCache::destroyInstance();
16 MessageCache::singleton()->enable();
17 }
18
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' );
27 }
28
29 function addDBDataOnce() {
30 $this->configureLanguages();
31
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' );
36
37 // Fallbacks where ab does not exist
38 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
39 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
40
41 // Fallback to the content language
42 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
43
44 // Add customizations for an existing message.
45 $this->makePage( 'sunday', 'ru' );
46
47 // Full key tests -- always want russian
48 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
49 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
50
51 // In content language -- get base if no derivative
52 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
53 }
54
62 protected function makePage( $title, $lang, $content = null ) {
63 if ( $content === null ) {
65 }
66 if ( $lang !== MediaWikiServices::getInstance()->getContentLanguage()->getCode() ) {
67 $title = "$title/$lang";
68 }
69
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" );
74 }
75
81 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
82 $result = MessageCache::singleton()->get( $message, true, $lang );
83 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
84 }
85
87 return [
88 [ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
89 [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
90 [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
91 [ 'FallbackLanguageTest-None', 'ab', false ],
92
93 // Existing message with customizations on the fallbacks
94 [ 'sunday', 'ab', 'амҽыш' ],
95
96 // T48579
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' ],
100 ];
101 }
102
103 public function testReplaceMsg() {
104 $messageCache = MessageCache::singleton();
105 $message = 'go';
106 $uckey = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $message );
107 $oldText = $messageCache->get( $message ); // "Ausführen"
108
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__ );
120
121 $this->assertEquals( 0,
122 DeferredUpdates::pendingUpdatesCount(),
123 'Post-commit deferred update triggers a run of all updates' );
124
125 $this->assertEquals( 'Race!', $messageCache->get( $message ), 'Correct final contents' );
126
127 $this->makePage( 'Go', 'de', $oldText );
128 $messageCache->replace( $uckey, $oldText ); // deferred update runs immediately
129 $this->assertEquals( $oldText, $messageCache->get( $message ), 'Content restored' );
130 }
131
132 public function testReplaceCache() {
133 global $wgWANObjectCaches;
134
135 // We need a WAN cache for this.
136 $this->setMwGlobals( [
137 'wgMainWANCache' => 'hash',
138 'wgWANObjectCaches' => $wgWANObjectCaches + [
139 'hash' => [
140 'class' => WANObjectCache::class,
141 'cacheId' => 'hash',
142 'channels' => []
143 ]
144 ]
145 ] );
146 $this->overrideMwServices();
147
148 MessageCache::destroyInstance();
149 $messageCache = MessageCache::singleton();
150 $messageCache->enable();
151
152 // Populate one key
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' );
158
159 // Screw up the database so MessageCache::loadFromDB() will
160 // produce the wrong result for reloading Key1
161 $this->db->delete(
162 'page', [ 'page_namespace' => NS_MEDIAWIKI, 'page_title' => 'Key1' ], __METHOD__
163 );
164
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' );
171
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' );
175 }
176
180 public function testNormalizeKey( $key, $expected ) {
181 $actual = MessageCache::normalizeKey( $key );
182 $this->assertEquals( $expected, $actual );
183 }
184
185 public function provideNormalizeKey() {
186 return [
187 [ 'Foo', 'foo' ],
188 [ 'foo', 'foo' ],
189 [ 'fOo', 'fOo' ],
190 [ 'FOO', 'fOO' ],
191 [ 'Foo bar', 'foo_bar' ],
192 [ 'Ćab', 'ćab' ],
193 [ 'Ćab_e 3', 'ćab_e_3' ],
194 [ 'ĆAB', 'ćAB' ],
195 [ 'ćab', 'ćab' ],
196 [ 'ćaB', 'ćaB' ],
197 ];
198 }
199
200 public function testNoDBAccess() {
201 global $wgContLanguageCode;
202
204
205 MessageCache::singleton()->getMsgFromNamespace( 'allpages', $wgContLanguageCode );
206
207 $this->assertEquals( 0, $dbr->trxLevel() );
208 $dbr->setFlag( DBO_TRX, $dbr::REMEMBER_PRIOR ); // make queries trigger TRX
209
210 MessageCache::singleton()->getMsgFromNamespace( 'go', $wgContLanguageCode );
211
212 $dbr->restoreFlags();
213
214 $this->assertEquals( 0, $dbr->trxLevel(), "No DB read queries" );
215 }
216}
$wgWANObjectCaches
Advanced WAN object cache configuration.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
foreach(LanguageCode::getNonstandardLanguageCodeMapping() as $code=> $bcp47) $wgContLanguageCode
Definition Setup.php:522
Base class that store and restore the Language objects.
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Database Cache MessageCache.
testMessageFallbacks( $message, $lang, $expectedContent)
Test message fallbacks, T3495.
testNormalizeKey( $key, $expected)
provideNormalizeKey
makePage( $title, $lang, $content=null)
Helper function for addDBData – adds a simple page to the database.
configureLanguages()
Helper function – setup site language for testing.
Class representing a MediaWiki article and history.
Definition WikiPage.php:44
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
$content
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26
const DBO_TRX
Definition defines.php:12
if(!isset( $args[0])) $lang