7 class MessageCacheTest
extends MediaWikiLangTestCase
{
9 protected function setUp() {
11 $this->configureLanguages();
12 MessageCache
::singleton()->enable();
16 * Helper function -- setup site language for testing
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)
22 $langObj = Language
::factory( $langCode );
24 $this->setMwGlobals( array(
25 'wgLanguageCode' => $langCode,
27 'wgContLang' => $langObj,
31 function addDBData() {
32 $this->configureLanguages();
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' );
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' );
45 // Fallback to the content language
46 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
47 $this->makePage( 'FallbackLanguageTest-ContLang', 'en' );
49 // Fallback to english
50 $this->makePage( 'FallbackLanguageTest-English', 'en' );
52 // Full key tests -- always want russian
53 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
54 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
58 * Helper function for addDBData -- adds a simple page to the database
60 * @param string $title Title of page to be created
61 * @param string $lang Language and content of the created page
63 protected function makePage( $title, $lang ) {
66 $title = Title
::newFromText(
67 ( $lang == $wgContLang->getCode() ) ?
$title : "$title/$lang",
70 $wikiPage = new WikiPage( $title );
71 $content = ContentHandler
::makeContent( $lang, $title );
72 $wikiPage->doEditContent( $content, "$lang translation test case" );
76 * Test message fallbacks, bug #1495
78 * @dataProvider provideMessagesForFallback
80 function testMessageFallbacks( $message, $lang, $expectedContent ) {
81 $result = MessageCache
::singleton()->get( $message, true, $lang );
82 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
85 public static function provideMessagesForFallback() {
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 ),
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
99 * @dataProvider provideMessagesForFullKeys
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." );
106 public static function provideMessagesForFullKeys() {
108 array( 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ),
109 array( 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ),
110 array( 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ),