8 class MessageCacheTest
extends MediaWikiLangTestCase
{
10 protected function setUp() {
12 $this->configureLanguages();
13 MessageCache
::singleton()->enable();
17 * Helper function -- setup site language for testing
19 protected function configureLanguages() {
20 // for the test, we need the content language to be anything but English,
21 // let's choose e.g. German (de)
23 $langObj = Language
::factory( $langCode );
25 $this->setMwGlobals( array(
26 'wgLanguageCode' => $langCode,
28 'wgContLang' => $langObj,
32 function addDBData() {
33 $this->configureLanguages();
35 // Set up messages and fallbacks ab -> ru -> de
36 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
37 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
38 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
40 // Fallbacks where ab does not exist
41 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
42 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
44 // Fallback to the content language
45 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
47 // Add customizations for an existing message.
48 $this->makePage( 'sunday', 'ru' );
50 // Full key tests -- always want russian
51 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
52 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
54 // In content language -- get base if no derivative
55 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none', false );
59 * Helper function for addDBData -- adds a simple page to the database
61 * @param string $title Title of page to be created
62 * @param string $lang Language and content of the created page
63 * @param string|null $content Content of the created page, or null for a generic string
64 * @param bool $createSubPage Set to false if a root page should be created
66 protected function makePage( $title, $lang, $content = null, $createSubPage = true ) {
69 if ( $content === null ) {
72 if ( $lang !== $wgContLang->getCode() ||
$createSubPage ) {
73 $title = "$title/$lang";
76 $title = Title
::newFromText( $title, NS_MEDIAWIKI
);
77 $wikiPage = new WikiPage( $title );
78 $contentHandler = ContentHandler
::makeContent( $content, $title );
79 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
83 * Test message fallbacks, bug #1495
85 * @dataProvider provideMessagesForFallback
87 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
88 $result = MessageCache
::singleton()->get( $message, true, $lang );
89 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
92 function provideMessagesForFallback() {
94 array( 'FallbackLanguageTest-Full', 'ab', 'ab' ),
95 array( 'FallbackLanguageTest-Partial', 'ab', 'ru' ),
96 array( 'FallbackLanguageTest-ContLang', 'ab', 'de' ),
97 array( 'FallbackLanguageTest-None', 'ab', false ),
99 // Existing message with customizations on the fallbacks
100 array( 'sunday', 'ab', 'амҽыш' ),
103 array( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ),
104 // UI language different from content language should only use de/none as last option
105 array( 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ),
110 * There's a fallback case where the message key is given as fully qualified -- this
111 * should ignore the passed $lang and use the language from the key
113 * @dataProvider provideMessagesForFullKeys
115 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
116 $result = MessageCache
::singleton()->get( $message, true, $lang, true );
117 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
120 function provideMessagesForFullKeys() {
122 array( 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ),
123 array( 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ),
124 array( 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ),