3 use Wikimedia\TestingAccessWrapper
;
8 class MessageTest
extends MediaWikiLangTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgForceUIMsgAsContentMsg' => [],
16 $this->setUserLang( 'en' );
20 * @covers Message::__construct
21 * @dataProvider provideConstructor
23 public function testConstructor( $expectedLang, $key, $params, $language ) {
24 $message = new Message( $key, $params, $language );
26 $this->assertSame( $key, $message->getKey() );
27 $this->assertSame( $params, $message->getParams() );
28 $this->assertSame( $expectedLang->getCode(), $message->getLanguage()->getCode() );
30 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier
::class );
31 $messageSpecifier->expects( $this->any() )
32 ->method( 'getKey' )->will( $this->returnValue( $key ) );
33 $messageSpecifier->expects( $this->any() )
34 ->method( 'getParams' )->will( $this->returnValue( $params ) );
35 $message = new Message( $messageSpecifier, [], $language );
37 $this->assertSame( $key, $message->getKey() );
38 $this->assertSame( $params, $message->getParams() );
39 $this->assertSame( $expectedLang->getCode(), $message->getLanguage()->getCode() );
42 public static function provideConstructor() {
43 $langDe = Language
::factory( 'de' );
44 $langEn = Language
::factory( 'en' );
47 [ $langDe, 'foo', [], $langDe ],
48 [ $langDe, 'foo', [ 'bar' ], $langDe ],
49 [ $langEn, 'foo', [ 'bar' ], null ]
53 public static function provideConstructorParams() {
80 [ Message
::rawParam( 'baz' ) ],
81 [ Message
::rawParam( 'baz' ) ],
84 [ Message
::rawParam( 'baz' ), 'foo' ],
85 [ Message
::rawParam( 'baz' ), 'foo' ],
88 [ Message
::rawParam( 'baz' ) ],
89 [ [ Message
::rawParam( 'baz' ) ] ],
92 [ Message
::rawParam( 'baz' ), 'foo' ],
93 [ [ Message
::rawParam( 'baz' ), 'foo' ] ],
96 // Test handling of erroneous input, to detect if it changes
98 [ [ 'baz', 'foo' ], 'hhh' ],
99 [ [ 'baz', 'foo' ], 'hhh' ],
102 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
103 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
106 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
107 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
110 [ [ 'baz' ], [ 'ahahahahha' ] ],
111 [ [ 'baz' ], [ 'ahahahahha' ] ],
117 * @covers Message::__construct
118 * @covers Message::getParams
119 * @dataProvider provideConstructorParams
121 public function testConstructorParams( $expected, $args ) {
122 $msg = new Message( 'imasomething' );
124 $returned = call_user_func_array( [ $msg, 'params' ], $args );
126 $this->assertSame( $msg, $returned );
127 $this->assertSame( $expected, $msg->getParams() );
130 public static function provideConstructorLanguage() {
132 [ 'foo', [ 'bar' ], 'en' ],
133 [ 'foo', [ 'bar' ], 'de' ]
138 * @covers Message::__construct
139 * @covers Message::getLanguage
140 * @dataProvider provideConstructorLanguage
142 public function testConstructorLanguage( $key, $params, $languageCode ) {
143 $language = Language
::factory( $languageCode );
144 $message = new Message( $key, $params, $language );
146 $this->assertEquals( $language, $message->getLanguage() );
149 public static function provideKeys() {
153 'expected' => [ 'mainpage' ],
156 'key' => [ 'mainpage' ],
157 'expected' => [ 'mainpage' ],
160 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
161 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
166 'exception' => 'InvalidArgumentException',
171 'exception' => 'InvalidArgumentException',
176 'exception' => 'InvalidArgumentException',
182 * @covers Message::__construct
183 * @covers Message::getKey
184 * @covers Message::isMultiKey
185 * @covers Message::getKeysToTry
186 * @dataProvider provideKeys
188 public function testKeys( $key, $expected, $exception = null ) {
190 $this->setExpectedException( $exception );
193 $msg = new Message( $key );
194 $this->assertContains( $msg->getKey(), $expected );
195 $this->assertSame( $expected, $msg->getKeysToTry() );
196 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
200 * @covers ::wfMessage
202 public function testWfMessage() {
203 $this->assertInstanceOf( Message
::class, wfMessage( 'mainpage' ) );
204 $this->assertInstanceOf( Message
::class, wfMessage( 'i-dont-exist-evar' ) );
208 * @covers Message::newFromKey
210 public function testNewFromKey() {
211 $this->assertInstanceOf( Message
::class, Message
::newFromKey( 'mainpage' ) );
212 $this->assertInstanceOf( Message
::class, Message
::newFromKey( 'i-dont-exist-evar' ) );
216 * @covers ::wfMessage
217 * @covers Message::__construct
219 public function testWfMessageParams() {
220 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
221 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
224 wfMessage( 'returnto', Message
::numParam( 1024 ) )->text()
228 wfMessage( 'returnto', [ Message
::numParam( 1024 ) ] )->text()
231 'You have foo (bar).',
232 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
235 'You have foo (bar).',
236 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
239 'You have 1,024 (bar).',
241 'youhavenewmessages',
242 Message
::numParam( 1024 ), 'bar'
246 'You have foo (2,048).',
248 'youhavenewmessages',
249 'foo', Message
::numParam( 2048 )
253 'You have 1,024 (2,048).',
255 'youhavenewmessages',
256 [ Message
::numParam( 1024 ), Message
::numParam( 2048 ) ]
262 * @covers Message::exists
264 public function testExists() {
265 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
266 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
267 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
268 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
269 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
270 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
274 * @covers Message::__construct
275 * @covers Message::text
276 * @covers Message::plain
277 * @covers Message::escaped
278 * @covers Message::toString
280 public function testToStringKey() {
281 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
282 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
283 $this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
284 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
285 $this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
286 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
288 '⧼i<dont>exist-evar⧽',
289 wfMessage( 'i<dont>exist-evar' )->escaped()
293 public static function provideToString() {
295 // key, transformation, transformed, transformed implicitly
296 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
297 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
298 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
299 [ 'script>alert(1)</script', 'escaped', '⧼script>alert(1)</script⧽',
300 '⧼script>alert(1)</script⧽' ],
301 [ 'script>alert(1)</script', 'plain', '⧼script>alert(1)</script⧽',
302 '⧼script>alert(1)</script⧽' ],
307 * @covers Message::toString
308 * @covers Message::__toString
309 * @dataProvider provideToString
311 public function testToString( $key, $format, $expect, $expectImplicit ) {
312 $msg = new Message( $key );
313 $this->assertSame( $expect, $msg->$format() );
314 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
315 $this->assertSame( $expectImplicit, $msg->__toString() );
316 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
319 public static function provideToString_raw() {
321 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
322 [ '<span>foo</span>', 'escaped', '<span>foo</span>',
323 '<span>foo</span>' ],
324 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
325 [ '<script>alert(1)</script>', 'parse', '<script>alert(1)</script>',
326 '<script>alert(1)</script>' ],
327 [ '<script>alert(1)</script>', 'escaped', '<script>alert(1)</script>',
328 '<script>alert(1)</script>' ],
329 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
330 '<script>alert(1)</script>' ],
335 * @covers Message::toString
336 * @covers Message::__toString
337 * @dataProvider provideToString_raw
339 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
340 // make the message behave like RawMessage and use the key as-is
341 $msg = $this->getMockBuilder( Message
::class )->setMethods( [ 'fetchMessage' ] )
342 ->disableOriginalConstructor()
344 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
345 /** @var Message $msg */
346 $this->assertSame( $expect, $msg->$format() );
347 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
348 $this->assertSame( $expectImplicit, $msg->__toString() );
349 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
353 * @covers Message::inLanguage
355 public function testInLanguage() {
356 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
357 $this->assertSame( 'Заглавная страница',
358 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
360 // NOTE: make sure internal caching of the message text is reset appropriately
361 $msg = wfMessage( 'mainpage' );
362 $this->assertSame( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
364 'Заглавная страница',
365 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
370 * @covers Message::rawParam
371 * @covers Message::rawParams
373 public function testRawParams() {
375 '(Заглавная страница)',
376 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
379 '(Заглавная страница $1)',
380 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
383 '(Заглавная страница)',
384 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
387 '(Заглавная страница $1)',
388 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
393 * @covers RawMessage::__construct
394 * @covers RawMessage::fetchMessage
396 public function testRawMessage() {
397 $msg = new RawMessage( 'example &' );
398 $this->assertSame( 'example &', $msg->plain() );
399 $this->assertSame( 'example &', $msg->escaped() );
403 * @covers CoreTagHooks::html
405 public function testRawHtmlInMsg() {
406 $this->setMwGlobals( 'wgRawHtml', true );
407 // We have to reset the core hook registration.
408 // to register the html hook
409 MessageCache
::destroyInstance();
410 $this->overrideMwServices();
412 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
413 $txt = '<span class="error"><html> tags cannot be' .
414 ' used outside of normal pages.</span>';
415 $this->assertSame( $txt, $msg->parse() );
419 * @covers Message::params
420 * @covers Message::toString
421 * @covers Message::replaceParameters
423 public function testReplaceManyParams() {
424 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
425 // One less than above has placeholders
426 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
429 $msg->params( $params )->plain(),
430 'Params > 9 are replaced correctly'
433 $msg = new RawMessage( 'Params$*' );
434 $params = [ 'ab', 'bc', 'cd' ];
436 'Params: ab, bc, cd',
437 $msg->params( $params )->text()
442 * @covers Message::numParam
443 * @covers Message::numParams
445 public function testNumParams() {
446 $lang = Language
::factory( 'en' );
447 $msg = new RawMessage( '$1' );
450 $lang->formatNum( 123456.789 ),
451 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
452 'numParams is handled correctly'
457 * @covers Message::durationParam
458 * @covers Message::durationParams
460 public function testDurationParams() {
461 $lang = Language
::factory( 'en' );
462 $msg = new RawMessage( '$1' );
465 $lang->formatDuration( 1234 ),
466 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
467 'durationParams is handled correctly'
472 * FIXME: This should not need database, but Language#formatExpiry does (T57912)
473 * @covers Message::expiryParam
474 * @covers Message::expiryParams
476 public function testExpiryParams() {
477 $lang = Language
::factory( 'en' );
478 $msg = new RawMessage( '$1' );
481 $lang->formatExpiry( wfTimestampNow() ),
482 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
483 'expiryParams is handled correctly'
488 * @covers Message::timeperiodParam
489 * @covers Message::timeperiodParams
491 public function testTimeperiodParams() {
492 $lang = Language
::factory( 'en' );
493 $msg = new RawMessage( '$1' );
496 $lang->formatTimePeriod( 1234 ),
497 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
498 'timeperiodParams is handled correctly'
503 * @covers Message::sizeParam
504 * @covers Message::sizeParams
506 public function testSizeParams() {
507 $lang = Language
::factory( 'en' );
508 $msg = new RawMessage( '$1' );
511 $lang->formatSize( 123456 ),
512 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
513 'sizeParams is handled correctly'
518 * @covers Message::bitrateParam
519 * @covers Message::bitrateParams
521 public function testBitrateParams() {
522 $lang = Language
::factory( 'en' );
523 $msg = new RawMessage( '$1' );
526 $lang->formatBitrate( 123456 ),
527 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
528 'bitrateParams is handled correctly'
532 public static function providePlaintextParams() {
535 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
541 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
546 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
551 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
556 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
563 * @covers Message::plaintextParam
564 * @covers Message::plaintextParams
565 * @covers Message::formatPlaintext
566 * @covers Message::toString
567 * @covers Message::parse
568 * @covers Message::parseAsBlock
569 * @dataProvider providePlaintextParams
571 public function testPlaintextParams( $expect, $format ) {
572 $lang = Language
::factory( 'en' );
574 $msg = new RawMessage( '$1 $2' );
577 '<div>foo</div> [[Bar]] {{Baz}} <',
581 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
582 "Fail formatting for $format"
586 public static function provideListParam() {
587 $lang = Language
::factory( 'de' );
588 $msg1 = new Message( 'mainpage', [], $lang );
589 $msg2 = new RawMessage( "''link''", [], $lang );
592 'Simple comma list' => [
599 'Simple semicolon list' => [
606 'Simple pipe list' => [
613 'Simple text list' => [
627 'List with all "before" params, ->text()' => [
628 [ "''link''", Message
::numParam( 12345678 ) ],
631 '\'\'link\'\'; 12,345,678'
634 'List with all "before" params, ->parse()' => [
635 [ "''link''", Message
::numParam( 12345678 ) ],
638 '<i>link</i>; 12,345,678'
641 'List with all "after" params, ->text()' => [
642 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
645 'Main Page; \'\'link\'\'; [[foo]]'
648 'List with all "after" params, ->parse()' => [
649 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ) ],
652 'Main Page; <i>link</i>; [[foo]]'
655 'List with both "before" and "after" params, ->text()' => [
656 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
659 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
662 'List with both "before" and "after" params, ->parse()' => [
663 [ $msg1, $msg2, Message
::rawParam( '[[foo]]' ), "''link''", Message
::numParam( 12345678 ) ],
666 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
672 * @covers Message::listParam
673 * @covers Message::extractParam
674 * @covers Message::formatListParam
675 * @dataProvider provideListParam
677 public function testListParam( $list, $type, $format, $expect ) {
678 $lang = Language
::factory( 'en' );
680 $msg = new RawMessage( '$1' );
681 $msg->params( [ Message
::listParam( $list, $type ) ] );
684 $msg->inLanguage( $lang )->$format()
689 * @covers Message::extractParam
691 public function testMessageAsParam() {
692 $this->setMwGlobals( [
693 'wgScript' => '/wiki/index.php',
694 'wgArticlePath' => '/wiki/$1',
697 $msg = new Message( 'returnto', [
698 new Message( 'apihelp-link', [
699 'foo', new Message( 'mainpage', [], Language
::factory( 'en' ) )
700 ], Language
::factory( 'de' ) )
701 ], Language
::factory( 'es' ) );
704 'Volver a [[Special:ApiHelp/foo|Página principal]].',
706 'Process with ->text()'
709 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
710 . "principal</a>.\n</p>",
711 $msg->parseAsBlock(),
712 'Process with ->parseAsBlock()'
716 public static function provideParser() {
719 "''&'' <x><!-- x -->",
724 "''&'' <x><!-- x -->",
728 '<i>&</i> <x>',
733 "<p><i>&</i> <x>\n</p>",
740 * @covers Message::text
741 * @covers Message::parse
742 * @covers Message::parseAsBlock
743 * @covers Message::toString
744 * @covers Message::transformText
745 * @covers Message::parseText
746 * @dataProvider provideParser
748 public function testParser( $expect, $format ) {
749 $msg = new RawMessage( "''&'' <x><!-- x -->" );
752 $msg->inLanguage( 'en' )->$format()
757 * @covers Message::inContentLanguage
759 public function testInContentLanguage() {
760 $this->setUserLang( 'fr' );
762 // NOTE: make sure internal caching of the message text is reset appropriately
763 $msg = wfMessage( 'mainpage' );
764 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
765 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
766 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
770 * @covers Message::inContentLanguage
772 public function testInContentLanguageOverride() {
773 $this->setMwGlobals( [
774 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
776 $this->setUserLang( 'fr' );
778 // NOTE: make sure internal caching of the message text is reset appropriately.
779 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
780 $msg = wfMessage( 'mainpage' );
783 $msg->inContentLanguage()->plain(),
784 'inContentLanguage() with ForceUIMsg override enabled'
786 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
789 $msg->inContentLanguage()->plain(),
790 'inContentLanguage() with ForceUIMsg override enabled'
792 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
796 * @expectedException MWException
797 * @covers Message::inLanguage
799 public function testInLanguageThrows() {
800 wfMessage( 'foo' )->inLanguage( 123 );
804 * @covers Message::serialize
805 * @covers Message::unserialize
807 public function testSerialization() {
808 $msg = new Message( 'parentheses' );
809 $msg->rawParams( '<a>foo</a>' );
810 $msg->title( Title
::newFromText( 'Testing' ) );
811 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
812 $msg = unserialize( serialize( $msg ) );
813 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
814 $title = TestingAccessWrapper
::newFromObject( $msg )->title
;
815 $this->assertInstanceOf( Title
::class, $title );
816 $this->assertSame( 'Testing', $title->getFullText() );
818 $msg = new Message( 'mainpage' );
819 $msg->inLanguage( 'de' );
820 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
821 $msg = unserialize( serialize( $msg ) );
822 $this->assertSame( 'Hauptseite', $msg->plain() );
826 * @covers Message::newFromSpecifier
827 * @dataProvider provideNewFromSpecifier
829 public function testNewFromSpecifier( $value, $expectedText ) {
830 $message = Message
::newFromSpecifier( $value );
831 $this->assertInstanceOf( Message
::class, $message );
832 if ( $value instanceof Message
) {
833 $this->assertInstanceOf( get_class( $value ), $message );
834 $this->assertEquals( $value, $message );
836 $this->assertSame( $expectedText, $message->text() );
839 public function provideNewFromSpecifier() {
840 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier
::class );
841 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
842 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
845 'string' => [ 'mainpage', 'Main Page' ],
846 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
847 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
848 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
849 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
850 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
851 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],