Don't check namespace in SpecialWantedtemplates
[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 $message = new Message( $key, $params, $language );
20
21 $this->assertEquals( $key, $message->getKey() );
22 $this->assertEquals( $params, $message->getParams() );
23 $this->assertEquals( $expectedLang, $message->getLanguage() );
24 }
25
26 public static function provideConstructor() {
27 $langDe = Language::factory( 'de' );
28 $langEn = Language::factory( 'en' );
29
30 return array(
31 array( $langDe, 'foo', array(), $langDe ),
32 array( $langDe, 'foo', array( 'bar' ), $langDe ),
33 array( $langEn, 'foo', array( 'bar' ), null )
34 );
35 }
36
37 public static function provideConstructorParams() {
38 return array(
39 array(
40 array(),
41 array(),
42 ),
43 array(
44 array( 'foo' ),
45 array( 'foo' ),
46 ),
47 array(
48 array( 'foo', 'bar' ),
49 array( 'foo', 'bar' ),
50 ),
51 array(
52 array( 'baz' ),
53 array( array( 'baz' ) ),
54 ),
55 array(
56 array( 'baz', 'foo' ),
57 array( array( 'baz', 'foo' ) ),
58 ),
59 array(
60 array( 'baz', 'foo' ),
61 array( array( 'baz', 'foo' ), 'hhh' ),
62 ),
63 array(
64 array( 'baz', 'foo' ),
65 array( array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
66 ),
67 array(
68 array( 'baz', 'foo' ),
69 array( array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
70 ),
71 array(
72 array( 'baz' ),
73 array( array( 'baz' ), array( 'ahahahahha' ) ),
74 ),
75 );
76 }
77
78 /**
79 * @covers Message::__construct
80 * @covers Message::getParams
81 * @dataProvider provideConstructorParams
82 */
83 public function testConstructorParams( $expected, $args ) {
84 $msg = new Message( 'imasomething' );
85
86 $returned = call_user_func_array( array( $msg, 'params' ), $args );
87
88 $this->assertSame( $msg, $returned );
89 $this->assertEquals( $expected, $msg->getParams() );
90 }
91
92 public static function provideConstructorLanguage() {
93 return array(
94 array( 'foo', array( 'bar' ), 'en' ),
95 array( 'foo', array( 'bar' ), 'de' )
96 );
97 }
98
99 /**
100 * @covers Message::__construct
101 * @covers Message::getLanguage
102 * @dataProvider provideConstructorLanguage
103 */
104 public function testConstructorLanguage( $key, $params, $languageCode ) {
105 $language = Language::factory( $languageCode );
106 $message = new Message( $key, $params, $language );
107
108 $this->assertEquals( $language, $message->getLanguage() );
109 }
110
111 public static function provideKeys() {
112 return array(
113 'string' => array(
114 'key' => 'mainpage',
115 'expected' => array( 'mainpage' ),
116 ),
117 'single' => array(
118 'key' => array( 'mainpage' ),
119 'expected' => array( 'mainpage' ),
120 ),
121 'multi' => array(
122 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
123 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
124 ),
125 'empty' => array(
126 'key' => array(),
127 'expected' => null,
128 'exception' => 'InvalidArgumentException',
129 ),
130 'null' => array(
131 'key' => null,
132 'expected' => null,
133 'exception' => 'InvalidArgumentException',
134 ),
135 'bad type' => array(
136 'key' => 123,
137 'expected' => null,
138 'exception' => 'InvalidArgumentException',
139 ),
140 );
141 }
142
143 /**
144 * @covers Message::__construct
145 * @covers Message::getKey
146 * @covers Message::isMultiKey
147 * @covers Message::getKeysToTry
148 * @dataProvider provideKeys
149 */
150 public function testKeys( $key, $expected, $exception = null ) {
151 if ( $exception ) {
152 $this->setExpectedException( $exception );
153 }
154
155 $msg = new Message( $key );
156 $this->assertContains( $msg->getKey(), $expected );
157 $this->assertEquals( $expected, $msg->getKeysToTry() );
158 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
159 }
160
161 /**
162 * @covers ::wfMessage
163 */
164 public function testWfMessage() {
165 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
166 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
167 }
168
169 /**
170 * @covers Message::newFromKey
171 */
172 public function testNewFromKey() {
173 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
174 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
175 }
176
177 /**
178 * @covers ::wfMessage
179 * @covers Message::__construct
180 */
181 public function testWfMessageParams() {
182 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
183 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
184 $this->assertEquals(
185 'You have foo (bar).',
186 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
187 );
188 $this->assertEquals(
189 'You have foo (bar).',
190 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
191 );
192 }
193
194 /**
195 * @covers Message::exists
196 */
197 public function testExists() {
198 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
199 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
200 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
201 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
202 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
203 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
204 }
205
206 /**
207 * @covers Message::__construct
208 * @covers Message::text
209 * @covers Message::plain
210 * @covers Message::escaped
211 * @covers Message::toString
212 */
213 public function testToStringKey() {
214 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
215 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
216 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->text() );
217 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
218 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
219 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
220 $this->assertEquals(
221 '&lt;i&lt;dont&gt;exist-evar&gt;',
222 wfMessage( 'i<dont>exist-evar' )->escaped()
223 );
224 }
225
226 public static function provideToString() {
227 return array(
228 array( 'mainpage', 'Main Page' ),
229 array( 'i-dont-exist-evar', '<i-dont-exist-evar>' ),
230 array( 'i-dont-exist-evar', '&lt;i-dont-exist-evar&gt;', 'escaped' ),
231 );
232 }
233
234 /**
235 * @covers Message::toString
236 * @covers Message::__toString
237 * @dataProvider provideToString
238 */
239 public function testToString( $key, $expect, $format = 'plain' ) {
240 $msg = new Message( $key );
241 $msg->$format();
242 $this->assertEquals( $expect, $msg->toString() );
243 $this->assertEquals( $expect, $msg->__toString() );
244 }
245
246 /**
247 * @covers Message::inLanguage
248 */
249 public function testInLanguage() {
250 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
251 $this->assertEquals( 'Заглавная страница',
252 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
253
254 // NOTE: make sure internal caching of the message text is reset appropriately
255 $msg = wfMessage( 'mainpage' );
256 $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
257 $this->assertEquals(
258 'Заглавная страница',
259 $msg->inLanguage( Language::factory( 'ru' ) )->text()
260 );
261 }
262
263 /**
264 * @covers Message::rawParam
265 * @covers Message::rawParams
266 */
267 public function testRawParams() {
268 $this->assertEquals(
269 '(Заглавная страница)',
270 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
271 );
272 $this->assertEquals(
273 '(Заглавная страница $1)',
274 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
275 );
276 $this->assertEquals(
277 '(Заглавная страница)',
278 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
279 );
280 $this->assertEquals(
281 '(Заглавная страница $1)',
282 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
283 );
284 }
285
286 /**
287 * @covers RawMessage::__construct
288 * @covers RawMessage::fetchMessage
289 */
290 public function testRawMessage() {
291 $msg = new RawMessage( 'example &' );
292 $this->assertEquals( 'example &', $msg->plain() );
293 $this->assertEquals( 'example &amp;', $msg->escaped() );
294 }
295
296 /**
297 * @covers Message::params
298 * @covers Message::toString
299 * @covers Message::replaceParameters
300 */
301 public function testReplaceManyParams() {
302 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
303 // One less than above has placeholders
304 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
305 $this->assertEquals(
306 'abcdefghijka2',
307 $msg->params( $params )->plain(),
308 'Params > 9 are replaced correctly'
309 );
310
311 $msg = new RawMessage( 'Params$*' );
312 $params = array( 'ab', 'bc', 'cd' );
313 $this->assertEquals(
314 'Params: ab, bc, cd',
315 $msg->params( $params )->text()
316 );
317 }
318
319 /**
320 * @covers Message::numParam
321 * @covers Message::numParams
322 */
323 public function testNumParams() {
324 $lang = Language::factory( 'en' );
325 $msg = new RawMessage( '$1' );
326
327 $this->assertEquals(
328 $lang->formatNum( 123456.789 ),
329 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
330 'numParams is handled correctly'
331 );
332 }
333
334 /**
335 * @covers Message::durationParam
336 * @covers Message::durationParams
337 */
338 public function testDurationParams() {
339 $lang = Language::factory( 'en' );
340 $msg = new RawMessage( '$1' );
341
342 $this->assertEquals(
343 $lang->formatDuration( 1234 ),
344 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
345 'durationParams is handled correctly'
346 );
347 }
348
349 /**
350 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
351 * @group Database
352 * @covers Message::expiryParam
353 * @covers Message::expiryParams
354 */
355 public function testExpiryParams() {
356 $lang = Language::factory( 'en' );
357 $msg = new RawMessage( '$1' );
358
359 $this->assertEquals(
360 $lang->formatExpiry( wfTimestampNow() ),
361 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
362 'expiryParams is handled correctly'
363 );
364 }
365
366 /**
367 * @covers Message::timeperiodParam
368 * @covers Message::timeperiodParams
369 */
370 public function testTimeperiodParams() {
371 $lang = Language::factory( 'en' );
372 $msg = new RawMessage( '$1' );
373
374 $this->assertEquals(
375 $lang->formatTimePeriod( 1234 ),
376 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
377 'timeperiodParams is handled correctly'
378 );
379 }
380
381 /**
382 * @covers Message::sizeParam
383 * @covers Message::sizeParams
384 */
385 public function testSizeParams() {
386 $lang = Language::factory( 'en' );
387 $msg = new RawMessage( '$1' );
388
389 $this->assertEquals(
390 $lang->formatSize( 123456 ),
391 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
392 'sizeParams is handled correctly'
393 );
394 }
395
396 /**
397 * @covers Message::bitrateParam
398 * @covers Message::bitrateParams
399 */
400 public function testBitrateParams() {
401 $lang = Language::factory( 'en' );
402 $msg = new RawMessage( '$1' );
403
404 $this->assertEquals(
405 $lang->formatBitrate( 123456 ),
406 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
407 'bitrateParams is handled correctly'
408 );
409 }
410
411 public static function providePlaintextParams() {
412 return array(
413 array(
414 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
415 'plain',
416 ),
417
418 array(
419 // expect
420 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
421 // format
422 'text',
423 ),
424 array(
425 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
426 'escaped',
427 ),
428
429 array(
430 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
431 'parse',
432 ),
433
434 array(
435 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
436 'parseAsBlock',
437 ),
438 );
439 }
440
441 /**
442 * @covers Message::plaintextParam
443 * @covers Message::plaintextParams
444 * @covers Message::formatPlaintext
445 * @covers Message::toString
446 * @covers Message::parse
447 * @covers Message::parseAsBlock
448 * @dataProvider providePlaintextParams
449 */
450 public function testPlaintextParams( $expect, $format ) {
451 $lang = Language::factory( 'en' );
452
453 $msg = new RawMessage( '$1 $2' );
454 $params = array(
455 'one $2',
456 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
457 );
458 $this->assertEquals(
459 $expect,
460 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
461 "Fail formatting for $format"
462 );
463 }
464
465 public static function provideParser() {
466 return array(
467 array(
468 "''&'' <x><!-- x -->",
469 'plain',
470 ),
471
472 array(
473 "''&'' <x><!-- x -->",
474 'text',
475 ),
476 array(
477 '<i>&amp;</i> &lt;x&gt;',
478 'parse',
479 ),
480
481 array(
482 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
483 'parseAsBlock',
484 ),
485 );
486 }
487
488 /**
489 * @covers Message::text
490 * @covers Message::parse
491 * @covers Message::parseAsBlock
492 * @covers Message::toString
493 * @covers Message::transformText
494 * @covers Message::parseText
495 * @dataProvider provideParser
496 */
497 public function testParser( $expect, $format ) {
498 $msg = new RawMessage( "''&'' <x><!-- x -->" );
499 $this->assertEquals(
500 $expect,
501 $msg->inLanguage( 'en' )->$format()
502 );
503 }
504
505 /**
506 * @covers Message::inContentLanguage
507 */
508 public function testInContentLanguage() {
509 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
510
511 // NOTE: make sure internal caching of the message text is reset appropriately
512 $msg = wfMessage( 'mainpage' );
513 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
514 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
515 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
516 }
517
518 /**
519 * @covers Message::inContentLanguage
520 */
521 public function testInContentLanguageOverride() {
522 $this->setMwGlobals( array(
523 'wgLang' => Language::factory( 'fr' ),
524 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
525 ) );
526
527 // NOTE: make sure internal caching of the message text is reset appropriately.
528 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
529 $msg = wfMessage( 'mainpage' );
530 $this->assertEquals(
531 'Accueil',
532 $msg->inContentLanguage()->plain(),
533 'inContentLanguage() with ForceUIMsg override enabled'
534 );
535 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
536 $this->assertEquals(
537 'Main Page',
538 $msg->inContentLanguage()->plain(),
539 'inContentLanguage() with ForceUIMsg override enabled'
540 );
541 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
542 }
543
544 /**
545 * @expectedException MWException
546 * @covers Message::inLanguage
547 */
548 public function testInLanguageThrows() {
549 wfMessage( 'foo' )->inLanguage( 123 );
550 }
551 }