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