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() );
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, array(), $language );
32 $this->assertEquals( $key, $message->getKey() );
33 $this->assertEquals( $params, $message->getParams() );
34 $this->assertEquals( $expectedLang, $message->getLanguage() );
37 public static function provideConstructor() {
38 $langDe = Language
::factory( 'de' );
39 $langEn = Language
::factory( 'en' );
42 array( $langDe, 'foo', array(), $langDe ),
43 array( $langDe, 'foo', array( 'bar' ), $langDe ),
44 array( $langEn, 'foo', array( 'bar' ), null )
48 public static function provideConstructorParams() {
59 array( 'foo', 'bar' ),
60 array( 'foo', 'bar' ),
64 array( array( 'baz' ) ),
67 array( 'baz', 'foo' ),
68 array( array( 'baz', 'foo' ) ),
71 array( 'baz', 'foo' ),
72 array( array( 'baz', 'foo' ), 'hhh' ),
75 array( 'baz', 'foo' ),
76 array( array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
79 array( 'baz', 'foo' ),
80 array( array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
84 array( array( 'baz' ), array( 'ahahahahha' ) ),
90 * @covers Message::__construct
91 * @covers Message::getParams
92 * @dataProvider provideConstructorParams
94 public function testConstructorParams( $expected, $args ) {
95 $msg = new Message( 'imasomething' );
97 $returned = call_user_func_array( array( $msg, 'params' ), $args );
99 $this->assertSame( $msg, $returned );
100 $this->assertEquals( $expected, $msg->getParams() );
103 public static function provideConstructorLanguage() {
105 array( 'foo', array( 'bar' ), 'en' ),
106 array( 'foo', array( 'bar' ), 'de' )
111 * @covers Message::__construct
112 * @covers Message::getLanguage
113 * @dataProvider provideConstructorLanguage
115 public function testConstructorLanguage( $key, $params, $languageCode ) {
116 $language = Language
::factory( $languageCode );
117 $message = new Message( $key, $params, $language );
119 $this->assertEquals( $language, $message->getLanguage() );
122 public static function provideKeys() {
126 'expected' => array( 'mainpage' ),
129 'key' => array( 'mainpage' ),
130 'expected' => array( 'mainpage' ),
133 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
134 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
139 'exception' => 'InvalidArgumentException',
144 'exception' => 'InvalidArgumentException',
149 'exception' => 'InvalidArgumentException',
155 * @covers Message::__construct
156 * @covers Message::getKey
157 * @covers Message::isMultiKey
158 * @covers Message::getKeysToTry
159 * @dataProvider provideKeys
161 public function testKeys( $key, $expected, $exception = null ) {
163 $this->setExpectedException( $exception );
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() );
173 * @covers ::wfMessage
175 public function testWfMessage() {
176 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
177 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
181 * @covers Message::newFromKey
183 public function testNewFromKey() {
184 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'mainpage' ) );
185 $this->assertInstanceOf( 'Message', Message
::newFromKey( 'i-dont-exist-evar' ) );
189 * @covers ::wfMessage
190 * @covers Message::__construct
192 public function testWfMessageParams() {
193 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
194 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
196 'You have foo (bar).',
197 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
200 'You have foo (bar).',
201 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
206 * @covers Message::exists
208 public function testExists() {
209 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
210 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->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( array() )->exists() );
214 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
218 * @covers Message::__construct
219 * @covers Message::text
220 * @covers Message::plain
221 * @covers Message::escaped
222 * @covers Message::toString
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<dont>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<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
230 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
232 '<i<dont>exist-evar>',
233 wfMessage( 'i<dont>exist-evar' )->escaped()
237 public static function provideToString() {
239 array( 'mainpage', 'Main Page' ),
240 array( 'i-dont-exist-evar', '<i-dont-exist-evar>' ),
241 array( 'i-dont-exist-evar', '<i-dont-exist-evar>', 'escaped' ),
246 * @covers Message::toString
247 * @covers Message::__toString
248 * @dataProvider provideToString
250 public function testToString( $key, $expect, $format = 'plain' ) {
251 $msg = new Message( $key );
253 $this->assertEquals( $expect, $msg->toString() );
254 $this->assertEquals( $expect, $msg->__toString() );
258 * @covers Message::inLanguage
260 public function testInLanguage() {
261 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
262 $this->assertEquals( 'Заглавная страница',
263 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
265 // NOTE: make sure internal caching of the message text is reset appropriately
266 $msg = wfMessage( 'mainpage' );
267 $this->assertEquals( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
269 'Заглавная страница',
270 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
275 * @covers Message::rawParam
276 * @covers Message::rawParams
278 public function testRawParams() {
280 '(Заглавная страница)',
281 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
284 '(Заглавная страница $1)',
285 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
288 '(Заглавная страница)',
289 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
292 '(Заглавная страница $1)',
293 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
298 * @covers RawMessage::__construct
299 * @covers RawMessage::fetchMessage
301 public function testRawMessage() {
302 $msg = new RawMessage( 'example &' );
303 $this->assertEquals( 'example &', $msg->plain() );
304 $this->assertEquals( 'example &', $msg->escaped() );
308 * @covers Message::params
309 * @covers Message::toString
310 * @covers Message::replaceParameters
312 public function testReplaceManyParams() {
313 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
314 // One less than above has placeholders
315 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
318 $msg->params( $params )->plain(),
319 'Params > 9 are replaced correctly'
322 $msg = new RawMessage( 'Params$*' );
323 $params = array( 'ab', 'bc', 'cd' );
325 'Params: ab, bc, cd',
326 $msg->params( $params )->text()
331 * @covers Message::numParam
332 * @covers Message::numParams
334 public function testNumParams() {
335 $lang = Language
::factory( 'en' );
336 $msg = new RawMessage( '$1' );
339 $lang->formatNum( 123456.789 ),
340 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
341 'numParams is handled correctly'
346 * @covers Message::durationParam
347 * @covers Message::durationParams
349 public function testDurationParams() {
350 $lang = Language
::factory( 'en' );
351 $msg = new RawMessage( '$1' );
354 $lang->formatDuration( 1234 ),
355 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
356 'durationParams is handled correctly'
361 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
363 * @covers Message::expiryParam
364 * @covers Message::expiryParams
366 public function testExpiryParams() {
367 $lang = Language
::factory( 'en' );
368 $msg = new RawMessage( '$1' );
371 $lang->formatExpiry( wfTimestampNow() ),
372 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
373 'expiryParams is handled correctly'
378 * @covers Message::timeperiodParam
379 * @covers Message::timeperiodParams
381 public function testTimeperiodParams() {
382 $lang = Language
::factory( 'en' );
383 $msg = new RawMessage( '$1' );
386 $lang->formatTimePeriod( 1234 ),
387 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
388 'timeperiodParams is handled correctly'
393 * @covers Message::sizeParam
394 * @covers Message::sizeParams
396 public function testSizeParams() {
397 $lang = Language
::factory( 'en' );
398 $msg = new RawMessage( '$1' );
401 $lang->formatSize( 123456 ),
402 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
403 'sizeParams is handled correctly'
408 * @covers Message::bitrateParam
409 * @covers Message::bitrateParams
411 public function testBitrateParams() {
412 $lang = Language
::factory( 'en' );
413 $msg = new RawMessage( '$1' );
416 $lang->formatBitrate( 123456 ),
417 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
418 'bitrateParams is handled correctly'
422 public static function providePlaintextParams() {
425 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
431 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
436 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
441 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
446 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
453 * @covers Message::plaintextParam
454 * @covers Message::plaintextParams
455 * @covers Message::formatPlaintext
456 * @covers Message::toString
457 * @covers Message::parse
458 * @covers Message::parseAsBlock
459 * @dataProvider providePlaintextParams
461 public function testPlaintextParams( $expect, $format ) {
462 $lang = Language
::factory( 'en' );
464 $msg = new RawMessage( '$1 $2' );
467 '<div>foo</div> [[Bar]] {{Baz}} <',
471 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
472 "Fail formatting for $format"
476 public static function provideParser() {
479 "''&'' <x><!-- x -->",
484 "''&'' <x><!-- x -->",
488 '<i>&</i> <x>',
493 "<p><i>&</i> <x>\n</p>",
500 * @covers Message::text
501 * @covers Message::parse
502 * @covers Message::parseAsBlock
503 * @covers Message::toString
504 * @covers Message::transformText
505 * @covers Message::parseText
506 * @dataProvider provideParser
508 public function testParser( $expect, $format ) {
509 $msg = new RawMessage( "''&'' <x><!-- x -->" );
512 $msg->inLanguage( 'en' )->$format()
517 * @covers Message::inContentLanguage
519 public function testInContentLanguage() {
520 $this->setMwGlobals( 'wgLang', Language
::factory( 'fr' ) );
522 // NOTE: make sure internal caching of the message text is reset appropriately
523 $msg = wfMessage( 'mainpage' );
524 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
525 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
526 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
530 * @covers Message::inContentLanguage
532 public function testInContentLanguageOverride() {
533 $this->setMwGlobals( array(
534 'wgLang' => Language
::factory( 'fr' ),
535 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
538 // NOTE: make sure internal caching of the message text is reset appropriately.
539 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
540 $msg = wfMessage( 'mainpage' );
543 $msg->inContentLanguage()->plain(),
544 'inContentLanguage() with ForceUIMsg override enabled'
546 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
549 $msg->inContentLanguage()->plain(),
550 'inContentLanguage() with ForceUIMsg override enabled'
552 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
556 * @expectedException MWException
557 * @covers Message::inLanguage
559 public function testInLanguageThrows() {
560 wfMessage( 'foo' )->inLanguage( 123 );
564 * @covers Message::serialize
565 * @covers Message::unserialize
567 public function testSerialization() {
568 $msg = new Message( 'parentheses' );
569 $msg->rawParams( '<a>foo</a>' );
570 $msg->title( Title
::newFromText( 'Testing' ) );
571 $this->assertEquals( '(<a>foo</a>)', $msg->parse(), 'Sanity check' );
572 $msg = unserialize( serialize( $msg ) );
573 $this->assertEquals( '(<a>foo</a>)', $msg->parse() );
574 $title = TestingAccessWrapper
::newFromObject( $msg )->title
;
575 $this->assertInstanceOf( 'Title', $title );
576 $this->assertEquals( 'Testing', $title->getFullText() );
578 $msg = new Message( 'mainpage' );
579 $msg->inLanguage( 'de' );
580 $this->assertEquals( 'Hauptseite', $msg->plain(), 'Sanity check' );
581 $msg = unserialize( serialize( $msg ) );
582 $this->assertEquals( 'Hauptseite', $msg->plain() );