TableDiffFormatter: Don't repeatedly call array_shift()
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / MessageCacheTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Cache
6 * @covers MessageCache
7 */
8 class MessageCacheTest extends MediaWikiLangTestCase {
9
10 protected function setUp() {
11 parent::setUp();
12 $this->configureLanguages();
13 MessageCache::singleton()->enable();
14 }
15
16 /**
17 * Helper function -- setup site language for testing
18 */
19 protected function configureLanguages() {
20 // for the test, we need the content language to be anything but English,
21 // let's choose e.g. German (de)
22 $langCode = 'de';
23 $langObj = Language::factory( $langCode );
24
25 $this->setMwGlobals( [
26 'wgLanguageCode' => $langCode,
27 'wgLang' => $langObj,
28 'wgContLang' => $langObj,
29 ] );
30 }
31
32 function addDBData() {
33 $this->configureLanguages();
34
35 // Set up messages and fallbacks ab -> ru -> de
36 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
37 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
38 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
39
40 // Fallbacks where ab does not exist
41 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
42 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
43
44 // Fallback to the content language
45 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
46
47 // Add customizations for an existing message.
48 $this->makePage( 'sunday', 'ru' );
49
50 // Full key tests -- always want russian
51 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
52 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
53
54 // In content language -- get base if no derivative
55 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
56 }
57
58 /**
59 * Helper function for addDBData -- adds a simple page to the database
60 *
61 * @param string $title Title of page to be created
62 * @param string $lang Language and content of the created page
63 * @param string|null $content Content of the created page, or null for a generic string
64 */
65 protected function makePage( $title, $lang, $content = null ) {
66 global $wgContLang;
67
68 if ( $content === null ) {
69 $content = $lang;
70 }
71 if ( $lang !== $wgContLang->getCode() ) {
72 $title = "$title/$lang";
73 }
74
75 $title = Title::newFromText( $title, NS_MEDIAWIKI );
76 $wikiPage = new WikiPage( $title );
77 $contentHandler = ContentHandler::makeContent( $content, $title );
78 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
79 }
80
81 /**
82 * Test message fallbacks, bug #1495
83 *
84 * @dataProvider provideMessagesForFallback
85 */
86 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
87 $result = MessageCache::singleton()->get( $message, true, $lang );
88 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
89 }
90
91 function provideMessagesForFallback() {
92 return [
93 [ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
94 [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
95 [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
96 [ 'FallbackLanguageTest-None', 'ab', false ],
97
98 // Existing message with customizations on the fallbacks
99 [ 'sunday', 'ab', 'амҽыш' ],
100
101 // bug 46579
102 [ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
103 // UI language different from content language should only use de/none as last option
104 [ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
105 ];
106 }
107
108 /**
109 * There's a fallback case where the message key is given as fully qualified -- this
110 * should ignore the passed $lang and use the language from the key
111 *
112 * @dataProvider provideMessagesForFullKeys
113 */
114 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
115 $result = MessageCache::singleton()->get( $message, true, $lang, true );
116 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
117 }
118
119 function provideMessagesForFullKeys() {
120 return [
121 [ 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ],
122 [ 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ],
123 [ 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ],
124 ];
125 }
126
127 /**
128 * @dataProvider provideNormalizeKey
129 */
130 public function testNormalizeKey( $key, $expected ) {
131 $actual = MessageCache::normalizeKey( $key );
132 $this->assertEquals( $expected, $actual );
133 }
134
135 public function provideNormalizeKey() {
136 return [
137 [ 'Foo', 'foo' ],
138 [ 'foo', 'foo' ],
139 [ 'fOo', 'fOo' ],
140 [ 'FOO', 'fOO' ],
141 [ 'Foo bar', 'foo_bar' ],
142 [ 'Ćab', 'ćab' ],
143 [ 'Ćab_e 3', 'ćab_e_3' ],
144 [ 'ĆAB', 'ćAB' ],
145 [ 'ćab', 'ćab' ],
146 [ 'ćaB', 'ćaB' ],
147 ];
148 }
149 }