MediaWiki  1.28.0
MessageTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5  protected function setUp() {
6  parent::setUp();
7 
8  $this->setMwGlobals( [
9  'wgForceUIMsgAsContentMsg' => [],
10  ] );
11  $this->setUserLang( 'en' );
12  }
13 
18  public function testConstructor( $expectedLang, $key, $params, $language ) {
19  $message = new Message( $key, $params, $language );
20 
21  $this->assertEquals( $key, $message->getKey() );
22  $this->assertEquals( $params, $message->getParams() );
23  $this->assertEquals( $expectedLang, $message->getLanguage() );
24 
25  $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
26  $messageSpecifier->expects( $this->any() )
27  ->method( 'getKey' )->will( $this->returnValue( $key ) );
28  $messageSpecifier->expects( $this->any() )
29  ->method( 'getParams' )->will( $this->returnValue( $params ) );
30  $message = new Message( $messageSpecifier, [], $language );
31 
32  $this->assertEquals( $key, $message->getKey() );
33  $this->assertEquals( $params, $message->getParams() );
34  $this->assertEquals( $expectedLang, $message->getLanguage() );
35  }
36 
37  public static function provideConstructor() {
38  $langDe = Language::factory( 'de' );
39  $langEn = Language::factory( 'en' );
40 
41  return [
42  [ $langDe, 'foo', [], $langDe ],
43  [ $langDe, 'foo', [ 'bar' ], $langDe ],
44  [ $langEn, 'foo', [ 'bar' ], null ]
45  ];
46  }
47 
48  public static function provideConstructorParams() {
49  return [
50  [
51  [],
52  [],
53  ],
54  [
55  [ 'foo' ],
56  [ 'foo' ],
57  ],
58  [
59  [ 'foo', 'bar' ],
60  [ 'foo', 'bar' ],
61  ],
62  [
63  [ 'baz' ],
64  [ [ 'baz' ] ],
65  ],
66  [
67  [ 'baz', 'foo' ],
68  [ [ 'baz', 'foo' ] ],
69  ],
70  [
71  [ 'baz', 'foo' ],
72  [ [ 'baz', 'foo' ], 'hhh' ],
73  ],
74  [
75  [ 'baz', 'foo' ],
76  [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
77  ],
78  [
79  [ 'baz', 'foo' ],
80  [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
81  ],
82  [
83  [ 'baz' ],
84  [ [ 'baz' ], [ 'ahahahahha' ] ],
85  ],
86  ];
87  }
88 
94  public function testConstructorParams( $expected, $args ) {
95  $msg = new Message( 'imasomething' );
96 
97  $returned = call_user_func_array( [ $msg, 'params' ], $args );
98 
99  $this->assertSame( $msg, $returned );
100  $this->assertEquals( $expected, $msg->getParams() );
101  }
102 
103  public static function provideConstructorLanguage() {
104  return [
105  [ 'foo', [ 'bar' ], 'en' ],
106  [ 'foo', [ 'bar' ], 'de' ]
107  ];
108  }
109 
115  public function testConstructorLanguage( $key, $params, $languageCode ) {
116  $language = Language::factory( $languageCode );
117  $message = new Message( $key, $params, $language );
118 
119  $this->assertEquals( $language, $message->getLanguage() );
120  }
121 
122  public static function provideKeys() {
123  return [
124  'string' => [
125  'key' => 'mainpage',
126  'expected' => [ 'mainpage' ],
127  ],
128  'single' => [
129  'key' => [ 'mainpage' ],
130  'expected' => [ 'mainpage' ],
131  ],
132  'multi' => [
133  'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
134  'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
135  ],
136  'empty' => [
137  'key' => [],
138  'expected' => null,
139  'exception' => 'InvalidArgumentException',
140  ],
141  'null' => [
142  'key' => null,
143  'expected' => null,
144  'exception' => 'InvalidArgumentException',
145  ],
146  'bad type' => [
147  'key' => 123,
148  'expected' => null,
149  'exception' => 'InvalidArgumentException',
150  ],
151  ];
152  }
153 
161  public function testKeys( $key, $expected, $exception = null ) {
162  if ( $exception ) {
163  $this->setExpectedException( $exception );
164  }
165 
166  $msg = new Message( $key );
167  $this->assertContains( $msg->getKey(), $expected );
168  $this->assertEquals( $expected, $msg->getKeysToTry() );
169  $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
170  }
171 
175  public function testWfMessage() {
176  $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
177  $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
178  }
179 
183  public function testNewFromKey() {
184  $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
185  $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
186  }
187 
192  public function testWfMessageParams() {
193  $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
194  $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
195  $this->assertEquals(
196  'You have foo (bar).',
197  wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
198  );
199  $this->assertEquals(
200  'You have foo (bar).',
201  wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
202  );
203  }
204 
208  public function testExists() {
209  $this->assertTrue( wfMessage( 'mainpage' )->exists() );
210  $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
211  $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
212  $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
213  $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
214  $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
215  }
216 
224  public function testToStringKey() {
225  $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
226  $this->assertEquals( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
227  $this->assertEquals( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
228  $this->assertEquals( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
229  $this->assertEquals( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
230  $this->assertEquals( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
231  $this->assertEquals(
232  '⧼i&lt;dont&gt;exist-evar⧽',
233  wfMessage( 'i<dont>exist-evar' )->escaped()
234  );
235  }
236 
237  public static function provideToString() {
238  return [
239  [ 'mainpage', 'Main Page' ],
240  [ 'i-dont-exist-evar', '⧼i-dont-exist-evar⧽' ],
241  [ 'i-dont-exist-evar', '⧼i-dont-exist-evar⧽', 'escaped' ],
242  [ 'script>alert(1)</script', '⧼script&gt;alert(1)&lt;/script⧽', 'escaped' ],
243  [ 'script>alert(1)</script', '⧼script&gt;alert(1)&lt;/script⧽' ],
244  ];
245  }
246 
252  public function testToString( $key, $expect, $format = 'plain' ) {
253  $msg = new Message( $key );
254  $msg->$format();
255  $this->assertEquals( $expect, $msg->toString() );
256  $this->assertEquals( $expect, $msg->__toString() );
257  }
258 
262  public function testInLanguage() {
263  $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
264  $this->assertEquals( 'Заглавная страница',
265  wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
266 
267  // NOTE: make sure internal caching of the message text is reset appropriately
268  $msg = wfMessage( 'mainpage' );
269  $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
270  $this->assertEquals(
271  'Заглавная страница',
272  $msg->inLanguage( Language::factory( 'ru' ) )->text()
273  );
274  }
275 
280  public function testRawParams() {
281  $this->assertEquals(
282  '(Заглавная страница)',
283  wfMessage( 'parentheses', 'Заглавная страница' )->plain()
284  );
285  $this->assertEquals(
286  '(Заглавная страница $1)',
287  wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
288  );
289  $this->assertEquals(
290  '(Заглавная страница)',
291  wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
292  );
293  $this->assertEquals(
294  '(Заглавная страница $1)',
295  wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
296  );
297  }
298 
303  public function testRawMessage() {
304  $msg = new RawMessage( 'example &' );
305  $this->assertEquals( 'example &', $msg->plain() );
306  $this->assertEquals( 'example &amp;', $msg->escaped() );
307  }
308 
314  public function testReplaceManyParams() {
315  $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
316  // One less than above has placeholders
317  $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
318  $this->assertEquals(
319  'abcdefghijka2',
320  $msg->params( $params )->plain(),
321  'Params > 9 are replaced correctly'
322  );
323 
324  $msg = new RawMessage( 'Params$*' );
325  $params = [ 'ab', 'bc', 'cd' ];
326  $this->assertEquals(
327  'Params: ab, bc, cd',
328  $msg->params( $params )->text()
329  );
330  }
331 
336  public function testNumParams() {
337  $lang = Language::factory( 'en' );
338  $msg = new RawMessage( '$1' );
339 
340  $this->assertEquals(
341  $lang->formatNum( 123456.789 ),
342  $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
343  'numParams is handled correctly'
344  );
345  }
346 
351  public function testDurationParams() {
352  $lang = Language::factory( 'en' );
353  $msg = new RawMessage( '$1' );
354 
355  $this->assertEquals(
356  $lang->formatDuration( 1234 ),
357  $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
358  'durationParams is handled correctly'
359  );
360  }
361 
368  public function testExpiryParams() {
369  $lang = Language::factory( 'en' );
370  $msg = new RawMessage( '$1' );
371 
372  $this->assertEquals(
373  $lang->formatExpiry( wfTimestampNow() ),
374  $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
375  'expiryParams is handled correctly'
376  );
377  }
378 
383  public function testTimeperiodParams() {
384  $lang = Language::factory( 'en' );
385  $msg = new RawMessage( '$1' );
386 
387  $this->assertEquals(
388  $lang->formatTimePeriod( 1234 ),
389  $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
390  'timeperiodParams is handled correctly'
391  );
392  }
393 
398  public function testSizeParams() {
399  $lang = Language::factory( 'en' );
400  $msg = new RawMessage( '$1' );
401 
402  $this->assertEquals(
403  $lang->formatSize( 123456 ),
404  $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
405  'sizeParams is handled correctly'
406  );
407  }
408 
413  public function testBitrateParams() {
414  $lang = Language::factory( 'en' );
415  $msg = new RawMessage( '$1' );
416 
417  $this->assertEquals(
418  $lang->formatBitrate( 123456 ),
419  $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
420  'bitrateParams is handled correctly'
421  );
422  }
423 
424  public static function providePlaintextParams() {
425  return [
426  [
427  'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
428  'plain',
429  ],
430 
431  [
432  // expect
433  'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
434  // format
435  'text',
436  ],
437  [
438  'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
439  'escaped',
440  ],
441 
442  [
443  'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
444  'parse',
445  ],
446 
447  [
448  "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
449  'parseAsBlock',
450  ],
451  ];
452  }
453 
463  public function testPlaintextParams( $expect, $format ) {
464  $lang = Language::factory( 'en' );
465 
466  $msg = new RawMessage( '$1 $2' );
467  $params = [
468  'one $2',
469  '<div>foo</div> [[Bar]] {{Baz}} &lt;',
470  ];
471  $this->assertEquals(
472  $expect,
473  $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
474  "Fail formatting for $format"
475  );
476  }
477 
478  public static function provideParser() {
479  return [
480  [
481  "''&'' <x><!-- x -->",
482  'plain',
483  ],
484 
485  [
486  "''&'' <x><!-- x -->",
487  'text',
488  ],
489  [
490  '<i>&amp;</i> &lt;x&gt;',
491  'parse',
492  ],
493 
494  [
495  "<p><i>&amp;</i> &lt;x&gt;\n</p>",
496  'parseAsBlock',
497  ],
498  ];
499  }
500 
510  public function testParser( $expect, $format ) {
511  $msg = new RawMessage( "''&'' <x><!-- x -->" );
512  $this->assertEquals(
513  $expect,
514  $msg->inLanguage( 'en' )->$format()
515  );
516  }
517 
521  public function testInContentLanguage() {
522  $this->setUserLang( 'fr' );
523 
524  // NOTE: make sure internal caching of the message text is reset appropriately
525  $msg = wfMessage( 'mainpage' );
526  $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
527  $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
528  $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
529  }
530 
534  public function testInContentLanguageOverride() {
535  $this->setMwGlobals( [
536  'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
537  ] );
538  $this->setUserLang( 'fr' );
539 
540  // NOTE: make sure internal caching of the message text is reset appropriately.
541  // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
542  $msg = wfMessage( 'mainpage' );
543  $this->assertEquals(
544  'Accueil',
545  $msg->inContentLanguage()->plain(),
546  'inContentLanguage() with ForceUIMsg override enabled'
547  );
548  $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
549  $this->assertEquals(
550  'Main Page',
551  $msg->inContentLanguage()->plain(),
552  'inContentLanguage() with ForceUIMsg override enabled'
553  );
554  $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
555  }
556 
561  public function testInLanguageThrows() {
562  wfMessage( 'foo' )->inLanguage( 123 );
563  }
564 
569  public function testSerialization() {
570  $msg = new Message( 'parentheses' );
571  $msg->rawParams( '<a>foo</a>' );
572  $msg->title( Title::newFromText( 'Testing' ) );
573  $this->assertEquals( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
574  $msg = unserialize( serialize( $msg ) );
575  $this->assertEquals( '(<a>foo</a>)', $msg->parse() );
577  $this->assertInstanceOf( 'Title', $title );
578  $this->assertEquals( 'Testing', $title->getFullText() );
579 
580  $msg = new Message( 'mainpage' );
581  $msg->inLanguage( 'de' );
582  $this->assertEquals( 'Hauptseite', $msg->plain(), 'Sanity check' );
583  $msg = unserialize( serialize( $msg ) );
584  $this->assertEquals( 'Hauptseite', $msg->plain() );
585  }
586 
591  public function testNewFromSpecifier( $value, $expectedText ) {
592  $message = Message::newFromSpecifier( $value );
593  $this->assertInstanceOf( Message::class, $message );
594  if ( $value instanceof Message ) {
595  $this->assertInstanceOf( get_class( $value ), $message );
596  $this->assertEquals( $value, $message );
597  }
598  $this->assertSame( $expectedText, $message->text() );
599  }
600 
601  public function provideNewFromSpecifier() {
602  $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
603  $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
604  $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
605 
606  return [
607  'string' => [ 'mainpage', 'Main Page' ],
608  'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
609  'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
610  'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
611  'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
612  'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
613  'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
614  ];
615  }
616 }
617 
testSerialization()
Message::serialize Message::unserialize.
either a plain
Definition: hooks.txt:1987
testExists()
Message::exists.
static provideKeys()
testDurationParams()
Message::durationParam Message::durationParams.
if(!isset($args[0])) $lang
testNewFromKey()
Message::newFromKey.
testToStringKey()
Message::__construct Message::text Message::plain Message::escaped Message::toString.
$value
testConstructorParams($expected, $args)
Message::__construct Message::getParams provideConstructorParams.
Definition: MessageTest.php:94
testInContentLanguage()
Message::inContentLanguage.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
testInLanguage()
Message::inLanguage.
testInLanguageThrows()
MWException Message::inLanguage.
testNewFromSpecifier($value, $expectedText)
Message::newFromSpecifier provideNewFromSpecifier.
if($line===false) $args
Definition: cdb.php:64
testSizeParams()
Message::sizeParam Message::sizeParams.
testRawMessage()
RawMessage::__construct RawMessage::fetchMessage.
static provideToString()
unserialize($serialized)
Definition: ApiMessage.php:102
static provideConstructorLanguage()
testPlaintextParams($expect, $format)
Message::plaintextParam Message::plaintextParams Message::formatPlaintext Message::toString Message::...
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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
testToString($key, $expect, $format= 'plain')
Message::toString Message::__toString provideToString.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
static provideConstructor()
Definition: MessageTest.php:37
$params
Base class that store and restore the Language objects.
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:115
provideNewFromSpecifier()
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
testParser($expect, $format)
Message::text Message::parse Message::parseAsBlock Message::toString Message::transformText Message::...
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:12
testNumParams()
Message::numParam Message::numParams.
static newFromKey($key)
Factory function that is just wrapper for the real constructor.
Definition: Message.php:379
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.
testConstructor($expectedLang, $key, $params, $language)
Message::__construct provideConstructor.
Definition: MessageTest.php:18
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: Message.php:1242
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:48
testExpiryParams()
FIXME: This should not need database, but Language::formatExpiry does (bug 55912) Database Message::e...
serialize()
Definition: ApiMessage.php:94
static newFromObject($object)
Return the same object, without access restrictions.
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181
testInContentLanguageOverride()
Message::inContentLanguage.
setMwGlobals($pairs, $value=null)
testConstructorLanguage($key, $params, $languageCode)
Message::__construct Message::getLanguage provideConstructorLanguage.
static provideParser()
testWfMessageParams()
wfMessage Message::__construct
static newFromSpecifier($value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:398