Make cache object mandatory for MessageCache
[mediawiki.git] / tests / phpunit / includes / MessageTest.php
blob8390e1fd397c1928c585e89482903810d967f7e3
1 <?php
3 class MessageTest extends MediaWikiLangTestCase {
5 protected function setUp() {
6 parent::setUp();
8 $this->setMwGlobals( [
9 'wgForceUIMsgAsContentMsg' => [],
10 ] );
11 $this->setUserLang( 'en' );
14 /**
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $message = new Message( $key, $params, $language );
21 $this->assertSame( $key, $message->getKey() );
22 $this->assertSame( $params, $message->getParams() );
23 $this->assertEquals( $expectedLang, $message->getLanguage() );
25 $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
26 $messageSpecifier->expects( $this->any() )
27 ->method( 'getKey' )->will( $this->returnValue( $key ) );
28 $messageSpecifier->expects( $this->any() )
29 ->method( 'getParams' )->will( $this->returnValue( $params ) );
30 $message = new Message( $messageSpecifier, [], $language );
32 $this->assertSame( $key, $message->getKey() );
33 $this->assertSame( $params, $message->getParams() );
34 $this->assertEquals( $expectedLang, $message->getLanguage() );
37 public static function provideConstructor() {
38 $langDe = Language::factory( 'de' );
39 $langEn = Language::factory( 'en' );
41 return [
42 [ $langDe, 'foo', [], $langDe ],
43 [ $langDe, 'foo', [ 'bar' ], $langDe ],
44 [ $langEn, 'foo', [ 'bar' ], null ]
48 public static function provideConstructorParams() {
49 return [
51 [],
52 [],
55 [ 'foo' ],
56 [ 'foo' ],
59 [ 'foo', 'bar' ],
60 [ 'foo', 'bar' ],
63 [ 'baz' ],
64 [ [ 'baz' ] ],
67 [ 'baz', 'foo' ],
68 [ [ 'baz', 'foo' ] ],
71 [ 'baz', 'foo' ],
72 [ [ 'baz', 'foo' ], 'hhh' ],
75 [ 'baz', 'foo' ],
76 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
79 [ 'baz', 'foo' ],
80 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
83 [ 'baz' ],
84 [ [ 'baz' ], [ 'ahahahahha' ] ],
89 /**
90 * @covers Message::__construct
91 * @covers Message::getParams
92 * @dataProvider provideConstructorParams
94 public function testConstructorParams( $expected, $args ) {
95 $msg = new Message( 'imasomething' );
97 $returned = call_user_func_array( [ $msg, 'params' ], $args );
99 $this->assertSame( $msg, $returned );
100 $this->assertSame( $expected, $msg->getParams() );
103 public static function provideConstructorLanguage() {
104 return [
105 [ 'foo', [ 'bar' ], 'en' ],
106 [ 'foo', [ 'bar' ], 'de' ]
111 * @covers Message::__construct
112 * @covers Message::getLanguage
113 * @dataProvider provideConstructorLanguage
115 public function testConstructorLanguage( $key, $params, $languageCode ) {
116 $language = Language::factory( $languageCode );
117 $message = new Message( $key, $params, $language );
119 $this->assertEquals( $language, $message->getLanguage() );
122 public static function provideKeys() {
123 return [
124 'string' => [
125 'key' => 'mainpage',
126 'expected' => [ 'mainpage' ],
128 'single' => [
129 'key' => [ 'mainpage' ],
130 'expected' => [ 'mainpage' ],
132 'multi' => [
133 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
134 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
136 'empty' => [
137 'key' => [],
138 'expected' => null,
139 'exception' => 'InvalidArgumentException',
141 'null' => [
142 'key' => null,
143 'expected' => null,
144 'exception' => 'InvalidArgumentException',
146 'bad type' => [
147 'key' => 123,
148 'expected' => null,
149 'exception' => 'InvalidArgumentException',
155 * @covers Message::__construct
156 * @covers Message::getKey
157 * @covers Message::isMultiKey
158 * @covers Message::getKeysToTry
159 * @dataProvider provideKeys
161 public function testKeys( $key, $expected, $exception = null ) {
162 if ( $exception ) {
163 $this->setExpectedException( $exception );
166 $msg = new Message( $key );
167 $this->assertContains( $msg->getKey(), $expected );
168 $this->assertSame( $expected, $msg->getKeysToTry() );
169 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
173 * @covers ::wfMessage
175 public function testWfMessage() {
176 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
177 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
181 * @covers Message::newFromKey
183 public function testNewFromKey() {
184 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
185 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
189 * @covers ::wfMessage
190 * @covers Message::__construct
192 public function testWfMessageParams() {
193 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
194 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
195 $this->assertSame(
196 'You have foo (bar).',
197 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
199 $this->assertSame(
200 'You have foo (bar).',
201 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
206 * @covers Message::exists
208 public function testExists() {
209 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
210 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
211 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
212 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
213 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
214 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
218 * @covers Message::__construct
219 * @covers Message::text
220 * @covers Message::plain
221 * @covers Message::escaped
222 * @covers Message::toString
224 public function testToStringKey() {
225 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
226 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
227 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
228 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
229 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
230 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
231 $this->assertSame(
232 '⧼i&lt;dont&gt;exist-evar⧽',
233 wfMessage( 'i<dont>exist-evar' )->escaped()
237 public static function provideToString() {
238 return [
239 // key, transformation, transformed, transformed implicitly
240 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
241 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
242 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
243 [ 'script>alert(1)</script', 'escaped', '⧼script&gt;alert(1)&lt;/script⧽',
244 '⧼script&gt;alert(1)&lt;/script⧽' ],
245 [ 'script>alert(1)</script', 'plain', '⧼script&gt;alert(1)&lt;/script⧽',
246 '⧼script&gt;alert(1)&lt;/script⧽' ],
251 * @covers Message::toString
252 * @covers Message::__toString
253 * @dataProvider provideToString
255 public function testToString( $key, $format, $expect, $expectImplicit ) {
256 $msg = new Message( $key );
257 $this->assertSame( $expect, $msg->$format() );
258 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
259 $this->assertSame( $expectImplicit, $msg->__toString() );
260 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
263 public static function provideToString_raw() {
264 return [
265 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
266 [ '<span>foo</span>', 'escaped', '&lt;span&gt;foo&lt;/span&gt;',
267 '<span>foo</span>' ],
268 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
269 [ '<script>alert(1)</script>', 'parse', '&lt;script&gt;alert(1)&lt;/script&gt;',
270 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
271 [ '<script>alert(1)</script>', 'escaped', '&lt;script&gt;alert(1)&lt;/script&gt;',
272 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
273 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
274 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
279 * @covers Message::toString
280 * @covers Message::__toString
281 * @dataProvider provideToString_raw
283 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
284 // make the message behave like RawMessage and use the key as-is
285 $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] )
286 ->disableOriginalConstructor()
287 ->getMock();
288 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
289 /** @var Message $msg */
290 $this->assertSame( $expect, $msg->$format() );
291 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
292 $this->assertSame( $expectImplicit, $msg->__toString() );
293 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
297 * @covers Message::inLanguage
299 public function testInLanguage() {
300 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
301 $this->assertSame( 'Заглавная страница',
302 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
304 // NOTE: make sure internal caching of the message text is reset appropriately
305 $msg = wfMessage( 'mainpage' );
306 $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
307 $this->assertSame(
308 'Заглавная страница',
309 $msg->inLanguage( Language::factory( 'ru' ) )->text()
314 * @covers Message::rawParam
315 * @covers Message::rawParams
317 public function testRawParams() {
318 $this->assertSame(
319 '(Заглавная страница)',
320 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
322 $this->assertSame(
323 '(Заглавная страница $1)',
324 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
326 $this->assertSame(
327 '(Заглавная страница)',
328 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
330 $this->assertSame(
331 '(Заглавная страница $1)',
332 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
337 * @covers RawMessage::__construct
338 * @covers RawMessage::fetchMessage
340 public function testRawMessage() {
341 $msg = new RawMessage( 'example &' );
342 $this->assertSame( 'example &', $msg->plain() );
343 $this->assertSame( 'example &amp;', $msg->escaped() );
347 * @covers Message::params
348 * @covers Message::toString
349 * @covers Message::replaceParameters
351 public function testReplaceManyParams() {
352 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
353 // One less than above has placeholders
354 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
355 $this->assertSame(
356 'abcdefghijka2',
357 $msg->params( $params )->plain(),
358 'Params > 9 are replaced correctly'
361 $msg = new RawMessage( 'Params$*' );
362 $params = [ 'ab', 'bc', 'cd' ];
363 $this->assertSame(
364 'Params: ab, bc, cd',
365 $msg->params( $params )->text()
370 * @covers Message::numParam
371 * @covers Message::numParams
373 public function testNumParams() {
374 $lang = Language::factory( 'en' );
375 $msg = new RawMessage( '$1' );
377 $this->assertSame(
378 $lang->formatNum( 123456.789 ),
379 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
380 'numParams is handled correctly'
385 * @covers Message::durationParam
386 * @covers Message::durationParams
388 public function testDurationParams() {
389 $lang = Language::factory( 'en' );
390 $msg = new RawMessage( '$1' );
392 $this->assertSame(
393 $lang->formatDuration( 1234 ),
394 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
395 'durationParams is handled correctly'
400 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
401 * @group Database
402 * @covers Message::expiryParam
403 * @covers Message::expiryParams
405 public function testExpiryParams() {
406 $lang = Language::factory( 'en' );
407 $msg = new RawMessage( '$1' );
409 $this->assertSame(
410 $lang->formatExpiry( wfTimestampNow() ),
411 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
412 'expiryParams is handled correctly'
417 * @covers Message::timeperiodParam
418 * @covers Message::timeperiodParams
420 public function testTimeperiodParams() {
421 $lang = Language::factory( 'en' );
422 $msg = new RawMessage( '$1' );
424 $this->assertSame(
425 $lang->formatTimePeriod( 1234 ),
426 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
427 'timeperiodParams is handled correctly'
432 * @covers Message::sizeParam
433 * @covers Message::sizeParams
435 public function testSizeParams() {
436 $lang = Language::factory( 'en' );
437 $msg = new RawMessage( '$1' );
439 $this->assertSame(
440 $lang->formatSize( 123456 ),
441 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
442 'sizeParams is handled correctly'
447 * @covers Message::bitrateParam
448 * @covers Message::bitrateParams
450 public function testBitrateParams() {
451 $lang = Language::factory( 'en' );
452 $msg = new RawMessage( '$1' );
454 $this->assertSame(
455 $lang->formatBitrate( 123456 ),
456 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
457 'bitrateParams is handled correctly'
461 public static function providePlaintextParams() {
462 return [
464 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
465 'plain',
469 // expect
470 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
471 // format
472 'text',
475 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
476 'escaped',
480 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
481 'parse',
485 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
486 'parseAsBlock',
492 * @covers Message::plaintextParam
493 * @covers Message::plaintextParams
494 * @covers Message::formatPlaintext
495 * @covers Message::toString
496 * @covers Message::parse
497 * @covers Message::parseAsBlock
498 * @dataProvider providePlaintextParams
500 public function testPlaintextParams( $expect, $format ) {
501 $lang = Language::factory( 'en' );
503 $msg = new RawMessage( '$1 $2' );
504 $params = [
505 'one $2',
506 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
508 $this->assertSame(
509 $expect,
510 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
511 "Fail formatting for $format"
515 public static function provideParser() {
516 return [
518 "''&'' <x><!-- x -->",
519 'plain',
523 "''&'' <x><!-- x -->",
524 'text',
527 '<i>&amp;</i> &lt;x&gt;',
528 'parse',
532 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
533 'parseAsBlock',
539 * @covers Message::text
540 * @covers Message::parse
541 * @covers Message::parseAsBlock
542 * @covers Message::toString
543 * @covers Message::transformText
544 * @covers Message::parseText
545 * @dataProvider provideParser
547 public function testParser( $expect, $format ) {
548 $msg = new RawMessage( "''&'' <x><!-- x -->" );
549 $this->assertSame(
550 $expect,
551 $msg->inLanguage( 'en' )->$format()
556 * @covers Message::inContentLanguage
558 public function testInContentLanguage() {
559 $this->setUserLang( 'fr' );
561 // NOTE: make sure internal caching of the message text is reset appropriately
562 $msg = wfMessage( 'mainpage' );
563 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
564 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
565 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
569 * @covers Message::inContentLanguage
571 public function testInContentLanguageOverride() {
572 $this->setMwGlobals( [
573 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
574 ] );
575 $this->setUserLang( 'fr' );
577 // NOTE: make sure internal caching of the message text is reset appropriately.
578 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
579 $msg = wfMessage( 'mainpage' );
580 $this->assertSame(
581 'Accueil',
582 $msg->inContentLanguage()->plain(),
583 'inContentLanguage() with ForceUIMsg override enabled'
585 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
586 $this->assertSame(
587 'Main Page',
588 $msg->inContentLanguage()->plain(),
589 'inContentLanguage() with ForceUIMsg override enabled'
591 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
595 * @expectedException MWException
596 * @covers Message::inLanguage
598 public function testInLanguageThrows() {
599 wfMessage( 'foo' )->inLanguage( 123 );
603 * @covers Message::serialize
604 * @covers Message::unserialize
606 public function testSerialization() {
607 $msg = new Message( 'parentheses' );
608 $msg->rawParams( '<a>foo</a>' );
609 $msg->title( Title::newFromText( 'Testing' ) );
610 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
611 $msg = unserialize( serialize( $msg ) );
612 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
613 $title = TestingAccessWrapper::newFromObject( $msg )->title;
614 $this->assertInstanceOf( 'Title', $title );
615 $this->assertSame( 'Testing', $title->getFullText() );
617 $msg = new Message( 'mainpage' );
618 $msg->inLanguage( 'de' );
619 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
620 $msg = unserialize( serialize( $msg ) );
621 $this->assertSame( 'Hauptseite', $msg->plain() );
625 * @covers Message::newFromSpecifier
626 * @dataProvider provideNewFromSpecifier
628 public function testNewFromSpecifier( $value, $expectedText ) {
629 $message = Message::newFromSpecifier( $value );
630 $this->assertInstanceOf( Message::class, $message );
631 if ( $value instanceof Message ) {
632 $this->assertInstanceOf( get_class( $value ), $message );
633 $this->assertEquals( $value, $message );
635 $this->assertSame( $expectedText, $message->text() );
638 public function provideNewFromSpecifier() {
639 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
640 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
641 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
643 return [
644 'string' => [ 'mainpage', 'Main Page' ],
645 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
646 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
647 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
648 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
649 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
650 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],