3 class MessageTest
extends MediaWikiLangTestCase
{
5 protected function setUp() {
8 $this->setMwGlobals( array(
9 'wgLang' => Language
::factory( 'en' ),
10 'wgForceUIMsgAsContentMsg' => array(),
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $message = new Message( $key, $params, $language );
21 $this->assertEquals( $key, $message->getKey() );
22 $this->assertEquals( $params, $message->getParams() );
23 $this->assertEquals( $expectedLang, $message->getLanguage() );
26 public static function provideConstructor() {
27 $langDe = Language
::factory( 'de' );
28 $langEn = Language
::factory( 'en' );
31 array( $langDe, 'foo', array(), $langDe ),
32 array( $langDe, 'foo', array( 'bar' ), $langDe ),
33 array( $langEn, 'foo', array( 'bar' ), null )
37 public static function provideConstructorParams() {
48 array( 'foo', 'bar' ),
49 array( 'foo', 'bar' ),
53 array( array( 'baz' ) ),
56 array( 'baz', 'foo' ),
57 array( array( 'baz', 'foo' ) ),
60 array( 'baz', 'foo' ),
61 array( array( 'baz', 'foo' ), 'hhh' ),
64 array( 'baz', 'foo' ),
65 array( array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
68 array( 'baz', 'foo' ),
69 array( array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
73 array( array( 'baz' ), array( 'ahahahahha' ) ),
79 * @covers Message::__construct
80 * @covers Message::getParams
81 * @dataProvider provideConstructorParams
83 public function testConstructorParams( $expected, $args ) {
84 $msg = new Message( 'imasomething' );
86 $returned = call_user_func_array( array( $msg, 'params' ), $args );
88 $this->assertSame( $msg, $returned );
89 $this->assertEquals( $expected, $msg->getParams() );
92 public static function provideConstructorLanguage() {
94 array( 'foo', array( 'bar' ), 'en' ),
95 array( 'foo', array( 'bar' ), 'de' )
100 * @covers Message::__construct
101 * @covers Message::getLanguage
102 * @dataProvider provideConstructorLanguage
104 public function testConstructorLanguage( $key, $params, $languageCode ) {
105 $language = Language
::factory( $languageCode );
106 $message = new Message( $key, $params, $language );
108 $this->assertEquals( $language, $message->getLanguage() );
111 public static function provideKeys() {
115 'expected' => array( 'mainpage' ),
118 'key' => array( 'mainpage' ),
119 'expected' => array( 'mainpage' ),
122 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
123 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
128 'exception' => 'InvalidArgumentException',
133 'exception' => 'InvalidArgumentException',
138 'exception' => 'InvalidArgumentException',
144 * @covers Message::__construct
145 * @covers Message::getKey
146 * @covers Message::isMultiKey
147 * @covers Message::getKeysToTry
148 * @dataProvider provideKeys
150 public function testKeys( $key, $expected, $exception = null ) {
152 $this->setExpectedException( $exception );
155 $msg = new Message( $key );
156 $this->assertContains( $msg->getKey(), $expected );
157 $this->assertEquals( $expected, $msg->getKeysToTry() );
158 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
162 * @covers ::wfMessage
164 public function testWfMessage() {
165 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
166 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
170 * @covers Message::newFromKey
172 public function testNewFromKey() {
173 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'mainpage' ) );
174 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'i-dont-exist-evar' ) );
178 * @covers ::wfMessage
179 * @covers Message::__construct
181 public function testWfMessageParams() {
182 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
183 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
185 'You have foo (bar).',
186 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
189 'You have foo (bar).',
190 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
195 * @covers Message::exists
197 public function testExists() {
198 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
199 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
200 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
201 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
202 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
203 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
207 * @covers Message::__construct
208 * @covers Message::text
209 * @covers Message::plain
210 * @covers Message::escaped
211 * @covers Message::toString
213 public function testToStringKey() {
214 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
215 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
216 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->text() );
217 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
218 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
219 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
221 '<i<dont>exist-evar>',
222 wfMessage( 'i<dont>exist-evar' )->escaped()
226 public static function provideToString() {
228 array( 'mainpage', 'Main Page' ),
229 array( 'i-dont-exist-evar', '<i-dont-exist-evar>' ),
230 array( 'i-dont-exist-evar', '<i-dont-exist-evar>', 'escaped' ),
235 * @covers Message::toString
236 * @covers Message::__toString
237 * @dataProvider provideToString
239 public function testToString( $key, $expect, $format = 'plain' ) {
240 $msg = new Message( $key );
242 $this->assertEquals( $expect, $msg->toString() );
243 $this->assertEquals( $expect, $msg->__toString() );
247 * @covers Message::inLanguage
249 public function testInLanguage() {
250 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
251 $this->assertEquals( 'Заглавная страница',
252 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
254 // NOTE: make sure internal caching of the message text is reset appropriately
255 $msg = wfMessage( 'mainpage' );
256 $this->assertEquals( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
258 'Заглавная страница',
259 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
264 * @covers Message::rawParam
265 * @covers Message::rawParams
267 public function testRawParams() {
269 '(Заглавная страница)',
270 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
273 '(Заглавная страница $1)',
274 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
277 '(Заглавная страница)',
278 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
281 '(Заглавная страница $1)',
282 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
287 * @covers RawMessage::__construct
288 * @covers RawMessage::fetchMessage
290 public function testRawMessage() {
291 $msg = new RawMessage( 'example &' );
292 $this->assertEquals( 'example &', $msg->plain() );
293 $this->assertEquals( 'example &', $msg->escaped() );
297 * @covers Message::params
298 * @covers Message::toString
299 * @covers Message::replaceParameters
301 public function testReplaceManyParams() {
302 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
303 // One less than above has placeholders
304 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
307 $msg->params( $params )->plain(),
308 'Params > 9 are replaced correctly'
311 $msg = new RawMessage( 'Params$*' );
312 $params = array( 'ab', 'bc', 'cd' );
314 'Params: ab, bc, cd',
315 $msg->params( $params )->text()
320 * @covers Message::numParam
321 * @covers Message::numParams
323 public function testNumParams() {
324 $lang = Language
::factory( 'en' );
325 $msg = new RawMessage( '$1' );
328 $lang->formatNum( 123456.789 ),
329 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
330 'numParams is handled correctly'
335 * @covers Message::durationParam
336 * @covers Message::durationParams
338 public function testDurationParams() {
339 $lang = Language
::factory( 'en' );
340 $msg = new RawMessage( '$1' );
343 $lang->formatDuration( 1234 ),
344 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
345 'durationParams is handled correctly'
350 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
352 * @covers Message::expiryParam
353 * @covers Message::expiryParams
355 public function testExpiryParams() {
356 $lang = Language
::factory( 'en' );
357 $msg = new RawMessage( '$1' );
360 $lang->formatExpiry( wfTimestampNow() ),
361 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
362 'expiryParams is handled correctly'
367 * @covers Message::timeperiodParam
368 * @covers Message::timeperiodParams
370 public function testTimeperiodParams() {
371 $lang = Language
::factory( 'en' );
372 $msg = new RawMessage( '$1' );
375 $lang->formatTimePeriod( 1234 ),
376 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
377 'timeperiodParams is handled correctly'
382 * @covers Message::sizeParam
383 * @covers Message::sizeParams
385 public function testSizeParams() {
386 $lang = Language
::factory( 'en' );
387 $msg = new RawMessage( '$1' );
390 $lang->formatSize( 123456 ),
391 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
392 'sizeParams is handled correctly'
397 * @covers Message::bitrateParam
398 * @covers Message::bitrateParams
400 public function testBitrateParams() {
401 $lang = Language
::factory( 'en' );
402 $msg = new RawMessage( '$1' );
405 $lang->formatBitrate( 123456 ),
406 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
407 'bitrateParams is handled correctly'
411 public static function providePlaintextParams() {
414 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
420 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
425 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
430 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
435 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
442 * @covers Message::plaintextParam
443 * @covers Message::plaintextParams
444 * @covers Message::formatPlaintext
445 * @covers Message::toString
446 * @covers Message::parse
447 * @covers Message::parseAsBlock
448 * @dataProvider providePlaintextParams
450 public function testPlaintextParams( $expect, $format ) {
451 $lang = Language
::factory( 'en' );
453 $msg = new RawMessage( '$1 $2' );
456 '<div>foo</div> [[Bar]] {{Baz}} <',
460 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
461 "Fail formatting for $format"
465 public static function provideParser() {
468 "''&'' <x><!-- x -->",
473 "''&'' <x><!-- x -->",
477 '<i>&</i> <x>',
482 "<p><i>&</i> <x>\n</p>",
489 * @covers Message::text
490 * @covers Message::parse
491 * @covers Message::parseAsBlock
492 * @covers Message::toString
493 * @covers Message::transformText
494 * @covers Message::parseText
495 * @dataProvider provideParser
497 public function testParser( $expect, $format ) {
498 $msg = new RawMessage( "''&'' <x><!-- x -->" );
501 $msg->inLanguage( 'en' )->$format()
506 * @covers Message::inContentLanguage
508 public function testInContentLanguage() {
509 $this->setMwGlobals( 'wgLang', Language
::factory( 'fr' ) );
511 // NOTE: make sure internal caching of the message text is reset appropriately
512 $msg = wfMessage( 'mainpage' );
513 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
514 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
515 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
519 * @covers Message::inContentLanguage
521 public function testInContentLanguageOverride() {
522 $this->setMwGlobals( array(
523 'wgLang' => Language
::factory( 'fr' ),
524 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
527 // NOTE: make sure internal caching of the message text is reset appropriately.
528 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
529 $msg = wfMessage( 'mainpage' );
532 $msg->inContentLanguage()->plain(),
533 'inContentLanguage() with ForceUIMsg override enabled'
535 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
538 $msg->inContentLanguage()->plain(),
539 'inContentLanguage() with ForceUIMsg override enabled'
541 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
545 * @expectedException MWException
546 * @covers Message::inLanguage
548 public function testInLanguageThrows() {
549 wfMessage( 'foo' )->inLanguage( 123 );