Mass conversion of $wgContLang to service
[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, bug #1495
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 /**
133 * There's a fallback case where the message key is given as fully qualified -- this
134 * should ignore the passed $lang and use the language from the key
135 *
136 * @dataProvider provideMessagesForFullKeys
137 */
138 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
139 $result = MessageCache::singleton()->get( $message, true, $lang, true );
140 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
141 }
142
143 function provideMessagesForFullKeys() {
144 return [
145 [ 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ],
146 [ 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ],
147 [ 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ],
148 ];
149 }
150
151 /**
152 * @dataProvider provideNormalizeKey
153 */
154 public function testNormalizeKey( $key, $expected ) {
155 $actual = MessageCache::normalizeKey( $key );
156 $this->assertEquals( $expected, $actual );
157 }
158
159 public function provideNormalizeKey() {
160 return [
161 [ 'Foo', 'foo' ],
162 [ 'foo', 'foo' ],
163 [ 'fOo', 'fOo' ],
164 [ 'FOO', 'fOO' ],
165 [ 'Foo bar', 'foo_bar' ],
166 [ 'Ćab', 'ćab' ],
167 [ 'Ćab_e 3', 'ćab_e_3' ],
168 [ 'ĆAB', 'ćAB' ],
169 [ 'ćab', 'ćab' ],
170 [ 'ćaB', 'ćaB' ],
171 ];
172 }
173 }