3 use Wikimedia\ObjectFactory;
4 use Wikimedia\TestingAccessWrapper;
15 'wgForceUIMsgAsContentMsg' => [],
27 $this->assertSame( $key, $message->getKey() );
28 $this->assertSame(
$params, $message->getParams() );
29 $this->assertEquals( $expectedLang, $message->getLanguage() );
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->assertEquals( $expectedLang, $message->getLanguage() );
48 [ $langDe,
'foo', [], $langDe ],
49 [ $langDe,
'foo', [
'bar' ], $langDe ],
50 [ $langEn,
'foo', [
'bar' ], null ]
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' ] ],
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() );
133 [
'foo', [
'bar' ],
'en' ],
134 [
'foo', [
'bar' ],
'de' ]
147 $this->assertEquals( $language, $message->getLanguage() );
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 );
195 $this->assertContains( $msg->getKey(), $expected );
196 $this->assertSame( $expected, $msg->getKeysToTry() );
197 $this->assertSame(
count( $expected ) > 1, $msg->isMultiKey() );
221 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto' )->
text() );
222 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto', [] )->
text() );
232 'You have foo (bar).',
236 'You have foo (bar).',
240 'You have 1,024 (bar).',
242 'youhavenewmessages',
247 'You have foo (2,048).',
249 'youhavenewmessages',
254 'You have 1,024 (2,048).',
256 'youhavenewmessages',
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() );
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()
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 ) {
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' );
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>' ],
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' );
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 'Заглавная страница',
376 '(Заглавная страница)',
380 '(Заглавная страница $1)',
384 '(Заглавная страница)',
385 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница' )->
plain()
388 '(Заглавная страница $1)',
389 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница $1' )->
plain()
399 $this->assertSame(
'example &', $msg->plain() );
400 $this->assertSame(
'example &', $msg->escaped() );
413 $msg =
new RawMessage(
'<html><script>alert("xss")</script></html>' );
414 $txt =
'<span class="error"><html> tags cannot be' .
415 ' used outside of normal pages.</span>';
416 $this->assertSame( $txt, $msg->parse() );
425 $msg =
new RawMessage(
'$1$2$3$4$5$6$7$8$9$10$11$12' );
427 $params = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k' ];
430 $msg->params(
$params )->plain(),
431 'Params > 9 are replaced correctly'
435 $params = [
'ab',
'bc',
'cd' ];
437 'Params: ab, bc, cd',
438 $msg->params(
$params )->text()
451 $lang->formatNum( 123456.789 ),
452 $msg->inLanguage(
$lang )->numParams( 123456.789 )->plain(),
453 'numParams is handled correctly'
466 $lang->formatDuration( 1234 ),
467 $msg->inLanguage(
$lang )->durationParams( 1234 )->plain(),
468 'durationParams is handled correctly'
484 'expiryParams is handled correctly'
497 $lang->formatTimePeriod( 1234 ),
498 $msg->inLanguage(
$lang )->timeperiodParams( 1234 )->plain(),
499 'timeperiodParams is handled correctly'
512 $lang->formatSize( 123456 ),
513 $msg->inLanguage(
$lang )->sizeParams( 123456 )->plain(),
514 'sizeParams is handled correctly'
527 $lang->formatBitrate( 123456 ),
528 $msg->inLanguage(
$lang )->bitrateParams( 123456 )->plain(),
529 'bitrateParams is handled correctly'
536 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
542 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
547 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
552 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
557 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
578 '<div>foo</div> [[Bar]] {{Baz}} <',
582 $msg->inLanguage(
$lang )->plaintextParams(
$params )->$format(),
583 "Fail formatting for $format"
593 'Simple comma list' => [
600 'Simple semicolon list' => [
607 'Simple pipe list' => [
614 'Simple text list' => [
628 'List with all "before" params, ->text()' => [
632 '\'\
'link\'\'; 12,345,678'
635 'List with all "before" params, ->parse()' => [
639 '<i>link</i>; 12,345,678'
642 'List with all "after" params, ->text()' => [
646 'Main Page; \'\'link\'\'; [[foo]]'
649 'List with all "after" params, ->parse()' => [
653 'Main Page; <i>link</i>; [[foo]]'
656 'List with both "before" and "after" params, ->text()' => [
660 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
663 'List with both "before" and "after" params, ->parse()' => [
667 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
685 $msg->inLanguage(
$lang )->$format()
694 'wgScript' =>
'/wiki/index.php',
695 'wgArticlePath' =>
'/wiki/$1',
698 $msg =
new Message(
'returnto', [
705 'Volver a [[Special:ApiHelp/foo|Página principal]].',
707 'Process with ->text()'
710 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
711 .
"principal</a>.\n</p>",
712 $msg->parseAsBlock(),
713 'Process with ->parseAsBlock()'
720 "''&'' <x><!-- x -->",
725 "''&'' <x><!-- x -->",
729 '<i>&</i> <x>',
734 "<p><i>&</i> <x>\n</p>",
750 $msg =
new RawMessage(
"''&'' <x><!-- x -->" );
753 $msg->inLanguage(
'en' )->$format()
765 $this->assertSame(
'Hauptseite', $msg->inLanguage(
'de' )->plain(),
"inLanguage( 'de' )" );
766 $this->assertSame(
'Main Page', $msg->inContentLanguage()->plain(),
"inContentLanguage()" );
767 $this->assertSame(
'Accueil', $msg->inLanguage(
'fr' )->plain(),
"inLanguage( 'fr' )" );
775 'wgForceUIMsgAsContentMsg' => [
'mainpage' ],
784 $msg->inContentLanguage()->plain(),
785 'inContentLanguage() with ForceUIMsg override enabled'
787 $this->assertSame(
'Main Page', $msg->inLanguage(
'en' )->plain(),
"inLanguage( 'en' )" );
790 $msg->inContentLanguage()->plain(),
791 'inContentLanguage() with ForceUIMsg override enabled'
793 $this->assertSame(
'Hauptseite', $msg->inLanguage(
'de' )->plain(),
"inLanguage( 'de' )" );
809 $msg =
new Message(
'parentheses' );
810 $msg->rawParams(
'<a>foo</a>' );
812 $this->assertSame(
'(<a>foo</a>)', $msg->parse(),
'Sanity check' );
814 $this->assertSame(
'(<a>foo</a>)', $msg->parse() );
815 $title = TestingAccessWrapper::newFromObject( $msg )->title;
817 $this->assertSame(
'Testing',
$title->getFullText() );
819 $msg =
new Message(
'mainpage' );
820 $msg->inLanguage(
'de' );
821 $this->assertSame(
'Hauptseite', $msg->plain(),
'Sanity check' );
823 $this->assertSame(
'Hauptseite', $msg->plain() );
834 $this->assertInstanceOf( get_class(
$value ), $message );
835 $this->assertEquals(
$value, $message );
837 $this->assertSame( $expectedText, $message->text() );
842 $messageSpecifier->expects( $this->
any() )->method(
'getKey' )->willReturn(
'mainpage' );
843 $messageSpecifier->expects( $this->
any() )->method(
'getParams' )->willReturn( [] );
846 'string' => [
'mainpage',
'Main Page' ],
847 'array' => [ [
'youhavenewmessages',
'foo',
'bar' ],
'You have foo (bar).' ],
848 'Message' => [
new Message(
'youhavenewmessages', [
'foo',
'bar' ] ),
'You have foo (bar).' ],
849 'RawMessage' => [
new RawMessage(
'foo ($1)', [
'bar' ] ),
'foo (bar)' ],
850 'ApiMessage' => [
new ApiMessage( [
'mainpage' ],
'code', [
'data' ] ),
'Main Page' ],
851 'MessageSpecifier' => [ $messageSpecifier,
'Main Page' ],
852 'nested RawMessage' => [ [
new RawMessage(
'foo ($1)', [
'bar' ] ) ],
'foo (bar)' ],