Merge "vector: Add SVG versions of collapsible menu icons"
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / MessageCacheTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Cache
6 */
7 class MessageCacheTest extends MediaWikiLangTestCase {
8
9 protected function setUp() {
10 parent::setUp();
11 $this->configureLanguages();
12 MessageCache::singleton()->enable();
13 }
14
15 /**
16 * Helper function -- setup site language for testing
17 */
18 protected function configureLanguages() {
19 // for the test, we need the content language to be anything but English,
20 // let's choose e.g. German (de)
21 $langCode = 'de';
22 $langObj = Language::factory( $langCode );
23
24 $this->setMwGlobals( array(
25 'wgLanguageCode' => $langCode,
26 'wgLang' => $langObj,
27 'wgContLang' => $langObj,
28 ) );
29 }
30
31 function addDBData() {
32 $this->configureLanguages();
33
34 // Set up messages and fallbacks ab -> ru -> de
35 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
36 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
37 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
38
39 // Fallbacks where ab does not exist
40 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
41 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
42
43 // Fallback to the content language
44 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
45
46 // Add customizations for an existing message.
47 $this->makePage( 'sunday', 'ru' );
48
49 // Full key tests -- always want russian
50 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
51 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
52
53 // In content language -- get base if no derivative
54 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none', false );
55 }
56
57 /**
58 * Helper function for addDBData -- adds a simple page to the database
59 *
60 * @param string $title Title of page to be created
61 * @param string $lang Language and content of the created page
62 * @param string|null $content Content of the created page, or null for a generic string
63 * @param bool $createSubPage Set to false if a root page should be created
64 */
65 protected function makePage( $title, $lang, $content = null, $createSubPage = true ) {
66 global $wgContLang;
67
68 if ( $content === null ) {
69 $content = $lang;
70 }
71 if ( $lang !== $wgContLang->getCode() || $createSubPage ) {
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 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 array(
93 array( 'FallbackLanguageTest-Full', 'ab', 'ab' ),
94 array( 'FallbackLanguageTest-Partial', 'ab', 'ru' ),
95 array( 'FallbackLanguageTest-ContLang', 'ab', 'de' ),
96 array( 'FallbackLanguageTest-None', 'ab', false ),
97
98 // Existing message with customizations on the fallbacks
99 array( 'sunday', 'ab', 'амҽыш' ),
100
101 // bug 46579
102 array( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ),
103 // UI language different from content language should only use de/none as last option
104 array( '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 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 array(
121 array( 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ),
122 array( 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ),
123 array( 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ),
124 );
125 }
126
127 }