MediaWiki  1.31.16
MessageTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\ObjectFactory;
4 use Wikimedia\TestingAccessWrapper;
5 
10 
11  protected function setUp() {
12  parent::setUp();
13 
14  $this->setMwGlobals( [
15  'wgForceUIMsgAsContentMsg' => [],
16  ] );
17  $this->setUserLang( 'en' );
18  }
19 
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->assertEquals( $expectedLang, $message->getLanguage() );
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->assertEquals( $expectedLang, $message->getLanguage() );
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 
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 
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 
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 
203  public function testWfMessage() {
204  $this->assertInstanceOf( Message::class, wfMessage( 'mainpage' ) );
205  $this->assertInstanceOf( Message::class, wfMessage( 'i-dont-exist-evar' ) );
206  }
207 
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 
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 
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 
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 
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 
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 );
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 
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 
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 
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() {
405  $this->setMwGlobals( 'wgRawHtml', true );
406  // We have to reset the core hook registration.
407  // to register the html hook
409  $this->setMwGlobals( 'wgParser',
410  ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
411  );
412 
413  $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
414  $txt = '<span class="error">&lt;html&gt; tags cannot be' .
415  ' used outside of normal pages.</span>';
416  $this->assertSame( $txt, $msg->parse() );
417  }
418 
424  public function testReplaceManyParams() {
425  $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
426  // One less than above has placeholders
427  $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
428  $this->assertSame(
429  'abcdefghijka2',
430  $msg->params( $params )->plain(),
431  'Params > 9 are replaced correctly'
432  );
433 
434  $msg = new RawMessage( 'Params$*' );
435  $params = [ 'ab', 'bc', 'cd' ];
436  $this->assertSame(
437  'Params: ab, bc, cd',
438  $msg->params( $params )->text()
439  );
440  }
441 
446  public function testNumParams() {
447  $lang = Language::factory( 'en' );
448  $msg = new RawMessage( '$1' );
449 
450  $this->assertSame(
451  $lang->formatNum( 123456.789 ),
452  $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
453  'numParams is handled correctly'
454  );
455  }
456 
461  public function testDurationParams() {
462  $lang = Language::factory( 'en' );
463  $msg = new RawMessage( '$1' );
464 
465  $this->assertSame(
466  $lang->formatDuration( 1234 ),
467  $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
468  'durationParams is handled correctly'
469  );
470  }
471 
477  public function testExpiryParams() {
478  $lang = Language::factory( 'en' );
479  $msg = new RawMessage( '$1' );
480 
481  $this->assertSame(
482  $lang->formatExpiry( wfTimestampNow() ),
483  $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
484  'expiryParams is handled correctly'
485  );
486  }
487 
492  public function testTimeperiodParams() {
493  $lang = Language::factory( 'en' );
494  $msg = new RawMessage( '$1' );
495 
496  $this->assertSame(
497  $lang->formatTimePeriod( 1234 ),
498  $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
499  'timeperiodParams is handled correctly'
500  );
501  }
502 
507  public function testSizeParams() {
508  $lang = Language::factory( 'en' );
509  $msg = new RawMessage( '$1' );
510 
511  $this->assertSame(
512  $lang->formatSize( 123456 ),
513  $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
514  'sizeParams is handled correctly'
515  );
516  }
517 
522  public function testBitrateParams() {
523  $lang = Language::factory( 'en' );
524  $msg = new RawMessage( '$1' );
525 
526  $this->assertSame(
527  $lang->formatBitrate( 123456 ),
528  $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
529  'bitrateParams is handled correctly'
530  );
531  }
532 
533  public static function providePlaintextParams() {
534  return [
535  [
536  'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
537  'plain',
538  ],
539 
540  [
541  // expect
542  'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
543  // format
544  'text',
545  ],
546  [
547  'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
548  'escaped',
549  ],
550 
551  [
552  'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
553  'parse',
554  ],
555 
556  [
557  "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
558  'parseAsBlock',
559  ],
560  ];
561  }
562 
572  public function testPlaintextParams( $expect, $format ) {
573  $lang = Language::factory( 'en' );
574 
575  $msg = new RawMessage( '$1 $2' );
576  $params = [
577  'one $2',
578  '<div>foo</div> [[Bar]] {{Baz}} &lt;',
579  ];
580  $this->assertSame(
581  $expect,
582  $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
583  "Fail formatting for $format"
584  );
585  }
586 
587  public static function provideListParam() {
588  $lang = Language::factory( 'de' );
589  $msg1 = new Message( 'mainpage', [], $lang );
590  $msg2 = new RawMessage( "''link''", [], $lang );
591 
592  return [
593  'Simple comma list' => [
594  [ 'a', 'b', 'c' ],
595  'comma',
596  'text',
597  'a, b, c'
598  ],
599 
600  'Simple semicolon list' => [
601  [ 'a', 'b', 'c' ],
602  'semicolon',
603  'text',
604  'a; b; c'
605  ],
606 
607  'Simple pipe list' => [
608  [ 'a', 'b', 'c' ],
609  'pipe',
610  'text',
611  'a | b | c'
612  ],
613 
614  'Simple text list' => [
615  [ 'a', 'b', 'c' ],
616  'text',
617  'text',
618  'a, b and c'
619  ],
620 
621  'Empty list' => [
622  [],
623  'comma',
624  'text',
625  ''
626  ],
627 
628  'List with all "before" params, ->text()' => [
629  [ "''link''", Message::numParam( 12345678 ) ],
630  'semicolon',
631  'text',
632  '\'\'link\'\'; 12,345,678'
633  ],
634 
635  'List with all "before" params, ->parse()' => [
636  [ "''link''", Message::numParam( 12345678 ) ],
637  'semicolon',
638  'parse',
639  '<i>link</i>; 12,345,678'
640  ],
641 
642  'List with all "after" params, ->text()' => [
643  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
644  'semicolon',
645  'text',
646  'Main Page; \'\'link\'\'; [[foo]]'
647  ],
648 
649  'List with all "after" params, ->parse()' => [
650  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ],
651  'semicolon',
652  'parse',
653  'Main Page; <i>link</i>; [[foo]]'
654  ],
655 
656  'List with both "before" and "after" params, ->text()' => [
657  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
658  'semicolon',
659  'text',
660  'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
661  ],
662 
663  'List with both "before" and "after" params, ->parse()' => [
664  [ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ],
665  'semicolon',
666  'parse',
667  'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
668  ],
669  ];
670  }
671 
678  public function testListParam( $list, $type, $format, $expect ) {
679  $lang = Language::factory( 'en' );
680 
681  $msg = new RawMessage( '$1' );
682  $msg->params( [ Message::listParam( $list, $type ) ] );
683  $this->assertEquals(
684  $expect,
685  $msg->inLanguage( $lang )->$format()
686  );
687  }
688 
692  public function testMessageAsParam() {
693  $this->setMwGlobals( [
694  'wgScript' => '/wiki/index.php',
695  'wgArticlePath' => '/wiki/$1',
696  ] );
697 
698  $msg = new Message( 'returnto', [
699  new Message( 'apihelp-link', [
700  'foo', new Message( 'mainpage', [], Language::factory( 'en' ) )
701  ], Language::factory( 'de' ) )
702  ], Language::factory( 'es' ) );
703 
704  $this->assertEquals(
705  'Volver a [[Special:ApiHelp/foo|Página principal]].',
706  $msg->text(),
707  'Process with ->text()'
708  );
709  $this->assertEquals(
710  '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
711  . "principal</a>.\n</p>",
712  $msg->parseAsBlock(),
713  'Process with ->parseAsBlock()'
714  );
715  }
716 
717  public static function provideParser() {
718  return [
719  [
720  "''&'' <x><!-- x -->",
721  'plain',
722  ],
723 
724  [
725  "''&'' <x><!-- x -->",
726  'text',
727  ],
728  [
729  '<i>&amp;</i> &lt;x&gt;',
730  'parse',
731  ],
732 
733  [
734  "<p><i>&amp;</i> &lt;x&gt;\n</p>",
735  'parseAsBlock',
736  ],
737  ];
738  }
739 
749  public function testParser( $expect, $format ) {
750  $msg = new RawMessage( "''&'' <x><!-- x -->" );
751  $this->assertSame(
752  $expect,
753  $msg->inLanguage( 'en' )->$format()
754  );
755  }
756 
760  public function testInContentLanguage() {
761  $this->setUserLang( 'fr' );
762 
763  // NOTE: make sure internal caching of the message text is reset appropriately
764  $msg = wfMessage( 'mainpage' );
765  $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
766  $this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
767  $this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
768  }
769 
773  public function testInContentLanguageOverride() {
774  $this->setMwGlobals( [
775  'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
776  ] );
777  $this->setUserLang( 'fr' );
778 
779  // NOTE: make sure internal caching of the message text is reset appropriately.
780  // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
781  $msg = wfMessage( 'mainpage' );
782  $this->assertSame(
783  'Accueil',
784  $msg->inContentLanguage()->plain(),
785  'inContentLanguage() with ForceUIMsg override enabled'
786  );
787  $this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
788  $this->assertSame(
789  'Main Page',
790  $msg->inContentLanguage()->plain(),
791  'inContentLanguage() with ForceUIMsg override enabled'
792  );
793  $this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
794  }
795 
800  public function testInLanguageThrows() {
801  wfMessage( 'foo' )->inLanguage( 123 );
802  }
803 
808  public function testSerialization() {
809  $msg = new Message( 'parentheses' );
810  $msg->rawParams( '<a>foo</a>' );
811  $msg->title( Title::newFromText( 'Testing' ) );
812  $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
813  $msg = unserialize( serialize( $msg ) );
814  $this->assertSame( '(<a>foo</a>)', $msg->parse() );
815  $title = TestingAccessWrapper::newFromObject( $msg )->title;
816  $this->assertInstanceOf( Title::class, $title );
817  $this->assertSame( 'Testing', $title->getFullText() );
818 
819  $msg = new Message( 'mainpage' );
820  $msg->inLanguage( 'de' );
821  $this->assertSame( 'Hauptseite', $msg->plain(), 'Sanity check' );
822  $msg = unserialize( serialize( $msg ) );
823  $this->assertSame( 'Hauptseite', $msg->plain() );
824  }
825 
830  public function testNewFromSpecifier( $value, $expectedText ) {
831  $message = Message::newFromSpecifier( $value );
832  $this->assertInstanceOf( Message::class, $message );
833  if ( $value instanceof Message ) {
834  $this->assertInstanceOf( get_class( $value ), $message );
835  $this->assertEquals( $value, $message );
836  }
837  $this->assertSame( $expectedText, $message->text() );
838  }
839 
840  public function provideNewFromSpecifier() {
841  $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
842  $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
843  $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
844 
845  return [
846  'string' => [ 'mainpage', 'Main Page' ],
847  'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
848  'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
849  'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
850  'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
851  'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
852  'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
853  ];
854  }
855 }
MessageTest\testDurationParams
testDurationParams()
Message::durationParam Message::durationParams.
Definition: MessageTest.php:461
MessageTest\testExpiryParams
testExpiryParams()
FIXME: This should not need database, but Language::formatExpiry does (T57912) Message::expiryParam M...
Definition: MessageTest.php:477
MessageTest\testInLanguage
testInLanguage()
Message::inLanguage.
Definition: MessageTest.php:356
Message\numParam
static numParam( $num)
Definition: Message.php:1028
Message\newFromSpecifier
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:414
MessageTest
Database.
Definition: MessageTest.php:9
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
MessageTest\testParser
testParser( $expect, $format)
Message::text Message::parse Message::parseAsBlock Message::toString Message::transformText Message::...
Definition: MessageTest.php:749
MessageTest\testInContentLanguage
testInContentLanguage()
Message::inContentLanguage.
Definition: MessageTest.php:760
MessageTest\testKeys
testKeys( $key, $expected, $exception=null)
Message::__construct Message::getKey Message::isMultiKey Message::getKeysToTry provideKeys.
Definition: MessageTest.php:189
MessageTest\testSizeParams
testSizeParams()
Message::sizeParam Message::sizeParams.
Definition: MessageTest.php:507
MessageTest\testListParam
testListParam( $list, $type, $format, $expect)
Message::listParam Message::extractParam Message::formatListParam provideListParam.
Definition: MessageTest.php:678
MessageTest\testRawHtmlInMsg
testRawHtmlInMsg()
Definition: MessageTest.php:403
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
$wgParserConf
$wgParserConf
Parser configuration.
Definition: DefaultSettings.php:4170
captcha-old.count
count
Definition: captcha-old.py:249
text
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
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:11
MessageTest\setUp
setUp()
Definition: MessageTest.php:11
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:192
$params
$params
Definition: styleTest.css.php:40
serialize
serialize()
Definition: ApiMessage.php:184
MessageTest\testWfMessageParams
testWfMessageParams()
wfMessage Message::__construct
Definition: MessageTest.php:220
MessageTest\provideConstructor
static provideConstructor()
Definition: MessageTest.php:43
MessageTest\testMessageAsParam
testMessageAsParam()
Message::extractParam.
Definition: MessageTest.php:692
MessageTest\provideConstructorLanguage
static provideConstructorLanguage()
Definition: MessageTest.php:131
MessageTest\testConstructorParams
testConstructorParams( $expected, $args)
Message::__construct Message::getParams provideConstructorParams.
Definition: MessageTest.php:122
Message\listParam
static listParam(array $list, $type='text')
Definition: Message.php:1105
php
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
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:205
MessageTest\testNumParams
testNumParams()
Message::numParam Message::numParams.
Definition: MessageTest.php:446
MessageTest\testNewFromKey
testNewFromKey()
Message::newFromKey.
Definition: MessageTest.php:211
MessageTest\testToString_raw
testToString_raw( $message, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString_raw.
Definition: MessageTest.php:340
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
MessageTest\provideListParam
static provideListParam()
Definition: MessageTest.php:587
MessageTest\testConstructor
testConstructor( $expectedLang, $key, $params, $language)
Message::__construct provideConstructor.
Definition: MessageTest.php:24
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
MessageTest\testToStringKey
testToStringKey()
Message::__construct Message::text Message::plain Message::escaped Message::toString.
Definition: MessageTest.php:281
MessageTest\testRawMessage
testRawMessage()
RawMessage::__construct RawMessage::fetchMessage.
Definition: MessageTest.php:397
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2009
MessageTest\provideToString_raw
static provideToString_raw()
Definition: MessageTest.php:320
MessageTest\testNewFromSpecifier
testNewFromSpecifier( $value, $expectedText)
Message::newFromSpecifier provideNewFromSpecifier.
Definition: MessageTest.php:830
MediaWikiTestCase\setUserLang
setUserLang( $lang)
Definition: MediaWikiTestCase.php:883
MessageTest\testBitrateParams
testBitrateParams()
Message::bitrateParam Message::bitrateParams.
Definition: MessageTest.php:522
MessageTest\testInLanguageThrows
testInLanguageThrows()
MWException Message::inLanguage.
Definition: MessageTest.php:800
MessageTest\testTimeperiodParams
testTimeperiodParams()
Message::timeperiodParam Message::timeperiodParams.
Definition: MessageTest.php:492
MessageTest\testReplaceManyParams
testReplaceManyParams()
Message::params Message::toString Message::replaceParameters.
Definition: MessageTest.php:424
MessageTest\testConstructorLanguage
testConstructorLanguage( $key, $params, $languageCode)
Message::__construct Message::getLanguage provideConstructorLanguage.
Definition: MessageTest.php:143
MessageTest\providePlaintextParams
static providePlaintextParams()
Definition: MessageTest.php:533
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
MessageTest\testWfMessage
testWfMessage()
wfMessage
Definition: MessageTest.php:203
$value
$value
Definition: styleTest.css.php:45
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1017
MessageTest\testInContentLanguageOverride
testInContentLanguageOverride()
Message::inContentLanguage.
Definition: MessageTest.php:773
MessageTest\provideToString
static provideToString()
Definition: MessageTest.php:294
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
MessageTest\provideConstructorParams
static provideConstructorParams()
Definition: MessageTest.php:54
plain
either a plain
Definition: hooks.txt:2056
MessageTest\provideParser
static provideParser()
Definition: MessageTest.php:717
MessageTest\testSerialization
testSerialization()
Message::serialize Message::unserialize.
Definition: MessageTest.php:808
$args
if( $line===false) $args
Definition: cdb.php:64
MessageTest\testPlaintextParams
testPlaintextParams( $expect, $format)
Message::plaintextParam Message::plaintextParams Message::formatPlaintext Message::toString Message::...
Definition: MessageTest.php:572
MessageTest\provideNewFromSpecifier
provideNewFromSpecifier()
Definition: MessageTest.php:840
MessageTest\testToString
testToString( $key, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString.
Definition: MessageTest.php:312
Message
The Message class provides methods which fulfil two basic services:
Definition: Message.php:159
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
wfMessage
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
class
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:56
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
Message\newFromKey
static newFromKey( $key)
Factory function that is just wrapper for the real constructor.
Definition: Message.php:395
MessageTest\testExists
testExists()
Message::exists.
Definition: MessageTest.php:265
MessageTest\provideKeys
static provideKeys()
Definition: MessageTest.php:150
MessageTest\testRawParams
testRawParams()
Message::rawParam Message::rawParams.
Definition: MessageTest.php:374
$type
$type
Definition: testCompression.php:48
MessageCache\destroyInstance
static destroyInstance()
Destroy the singleton instance.
Definition: MessageCache.php:135