MediaWiki
REL1_22
|
00001 <?php 00002 00008 class MessageCacheTest extends MediaWikiLangTestCase { 00009 00010 protected function setUp() { 00011 parent::setUp(); 00012 $this->configureLanguages(); 00013 MessageCache::singleton()->enable(); 00014 } 00015 00019 protected function configureLanguages() { 00020 // for the test, we need the content language to be anything but English, 00021 // let's choose e.g. German (de) 00022 $langCode = 'de'; 00023 $langObj = Language::factory( $langCode ); 00024 00025 $this->setMwGlobals( array( 00026 'wgLanguageCode' => $langCode, 00027 'wgLang' => $langObj, 00028 'wgContLang' => $langObj, 00029 ) ); 00030 } 00031 00032 function addDBData() { 00033 $this->configureLanguages(); 00034 00035 // Set up messages and fallbacks ab -> ru -> de 00036 $this->makePage( 'FallbackLanguageTest-Full', 'ab' ); 00037 $this->makePage( 'FallbackLanguageTest-Full', 'ru' ); 00038 $this->makePage( 'FallbackLanguageTest-Full', 'de' ); 00039 00040 // Fallbacks where ab does not exist 00041 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' ); 00042 $this->makePage( 'FallbackLanguageTest-Partial', 'de' ); 00043 00044 // Fallback to the content language 00045 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' ); 00046 00047 // Add customizations for an existing message. 00048 $this->makePage( 'sunday', 'ru' ); 00049 00050 // Full key tests -- always want russian 00051 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' ); 00052 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' ); 00053 00054 // In content language -- get base if no derivative 00055 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none', false ); 00056 } 00057 00066 protected function makePage( $title, $lang, $content = null, $createSubPage = true ) { 00067 global $wgContLang; 00068 00069 if ( $content === null ) { 00070 $content = $lang; 00071 } 00072 if ( $lang !== $wgContLang->getCode() || $createSubPage ) { 00073 $title = "$title/$lang"; 00074 } 00075 00076 $title = Title::newFromText( $title, NS_MEDIAWIKI ); 00077 $wikiPage = new WikiPage( $title ); 00078 $contentHandler = ContentHandler::makeContent( $content, $title ); 00079 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" ); 00080 } 00081 00087 public function testMessageFallbacks( $message, $lang, $expectedContent ) { 00088 $result = MessageCache::singleton()->get( $message, true, $lang ); 00089 $this->assertEquals( $expectedContent, $result, "Message fallback failed." ); 00090 } 00091 00092 function provideMessagesForFallback() { 00093 return array( 00094 array( 'FallbackLanguageTest-Full', 'ab', 'ab' ), 00095 array( 'FallbackLanguageTest-Partial', 'ab', 'ru' ), 00096 array( 'FallbackLanguageTest-ContLang', 'ab', 'de' ), 00097 array( 'FallbackLanguageTest-None', 'ab', false ), 00098 00099 // Existing message with customizations on the fallbacks 00100 array( 'sunday', 'ab', 'амҽыш' ), 00101 00102 // bug 46579 00103 array( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ), 00104 // UI language different from content language should only use de/none as last option 00105 array( 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ), 00106 ); 00107 } 00108 00115 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) { 00116 $result = MessageCache::singleton()->get( $message, true, $lang, true ); 00117 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." ); 00118 } 00119 00120 function provideMessagesForFullKeys() { 00121 return array( 00122 array( 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ), 00123 array( 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ), 00124 array( 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ), 00125 ); 00126 } 00127 00128 }