MediaWiki  master
MessageTest.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10  protected function setUp() {
11  parent::setUp();
12 
13  $this->setMwGlobals( [
14  'wgForceUIMsgAsContentMsg' => [],
15  ] );
16  $this->setUserLang( 'en' );
17  }
18 
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 
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 
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 
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 
202  public function testWfMessage() {
203  $this->assertInstanceOf( Message::class, wfMessage( 'mainpage' ) );
204  $this->assertInstanceOf( Message::class, wfMessage( 'i-dont-exist-evar' ) );
205  }
206 
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 
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 
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 
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 
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 
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 );
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 
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 
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 
396  public function testRawMessage() {
397  $msg = new RawMessage( 'example &' );
398  $this->assertSame( 'example &', $msg->plain() );
399  $this->assertSame( 'example &amp;', $msg->escaped() );
400  }
401 
405  public function testRawHtmlInMsg() {
406  $this->setMwGlobals( 'wgRawHtml', true );
407  // We have to reset the core hook registration.
408  // to register the html hook
410  $this->overrideMwServices();
411 
412  $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
413  $txt = '<span class="error">&lt;html&gt; tags cannot be' .
414  ' used outside of normal pages.</span>';
415  $this->assertSame( $txt, $msg->parse() );
416  }
417 
423  public function testReplaceManyParams() {
424  $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
425  // One less than above has placeholders
426  $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
427  $this->assertSame(
428  'abcdefghijka2',
429  $msg->params( $params )->plain(),
430  'Params > 9 are replaced correctly'
431  );
432 
433  $msg = new RawMessage( 'Params$*' );
434  $params = [ 'ab', 'bc', 'cd' ];
435  $this->assertSame(
436  'Params: ab, bc, cd',
437  $msg->params( $params )->text()
438  );
439  }
440 
445  public function testNumParams() {
446  $lang = Language::factory( 'en' );
447  $msg = new RawMessage( '$1' );
448 
449  $this->assertSame(
450  $lang->formatNum( 123456.789 ),
451  $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
452  'numParams is handled correctly'
453  );
454  }
455 
460  public function testDurationParams() {
461  $lang = Language::factory( 'en' );
462  $msg = new RawMessage( '$1' );
463 
464  $this->assertSame(
465  $lang->formatDuration( 1234 ),
466  $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
467  'durationParams is handled correctly'
468  );
469  }
470 
476  public function testExpiryParams() {
477  $lang = Language::factory( 'en' );
478  $msg = new RawMessage( '$1' );
479 
480  $this->assertSame(
481  $lang->formatExpiry( wfTimestampNow() ),
482  $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
483  'expiryParams is handled correctly'
484  );
485  }
486 
491  public function testTimeperiodParams() {
492  $lang = Language::factory( 'en' );
493  $msg = new RawMessage( '$1' );
494 
495  $this->assertSame(
496  $lang->formatTimePeriod( 1234 ),
497  $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
498  'timeperiodParams is handled correctly'
499  );
500  }
501 
506  public function testSizeParams() {
507  $lang = Language::factory( 'en' );
508  $msg = new RawMessage( '$1' );
509 
510  $this->assertSame(
511  $lang->formatSize( 123456 ),
512  $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
513  'sizeParams is handled correctly'
514  );
515  }
516 
521  public function testBitrateParams() {
522  $lang = Language::factory( 'en' );
523  $msg = new RawMessage( '$1' );
524 
525  $this->assertSame(
526  $lang->formatBitrate( 123456 ),
527  $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
528  'bitrateParams is handled correctly'
529  );
530  }
531 
532  public static function providePlaintextParams() {
533  return [
534  [
535  'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
536  'plain',
537  ],
538 
539  [
540  // expect
541  'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
542  // format
543  'text',
544  ],
545  [
546  'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
547  'escaped',
548  ],
549 
550  [
551  'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
552  'parse',
553  ],
554 
555  [
556  "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
557  'parseAsBlock',
558  ],
559  ];
560  }
561 
571  public function testPlaintextParams( $expect, $format ) {
572  $lang = Language::factory( 'en' );
573 
574  $msg = new RawMessage( '$1 $2' );
575  $params = [
576  'one $2',
577  '<div>foo</div> [[Bar]] {{Baz}} &lt;',
578  ];
579  $this->assertSame(
580  $expect,
581  $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
582  "Fail formatting for $format"
583  );
584  }
585 
586  public static function provideListParam() {
587  $lang = Language::factory( 'de' );
588  $msg1 = new Message( 'mainpage', [], $lang );
589  $msg2 = new RawMessage( "''link''", [], $lang );
590 
591  return [
592  'Simple comma list' => [
593  [ 'a', 'b', 'c' ],
594  'comma',
595  'text',
596  'a, b, c'
597  ],
598 
599  'Simple semicolon list' => [
600  [ 'a', 'b', 'c' ],
601  'semicolon',
602  'text',
603  'a; b; c'
604  ],
605 
606  'Simple pipe list' => [
607  [ 'a', 'b', 'c' ],
608  'pipe',
609  'text',
610  'a | b | c'
611  ],
612 
613  'Simple text list' => [
614  [ 'a', 'b', 'c' ],
615  'text',
616  'text',
617  'a, b and c'
618  ],
619 
620  'Empty list' => [
621  [],
622  'comma',
623  'text',
624  ''
625  ],
626 
627  'List with all "before" params, ->text()' => [
628  [ "''link''", Message::numParam( 12345678 ) ],
629  'semicolon',
630  'text',
631  '\'\'link\'\'; 12,345,678'
632  ],
633 
634  'List with all "before" params, ->parse()' => [
635  [ "''link''", Message::numParam( 12345678 ) ],
636  'semicolon',
637  'parse',
638  '<i>link</i>; 12,345,678'
639  ],
640 
641  'List with all "after" params, ->text()' => [
642  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
643  'semicolon',
644  'text',
645  'Main Page; \'\'link\'\'; [[foo]]'
646  ],
647 
648  'List with all "after" params, ->parse()' => [
649  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
650  'semicolon',
651  'parse',
652  'Main Page; <i>link</i>; [[foo]]'
653  ],
654 
655  'List with both "before" and "after" params, ->text()' => [
656  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
657  'semicolon',
658  'text',
659  'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
660  ],
661 
662  'List with both "before" and "after" params, ->parse()' => [
663  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
664  'semicolon',
665  'parse',
666  'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
667  ],
668  ];
669  }
670 
677  public function testListParam( $list, $type, $format, $expect ) {
678  $lang = Language::factory( 'en' );
679 
680  $msg = new RawMessage( '$1' );
681  $msg->params( [ Message::listParam( $list, $type ) ] );
682  $this->assertEquals(
683  $expect,
684  $msg->inLanguage( $lang )->$format()
685  );
686  }
687 
691  public function testMessageAsParam() {
692  $this->setMwGlobals( [
693  'wgScript' => '/wiki/index.php',
694  'wgArticlePath' => '/wiki/$1',
695  ] );
696 
697  $msg = new Message( 'returnto', [
698  new Message( 'apihelp-link', [
699  'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
700  ], Language::factory( 'de' ) )
701  ], Language::factory( 'es' ) );
702 
703  $this->assertEquals(
704  'Volver a [[Special:ApiHelp/foo|Página principal]].',
705  $msg->text(),
706  'Process with ->text()'
707  );
708  $this->assertEquals(
709  '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
710  . "principal</a>.\n</p>",
711  $msg->parseAsBlock(),
712  'Process with ->parseAsBlock()'
713  );
714  }
715 
716  public static function provideParser() {
717  return [
718  [
719  "''&'' <x><!-- x -->",
720  'plain',
721  ],
722 
723  [
724  "''&'' <x><!-- x -->",
725  'text',
726  ],
727  [
728  '<i>&amp;</i> &lt;x&gt;',
729  'parse',
730  ],
731 
732  [
733  "<p><i>&amp;</i> &lt;x&gt;\n</p>",
734  'parseAsBlock',
735  ],
736  ];
737  }
738 
748  public function testParser( $expect, $format ) {
749  $msg = new RawMessage( "''&'' <x><!-- x -->" );
750  $this->assertSame(
751  $expect,
752  $msg->inLanguage( 'en' )->$format()
753  );
754  }
755 
759  public function testInContentLanguage() {
760  $this->setUserLang( 'fr' );
761 
762  // NOTE: make sure internal caching of the message text is reset appropriately
763  $msg = wfMessage( 'mainpage' );
764  $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
765  $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
766  $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
767  }
768 
772  public function testInContentLanguageOverride() {
773  $this->setMwGlobals( [
774  'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
775  ] );
776  $this->setUserLang( 'fr' );
777 
778  // NOTE: make sure internal caching of the message text is reset appropriately.
779  // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
780  $msg = wfMessage( 'mainpage' );
781  $this->assertSame(
782  'Accueil',
783  $msg->inContentLanguage()->plain(),
784  'inContentLanguage() with ForceUIMsg override enabled'
785  );
786  $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
787  $this->assertSame(
788  'Main Page',
789  $msg->inContentLanguage()->plain(),
790  'inContentLanguage() with ForceUIMsg override enabled'
791  );
792  $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
793  }
794 
799  public function testInLanguageThrows() {
800  wfMessage( 'foo' )->inLanguage( 123 );
801  }
802 
807  public function testSerialization() {
808  $msg = new Message( 'parentheses' );
809  $msg->rawParams( '<a>foo</a>' );
810  $msg->title( Title::newFromText( 'Testing' ) );
811  $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
812  $msg = unserialize( serialize( $msg ) );
813  $this->assertSame( '(<a>foo</a>)', $msg->parse() );
814  $title = TestingAccessWrapper::newFromObject( $msg )->title;
815  $this->assertInstanceOf( Title::class, $title );
816  $this->assertSame( 'Testing', $title->getFullText() );
817 
818  $msg = new Message( 'mainpage' );
819  $msg->inLanguage( 'de' );
820  $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
821  $msg = unserialize( serialize( $msg ) );
822  $this->assertSame( 'Hauptseite', $msg->plain() );
823  }
824 
829  public function testNewFromSpecifier( $value, $expectedText ) {
830  $message = Message::newFromSpecifier( $value );
831  $this->assertInstanceOf( Message::class, $message );
832  if ( $value instanceof Message ) {
833  $this->assertInstanceOf( get_class( $value ), $message );
834  $this->assertEquals( $value, $message );
835  }
836  $this->assertSame( $expectedText, $message->text() );
837  }
838 
839  public function provideNewFromSpecifier() {
840  $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
841  $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
842  $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
843 
844  return [
845  'string' => [ 'mainpage', 'Main Page' ],
846  'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
847  'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
848  'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
849  'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
850  'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
851  'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
852  ];
853  }
854 }
testSerialization()
Message::serialize Message::unserialize.
static rawParam( $raw)
Definition: Message.php:1038
testToString( $key, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString.
either a plain
Definition: hooks.txt:2043
testExists()
Message::exists.
static provideKeys()
serialize()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testConstructorParams( $expected, $args)
Message::__construct Message::getParams provideConstructorParams.
Database.
Definition: MessageTest.php:8
static provideListParam()
testDurationParams()
Message::durationParam Message::durationParams.
if(!isset( $args[0])) $lang
static destroyInstance()
Destroy the singleton instance.
testNewFromKey()
Message::newFromKey.
testToStringKey()
Message::__construct Message::text Message::plain Message::escaped Message::toString.
$value
testParser( $expect, $format)
Message::text Message::parse Message::parseAsBlock Message::toString Message::transformText Message::...
testInContentLanguage()
Message::inContentLanguage.
testRawHtmlInMsg()
CoreTagHooks::html.
static provideToString_raw()
static numParam( $num)
Definition: Message.php:1049
testInLanguage()
Message::inLanguage.
testInLanguageThrows()
MWException Message::inLanguage.
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
if( $line===false) $args
Definition: cdb.php:64
testSizeParams()
Message::sizeParam Message::sizeParams.
testRawMessage()
RawMessage::__construct RawMessage::fetchMessage.
testNewFromSpecifier( $value, $expectedText)
Message::newFromSpecifier provideNewFromSpecifier.
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 use $formDescriptor instead 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
static provideToString()
testConstructorLanguage( $key, $params, $languageCode)
Message::__construct Message::getLanguage provideConstructorLanguage.
testMessageAsParam()
Message::extractParam.
static provideConstructorLanguage()
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
testToString_raw( $message, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString_raw.
static provideConstructor()
Definition: MessageTest.php:42
$params
unserialize( $serialized)
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
static newFromKey( $key)
Factory function that is just wrapper for the real constructor.
Definition: Message.php:408
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
provideNewFromSpecifier()
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
testConstructor( $expectedLang, $key, $params, $language)
Message::__construct provideConstructor.
Definition: MessageTest.php:23
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:216
testNumParams()
Message::numParam Message::numParams.
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
testPlaintextParams( $expect, $format)
Message::plaintextParam Message::plaintextParams Message::formatPlaintext Message::toString Message::...
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:35
testTimeperiodParams()
Message::timeperiodParam Message::timeperiodParams.
testReplaceManyParams()
Message::params Message::toString Message::replaceParameters.
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:427
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
testRawParams()
Message::rawParam Message::rawParams.
Variant of the Message class.
Definition: RawMessage.php:34
testWfMessage()
wfMessage
testBitrateParams()
Message::bitrateParam Message::bitrateParams.
testKeys( $key, $expected, $exception=null)
Message::__construct Message::getKey Message::isMultiKey Message::getKeysToTry provideKeys.
static providePlaintextParams()
static provideConstructorParams()
Definition: MessageTest.php:53
testExpiryParams()
FIXME: This should not need database, but Language::formatExpiry does (T57912) Message::expiryParam M...
testInContentLanguageOverride()
Message::inContentLanguage.
testListParam( $list, $type, $format, $expect)
Message::listParam Message::extractParam Message::formatListParam provideListParam.
static listParam(array $list, $type='text')
Definition: Message.php:1126
static provideParser()
testWfMessageParams()
wfMessage Message::__construct
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319