Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / MessageTest.php
blob4c5424c5ec3b0cb8bc73370a4d06714620aef1a9
1 <?php
3 class MessageTest extends MediaWikiLangTestCase {
5 protected function setUp() {
6 parent::setUp();
8 $this->setMwGlobals( array(
9 'wgLang' => Language::factory( 'en' ),
10 'wgForceUIMsgAsContentMsg' => array(),
11 ) );
14 /**
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' );
41 return array(
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() {
49 return array(
50 array( array() ),
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() {
63 return array(
64 array( 'foo', array( 'bar' ), 'en' ),
65 array( 'foo', array( 'bar' ), 'de' )
69 /**
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() );
80 /**
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() );
93 /**
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( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
117 $this->assertEquals(
118 '&lt;i&lt;dont&gt;exist-evar&gt;',
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() );
134 $this->assertEquals(
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() );
146 $this->assertEquals(
147 'You have foo (bar).',
148 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
150 $this->assertEquals(
151 'You have foo (bar).',
152 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
157 * @covers Message::__construct
158 * @covers Message::rawParams
160 public function testMessageParamSubstitution() {
161 $this->assertEquals(
162 '(Заглавная страница)',
163 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
165 $this->assertEquals(
166 '(Заглавная страница $1)',
167 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
169 $this->assertEquals(
170 '(Заглавная страница)',
171 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
173 $this->assertEquals(
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' );
187 $this->assertEquals(
188 'abcdefghijka2',
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' );
201 $this->assertEquals(
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' );
215 $this->assertEquals(
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)
224 * @group Database
225 * @covers Message::expiryParams
227 public function testMessageExpiryParams() {
228 $lang = Language::factory( 'en' );
229 $msg = new RawMessage( '$1' );
231 $this->assertEquals(
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' );
245 $this->assertEquals(
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' );
259 $this->assertEquals(
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' );
273 $this->assertEquals(
274 $lang->formatBitrate( 123456 ),
275 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
276 'bitrateParams is handled correctly'
280 public function messagePlaintextParamsProvider() {
281 return array(
282 array(
283 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
284 'plain',
287 array(
288 // expect
289 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
290 // format
291 'text',
293 array(
294 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
295 'escaped',
298 array(
299 'one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;',
300 'parse',
303 array(
304 "<p>one $2 &lt;div&gt;foo&lt;/div&gt; [[Bar]] {{Baz}} &amp;lt;\n</p>",
305 'parseAsBlock',
311 * @dataProvider messagePlaintextParamsProvider
312 * @covers Message::plaintextParams
314 public function testMessagePlaintextParams( $expect, $format ) {
315 $lang = Language::factory( 'en' );
317 $msg = new RawMessage( '$1 $2' );
318 $params = array(
319 'one $2',
320 '<div>foo</div> [[Bar]] {{Baz}} &lt;',
322 $this->assertEquals(
323 $expect,
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' ),
349 ) );
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' );
354 $this->assertEquals(
355 'Accueil',
356 $msg->inContentLanguage()->plain(),
357 'inContentLanguage() with ForceUIMsg override enabled'
359 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
360 $this->assertEquals(
361 'Main Page',
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() {
377 return array(
378 'string' => array(
379 'key' => 'mainpage',
380 'expected' => array( 'mainpage' ),
382 'single' => array(
383 'key' => array( 'mainpage' ),
384 'expected' => array( 'mainpage' ),
386 'multi' => array(
387 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
388 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
390 'empty' => array(
391 'key' => array(),
392 'expected' => null,
393 'exception' => 'InvalidArgumentException',
395 'null' => array(
396 'key' => null,
397 'expected' => null,
398 'exception' => 'InvalidArgumentException',
400 'bad type' => array(
401 'key' => 17,
402 'expected' => null,
403 'exception' => 'InvalidArgumentException',
409 * @dataProvider keyProvider()
411 * @covers Message::getKey
413 public function testGetKey( $key, $expected, $exception = null ) {
414 if ( $exception ) {
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 );