MediaWiki REL1_30
MessageCacheTest.php
Go to the documentation of this file.
1<?php
2
9
10 protected function setUp() {
11 parent::setUp();
12 $this->configureLanguages();
13 MessageCache::singleton()->enable();
14 }
15
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)
22 $this->setUserLang( 'de' );
23 $this->setContentLang( 'de' );
24 }
25
26 function addDBDataOnce() {
27 $this->configureLanguages();
28
29 // Set up messages and fallbacks ab -> ru -> de
30 $this->makePage( 'FallbackLanguageTest-Full', 'ab' );
31 $this->makePage( 'FallbackLanguageTest-Full', 'ru' );
32 $this->makePage( 'FallbackLanguageTest-Full', 'de' );
33
34 // Fallbacks where ab does not exist
35 $this->makePage( 'FallbackLanguageTest-Partial', 'ru' );
36 $this->makePage( 'FallbackLanguageTest-Partial', 'de' );
37
38 // Fallback to the content language
39 $this->makePage( 'FallbackLanguageTest-ContLang', 'de' );
40
41 // Add customizations for an existing message.
42 $this->makePage( 'sunday', 'ru' );
43
44 // Full key tests -- always want russian
45 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' );
46 $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' );
47
48 // In content language -- get base if no derivative
49 $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' );
50 }
51
59 protected function makePage( $title, $lang, $content = null ) {
60 global $wgContLang;
61
62 if ( $content === null ) {
63 $content = $lang;
64 }
65 if ( $lang !== $wgContLang->getCode() ) {
66 $title = "$title/$lang";
67 }
68
69 $title = Title::newFromText( $title, NS_MEDIAWIKI );
70 $wikiPage = new WikiPage( $title );
71 $contentHandler = ContentHandler::makeContent( $content, $title );
72 $wikiPage->doEditContent( $contentHandler, "$lang translation test case" );
73 }
74
80 public function testMessageFallbacks( $message, $lang, $expectedContent ) {
81 $result = MessageCache::singleton()->get( $message, true, $lang );
82 $this->assertEquals( $expectedContent, $result, "Message fallback failed." );
83 }
84
86 return [
87 [ 'FallbackLanguageTest-Full', 'ab', 'ab' ],
88 [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ],
89 [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ],
90 [ 'FallbackLanguageTest-None', 'ab', false ],
91
92 // Existing message with customizations on the fallbacks
93 [ 'sunday', 'ab', 'амҽыш' ],
94
95 // T48579
96 [ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ],
97 // UI language different from content language should only use de/none as last option
98 [ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ],
99 ];
100 }
101
102 public function testReplaceMsg() {
103 global $wgContLang;
104
105 $messageCache = MessageCache::singleton();
106 $message = 'go';
107 $uckey = $wgContLang->ucfirst( $message );
108 $oldText = $messageCache->get( $message ); // "Ausführen"
109
110 $dbw = wfGetDB( DB_MASTER );
111 $dbw->startAtomic( __METHOD__ ); // simulate request and block deferred updates
112 $messageCache->replace( $uckey, 'Allez!' );
113 $this->assertEquals( 'Allez!',
114 $messageCache->getMsgFromNamespace( $uckey, 'de' ),
115 'Updates are reflected in-process immediately' );
116 $this->assertEquals( 'Allez!',
117 $messageCache->get( $message ),
118 'Updates are reflected in-process immediately' );
119 $this->makePage( 'Go', 'de', 'Race!' );
120 $dbw->endAtomic( __METHOD__ );
121
122 $this->assertEquals( 0,
123 DeferredUpdates::pendingUpdatesCount(),
124 'Post-commit deferred update triggers a run of all updates' );
125
126 $this->assertEquals( 'Race!', $messageCache->get( $message ), 'Correct final contents' );
127
128 $this->makePage( 'Go', 'de', $oldText );
129 $messageCache->replace( $uckey, $oldText ); // deferred update runs immediately
130 $this->assertEquals( $oldText, $messageCache->get( $message ), 'Content restored' );
131 }
132
139 public function testFullKeyBehaviour( $message, $lang, $expectedContent ) {
140 $result = MessageCache::singleton()->get( $message, true, $lang, true );
141 $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." );
142 }
143
145 return [
146 [ 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ],
147 [ 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ],
148 [ 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ],
149 ];
150 }
151
155 public function testNormalizeKey( $key, $expected ) {
156 $actual = MessageCache::normalizeKey( $key );
157 $this->assertEquals( $expected, $actual );
158 }
159
160 public function provideNormalizeKey() {
161 return [
162 [ 'Foo', 'foo' ],
163 [ 'foo', 'foo' ],
164 [ 'fOo', 'fOo' ],
165 [ 'FOO', 'fOO' ],
166 [ 'Foo bar', 'foo_bar' ],
167 [ 'Ćab', 'ćab' ],
168 [ 'Ćab_e 3', 'ćab_e_3' ],
169 [ 'ĆAB', 'ćAB' ],
170 [ 'ćab', 'ćab' ],
171 [ 'ćaB', 'ćaB' ],
172 ];
173 }
174}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Base class that store and restore the Language objects.
Database Cache MessageCache.
testFullKeyBehaviour( $message, $lang, $expectedContent)
There's a fallback case where the message key is given as fully qualified – this should ignore the pa...
testMessageFallbacks( $message, $lang, $expectedContent)
Test message fallbacks, bug #1495.
testNormalizeKey( $key, $expected)
provideNormalizeKey
makePage( $title, $lang, $content=null)
Helper function for addDBData – adds a simple page to the database.
configureLanguages()
Helper function – setup site language for testing.
static singleton()
Get the signleton instance of this class.
static normalizeKey( $key)
Normalize message key input.
Class representing a MediaWiki article and history.
Definition WikiPage.php:37
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const DB_MASTER
Definition defines.php:26
if(!isset( $args[0])) $lang