3 class MessageTest
extends MediaWikiLangTestCase
{
5 protected function setUp() {
9 'wgForceUIMsgAsContentMsg' => [],
11 $this->setUserLang( 'en' );
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' );
42 [ $langDe, 'foo', [], $langDe ],
43 [ $langDe, 'foo', [ 'bar' ], $langDe ],
44 [ $langEn, 'foo', [ 'bar' ], null ]
48 public static function provideConstructorParams() {
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() {
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() {
148 'expected' => [ 'mainpage' ],
151 'key' => [ 'mainpage' ],
152 'expected' => [ 'mainpage' ],
155 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
156 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
161 'exception' => 'InvalidArgumentException',
166 'exception' => 'InvalidArgumentException',
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 ) {
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() );
219 wfMessage( 'returnto', Message
::numParam( 1024 ) )->text()
223 wfMessage( 'returnto', [ Message
::numParam( 1024 ) ] )->text()
226 'You have foo (bar).',
227 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
230 'You have foo (bar).',
231 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
234 'You have 1,024 (bar).',
236 'youhavenewmessages',
237 Message
::numParam( 1024 ), 'bar'
241 'You have foo (2,048).',
243 'youhavenewmessages',
244 'foo', Message
::numParam( 2048 )
248 'You have 1,024 (2,048).',
250 'youhavenewmessages',
251 [ Message
::numParam( 1024 ), Message
::numParam( 2048 ) ]
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<dont>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<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
281 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
283 '⧼i<dont>exist-evar⧽',
284 wfMessage( 'i<dont>exist-evar' )->escaped()
288 public static function provideToString() {
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>alert(1)</script⧽',
295 '⧼script>alert(1)</script⧽' ],
296 [ 'script>alert(1)</script', 'plain', '⧼script>alert(1)</script⧽',
297 '⧼script>alert(1)</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() {
316 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
317 [ '<span>foo</span>', 'escaped', '<span>foo</span>',
318 '<span>foo</span>' ],
319 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
320 [ '<script>alert(1)</script>', 'parse', '<script>alert(1)</script>',
321 '<script>alert(1)</script>' ],
322 [ '<script>alert(1)</script>', 'escaped', '<script>alert(1)</script>',
323 '<script>alert(1)</script>' ],
324 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
325 '<script>alert(1)</script>' ],
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()
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() );
359 'Заглавная страница',
360 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
365 * @covers Message::rawParam
366 * @covers Message::rawParams
368 public function testRawParams() {
370 '(Заглавная страница)',
371 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
374 '(Заглавная страница $1)',
375 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
378 '(Заглавная страница)',
379 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
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 &', $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' ];
408 $msg->params( $params )->plain(),
409 'Params > 9 are replaced correctly'
412 $msg = new RawMessage( 'Params$*' );
413 $params = [ 'ab', 'bc', 'cd' ];
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' );
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' );
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)
453 * @covers Message::expiryParam
454 * @covers Message::expiryParams
456 public function testExpiryParams() {
457 $lang = Language
::factory( 'en' );
458 $msg = new RawMessage( '$1' );
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' );
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' );
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' );
506 $lang->formatBitrate( 123456 ),
507 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
508 'bitrateParams is handled correctly'
512 public static function providePlaintextParams() {
515 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
521 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
526 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
531 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
536 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
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' );
557 '<div>foo</div> [[Bar]] {{Baz}} <',
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 );
572 'Simple comma list' => [
579 'Simple semicolon list' => [
586 'Simple pipe list' => [
593 'Simple text list' => [
607 'List with all "before" params, ->text()' => [
608 [ "''link''", Message
::numParam( 12345678 ) ],
611 '\'\'link\'\'; 12,345,678'
614 'List with all "before" params, ->parse()' => [
615 [ "''link''", Message
::numParam( 12345678 ) ],
618 '<i>link</i>; 12,345,678'
621 'List with all "after" params, ->text()' => [
622 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
625 'Main Page; \'\'link\'\'; [[foo]]'
628 'List with all "after" params, ->parse()' => [
629 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
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 ) ],
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 ) ],
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 ) ] );
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',
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' ) );
684 'Volver a [[Special:ApiHelp/foo|Página principal]].',
686 'Process with ->text()'
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() {
699 "''&'' <x><!-- x -->",
704 "''&'' <x><!-- x -->",
708 '<i>&</i> <x>',
713 "<p><i>&</i> <x>\n</p>",
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 -->" );
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' ],
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' );
763 $msg->inContentLanguage()->plain(),
764 'inContentLanguage() with ForceUIMsg override enabled'
766 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
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( [] );
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)' ],