4cc8bd2dad021286147cf080c2958bb9bcebedf7
[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 MessageCache::singleton()->enable();
12 }
13
14 function addDBData() {
15 // be sure english messages under $key, not $key/en
16 $this->setMwGlobals( array(
17 'wgLanguageCode' => 'en',
18 'wgContLang' => Language::factory( 'en' ),
19 ) );
20
21 // Set up messages and fallbacks ab -> ru -> en
22 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
23 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
24 $this->makePage( 'FallbackLanguageTest-Full', 'en' );
25
26 // Fallbacks where ab does not exist
27 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
28 $this->makePage( 'FallbackLanguageTest-Partial', 'en' );
29
30 // Fallback to english
31 $this->makePage( 'FallbackLanguageTest-English', 'en' );
32
33 // Full key tests -- always want russian
34 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
35 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
36 }
37
38 /**
39 * Helper function for addDBData -- adds a simple page to the database
40 *
41 * @param string $title Title of page to be created
42 * @param string $lang Language and content of the created page
43 */
44 protected function makePage( $title, $lang ) {
45 global $wgContLang;
46
47 $title = Title::newFromText(
48 ($lang == $wgContLang->getCode()) ? $title : "$title/$lang",
49 NS_MEDIAWIKI
50 );
51 $wikiPage = new WikiPage( $title );
52 $content = ContentHandler::makeContent( $lang, $title );
53 $wikiPage->doEditContent( $content, "$lang translation test case" );
54 }
55
56 /**
57 * Test message fallbacks, bug #1495
58 *
59 * @dataProvider provideMessagesForFallback
60 */
61 function testMessageFallbacks( $message, $lang, $expectedContent ) {
62 $result = MessageCache::singleton()->get( $message, true, $lang );
63 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
64 }
65
66 function provideMessagesForFallback() {
67 return array(
68 array( 'FallbackLanguageTest-Full', 'ab', 'ab' ),
69 array( 'FallbackLanguageTest-Partial', 'ab', 'ru' ),
70 array( 'FallbackLanguageTest-English', 'ab', 'en' ),
71 array( 'FallbackLanguageTest-None', 'ab', false ),
72 );
73 }
74
75 /**
76 * There's a fallback case where the message key is given as fully qualified -- this
77 * should ignore the passed $lang and use the language from the key
78 *
79 * @dataProvider provideMessagesForFullKeys
80 */
81 function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
82 $result = MessageCache::singleton()->get( $message, true, $lang, true );
83 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
84 }
85
86 function provideMessagesForFullKeys() {
87 return array(
88 array( 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ),
89 array( 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ),
90 array( 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ),
91 );
92 }
93
94 }