MediaWiki REL1_27
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<dont>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<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
231 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
232 $this->assertEquals(
233 '&lt;i&lt;dont&gt;exist-evar&gt;',
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', '&lt;i-dont-exist-evar&gt;', 'escaped' ],
243 ];
244 }
245
251 public function testToString( $key, $expect, $format = 'plain' ) {
252 $msg = new Message( $key );
253 $msg->$format();
254 $this->assertEquals( $expect, $msg->toString() );
255 $this->assertEquals( $expect, $msg->__toString() );
256 }
257
261 public function testInLanguage() {
262 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
263 $this->assertEquals( 'Заглавная страница',
264 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
265
266 // NOTE: make sure internal caching of the message text is reset appropriately
267 $msg = wfMessage( 'mainpage' );
268 $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
269 $this->assertEquals(
270 'Заглавная страница',
271 $msg->inLanguage( Language::factory( 'ru' ) )->text()
272 );
273 }
274
279 public function testRawParams() {
280 $this->assertEquals(
281 '(Заглавная страница)',
282 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
283 );
284 $this->assertEquals(
285 '(Заглавная страница $1)',
286 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
287 );
288 $this->assertEquals(
289 '(Заглавная страница)',
290 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
291 );
292 $this->assertEquals(
293 '(Заглавная страница $1)',
294 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
295 );
296 }
297
302 public function testRawMessage() {
303 $msg = new RawMessage( 'example &' );
304 $this->assertEquals( 'example &', $msg->plain() );
305 $this->assertEquals( 'example &amp;', $msg->escaped() );
306 }
307
308 public function testRawHtmlInMsg() {
310 $this->setMwGlobals( 'wgRawHtml', true );
311 // We have to reset the core hook registration.
312 // to register the html hook
314 $this->setMwGlobals( 'wgParser',
315 ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
316 );
317
318 $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
319 $txt = '<span class="error">&lt;html&gt; tags cannot be' .
320 ' used outside of normal pages.</span>';
321 $this->assertSame( $txt, $msg->parse() );
322 }
323
329 public function testReplaceManyParams() {
330 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
331 // One less than above has placeholders
332 $params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ];
333 $this->assertEquals(
334 'abcdefghijka2',
335 $msg->params( $params )->plain(),
336 'Params > 9 are replaced correctly'
337 );
338
339 $msg = new RawMessage( 'Params$*' );
340 $params = [ 'ab', 'bc', 'cd' ];
341 $this->assertEquals(
342 'Params: ab, bc, cd',
343 $msg->params( $params )->text()
344 );
345 }
346
351 public function testNumParams() {
352 $lang = Language::factory( 'en' );
353 $msg = new RawMessage( '$1' );
354
355 $this->assertEquals(
356 $lang->formatNum( 123456.789 ),
357 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
358 'numParams is handled correctly'
359 );
360 }
361
366 public function testDurationParams() {
367 $lang = Language::factory( 'en' );
368 $msg = new RawMessage( '$1' );
369
370 $this->assertEquals(
371 $lang->formatDuration( 1234 ),
372 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
373 'durationParams is handled correctly'
374 );
375 }
376
383 public function testExpiryParams() {
384 $lang = Language::factory( 'en' );
385 $msg = new RawMessage( '$1' );
386
387 $this->assertEquals(
388 $lang->formatExpiry( wfTimestampNow() ),
389 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
390 'expiryParams is handled correctly'
391 );
392 }
393
398 public function testTimeperiodParams() {
399 $lang = Language::factory( 'en' );
400 $msg = new RawMessage( '$1' );
401
402 $this->assertEquals(
403 $lang->formatTimePeriod( 1234 ),
404 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
405 'timeperiodParams is handled correctly'
406 );
407 }
408
413 public function testSizeParams() {
414 $lang = Language::factory( 'en' );
415 $msg = new RawMessage( '$1' );
416
417 $this->assertEquals(
418 $lang->formatSize( 123456 ),
419 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
420 'sizeParams is handled correctly'
421 );
422 }
423
428 public function testBitrateParams() {
429 $lang = Language::factory( 'en' );
430 $msg = new RawMessage( '$1' );
431
432 $this->assertEquals(
433 $lang->formatBitrate( 123456 ),
434 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
435 'bitrateParams is handled correctly'
436 );
437 }
438
439 public static function providePlaintextParams() {
440 return [
441 [
442 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
443 'plain',
444 ],
445
446 [
447 // expect
448 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
449 // format
450 'text',
451 ],
452 [
453 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
454 'escaped',
455 ],
456
457 [
458 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
459 'parse',
460 ],
461
462 [
463 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
464 'parseAsBlock',
465 ],
466 ];
467 }
468
478 public function testPlaintextParams( $expect, $format ) {
479 $lang = Language::factory( 'en' );
480
481 $msg = new RawMessage( '$1 $2' );
482 $params = [
483 'one $2',
484 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
485 ];
486 $this->assertEquals(
487 $expect,
488 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
489 "Fail formatting for $format"
490 );
491 }
492
493 public static function provideParser() {
494 return [
495 [
496 "''&'' <x><!-- x -->",
497 'plain',
498 ],
499
500 [
501 "''&'' <x><!-- x -->",
502 'text',
503 ],
504 [
505 '<i>&amp;</i> &lt;x&gt;',
506 'parse',
507 ],
508
509 [
510 "<p><i>&amp;</i> &lt;x&gt;\n</p>",
511 'parseAsBlock',
512 ],
513 ];
514 }
515
525 public function testParser( $expect, $format ) {
526 $msg = new RawMessage( "''&'' <x><!-- x -->" );
527 $this->assertEquals(
528 $expect,
529 $msg->inLanguage( 'en' )->$format()
530 );
531 }
532
536 public function testInContentLanguage() {
537 $this->setUserLang( 'fr' );
538
539 // NOTE: make sure internal caching of the message text is reset appropriately
540 $msg = wfMessage( 'mainpage' );
541 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
542 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
543 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
544 }
545
550 $this->setMwGlobals( [
551 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ],
552 ] );
553 $this->setUserLang( 'fr' );
554
555 // NOTE: make sure internal caching of the message text is reset appropriately.
556 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
557 $msg = wfMessage( 'mainpage' );
558 $this->assertEquals(
559 'Accueil',
560 $msg->inContentLanguage()->plain(),
561 'inContentLanguage() with ForceUIMsg override enabled'
562 );
563 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
564 $this->assertEquals(
565 'Main Page',
566 $msg->inContentLanguage()->plain(),
567 'inContentLanguage() with ForceUIMsg override enabled'
568 );
569 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
570 }
571
576 public function testInLanguageThrows() {
577 wfMessage( 'foo' )->inLanguage( 123 );
578 }
579
584 public function testSerialization() {
585 $msg = new Message( 'parentheses' );
586 $msg->rawParams( '<a>foo</a>' );
587 $msg->title( Title::newFromText( 'Testing' ) );
588 $this->assertEquals( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
589 $msg = unserialize( serialize( $msg ) );
590 $this->assertEquals( '(<a>foo</a>)', $msg->parse() );
591 $title = TestingAccessWrapper::newFromObject( $msg )->title;
592 $this->assertInstanceOf( 'Title', $title );
593 $this->assertEquals( 'Testing', $title->getFullText() );
594
595 $msg = new Message( 'mainpage' );
596 $msg->inLanguage( 'de' );
597 $this->assertEquals( 'Hauptseite', $msg->plain(), 'Sanity check' );
598 $msg = unserialize( serialize( $msg ) );
599 $this->assertEquals( 'Hauptseite', $msg->plain() );
600 }
601
606 public function testNewFromSpecifier( $value, $expectedText ) {
607 $message = Message::newFromSpecifier( $value );
608 $this->assertInstanceOf( Message::class, $message );
609 $this->assertSame( $expectedText, $message->text() );
610 }
611
612 public function provideNewFromSpecifier() {
613 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
614 $messageSpecifier->expects( $this->any() )->method( 'getKey' )->willReturn( 'mainpage' );
615 $messageSpecifier->expects( $this->any() )->method( 'getParams' )->willReturn( [] );
616
617 return [
618 'string' => [ 'mainpage', 'Main Page' ],
619 'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 'You have foo (bar).' ],
620 'Message' => [ new Message( 'youhavenewmessages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ],
621 'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ],
622 'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ],
623 'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ],
624 ];
625 }
626}
627
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
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:1250
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:277
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
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing 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
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:944
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