Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / tests / phpunit / includes / MessageTest.php
blob1de9c23a367d4fdd6a1372036eeea2ead176cb96
1 <?php
3 class MessageTest extends MediaWikiLangTestCase {
5 protected function setUp() {
6 parent::setUp();
8 $this->setMwGlobals( [
9 'wgForceUIMsgAsContentMsg' => [],
10 ] );
11 $this->setUserLang( 'en' );
14 /**
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $message = new Message( $key, $params, $language );
21 $this->assertSame( $key, $message->getKey() );
22 $this->assertSame( $params, $message->getParams() );
23 $this->assertEquals( $expectedLang, $message->getLanguage() );
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 );
32 $this->assertSame( $key, $message->getKey() );
33 $this->assertSame( $params, $message->getParams() );
34 $this->assertEquals( $expectedLang, $message->getLanguage() );
37 public static function provideConstructor() {
38 $langDe = Language::factory( 'de' );
39 $langEn = Language::factory( 'en' );
41 return [
42 [ $langDe, 'foo', [], $langDe ],
43 [ $langDe, 'foo', [ 'bar' ], $langDe ],
44 [ $langEn, 'foo', [ 'bar' ], null ]
48 public static function provideConstructorParams() {
49 return [
51 [],
52 [],
55 [],
56 [ [] ],
59 [ 'foo' ],
60 [ 'foo' ],
63 [ 'foo', 'bar' ],
64 [ 'foo', 'bar' ],
67 [ 'baz' ],
68 [ [ 'baz' ] ],
71 [ 'baz', 'foo' ],
72 [ [ 'baz', 'foo' ] ],
75 [ Message::rawParam( 'baz' ) ],
76 [ Message::rawParam( 'baz' ) ],
79 [ Message::rawParam( 'baz' ), 'foo' ],
80 [ Message::rawParam( 'baz' ), 'foo' ],
83 [ Message::rawParam( 'baz' ) ],
84 [ [ Message::rawParam( 'baz' ) ] ],
87 [ Message::rawParam( 'baz' ), 'foo' ],
88 [ [ Message::rawParam( 'baz' ), 'foo' ] ],
91 // Test handling of erroneous input, to detect if it changes
93 [ [ 'baz', 'foo' ], 'hhh' ],
94 [ [ 'baz', 'foo' ], 'hhh' ],
97 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
98 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
101 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
102 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
105 [ [ 'baz' ], [ 'ahahahahha' ] ],
106 [ [ 'baz' ], [ 'ahahahahha' ] ],
112 * @covers Message::__construct
113 * @covers Message::getParams
114 * @dataProvider provideConstructorParams
116 public function testConstructorParams( $expected, $args ) {
117 $msg = new Message( 'imasomething' );
119 $returned = call_user_func_array( [ $msg, 'params' ], $args );
121 $this->assertSame( $msg, $returned );
122 $this->assertSame( $expected, $msg->getParams() );
125 public static function provideConstructorLanguage() {
126 return [
127 [ 'foo', [ 'bar' ], 'en' ],
128 [ 'foo', [ 'bar' ], 'de' ]
133 * @covers Message::__construct
134 * @covers Message::getLanguage
135 * @dataProvider provideConstructorLanguage
137 public function testConstructorLanguage( $key, $params, $languageCode ) {
138 $language = Language::factory( $languageCode );
139 $message = new Message( $key, $params, $language );
141 $this->assertEquals( $language, $message->getLanguage() );
144 public static function provideKeys() {
145 return [
146 'string' => [
147 'key' => 'mainpage',
148 'expected' => [ 'mainpage' ],
150 'single' => [
151 'key' => [ 'mainpage' ],
152 'expected' => [ 'mainpage' ],
154 'multi' => [
155 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
156 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
158 'empty' => [
159 'key' => [],
160 'expected' => null,
161 'exception' => 'InvalidArgumentException',
163 'null' => [
164 'key' => null,
165 'expected' => null,
166 'exception' => 'InvalidArgumentException',
168 'bad type' => [
169 'key' => 123,
170 'expected' => null,
171 'exception' => 'InvalidArgumentException',
177 * @covers Message::__construct
178 * @covers Message::getKey
179 * @covers Message::isMultiKey
180 * @covers Message::getKeysToTry
181 * @dataProvider provideKeys
183 public function testKeys( $key, $expected, $exception = null ) {
184 if ( $exception ) {
185 $this->setExpectedException( $exception );
188 $msg = new Message( $key );
189 $this->assertContains( $msg->getKey(), $expected );
190 $this->assertSame( $expected, $msg->getKeysToTry() );
191 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
195 * @covers ::wfMessage
197 public function testWfMessage() {
198 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
199 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
203 * @covers Message::newFromKey
205 public function testNewFromKey() {
206 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
207 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
211 * @covers ::wfMessage
212 * @covers Message::__construct
214 public function testWfMessageParams() {
215 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
216 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
217 $this->assertSame(
218 'Return to 1,024.',
219 wfMessage( 'returnto', Message::numParam( 1024 ) )->text()
221 $this->assertSame(
222 'Return to 1,024.',
223 wfMessage( 'returnto', [ Message::numParam( 1024 ) ] )->text()
225 $this->assertSame(
226 'You have foo (bar).',
227 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
229 $this->assertSame(
230 'You have foo (bar).',
231 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
233 $this->assertSame(
234 'You have 1,024 (bar).',
235 wfMessage(
236 'youhavenewmessages',
237 Message::numParam( 1024 ), 'bar'
238 )->text()
240 $this->assertSame(
241 'You have foo (2,048).',
242 wfMessage(
243 'youhavenewmessages',
244 'foo', Message::numParam( 2048 )
245 )->text()
247 $this->assertSame(
248 'You have 1,024 (2,048).',
249 wfMessage(
250 'youhavenewmessages',
251 [ Message::numParam( 1024 ), Message::numParam( 2048 ) ]
252 )->text()
257 * @covers Message::exists
259 public function testExists() {
260 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
261 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
262 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
263 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
264 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
265 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
269 * @covers Message::__construct
270 * @covers Message::text
271 * @covers Message::plain
272 * @covers Message::escaped
273 * @covers Message::toString
275 public function testToStringKey() {
276 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
277 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
278 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
279 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
280 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
281 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
282 $this->assertSame(
283 '⧼i&lt;dont&gt;exist-evar⧽',
284 wfMessage( 'i<dont>exist-evar' )->escaped()
288 public static function provideToString() {
289 return [
290 // key, transformation, transformed, transformed implicitly
291 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
292 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
293 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
294 [ 'script>alert(1)</script', 'escaped', '⧼script&gt;alert(1)&lt;/script⧽',
295 '⧼script&gt;alert(1)&lt;/script⧽' ],
296 [ 'script>alert(1)</script', 'plain', '⧼script&gt;alert(1)&lt;/script⧽',
297 '⧼script&gt;alert(1)&lt;/script⧽' ],
302 * @covers Message::toString
303 * @covers Message::__toString
304 * @dataProvider provideToString
306 public function testToString( $key, $format, $expect, $expectImplicit ) {
307 $msg = new Message( $key );
308 $this->assertSame( $expect, $msg->$format() );
309 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
310 $this->assertSame( $expectImplicit, $msg->__toString() );
311 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
314 public static function provideToString_raw() {
315 return [
316 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
317 [ '<span>foo</span>', 'escaped', '&lt;span&gt;foo&lt;/span&gt;',
318 '<span>foo</span>' ],
319 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
320 [ '<script>alert(1)</script>', 'parse', '&lt;script&gt;alert(1)&lt;/script&gt;',
321 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
322 [ '<script>alert(1)</script>', 'escaped', '&lt;script&gt;alert(1)&lt;/script&gt;',
323 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
324 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
325 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
330 * @covers Message::toString
331 * @covers Message::__toString
332 * @dataProvider provideToString_raw
334 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
335 // make the message behave like RawMessage and use the key as-is
336 $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] )
337 ->disableOriginalConstructor()
338 ->getMock();
339 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
340 /** @var Message $msg */
341 $this->assertSame( $expect, $msg->$format() );
342 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
343 $this->assertSame( $expectImplicit, $msg->__toString() );
344 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
348 * @covers Message::inLanguage
350 public function testInLanguage() {
351 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
352 $this->assertSame( 'Заглавная страница',
353 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
355 // NOTE: make sure internal caching of the message text is reset appropriately
356 $msg = wfMessage( 'mainpage' );
357 $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
358 $this->assertSame(
359 'Заглавная страница',
360 $msg->inLanguage( Language::factory( 'ru' ) )->text()
365 * @covers Message::rawParam
366 * @covers Message::rawParams
368 public function testRawParams() {
369 $this->assertSame(
370 '(Заглавная страница)',
371 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
373 $this->assertSame(
374 '(Заглавная страница $1)',
375 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
377 $this->assertSame(
378 '(Заглавная страница)',
379 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
381 $this->assertSame(
382 '(Заглавная страница $1)',
383 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
388 * @covers RawMessage::__construct
389 * @covers RawMessage::fetchMessage
391 public function testRawMessage() {
392 $msg = new RawMessage( 'example &' );
393 $this->assertSame( 'example &', $msg->plain() );
394 $this->assertSame( 'example &amp;', $msg->escaped() );
398 * @covers Message::params
399 * @covers Message::toString
400 * @covers Message::replaceParameters
402 public function testReplaceManyParams() {
403 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
404 // One less than above has placeholders
405 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
406 $this->assertSame(
407 'abcdefghijka2',
408 $msg->params( $params )->plain(),
409 'Params > 9 are replaced correctly'
412 $msg = new RawMessage( 'Params$*' );
413 $params = [ 'ab', 'bc', 'cd' ];
414 $this->assertSame(
415 'Params: ab, bc, cd',
416 $msg->params( $params )->text()
421 * @covers Message::numParam
422 * @covers Message::numParams
424 public function testNumParams() {
425 $lang = Language::factory( 'en' );
426 $msg = new RawMessage( '$1' );
428 $this->assertSame(
429 $lang->formatNum( 123456.789 ),
430 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
431 'numParams is handled correctly'
436 * @covers Message::durationParam
437 * @covers Message::durationParams
439 public function testDurationParams() {
440 $lang = Language::factory( 'en' );
441 $msg = new RawMessage( '$1' );
443 $this->assertSame(
444 $lang->formatDuration( 1234 ),
445 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
446 'durationParams is handled correctly'
451 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
452 * @group Database
453 * @covers Message::expiryParam
454 * @covers Message::expiryParams
456 public function testExpiryParams() {
457 $lang = Language::factory( 'en' );
458 $msg = new RawMessage( '$1' );
460 $this->assertSame(
461 $lang->formatExpiry( wfTimestampNow() ),
462 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
463 'expiryParams is handled correctly'
468 * @covers Message::timeperiodParam
469 * @covers Message::timeperiodParams
471 public function testTimeperiodParams() {
472 $lang = Language::factory( 'en' );
473 $msg = new RawMessage( '$1' );
475 $this->assertSame(
476 $lang->formatTimePeriod( 1234 ),
477 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
478 'timeperiodParams is handled correctly'
483 * @covers Message::sizeParam
484 * @covers Message::sizeParams
486 public function testSizeParams() {
487 $lang = Language::factory( 'en' );
488 $msg = new RawMessage( '$1' );
490 $this->assertSame(
491 $lang->formatSize( 123456 ),
492 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
493 'sizeParams is handled correctly'
498 * @covers Message::bitrateParam
499 * @covers Message::bitrateParams
501 public function testBitrateParams() {
502 $lang = Language::factory( 'en' );
503 $msg = new RawMessage( '$1' );
505 $this->assertSame(
506 $lang->formatBitrate( 123456 ),
507 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
508 'bitrateParams is handled correctly'
512 public static function providePlaintextParams() {
513 return [
515 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
516 'plain',
520 // expect
521 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
522 // format
523 'text',
526 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
527 'escaped',
531 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
532 'parse',
536 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
537 'parseAsBlock',
543 * @covers Message::plaintextParam
544 * @covers Message::plaintextParams
545 * @covers Message::formatPlaintext
546 * @covers Message::toString
547 * @covers Message::parse
548 * @covers Message::parseAsBlock
549 * @dataProvider providePlaintextParams
551 public function testPlaintextParams( $expect, $format ) {
552 $lang = Language::factory( 'en' );
554 $msg = new RawMessage( '$1 $2' );
555 $params = [
556 'one $2',
557 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
559 $this->assertSame(
560 $expect,
561 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
562 "Fail formatting for $format"
566 public static function provideListParam() {
567 $lang = Language::factory( 'de' );
568 $msg1 = new Message( 'mainpage', [], $lang );
569 $msg2 = new RawMessage( "''link''", [], $lang );
571 return [
572 'Simple comma list' => [
573 [ 'a', 'b', 'c' ],
574 'comma',
575 'text',
576 'a, b, c'
579 'Simple semicolon list' => [
580 [ 'a', 'b', 'c' ],
581 'semicolon',
582 'text',
583 'a; b; c'
586 'Simple pipe list' => [
587 [ 'a', 'b', 'c' ],
588 'pipe',
589 'text',
590 'a | b | c'
593 'Simple text list' => [
594 [ 'a', 'b', 'c' ],
595 'text',
596 'text',
597 'a, b and c'
600 'Empty list' => [
602 'comma',
603 'text',
607 'List with all "before" params, ->text()' => [
608 [ "''link''", Message::numParam( 12345678 ) ],
609 'semicolon',
610 'text',
611 '\'\'link\'\'; 12,345,678'
614 'List with all "before" params, ->parse()' => [
615 [ "''link''", Message::numParam( 12345678 ) ],
616 'semicolon',
617 'parse',
618 '<i>link</i>; 12,345,678'
621 'List with all "after" params, ->text()' => [
622 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
623 'semicolon',
624 'text',
625 'Main Page; \'\'link\'\'; [[foo]]'
628 'List with all "after" params, ->parse()' => [
629 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
630 'semicolon',
631 'parse',
632 'Main Page; <i>link</i>; [[foo]]'
635 'List with both "before" and "after" params, ->text()' => [
636 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
637 'semicolon',
638 'text',
639 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
642 'List with both "before" and "after" params, ->parse()' => [
643 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
644 'semicolon',
645 'parse',
646 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
652 * @covers Message::listParam
653 * @covers Message::extractParam
654 * @covers Message::formatListParam
655 * @dataProvider provideListParam
657 public function testListParam( $list, $type, $format, $expect ) {
658 $lang = Language::factory( 'en' );
660 $msg = new RawMessage( '$1' );
661 $msg->params( [ Message::listParam( $list, $type ) ] );
662 $this->assertEquals(
663 $expect,
664 $msg->inLanguage( $lang )->$format()
669 * @covers Message::extractParam
671 public function testMessageAsParam() {
672 $this->setMwGlobals( [
673 'wgScript' => '/wiki/index.php',
674 'wgArticlePath' => '/wiki/$1',
675 ] );
677 $msg = new Message( 'returnto', [
678 new Message( 'apihelp-link', [
679 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
680 ], Language::factory( 'de' ) )
681 ], Language::factory( 'es' ) );
683 $this->assertEquals(
684 'Volver a [[Special:ApiHelp/foo|Página principal]].',
685 $msg->text(),
686 'Process with ->text()'
688 $this->assertEquals(
689 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
690 . "principal</a>.\n</p>",
691 $msg->parseAsBlock(),
692 'Process with ->parseAsBlock()'
696 public static function provideParser() {
697 return [
699 "''&'' <x><!-- x -->",
700 'plain',
704 "''&'' <x><!-- x -->",
705 'text',
708 '<i>&amp;</i> &lt;x&gt;',
709 'parse',
713 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
714 'parseAsBlock',
720 * @covers Message::text
721 * @covers Message::parse
722 * @covers Message::parseAsBlock
723 * @covers Message::toString
724 * @covers Message::transformText
725 * @covers Message::parseText
726 * @dataProvider provideParser
728 public function testParser( $expect, $format ) {
729 $msg = new RawMessage( "''&'' <x><!-- x -->" );
730 $this->assertSame(
731 $expect,
732 $msg->inLanguage( 'en' )->$format()
737 * @covers Message::inContentLanguage
739 public function testInContentLanguage() {
740 $this->setUserLang( 'fr' );
742 // NOTE: make sure internal caching of the message text is reset appropriately
743 $msg = wfMessage( 'mainpage' );
744 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
745 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
746 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
750 * @covers Message::inContentLanguage
752 public function testInContentLanguageOverride() {
753 $this->setMwGlobals( [
754 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
755 ] );
756 $this->setUserLang( 'fr' );
758 // NOTE: make sure internal caching of the message text is reset appropriately.
759 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
760 $msg = wfMessage( 'mainpage' );
761 $this->assertSame(
762 'Accueil',
763 $msg->inContentLanguage()->plain(),
764 'inContentLanguage() with ForceUIMsg override enabled'
766 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
767 $this->assertSame(
768 'Main Page',
769 $msg->inContentLanguage()->plain(),
770 'inContentLanguage() with ForceUIMsg override enabled'
772 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
776 * @expectedException MWException
777 * @covers Message::inLanguage
779 public function testInLanguageThrows() {
780 wfMessage( 'foo' )->inLanguage( 123 );
784 * @covers Message::serialize
785 * @covers Message::unserialize
787 public function testSerialization() {
788 $msg = new Message( 'parentheses' );
789 $msg->rawParams( '<a>foo</a>' );
790 $msg->title( Title::newFromText( 'Testing' ) );
791 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
792 $msg = unserialize( serialize( $msg ) );
793 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
794 $title = TestingAccessWrapper::newFromObject( $msg )->title;
795 $this->assertInstanceOf( 'Title', $title );
796 $this->assertSame( 'Testing', $title->getFullText() );
798 $msg = new Message( 'mainpage' );
799 $msg->inLanguage( 'de' );
800 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
801 $msg = unserialize( serialize( $msg ) );
802 $this->assertSame( 'Hauptseite', $msg->plain() );
806 * @covers Message::newFromSpecifier
807 * @dataProvider provideNewFromSpecifier
809 public function testNewFromSpecifier( $value, $expectedText ) {
810 $message = Message::newFromSpecifier( $value );
811 $this->assertInstanceOf( Message::class, $message );
812 if ( $value instanceof Message ) {
813 $this->assertInstanceOf( get_class( $value ), $message );
814 $this->assertEquals( $value, $message );
816 $this->assertSame( $expectedText, $message->text() );
819 public function provideNewFromSpecifier() {
820 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
821 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
822 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
824 return [
825 'string' => [ 'mainpage', 'Main Page' ],
826 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
827 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
828 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
829 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
830 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
831 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],