MediaWiki REL1_30
MessageTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
6
7 protected function setUp() {
8 parent::setUp();
9
10 $this->setMwGlobals( [
11 'wgForceUIMsgAsContentMsg' => [],
12 ] );
13 $this->setUserLang( 'en' );
14 }
15
20 public function testConstructor( $expectedLang, $key, $params, $language ) {
21 $message = new Message( $key, $params, $language );
22
23 $this->assertSame( $key, $message->getKey() );
24 $this->assertSame( $params, $message->getParams() );
25 $this->assertEquals( $expectedLang, $message->getLanguage() );
26
27 $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
28 $messageSpecifier->expects( $this->any() )
29 ->method( 'getKey' )->will( $this->returnValue( $key ) );
30 $messageSpecifier->expects( $this->any() )
31 ->method( 'getParams' )->will( $this->returnValue( $params ) );
32 $message = new Message( $messageSpecifier, [], $language );
33
34 $this->assertSame( $key, $message->getKey() );
35 $this->assertSame( $params, $message->getParams() );
36 $this->assertEquals( $expectedLang, $message->getLanguage() );
37 }
38
39 public static function provideConstructor() {
40 $langDe = Language::factory( 'de' );
41 $langEn = Language::factory( 'en' );
42
43 return [
44 [ $langDe, 'foo', [], $langDe ],
45 [ $langDe, 'foo', [ 'bar' ], $langDe ],
46 [ $langEn, 'foo', [ 'bar' ], null ]
47 ];
48 }
49
50 public static function provideConstructorParams() {
51 return [
52 [
53 [],
54 [],
55 ],
56 [
57 [],
58 [ [] ],
59 ],
60 [
61 [ 'foo' ],
62 [ 'foo' ],
63 ],
64 [
65 [ 'foo', 'bar' ],
66 [ 'foo', 'bar' ],
67 ],
68 [
69 [ 'baz' ],
70 [ [ 'baz' ] ],
71 ],
72 [
73 [ 'baz', 'foo' ],
74 [ [ 'baz', 'foo' ] ],
75 ],
76 [
77 [ Message::rawParam( 'baz' ) ],
78 [ Message::rawParam( 'baz' ) ],
79 ],
80 [
81 [ Message::rawParam( 'baz' ), 'foo' ],
82 [ Message::rawParam( 'baz' ), 'foo' ],
83 ],
84 [
85 [ Message::rawParam( 'baz' ) ],
86 [ [ Message::rawParam( 'baz' ) ] ],
87 ],
88 [
89 [ Message::rawParam( 'baz' ), 'foo' ],
90 [ [ Message::rawParam( 'baz' ), 'foo' ] ],
91 ],
92
93 // Test handling of erroneous input, to detect if it changes
94 [
95 [ [ 'baz', 'foo' ], 'hhh' ],
96 [ [ 'baz', 'foo' ], 'hhh' ],
97 ],
98 [
99 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
100 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
101 ],
102 [
103 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
104 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
105 ],
106 [
107 [ [ 'baz' ], [ 'ahahahahha' ] ],
108 [ [ 'baz' ], [ 'ahahahahha' ] ],
109 ],
110 ];
111 }
112
118 public function testConstructorParams( $expected, $args ) {
119 $msg = new Message( 'imasomething' );
120
121 $returned = call_user_func_array( [ $msg, 'params' ], $args );
122
123 $this->assertSame( $msg, $returned );
124 $this->assertSame( $expected, $msg->getParams() );
125 }
126
127 public static function provideConstructorLanguage() {
128 return [
129 [ 'foo', [ 'bar' ], 'en' ],
130 [ 'foo', [ 'bar' ], 'de' ]
131 ];
132 }
133
139 public function testConstructorLanguage( $key, $params, $languageCode ) {
140 $language = Language::factory( $languageCode );
141 $message = new Message( $key, $params, $language );
142
143 $this->assertEquals( $language, $message->getLanguage() );
144 }
145
146 public static function provideKeys() {
147 return [
148 'string' => [
149 'key' => 'mainpage',
150 'expected' => [ 'mainpage' ],
151 ],
152 'single' => [
153 'key' => [ 'mainpage' ],
154 'expected' => [ 'mainpage' ],
155 ],
156 'multi' => [
157 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
158 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
159 ],
160 'empty' => [
161 'key' => [],
162 'expected' => null,
163 'exception' => 'InvalidArgumentException',
164 ],
165 'null' => [
166 'key' => null,
167 'expected' => null,
168 'exception' => 'InvalidArgumentException',
169 ],
170 'bad type' => [
171 'key' => 123,
172 'expected' => null,
173 'exception' => 'InvalidArgumentException',
174 ],
175 ];
176 }
177
185 public function testKeys( $key, $expected, $exception = null ) {
186 if ( $exception ) {
187 $this->setExpectedException( $exception );
188 }
189
190 $msg = new Message( $key );
191 $this->assertContains( $msg->getKey(), $expected );
192 $this->assertSame( $expected, $msg->getKeysToTry() );
193 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
194 }
195
199 public function testWfMessage() {
200 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
201 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
202 }
203
207 public function testNewFromKey() {
208 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
209 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
210 }
211
216 public function testWfMessageParams() {
217 $this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() );
218 $this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
219 $this->assertSame(
220 'Return to 1,024.',
221 wfMessage( 'returnto', Message::numParam( 1024 ) )->text()
222 );
223 $this->assertSame(
224 'Return to 1,024.',
225 wfMessage( 'returnto', [ Message::numParam( 1024 ) ] )->text()
226 );
227 $this->assertSame(
228 'You have foo (bar).',
229 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
230 );
231 $this->assertSame(
232 'You have foo (bar).',
233 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
234 );
235 $this->assertSame(
236 'You have 1,024 (bar).',
237 wfMessage(
238 'youhavenewmessages',
239 Message::numParam( 1024 ), 'bar'
240 )->text()
241 );
242 $this->assertSame(
243 'You have foo (2,048).',
244 wfMessage(
245 'youhavenewmessages',
246 'foo', Message::numParam( 2048 )
247 )->text()
248 );
249 $this->assertSame(
250 'You have 1,024 (2,048).',
251 wfMessage(
252 'youhavenewmessages',
253 [ Message::numParam( 1024 ), Message::numParam( 2048 ) ]
254 )->text()
255 );
256 }
257
261 public function testExists() {
262 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
263 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
264 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
265 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
266 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
267 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
268 }
269
277 public function testToStringKey() {
278 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() );
279 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
280 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
281 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
282 $this->assertSame( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
283 $this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
284 $this->assertSame(
285 '⧼i&lt;dont&gt;exist-evar⧽',
286 wfMessage( 'i<dont>exist-evar' )->escaped()
287 );
288 }
289
290 public static function provideToString() {
291 return [
292 // key, transformation, transformed, transformed implicitly
293 [ 'mainpage', 'plain', 'Main Page', 'Main Page' ],
294 [ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
295 [ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ],
296 [ 'script>alert(1)</script', 'escaped', '⧼script&gt;alert(1)&lt;/script⧽',
297 '⧼script&gt;alert(1)&lt;/script⧽' ],
298 [ 'script>alert(1)</script', 'plain', '⧼script&gt;alert(1)&lt;/script⧽',
299 '⧼script&gt;alert(1)&lt;/script⧽' ],
300 ];
301 }
302
308 public function testToString( $key, $format, $expect, $expectImplicit ) {
309 $msg = new Message( $key );
310 $this->assertSame( $expect, $msg->$format() );
311 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
312 $this->assertSame( $expectImplicit, $msg->__toString() );
313 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
314 }
315
316 public static function provideToString_raw() {
317 return [
318 [ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ],
319 [ '<span>foo</span>', 'escaped', '&lt;span&gt;foo&lt;/span&gt;',
320 '<span>foo</span>' ],
321 [ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ],
322 [ '<script>alert(1)</script>', 'parse', '&lt;script&gt;alert(1)&lt;/script&gt;',
323 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
324 [ '<script>alert(1)</script>', 'escaped', '&lt;script&gt;alert(1)&lt;/script&gt;',
325 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
326 [ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>',
327 '&lt;script&gt;alert(1)&lt;/script&gt;' ],
328 ];
329 }
330
336 public function testToString_raw( $message, $format, $expect, $expectImplicit ) {
337 // make the message behave like RawMessage and use the key as-is
338 $msg = $this->getMockBuilder( Message::class )->setMethods( [ 'fetchMessage' ] )
339 ->disableOriginalConstructor()
340 ->getMock();
341 $msg->expects( $this->any() )->method( 'fetchMessage' )->willReturn( $message );
343 $this->assertSame( $expect, $msg->$format() );
344 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by previous call' );
345 $this->assertSame( $expectImplicit, $msg->__toString() );
346 $this->assertSame( $expect, $msg->toString(), 'toString is unaffected by __toString' );
347 }
348
352 public function testInLanguage() {
353 $this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
354 $this->assertSame( 'Заглавная страница',
355 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
356
357 // NOTE: make sure internal caching of the message text is reset appropriately
358 $msg = wfMessage( 'mainpage' );
359 $this->assertSame( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
360 $this->assertSame(
361 'Заглавная страница',
362 $msg->inLanguage( Language::factory( 'ru' ) )->text()
363 );
364 }
365
370 public function testRawParams() {
371 $this->assertSame(
372 '(Заглавная страница)',
373 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
374 );
375 $this->assertSame(
376 '(Заглавная страница $1)',
377 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
378 );
379 $this->assertSame(
380 '(Заглавная страница)',
381 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
382 );
383 $this->assertSame(
384 '(Заглавная страница $1)',
385 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
386 );
387 }
388
393 public function testRawMessage() {
394 $msg = new RawMessage( 'example &' );
395 $this->assertSame( 'example &', $msg->plain() );
396 $this->assertSame( 'example &amp;', $msg->escaped() );
397 }
398
399 public function testRawHtmlInMsg() {
401 $this->setMwGlobals( 'wgRawHtml', true );
402 // We have to reset the core hook registration.
403 // to register the html hook
405 $this->setMwGlobals( 'wgParser',
406 ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
407 );
408
409 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
410 $txt = '<span class="error">&lt;html&gt; tags cannot be' .
411 ' used outside of normal pages.</span>';
412 $this->assertSame( $txt, $msg->parse() );
413 }
414
420 public function testReplaceManyParams() {
421 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
422 // One less than above has placeholders
423 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
424 $this->assertSame(
425 'abcdefghijka2',
426 $msg->params( $params )->plain(),
427 'Params > 9 are replaced correctly'
428 );
429
430 $msg = new RawMessage( 'Params$*' );
431 $params = [ 'ab', 'bc', 'cd' ];
432 $this->assertSame(
433 'Params: ab, bc, cd',
434 $msg->params( $params )->text()
435 );
436 }
437
442 public function testNumParams() {
443 $lang = Language::factory( 'en' );
444 $msg = new RawMessage( '$1' );
445
446 $this->assertSame(
447 $lang->formatNum( 123456.789 ),
448 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
449 'numParams is handled correctly'
450 );
451 }
452
457 public function testDurationParams() {
458 $lang = Language::factory( 'en' );
459 $msg = new RawMessage( '$1' );
460
461 $this->assertSame(
462 $lang->formatDuration( 1234 ),
463 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
464 'durationParams is handled correctly'
465 );
466 }
467
474 public function testExpiryParams() {
475 $lang = Language::factory( 'en' );
476 $msg = new RawMessage( '$1' );
477
478 $this->assertSame(
479 $lang->formatExpiry( wfTimestampNow() ),
480 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
481 'expiryParams is handled correctly'
482 );
483 }
484
489 public function testTimeperiodParams() {
490 $lang = Language::factory( 'en' );
491 $msg = new RawMessage( '$1' );
492
493 $this->assertSame(
494 $lang->formatTimePeriod( 1234 ),
495 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
496 'timeperiodParams is handled correctly'
497 );
498 }
499
504 public function testSizeParams() {
505 $lang = Language::factory( 'en' );
506 $msg = new RawMessage( '$1' );
507
508 $this->assertSame(
509 $lang->formatSize( 123456 ),
510 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
511 'sizeParams is handled correctly'
512 );
513 }
514
519 public function testBitrateParams() {
520 $lang = Language::factory( 'en' );
521 $msg = new RawMessage( '$1' );
522
523 $this->assertSame(
524 $lang->formatBitrate( 123456 ),
525 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
526 'bitrateParams is handled correctly'
527 );
528 }
529
530 public static function providePlaintextParams() {
531 return [
532 [
533 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
534 'plain',
535 ],
536
537 [
538 // expect
539 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
540 // format
541 'text',
542 ],
543 [
544 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
545 'escaped',
546 ],
547
548 [
549 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
550 'parse',
551 ],
552
553 [
554 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
555 'parseAsBlock',
556 ],
557 ];
558 }
559
569 public function testPlaintextParams( $expect, $format ) {
570 $lang = Language::factory( 'en' );
571
572 $msg = new RawMessage( '$1 $2' );
573 $params = [
574 'one $2',
575 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
576 ];
577 $this->assertSame(
578 $expect,
579 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
580 "Fail formatting for $format"
581 );
582 }
583
584 public static function provideListParam() {
585 $lang = Language::factory( 'de' );
586 $msg1 = new Message( 'mainpage', [], $lang );
587 $msg2 = new RawMessage( "''link''", [], $lang );
588
589 return [
590 'Simple comma list' => [
591 [ 'a', 'b', 'c' ],
592 'comma',
593 'text',
594 'a, b, c'
595 ],
596
597 'Simple semicolon list' => [
598 [ 'a', 'b', 'c' ],
599 'semicolon',
600 'text',
601 'a; b; c'
602 ],
603
604 'Simple pipe list' => [
605 [ 'a', 'b', 'c' ],
606 'pipe',
607 'text',
608 'a | b | c'
609 ],
610
611 'Simple text list' => [
612 [ 'a', 'b', 'c' ],
613 'text',
614 'text',
615 'a, b and c'
616 ],
617
618 'Empty list' => [
619 [],
620 'comma',
621 'text',
622 ''
623 ],
624
625 'List with all "before" params, ->text()' => [
626 [ "''link''", Message::numParam( 12345678 ) ],
627 'semicolon',
628 'text',
629 '\'\'link\'\'; 12,345,678'
630 ],
631
632 'List with all "before" params, ->parse()' => [
633 [ "''link''", Message::numParam( 12345678 ) ],
634 'semicolon',
635 'parse',
636 '<i>link</i>; 12,345,678'
637 ],
638
639 'List with all "after" params, ->text()' => [
640 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
641 'semicolon',
642 'text',
643 'Main Page; \'\'link\'\'; [[foo]]'
644 ],
645
646 'List with all "after" params, ->parse()' => [
647 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
648 'semicolon',
649 'parse',
650 'Main Page; <i>link</i>; [[foo]]'
651 ],
652
653 'List with both "before" and "after" params, ->text()' => [
654 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
655 'semicolon',
656 'text',
657 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
658 ],
659
660 'List with both "before" and "after" params, ->parse()' => [
661 [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
662 'semicolon',
663 'parse',
664 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
665 ],
666 ];
667 }
668
675 public function testListParam( $list, $type, $format, $expect ) {
676 $lang = Language::factory( 'en' );
677
678 $msg = new RawMessage( '$1' );
679 $msg->params( [ Message::listParam( $list, $type ) ] );
680 $this->assertEquals(
681 $expect,
682 $msg->inLanguage( $lang )->$format()
683 );
684 }
685
689 public function testMessageAsParam() {
690 $this->setMwGlobals( [
691 'wgScript' => '/wiki/index.php',
692 'wgArticlePath' => '/wiki/$1',
693 ] );
694
695 $msg = new Message( 'returnto', [
696 new Message( 'apihelp-link', [
697 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
698 ], Language::factory( 'de' ) )
699 ], Language::factory( 'es' ) );
700
701 $this->assertEquals(
702 'Volver a [[Special:ApiHelp/foo|Página principal]].',
703 $msg->text(),
704 'Process with ->text()'
705 );
706 $this->assertEquals(
707 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
708 . "principal</a>.\n</p>",
709 $msg->parseAsBlock(),
710 'Process with ->parseAsBlock()'
711 );
712 }
713
714 public static function provideParser() {
715 return [
716 [
717 "''&'' <x><!-- x -->",
718 'plain',
719 ],
720
721 [
722 "''&'' <x><!-- x -->",
723 'text',
724 ],
725 [
726 '<i>&amp;</i> &lt;x&gt;',
727 'parse',
728 ],
729
730 [
731 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
732 'parseAsBlock',
733 ],
734 ];
735 }
736
746 public function testParser( $expect, $format ) {
747 $msg = new RawMessage( "''&'' <x><!-- x -->" );
748 $this->assertSame(
749 $expect,
750 $msg->inLanguage( 'en' )->$format()
751 );
752 }
753
757 public function testInContentLanguage() {
758 $this->setUserLang( 'fr' );
759
760 // NOTE: make sure internal caching of the message text is reset appropriately
761 $msg = wfMessage( 'mainpage' );
762 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
763 $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
764 $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
765 }
766
771 $this->setMwGlobals( [
772 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
773 ] );
774 $this->setUserLang( 'fr' );
775
776 // NOTE: make sure internal caching of the message text is reset appropriately.
777 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
778 $msg = wfMessage( 'mainpage' );
779 $this->assertSame(
780 'Accueil',
781 $msg->inContentLanguage()->plain(),
782 'inContentLanguage() with ForceUIMsg override enabled'
783 );
784 $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
785 $this->assertSame(
786 'Main Page',
787 $msg->inContentLanguage()->plain(),
788 'inContentLanguage() with ForceUIMsg override enabled'
789 );
790 $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
791 }
792
797 public function testInLanguageThrows() {
798 wfMessage( 'foo' )->inLanguage( 123 );
799 }
800
805 public function testSerialization() {
806 $msg = new Message( 'parentheses' );
807 $msg->rawParams( '<a>foo</a>' );
808 $msg->title( Title::newFromText( 'Testing' ) );
809 $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
810 $msg = unserialize( serialize( $msg ) );
811 $this->assertSame( '(<a>foo</a>)', $msg->parse() );
812 $title = TestingAccessWrapper::newFromObject( $msg )->title;
813 $this->assertInstanceOf( 'Title', $title );
814 $this->assertSame( 'Testing', $title->getFullText() );
815
816 $msg = new Message( 'mainpage' );
817 $msg->inLanguage( 'de' );
818 $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
819 $msg = unserialize( serialize( $msg ) );
820 $this->assertSame( 'Hauptseite', $msg->plain() );
821 }
822
827 public function testNewFromSpecifier( $value, $expectedText ) {
828 $message = Message::newFromSpecifier( $value );
829 $this->assertInstanceOf( Message::class, $message );
830 if ( $value instanceof Message ) {
831 $this->assertInstanceOf( get_class( $value ), $message );
832 $this->assertEquals( $value, $message );
833 }
834 $this->assertSame( $expectedText, $message->text() );
835 }
836
837 public function provideNewFromSpecifier() {
838 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
839 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
840 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
841
842 return [
843 'string' => [ 'mainpage', 'Main Page' ],
844 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
845 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
846 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
847 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
848 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
849 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
850 ];
851 }
852}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
serialize()
unserialize( $serialized)
$wgParserConf
Parser configuration.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
if( $line===false) $args
Definition cdb.php:63
Extension of Message implementing IApiMessage.
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
static destroyInstance()
Destroy the singleton instance.
testExpiryParams()
FIXME: This should not need database, but Language::formatExpiry does (T57912) Database Message::expi...
testConstructorParams( $expected, $args)
Message::__construct Message::getParams provideConstructorParams.
static provideToString()
static provideToString_raw()
static provideConstructorParams()
testParser( $expect, $format)
Message::text Message::parse Message::parseAsBlock Message::toString Message::transformText Message::...
testToString( $key, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString.
testToString_raw( $message, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString_raw.
testInLanguageThrows()
MWException Message::inLanguage.
static provideListParam()
testInLanguage()
Message::inLanguage.
testInContentLanguage()
Message::inContentLanguage.
static provideConstructorLanguage()
testSizeParams()
Message::sizeParam Message::sizeParams.
testRawParams()
Message::rawParam Message::rawParams.
testDurationParams()
Message::durationParam Message::durationParams.
static provideKeys()
testNumParams()
Message::numParam Message::numParams.
testNewFromSpecifier( $value, $expectedText)
Message::newFromSpecifier provideNewFromSpecifier.
testReplaceManyParams()
Message::params Message::toString Message::replaceParameters.
testExists()
Message::exists.
static provideConstructor()
testWfMessageParams()
wfMessage Message::__construct
testListParam( $list, $type, $format, $expect)
Message::listParam Message::extractParam Message::formatListParam provideListParam.
testTimeperiodParams()
Message::timeperiodParam Message::timeperiodParams.
testBitrateParams()
Message::bitrateParam Message::bitrateParams.
testConstructorLanguage( $key, $params, $languageCode)
Message::__construct Message::getLanguage provideConstructorLanguage.
provideNewFromSpecifier()
testKeys( $key, $expected, $exception=null)
Message::__construct Message::getKey Message::isMultiKey Message::getKeysToTry provideKeys.
testWfMessage()
wfMessage
testConstructor( $expectedLang, $key, $params, $language)
Message::__construct provideConstructor.
testNewFromKey()
Message::newFromKey.
testPlaintextParams( $expect, $format)
Message::plaintextParam Message::plaintextParams Message::formatPlaintext Message::toString Message::...
static provideParser()
testInContentLanguageOverride()
Message::inContentLanguage.
static providePlaintextParams()
testToStringKey()
Message::__construct Message::text Message::plain Message::escaped Message::toString.
testSerialization()
Message::serialize Message::unserialize.
testRawMessage()
RawMessage::__construct RawMessage::fetchMessage.
testMessageAsParam()
Message::extractParam.
The Message class provides methods which fulfil two basic services:
Definition Message.php:159
Variant of the Message class.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
either a plain
Definition hooks.txt:2026
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:962
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params
if(!isset( $args[0])) $lang