3 use Wikimedia\TestingAccessWrapper
;
5 class MessageTest
extends MediaWikiLangTestCase
{
7 protected function setUp() {
10 $this->setMwGlobals( [
11 'wgForceUIMsgAsContentMsg' => [],
13 $this->setUserLang( 'en' );
17 * @covers Message::__construct
18 * @dataProvider provideConstructor
20 public function testConstructor( $expectedLang, $key, $params, $language ) {
21 $message = new Message( $key, $params, $language );
23 $this->assertSame( $key, $message->getKey() );
24 $this->assertSame( $params, $message->getParams() );
25 $this->assertEquals( $expectedLang, $message->getLanguage() );
27 $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
28 $messageSpecifier->expects( $this->any() )
29 ->method( 'getKey' )->will( $this->returnValue( $key ) );
30 $messageSpecifier->expects( $this->any() )
31 ->method( 'getParams' )->will( $this->returnValue( $params ) );
32 $message = new Message( $messageSpecifier, [], $language );
34 $this->assertSame( $key, $message->getKey() );
35 $this->assertSame( $params, $message->getParams() );
36 $this->assertEquals( $expectedLang, $message->getLanguage() );
39 public static function provideConstructor() {
40 $langDe = Language
::factory( 'de' );
41 $langEn = Language
::factory( 'en' );
44 [ $langDe, 'foo', [], $langDe ],
45 [ $langDe, 'foo', [ 'bar' ], $langDe ],
46 [ $langEn, 'foo', [ 'bar' ], null ]
50 public static function provideConstructorParams() {
77 [ Message
::rawParam( 'baz' ) ],
78 [ Message
::rawParam( 'baz' ) ],
81 [ Message
::rawParam( 'baz' ), 'foo' ],
82 [ Message
::rawParam( 'baz' ), 'foo' ],
85 [ Message
::rawParam( 'baz' ) ],
86 [ [ Message
::rawParam( 'baz' ) ] ],
89 [ Message
::rawParam( 'baz' ), 'foo' ],
90 [ [ Message
::rawParam( 'baz' ), 'foo' ] ],
93 // Test handling of erroneous input, to detect if it changes
95 [ [ 'baz', 'foo' ], 'hhh' ],
96 [ [ 'baz', 'foo' ], 'hhh' ],
99 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
100 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
103 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
104 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
107 [ [ 'baz' ], [ 'ahahahahha' ] ],
108 [ [ 'baz' ], [ 'ahahahahha' ] ],
114 * @covers Message::__construct
115 * @covers Message::getParams
116 * @dataProvider provideConstructorParams
118 public function testConstructorParams( $expected, $args ) {
119 $msg = new Message( 'imasomething' );
121 $returned = call_user_func_array( [ $msg, 'params' ], $args );
123 $this->assertSame( $msg, $returned );
124 $this->assertSame( $expected, $msg->getParams() );
127 public static function provideConstructorLanguage() {
129 [ 'foo', [ 'bar' ], 'en' ],
130 [ 'foo', [ 'bar' ], 'de' ]
135 * @covers Message::__construct
136 * @covers Message::getLanguage
137 * @dataProvider provideConstructorLanguage
139 public function testConstructorLanguage( $key, $params, $languageCode ) {
140 $language = Language
::factory( $languageCode );
141 $message = new Message( $key, $params, $language );
143 $this->assertEquals( $language, $message->getLanguage() );
146 public static function provideKeys() {
150 'expected' => [ 'mainpage' ],
153 'key' => [ 'mainpage' ],
154 'expected' => [ 'mainpage' ],
157 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
158 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
163 'exception' => 'InvalidArgumentException',
168 'exception' => 'InvalidArgumentException',
173 'exception' => 'InvalidArgumentException',
179 * @covers Message::__construct
180 * @covers Message::getKey
181 * @covers Message::isMultiKey
182 * @covers Message::getKeysToTry
183 * @dataProvider provideKeys
185 public function testKeys( $key, $expected, $exception = null ) {
187 $this->setExpectedException( $exception );
190 $msg = new Message( $key );
191 $this->assertContains( $msg->getKey(), $expected );
192 $this->assertSame( $expected, $msg->getKeysToTry() );
193 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
197 * @covers ::wfMessage
199 public function testWfMessage() {
200 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
201 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
205 * @covers Message::newFromKey
207 public function testNewFromKey() {
208 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'mainpage' ) );
209 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'i-dont-exist-evar' ) );
213 * @covers ::wfMessage
214 * @covers Message::__construct
216 public function testWfMessageParams() {
217 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
218 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
221 wfMessage( 'returnto', Message
::numParam( 1024 ) )->text()
225 wfMessage( 'returnto', [ Message
::numParam( 1024 ) ] )->text()
228 'You have foo (bar).',
229 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
232 'You have foo (bar).',
233 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
236 'You have 1,024 (bar).',
238 'youhavenewmessages',
239 Message
::numParam( 1024 ), 'bar'
243 'You have foo (2,048).',
245 'youhavenewmessages',
246 'foo', Message
::numParam( 2048 )
250 'You have 1,024 (2,048).',
252 'youhavenewmessages',
253 [ Message
::numParam( 1024 ), Message
::numParam( 2048 ) ]
259 * @covers Message::exists
261 public function testExists() {
262 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
263 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
264 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
265 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
266 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
267 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
271 * @covers Message::__construct
272 * @covers Message::text
273 * @covers Message::plain
274 * @covers Message::escaped
275 * @covers Message::toString
277 public function testToStringKey() {
278 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->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' )->text() );
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' )->plain() );
283 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
285 '⧼i<dont>exist-evar⧽',
286 wfMessage( 'i<dont>exist-evar' )->escaped()
290 public static function provideToString() {
292 // key, transformation, transformed, transformed implicitly
293 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
294 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
295 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
296 [ 'script>alert(1)</script', 'escaped', '⧼script>alert(1)</script⧽',
297 '⧼script>alert(1)</script⧽' ],
298 [ 'script>alert(1)</script', 'plain', '⧼script>alert(1)</script⧽',
299 '⧼script>alert(1)</script⧽' ],
304 * @covers Message::toString
305 * @covers Message::__toString
306 * @dataProvider provideToString
308 public function testToString( $key, $format, $expect, $expectImplicit ) {
309 $msg = new Message( $key );
310 $this->assertSame( $expect, $msg->$format() );
311 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
312 $this->assertSame( $expectImplicit, $msg->__toString() );
313 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
316 public static function provideToString_raw() {
318 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
319 [ '<span>foo</span>', 'escaped', '<span>foo</span>',
320 '<span>foo</span>' ],
321 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
322 [ '<script>alert(1)</script>', 'parse', '<script>alert(1)</script>',
323 '<script>alert(1)</script>' ],
324 [ '<script>alert(1)</script>', 'escaped', '<script>alert(1)</script>',
325 '<script>alert(1)</script>' ],
326 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
327 '<script>alert(1)</script>' ],
332 * @covers Message::toString
333 * @covers Message::__toString
334 * @dataProvider provideToString_raw
336 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
337 // make the message behave like RawMessage and use the key as-is
338 $msg = $this->getMockBuilder( Message
::class )->setMethods( [ 'fetchMessage' ] )
339 ->disableOriginalConstructor()
341 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
342 /** @var Message $msg */
343 $this->assertSame( $expect, $msg->$format() );
344 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
345 $this->assertSame( $expectImplicit, $msg->__toString() );
346 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
350 * @covers Message::inLanguage
352 public function testInLanguage() {
353 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
354 $this->assertSame( 'Заглавная страница',
355 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
357 // NOTE: make sure internal caching of the message text is reset appropriately
358 $msg = wfMessage( 'mainpage' );
359 $this->assertSame( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
361 'Заглавная страница',
362 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
367 * @covers Message::rawParam
368 * @covers Message::rawParams
370 public function testRawParams() {
372 '(Заглавная страница)',
373 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
376 '(Заглавная страница $1)',
377 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
380 '(Заглавная страница)',
381 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
384 '(Заглавная страница $1)',
385 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
390 * @covers RawMessage::__construct
391 * @covers RawMessage::fetchMessage
393 public function testRawMessage() {
394 $msg = new RawMessage( 'example &' );
395 $this->assertSame( 'example &', $msg->plain() );
396 $this->assertSame( 'example &', $msg->escaped() );
399 public function testRawHtmlInMsg() {
400 global $wgParserConf;
401 $this->setMwGlobals( 'wgRawHtml', true );
402 // We have to reset the core hook registration.
403 // to register the html hook
404 MessageCache
::destroyInstance();
405 $this->setMwGlobals( 'wgParser',
406 ObjectFactory
::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
409 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
410 $txt = '<span class="error"><html> tags cannot be' .
411 ' used outside of normal pages.</span>';
412 $this->assertSame( $txt, $msg->parse() );
416 * @covers Message::params
417 * @covers Message::toString
418 * @covers Message::replaceParameters
420 public function testReplaceManyParams() {
421 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
422 // One less than above has placeholders
423 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
426 $msg->params( $params )->plain(),
427 'Params > 9 are replaced correctly'
430 $msg = new RawMessage( 'Params$*' );
431 $params = [ 'ab', 'bc', 'cd' ];
433 'Params: ab, bc, cd',
434 $msg->params( $params )->text()
439 * @covers Message::numParam
440 * @covers Message::numParams
442 public function testNumParams() {
443 $lang = Language
::factory( 'en' );
444 $msg = new RawMessage( '$1' );
447 $lang->formatNum( 123456.789 ),
448 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
449 'numParams is handled correctly'
454 * @covers Message::durationParam
455 * @covers Message::durationParams
457 public function testDurationParams() {
458 $lang = Language
::factory( 'en' );
459 $msg = new RawMessage( '$1' );
462 $lang->formatDuration( 1234 ),
463 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
464 'durationParams is handled correctly'
469 * FIXME: This should not need database, but Language#formatExpiry does (T57912)
471 * @covers Message::expiryParam
472 * @covers Message::expiryParams
474 public function testExpiryParams() {
475 $lang = Language
::factory( 'en' );
476 $msg = new RawMessage( '$1' );
479 $lang->formatExpiry( wfTimestampNow() ),
480 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
481 'expiryParams is handled correctly'
486 * @covers Message::timeperiodParam
487 * @covers Message::timeperiodParams
489 public function testTimeperiodParams() {
490 $lang = Language
::factory( 'en' );
491 $msg = new RawMessage( '$1' );
494 $lang->formatTimePeriod( 1234 ),
495 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
496 'timeperiodParams is handled correctly'
501 * @covers Message::sizeParam
502 * @covers Message::sizeParams
504 public function testSizeParams() {
505 $lang = Language
::factory( 'en' );
506 $msg = new RawMessage( '$1' );
509 $lang->formatSize( 123456 ),
510 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
511 'sizeParams is handled correctly'
516 * @covers Message::bitrateParam
517 * @covers Message::bitrateParams
519 public function testBitrateParams() {
520 $lang = Language
::factory( 'en' );
521 $msg = new RawMessage( '$1' );
524 $lang->formatBitrate( 123456 ),
525 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
526 'bitrateParams is handled correctly'
530 public static function providePlaintextParams() {
533 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
539 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
544 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
549 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
554 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
561 * @covers Message::plaintextParam
562 * @covers Message::plaintextParams
563 * @covers Message::formatPlaintext
564 * @covers Message::toString
565 * @covers Message::parse
566 * @covers Message::parseAsBlock
567 * @dataProvider providePlaintextParams
569 public function testPlaintextParams( $expect, $format ) {
570 $lang = Language
::factory( 'en' );
572 $msg = new RawMessage( '$1 $2' );
575 '<div>foo</div> [[Bar]] {{Baz}} <',
579 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
580 "Fail formatting for $format"
584 public static function provideListParam() {
585 $lang = Language
::factory( 'de' );
586 $msg1 = new Message( 'mainpage', [], $lang );
587 $msg2 = new RawMessage( "''link''", [], $lang );
590 'Simple comma list' => [
597 'Simple semicolon list' => [
604 'Simple pipe list' => [
611 'Simple text list' => [
625 'List with all "before" params, ->text()' => [
626 [ "''link''", Message
::numParam( 12345678 ) ],
629 '\'\'link\'\'; 12,345,678'
632 'List with all "before" params, ->parse()' => [
633 [ "''link''", Message
::numParam( 12345678 ) ],
636 '<i>link</i>; 12,345,678'
639 'List with all "after" params, ->text()' => [
640 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
643 'Main Page; \'\'link\'\'; [[foo]]'
646 'List with all "after" params, ->parse()' => [
647 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
650 'Main Page; <i>link</i>; [[foo]]'
653 'List with both "before" and "after" params, ->text()' => [
654 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
657 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
660 'List with both "before" and "after" params, ->parse()' => [
661 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
664 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
670 * @covers Message::listParam
671 * @covers Message::extractParam
672 * @covers Message::formatListParam
673 * @dataProvider provideListParam
675 public function testListParam( $list, $type, $format, $expect ) {
676 $lang = Language
::factory( 'en' );
678 $msg = new RawMessage( '$1' );
679 $msg->params( [ Message
::listParam( $list, $type ) ] );
682 $msg->inLanguage( $lang )->$format()
687 * @covers Message::extractParam
689 public function testMessageAsParam() {
690 $this->setMwGlobals( [
691 'wgScript' => '/wiki/index.php',
692 'wgArticlePath' => '/wiki/$1',
695 $msg = new Message( 'returnto', [
696 new Message( 'apihelp-link', [
697 'foo', new Message( 'mainpage', [], Language
::factory( 'en' ) )
698 ], Language
::factory( 'de' ) )
699 ], Language
::factory( 'es' ) );
702 'Volver a [[Special:ApiHelp/foo|Página principal]].',
704 'Process with ->text()'
707 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
708 . "principal</a>.\n</p>",
709 $msg->parseAsBlock(),
710 'Process with ->parseAsBlock()'
714 public static function provideParser() {
717 "''&'' <x><!-- x -->",
722 "''&'' <x><!-- x -->",
726 '<i>&</i> <x>',
731 "<p><i>&</i> <x>\n</p>",
738 * @covers Message::text
739 * @covers Message::parse
740 * @covers Message::parseAsBlock
741 * @covers Message::toString
742 * @covers Message::transformText
743 * @covers Message::parseText
744 * @dataProvider provideParser
746 public function testParser( $expect, $format ) {
747 $msg = new RawMessage( "''&'' <x><!-- x -->" );
750 $msg->inLanguage( 'en' )->$format()
755 * @covers Message::inContentLanguage
757 public function testInContentLanguage() {
758 $this->setUserLang( 'fr' );
760 // NOTE: make sure internal caching of the message text is reset appropriately
761 $msg = wfMessage( 'mainpage' );
762 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
763 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
764 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
768 * @covers Message::inContentLanguage
770 public function testInContentLanguageOverride() {
771 $this->setMwGlobals( [
772 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
774 $this->setUserLang( 'fr' );
776 // NOTE: make sure internal caching of the message text is reset appropriately.
777 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
778 $msg = wfMessage( 'mainpage' );
781 $msg->inContentLanguage()->plain(),
782 'inContentLanguage() with ForceUIMsg override enabled'
784 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
787 $msg->inContentLanguage()->plain(),
788 'inContentLanguage() with ForceUIMsg override enabled'
790 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
794 * @expectedException MWException
795 * @covers Message::inLanguage
797 public function testInLanguageThrows() {
798 wfMessage( 'foo' )->inLanguage( 123 );
802 * @covers Message::serialize
803 * @covers Message::unserialize
805 public function testSerialization() {
806 $msg = new Message( 'parentheses' );
807 $msg->rawParams( '<a>foo</a>' );
808 $msg->title( Title
::newFromText( 'Testing' ) );
809 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
810 $msg = unserialize( serialize( $msg ) );
811 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
812 $title = TestingAccessWrapper
::newFromObject( $msg )->title
;
813 $this->assertInstanceOf( 'Title', $title );
814 $this->assertSame( 'Testing', $title->getFullText() );
816 $msg = new Message( 'mainpage' );
817 $msg->inLanguage( 'de' );
818 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
819 $msg = unserialize( serialize( $msg ) );
820 $this->assertSame( 'Hauptseite', $msg->plain() );
824 * @covers Message::newFromSpecifier
825 * @dataProvider provideNewFromSpecifier
827 public function testNewFromSpecifier( $value, $expectedText ) {
828 $message = Message
::newFromSpecifier( $value );
829 $this->assertInstanceOf( Message
::class, $message );
830 if ( $value instanceof Message
) {
831 $this->assertInstanceOf( get_class( $value ), $message );
832 $this->assertEquals( $value, $message );
834 $this->assertSame( $expectedText, $message->text() );
837 public function provideNewFromSpecifier() {
838 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier
::class );
839 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
840 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
843 'string' => [ 'mainpage', 'Main Page' ],
844 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
845 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
846 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
847 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
848 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
849 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],