ada453c2088f95697877322a3edd4b7c3004f68a
[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 -> en
35 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
36 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
37 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
38 $this->makePage( 'FallbackLanguageTest-Full', 'en' );
39
40 // Fallbacks where ab does not exist
41 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
42 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
43 $this->makePage( 'FallbackLanguageTest-Partial', 'en' );
44
45 // Fallback to the content language
46 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
47 $this->makePage( 'FallbackLanguageTest-ContLang', 'en' );
48
49 // Fallback to english
50 $this->makePage( 'FallbackLanguageTest-English', 'en' );
51
52 // Full key tests -- always want russian
53 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
54 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
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 */
63 protected function makePage( $title, $lang ) {
64 global $wgContLang;
65
66 $title = Title::newFromText(
67 ( $lang == $wgContLang->getCode() ) ? $title : "$title/$lang",
68 NS_MEDIAWIKI
69 );
70 $wikiPage = new WikiPage( $title );
71 $content = ContentHandler::makeContent( $lang, $title );
72 $wikiPage->doEditContent( $content, "$lang translation test case" );
73 }
74
75 /**
76 * Test message fallbacks, bug #1495
77 *
78 * @dataProvider provideMessagesForFallback
79 */
80 function testMessageFallbacks( $message, $lang, $expectedContent ) {
81 $result = MessageCache::singleton()->get( $message, true, $lang );
82 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
83 }
84
85 public static function provideMessagesForFallback() {
86 return array(
87 array( 'FallbackLanguageTest-Full', 'ab', 'ab' ),
88 array( 'FallbackLanguageTest-Partial', 'ab', 'ru' ),
89 array( 'FallbackLanguageTest-ContLang', 'ab', 'de' ),
90 array( 'FallbackLanguageTest-English', 'ab', 'en' ),
91 array( 'FallbackLanguageTest-None', 'ab', false ),
92 );
93 }
94
95 /**
96 * There's a fallback case where the message key is given as fully qualified -- this
97 * should ignore the passed $lang and use the language from the key
98 *
99 * @dataProvider provideMessagesForFullKeys
100 */
101 function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
102 $result = MessageCache::singleton()->get( $message, true, $lang, true );
103 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
104 }
105
106 public static function provideMessagesForFullKeys() {
107 return array(
108 array( 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ),
109 array( 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ),
110 array( 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ),
111 );
112 }
113
114 }