4 use Wikimedia\TestingAccessWrapper;
11 protected function setUp() {
15 'wgForceUIMsgAsContentMsg' => [],
24 public function testConstructor( $expectedLang, $key,
$params, $language ) {
25 $message =
new Message( $key,
$params, $language );
27 $this->assertSame( $key, $message->getKey() );
28 $this->assertSame(
$params, $message->getParams() );
29 $this->assertSame( $expectedLang->getCode(), $message->getLanguage()->getCode() );
32 $messageSpecifier->expects( $this->
any() )
33 ->method(
'getKey' )->will( $this->returnValue( $key ) );
34 $messageSpecifier->expects( $this->
any() )
35 ->method(
'getParams' )->will( $this->returnValue(
$params ) );
36 $message =
new Message( $messageSpecifier, [], $language );
38 $this->assertSame( $key, $message->getKey() );
39 $this->assertSame(
$params, $message->getParams() );
40 $this->assertSame( $expectedLang->getCode(), $message->getLanguage()->getCode() );
43 public static function provideConstructor() {
48 [ $langDe,
'foo', [], $langDe ],
49 [ $langDe,
'foo', [
'bar' ], $langDe ],
50 [ $langEn,
'foo', [
'bar' ],
null ]
54 public static function provideConstructorParams() {
81 [ Message::rawParam(
'baz' ) ],
82 [ Message::rawParam(
'baz' ) ],
85 [ Message::rawParam(
'baz' ),
'foo' ],
86 [ Message::rawParam(
'baz' ),
'foo' ],
89 [ Message::rawParam(
'baz' ) ],
90 [ [ Message::rawParam(
'baz' ) ] ],
93 [ Message::rawParam(
'baz' ),
'foo' ],
94 [ [ Message::rawParam(
'baz' ),
'foo' ] ],
99 [ [
'baz',
'foo' ],
'hhh' ],
100 [ [
'baz',
'foo' ],
'hhh' ],
103 [ [
'baz',
'foo' ],
'hhh', [
'ahahahahha' ] ],
104 [ [
'baz',
'foo' ],
'hhh', [
'ahahahahha' ] ],
107 [ [
'baz',
'foo' ], [
'ahahahahha' ] ],
108 [ [
'baz',
'foo' ], [
'ahahahahha' ] ],
111 [ [
'baz' ], [
'ahahahahha' ] ],
112 [ [
'baz' ], [
'ahahahahha' ] ],
122 public function testConstructorParams( $expected,
$args ) {
123 $msg =
new Message(
'imasomething' );
125 $returned = call_user_func_array( [ $msg,
'params' ],
$args );
127 $this->assertSame( $msg, $returned );
128 $this->assertSame( $expected, $msg->getParams() );
131 public static function provideConstructorLanguage() {
133 [
'foo', [
'bar' ],
'en' ],
134 [
'foo', [
'bar' ],
'de' ]
143 public function testConstructorLanguage( $key,
$params, $languageCode ) {
145 $message =
new Message( $key,
$params, $language );
147 $this->assertEquals( $language, $message->getLanguage() );
150 public static function provideKeys() {
154 'expected' => [
'mainpage' ],
157 'key' => [
'mainpage' ],
158 'expected' => [
'mainpage' ],
161 'key' => [
'mainpage-foo',
'mainpage-bar',
'mainpage' ],
162 'expected' => [
'mainpage-foo',
'mainpage-bar',
'mainpage' ],
167 'exception' =>
'InvalidArgumentException',
172 'exception' =>
'InvalidArgumentException',
177 'exception' =>
'InvalidArgumentException',
189 public function testKeys( $key, $expected, $exception =
null ) {
191 $this->setExpectedException( $exception );
194 $msg =
new Message( $key );
195 $this->assertContains( $msg->getKey(), $expected );
196 $this->assertSame( $expected, $msg->getKeysToTry() );
197 $this->assertSame(
count( $expected ) > 1, $msg->isMultiKey() );
203 public function testWfMessage() {
211 public function testNewFromKey() {
212 $this->assertInstanceOf(
Message::class, Message::newFromKey(
'mainpage' ) );
213 $this->assertInstanceOf(
Message::class, Message::newFromKey(
'i-dont-exist-evar' ) );
220 public function testWfMessageParams() {
221 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto' )->
text() );
222 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto', [] )->
text() );
229 wfMessage(
'returnto', [ Message::numParam( 1024 ) ] )->
text()
232 'You have foo (bar).',
236 'You have foo (bar).',
240 'You have 1,024 (bar).',
242 'youhavenewmessages',
243 Message::numParam( 1024 ),
'bar'
247 'You have foo (2,048).',
249 'youhavenewmessages',
250 'foo', Message::numParam( 2048 )
254 'You have 1,024 (2,048).',
256 'youhavenewmessages',
257 [ Message::numParam( 1024 ), Message::numParam( 2048 ) ]
265 public function testExists() {
266 $this->assertTrue(
wfMessage(
'mainpage' )->exists() );
267 $this->assertTrue(
wfMessage(
'mainpage' )->params( [] )->exists() );
268 $this->assertTrue(
wfMessage(
'mainpage' )->rawParams(
'foo', 123 )->exists() );
269 $this->assertFalse(
wfMessage(
'i-dont-exist-evar' )->exists() );
270 $this->assertFalse(
wfMessage(
'i-dont-exist-evar' )->params( [] )->exists() );
271 $this->assertFalse(
wfMessage(
'i-dont-exist-evar' )->rawParams(
'foo', 123 )->exists() );
281 public function testToStringKey() {
282 $this->assertSame(
'Main Page',
wfMessage(
'mainpage' )->
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' )->
text() );
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' )->
plain() );
287 $this->assertSame(
'⧼i-dont-exist-evar⧽',
wfMessage(
'i-dont-exist-evar' )->escaped() );
289 '⧼i<dont>exist-evar⧽',
290 wfMessage(
'i<dont>exist-evar' )->escaped()
294 public static function provideToString() {
297 [
'mainpage',
'plain',
'Main Page',
'Main Page' ],
298 [
'i-dont-exist-evar',
'plain',
'⧼i-dont-exist-evar⧽',
'⧼i-dont-exist-evar⧽' ],
299 [
'i-dont-exist-evar',
'escaped',
'⧼i-dont-exist-evar⧽',
'⧼i-dont-exist-evar⧽' ],
300 [
'script>alert(1)</script',
'escaped',
'⧼script>alert(1)</script⧽',
301 '⧼script>alert(1)</script⧽' ],
302 [
'script>alert(1)</script',
'plain',
'⧼script>alert(1)</script⧽',
303 '⧼script>alert(1)</script⧽' ],
312 public function testToString( $key, $format, $expect, $expectImplicit ) {
313 $msg =
new Message( $key );
314 $this->assertSame( $expect, $msg->$format() );
315 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by previous call' );
316 $this->assertSame( $expectImplicit, $msg->__toString() );
317 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by __toString' );
320 public static function provideToString_raw() {
322 [
'<span>foo</span>',
'parse',
'<span>foo</span>',
'<span>foo</span>' ],
323 [
'<span>foo</span>',
'escaped',
'<span>foo</span>',
324 '<span>foo</span>' ],
325 [
'<span>foo</span>',
'plain',
'<span>foo</span>',
'<span>foo</span>' ],
326 [
'<script>alert(1)</script>',
'parse',
'<script>alert(1)</script>',
327 '<script>alert(1)</script>' ],
328 [
'<script>alert(1)</script>',
'escaped',
'<script>alert(1)</script>',
329 '<script>alert(1)</script>' ],
330 [
'<script>alert(1)</script>',
'plain',
'<script>alert(1)</script>',
331 '<script>alert(1)</script>' ],
340 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
342 $msg = $this->getMockBuilder(
Message::class )->setMethods( [
'fetchMessage' ] )
343 ->disableOriginalConstructor()
345 $msg->expects( $this->
any() )->method(
'fetchMessage' )->willReturn( $message );
347 $this->assertSame( $expect, $msg->$format() );
348 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by previous call' );
349 $this->assertSame( $expectImplicit, $msg->__toString() );
350 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by __toString' );
356 public function testInLanguage() {
357 $this->assertSame(
'Main Page',
wfMessage(
'mainpage' )->inLanguage(
'en' )->
text() );
358 $this->assertSame(
'Заглавная страница',
363 $this->assertSame(
'Main Page', $msg->inLanguage(
Language::factory(
'en' ) )->text() );
365 'Заглавная страница',
374 public function testRawParams() {
376 '(Заглавная страница)',
380 '(Заглавная страница $1)',
384 '(Заглавная страница)',
385 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница' )->
plain()
388 '(Заглавная страница $1)',
389 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница $1' )->
plain()
397 public function testRawMessage() {
399 $this->assertSame(
'example &', $msg->plain() );
400 $this->assertSame(
'example &', $msg->escaped() );
406 public function testRawHtmlInMsg() {
412 MediaWikiServices::getInstance()->getParserFactory()->create() );
414 $msg =
new RawMessage(
'<html><script>alert("xss")</script></html>' );
415 $txt =
'<span class="error"><html> tags cannot be' .
416 ' used outside of normal pages.</span>';
417 $this->assertSame( $txt, $msg->parse() );
425 public function testReplaceManyParams() {
426 $msg =
new RawMessage(
'$1$2$3$4$5$6$7$8$9$10$11$12' );
428 $params = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k' ];
431 $msg->params(
$params )->plain(),
432 'Params > 9 are replaced correctly'
436 $params = [
'ab',
'bc',
'cd' ];
438 'Params: ab, bc, cd',
439 $msg->params(
$params )->text()
447 public function testNumParams() {
452 $lang->formatNum( 123456.789 ),
453 $msg->inLanguage(
$lang )->numParams( 123456.789 )->plain(),
454 'numParams is handled correctly'
462 public function testDurationParams() {
467 $lang->formatDuration( 1234 ),
468 $msg->inLanguage(
$lang )->durationParams( 1234 )->plain(),
469 'durationParams is handled correctly'
478 public function testExpiryParams() {
485 'expiryParams is handled correctly'
493 public function testTimeperiodParams() {
498 $lang->formatTimePeriod( 1234 ),
499 $msg->inLanguage(
$lang )->timeperiodParams( 1234 )->plain(),
500 'timeperiodParams is handled correctly'
508 public function testSizeParams() {
513 $lang->formatSize( 123456 ),
514 $msg->inLanguage(
$lang )->sizeParams( 123456 )->plain(),
515 'sizeParams is handled correctly'
523 public function testBitrateParams() {
528 $lang->formatBitrate( 123456 ),
529 $msg->inLanguage(
$lang )->bitrateParams( 123456 )->plain(),
530 'bitrateParams is handled correctly'
534 public static function providePlaintextParams() {
537 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
543 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
548 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
553 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
558 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
573 public function testPlaintextParams( $expect, $format ) {
579 '<div>foo</div> [[Bar]] {{Baz}} <',
583 $msg->inLanguage(
$lang )->plaintextParams(
$params )->$format(),
584 "Fail formatting for $format"
588 public static function provideListParam() {
590 $msg1 =
new Message(
'mainpage', [],
$lang );
594 'Simple comma list' => [
601 'Simple semicolon list' => [
608 'Simple pipe list' => [
615 'Simple text list' => [
629 'List with all "before" params, ->text()' => [
630 [
"''link''", Message::numParam( 12345678 ) ],
633 '\'\
'link\'\'; 12,345,678'
636 'List with all "before" params, ->parse()' => [
637 [
"''link''", Message::numParam( 12345678 ) ],
640 '<i>link</i>; 12,345,678'
643 'List with all "after" params, ->text()' => [
644 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ) ],
647 'Main Page; \'\'link\'\'; [[foo]]'
650 'List with all "after" params, ->parse()' => [
651 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ) ],
654 'Main Page; <i>link</i>; [[foo]]'
657 'List with both "before" and "after" params, ->text()' => [
658 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ),
"''link''", Message::numParam( 12345678 ) ],
661 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
664 'List with both "before" and "after" params, ->parse()' => [
665 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ),
"''link''", Message::numParam( 12345678 ) ],
668 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
679 public function testListParam( $list,
$type, $format, $expect ) {
683 $msg->params( [ Message::listParam( $list,
$type ) ] );
686 $msg->inLanguage(
$lang )->$format()
693 public function testMessageAsParam() {
695 'wgScript' =>
'/wiki/index.php',
696 'wgArticlePath' =>
'/wiki/$1',
699 $msg =
new Message(
'returnto', [
700 new Message(
'apihelp-link', [
706 'Volver a [[Special:ApiHelp/foo|Página principal]].',
708 'Process with ->text()'
711 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
712 .
"principal</a>.\n</p>",
713 $msg->parseAsBlock(),
714 'Process with ->parseAsBlock()'
718 public static function provideParser() {
721 "''&'' <x><!-- x -->",
726 "''&'' <x><!-- x -->",
730 '<i>&</i> <x>',
735 "<p><i>&</i> <x>\n</p>",
750 public function testParser( $expect, $format ) {
751 $msg =
new RawMessage(
"''&'' <x><!-- x -->" );
754 $msg->inLanguage(
'en' )->$format()
761 public function testInContentLanguage() {
766 $this->assertSame(
'Hauptseite', $msg->inLanguage(
'de' )->plain(),
"inLanguage( 'de' )" );
767 $this->assertSame(
'Main Page', $msg->inContentLanguage()->plain(),
"inContentLanguage()" );
768 $this->assertSame(
'Accueil', $msg->inLanguage(
'fr' )->plain(),
"inLanguage( 'fr' )" );
774 public function testInContentLanguageOverride() {
776 'wgForceUIMsgAsContentMsg' => [
'mainpage' ],
785 $msg->inContentLanguage()->plain(),
786 'inContentLanguage() with ForceUIMsg override enabled'
788 $this->assertSame(
'Main Page', $msg->inLanguage(
'en' )->plain(),
"inLanguage( 'en' )" );
791 $msg->inContentLanguage()->plain(),
792 'inContentLanguage() with ForceUIMsg override enabled'
794 $this->assertSame(
'Hauptseite', $msg->inLanguage(
'de' )->plain(),
"inLanguage( 'de' )" );
801 public function testInLanguageThrows() {
809 public function testSerialization() {
810 $msg =
new Message(
'parentheses' );
811 $msg->rawParams(
'<a>foo</a>' );
813 $this->assertSame(
'(<a>foo</a>)', $msg->parse(),
'Sanity check' );
815 $this->assertSame(
'(<a>foo</a>)', $msg->parse() );
816 $title = TestingAccessWrapper::newFromObject( $msg )->title;
818 $this->assertSame(
'Testing',
$title->getFullText() );
820 $msg =
new Message(
'mainpage' );
821 $msg->inLanguage(
'de' );
822 $this->assertSame(
'Hauptseite', $msg->plain(),
'Sanity check' );
824 $this->assertSame(
'Hauptseite', $msg->plain() );
831 public function testNewFromSpecifier(
$value, $expectedText ) {
832 $message = Message::newFromSpecifier(
$value );
834 if (
$value instanceof Message ) {
835 $this->assertInstanceOf( get_class(
$value ), $message );
836 $this->assertEquals(
$value, $message );
838 $this->assertSame( $expectedText, $message->text() );
841 public function provideNewFromSpecifier() {
843 $messageSpecifier->expects( $this->
any() )->method(
'getKey' )->willReturn(
'mainpage' );
844 $messageSpecifier->expects( $this->
any() )->method(
'getParams' )->willReturn( [] );
847 'string' => [
'mainpage',
'Main Page' ],
848 'array' => [ [
'youhavenewmessages',
'foo',
'bar' ],
'You have foo (bar).' ],
849 'Message' => [
new Message(
'youhavenewmessages', [
'foo',
'bar' ] ),
'You have foo (bar).' ],
850 'RawMessage' => [
new RawMessage(
'foo ($1)', [
'bar' ] ),
'foo (bar)' ],
851 'ApiMessage' => [
new ApiMessage( [
'mainpage' ],
'code', [
'data' ] ),
'Main Page' ],
852 'MessageSpecifier' => [ $messageSpecifier,
'Main Page' ],
853 'nested RawMessage' => [ [
new RawMessage(
'foo ($1)', [
'bar' ] ) ],
'foo (bar)' ],