Merge "Don't rely on $wgTitle in WebRequest"
[lhc/web/wiklou.git] / tests / phpunit / includes / MessageTest.php
1 <?php
2
3 class MessageTest extends MediaWikiLangTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7
8 $this->setMwGlobals( array(
9 'wgLang' => Language::factory( 'en' ),
10 'wgForceUIMsgAsContentMsg' => array(),
11 ) );
12 }
13
14 /**
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
17 */
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $reflection = new ReflectionClass( 'Message' );
20
21 $keyProperty = $reflection->getProperty( 'key' );
22 $keyProperty->setAccessible( true );
23
24 $paramsProperty = $reflection->getProperty( 'parameters' );
25 $paramsProperty->setAccessible( true );
26
27 $langProperty = $reflection->getProperty( 'language' );
28 $langProperty->setAccessible( true );
29
30 $message = new Message( $key, $params, $language );
31
32 $this->assertEquals( $key, $keyProperty->getValue( $message ) );
33 $this->assertEquals( $params, $paramsProperty->getValue( $message ) );
34 $this->assertEquals( $expectedLang, $langProperty->getValue( $message ) );
35 }
36
37 public static function provideConstructor() {
38 $langDe = Language::factory( 'de' );
39 $langEn = Language::factory( 'en' );
40
41 return array(
42 array( $langDe, 'foo', array(), $langDe ),
43 array( $langDe, 'foo', array( 'bar' ), $langDe ),
44 array( $langEn, 'foo', array( 'bar' ), null )
45 );
46 }
47
48 public static function provideTestParams() {
49 return array(
50 array( array() ),
51 array( array( 'foo' ), 'foo' ),
52 array( array( 'foo', 'bar' ), 'foo', 'bar' ),
53 array( array( 'baz' ), array( 'baz' ) ),
54 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ) ),
55 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh' ),
56 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
57 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
58 array( array( 'baz' ), array( 'baz' ), array( 'ahahahahha' ) ),
59 );
60 }
61
62 public function getLanguageProvider() {
63 return array(
64 array( 'foo', array( 'bar' ), 'en' ),
65 array( 'foo', array( 'bar' ), 'de' )
66 );
67 }
68
69 /**
70 * @covers Message::getLanguage
71 * @dataProvider getLanguageProvider
72 */
73 public function testGetLanguageCode( $key, $params, $languageCode ) {
74 $language = Language::factory( $languageCode );
75 $message = new Message( $key, $params, $language );
76
77 $this->assertEquals( $language, $message->getLanguage() );
78 }
79
80 /**
81 * @covers Message::params
82 * @dataProvider provideTestParams
83 */
84 public function testParams( $expected ) {
85 $msg = new Message( 'imasomething' );
86
87 $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
88
89 $this->assertSame( $msg, $returned );
90 $this->assertEquals( $expected, $msg->getParams() );
91 }
92
93 /**
94 * @covers Message::exists
95 */
96 public function testExists() {
97 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
98 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
99 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
100 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
101 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
102 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
103 }
104
105 /**
106 * @covers Message::__construct
107 */
108 public function testKey() {
109 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
110 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
111 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
112 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
113 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->text() );
114 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
115 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
116 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
117 $this->assertEquals(
118 '&lt;i&lt;dont&gt;exist-evar&gt;',
119 wfMessage( 'i<dont>exist-evar' )->escaped()
120 );
121 }
122
123 /**
124 * @covers Message::inLanguage
125 */
126 public function testInLanguage() {
127 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
128 $this->assertEquals( 'Заглавная страница',
129 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
130
131 // NOTE: make sure internal caching of the message text is reset appropriately
132 $msg = wfMessage( 'mainpage' );
133 $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
134 $this->assertEquals(
135 'Заглавная страница',
136 $msg->inLanguage( Language::factory( 'ru' ) )->text()
137 );
138 }
139
140 /**
141 * @covers Message::__construct
142 */
143 public function testMessageParams() {
144 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
145 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
146 $this->assertEquals(
147 'You have foo (bar).',
148 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
149 );
150 $this->assertEquals(
151 'You have foo (bar).',
152 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
153 );
154 }
155
156 /**
157 * @covers Message::__construct
158 * @covers Message::rawParams
159 */
160 public function testMessageParamSubstitution() {
161 $this->assertEquals(
162 '(Заглавная страница)',
163 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
164 );
165 $this->assertEquals(
166 '(Заглавная страница $1)',
167 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
168 );
169 $this->assertEquals(
170 '(Заглавная страница)',
171 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
172 );
173 $this->assertEquals(
174 '(Заглавная страница $1)',
175 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
176 );
177 }
178
179 /**
180 * @covers Message::__construct
181 * @covers Message::params
182 */
183 public function testDeliciouslyManyParams() {
184 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
185 // One less than above has placeholders
186 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
187 $this->assertEquals(
188 'abcdefghijka2',
189 $msg->params( $params )->plain(),
190 'Params > 9 are replaced correctly'
191 );
192 }
193
194 /**
195 * @covers Message::numParams
196 */
197 public function testMessageNumParams() {
198 $lang = Language::factory( 'en' );
199 $msg = new RawMessage( '$1' );
200
201 $this->assertEquals(
202 $lang->formatNum( 123456.789 ),
203 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
204 'numParams is handled correctly'
205 );
206 }
207
208 /**
209 * @covers Message::durationParams
210 */
211 public function testMessageDurationParams() {
212 $lang = Language::factory( 'en' );
213 $msg = new RawMessage( '$1' );
214
215 $this->assertEquals(
216 $lang->formatDuration( 1234 ),
217 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
218 'durationParams is handled correctly'
219 );
220 }
221
222 /**
223 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
224 * @group Database
225 * @covers Message::expiryParams
226 */
227 public function testMessageExpiryParams() {
228 $lang = Language::factory( 'en' );
229 $msg = new RawMessage( '$1' );
230
231 $this->assertEquals(
232 $lang->formatExpiry( wfTimestampNow() ),
233 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
234 'expiryParams is handled correctly'
235 );
236 }
237
238 /**
239 * @covers Message::timeperiodParams
240 */
241 public function testMessageTimeperiodParams() {
242 $lang = Language::factory( 'en' );
243 $msg = new RawMessage( '$1' );
244
245 $this->assertEquals(
246 $lang->formatTimePeriod( 1234 ),
247 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
248 'timeperiodParams is handled correctly'
249 );
250 }
251
252 /**
253 * @covers Message::sizeParams
254 */
255 public function testMessageSizeParams() {
256 $lang = Language::factory( 'en' );
257 $msg = new RawMessage( '$1' );
258
259 $this->assertEquals(
260 $lang->formatSize( 123456 ),
261 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
262 'sizeParams is handled correctly'
263 );
264 }
265
266 /**
267 * @covers Message::bitrateParams
268 */
269 public function testMessageBitrateParams() {
270 $lang = Language::factory( 'en' );
271 $msg = new RawMessage( '$1' );
272
273 $this->assertEquals(
274 $lang->formatBitrate( 123456 ),
275 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
276 'bitrateParams is handled correctly'
277 );
278 }
279
280 public function messagePlaintextParamsProvider() {
281 return array(
282 array(
283 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
284 'plain',
285 ),
286
287 array(
288 // expect
289 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
290 // format
291 'text',
292 ),
293 array(
294 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
295 'escaped',
296 ),
297
298 array(
299 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
300 'parse',
301 ),
302
303 array(
304 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
305 'parseAsBlock',
306 ),
307 );
308 }
309
310 /**
311 * @dataProvider messagePlaintextParamsProvider
312 * @covers Message::plaintextParams
313 */
314 public function testMessagePlaintextParams( $expect, $format ) {
315 $lang = Language::factory( 'en' );
316
317 $msg = new RawMessage( '$1 $2' );
318 $params = array(
319 'one $2',
320 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
321 );
322 $this->assertEquals(
323 $expect,
324 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
325 "Fail formatting for $format"
326 );
327 }
328
329 /**
330 * @covers Message::inContentLanguage
331 */
332 public function testInContentLanguage() {
333 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
334
335 // NOTE: make sure internal caching of the message text is reset appropriately
336 $msg = wfMessage( 'mainpage' );
337 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
338 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
339 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
340 }
341
342 /**
343 * @covers Message::inContentLanguage
344 */
345 public function testInContentLanguageOverride() {
346 $this->setMwGlobals( array(
347 'wgLang' => Language::factory( 'fr' ),
348 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
349 ) );
350
351 // NOTE: make sure internal caching of the message text is reset appropriately.
352 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
353 $msg = wfMessage( 'mainpage' );
354 $this->assertEquals(
355 'Accueil',
356 $msg->inContentLanguage()->plain(),
357 'inContentLanguage() with ForceUIMsg override enabled'
358 );
359 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
360 $this->assertEquals(
361 'Main Page',
362 $msg->inContentLanguage()->plain(),
363 'inContentLanguage() with ForceUIMsg override enabled'
364 );
365 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
366 }
367
368 /**
369 * @expectedException MWException
370 * @covers Message::inLanguage
371 */
372 public function testInLanguageThrows() {
373 wfMessage( 'foo' )->inLanguage( 123 );
374 }
375
376 public function keyProvider() {
377 return array(
378 'string' => array(
379 'key' => 'mainpage',
380 'expected' => array( 'mainpage' ),
381 ),
382 'single' => array(
383 'key' => array( 'mainpage' ),
384 'expected' => array( 'mainpage' ),
385 ),
386 'multi' => array(
387 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
388 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
389 ),
390 'empty' => array(
391 'key' => array(),
392 'expected' => null,
393 'exception' => 'InvalidArgumentException',
394 ),
395 'null' => array(
396 'key' => null,
397 'expected' => null,
398 'exception' => 'InvalidArgumentException',
399 ),
400 'bad type' => array(
401 'key' => 17,
402 'expected' => null,
403 'exception' => 'InvalidArgumentException',
404 ),
405 );
406 }
407
408 /**
409 * @dataProvider keyProvider()
410 *
411 * @covers Message::getKey
412 */
413 public function testGetKey( $key, $expected, $exception = null ) {
414 if ( $exception ) {
415 $this->setExpectedException( $exception );
416 }
417
418 $msg = new Message( $key );
419 $this->assertEquals( $expected, $msg->getKeysToTry() );
420 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
421 $this->assertContains( $msg->getKey(), $expected );
422 }
423 }