test: Clean up data providers that should be static
[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( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->text() );
113 $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
114 $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
115 }
116
117 /**
118 * @covers Message::inLanguage
119 */
120 public function testInLanguage() {
121 $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() );
122 $this->assertEquals( 'Заглавная страница',
123 wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
124
125 // NOTE: make sure internal caching of the message text is reset appropriately
126 $msg = wfMessage( 'mainpage' );
127 $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
128 $this->assertEquals(
129 'Заглавная страница',
130 $msg->inLanguage( Language::factory( 'ru' ) )->text()
131 );
132 }
133
134 /**
135 * @covers Message::__construct
136 */
137 public function testMessageParams() {
138 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
139 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
140 $this->assertEquals(
141 'You have foo (bar).',
142 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
143 );
144 $this->assertEquals(
145 'You have foo (bar).',
146 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
147 );
148 }
149
150 /**
151 * @covers Message::__construct
152 * @covers Message::rawParams
153 */
154 public function testMessageParamSubstitution() {
155 $this->assertEquals(
156 '(Заглавная страница)',
157 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
158 );
159 $this->assertEquals(
160 '(Заглавная страница $1)',
161 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
162 );
163 $this->assertEquals(
164 '(Заглавная страница)',
165 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
166 );
167 $this->assertEquals(
168 '(Заглавная страница $1)',
169 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
170 );
171 }
172
173 /**
174 * @covers Message::__construct
175 * @covers Message::params
176 */
177 public function testDeliciouslyManyParams() {
178 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
179 // One less than above has placeholders
180 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
181 $this->assertEquals(
182 'abcdefghijka2',
183 $msg->params( $params )->plain(),
184 'Params > 9 are replaced correctly'
185 );
186 }
187
188 /**
189 * @covers Message::numParams
190 */
191 public function testMessageNumParams() {
192 $lang = Language::factory( 'en' );
193 $msg = new RawMessage( '$1' );
194
195 $this->assertEquals(
196 $lang->formatNum( 123456.789 ),
197 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
198 'numParams is handled correctly'
199 );
200 }
201
202 /**
203 * @covers Message::durationParams
204 */
205 public function testMessageDurationParams() {
206 $lang = Language::factory( 'en' );
207 $msg = new RawMessage( '$1' );
208
209 $this->assertEquals(
210 $lang->formatDuration( 1234 ),
211 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
212 'durationParams is handled correctly'
213 );
214 }
215
216 /**
217 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
218 * @group Database
219 * @covers Message::expiryParams
220 */
221 public function testMessageExpiryParams() {
222 $lang = Language::factory( 'en' );
223 $msg = new RawMessage( '$1' );
224
225 $this->assertEquals(
226 $lang->formatExpiry( wfTimestampNow() ),
227 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
228 'expiryParams is handled correctly'
229 );
230 }
231
232 /**
233 * @covers Message::timeperiodParams
234 */
235 public function testMessageTimeperiodParams() {
236 $lang = Language::factory( 'en' );
237 $msg = new RawMessage( '$1' );
238
239 $this->assertEquals(
240 $lang->formatTimePeriod( 1234 ),
241 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
242 'timeperiodParams is handled correctly'
243 );
244 }
245
246 /**
247 * @covers Message::sizeParams
248 */
249 public function testMessageSizeParams() {
250 $lang = Language::factory( 'en' );
251 $msg = new RawMessage( '$1' );
252
253 $this->assertEquals(
254 $lang->formatSize( 123456 ),
255 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
256 'sizeParams is handled correctly'
257 );
258 }
259
260 /**
261 * @covers Message::bitrateParams
262 */
263 public function testMessageBitrateParams() {
264 $lang = Language::factory( 'en' );
265 $msg = new RawMessage( '$1' );
266
267 $this->assertEquals(
268 $lang->formatBitrate( 123456 ),
269 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
270 'bitrateParams is handled correctly'
271 );
272 }
273
274 /**
275 * @covers Message::inContentLanguage
276 */
277 public function testInContentLanguage() {
278 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
279
280 // NOTE: make sure internal caching of the message text is reset appropriately
281 $msg = wfMessage( 'mainpage' );
282 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
283 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
284 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
285 }
286
287 /**
288 * @covers Message::inContentLanguage
289 */
290 public function testInContentLanguageOverride() {
291 $this->setMwGlobals( array(
292 'wgLang' => Language::factory( 'fr' ),
293 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
294 ) );
295
296 // NOTE: make sure internal caching of the message text is reset appropriately.
297 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
298 $msg = wfMessage( 'mainpage' );
299 $this->assertEquals(
300 'Accueil',
301 $msg->inContentLanguage()->plain(),
302 'inContentLanguage() with ForceUIMsg override enabled'
303 );
304 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
305 $this->assertEquals(
306 'Main Page',
307 $msg->inContentLanguage()->plain(),
308 'inContentLanguage() with ForceUIMsg override enabled'
309 );
310 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
311 }
312
313 /**
314 * @expectedException MWException
315 * @covers Message::inLanguage
316 */
317 public function testInLanguageThrows() {
318 wfMessage( 'foo' )->inLanguage( 123 );
319 }
320
321 public function keyProvider() {
322 return array(
323 'string' => array(
324 'key' => 'mainpage',
325 'expected' => array( 'mainpage' ),
326 ),
327 'single' => array(
328 'key' => array( 'mainpage' ),
329 'expected' => array( 'mainpage' ),
330 ),
331 'multi' => array(
332 'key' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
333 'expected' => array( 'mainpage-foo', 'mainpage-bar', 'mainpage' ),
334 ),
335 'empty' => array(
336 'key' => array(),
337 'expected' => null,
338 'exception' => 'InvalidArgumentException',
339 ),
340 'null' => array(
341 'key' => null,
342 'expected' => null,
343 'exception' => 'InvalidArgumentException',
344 ),
345 'bad type' => array(
346 'key' => 17,
347 'expected' => null,
348 'exception' => 'InvalidArgumentException',
349 ),
350 );
351 }
352
353 /**
354 * @dataProvider keyProvider()
355 *
356 * @covers Message::getKey
357 */
358 public function testGetKey( $key, $expected, $exception = null ) {
359 if ( $exception ) {
360 $this->setExpectedException( $exception );
361 }
362
363 $msg = new Message( $key );
364 $this->assertEquals( $expected, $msg->getKeysToTry() );
365 $this->assertEquals( count( $expected ) > 1, $msg->isMultiKey() );
366 $this->assertContains( $msg->getKey(), $expected );
367 }
368 }