661f325149db7ca8624dffa3f7b41e1508b60ed8
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / MessageCacheTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @group Database
7 * @group Cache
8 * @covers MessageCache
9 */
10 class MessageCacheTest extends MediaWikiLangTestCase {
11
12 protected function setUp() {
13 parent::setUp();
14 $this->configureLanguages();
15 MessageCache::destroyInstance();
16 MessageCache::singleton()->enable();
17 }
18
19 /**
20 * Helper function -- setup site language for testing
21 */
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
55 /**
56 * Helper function for addDBData -- adds a simple page to the database
57 *
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
61 */
62 protected function makePage( $title, $lang, $content = null ) {
63 if ( $content === null ) {
64 $content = $lang;
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
76 /**
77 * Test message fallbacks, T3495
78 *
79 * @dataProvider provideMessagesForFallback
80 */
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
86 function provideMessagesForFallback() {
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
177 /**
178 * @dataProvider provideNormalizeKey
179 */
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 }