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 $reflection = new ReflectionClass( 'Message' );
21 $keyProperty = $reflection->getProperty( 'key' );
22 $keyProperty->setAccessible( true );
24 $paramsProperty = $reflection->getProperty( 'parameters' );
25 $paramsProperty->setAccessible( true );
27 $langProperty = $reflection->getProperty( 'language' );
28 $langProperty->setAccessible( true );
30 $message = new Message( $key, $params, $language );
32 $this->assertEquals( $key, $keyProperty->getValue( $message ) );
33 $this->assertEquals( $params, $paramsProperty->getValue( $message ) );
34 $this->assertEquals( $expectedLang, $langProperty->getValue( $message ) );
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 provideTestParams() {
51 array( array( 'foo' ), 'foo' ),
52 array( array( 'foo', 'bar' ), 'foo', 'bar' ),
53 array( array( 'baz' ), array( 'baz' ) ),
54 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ) ),
55 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh' ),
56 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), 'hhh', array( 'ahahahahha' ) ),
57 array( array( 'baz', 'foo' ), array( 'baz', 'foo' ), array( 'ahahahahha' ) ),
58 array( array( 'baz' ), array( 'baz' ), array( 'ahahahahha' ) ),
62 public function getLanguageProvider() {
64 array( 'foo', array( 'bar' ), 'en' ),
65 array( 'foo', array( 'bar' ), 'de' )
70 * @covers Message::getLanguage
71 * @dataProvider getLanguageProvider
73 public function testGetLanguageCode( $key, $params, $languageCode ) {
74 $language = Language
::factory( $languageCode );
75 $message = new Message( $key, $params, $language );
77 $this->assertEquals( $language, $message->getLanguage() );
81 * @covers Message::params
82 * @dataProvider provideTestParams
84 public function testParams( $expected ) {
85 $msg = new Message( 'imasomething' );
87 $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
89 $this->assertSame( $msg, $returned );
90 $this->assertEquals( $expected, $msg->getParams() );
94 * @covers Message::exists
96 public function testExists() {
97 $this->assertTrue( wfMessage( 'mainpage' )->exists() );
98 $this->assertTrue( wfMessage( 'mainpage' )->params( array() )->exists() );
99 $this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() );
100 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() );
101 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( array() )->exists() );
102 $this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() );
106 * @covers Message::__construct
108 public function testKey() {
109 $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
110 $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
111 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
112 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
113 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->text() );
114 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
115 $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
116 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->escaped() );
118 '<i<dont>exist-evar>',
119 wfMessage( 'i<dont>exist-evar' )->escaped()
124 * @covers Message::inLanguage
126 public function testInLanguage() {
127 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
128 $this->assertEquals( 'Заглавная страница',
129 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
131 // NOTE: make sure internal caching of the message text is reset appropriately
132 $msg = wfMessage( 'mainpage' );
133 $this->assertEquals( 'Main Page', $msg->inLanguage( Language
::factory( 'en' ) )->text() );
135 'Заглавная страница',
136 $msg->inLanguage( Language
::factory( 'ru' ) )->text()
141 * @covers Message::__construct
143 public function testMessageParams() {
144 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
145 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
147 'You have foo (bar).',
148 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
151 'You have foo (bar).',
152 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
157 * @covers Message::__construct
158 * @covers Message::rawParams
160 public function testMessageParamSubstitution() {
162 '(Заглавная страница)',
163 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
166 '(Заглавная страница $1)',
167 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
170 '(Заглавная страница)',
171 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
174 '(Заглавная страница $1)',
175 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
180 * @covers Message::__construct
181 * @covers Message::params
183 public function testDeliciouslyManyParams() {
184 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
185 // One less than above has placeholders
186 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
189 $msg->params( $params )->plain(),
190 'Params > 9 are replaced correctly'
195 * @covers Message::numParams
197 public function testMessageNumParams() {
198 $lang = Language
::factory( 'en' );
199 $msg = new RawMessage( '$1' );
202 $lang->formatNum( 123456.789 ),
203 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
204 'numParams is handled correctly'
209 * @covers Message::durationParams
211 public function testMessageDurationParams() {
212 $lang = Language
::factory( 'en' );
213 $msg = new RawMessage( '$1' );
216 $lang->formatDuration( 1234 ),
217 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
218 'durationParams is handled correctly'
223 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
225 * @covers Message::expiryParams
227 public function testMessageExpiryParams() {
228 $lang = Language
::factory( 'en' );
229 $msg = new RawMessage( '$1' );
232 $lang->formatExpiry( wfTimestampNow() ),
233 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
234 'expiryParams is handled correctly'
239 * @covers Message::timeperiodParams
241 public function testMessageTimeperiodParams() {
242 $lang = Language
::factory( 'en' );
243 $msg = new RawMessage( '$1' );
246 $lang->formatTimePeriod( 1234 ),
247 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
248 'timeperiodParams is handled correctly'
253 * @covers Message::sizeParams
255 public function testMessageSizeParams() {
256 $lang = Language
::factory( 'en' );
257 $msg = new RawMessage( '$1' );
260 $lang->formatSize( 123456 ),
261 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
262 'sizeParams is handled correctly'
267 * @covers Message::bitrateParams
269 public function testMessageBitrateParams() {
270 $lang = Language
::factory( 'en' );
271 $msg = new RawMessage( '$1' );
274 $lang->formatBitrate( 123456 ),
275 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
276 'bitrateParams is handled correctly'
280 public function messagePlaintextParamsProvider() {
283 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
289 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
294 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
299 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
304 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
311 * @dataProvider messagePlaintextParamsProvider
312 * @covers Message::plaintextParams
314 public function testMessagePlaintextParams( $expect, $format ) {
315 $lang = Language
::factory( 'en' );
317 $msg = new RawMessage( '$1 $2' );
320 '<div>foo</div> [[Bar]] {{Baz}} <',
324 $msg->inLanguage( $lang )->plaintextParams( $params )->$format(),
325 "Fail formatting for $format"
330 * @covers Message::inContentLanguage
332 public function testInContentLanguage() {
333 $this->setMwGlobals( 'wgLang', Language
::factory( 'fr' ) );
335 // NOTE: make sure internal caching of the message text is reset appropriately
336 $msg = wfMessage( 'mainpage' );
337 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
338 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
339 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
343 * @covers Message::inContentLanguage
345 public function testInContentLanguageOverride() {
346 $this->setMwGlobals( array(
347 'wgLang' => Language
::factory( 'fr' ),
348 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
351 // NOTE: make sure internal caching of the message text is reset appropriately.
352 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
353 $msg = wfMessage( 'mainpage' );
356 $msg->inContentLanguage()->plain(),
357 'inContentLanguage() with ForceUIMsg override enabled'
359 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
362 $msg->inContentLanguage()->plain(),
363 'inContentLanguage() with ForceUIMsg override enabled'
365 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
369 * @expectedException MWException
370 * @covers Message::inLanguage
372 public function testInLanguageThrows() {
373 wfMessage( 'foo' )->inLanguage( 123 );
376 public function keyProvider() {
380 'expected' => array( 'mainpage' ),
383 'key' => array( 'mainpage' ),
384 'expected' => array( 'mainpage' ),
387 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
388 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
393 'exception' => 'InvalidArgumentException',
398 'exception' => 'InvalidArgumentException',
403 'exception' => 'InvalidArgumentException',
409 * @dataProvider keyProvider()
411 * @covers Message::getKey
413 public function testGetKey( $key, $expected, $exception = null ) {
415 $this->setExpectedException( $exception );
418 $msg = new Message( $key );
419 $this->assertEquals( $expected, $msg->getKeysToTry() );
420 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
421 $this->assertContains( $msg->getKey(), $expected );