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