Merge "mediawiki.language: fix convertNumber( ..., true )"
[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( [
9 'wgForceUIMsgAsContentMsg' => [],
10 ] );
11 $this->setUserLang( 'en' );
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->assertSame( $key, $message->getKey() );
22 $this->assertSame( $params, $message->getParams() );
23 $this->assertEquals( $expectedLang, $message->getLanguage() );
24
25 $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
26 $messageSpecifier->expects( $this->any() )
27 ->method( 'getKey' )->will( $this->returnValue( $key ) );
28 $messageSpecifier->expects( $this->any() )
29 ->method( 'getParams' )->will( $this->returnValue( $params ) );
30 $message = new Message( $messageSpecifier, [], $language );
31
32 $this->assertSame( $key, $message->getKey() );
33 $this->assertSame( $params, $message->getParams() );
34 $this->assertEquals( $expectedLang, $message->getLanguage() );
35 }
36
37 public static function provideConstructor() {
38 $langDe = Language::factory( 'de' );
39 $langEn = Language::factory( 'en' );
40
41 return [
42 [ $langDe, 'foo', [], $langDe ],
43 [ $langDe, 'foo', [ 'bar' ], $langDe ],
44 [ $langEn, 'foo', [ 'bar' ], null ]
45 ];
46 }
47
48 public static function provideConstructorParams() {
49 return [
50 [
51 [],
52 [],
53 ],
54 [
55 [ 'foo' ],
56 [ 'foo' ],
57 ],
58 [
59 [ 'foo', 'bar' ],
60 [ 'foo', 'bar' ],
61 ],
62 [
63 [ 'baz' ],
64 [ [ 'baz' ] ],
65 ],
66 [
67 [ 'baz', 'foo' ],
68 [ [ 'baz', 'foo' ] ],
69 ],
70 [
71 [ 'baz', 'foo' ],
72 [ [ 'baz', 'foo' ], 'hhh' ],
73 ],
74 [
75 [ 'baz', 'foo' ],
76 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
77 ],
78 [
79 [ 'baz', 'foo' ],
80 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
81 ],
82 [
83 [ 'baz' ],
84 [ [ 'baz' ], [ 'ahahahahha' ] ],
85 ],
86 ];
87 }
88
89 /**
90 * @covers Message::__construct
91 * @covers Message::getParams
92 * @dataProvider provideConstructorParams
93 */
94 public function testConstructorParams( $expected, $args ) {
95 $msg = new Message( 'imasomething' );
96
97 $returned = call_user_func_array( [ $msg, 'params' ], $args );
98
99 $this->assertSame( $msg, $returned );
100 $this->assertSame( $expected, $msg->getParams() );
101 }
102
103 public static function provideConstructorLanguage() {
104 return [
105 [ 'foo', [ 'bar' ], 'en' ],
106 [ 'foo', [ 'bar' ], 'de' ]
107 ];
108 }
109
110 /**
111 * @covers Message::__construct
112 * @covers Message::getLanguage
113 * @dataProvider provideConstructorLanguage
114 */
115 public function testConstructorLanguage( $key, $params, $languageCode ) {
116 $language = Language::factory( $languageCode );
117 $message = new Message( $key, $params, $language );
118
119 $this->assertEquals( $language, $message->getLanguage() );
120 }
121
122 public static function provideKeys() {
123 return [
124 'string' => [
125 'key' => 'mainpage',
126 'expected' => [ 'mainpage' ],
127 ],
128 'single' => [
129 'key' => [ 'mainpage' ],
130 'expected' => [ 'mainpage' ],
131 ],
132 'multi' => [
133 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
134 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
135 ],
136 'empty' => [
137 'key' => [],
138 'expected' => null,
139 'exception' => 'InvalidArgumentException',
140 ],
141 'null' => [
142 'key' => null,
143 'expected' => null,
144 'exception' => 'InvalidArgumentException',
145 ],
146 'bad type' => [
147 'key' => 123,
148 'expected' => null,
149 'exception' => 'InvalidArgumentException',
150 ],
151 ];
152 }
153
154 /**
155 * @covers Message::__construct
156 * @covers Message::getKey
157 * @covers Message::isMultiKey
158 * @covers Message::getKeysToTry
159 * @dataProvider provideKeys
160 */
161 public function testKeys( $key, $expected, $exception = null ) {
162 if ( $exception ) {
163 $this->setExpectedException( $exception );
164 }
165
166 $msg = new Message( $key );
167 $this->assertContains( $msg->getKey(), $expected );
168 $this->assertSame( $expected, $msg->getKeysToTry() );
169 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
170 }
171
172 /**
173 * @covers ::wfMessage
174 */
175 public function testWfMessage() {
176 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
177 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
178 }
179
180 /**
181 * @covers Message::newFromKey
182 */
183 public function testNewFromKey() {
184 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
185 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
186 }
187
188 /**
189 * @covers ::wfMessage
190 * @covers Message::__construct
191 */
192 public function testWfMessageParams() {
193 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
194 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
195 $this->assertSame(
196 'You have foo (bar).',
197 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
198 );
199 $this->assertSame(
200 'You have foo (bar).',
201 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
202 );
203 }
204
205 /**
206 * @covers Message::exists
207 */
208 public function testExists() {
209 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
210 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
211 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
212 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
213 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
214 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
215 }
216
217 /**
218 * @covers Message::__construct
219 * @covers Message::text
220 * @covers Message::plain
221 * @covers Message::escaped
222 * @covers Message::toString
223 */
224 public function testToStringKey() {
225 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
226 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
227 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
228 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
229 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
230 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
231 $this->assertSame(
232 '⧼i&lt;dont&gt;exist-evar⧽',
233 wfMessage( 'i<dont>exist-evar' )->escaped()
234 );
235 }
236
237 public static function provideToString() {
238 return [
239 // key, transformation, transformed, transformed implicitly
240 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
241 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
242 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
243 [ 'script>alert(1)</script', 'escaped', '⧼script&gt;alert(1)&lt;/script⧽',
244 '⧼script&gt;alert(1)&lt;/script⧽' ],
245 [ 'script>alert(1)</script', 'plain', '⧼script&gt;alert(1)&lt;/script⧽',
246 '⧼script&gt;alert(1)&lt;/script⧽' ],
247 ];
248 }
249
250 /**
251 * @covers Message::toString
252 * @covers Message::__toString
253 * @dataProvider provideToString
254 */
255 public function testToString( $key, $format, $expect, $expectImplicit ) {
256 $msg = new Message( $key );
257 $this->assertSame( $expect, $msg->$format() );
258 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
259 $this->assertSame( $expectImplicit, $msg->__toString() );
260 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
261 }
262
263 public static function provideToString_raw() {
264 return [
265 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
266 [ '<span>foo</span>', 'escaped', '&lt;span&gt;foo&lt;/span&gt;',
267 '<span>foo</span>' ],
268 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
269 [ '<script>alert(1)</script>', 'parse', '&lt;script&gt;alert(1)&lt;/script&gt;',
270 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
271 [ '<script>alert(1)</script>', 'escaped', '&lt;script&gt;alert(1)&lt;/script&gt;',
272 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
273 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
274 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
275 ];
276 }
277
278 /**
279 * @covers Message::toString
280 * @covers Message::__toString
281 * @dataProvider provideToString_raw
282 */
283 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
284 // make the message behave like RawMessage and use the key as-is
285 $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] )
286 ->disableOriginalConstructor()
287 ->getMock();
288 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
289 /** @var Message $msg */
290 $this->assertSame( $expect, $msg->$format() );
291 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
292 $this->assertSame( $expectImplicit, $msg->__toString() );
293 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
294 }
295
296 /**
297 * @covers Message::inLanguage
298 */
299 public function testInLanguage() {
300 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
301 $this->assertSame( 'Заглавная страница',
302 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
303
304 // NOTE: make sure internal caching of the message text is reset appropriately
305 $msg = wfMessage( 'mainpage' );
306 $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
307 $this->assertSame(
308 'Заглавная страница',
309 $msg->inLanguage( Language::factory( 'ru' ) )->text()
310 );
311 }
312
313 /**
314 * @covers Message::rawParam
315 * @covers Message::rawParams
316 */
317 public function testRawParams() {
318 $this->assertSame(
319 '(Заглавная страница)',
320 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
321 );
322 $this->assertSame(
323 '(Заглавная страница $1)',
324 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
325 );
326 $this->assertSame(
327 '(Заглавная страница)',
328 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
329 );
330 $this->assertSame(
331 '(Заглавная страница $1)',
332 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
333 );
334 }
335
336 /**
337 * @covers RawMessage::__construct
338 * @covers RawMessage::fetchMessage
339 */
340 public function testRawMessage() {
341 $msg = new RawMessage( 'example &' );
342 $this->assertSame( 'example &', $msg->plain() );
343 $this->assertSame( 'example &amp;', $msg->escaped() );
344 }
345
346 /**
347 * @covers Message::params
348 * @covers Message::toString
349 * @covers Message::replaceParameters
350 */
351 public function testReplaceManyParams() {
352 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
353 // One less than above has placeholders
354 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
355 $this->assertSame(
356 'abcdefghijka2',
357 $msg->params( $params )->plain(),
358 'Params > 9 are replaced correctly'
359 );
360
361 $msg = new RawMessage( 'Params$*' );
362 $params = [ 'ab', 'bc', 'cd' ];
363 $this->assertSame(
364 'Params: ab, bc, cd',
365 $msg->params( $params )->text()
366 );
367 }
368
369 /**
370 * @covers Message::numParam
371 * @covers Message::numParams
372 */
373 public function testNumParams() {
374 $lang = Language::factory( 'en' );
375 $msg = new RawMessage( '$1' );
376
377 $this->assertSame(
378 $lang->formatNum( 123456.789 ),
379 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
380 'numParams is handled correctly'
381 );
382 }
383
384 /**
385 * @covers Message::durationParam
386 * @covers Message::durationParams
387 */
388 public function testDurationParams() {
389 $lang = Language::factory( 'en' );
390 $msg = new RawMessage( '$1' );
391
392 $this->assertSame(
393 $lang->formatDuration( 1234 ),
394 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
395 'durationParams is handled correctly'
396 );
397 }
398
399 /**
400 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
401 * @group Database
402 * @covers Message::expiryParam
403 * @covers Message::expiryParams
404 */
405 public function testExpiryParams() {
406 $lang = Language::factory( 'en' );
407 $msg = new RawMessage( '$1' );
408
409 $this->assertSame(
410 $lang->formatExpiry( wfTimestampNow() ),
411 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
412 'expiryParams is handled correctly'
413 );
414 }
415
416 /**
417 * @covers Message::timeperiodParam
418 * @covers Message::timeperiodParams
419 */
420 public function testTimeperiodParams() {
421 $lang = Language::factory( 'en' );
422 $msg = new RawMessage( '$1' );
423
424 $this->assertSame(
425 $lang->formatTimePeriod( 1234 ),
426 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
427 'timeperiodParams is handled correctly'
428 );
429 }
430
431 /**
432 * @covers Message::sizeParam
433 * @covers Message::sizeParams
434 */
435 public function testSizeParams() {
436 $lang = Language::factory( 'en' );
437 $msg = new RawMessage( '$1' );
438
439 $this->assertSame(
440 $lang->formatSize( 123456 ),
441 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
442 'sizeParams is handled correctly'
443 );
444 }
445
446 /**
447 * @covers Message::bitrateParam
448 * @covers Message::bitrateParams
449 */
450 public function testBitrateParams() {
451 $lang = Language::factory( 'en' );
452 $msg = new RawMessage( '$1' );
453
454 $this->assertSame(
455 $lang->formatBitrate( 123456 ),
456 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
457 'bitrateParams is handled correctly'
458 );
459 }
460
461 public static function providePlaintextParams() {
462 return [
463 [
464 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
465 'plain',
466 ],
467
468 [
469 // expect
470 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
471 // format
472 'text',
473 ],
474 [
475 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
476 'escaped',
477 ],
478
479 [
480 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
481 'parse',
482 ],
483
484 [
485 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
486 'parseAsBlock',
487 ],
488 ];
489 }
490
491 /**
492 * @covers Message::plaintextParam
493 * @covers Message::plaintextParams
494 * @covers Message::formatPlaintext
495 * @covers Message::toString
496 * @covers Message::parse
497 * @covers Message::parseAsBlock
498 * @dataProvider providePlaintextParams
499 */
500 public function testPlaintextParams( $expect, $format ) {
501 $lang = Language::factory( 'en' );
502
503 $msg = new RawMessage( '$1 $2' );
504 $params = [
505 'one $2',
506 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
507 ];
508 $this->assertSame(
509 $expect,
510 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
511 "Fail formatting for $format"
512 );
513 }
514
515 public static function provideListParam() {
516 $lang = Language::factory( 'de' );
517 $msg1 = new Message( 'mainpage', [], $lang );
518 $msg2 = new RawMessage( "''link''", [], $lang );
519
520 return [
521 'Simple comma list' => [
522 [ 'a', 'b', 'c' ],
523 'comma',
524 'text',
525 'a, b, c'
526 ],
527
528 'Simple semicolon list' => [
529 [ 'a', 'b', 'c' ],
530 'semicolon',
531 'text',
532 'a; b; c'
533 ],
534
535 'Simple pipe list' => [
536 [ 'a', 'b', 'c' ],
537 'pipe',
538 'text',
539 'a | b | c'
540 ],
541
542 'Simple text list' => [
543 [ 'a', 'b', 'c' ],
544 'text',
545 'text',
546 'a, b and c'
547 ],
548
549 'Empty list' => [
550 [],
551 'comma',
552 'text',
553 ''
554 ],
555
556 'List with all "before" params, ->text()' => [
557 [ "''link''", Message::numParam( 12345678 ) ],
558 'semicolon',
559 'text',
560 '\'\'link\'\'; 12,345,678'
561 ],
562
563 'List with all "before" params, ->parse()' => [
564 [ "''link''", Message::numParam( 12345678 ) ],
565 'semicolon',
566 'parse',
567 '<i>link</i>; 12,345,678'
568 ],
569
570 'List with all "after" params, ->text()' => [
571 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
572 'semicolon',
573 'text',
574 'Main Page; \'\'link\'\'; [[foo]]'
575 ],
576
577 'List with all "after" params, ->parse()' => [
578 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
579 'semicolon',
580 'parse',
581 'Main Page; <i>link</i>; [[foo]]'
582 ],
583
584 'List with both "before" and "after" params, ->text()' => [
585 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
586 'semicolon',
587 'text',
588 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
589 ],
590
591 'List with both "before" and "after" params, ->parse()' => [
592 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
593 'semicolon',
594 'parse',
595 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
596 ],
597 ];
598 }
599
600 /**
601 * @covers Message::listParam
602 * @covers Message::extractParam
603 * @covers Message::formatListParam
604 * @dataProvider provideListParam
605 */
606 public function testListParam( $list, $type, $format, $expect ) {
607 $lang = Language::factory( 'en' );
608
609 $msg = new RawMessage( '$1' );
610 $msg->params( [ Message::listParam( $list, $type ) ] );
611 $this->assertEquals(
612 $expect,
613 $msg->inLanguage( $lang )->$format()
614 );
615 }
616
617 /**
618 * @covers Message::extractParam
619 */
620 public function testMessageAsParam() {
621 $this->setMwGlobals( [
622 'wgScript' => '/wiki/index.php',
623 'wgArticlePath' => '/wiki/$1',
624 ] );
625
626 $msg = new Message( 'returnto', [
627 new Message( 'apihelp-link', [
628 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
629 ], Language::factory( 'de' ) )
630 ], Language::factory( 'es' ) );
631
632 $this->assertEquals(
633 'Volver a [[Special:ApiHelp/foo|Página principal]].',
634 $msg->text(),
635 'Process with ->text()'
636 );
637 $this->assertEquals(
638 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
639 . "principal</a>.\n</p>",
640 $msg->parseAsBlock(),
641 'Process with ->parseAsBlock()'
642 );
643 }
644
645 public static function provideParser() {
646 return [
647 [
648 "''&'' <x><!-- x -->",
649 'plain',
650 ],
651
652 [
653 "''&'' <x><!-- x -->",
654 'text',
655 ],
656 [
657 '<i>&amp;</i> &lt;x&gt;',
658 'parse',
659 ],
660
661 [
662 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
663 'parseAsBlock',
664 ],
665 ];
666 }
667
668 /**
669 * @covers Message::text
670 * @covers Message::parse
671 * @covers Message::parseAsBlock
672 * @covers Message::toString
673 * @covers Message::transformText
674 * @covers Message::parseText
675 * @dataProvider provideParser
676 */
677 public function testParser( $expect, $format ) {
678 $msg = new RawMessage( "''&'' <x><!-- x -->" );
679 $this->assertSame(
680 $expect,
681 $msg->inLanguage( 'en' )->$format()
682 );
683 }
684
685 /**
686 * @covers Message::inContentLanguage
687 */
688 public function testInContentLanguage() {
689 $this->setUserLang( 'fr' );
690
691 // NOTE: make sure internal caching of the message text is reset appropriately
692 $msg = wfMessage( 'mainpage' );
693 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
694 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
695 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
696 }
697
698 /**
699 * @covers Message::inContentLanguage
700 */
701 public function testInContentLanguageOverride() {
702 $this->setMwGlobals( [
703 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
704 ] );
705 $this->setUserLang( 'fr' );
706
707 // NOTE: make sure internal caching of the message text is reset appropriately.
708 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
709 $msg = wfMessage( 'mainpage' );
710 $this->assertSame(
711 'Accueil',
712 $msg->inContentLanguage()->plain(),
713 'inContentLanguage() with ForceUIMsg override enabled'
714 );
715 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
716 $this->assertSame(
717 'Main Page',
718 $msg->inContentLanguage()->plain(),
719 'inContentLanguage() with ForceUIMsg override enabled'
720 );
721 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
722 }
723
724 /**
725 * @expectedException MWException
726 * @covers Message::inLanguage
727 */
728 public function testInLanguageThrows() {
729 wfMessage( 'foo' )->inLanguage( 123 );
730 }
731
732 /**
733 * @covers Message::serialize
734 * @covers Message::unserialize
735 */
736 public function testSerialization() {
737 $msg = new Message( 'parentheses' );
738 $msg->rawParams( '<a>foo</a>' );
739 $msg->title( Title::newFromText( 'Testing' ) );
740 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
741 $msg = unserialize( serialize( $msg ) );
742 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
743 $title = TestingAccessWrapper::newFromObject( $msg )->title;
744 $this->assertInstanceOf( 'Title', $title );
745 $this->assertSame( 'Testing', $title->getFullText() );
746
747 $msg = new Message( 'mainpage' );
748 $msg->inLanguage( 'de' );
749 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
750 $msg = unserialize( serialize( $msg ) );
751 $this->assertSame( 'Hauptseite', $msg->plain() );
752 }
753
754 /**
755 * @covers Message::newFromSpecifier
756 * @dataProvider provideNewFromSpecifier
757 */
758 public function testNewFromSpecifier( $value, $expectedText ) {
759 $message = Message::newFromSpecifier( $value );
760 $this->assertInstanceOf( Message::class, $message );
761 if ( $value instanceof Message ) {
762 $this->assertInstanceOf( get_class( $value ), $message );
763 $this->assertEquals( $value, $message );
764 }
765 $this->assertSame( $expectedText, $message->text() );
766 }
767
768 public function provideNewFromSpecifier() {
769 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
770 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
771 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
772
773 return [
774 'string' => [ 'mainpage', 'Main Page' ],
775 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
776 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
777 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
778 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
779 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
780 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
781 ];
782 }
783 }