2 use MediaWiki\MediaWikiServices
;
4 class MessageTest
extends MediaWikiLangTestCase
{
6 protected function setUp() {
10 'wgForceUIMsgAsContentMsg' => [],
12 $this->setUserLang( 'en' );
16 * @covers Message::__construct
17 * @dataProvider provideConstructor
19 public function testConstructor( $expectedLang, $key, $params, $language ) {
20 $message = new Message( $key, $params, $language );
22 $this->assertSame( $key, $message->getKey() );
23 $this->assertSame( $params, $message->getParams() );
24 $this->assertEquals( $expectedLang, $message->getLanguage() );
26 $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
27 $messageSpecifier->expects( $this->any() )
28 ->method( 'getKey' )->will( $this->returnValue( $key ) );
29 $messageSpecifier->expects( $this->any() )
30 ->method( 'getParams' )->will( $this->returnValue( $params ) );
31 $message = new Message( $messageSpecifier, [], $language );
33 $this->assertSame( $key, $message->getKey() );
34 $this->assertSame( $params, $message->getParams() );
35 $this->assertEquals( $expectedLang, $message->getLanguage() );
38 public static function provideConstructor() {
39 $langDe = Language
::factory( 'de' );
40 $langEn = Language
::factory( 'en' );
43 [ $langDe, 'foo', [], $langDe ],
44 [ $langDe, 'foo', [ 'bar' ], $langDe ],
45 [ $langEn, 'foo', [ 'bar' ], null ]
49 public static function provideConstructorParams() {
76 [ Message
::rawParam( 'baz' ) ],
77 [ Message
::rawParam( 'baz' ) ],
80 [ Message
::rawParam( 'baz' ), 'foo' ],
81 [ Message
::rawParam( 'baz' ), 'foo' ],
84 [ Message
::rawParam( 'baz' ) ],
85 [ [ Message
::rawParam( 'baz' ) ] ],
88 [ Message
::rawParam( 'baz' ), 'foo' ],
89 [ [ Message
::rawParam( 'baz' ), 'foo' ] ],
92 // Test handling of erroneous input, to detect if it changes
94 [ [ 'baz', 'foo' ], 'hhh' ],
95 [ [ 'baz', 'foo' ], 'hhh' ],
98 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
99 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
102 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
103 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
106 [ [ 'baz' ], [ 'ahahahahha' ] ],
107 [ [ 'baz' ], [ 'ahahahahha' ] ],
113 * @covers Message::__construct
114 * @covers Message::getParams
115 * @dataProvider provideConstructorParams
117 public function testConstructorParams( $expected, $args ) {
118 $msg = new Message( 'imasomething' );
120 $returned = call_user_func_array( [ $msg, 'params' ], $args );
122 $this->assertSame( $msg, $returned );
123 $this->assertSame( $expected, $msg->getParams() );
126 public static function provideConstructorLanguage() {
128 [ 'foo', [ 'bar' ], 'en' ],
129 [ 'foo', [ 'bar' ], 'de' ]
134 * @covers Message::__construct
135 * @covers Message::getLanguage
136 * @dataProvider provideConstructorLanguage
138 public function testConstructorLanguage( $key, $params, $languageCode ) {
139 $language = Language
::factory( $languageCode );
140 $message = new Message( $key, $params, $language );
142 $this->assertEquals( $language, $message->getLanguage() );
145 public static function provideKeys() {
149 'expected' => [ 'mainpage' ],
152 'key' => [ 'mainpage' ],
153 'expected' => [ 'mainpage' ],
156 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
157 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
162 'exception' => 'InvalidArgumentException',
167 'exception' => 'InvalidArgumentException',
172 'exception' => 'InvalidArgumentException',
178 * @covers Message::__construct
179 * @covers Message::getKey
180 * @covers Message::isMultiKey
181 * @covers Message::getKeysToTry
182 * @dataProvider provideKeys
184 public function testKeys( $key, $expected, $exception = null ) {
186 $this->setExpectedException( $exception );
189 $msg = new Message( $key );
190 $this->assertContains( $msg->getKey(), $expected );
191 $this->assertSame( $expected, $msg->getKeysToTry() );
192 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
196 * @covers ::wfMessage
198 public function testWfMessage() {
199 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
200 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
204 * @covers Message::newFromKey
206 public function testNewFromKey() {
207 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'mainpage' ) );
208 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'i-dont-exist-evar' ) );
212 * @covers ::wfMessage
213 * @covers Message::__construct
215 public function testWfMessageParams() {
216 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
217 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
220 wfMessage( 'returnto', Message
::numParam( 1024 ) )->text()
224 wfMessage( 'returnto', [ Message
::numParam( 1024 ) ] )->text()
227 'You have foo (bar).',
228 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
231 'You have foo (bar).',
232 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
235 'You have 1,024 (bar).',
237 'youhavenewmessages',
238 Message
::numParam( 1024 ), 'bar'
242 'You have foo (2,048).',
244 'youhavenewmessages',
245 'foo', Message
::numParam( 2048 )
249 'You have 1,024 (2,048).',
251 'youhavenewmessages',
252 [ Message
::numParam( 1024 ), Message
::numParam( 2048 ) ]
258 * @covers Message::exists
260 public function testExists() {
261 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
262 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
263 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
264 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
265 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
266 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
270 * @covers Message::__construct
271 * @covers Message::text
272 * @covers Message::plain
273 * @covers Message::escaped
274 * @covers Message::toString
276 public function testToStringKey() {
277 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->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' )->text() );
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' )->plain() );
282 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
284 '⧼i<dont>exist-evar⧽',
285 wfMessage( 'i<dont>exist-evar' )->escaped()
289 public static function provideToString() {
291 // key, transformation, transformed, transformed implicitly
292 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
293 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
294 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
295 [ 'script>alert(1)</script', 'escaped', '⧼script>alert(1)</script⧽',
296 '⧼script>alert(1)</script⧽' ],
297 [ 'script>alert(1)</script', 'plain', '⧼script>alert(1)</script⧽',
298 '⧼script>alert(1)</script⧽' ],
303 * @covers Message::toString
304 * @covers Message::__toString
305 * @dataProvider provideToString
307 public function testToString( $key, $format, $expect, $expectImplicit ) {
308 $msg = new Message( $key );
309 $this->assertSame( $expect, $msg->$format() );
310 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
311 $this->assertSame( $expectImplicit, $msg->__toString() );
312 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
315 public static function provideToString_raw() {
317 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
318 [ '<span>foo</span>', 'escaped', '<span>foo</span>',
319 '<span>foo</span>' ],
320 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
321 [ '<script>alert(1)</script>', 'parse', '<script>alert(1)</script>',
322 '<script>alert(1)</script>' ],
323 [ '<script>alert(1)</script>', 'escaped', '<script>alert(1)</script>',
324 '<script>alert(1)</script>' ],
325 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
326 '<script>alert(1)</script>' ],
331 * @covers Message::toString
332 * @covers Message::__toString
333 * @dataProvider provideToString_raw
335 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
336 // make the message behave like RawMessage and use the key as-is
337 $msg = $this->getMockBuilder( Message
::class )->setMethods( [ 'fetchMessage' ] )
338 ->disableOriginalConstructor()
340 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
341 /** @var Message $msg */
342 $this->assertSame( $expect, $msg->$format() );
343 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
344 $this->assertSame( $expectImplicit, $msg->__toString() );
345 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
349 * @covers Message::inLanguage
351 public function testInLanguage() {
352 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
353 $this->assertSame( 'Заглавная страница',
354 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
356 // NOTE: make sure internal caching of the message text is reset appropriately
357 $msg = wfMessage( 'mainpage' );
358 $this->assertSame( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
360 'Заглавная страница',
361 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
366 * @covers Message::rawParam
367 * @covers Message::rawParams
369 public function testRawParams() {
371 '(Заглавная страница)',
372 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
375 '(Заглавная страница $1)',
376 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
379 '(Заглавная страница)',
380 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
383 '(Заглавная страница $1)',
384 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
389 * @covers RawMessage::__construct
390 * @covers RawMessage::fetchMessage
392 public function testRawMessage() {
393 $msg = new RawMessage( 'example &' );
394 $this->assertSame( 'example &', $msg->plain() );
395 $this->assertSame( 'example &', $msg->escaped() );
398 public function testRawHtmlInMsg() {
399 global $wgParserConf;
400 $this->setMwGlobals( 'wgRawHtml', true );
401 // We have to reset the core hook registration.
402 // to register the html hook
403 MessageCache
::destroyInstance();
404 $this->setMwGlobals( 'wgParser',
405 ObjectFactory
::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
408 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
409 $txt = '<span class="error"><html> tags cannot be' .
410 ' used outside of normal pages.</span>';
411 $this->assertSame( $txt, $msg->parse() );
415 * @covers Message::params
416 * @covers Message::toString
417 * @covers Message::replaceParameters
419 public function testReplaceManyParams() {
420 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
421 // One less than above has placeholders
422 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
425 $msg->params( $params )->plain(),
426 'Params > 9 are replaced correctly'
429 $msg = new RawMessage( 'Params$*' );
430 $params = [ 'ab', 'bc', 'cd' ];
432 'Params: ab, bc, cd',
433 $msg->params( $params )->text()
438 * @covers Message::numParam
439 * @covers Message::numParams
441 public function testNumParams() {
442 $lang = Language
::factory( 'en' );
443 $msg = new RawMessage( '$1' );
446 $lang->formatNum( 123456.789 ),
447 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
448 'numParams is handled correctly'
453 * @covers Message::durationParam
454 * @covers Message::durationParams
456 public function testDurationParams() {
457 $lang = Language
::factory( 'en' );
458 $msg = new RawMessage( '$1' );
461 $lang->formatDuration( 1234 ),
462 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
463 'durationParams is handled correctly'
468 * FIXME: This should not need database, but Language#formatExpiry does (T57912)
470 * @covers Message::expiryParam
471 * @covers Message::expiryParams
473 public function testExpiryParams() {
474 $lang = Language
::factory( 'en' );
475 $msg = new RawMessage( '$1' );
478 $lang->formatExpiry( wfTimestampNow() ),
479 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
480 'expiryParams is handled correctly'
485 * @covers Message::timeperiodParam
486 * @covers Message::timeperiodParams
488 public function testTimeperiodParams() {
489 $lang = Language
::factory( 'en' );
490 $msg = new RawMessage( '$1' );
493 $lang->formatTimePeriod( 1234 ),
494 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
495 'timeperiodParams is handled correctly'
500 * @covers Message::sizeParam
501 * @covers Message::sizeParams
503 public function testSizeParams() {
504 $lang = Language
::factory( 'en' );
505 $msg = new RawMessage( '$1' );
508 $lang->formatSize( 123456 ),
509 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
510 'sizeParams is handled correctly'
515 * @covers Message::bitrateParam
516 * @covers Message::bitrateParams
518 public function testBitrateParams() {
519 $lang = Language
::factory( 'en' );
520 $msg = new RawMessage( '$1' );
523 $lang->formatBitrate( 123456 ),
524 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
525 'bitrateParams is handled correctly'
529 public static function providePlaintextParams() {
532 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
538 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
543 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
548 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
553 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
560 * @covers Message::plaintextParam
561 * @covers Message::plaintextParams
562 * @covers Message::formatPlaintext
563 * @covers Message::toString
564 * @covers Message::parse
565 * @covers Message::parseAsBlock
566 * @dataProvider providePlaintextParams
568 public function testPlaintextParams( $expect, $format ) {
569 $lang = Language
::factory( 'en' );
571 $msg = new RawMessage( '$1 $2' );
574 '<div>foo</div> [[Bar]] {{Baz}} <',
578 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
579 "Fail formatting for $format"
583 public static function provideListParam() {
584 $lang = Language
::factory( 'de' );
585 $msg1 = new Message( 'mainpage', [], $lang );
586 $msg2 = new RawMessage( "''link''", [], $lang );
589 'Simple comma list' => [
596 'Simple semicolon list' => [
603 'Simple pipe list' => [
610 'Simple text list' => [
624 'List with all "before" params, ->text()' => [
625 [ "''link''", Message
::numParam( 12345678 ) ],
628 '\'\'link\'\'; 12,345,678'
631 'List with all "before" params, ->parse()' => [
632 [ "''link''", Message
::numParam( 12345678 ) ],
635 '<i>link</i>; 12,345,678'
638 'List with all "after" params, ->text()' => [
639 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
642 'Main Page; \'\'link\'\'; [[foo]]'
645 'List with all "after" params, ->parse()' => [
646 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
649 'Main Page; <i>link</i>; [[foo]]'
652 'List with both "before" and "after" params, ->text()' => [
653 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
656 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
659 'List with both "before" and "after" params, ->parse()' => [
660 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
663 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
669 * @covers Message::listParam
670 * @covers Message::extractParam
671 * @covers Message::formatListParam
672 * @dataProvider provideListParam
674 public function testListParam( $list, $type, $format, $expect ) {
675 $lang = Language
::factory( 'en' );
677 $msg = new RawMessage( '$1' );
678 $msg->params( [ Message
::listParam( $list, $type ) ] );
681 $msg->inLanguage( $lang )->$format()
686 * @covers Message::extractParam
688 public function testMessageAsParam() {
689 $this->setMwGlobals( [
690 'wgScript' => '/wiki/index.php',
691 'wgArticlePath' => '/wiki/$1',
694 $msg = new Message( 'returnto', [
695 new Message( 'apihelp-link', [
696 'foo', new Message( 'mainpage', [], Language
::factory( 'en' ) )
697 ], Language
::factory( 'de' ) )
698 ], Language
::factory( 'es' ) );
701 'Volver a [[Special:ApiHelp/foo|Página principal]].',
703 'Process with ->text()'
706 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
707 . "principal</a>.\n</p>",
708 $msg->parseAsBlock(),
709 'Process with ->parseAsBlock()'
713 public static function provideParser() {
716 "''&'' <x><!-- x -->",
721 "''&'' <x><!-- x -->",
725 '<i>&</i> <x>',
730 "<p><i>&</i> <x>\n</p>",
737 * @covers Message::text
738 * @covers Message::parse
739 * @covers Message::parseAsBlock
740 * @covers Message::toString
741 * @covers Message::transformText
742 * @covers Message::parseText
743 * @dataProvider provideParser
745 public function testParser( $expect, $format ) {
746 $msg = new RawMessage( "''&'' <x><!-- x -->" );
749 $msg->inLanguage( 'en' )->$format()
754 * @covers Message::inContentLanguage
756 public function testInContentLanguage() {
757 $this->setUserLang( 'fr' );
759 // NOTE: make sure internal caching of the message text is reset appropriately
760 $msg = wfMessage( 'mainpage' );
761 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
762 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
763 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
767 * @covers Message::inContentLanguage
769 public function testInContentLanguageOverride() {
770 $this->setMwGlobals( [
771 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
773 $this->setUserLang( 'fr' );
775 // NOTE: make sure internal caching of the message text is reset appropriately.
776 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
777 $msg = wfMessage( 'mainpage' );
780 $msg->inContentLanguage()->plain(),
781 'inContentLanguage() with ForceUIMsg override enabled'
783 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
786 $msg->inContentLanguage()->plain(),
787 'inContentLanguage() with ForceUIMsg override enabled'
789 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
793 * @expectedException MWException
794 * @covers Message::inLanguage
796 public function testInLanguageThrows() {
797 wfMessage( 'foo' )->inLanguage( 123 );
801 * @covers Message::serialize
802 * @covers Message::unserialize
804 public function testSerialization() {
805 $msg = new Message( 'parentheses' );
806 $msg->rawParams( '<a>foo</a>' );
807 $msg->title( Title
::newFromText( 'Testing' ) );
808 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
809 $msg = unserialize( serialize( $msg ) );
810 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
811 $title = TestingAccessWrapper
::newFromObject( $msg )->title
;
812 $this->assertInstanceOf( 'Title', $title );
813 $this->assertSame( 'Testing', $title->getFullText() );
815 $msg = new Message( 'mainpage' );
816 $msg->inLanguage( 'de' );
817 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
818 $msg = unserialize( serialize( $msg ) );
819 $this->assertSame( 'Hauptseite', $msg->plain() );
823 * @covers Message::newFromSpecifier
824 * @dataProvider provideNewFromSpecifier
826 public function testNewFromSpecifier( $value, $expectedText ) {
827 $message = Message
::newFromSpecifier( $value );
828 $this->assertInstanceOf( Message
::class, $message );
829 if ( $value instanceof Message
) {
830 $this->assertInstanceOf( get_class( $value ), $message );
831 $this->assertEquals( $value, $message );
833 $this->assertSame( $expectedText, $message->text() );
836 public function provideNewFromSpecifier() {
837 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier
::class );
838 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
839 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
842 'string' => [ 'mainpage', 'Main Page' ],
843 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
844 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
845 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
846 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
847 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
848 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],