wfMessage: use Message::params() to handle all the message parameters
[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 'Return to 1,024.',
219 wfMessage( 'returnto', Message::numParam( 1024 ) )->text()
220 );
221 $this->assertSame(
222 'Return to 1,024.',
223 wfMessage( 'returnto', [ Message::numParam( 1024 ) ] )->text()
224 );
225 $this->assertSame(
226 'You have foo (bar).',
227 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
228 );
229 $this->assertSame(
230 'You have foo (bar).',
231 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
232 );
233 $this->assertSame(
234 'You have 1,024 (bar).',
235 wfMessage(
236 'youhavenewmessages',
237 Message::numParam( 1024 ), 'bar'
238 )->text()
239 );
240 $this->assertSame(
241 'You have foo (2,048).',
242 wfMessage(
243 'youhavenewmessages',
244 'foo', Message::numParam( 2048 )
245 )->text()
246 );
247 $this->assertSame(
248 'You have 1,024 (2,048).',
249 wfMessage(
250 'youhavenewmessages',
251 [ Message::numParam( 1024 ), Message::numParam( 2048 ) ]
252 )->text()
253 );
254 }
255
256 /**
257 * @covers Message::exists
258 */
259 public function testExists() {
260 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
261 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
262 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
263 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
264 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
265 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
266 }
267
268 /**
269 * @covers Message::__construct
270 * @covers Message::text
271 * @covers Message::plain
272 * @covers Message::escaped
273 * @covers Message::toString
274 */
275 public function testToStringKey() {
276 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
277 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
278 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
279 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
280 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
281 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
282 $this->assertSame(
283 '⧼i&lt;dont&gt;exist-evar⧽',
284 wfMessage( 'i<dont>exist-evar' )->escaped()
285 );
286 }
287
288 public static function provideToString() {
289 return [
290 // key, transformation, transformed, transformed implicitly
291 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
292 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
293 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
294 [ 'script>alert(1)</script', 'escaped', '⧼script&gt;alert(1)&lt;/script⧽',
295 '⧼script&gt;alert(1)&lt;/script⧽' ],
296 [ 'script>alert(1)</script', 'plain', '⧼script&gt;alert(1)&lt;/script⧽',
297 '⧼script&gt;alert(1)&lt;/script⧽' ],
298 ];
299 }
300
301 /**
302 * @covers Message::toString
303 * @covers Message::__toString
304 * @dataProvider provideToString
305 */
306 public function testToString( $key, $format, $expect, $expectImplicit ) {
307 $msg = new Message( $key );
308 $this->assertSame( $expect, $msg->$format() );
309 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
310 $this->assertSame( $expectImplicit, $msg->__toString() );
311 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
312 }
313
314 public static function provideToString_raw() {
315 return [
316 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
317 [ '<span>foo</span>', 'escaped', '&lt;span&gt;foo&lt;/span&gt;',
318 '<span>foo</span>' ],
319 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
320 [ '<script>alert(1)</script>', 'parse', '&lt;script&gt;alert(1)&lt;/script&gt;',
321 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
322 [ '<script>alert(1)</script>', 'escaped', '&lt;script&gt;alert(1)&lt;/script&gt;',
323 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
324 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
325 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
326 ];
327 }
328
329 /**
330 * @covers Message::toString
331 * @covers Message::__toString
332 * @dataProvider provideToString_raw
333 */
334 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
335 // make the message behave like RawMessage and use the key as-is
336 $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] )
337 ->disableOriginalConstructor()
338 ->getMock();
339 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
340 /** @var Message $msg */
341 $this->assertSame( $expect, $msg->$format() );
342 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
343 $this->assertSame( $expectImplicit, $msg->__toString() );
344 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
345 }
346
347 /**
348 * @covers Message::inLanguage
349 */
350 public function testInLanguage() {
351 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
352 $this->assertSame( 'Заглавная страница',
353 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
354
355 // NOTE: make sure internal caching of the message text is reset appropriately
356 $msg = wfMessage( 'mainpage' );
357 $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
358 $this->assertSame(
359 'Заглавная страница',
360 $msg->inLanguage( Language::factory( 'ru' ) )->text()
361 );
362 }
363
364 /**
365 * @covers Message::rawParam
366 * @covers Message::rawParams
367 */
368 public function testRawParams() {
369 $this->assertSame(
370 '(Заглавная страница)',
371 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
372 );
373 $this->assertSame(
374 '(Заглавная страница $1)',
375 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
376 );
377 $this->assertSame(
378 '(Заглавная страница)',
379 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
380 );
381 $this->assertSame(
382 '(Заглавная страница $1)',
383 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
384 );
385 }
386
387 /**
388 * @covers RawMessage::__construct
389 * @covers RawMessage::fetchMessage
390 */
391 public function testRawMessage() {
392 $msg = new RawMessage( 'example &' );
393 $this->assertSame( 'example &', $msg->plain() );
394 $this->assertSame( 'example &amp;', $msg->escaped() );
395 }
396
397 /**
398 * @covers Message::params
399 * @covers Message::toString
400 * @covers Message::replaceParameters
401 */
402 public function testReplaceManyParams() {
403 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
404 // One less than above has placeholders
405 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
406 $this->assertSame(
407 'abcdefghijka2',
408 $msg->params( $params )->plain(),
409 'Params > 9 are replaced correctly'
410 );
411
412 $msg = new RawMessage( 'Params$*' );
413 $params = [ 'ab', 'bc', 'cd' ];
414 $this->assertSame(
415 'Params: ab, bc, cd',
416 $msg->params( $params )->text()
417 );
418 }
419
420 /**
421 * @covers Message::numParam
422 * @covers Message::numParams
423 */
424 public function testNumParams() {
425 $lang = Language::factory( 'en' );
426 $msg = new RawMessage( '$1' );
427
428 $this->assertSame(
429 $lang->formatNum( 123456.789 ),
430 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
431 'numParams is handled correctly'
432 );
433 }
434
435 /**
436 * @covers Message::durationParam
437 * @covers Message::durationParams
438 */
439 public function testDurationParams() {
440 $lang = Language::factory( 'en' );
441 $msg = new RawMessage( '$1' );
442
443 $this->assertSame(
444 $lang->formatDuration( 1234 ),
445 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
446 'durationParams is handled correctly'
447 );
448 }
449
450 /**
451 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
452 * @group Database
453 * @covers Message::expiryParam
454 * @covers Message::expiryParams
455 */
456 public function testExpiryParams() {
457 $lang = Language::factory( 'en' );
458 $msg = new RawMessage( '$1' );
459
460 $this->assertSame(
461 $lang->formatExpiry( wfTimestampNow() ),
462 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
463 'expiryParams is handled correctly'
464 );
465 }
466
467 /**
468 * @covers Message::timeperiodParam
469 * @covers Message::timeperiodParams
470 */
471 public function testTimeperiodParams() {
472 $lang = Language::factory( 'en' );
473 $msg = new RawMessage( '$1' );
474
475 $this->assertSame(
476 $lang->formatTimePeriod( 1234 ),
477 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
478 'timeperiodParams is handled correctly'
479 );
480 }
481
482 /**
483 * @covers Message::sizeParam
484 * @covers Message::sizeParams
485 */
486 public function testSizeParams() {
487 $lang = Language::factory( 'en' );
488 $msg = new RawMessage( '$1' );
489
490 $this->assertSame(
491 $lang->formatSize( 123456 ),
492 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
493 'sizeParams is handled correctly'
494 );
495 }
496
497 /**
498 * @covers Message::bitrateParam
499 * @covers Message::bitrateParams
500 */
501 public function testBitrateParams() {
502 $lang = Language::factory( 'en' );
503 $msg = new RawMessage( '$1' );
504
505 $this->assertSame(
506 $lang->formatBitrate( 123456 ),
507 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
508 'bitrateParams is handled correctly'
509 );
510 }
511
512 public static function providePlaintextParams() {
513 return [
514 [
515 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
516 'plain',
517 ],
518
519 [
520 // expect
521 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
522 // format
523 'text',
524 ],
525 [
526 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
527 'escaped',
528 ],
529
530 [
531 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
532 'parse',
533 ],
534
535 [
536 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
537 'parseAsBlock',
538 ],
539 ];
540 }
541
542 /**
543 * @covers Message::plaintextParam
544 * @covers Message::plaintextParams
545 * @covers Message::formatPlaintext
546 * @covers Message::toString
547 * @covers Message::parse
548 * @covers Message::parseAsBlock
549 * @dataProvider providePlaintextParams
550 */
551 public function testPlaintextParams( $expect, $format ) {
552 $lang = Language::factory( 'en' );
553
554 $msg = new RawMessage( '$1 $2' );
555 $params = [
556 'one $2',
557 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
558 ];
559 $this->assertSame(
560 $expect,
561 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
562 "Fail formatting for $format"
563 );
564 }
565
566 public static function provideListParam() {
567 $lang = Language::factory( 'de' );
568 $msg1 = new Message( 'mainpage', [], $lang );
569 $msg2 = new RawMessage( "''link''", [], $lang );
570
571 return [
572 'Simple comma list' => [
573 [ 'a', 'b', 'c' ],
574 'comma',
575 'text',
576 'a, b, c'
577 ],
578
579 'Simple semicolon list' => [
580 [ 'a', 'b', 'c' ],
581 'semicolon',
582 'text',
583 'a; b; c'
584 ],
585
586 'Simple pipe list' => [
587 [ 'a', 'b', 'c' ],
588 'pipe',
589 'text',
590 'a | b | c'
591 ],
592
593 'Simple text list' => [
594 [ 'a', 'b', 'c' ],
595 'text',
596 'text',
597 'a, b and c'
598 ],
599
600 'Empty list' => [
601 [],
602 'comma',
603 'text',
604 ''
605 ],
606
607 'List with all "before" params, ->text()' => [
608 [ "''link''", Message::numParam( 12345678 ) ],
609 'semicolon',
610 'text',
611 '\'\'link\'\'; 12,345,678'
612 ],
613
614 'List with all "before" params, ->parse()' => [
615 [ "''link''", Message::numParam( 12345678 ) ],
616 'semicolon',
617 'parse',
618 '<i>link</i>; 12,345,678'
619 ],
620
621 'List with all "after" params, ->text()' => [
622 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
623 'semicolon',
624 'text',
625 'Main Page; \'\'link\'\'; [[foo]]'
626 ],
627
628 'List with all "after" params, ->parse()' => [
629 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
630 'semicolon',
631 'parse',
632 'Main Page; <i>link</i>; [[foo]]'
633 ],
634
635 'List with both "before" and "after" params, ->text()' => [
636 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
637 'semicolon',
638 'text',
639 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
640 ],
641
642 'List with both "before" and "after" params, ->parse()' => [
643 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
644 'semicolon',
645 'parse',
646 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
647 ],
648 ];
649 }
650
651 /**
652 * @covers Message::listParam
653 * @covers Message::extractParam
654 * @covers Message::formatListParam
655 * @dataProvider provideListParam
656 */
657 public function testListParam( $list, $type, $format, $expect ) {
658 $lang = Language::factory( 'en' );
659
660 $msg = new RawMessage( '$1' );
661 $msg->params( [ Message::listParam( $list, $type ) ] );
662 $this->assertEquals(
663 $expect,
664 $msg->inLanguage( $lang )->$format()
665 );
666 }
667
668 /**
669 * @covers Message::extractParam
670 */
671 public function testMessageAsParam() {
672 $this->setMwGlobals( [
673 'wgScript' => '/wiki/index.php',
674 'wgArticlePath' => '/wiki/$1',
675 ] );
676
677 $msg = new Message( 'returnto', [
678 new Message( 'apihelp-link', [
679 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
680 ], Language::factory( 'de' ) )
681 ], Language::factory( 'es' ) );
682
683 $this->assertEquals(
684 'Volver a [[Special:ApiHelp/foo|Página principal]].',
685 $msg->text(),
686 'Process with ->text()'
687 );
688 $this->assertEquals(
689 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
690 . "principal</a>.\n</p>",
691 $msg->parseAsBlock(),
692 'Process with ->parseAsBlock()'
693 );
694 }
695
696 public static function provideParser() {
697 return [
698 [
699 "''&'' <x><!-- x -->",
700 'plain',
701 ],
702
703 [
704 "''&'' <x><!-- x -->",
705 'text',
706 ],
707 [
708 '<i>&amp;</i> &lt;x&gt;',
709 'parse',
710 ],
711
712 [
713 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
714 'parseAsBlock',
715 ],
716 ];
717 }
718
719 /**
720 * @covers Message::text
721 * @covers Message::parse
722 * @covers Message::parseAsBlock
723 * @covers Message::toString
724 * @covers Message::transformText
725 * @covers Message::parseText
726 * @dataProvider provideParser
727 */
728 public function testParser( $expect, $format ) {
729 $msg = new RawMessage( "''&'' <x><!-- x -->" );
730 $this->assertSame(
731 $expect,
732 $msg->inLanguage( 'en' )->$format()
733 );
734 }
735
736 /**
737 * @covers Message::inContentLanguage
738 */
739 public function testInContentLanguage() {
740 $this->setUserLang( 'fr' );
741
742 // NOTE: make sure internal caching of the message text is reset appropriately
743 $msg = wfMessage( 'mainpage' );
744 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
745 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
746 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
747 }
748
749 /**
750 * @covers Message::inContentLanguage
751 */
752 public function testInContentLanguageOverride() {
753 $this->setMwGlobals( [
754 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
755 ] );
756 $this->setUserLang( 'fr' );
757
758 // NOTE: make sure internal caching of the message text is reset appropriately.
759 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
760 $msg = wfMessage( 'mainpage' );
761 $this->assertSame(
762 'Accueil',
763 $msg->inContentLanguage()->plain(),
764 'inContentLanguage() with ForceUIMsg override enabled'
765 );
766 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
767 $this->assertSame(
768 'Main Page',
769 $msg->inContentLanguage()->plain(),
770 'inContentLanguage() with ForceUIMsg override enabled'
771 );
772 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
773 }
774
775 /**
776 * @expectedException MWException
777 * @covers Message::inLanguage
778 */
779 public function testInLanguageThrows() {
780 wfMessage( 'foo' )->inLanguage( 123 );
781 }
782
783 /**
784 * @covers Message::serialize
785 * @covers Message::unserialize
786 */
787 public function testSerialization() {
788 $msg = new Message( 'parentheses' );
789 $msg->rawParams( '<a>foo</a>' );
790 $msg->title( Title::newFromText( 'Testing' ) );
791 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
792 $msg = unserialize( serialize( $msg ) );
793 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
794 $title = TestingAccessWrapper::newFromObject( $msg )->title;
795 $this->assertInstanceOf( 'Title', $title );
796 $this->assertSame( 'Testing', $title->getFullText() );
797
798 $msg = new Message( 'mainpage' );
799 $msg->inLanguage( 'de' );
800 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
801 $msg = unserialize( serialize( $msg ) );
802 $this->assertSame( 'Hauptseite', $msg->plain() );
803 }
804
805 /**
806 * @covers Message::newFromSpecifier
807 * @dataProvider provideNewFromSpecifier
808 */
809 public function testNewFromSpecifier( $value, $expectedText ) {
810 $message = Message::newFromSpecifier( $value );
811 $this->assertInstanceOf( Message::class, $message );
812 if ( $value instanceof Message ) {
813 $this->assertInstanceOf( get_class( $value ), $message );
814 $this->assertEquals( $value, $message );
815 }
816 $this->assertSame( $expectedText, $message->text() );
817 }
818
819 public function provideNewFromSpecifier() {
820 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
821 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
822 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
823
824 return [
825 'string' => [ 'mainpage', 'Main Page' ],
826 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
827 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
828 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
829 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
830 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
831 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
832 ];
833 }
834 }