Merge "Allow local interwiki links with an empty title part"
[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 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 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( 'Заглавная страница', wfMessage( 'mainpage' )->inLanguage( 'ru' )->text() );
123
124 // NOTE: make sure internal caching of the message text is reset appropriately
125 $msg = wfMessage( 'mainpage' );
126 $this->assertEquals( 'Main Page', $msg->inLanguage( Language::factory( 'en' ) )->text() );
127 $this->assertEquals(
128 'Заглавная страница',
129 $msg->inLanguage( Language::factory( 'ru' ) )->text()
130 );
131 }
132
133 /**
134 * @covers Message::__construct
135 */
136 public function testMessageParams() {
137 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto' )->text() );
138 $this->assertEquals( 'Return to $1.', wfMessage( 'returnto', array() )->text() );
139 $this->assertEquals(
140 'You have foo (bar).',
141 wfMessage( 'youhavenewmessages', 'foo', 'bar' )->text()
142 );
143 $this->assertEquals(
144 'You have foo (bar).',
145 wfMessage( 'youhavenewmessages', array( 'foo', 'bar' ) )->text()
146 );
147 }
148
149 /**
150 * @covers Message::__construct
151 * @covers Message::rawParams
152 */
153 public function testMessageParamSubstitution() {
154 $this->assertEquals(
155 '(Заглавная страница)',
156 wfMessage( 'parentheses', 'Заглавная страница' )->plain()
157 );
158 $this->assertEquals(
159 '(Заглавная страница $1)',
160 wfMessage( 'parentheses', 'Заглавная страница $1' )->plain()
161 );
162 $this->assertEquals(
163 '(Заглавная страница)',
164 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain()
165 );
166 $this->assertEquals(
167 '(Заглавная страница $1)',
168 wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain()
169 );
170 }
171
172 /**
173 * @covers Message::__construct
174 * @covers Message::params
175 */
176 public function testDeliciouslyManyParams() {
177 $msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' );
178 // One less than above has placeholders
179 $params = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' );
180 $this->assertEquals(
181 'abcdefghijka2',
182 $msg->params( $params )->plain(),
183 'Params > 9 are replaced correctly'
184 );
185 }
186
187 /**
188 * @covers Message::numParams
189 */
190 public function testMessageNumParams() {
191 $lang = Language::factory( 'en' );
192 $msg = new RawMessage( '$1' );
193
194 $this->assertEquals(
195 $lang->formatNum( 123456.789 ),
196 $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
197 'numParams is handled correctly'
198 );
199 }
200
201 /**
202 * @covers Message::durationParams
203 */
204 public function testMessageDurationParams() {
205 $lang = Language::factory( 'en' );
206 $msg = new RawMessage( '$1' );
207
208 $this->assertEquals(
209 $lang->formatDuration( 1234 ),
210 $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
211 'durationParams is handled correctly'
212 );
213 }
214
215 /**
216 * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
217 * @group Database
218 * @covers Message::expiryParams
219 */
220 public function testMessageExpiryParams() {
221 $lang = Language::factory( 'en' );
222 $msg = new RawMessage( '$1' );
223
224 $this->assertEquals(
225 $lang->formatExpiry( wfTimestampNow() ),
226 $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
227 'expiryParams is handled correctly'
228 );
229 }
230
231 /**
232 * @covers Message::timeperiodParams
233 */
234 public function testMessageTimeperiodParams() {
235 $lang = Language::factory( 'en' );
236 $msg = new RawMessage( '$1' );
237
238 $this->assertEquals(
239 $lang->formatTimePeriod( 1234 ),
240 $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
241 'timeperiodParams is handled correctly'
242 );
243 }
244
245 /**
246 * @covers Message::sizeParams
247 */
248 public function testMessageSizeParams() {
249 $lang = Language::factory( 'en' );
250 $msg = new RawMessage( '$1' );
251
252 $this->assertEquals(
253 $lang->formatSize( 123456 ),
254 $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
255 'sizeParams is handled correctly'
256 );
257 }
258
259 /**
260 * @covers Message::bitrateParams
261 */
262 public function testMessageBitrateParams() {
263 $lang = Language::factory( 'en' );
264 $msg = new RawMessage( '$1' );
265
266 $this->assertEquals(
267 $lang->formatBitrate( 123456 ),
268 $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
269 'bitrateParams is handled correctly'
270 );
271 }
272
273 /**
274 * @covers Message::inContentLanguage
275 */
276 public function testInContentLanguage() {
277 $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );
278
279 // NOTE: make sure internal caching of the message text is reset appropriately
280 $msg = wfMessage( 'mainpage' );
281 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
282 $this->assertEquals( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" );
283 $this->assertEquals( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" );
284 }
285
286 /**
287 * @covers Message::inContentLanguage
288 */
289 public function testInContentLanguageOverride() {
290 $this->setMwGlobals( array(
291 'wgLang' => Language::factory( 'fr' ),
292 'wgForceUIMsgAsContentMsg' => array( 'mainpage' ),
293 ) );
294
295 // NOTE: make sure internal caching of the message text is reset appropriately.
296 // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used.
297 $msg = wfMessage( 'mainpage' );
298 $this->assertEquals(
299 'Accueil',
300 $msg->inContentLanguage()->plain(),
301 'inContentLanguage() with ForceUIMsg override enabled'
302 );
303 $this->assertEquals( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" );
304 $this->assertEquals(
305 'Main Page',
306 $msg->inContentLanguage()->plain(),
307 'inContentLanguage() with ForceUIMsg override enabled'
308 );
309 $this->assertEquals( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" );
310 }
311
312 /**
313 * @expectedException MWException
314 * @covers Message::inLanguage
315 */
316 public function testInLanguageThrows() {
317 wfMessage( 'foo' )->inLanguage( 123 );
318 }
319 }