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