2d45c2ebf5a4042252e5f8ddc0d3c47d9bdf9132
[lhc/web/wiklou.git] / tests / phpunit / includes / MessageTest.php
1 <?php
2
3 class MessageTest extends MediaWikiLangTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7
8 $this->setMwGlobals( [
9 'wgForceUIMsgAsContentMsg' => [],
10 ] );
11 $this->setUserLang( 'en' );
12 }
13
14 /**
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
17 */
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $message = new Message( $key, $params, $language );
20
21 $this->assertSame( $key, $message->getKey() );
22 $this->assertSame( $params, $message->getParams() );
23 $this->assertEquals( $expectedLang, $message->getLanguage() );
24
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 );
31
32 $this->assertSame( $key, $message->getKey() );
33 $this->assertSame( $params, $message->getParams() );
34 $this->assertEquals( $expectedLang, $message->getLanguage() );
35 }
36
37 public static function provideConstructor() {
38 $langDe = Language::factory( 'de' );
39 $langEn = Language::factory( 'en' );
40
41 return [
42 [ $langDe, 'foo', [], $langDe ],
43 [ $langDe, 'foo', [ 'bar' ], $langDe ],
44 [ $langEn, 'foo', [ 'bar' ], null ]
45 ];
46 }
47
48 public static function provideConstructorParams() {
49 return [
50 [
51 [],
52 [],
53 ],
54 [
55 [],
56 [ [] ],
57 ],
58 [
59 [ 'foo' ],
60 [ 'foo' ],
61 ],
62 [
63 [ 'foo', 'bar' ],
64 [ 'foo', 'bar' ],
65 ],
66 [
67 [ 'baz' ],
68 [ [ 'baz' ] ],
69 ],
70 [
71 [ 'baz', 'foo' ],
72 [ [ 'baz', 'foo' ] ],
73 ],
74 [
75 [ Message::rawParam( 'baz' ) ],
76 [ Message::rawParam( 'baz' ) ],
77 ],
78 [
79 [ Message::rawParam( 'baz' ), 'foo' ],
80 [ Message::rawParam( 'baz' ), 'foo' ],
81 ],
82 [
83 [ Message::rawParam( 'baz' ) ],
84 [ [ Message::rawParam( 'baz' ) ] ],
85 ],
86 [
87 [ Message::rawParam( 'baz' ), 'foo' ],
88 [ [ Message::rawParam( 'baz' ), 'foo' ] ],
89 ],
90
91 // Test handling of erroneous input, to detect if it changes
92 [
93 [ [ 'baz', 'foo' ], 'hhh' ],
94 [ [ 'baz', 'foo' ], 'hhh' ],
95 ],
96 [
97 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
98 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
99 ],
100 [
101 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
102 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
103 ],
104 [
105 [ [ 'baz' ], [ 'ahahahahha' ] ],
106 [ [ 'baz' ], [ 'ahahahahha' ] ],
107 ],
108 ];
109 }
110
111 /**
112 * @covers Message::__construct
113 * @covers Message::getParams
114 * @dataProvider provideConstructorParams
115 */
116 public function testConstructorParams( $expected, $args ) {
117 $msg = new Message( 'imasomething' );
118
119 $returned = call_user_func_array( [ $msg, 'params' ], $args );
120
121 $this->assertSame( $msg, $returned );
122 $this->assertSame( $expected, $msg->getParams() );
123 }
124
125 public static function provideConstructorLanguage() {
126 return [
127 [ 'foo', [ 'bar' ], 'en' ],
128 [ 'foo', [ 'bar' ], 'de' ]
129 ];
130 }
131
132 /**
133 * @covers Message::__construct
134 * @covers Message::getLanguage
135 * @dataProvider provideConstructorLanguage
136 */
137 public function testConstructorLanguage( $key, $params, $languageCode ) {
138 $language = Language::factory( $languageCode );
139 $message = new Message( $key, $params, $language );
140
141 $this->assertEquals( $language, $message->getLanguage() );
142 }
143
144 public static function provideKeys() {
145 return [
146 'string' => [
147 'key' => 'mainpage',
148 'expected' => [ 'mainpage' ],
149 ],
150 'single' => [
151 'key' => [ 'mainpage' ],
152 'expected' => [ 'mainpage' ],
153 ],
154 'multi' => [
155 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
156 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
157 ],
158 'empty' => [
159 'key' => [],
160 'expected' => null,
161 'exception' => 'InvalidArgumentException',
162 ],
163 'null' => [
164 'key' => null,
165 'expected' => null,
166 'exception' => 'InvalidArgumentException',
167 ],
168 'bad type' => [
169 'key' => 123,
170 'expected' => null,
171 'exception' => 'InvalidArgumentException',
172 ],
173 ];
174 }
175
176 /**
177 * @covers Message::__construct
178 * @covers Message::getKey
179 * @covers Message::isMultiKey
180 * @covers Message::getKeysToTry
181 * @dataProvider provideKeys
182 */
183 public function testKeys( $key, $expected, $exception = null ) {
184 if ( $exception ) {
185 $this->setExpectedException( $exception );
186 }
187
188 $msg = new Message( $key );
189 $this->assertContains( $msg->getKey(), $expected );
190 $this->assertSame( $expected, $msg->getKeysToTry() );
191 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
192 }
193
194 /**
195 * @covers ::wfMessage
196 */
197 public function testWfMessage() {
198 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
199 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
200 }
201
202 /**
203 * @covers Message::newFromKey
204 */
205 public function testNewFromKey() {
206 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
207 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
208 }
209
210 /**
211 * @covers ::wfMessage
212 * @covers Message::__construct
213 */
214 public function testWfMessageParams() {
215 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
216 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
217 $this->assertSame(
218 'You have foo (bar).',
219 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
220 );
221 $this->assertSame(
222 'You have foo (bar).',
223 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
224 );
225 }
226
227 /**
228 * @covers Message::exists
229 */
230 public function testExists() {
231 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
232 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
233 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
234 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
235 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
236 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
237 }
238
239 /**
240 * @covers Message::__construct
241 * @covers Message::text
242 * @covers Message::plain
243 * @covers Message::escaped
244 * @covers Message::toString
245 */
246 public function testToStringKey() {
247 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
248 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
249 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
250 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
251 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
252 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
253 $this->assertSame(
254 '⧼i&lt;dont&gt;exist-evar⧽',
255 wfMessage( 'i<dont>exist-evar' )->escaped()
256 );
257 }
258
259 public static function provideToString() {
260 return [
261 // key, transformation, transformed, transformed implicitly
262 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
263 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
264 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
265 [ 'script>alert(1)</script', 'escaped', '⧼script&gt;alert(1)&lt;/script⧽',
266 '⧼script&gt;alert(1)&lt;/script⧽' ],
267 [ 'script>alert(1)</script', 'plain', '⧼script&gt;alert(1)&lt;/script⧽',
268 '⧼script&gt;alert(1)&lt;/script⧽' ],
269 ];
270 }
271
272 /**
273 * @covers Message::toString
274 * @covers Message::__toString
275 * @dataProvider provideToString
276 */
277 public function testToString( $key, $format, $expect, $expectImplicit ) {
278 $msg = new Message( $key );
279 $this->assertSame( $expect, $msg->$format() );
280 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
281 $this->assertSame( $expectImplicit, $msg->__toString() );
282 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
283 }
284
285 public static function provideToString_raw() {
286 return [
287 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
288 [ '<span>foo</span>', 'escaped', '&lt;span&gt;foo&lt;/span&gt;',
289 '<span>foo</span>' ],
290 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
291 [ '<script>alert(1)</script>', 'parse', '&lt;script&gt;alert(1)&lt;/script&gt;',
292 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
293 [ '<script>alert(1)</script>', 'escaped', '&lt;script&gt;alert(1)&lt;/script&gt;',
294 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
295 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
296 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
297 ];
298 }
299
300 /**
301 * @covers Message::toString
302 * @covers Message::__toString
303 * @dataProvider provideToString_raw
304 */
305 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
306 // make the message behave like RawMessage and use the key as-is
307 $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] )
308 ->disableOriginalConstructor()
309 ->getMock();
310 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
311 /** @var Message $msg */
312 $this->assertSame( $expect, $msg->$format() );
313 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
314 $this->assertSame( $expectImplicit, $msg->__toString() );
315 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
316 }
317
318 /**
319 * @covers Message::inLanguage
320 */
321 public function testInLanguage() {
322 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
323 $this->assertSame( 'Заглавная страница',
324 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
325
326 // NOTE: make sure internal caching of the message text is reset appropriately
327 $msg = wfMessage( 'mainpage' );
328 $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
329 $this->assertSame(
330 'Заглавная страница',
331 $msg->inLanguage( Language::factory( 'ru' ) )->text()
332 );
333 }
334
335 /**
336 * @covers Message::rawParam
337 * @covers Message::rawParams
338 */
339 public function testRawParams() {
340 $this->assertSame(
341 '(Заглавная страница)',
342 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
343 );
344 $this->assertSame(
345 '(Заглавная страница $1)',
346 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
347 );
348 $this->assertSame(
349 '(Заглавная страница)',
350 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
351 );
352 $this->assertSame(
353 '(Заглавная страница $1)',
354 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
355 );
356 }
357
358 /**
359 * @covers RawMessage::__construct
360 * @covers RawMessage::fetchMessage
361 */
362 public function testRawMessage() {
363 $msg = new RawMessage( 'example &' );
364 $this->assertSame( 'example &', $msg->plain() );
365 $this->assertSame( 'example &amp;', $msg->escaped() );
366 }
367
368 /**
369 * @covers Message::params
370 * @covers Message::toString
371 * @covers Message::replaceParameters
372 */
373 public function testReplaceManyParams() {
374 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
375 // One less than above has placeholders
376 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
377 $this->assertSame(
378 'abcdefghijka2',
379 $msg->params( $params )->plain(),
380 'Params > 9 are replaced correctly'
381 );
382
383 $msg = new RawMessage( 'Params$*' );
384 $params = [ 'ab', 'bc', 'cd' ];
385 $this->assertSame(
386 'Params: ab, bc, cd',
387 $msg->params( $params )->text()
388 );
389 }
390
391 /**
392 * @covers Message::numParam
393 * @covers Message::numParams
394 */
395 public function testNumParams() {
396 $lang = Language::factory( 'en' );
397 $msg = new RawMessage( '$1' );
398
399 $this->assertSame(
400 $lang->formatNum( 123456.789 ),
401 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
402 'numParams is handled correctly'
403 );
404 }
405
406 /**
407 * @covers Message::durationParam
408 * @covers Message::durationParams
409 */
410 public function testDurationParams() {
411 $lang = Language::factory( 'en' );
412 $msg = new RawMessage( '$1' );
413
414 $this->assertSame(
415 $lang->formatDuration( 1234 ),
416 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
417 'durationParams is handled correctly'
418 );
419 }
420
421 /**
422 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
423 * @group Database
424 * @covers Message::expiryParam
425 * @covers Message::expiryParams
426 */
427 public function testExpiryParams() {
428 $lang = Language::factory( 'en' );
429 $msg = new RawMessage( '$1' );
430
431 $this->assertSame(
432 $lang->formatExpiry( wfTimestampNow() ),
433 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
434 'expiryParams is handled correctly'
435 );
436 }
437
438 /**
439 * @covers Message::timeperiodParam
440 * @covers Message::timeperiodParams
441 */
442 public function testTimeperiodParams() {
443 $lang = Language::factory( 'en' );
444 $msg = new RawMessage( '$1' );
445
446 $this->assertSame(
447 $lang->formatTimePeriod( 1234 ),
448 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
449 'timeperiodParams is handled correctly'
450 );
451 }
452
453 /**
454 * @covers Message::sizeParam
455 * @covers Message::sizeParams
456 */
457 public function testSizeParams() {
458 $lang = Language::factory( 'en' );
459 $msg = new RawMessage( '$1' );
460
461 $this->assertSame(
462 $lang->formatSize( 123456 ),
463 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
464 'sizeParams is handled correctly'
465 );
466 }
467
468 /**
469 * @covers Message::bitrateParam
470 * @covers Message::bitrateParams
471 */
472 public function testBitrateParams() {
473 $lang = Language::factory( 'en' );
474 $msg = new RawMessage( '$1' );
475
476 $this->assertSame(
477 $lang->formatBitrate( 123456 ),
478 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
479 'bitrateParams is handled correctly'
480 );
481 }
482
483 public static function providePlaintextParams() {
484 return [
485 [
486 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
487 'plain',
488 ],
489
490 [
491 // expect
492 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
493 // format
494 'text',
495 ],
496 [
497 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
498 'escaped',
499 ],
500
501 [
502 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
503 'parse',
504 ],
505
506 [
507 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
508 'parseAsBlock',
509 ],
510 ];
511 }
512
513 /**
514 * @covers Message::plaintextParam
515 * @covers Message::plaintextParams
516 * @covers Message::formatPlaintext
517 * @covers Message::toString
518 * @covers Message::parse
519 * @covers Message::parseAsBlock
520 * @dataProvider providePlaintextParams
521 */
522 public function testPlaintextParams( $expect, $format ) {
523 $lang = Language::factory( 'en' );
524
525 $msg = new RawMessage( '$1 $2' );
526 $params = [
527 'one $2',
528 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
529 ];
530 $this->assertSame(
531 $expect,
532 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
533 "Fail formatting for $format"
534 );
535 }
536
537 public static function provideListParam() {
538 $lang = Language::factory( 'de' );
539 $msg1 = new Message( 'mainpage', [], $lang );
540 $msg2 = new RawMessage( "''link''", [], $lang );
541
542 return [
543 'Simple comma list' => [
544 [ 'a', 'b', 'c' ],
545 'comma',
546 'text',
547 'a, b, c'
548 ],
549
550 'Simple semicolon list' => [
551 [ 'a', 'b', 'c' ],
552 'semicolon',
553 'text',
554 'a; b; c'
555 ],
556
557 'Simple pipe list' => [
558 [ 'a', 'b', 'c' ],
559 'pipe',
560 'text',
561 'a | b | c'
562 ],
563
564 'Simple text list' => [
565 [ 'a', 'b', 'c' ],
566 'text',
567 'text',
568 'a, b and c'
569 ],
570
571 'Empty list' => [
572 [],
573 'comma',
574 'text',
575 ''
576 ],
577
578 'List with all "before" params, ->text()' => [
579 [ "''link''", Message::numParam( 12345678 ) ],
580 'semicolon',
581 'text',
582 '\'\'link\'\'; 12,345,678'
583 ],
584
585 'List with all "before" params, ->parse()' => [
586 [ "''link''", Message::numParam( 12345678 ) ],
587 'semicolon',
588 'parse',
589 '<i>link</i>; 12,345,678'
590 ],
591
592 'List with all "after" params, ->text()' => [
593 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
594 'semicolon',
595 'text',
596 'Main Page; \'\'link\'\'; [[foo]]'
597 ],
598
599 'List with all "after" params, ->parse()' => [
600 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
601 'semicolon',
602 'parse',
603 'Main Page; <i>link</i>; [[foo]]'
604 ],
605
606 'List with both "before" and "after" params, ->text()' => [
607 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
608 'semicolon',
609 'text',
610 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
611 ],
612
613 'List with both "before" and "after" params, ->parse()' => [
614 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
615 'semicolon',
616 'parse',
617 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
618 ],
619 ];
620 }
621
622 /**
623 * @covers Message::listParam
624 * @covers Message::extractParam
625 * @covers Message::formatListParam
626 * @dataProvider provideListParam
627 */
628 public function testListParam( $list, $type, $format, $expect ) {
629 $lang = Language::factory( 'en' );
630
631 $msg = new RawMessage( '$1' );
632 $msg->params( [ Message::listParam( $list, $type ) ] );
633 $this->assertEquals(
634 $expect,
635 $msg->inLanguage( $lang )->$format()
636 );
637 }
638
639 /**
640 * @covers Message::extractParam
641 */
642 public function testMessageAsParam() {
643 $this->setMwGlobals( [
644 'wgScript' => '/wiki/index.php',
645 'wgArticlePath' => '/wiki/$1',
646 ] );
647
648 $msg = new Message( 'returnto', [
649 new Message( 'apihelp-link', [
650 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
651 ], Language::factory( 'de' ) )
652 ], Language::factory( 'es' ) );
653
654 $this->assertEquals(
655 'Volver a [[Special:ApiHelp/foo|Página principal]].',
656 $msg->text(),
657 'Process with ->text()'
658 );
659 $this->assertEquals(
660 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
661 . "principal</a>.\n</p>",
662 $msg->parseAsBlock(),
663 'Process with ->parseAsBlock()'
664 );
665 }
666
667 public static function provideParser() {
668 return [
669 [
670 "''&'' <x><!-- x -->",
671 'plain',
672 ],
673
674 [
675 "''&'' <x><!-- x -->",
676 'text',
677 ],
678 [
679 '<i>&amp;</i> &lt;x&gt;',
680 'parse',
681 ],
682
683 [
684 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
685 'parseAsBlock',
686 ],
687 ];
688 }
689
690 /**
691 * @covers Message::text
692 * @covers Message::parse
693 * @covers Message::parseAsBlock
694 * @covers Message::toString
695 * @covers Message::transformText
696 * @covers Message::parseText
697 * @dataProvider provideParser
698 */
699 public function testParser( $expect, $format ) {
700 $msg = new RawMessage( "''&'' <x><!-- x -->" );
701 $this->assertSame(
702 $expect,
703 $msg->inLanguage( 'en' )->$format()
704 );
705 }
706
707 /**
708 * @covers Message::inContentLanguage
709 */
710 public function testInContentLanguage() {
711 $this->setUserLang( 'fr' );
712
713 // NOTE: make sure internal caching of the message text is reset appropriately
714 $msg = wfMessage( 'mainpage' );
715 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
716 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
717 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
718 }
719
720 /**
721 * @covers Message::inContentLanguage
722 */
723 public function testInContentLanguageOverride() {
724 $this->setMwGlobals( [
725 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
726 ] );
727 $this->setUserLang( 'fr' );
728
729 // NOTE: make sure internal caching of the message text is reset appropriately.
730 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
731 $msg = wfMessage( 'mainpage' );
732 $this->assertSame(
733 'Accueil',
734 $msg->inContentLanguage()->plain(),
735 'inContentLanguage() with ForceUIMsg override enabled'
736 );
737 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
738 $this->assertSame(
739 'Main Page',
740 $msg->inContentLanguage()->plain(),
741 'inContentLanguage() with ForceUIMsg override enabled'
742 );
743 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
744 }
745
746 /**
747 * @expectedException MWException
748 * @covers Message::inLanguage
749 */
750 public function testInLanguageThrows() {
751 wfMessage( 'foo' )->inLanguage( 123 );
752 }
753
754 /**
755 * @covers Message::serialize
756 * @covers Message::unserialize
757 */
758 public function testSerialization() {
759 $msg = new Message( 'parentheses' );
760 $msg->rawParams( '<a>foo</a>' );
761 $msg->title( Title::newFromText( 'Testing' ) );
762 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
763 $msg = unserialize( serialize( $msg ) );
764 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
765 $title = TestingAccessWrapper::newFromObject( $msg )->title;
766 $this->assertInstanceOf( 'Title', $title );
767 $this->assertSame( 'Testing', $title->getFullText() );
768
769 $msg = new Message( 'mainpage' );
770 $msg->inLanguage( 'de' );
771 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
772 $msg = unserialize( serialize( $msg ) );
773 $this->assertSame( 'Hauptseite', $msg->plain() );
774 }
775
776 /**
777 * @covers Message::newFromSpecifier
778 * @dataProvider provideNewFromSpecifier
779 */
780 public function testNewFromSpecifier( $value, $expectedText ) {
781 $message = Message::newFromSpecifier( $value );
782 $this->assertInstanceOf( Message::class, $message );
783 if ( $value instanceof Message ) {
784 $this->assertInstanceOf( get_class( $value ), $message );
785 $this->assertEquals( $value, $message );
786 }
787 $this->assertSame( $expectedText, $message->text() );
788 }
789
790 public function provideNewFromSpecifier() {
791 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
792 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
793 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
794
795 return [
796 'string' => [ 'mainpage', 'Main Page' ],
797 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
798 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
799 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
800 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
801 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
802 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
803 ];
804 }
805 }