3 use Wikimedia\TestingAccessWrapper;
7 protected function setUp() {
11 'wgForceUIMsgAsContentMsg' => [],
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() {
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' ] ],
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' ] ],
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' ]
139 public function testConstructorLanguage( $key,
$params, $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',
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() );
199 public function testWfMessage() {
200 $this->assertInstanceOf(
'Message',
wfMessage(
'mainpage' ) );
201 $this->assertInstanceOf(
'Message',
wfMessage(
'i-dont-exist-evar' ) );
207 public function testNewFromKey() {
208 $this->assertInstanceOf(
'Message', Message::newFromKey(
'mainpage' ) );
209 $this->assertInstanceOf(
'Message', Message::newFromKey(
'i-dont-exist-evar' ) );
216 public function testWfMessageParams() {
217 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto' )->
text() );
218 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto', [] )->
text() );
225 wfMessage(
'returnto', [ Message::numParam( 1024 ) ] )->
text()
228 'You have foo (bar).',
232 'You have foo (bar).',
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 ) ]
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() );
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() {
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⧽' ],
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>' ],
336 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
338 $msg = $this->getMockBuilder(
Message::class )->setMethods( [
'fetchMessage' ] )
339 ->disableOriginalConstructor()
341 $msg->expects( $this->
any() )->method(
'fetchMessage' )->willReturn( $message );
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' );
352 public function testInLanguage() {
353 $this->assertSame(
'Main Page',
wfMessage(
'mainpage' )->inLanguage(
'en' )->
text() );
354 $this->assertSame(
'Заглавная страница',
359 $this->assertSame(
'Main Page', $msg->inLanguage(
Language::factory(
'en' ) )->text() );
361 'Заглавная страница',
370 public function testRawParams() {
372 '(Заглавная страница)',
376 '(Заглавная страница $1)',
380 '(Заглавная страница)',
381 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница' )->
plain()
384 '(Заглавная страница $1)',
385 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница $1' )->
plain()
393 public function testRawMessage() {
395 $this->assertSame(
'example &', $msg->plain() );
396 $this->assertSame(
'example &', $msg->escaped() );
399 public function testRawHtmlInMsg() {
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() );
420 public function testReplaceManyParams() {
421 $msg =
new RawMessage(
'$1$2$3$4$5$6$7$8$9$10$11$12' );
423 $params = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k' ];
426 $msg->params(
$params )->plain(),
427 'Params > 9 are replaced correctly'
431 $params = [
'ab',
'bc',
'cd' ];
433 'Params: ab, bc, cd',
434 $msg->params(
$params )->text()
442 public function testNumParams() {
447 $lang->formatNum( 123456.789 ),
448 $msg->inLanguage(
$lang )->numParams( 123456.789 )->plain(),
449 'numParams is handled correctly'
457 public function testDurationParams() {
462 $lang->formatDuration( 1234 ),
463 $msg->inLanguage(
$lang )->durationParams( 1234 )->plain(),
464 'durationParams is handled correctly'
474 public function testExpiryParams() {
481 'expiryParams is handled correctly'
489 public function testTimeperiodParams() {
494 $lang->formatTimePeriod( 1234 ),
495 $msg->inLanguage(
$lang )->timeperiodParams( 1234 )->plain(),
496 'timeperiodParams is handled correctly'
504 public function testSizeParams() {
509 $lang->formatSize( 123456 ),
510 $msg->inLanguage(
$lang )->sizeParams( 123456 )->plain(),
511 'sizeParams is handled correctly'
519 public function testBitrateParams() {
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>",
569 public function testPlaintextParams( $expect, $format ) {
575 '<div>foo</div> [[Bar]] {{Baz}} <',
579 $msg->inLanguage(
$lang )->plaintextParams(
$params )->$format(),
580 "Fail formatting for $format"
584 public static function provideListParam() {
586 $msg1 =
new Message(
'mainpage', [],
$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'
675 public function testListParam( $list,
$type, $format, $expect ) {
679 $msg->params( [ Message::listParam( $list,
$type ) ] );
682 $msg->inLanguage(
$lang )->$format()
689 public function testMessageAsParam() {
691 'wgScript' =>
'/wiki/index.php',
692 'wgArticlePath' =>
'/wiki/$1',
695 $msg =
new Message(
'returnto', [
696 new Message(
'apihelp-link', [
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>",
746 public function testParser( $expect, $format ) {
747 $msg =
new RawMessage(
"''&'' <x><!-- x -->" );
750 $msg->inLanguage(
'en' )->$format()
757 public function testInContentLanguage() {
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' )" );
770 public function testInContentLanguageOverride() {
772 'wgForceUIMsgAsContentMsg' => [
'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' )" );
797 public function testInLanguageThrows() {
805 public function testSerialization() {
806 $msg =
new Message(
'parentheses' );
807 $msg->rawParams(
'<a>foo</a>' );
809 $this->assertSame(
'(<a>foo</a>)', $msg->parse(),
'Sanity check' );
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' );
820 $this->assertSame(
'Hauptseite', $msg->plain() );
827 public function testNewFromSpecifier(
$value, $expectedText ) {
828 $message = Message::newFromSpecifier(
$value );
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() {
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)' ],