Avoid rebuilding database fixtures for every test run
[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 $this->setUserLang( 'de' );
23 $this->setContentLang( 'de' );
24 }
25
26 function addDBDataOnce() {
27 $this->configureLanguages();
28
29 // Set up messages and fallbacks ab -> ru -> de
30 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
31 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
32 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
33
34 // Fallbacks where ab does not exist
35 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
36 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
37
38 // Fallback to the content language
39 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
40
41 // Add customizations for an existing message.
42 $this->makePage( 'sunday', 'ru' );
43
44 // Full key tests -- always want russian
45 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
46 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
47
48 // In content language -- get base if no derivative
49 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
50 }
51
52 /**
53 * Helper function for addDBData -- adds a simple page to the database
54 *
55 * @param string $title Title of page to be created
56 * @param string $lang Language and content of the created page
57 * @param string|null $content Content of the created page, or null for a generic string
58 */
59 protected function makePage( $title, $lang, $content = null ) {
60 global $wgContLang;
61
62 if ( $content === null ) {
63 $content = $lang;
64 }
65 if ( $lang !== $wgContLang->getCode() ) {
66 $title = "$title/$lang";
67 }
68
69 $title = Title::newFromText( $title, NS_MEDIAWIKI );
70 $wikiPage = new WikiPage( $title );
71 $contentHandler = ContentHandler::makeContent( $content, $title );
72 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
73 }
74
75 /**
76 * Test message fallbacks, bug #1495
77 *
78 * @dataProvider provideMessagesForFallback
79 */
80 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
81 $result = MessageCache::singleton()->get( $message, true, $lang );
82 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
83 }
84
85 function provideMessagesForFallback() {
86 return [
87 [ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
88 [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
89 [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
90 [ 'FallbackLanguageTest-None', 'ab', false ],
91
92 // Existing message with customizations on the fallbacks
93 [ 'sunday', 'ab', 'амҽыш' ],
94
95 // bug 46579
96 [ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
97 // UI language different from content language should only use de/none as last option
98 [ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
99 ];
100 }
101
102 /**
103 * There's a fallback case where the message key is given as fully qualified -- this
104 * should ignore the passed $lang and use the language from the key
105 *
106 * @dataProvider provideMessagesForFullKeys
107 */
108 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
109 $result = MessageCache::singleton()->get( $message, true, $lang, true );
110 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
111 }
112
113 function provideMessagesForFullKeys() {
114 return [
115 [ 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ],
116 [ 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ],
117 [ 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ],
118 ];
119 }
120
121 /**
122 * @dataProvider provideNormalizeKey
123 */
124 public function testNormalizeKey( $key, $expected ) {
125 $actual = MessageCache::normalizeKey( $key );
126 $this->assertEquals( $expected, $actual );
127 }
128
129 public function provideNormalizeKey() {
130 return [
131 [ 'Foo', 'foo' ],
132 [ 'foo', 'foo' ],
133 [ 'fOo', 'fOo' ],
134 [ 'FOO', 'fOO' ],
135 [ 'Foo bar', 'foo_bar' ],
136 [ 'Ćab', 'ćab' ],
137 [ 'Ćab_e 3', 'ćab_e_3' ],
138 [ 'ĆAB', 'ćAB' ],
139 [ 'ćab', 'ćab' ],
140 [ 'ćaB', 'ćaB' ],
141 ];
142 }
143 }