Merge "Add cURL constants test"
[lhc/web/wiklou.git] / tests / phpunit / includes / MessageTest.php
1 <?php
2
3 class MessageTest extends MediaWikiLangTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7
8 $this->setMwGlobals( array(
9 'wgLang' => Language::factory( 'en' ),
10 'wgForceUIMsgAsContentMsg' => array(),
11 ) );
12 }
13
14 /**
15 * @covers Message::__construct
16 * @dataProvider provideConstructor
17 */
18 public function testConstructor( $expectedLang, $key, $params, $language ) {
19 $reflection = new ReflectionClass( 'Message' );
20
21 $keyProperty = $reflection->getProperty( 'key' );
22 $keyProperty->setAccessible( true );
23
24 $paramsProperty = $reflection->getProperty( 'parameters' );
25 $paramsProperty->setAccessible( true );
26
27 $langProperty = $reflection->getProperty( 'language' );
28 $langProperty->setAccessible( true );
29
30 $message = new Message( $key, $params, $language );
31
32 $this->assertEquals( $key, $keyProperty->getValue( $message ) );
33 $this->assertEquals( $params, $paramsProperty->getValue( $message ) );
34 $this->assertEquals( $expectedLang, $langProperty->getValue( $message ) );
35 }
36
37 public static function provideConstructor() {
38 $langDe = Language::factory( 'de' );
39 $langEn = Language::factory( 'en' );
40
41 return array(
42 array( $langDe, 'foo', array(), $langDe ),
43 array( $langDe, 'foo', array( 'bar' ), $langDe ),
44 array( $langEn, 'foo', array( 'bar' ), null )
45 );
46 }
47
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' ) ),
59 );
60 }
61
62 public function getLanguageProvider() {
63 return array(
64 array( 'foo', array( 'bar' ), 'en' ),
65 array( 'foo', array( 'bar' ), 'de' )
66 );
67 }
68
69 /**
70 * @covers Message::getLanguage
71 * @dataProvider getLanguageProvider
72 */
73 public function testGetLanguageCode( $key, $params, $languageCode ) {
74 $language = Language::factory( $languageCode );
75 $message = new Message( $key, $params, $language );
76
77 $this->assertEquals( $language, $message->getLanguage() );
78 }
79
80 /**
81 * @covers Message::params
82 * @dataProvider provideTestParams
83 */
84 public function testParams( $expected ) {
85 $msg = new Message( 'imasomething' );
86
87 $returned = call_user_func_array( array( $msg, 'params' ), array_slice( func_get_args(), 1 ) );
88
89 $this->assertSame( $msg, $returned );
90 $this->assertEquals( $expected, $msg->getParams() );
91 }
92
93 /**
94 * @covers Message::exists
95 */
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() );
103 }
104
105 /**
106 * @covers Message::__construct
107 */
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()
120 );
121 }
122
123 /**
124 * @covers Message::inLanguage
125 */
126 public function testInLanguage() {
127 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
128 $this->assertEquals( 'Заглавная страница',
129 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
130
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()
137 );
138 }
139
140 /**
141 * @covers Message::__construct
142 */
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()
149 );
150 $this->assertEquals(
151 'You have foo (bar).',
152 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
153 );
154 }
155
156 /**
157 * @covers Message::__construct
158 * @covers Message::rawParams
159 */
160 public function testMessageParamSubstitution() {
161 $this->assertEquals(
162 '(Заглавная страница)',
163 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
164 );
165 $this->assertEquals(
166 '(Заглавная страница $1)',
167 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
168 );
169 $this->assertEquals(
170 '(Заглавная страница)',
171 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
172 );
173 $this->assertEquals(
174 '(Заглавная страница $1)',
175 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
176 );
177 }
178
179 /**
180 * @covers Message::__construct
181 * @covers Message::params
182 */
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'
191 );
192 }
193
194 /**
195 * @covers Message::numParams
196 */
197 public function testMessageNumParams() {
198 $lang = Language::factory( 'en' );
199 $msg = new RawMessage( '$1' );
200
201 $this->assertEquals(
202 $lang->formatNum( 123456.789 ),
203 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
204 'numParams is handled correctly'
205 );
206 }
207
208 /**
209 * @covers Message::durationParams
210 */
211 public function testMessageDurationParams() {
212 $lang = Language::factory( 'en' );
213 $msg = new RawMessage( '$1' );
214
215 $this->assertEquals(
216 $lang->formatDuration( 1234 ),
217 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
218 'durationParams is handled correctly'
219 );
220 }
221
222 /**
223 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
224 * @group Database
225 * @covers Message::expiryParams
226 */
227 public function testMessageExpiryParams() {
228 $lang = Language::factory( 'en' );
229 $msg = new RawMessage( '$1' );
230
231 $this->assertEquals(
232 $lang->formatExpiry( wfTimestampNow() ),
233 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
234 'expiryParams is handled correctly'
235 );
236 }
237
238 /**
239 * @covers Message::timeperiodParams
240 */
241 public function testMessageTimeperiodParams() {
242 $lang = Language::factory( 'en' );
243 $msg = new RawMessage( '$1' );
244
245 $this->assertEquals(
246 $lang->formatTimePeriod( 1234 ),
247 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
248 'timeperiodParams is handled correctly'
249 );
250 }
251
252 /**
253 * @covers Message::sizeParams
254 */
255 public function testMessageSizeParams() {
256 $lang = Language::factory( 'en' );
257 $msg = new RawMessage( '$1' );
258
259 $this->assertEquals(
260 $lang->formatSize( 123456 ),
261 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
262 'sizeParams is handled correctly'
263 );
264 }
265
266 /**
267 * @covers Message::bitrateParams
268 */
269 public function testMessageBitrateParams() {
270 $lang = Language::factory( 'en' );
271 $msg = new RawMessage( '$1' );
272
273 $this->assertEquals(
274 $lang->formatBitrate( 123456 ),
275 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
276 'bitrateParams is handled correctly'
277 );
278 }
279
280 /**
281 * @covers Message::inContentLanguage
282 */
283 public function testInContentLanguage() {
284 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
285
286 // NOTE: make sure internal caching of the message text is reset appropriately
287 $msg = wfMessage( 'mainpage' );
288 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
289 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
290 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
291 }
292
293 /**
294 * @covers Message::inContentLanguage
295 */
296 public function testInContentLanguageOverride() {
297 $this->setMwGlobals( array(
298 'wgLang' => Language::factory( 'fr' ),
299 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
300 ) );
301
302 // NOTE: make sure internal caching of the message text is reset appropriately.
303 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
304 $msg = wfMessage( 'mainpage' );
305 $this->assertEquals(
306 'Accueil',
307 $msg->inContentLanguage()->plain(),
308 'inContentLanguage() with ForceUIMsg override enabled'
309 );
310 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
311 $this->assertEquals(
312 'Main Page',
313 $msg->inContentLanguage()->plain(),
314 'inContentLanguage() with ForceUIMsg override enabled'
315 );
316 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
317 }
318
319 /**
320 * @expectedException MWException
321 * @covers Message::inLanguage
322 */
323 public function testInLanguageThrows() {
324 wfMessage( 'foo' )->inLanguage( 123 );
325 }
326
327 public function keyProvider() {
328 return array(
329 'string' => array(
330 'key' => 'mainpage',
331 'expected' => array( 'mainpage' ),
332 ),
333 'single' => array(
334 'key' => array( 'mainpage' ),
335 'expected' => array( 'mainpage' ),
336 ),
337 'multi' => array(
338 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
339 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
340 ),
341 'empty' => array(
342 'key' => array(),
343 'expected' => null,
344 'exception' => 'InvalidArgumentException',
345 ),
346 'null' => array(
347 'key' => null,
348 'expected' => null,
349 'exception' => 'InvalidArgumentException',
350 ),
351 'bad type' => array(
352 'key' => 17,
353 'expected' => null,
354 'exception' => 'InvalidArgumentException',
355 ),
356 );
357 }
358
359 /**
360 * @dataProvider keyProvider()
361 *
362 * @covers Message::getKey
363 */
364 public function testGetKey( $key, $expected, $exception = null ) {
365 if ( $exception ) {
366 $this->setExpectedException( $exception );
367 }
368
369 $msg = new Message( $key );
370 $this->assertEquals( $expected, $msg->getKeysToTry() );
371 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
372 $this->assertContains( $msg->getKey(), $expected );
373 }
374 }