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