MediaWiki  1.33.0
MessageTest.php
Go to the documentation of this file.
1 <?php
2 
4 use Wikimedia\TestingAccessWrapper;
5 
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 
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 
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 
406  public function testRawHtmlInMsg() {
407  $this->setMwGlobals( 'wgRawHtml', true );
408  // We have to reset the core hook registration.
409  // to register the html hook
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
801  public function testInLanguageThrows() {
802  wfMessage( 'foo' )->inLanguage( 123 );
803  }
804 
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 
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 }
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:306
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
captcha-old.count
count
Definition: captcha-old.py:249
$params
$params
Definition: styleTest.css.php:44
serialize
serialize()
Definition: ApiMessageTrait.php:134
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:35
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
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:709
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:10
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1941
MediaWikiTestCase\setUserLang
setUserLang( $lang)
Definition: MediaWikiTestCase.php:1057
null
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
MediaWikiLangTestCase\setUp
setUp()
Definition: MediaWikiLangTestCase.php:9
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
$value
$value
Definition: styleTest.css.php:49
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
plain
either a plain
Definition: hooks.txt:2046
text
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
Definition: All_system_messages.txt:1267
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
$args
if( $line===false) $args
Definition: cdb.php:64
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
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:52
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
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 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
$type
$type
Definition: testCompression.php:48
MessageCache\destroyInstance
static destroyInstance()
Destroy the singleton instance.
Definition: MessageCache.php:138