jquery.tablesorter: Fix pre-JS selector to match wikitext-generated sortable tables
[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 /**
404 * @covers CoreTagHooks::html
405 */
406 public function testRawHtmlInMsg() {
407 $this->setMwGlobals( 'wgRawHtml', true );
408 // We have to reset the core hook registration.
409 // to register the html hook
410 MessageCache::destroyInstance();
411 $this->setMwGlobals( 'wgParser',
412 MediaWikiServices::getInstance()->getParserFactory()->create() );
413
414 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
415 $txt = '<span class="error">&lt;html&gt; tags cannot be' .
416 ' used outside of normal pages.</span>';
417 $this->assertSame( $txt, $msg->parse() );
418 }
419
420 /**
421 * @covers Message::params
422 * @covers Message::toString
423 * @covers Message::replaceParameters
424 */
425 public function testReplaceManyParams() {
426 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
427 // One less than above has placeholders
428 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
429 $this->assertSame(
430 'abcdefghijka2',
431 $msg->params( $params )->plain(),
432 'Params > 9 are replaced correctly'
433 );
434
435 $msg = new RawMessage( 'Params$*' );
436 $params = [ 'ab', 'bc', 'cd' ];
437 $this->assertSame(
438 'Params: ab, bc, cd',
439 $msg->params( $params )->text()
440 );
441 }
442
443 /**
444 * @covers Message::numParam
445 * @covers Message::numParams
446 */
447 public function testNumParams() {
448 $lang = Language::factory( 'en' );
449 $msg = new RawMessage( '$1' );
450
451 $this->assertSame(
452 $lang->formatNum( 123456.789 ),
453 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
454 'numParams is handled correctly'
455 );
456 }
457
458 /**
459 * @covers Message::durationParam
460 * @covers Message::durationParams
461 */
462 public function testDurationParams() {
463 $lang = Language::factory( 'en' );
464 $msg = new RawMessage( '$1' );
465
466 $this->assertSame(
467 $lang->formatDuration( 1234 ),
468 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
469 'durationParams is handled correctly'
470 );
471 }
472
473 /**
474 * FIXME: This should not need database, but Language#formatExpiry does (T57912)
475 * @covers Message::expiryParam
476 * @covers Message::expiryParams
477 */
478 public function testExpiryParams() {
479 $lang = Language::factory( 'en' );
480 $msg = new RawMessage( '$1' );
481
482 $this->assertSame(
483 $lang->formatExpiry( wfTimestampNow() ),
484 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
485 'expiryParams is handled correctly'
486 );
487 }
488
489 /**
490 * @covers Message::timeperiodParam
491 * @covers Message::timeperiodParams
492 */
493 public function testTimeperiodParams() {
494 $lang = Language::factory( 'en' );
495 $msg = new RawMessage( '$1' );
496
497 $this->assertSame(
498 $lang->formatTimePeriod( 1234 ),
499 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
500 'timeperiodParams is handled correctly'
501 );
502 }
503
504 /**
505 * @covers Message::sizeParam
506 * @covers Message::sizeParams
507 */
508 public function testSizeParams() {
509 $lang = Language::factory( 'en' );
510 $msg = new RawMessage( '$1' );
511
512 $this->assertSame(
513 $lang->formatSize( 123456 ),
514 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
515 'sizeParams is handled correctly'
516 );
517 }
518
519 /**
520 * @covers Message::bitrateParam
521 * @covers Message::bitrateParams
522 */
523 public function testBitrateParams() {
524 $lang = Language::factory( 'en' );
525 $msg = new RawMessage( '$1' );
526
527 $this->assertSame(
528 $lang->formatBitrate( 123456 ),
529 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
530 'bitrateParams is handled correctly'
531 );
532 }
533
534 public static function providePlaintextParams() {
535 return [
536 [
537 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
538 'plain',
539 ],
540
541 [
542 // expect
543 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
544 // format
545 'text',
546 ],
547 [
548 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
549 'escaped',
550 ],
551
552 [
553 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
554 'parse',
555 ],
556
557 [
558 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
559 'parseAsBlock',
560 ],
561 ];
562 }
563
564 /**
565 * @covers Message::plaintextParam
566 * @covers Message::plaintextParams
567 * @covers Message::formatPlaintext
568 * @covers Message::toString
569 * @covers Message::parse
570 * @covers Message::parseAsBlock
571 * @dataProvider providePlaintextParams
572 */
573 public function testPlaintextParams( $expect, $format ) {
574 $lang = Language::factory( 'en' );
575
576 $msg = new RawMessage( '$1 $2' );
577 $params = [
578 'one $2',
579 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
580 ];
581 $this->assertSame(
582 $expect,
583 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
584 "Fail formatting for $format"
585 );
586 }
587
588 public static function provideListParam() {
589 $lang = Language::factory( 'de' );
590 $msg1 = new Message( 'mainpage', [], $lang );
591 $msg2 = new RawMessage( "''link''", [], $lang );
592
593 return [
594 'Simple comma list' => [
595 [ 'a', 'b', 'c' ],
596 'comma',
597 'text',
598 'a, b, c'
599 ],
600
601 'Simple semicolon list' => [
602 [ 'a', 'b', 'c' ],
603 'semicolon',
604 'text',
605 'a; b; c'
606 ],
607
608 'Simple pipe list' => [
609 [ 'a', 'b', 'c' ],
610 'pipe',
611 'text',
612 'a | b | c'
613 ],
614
615 'Simple text list' => [
616 [ 'a', 'b', 'c' ],
617 'text',
618 'text',
619 'a, b and c'
620 ],
621
622 'Empty list' => [
623 [],
624 'comma',
625 'text',
626 ''
627 ],
628
629 'List with all "before" params, ->text()' => [
630 [ "''link''", Message::numParam( 12345678 ) ],
631 'semicolon',
632 'text',
633 '\'\'link\'\'; 12,345,678'
634 ],
635
636 'List with all "before" params, ->parse()' => [
637 [ "''link''", Message::numParam( 12345678 ) ],
638 'semicolon',
639 'parse',
640 '<i>link</i>; 12,345,678'
641 ],
642
643 'List with all "after" params, ->text()' => [
644 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
645 'semicolon',
646 'text',
647 'Main Page; \'\'link\'\'; [[foo]]'
648 ],
649
650 'List with all "after" params, ->parse()' => [
651 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
652 'semicolon',
653 'parse',
654 'Main Page; <i>link</i>; [[foo]]'
655 ],
656
657 'List with both "before" and "after" params, ->text()' => [
658 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
659 'semicolon',
660 'text',
661 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
662 ],
663
664 'List with both "before" and "after" params, ->parse()' => [
665 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
666 'semicolon',
667 'parse',
668 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
669 ],
670 ];
671 }
672
673 /**
674 * @covers Message::listParam
675 * @covers Message::extractParam
676 * @covers Message::formatListParam
677 * @dataProvider provideListParam
678 */
679 public function testListParam( $list, $type, $format, $expect ) {
680 $lang = Language::factory( 'en' );
681
682 $msg = new RawMessage( '$1' );
683 $msg->params( [ Message::listParam( $list, $type ) ] );
684 $this->assertEquals(
685 $expect,
686 $msg->inLanguage( $lang )->$format()
687 );
688 }
689
690 /**
691 * @covers Message::extractParam
692 */
693 public function testMessageAsParam() {
694 $this->setMwGlobals( [
695 'wgScript' => '/wiki/index.php',
696 'wgArticlePath' => '/wiki/$1',
697 ] );
698
699 $msg = new Message( 'returnto', [
700 new Message( 'apihelp-link', [
701 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
702 ], Language::factory( 'de' ) )
703 ], Language::factory( 'es' ) );
704
705 $this->assertEquals(
706 'Volver a [[Special:ApiHelp/foo|Página principal]].',
707 $msg->text(),
708 'Process with ->text()'
709 );
710 $this->assertEquals(
711 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
712 . "principal</a>.\n</p>",
713 $msg->parseAsBlock(),
714 'Process with ->parseAsBlock()'
715 );
716 }
717
718 public static function provideParser() {
719 return [
720 [
721 "''&'' <x><!-- x -->",
722 'plain',
723 ],
724
725 [
726 "''&'' <x><!-- x -->",
727 'text',
728 ],
729 [
730 '<i>&amp;</i> &lt;x&gt;',
731 'parse',
732 ],
733
734 [
735 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
736 'parseAsBlock',
737 ],
738 ];
739 }
740
741 /**
742 * @covers Message::text
743 * @covers Message::parse
744 * @covers Message::parseAsBlock
745 * @covers Message::toString
746 * @covers Message::transformText
747 * @covers Message::parseText
748 * @dataProvider provideParser
749 */
750 public function testParser( $expect, $format ) {
751 $msg = new RawMessage( "''&'' <x><!-- x -->" );
752 $this->assertSame(
753 $expect,
754 $msg->inLanguage( 'en' )->$format()
755 );
756 }
757
758 /**
759 * @covers Message::inContentLanguage
760 */
761 public function testInContentLanguage() {
762 $this->setUserLang( 'fr' );
763
764 // NOTE: make sure internal caching of the message text is reset appropriately
765 $msg = wfMessage( 'mainpage' );
766 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
767 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
768 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
769 }
770
771 /**
772 * @covers Message::inContentLanguage
773 */
774 public function testInContentLanguageOverride() {
775 $this->setMwGlobals( [
776 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
777 ] );
778 $this->setUserLang( 'fr' );
779
780 // NOTE: make sure internal caching of the message text is reset appropriately.
781 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
782 $msg = wfMessage( 'mainpage' );
783 $this->assertSame(
784 'Accueil',
785 $msg->inContentLanguage()->plain(),
786 'inContentLanguage() with ForceUIMsg override enabled'
787 );
788 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
789 $this->assertSame(
790 'Main Page',
791 $msg->inContentLanguage()->plain(),
792 'inContentLanguage() with ForceUIMsg override enabled'
793 );
794 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
795 }
796
797 /**
798 * @expectedException MWException
799 * @covers Message::inLanguage
800 */
801 public function testInLanguageThrows() {
802 wfMessage( 'foo' )->inLanguage( 123 );
803 }
804
805 /**
806 * @covers Message::serialize
807 * @covers Message::unserialize
808 */
809 public function testSerialization() {
810 $msg = new Message( 'parentheses' );
811 $msg->rawParams( '<a>foo</a>' );
812 $msg->title( Title::newFromText( 'Testing' ) );
813 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
814 $msg = unserialize( serialize( $msg ) );
815 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
816 $title = TestingAccessWrapper::newFromObject( $msg )->title;
817 $this->assertInstanceOf( Title::class, $title );
818 $this->assertSame( 'Testing', $title->getFullText() );
819
820 $msg = new Message( 'mainpage' );
821 $msg->inLanguage( 'de' );
822 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
823 $msg = unserialize( serialize( $msg ) );
824 $this->assertSame( 'Hauptseite', $msg->plain() );
825 }
826
827 /**
828 * @covers Message::newFromSpecifier
829 * @dataProvider provideNewFromSpecifier
830 */
831 public function testNewFromSpecifier( $value, $expectedText ) {
832 $message = Message::newFromSpecifier( $value );
833 $this->assertInstanceOf( Message::class, $message );
834 if ( $value instanceof Message ) {
835 $this->assertInstanceOf( get_class( $value ), $message );
836 $this->assertEquals( $value, $message );
837 }
838 $this->assertSame( $expectedText, $message->text() );
839 }
840
841 public function provideNewFromSpecifier() {
842 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
843 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
844 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
845
846 return [
847 'string' => [ 'mainpage', 'Main Page' ],
848 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
849 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
850 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
851 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
852 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
853 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
854 ];
855 }
856 }