MediaWiki REL1_28
MessageTest.php
Go to the documentation of this file.
1<?php
3
5
6 protected function setUp() {
7 parent::setUp();
8
9 $this->setMwGlobals( [
10 'wgForceUIMsgAsContentMsg' => [],
11 ] );
12 $this->setUserLang( 'en' );
13 }
14
19 public function testConstructor( $expectedLang, $key, $params, $language ) {
20 $message = new Message( $key, $params, $language );
21
22 $this->assertEquals( $key, $message->getKey() );
23 $this->assertEquals( $params, $message->getParams() );
24 $this->assertEquals( $expectedLang, $message->getLanguage() );
25
26 $messageSpecifier = $this->getMockForAbstractClass( 'MessageSpecifier' );
27 $messageSpecifier->expects( $this->any() )
28 ->method( 'getKey' )->will( $this->returnValue( $key ) );
29 $messageSpecifier->expects( $this->any() )
30 ->method( 'getParams' )->will( $this->returnValue( $params ) );
31 $message = new Message( $messageSpecifier, [], $language );
32
33 $this->assertEquals( $key, $message->getKey() );
34 $this->assertEquals( $params, $message->getParams() );
35 $this->assertEquals( $expectedLang, $message->getLanguage() );
36 }
37
38 public static function provideConstructor() {
39 $langDe = Language::factory( 'de' );
40 $langEn = Language::factory( 'en' );
41
42 return [
43 [ $langDe, 'foo', [], $langDe ],
44 [ $langDe, 'foo', [ 'bar' ], $langDe ],
45 [ $langEn, 'foo', [ 'bar' ], null ]
46 ];
47 }
48
49 public static function provideConstructorParams() {
50 return [
51 [
52 [],
53 [],
54 ],
55 [
56 [ 'foo' ],
57 [ 'foo' ],
58 ],
59 [
60 [ 'foo', 'bar' ],
61 [ 'foo', 'bar' ],
62 ],
63 [
64 [ 'baz' ],
65 [ [ 'baz' ] ],
66 ],
67 [
68 [ 'baz', 'foo' ],
69 [ [ 'baz', 'foo' ] ],
70 ],
71 [
72 [ 'baz', 'foo' ],
73 [ [ 'baz', 'foo' ], 'hhh' ],
74 ],
75 [
76 [ 'baz', 'foo' ],
77 [ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ],
78 ],
79 [
80 [ 'baz', 'foo' ],
81 [ [ 'baz', 'foo' ], [ 'ahahahahha' ] ],
82 ],
83 [
84 [ 'baz' ],
85 [ [ 'baz' ], [ 'ahahahahha' ] ],
86 ],
87 ];
88 }
89
95 public function testConstructorParams( $expected, $args ) {
96 $msg = new Message( 'imasomething' );
97
98 $returned = call_user_func_array( [ $msg, 'params' ], $args );
99
100 $this->assertSame( $msg, $returned );
101 $this->assertEquals( $expected, $msg->getParams() );
102 }
103
104 public static function provideConstructorLanguage() {
105 return [
106 [ 'foo', [ 'bar' ], 'en' ],
107 [ 'foo', [ 'bar' ], 'de' ]
108 ];
109 }
110
116 public function testConstructorLanguage( $key, $params, $languageCode ) {
117 $language = Language::factory( $languageCode );
118 $message = new Message( $key, $params, $language );
119
120 $this->assertEquals( $language, $message->getLanguage() );
121 }
122
123 public static function provideKeys() {
124 return [
125 'string' => [
126 'key' => 'mainpage',
127 'expected' => [ 'mainpage' ],
128 ],
129 'single' => [
130 'key' => [ 'mainpage' ],
131 'expected' => [ 'mainpage' ],
132 ],
133 'multi' => [
134 'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
135 'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ],
136 ],
137 'empty' => [
138 'key' => [],
139 'expected' => null,
140 'exception' => 'InvalidArgumentException',
141 ],
142 'null' => [
143 'key' => null,
144 'expected' => null,
145 'exception' => 'InvalidArgumentException',
146 ],
147 'bad type' => [
148 'key' => 123,
149 'expected' => null,
150 'exception' => 'InvalidArgumentException',
151 ],
152 ];
153 }
154
162 public function testKeys( $key, $expected, $exception = null ) {
163 if ( $exception ) {
164 $this->setExpectedException( $exception );
165 }
166
167 $msg = new Message( $key );
168 $this->assertContains( $msg->getKey(), $expected );
169 $this->assertEquals( $expected, $msg->getKeysToTry() );
170 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
171 }
172
176 public function testWfMessage() {
177 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
178 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
179 }
180
184 public function testNewFromKey() {
185 $this->assertInstanceOf( 'Message', Message::newFromKey( 'mainpage' ) );
186 $this->assertInstanceOf( 'Message', Message::newFromKey( 'i-dont-exist-evar' ) );
187 }
188
193 public function testWfMessageParams() {
194 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
195 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', [] )->text() );
196 $this->assertEquals(
197 'You have foo (bar).',
198 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
199 );
200 $this->assertEquals(
201 'You have foo (bar).',
202 wfMessage( 'youhavenewmessages', [ 'foo', 'bar' ] )->text()
203 );
204 }
205
209 public function testExists() {
210 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
211 $this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() );
212 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
213 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
214 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() );
215 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
216 }
217
225 public function testToStringKey() {
226 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
227 $this->assertEquals( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() );
228 $this->assertEquals( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() );
229 $this->assertEquals( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() );
230 $this->assertEquals( '⧼i&lt;dont&gt;exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() );
231 $this->assertEquals( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() );
232 $this->assertEquals(
233 '⧼i&lt;dont&gt;exist-evar⧽',
234 wfMessage( 'i<dont>exist-evar' )->escaped()
235 );
236 }
237
238 public static function provideToString() {
239 return [
240 [ 'mainpage', 'Main Page' ],
241 [ 'i-dont-exist-evar', '⧼i-dont-exist-evar⧽' ],
242 [ 'i-dont-exist-evar', '⧼i-dont-exist-evar⧽', 'escaped' ],
243 [ 'script>alert(1)</script', '⧼script&gt;alert(1)&lt;/script⧽', 'escaped' ],
244 [ 'script>alert(1)</script', '⧼script&gt;alert(1)&lt;/script⧽' ],
245 ];
246 }
247
253 public function testToString( $key, $expect, $format = 'plain' ) {
254 $msg = new Message( $key );
255 $msg->$format();
256 $this->assertEquals( $expect, $msg->toString() );
257 $this->assertEquals( $expect, $msg->__toString() );
258 }
259
263 public function testInLanguage() {
264 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
265 $this->assertEquals( 'Заглавная страница',
266 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
267
268 // NOTE: make sure internal caching of the message text is reset appropriately
269 $msg = wfMessage( 'mainpage' );
270 $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
271 $this->assertEquals(
272 'Заглавная страница',
273 $msg->inLanguage( Language::factory( 'ru' ) )->text()
274 );
275 }
276
281 public function testRawParams() {
282 $this->assertEquals(
283 '(Заглавная страница)',
284 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
285 );
286 $this->assertEquals(
287 '(Заглавная страница $1)',
288 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
289 );
290 $this->assertEquals(
291 '(Заглавная страница)',
292 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
293 );
294 $this->assertEquals(
295 '(Заглавная страница $1)',
296 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
297 );
298 }
299
304 public function testRawMessage() {
305 $msg = new RawMessage( 'example &' );
306 $this->assertEquals( 'example &', $msg->plain() );
307 $this->assertEquals( 'example &amp;', $msg->escaped() );
308 }
309
310 public function testRawHtmlInMsg() {
312 $this->setMwGlobals( 'wgRawHtml', true );
313 // We have to reset the core hook registration.
314 // to register the html hook
316 $this->setMwGlobals( 'wgParser',
317 ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
318 );
319
320 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
321 $txt = '<span class="error">&lt;html&gt; tags cannot be' .
322 ' used outside of normal pages.</span>';
323 $this->assertSame( $txt, $msg->parse() );
324 }
325
331 public function testReplaceManyParams() {
332 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
333 // One less than above has placeholders
334 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
335 $this->assertEquals(
336 'abcdefghijka2',
337 $msg->params( $params )->plain(),
338 'Params > 9 are replaced correctly'
339 );
340
341 $msg = new RawMessage( 'Params$*' );
342 $params = [ 'ab', 'bc', 'cd' ];
343 $this->assertEquals(
344 'Params: ab, bc, cd',
345 $msg->params( $params )->text()
346 );
347 }
348
353 public function testNumParams() {
354 $lang = Language::factory( 'en' );
355 $msg = new RawMessage( '$1' );
356
357 $this->assertEquals(
358 $lang->formatNum( 123456.789 ),
359 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
360 'numParams is handled correctly'
361 );
362 }
363
368 public function testDurationParams() {
369 $lang = Language::factory( 'en' );
370 $msg = new RawMessage( '$1' );
371
372 $this->assertEquals(
373 $lang->formatDuration( 1234 ),
374 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
375 'durationParams is handled correctly'
376 );
377 }
378
385 public function testExpiryParams() {
386 $lang = Language::factory( 'en' );
387 $msg = new RawMessage( '$1' );
388
389 $this->assertEquals(
390 $lang->formatExpiry( wfTimestampNow() ),
391 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
392 'expiryParams is handled correctly'
393 );
394 }
395
400 public function testTimeperiodParams() {
401 $lang = Language::factory( 'en' );
402 $msg = new RawMessage( '$1' );
403
404 $this->assertEquals(
405 $lang->formatTimePeriod( 1234 ),
406 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
407 'timeperiodParams is handled correctly'
408 );
409 }
410
415 public function testSizeParams() {
416 $lang = Language::factory( 'en' );
417 $msg = new RawMessage( '$1' );
418
419 $this->assertEquals(
420 $lang->formatSize( 123456 ),
421 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
422 'sizeParams is handled correctly'
423 );
424 }
425
430 public function testBitrateParams() {
431 $lang = Language::factory( 'en' );
432 $msg = new RawMessage( '$1' );
433
434 $this->assertEquals(
435 $lang->formatBitrate( 123456 ),
436 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
437 'bitrateParams is handled correctly'
438 );
439 }
440
441 public static function providePlaintextParams() {
442 return [
443 [
444 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
445 'plain',
446 ],
447
448 [
449 // expect
450 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
451 // format
452 'text',
453 ],
454 [
455 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
456 'escaped',
457 ],
458
459 [
460 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
461 'parse',
462 ],
463
464 [
465 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
466 'parseAsBlock',
467 ],
468 ];
469 }
470
480 public function testPlaintextParams( $expect, $format ) {
481 $lang = Language::factory( 'en' );
482
483 $msg = new RawMessage( '$1 $2' );
484 $params = [
485 'one $2',
486 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
487 ];
488 $this->assertEquals(
489 $expect,
490 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
491 "Fail formatting for $format"
492 );
493 }
494
495 public static function provideParser() {
496 return [
497 [
498 "''&'' <x><!-- x -->",
499 'plain',
500 ],
501
502 [
503 "''&'' <x><!-- x -->",
504 'text',
505 ],
506 [
507 '<i>&amp;</i> &lt;x&gt;',
508 'parse',
509 ],
510
511 [
512 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
513 'parseAsBlock',
514 ],
515 ];
516 }
517
527 public function testParser( $expect, $format ) {
528 $msg = new RawMessage( "''&'' <x><!-- x -->" );
529 $this->assertEquals(
530 $expect,
531 $msg->inLanguage( 'en' )->$format()
532 );
533 }
534
538 public function testInContentLanguage() {
539 $this->setUserLang( 'fr' );
540
541 // NOTE: make sure internal caching of the message text is reset appropriately
542 $msg = wfMessage( 'mainpage' );
543 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
544 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
545 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
546 }
547
552 $this->setMwGlobals( [
553 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
554 ] );
555 $this->setUserLang( 'fr' );
556
557 // NOTE: make sure internal caching of the message text is reset appropriately.
558 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
559 $msg = wfMessage( 'mainpage' );
560 $this->assertEquals(
561 'Accueil',
562 $msg->inContentLanguage()->plain(),
563 'inContentLanguage() with ForceUIMsg override enabled'
564 );
565 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
566 $this->assertEquals(
567 'Main Page',
568 $msg->inContentLanguage()->plain(),
569 'inContentLanguage() with ForceUIMsg override enabled'
570 );
571 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
572 }
573
578 public function testInLanguageThrows() {
579 wfMessage( 'foo' )->inLanguage( 123 );
580 }
581
586 public function testSerialization() {
587 $msg = new Message( 'parentheses' );
588 $msg->rawParams( '<a>foo</a>' );
589 $msg->title( Title::newFromText( 'Testing' ) );
590 $this->assertEquals( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
591 $msg = unserialize( serialize( $msg ) );
592 $this->assertEquals( '(<a>foo</a>)', $msg->parse() );
593 $title = TestingAccessWrapper::newFromObject( $msg )->title;
594 $this->assertInstanceOf( 'Title', $title );
595 $this->assertEquals( 'Testing', $title->getFullText() );
596
597 $msg = new Message( 'mainpage' );
598 $msg->inLanguage( 'de' );
599 $this->assertEquals( 'Hauptseite', $msg->plain(), 'Sanity check' );
600 $msg = unserialize( serialize( $msg ) );
601 $this->assertEquals( 'Hauptseite', $msg->plain() );
602 }
603
608 public function testNewFromSpecifier( $value, $expectedText ) {
609 $message = Message::newFromSpecifier( $value );
610 $this->assertInstanceOf( Message::class, $message );
611 if ( $value instanceof Message ) {
612 $this->assertInstanceOf( get_class( $value ), $message );
613 $this->assertEquals( $value, $message );
614 }
615 $this->assertSame( $expectedText, $message->text() );
616 }
617
618 public function provideNewFromSpecifier() {
619 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
620 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
621 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
622
623 return [
624 'string' => [ 'mainpage', 'Main Page' ],
625 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
626 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
627 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
628 'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ],
629 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
630 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
631 ];
632 }
633}
634
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
serialize()
unserialize( $serialized)
$wgParserConf
Parser configuration.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
if( $line===false) $args
Definition cdb.php:64
Extension of Message implementing IApiMessage.
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
MediaWikiServices is the service locator for the application scope of MediaWiki.
static destroyInstance()
Destroy the singleton instance.
testExpiryParams()
FIXME: This should not need database, but Language::formatExpiry does (bug 55912) Database Message::e...
testConstructorParams( $expected, $args)
Message::__construct Message::getParams provideConstructorParams.
static provideToString()
static provideConstructorParams()
testParser( $expect, $format)
Message::text Message::parse Message::parseAsBlock Message::toString Message::transformText Message::...
testInLanguageThrows()
MWException Message::inLanguage.
testInLanguage()
Message::inLanguage.
testInContentLanguage()
Message::inContentLanguage.
static provideConstructorLanguage()
testSizeParams()
Message::sizeParam Message::sizeParams.
testRawParams()
Message::rawParam Message::rawParams.
testDurationParams()
Message::durationParam Message::durationParams.
static provideKeys()
testNumParams()
Message::numParam Message::numParams.
testNewFromSpecifier( $value, $expectedText)
Message::newFromSpecifier provideNewFromSpecifier.
testReplaceManyParams()
Message::params Message::toString Message::replaceParameters.
testToString( $key, $expect, $format='plain')
Message::toString Message::__toString provideToString.
testExists()
Message::exists.
static provideConstructor()
testWfMessageParams()
wfMessage Message::__construct
testTimeperiodParams()
Message::timeperiodParam Message::timeperiodParams.
testBitrateParams()
Message::bitrateParam Message::bitrateParams.
testConstructorLanguage( $key, $params, $languageCode)
Message::__construct Message::getLanguage provideConstructorLanguage.
provideNewFromSpecifier()
testKeys( $key, $expected, $exception=null)
Message::__construct Message::getKey Message::isMultiKey Message::getKeysToTry provideKeys.
testWfMessage()
wfMessage
testConstructor( $expectedLang, $key, $params, $language)
Message::__construct provideConstructor.
testNewFromKey()
Message::newFromKey.
testPlaintextParams( $expect, $format)
Message::plaintextParam Message::plaintextParams Message::formatPlaintext Message::toString Message::...
static provideParser()
testInContentLanguageOverride()
Message::inContentLanguage.
static providePlaintextParams()
testToStringKey()
Message::__construct Message::text Message::plain Message::escaped Message::toString.
testSerialization()
Message::serialize Message::unserialize.
testRawMessage()
RawMessage::__construct RawMessage::fetchMessage.
The Message class provides methods which fulfil two basic services:
Definition Message.php:159
Variant of the Message class.
Definition Message.php:1260
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:986
either a plain
Definition hooks.txt:1990
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params
if(!isset( $args[0])) $lang