3 use MediaWiki\MediaWikiServices
;
10 class MessageCacheTest
extends MediaWikiLangTestCase
{
12 protected function setUp() {
14 $this->configureLanguages();
15 MessageCache
::destroyInstance();
16 MessageCache
::singleton()->enable();
20 * Helper function -- setup site language for testing
22 protected function configureLanguages() {
23 // for the test, we need the content language to be anything but English,
24 // let's choose e.g. German (de)
25 $this->setUserLang( 'de' );
26 $this->setContentLang( 'de' );
29 function addDBDataOnce() {
30 $this->configureLanguages();
32 // Set up messages and fallbacks ab -> ru -> de
33 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
34 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
35 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
37 // Fallbacks where ab does not exist
38 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
39 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
41 // Fallback to the content language
42 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
44 // Add customizations for an existing message.
45 $this->makePage( 'sunday', 'ru' );
47 // Full key tests -- always want russian
48 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
49 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
51 // In content language -- get base if no derivative
52 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
56 * Helper function for addDBData -- adds a simple page to the database
58 * @param string $title Title of page to be created
59 * @param string $lang Language and content of the created page
60 * @param string|null $content Content of the created page, or null for a generic string
62 protected function makePage( $title, $lang, $content = null ) {
63 if ( $content === null ) {
66 if ( $lang !== MediaWikiServices
::getInstance()->getContentLanguage()->getCode() ) {
67 $title = "$title/$lang";
70 $title = Title
::newFromText( $title, NS_MEDIAWIKI
);
71 $wikiPage = new WikiPage( $title );
72 $contentHandler = ContentHandler
::makeContent( $content, $title );
73 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
77 * Test message fallbacks, bug #1495
79 * @dataProvider provideMessagesForFallback
81 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
82 $result = MessageCache
::singleton()->get( $message, true, $lang );
83 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
86 function provideMessagesForFallback() {
88 [ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
89 [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
90 [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
91 [ 'FallbackLanguageTest-None', 'ab', false ],
93 // Existing message with customizations on the fallbacks
94 [ 'sunday', 'ab', 'амҽыш' ],
97 [ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
98 // UI language different from content language should only use de/none as last option
99 [ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
103 public function testReplaceMsg() {
104 $messageCache = MessageCache
::singleton();
106 $uckey = MediaWikiServices
::getInstance()->getContentLanguage()->ucfirst( $message );
107 $oldText = $messageCache->get( $message ); // "Ausführen"
109 $dbw = wfGetDB( DB_MASTER
);
110 $dbw->startAtomic( __METHOD__
); // simulate request and block deferred updates
111 $messageCache->replace( $uckey, 'Allez!' );
112 $this->assertEquals( 'Allez!',
113 $messageCache->getMsgFromNamespace( $uckey, 'de' ),
114 'Updates are reflected in-process immediately' );
115 $this->assertEquals( 'Allez!',
116 $messageCache->get( $message ),
117 'Updates are reflected in-process immediately' );
118 $this->makePage( 'Go', 'de', 'Race!' );
119 $dbw->endAtomic( __METHOD__
);
121 $this->assertEquals( 0,
122 DeferredUpdates
::pendingUpdatesCount(),
123 'Post-commit deferred update triggers a run of all updates' );
125 $this->assertEquals( 'Race!', $messageCache->get( $message ), 'Correct final contents' );
127 $this->makePage( 'Go', 'de', $oldText );
128 $messageCache->replace( $uckey, $oldText ); // deferred update runs immediately
129 $this->assertEquals( $oldText, $messageCache->get( $message ), 'Content restored' );
133 * There's a fallback case where the message key is given as fully qualified -- this
134 * should ignore the passed $lang and use the language from the key
136 * @dataProvider provideMessagesForFullKeys
138 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
139 $result = MessageCache
::singleton()->get( $message, true, $lang, true );
140 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
143 function provideMessagesForFullKeys() {
145 [ 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ],
146 [ 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ],
147 [ 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ],
152 * @dataProvider provideNormalizeKey
154 public function testNormalizeKey( $key, $expected ) {
155 $actual = MessageCache
::normalizeKey( $key );
156 $this->assertEquals( $expected, $actual );
159 public function provideNormalizeKey() {
165 [ 'Foo bar', 'foo_bar' ],
167 [ 'Ćab_e 3', 'ćab_e_3' ],