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() );
218 'You have foo (bar).',
219 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
222 'You have foo (bar).',
223 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
228 * @covers Message::exists
230 public function testExists() {
231 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
232 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
233 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
234 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
235 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
236 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
240 * @covers Message::__construct
241 * @covers Message::text
242 * @covers Message::plain
243 * @covers Message::escaped
244 * @covers Message::toString
246 public function testToStringKey() {
247 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
248 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
249 $this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
250 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
251 $this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
252 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
254 '⧼i<dont>exist-evar⧽',
255 wfMessage( 'i<dont>exist-evar' )->escaped()
259 public static function provideToString() {
261 // key, transformation, transformed, transformed implicitly
262 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
263 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
264 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
265 [ 'script>alert(1)</script', 'escaped', '⧼script>alert(1)</script⧽',
266 '⧼script>alert(1)</script⧽' ],
267 [ 'script>alert(1)</script', 'plain', '⧼script>alert(1)</script⧽',
268 '⧼script>alert(1)</script⧽' ],
273 * @covers Message::toString
274 * @covers Message::__toString
275 * @dataProvider provideToString
277 public function testToString( $key, $format, $expect, $expectImplicit ) {
278 $msg = new Message( $key );
279 $this->assertSame( $expect, $msg->$format() );
280 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
281 $this->assertSame( $expectImplicit, $msg->__toString() );
282 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
285 public static function provideToString_raw() {
287 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
288 [ '<span>foo</span>', 'escaped', '<span>foo</span>',
289 '<span>foo</span>' ],
290 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
291 [ '<script>alert(1)</script>', 'parse', '<script>alert(1)</script>',
292 '<script>alert(1)</script>' ],
293 [ '<script>alert(1)</script>', 'escaped', '<script>alert(1)</script>',
294 '<script>alert(1)</script>' ],
295 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
296 '<script>alert(1)</script>' ],
301 * @covers Message::toString
302 * @covers Message::__toString
303 * @dataProvider provideToString_raw
305 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
306 // make the message behave like RawMessage and use the key as-is
307 $msg = $this->getMockBuilder( Message
::class )->setMethods( [ 'fetchMessage' ] )
308 ->disableOriginalConstructor()
310 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
311 /** @var Message $msg */
312 $this->assertSame( $expect, $msg->$format() );
313 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
314 $this->assertSame( $expectImplicit, $msg->__toString() );
315 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
319 * @covers Message::inLanguage
321 public function testInLanguage() {
322 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
323 $this->assertSame( 'Заглавная страница',
324 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
326 // NOTE: make sure internal caching of the message text is reset appropriately
327 $msg = wfMessage( 'mainpage' );
328 $this->assertSame( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
330 'Заглавная страница',
331 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
336 * @covers Message::rawParam
337 * @covers Message::rawParams
339 public function testRawParams() {
341 '(Заглавная страница)',
342 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
345 '(Заглавная страница $1)',
346 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
349 '(Заглавная страница)',
350 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
353 '(Заглавная страница $1)',
354 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
359 * @covers RawMessage::__construct
360 * @covers RawMessage::fetchMessage
362 public function testRawMessage() {
363 $msg = new RawMessage( 'example &' );
364 $this->assertSame( 'example &', $msg->plain() );
365 $this->assertSame( 'example &', $msg->escaped() );
369 * @covers Message::params
370 * @covers Message::toString
371 * @covers Message::replaceParameters
373 public function testReplaceManyParams() {
374 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
375 // One less than above has placeholders
376 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
379 $msg->params( $params )->plain(),
380 'Params > 9 are replaced correctly'
383 $msg = new RawMessage( 'Params$*' );
384 $params = [ 'ab', 'bc', 'cd' ];
386 'Params: ab, bc, cd',
387 $msg->params( $params )->text()
392 * @covers Message::numParam
393 * @covers Message::numParams
395 public function testNumParams() {
396 $lang = Language
::factory( 'en' );
397 $msg = new RawMessage( '$1' );
400 $lang->formatNum( 123456.789 ),
401 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
402 'numParams is handled correctly'
407 * @covers Message::durationParam
408 * @covers Message::durationParams
410 public function testDurationParams() {
411 $lang = Language
::factory( 'en' );
412 $msg = new RawMessage( '$1' );
415 $lang->formatDuration( 1234 ),
416 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
417 'durationParams is handled correctly'
422 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
424 * @covers Message::expiryParam
425 * @covers Message::expiryParams
427 public function testExpiryParams() {
428 $lang = Language
::factory( 'en' );
429 $msg = new RawMessage( '$1' );
432 $lang->formatExpiry( wfTimestampNow() ),
433 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
434 'expiryParams is handled correctly'
439 * @covers Message::timeperiodParam
440 * @covers Message::timeperiodParams
442 public function testTimeperiodParams() {
443 $lang = Language
::factory( 'en' );
444 $msg = new RawMessage( '$1' );
447 $lang->formatTimePeriod( 1234 ),
448 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
449 'timeperiodParams is handled correctly'
454 * @covers Message::sizeParam
455 * @covers Message::sizeParams
457 public function testSizeParams() {
458 $lang = Language
::factory( 'en' );
459 $msg = new RawMessage( '$1' );
462 $lang->formatSize( 123456 ),
463 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
464 'sizeParams is handled correctly'
469 * @covers Message::bitrateParam
470 * @covers Message::bitrateParams
472 public function testBitrateParams() {
473 $lang = Language
::factory( 'en' );
474 $msg = new RawMessage( '$1' );
477 $lang->formatBitrate( 123456 ),
478 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
479 'bitrateParams is handled correctly'
483 public static function providePlaintextParams() {
486 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
492 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
497 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
502 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
507 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
514 * @covers Message::plaintextParam
515 * @covers Message::plaintextParams
516 * @covers Message::formatPlaintext
517 * @covers Message::toString
518 * @covers Message::parse
519 * @covers Message::parseAsBlock
520 * @dataProvider providePlaintextParams
522 public function testPlaintextParams( $expect, $format ) {
523 $lang = Language
::factory( 'en' );
525 $msg = new RawMessage( '$1 $2' );
528 '<div>foo</div> [[Bar]] {{Baz}} <',
532 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
533 "Fail formatting for $format"
537 public static function provideListParam() {
538 $lang = Language
::factory( 'de' );
539 $msg1 = new Message( 'mainpage', [], $lang );
540 $msg2 = new RawMessage( "''link''", [], $lang );
543 'Simple comma list' => [
550 'Simple semicolon list' => [
557 'Simple pipe list' => [
564 'Simple text list' => [
578 'List with all "before" params, ->text()' => [
579 [ "''link''", Message
::numParam( 12345678 ) ],
582 '\'\'link\'\'; 12,345,678'
585 'List with all "before" params, ->parse()' => [
586 [ "''link''", Message
::numParam( 12345678 ) ],
589 '<i>link</i>; 12,345,678'
592 'List with all "after" params, ->text()' => [
593 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
596 'Main Page; \'\'link\'\'; [[foo]]'
599 'List with all "after" params, ->parse()' => [
600 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
603 'Main Page; <i>link</i>; [[foo]]'
606 'List with both "before" and "after" params, ->text()' => [
607 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
610 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
613 'List with both "before" and "after" params, ->parse()' => [
614 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
617 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
623 * @covers Message::listParam
624 * @covers Message::extractParam
625 * @covers Message::formatListParam
626 * @dataProvider provideListParam
628 public function testListParam( $list, $type, $format, $expect ) {
629 $lang = Language
::factory( 'en' );
631 $msg = new RawMessage( '$1' );
632 $msg->params( [ Message
::listParam( $list, $type ) ] );
635 $msg->inLanguage( $lang )->$format()
640 * @covers Message::extractParam
642 public function testMessageAsParam() {
643 $this->setMwGlobals( [
644 'wgScript' => '/wiki/index.php',
645 'wgArticlePath' => '/wiki/$1',
648 $msg = new Message( 'returnto', [
649 new Message( 'apihelp-link', [
650 'foo', new Message( 'mainpage', [], Language
::factory( 'en' ) )
651 ], Language
::factory( 'de' ) )
652 ], Language
::factory( 'es' ) );
655 'Volver a [[Special:ApiHelp/foo|Página principal]].',
657 'Process with ->text()'
660 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
661 . "principal</a>.\n</p>",
662 $msg->parseAsBlock(),
663 'Process with ->parseAsBlock()'
667 public static function provideParser() {
670 "''&'' <x><!-- x -->",
675 "''&'' <x><!-- x -->",
679 '<i>&</i> <x>',
684 "<p><i>&</i> <x>\n</p>",
691 * @covers Message::text
692 * @covers Message::parse
693 * @covers Message::parseAsBlock
694 * @covers Message::toString
695 * @covers Message::transformText
696 * @covers Message::parseText
697 * @dataProvider provideParser
699 public function testParser( $expect, $format ) {
700 $msg = new RawMessage( "''&'' <x><!-- x -->" );
703 $msg->inLanguage( 'en' )->$format()
708 * @covers Message::inContentLanguage
710 public function testInContentLanguage() {
711 $this->setUserLang( 'fr' );
713 // NOTE: make sure internal caching of the message text is reset appropriately
714 $msg = wfMessage( 'mainpage' );
715 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
716 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
717 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
721 * @covers Message::inContentLanguage
723 public function testInContentLanguageOverride() {
724 $this->setMwGlobals( [
725 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
727 $this->setUserLang( 'fr' );
729 // NOTE: make sure internal caching of the message text is reset appropriately.
730 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
731 $msg = wfMessage( 'mainpage' );
734 $msg->inContentLanguage()->plain(),
735 'inContentLanguage() with ForceUIMsg override enabled'
737 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
740 $msg->inContentLanguage()->plain(),
741 'inContentLanguage() with ForceUIMsg override enabled'
743 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
747 * @expectedException MWException
748 * @covers Message::inLanguage
750 public function testInLanguageThrows() {
751 wfMessage( 'foo' )->inLanguage( 123 );
755 * @covers Message::serialize
756 * @covers Message::unserialize
758 public function testSerialization() {
759 $msg = new Message( 'parentheses' );
760 $msg->rawParams( '<a>foo</a>' );
761 $msg->title( Title
::newFromText( 'Testing' ) );
762 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
763 $msg = unserialize( serialize( $msg ) );
764 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
765 $title = TestingAccessWrapper
::newFromObject( $msg )->title
;
766 $this->assertInstanceOf( 'Title', $title );
767 $this->assertSame( 'Testing', $title->getFullText() );
769 $msg = new Message( 'mainpage' );
770 $msg->inLanguage( 'de' );
771 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
772 $msg = unserialize( serialize( $msg ) );
773 $this->assertSame( 'Hauptseite', $msg->plain() );
777 * @covers Message::newFromSpecifier
778 * @dataProvider provideNewFromSpecifier
780 public function testNewFromSpecifier( $value, $expectedText ) {
781 $message = Message
::newFromSpecifier( $value );
782 $this->assertInstanceOf( Message
::class, $message );
783 if ( $value instanceof Message
) {
784 $this->assertInstanceOf( get_class( $value ), $message );
785 $this->assertEquals( $value, $message );
787 $this->assertSame( $expectedText, $message->text() );
790 public function provideNewFromSpecifier() {
791 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier
::class );
792 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
793 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
796 'string' => [ 'mainpage', 'Main Page' ],
797 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
798 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
799 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
800 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
801 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
802 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],