Merge "Avoid double-rendering on late exceptions"
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlTest.php
1 <?php
2
3 /**
4 * @group Xml
5 */
6 class XmlTest extends MediaWikiTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 $langObj = Language::factory( 'en' );
12 $langObj->setNamespaces( [
13 -2 => 'Media',
14 -1 => 'Special',
15 0 => '',
16 1 => 'Talk',
17 2 => 'User',
18 3 => 'User_talk',
19 4 => 'MyWiki',
20 5 => 'MyWiki_Talk',
21 6 => 'File',
22 7 => 'File_talk',
23 8 => 'MediaWiki',
24 9 => 'MediaWiki_talk',
25 10 => 'Template',
26 11 => 'Template_talk',
27 100 => 'Custom',
28 101 => 'Custom_talk',
29 ] );
30
31 $this->setMwGlobals( [
32 'wgLang' => $langObj,
33 'wgWellFormedXml' => true,
34 'wgUseMediaWikiUIEverywhere' => false,
35 ] );
36 }
37
38 /**
39 * @covers Xml::expandAttributes
40 */
41 public function testExpandAttributes() {
42 $this->assertNull( Xml::expandAttributes( null ),
43 'Converting a null list of attributes'
44 );
45 $this->assertEquals( '', Xml::expandAttributes( [] ),
46 'Converting an empty list of attributes'
47 );
48 }
49
50 /**
51 * @covers Xml::expandAttributes
52 */
53 public function testExpandAttributesException() {
54 $this->setExpectedException( 'MWException' );
55 Xml::expandAttributes( 'string' );
56 }
57
58 /**
59 * @covers Xml::element
60 */
61 public function testElementOpen() {
62 $this->assertEquals(
63 '<element>',
64 Xml::element( 'element', null, null ),
65 'Opening element with no attributes'
66 );
67 }
68
69 /**
70 * @covers Xml::element
71 */
72 public function testElementEmpty() {
73 $this->assertEquals(
74 '<element />',
75 Xml::element( 'element', null, '' ),
76 'Terminated empty element'
77 );
78 }
79
80 /**
81 * @covers Xml::input
82 */
83 public function testElementInputCanHaveAValueOfZero() {
84 $this->assertEquals(
85 '<input name="name" value="0" />',
86 Xml::input( 'name', false, 0 ),
87 'Input with a value of 0 (bug 23797)'
88 );
89 }
90
91 /**
92 * @covers Xml::element
93 */
94 public function testElementEscaping() {
95 $this->assertEquals(
96 '<element>hello &lt;there&gt; you &amp; you</element>',
97 Xml::element( 'element', null, 'hello <there> you & you' ),
98 'Element with no attributes and content that needs escaping'
99 );
100 }
101
102 /**
103 * @covers Xml::escapeTagsOnly
104 */
105 public function testEscapeTagsOnly() {
106 $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
107 'replace " > and < with their HTML entitites'
108 );
109 }
110
111 /**
112 * @covers Xml::element
113 */
114 public function testElementAttributes() {
115 $this->assertEquals(
116 '<element key="value" <>="&lt;&gt;">',
117 Xml::element( 'element', [ 'key' => 'value', '<>' => '<>' ], null ),
118 'Element attributes, keys are not escaped'
119 );
120 }
121
122 /**
123 * @covers Xml::openElement
124 */
125 public function testOpenElement() {
126 $this->assertEquals(
127 '<element k="v">',
128 Xml::openElement( 'element', [ 'k' => 'v' ] ),
129 'openElement() shortcut'
130 );
131 }
132
133 /**
134 * @covers Xml::closeElement
135 */
136 public function testCloseElement() {
137 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
138 }
139
140 /**
141 * @covers Xml::dateMenu
142 */
143 public function testDateMenu() {
144 $curYear = intval( gmdate( 'Y' ) );
145 $prevYear = $curYear - 1;
146
147 $curMonth = intval( gmdate( 'n' ) );
148
149 $nextMonth = $curMonth + 1;
150 if ( $nextMonth == 13 ) {
151 $nextMonth = 1;
152 }
153
154 $this->assertEquals(
155 '<label for="year">From year (and earlier):</label> ' .
156 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
157 '<label for="month">From month (and earlier):</label> ' .
158 '<select name="month" id="month" class="mw-month-selector">' .
159 '<option value="-1">all</option>' . "\n" .
160 '<option value="1">January</option>' . "\n" .
161 '<option value="2" selected="">February</option>' . "\n" .
162 '<option value="3">March</option>' . "\n" .
163 '<option value="4">April</option>' . "\n" .
164 '<option value="5">May</option>' . "\n" .
165 '<option value="6">June</option>' . "\n" .
166 '<option value="7">July</option>' . "\n" .
167 '<option value="8">August</option>' . "\n" .
168 '<option value="9">September</option>' . "\n" .
169 '<option value="10">October</option>' . "\n" .
170 '<option value="11">November</option>' . "\n" .
171 '<option value="12">December</option></select>',
172 Xml::dateMenu( 2011, 02 ),
173 "Date menu for february 2011"
174 );
175 $this->assertEquals(
176 '<label for="year">From year (and earlier):</label> ' .
177 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
178 '<label for="month">From month (and earlier):</label> ' .
179 '<select name="month" id="month" class="mw-month-selector">' .
180 '<option value="-1">all</option>' . "\n" .
181 '<option value="1">January</option>' . "\n" .
182 '<option value="2">February</option>' . "\n" .
183 '<option value="3">March</option>' . "\n" .
184 '<option value="4">April</option>' . "\n" .
185 '<option value="5">May</option>' . "\n" .
186 '<option value="6">June</option>' . "\n" .
187 '<option value="7">July</option>' . "\n" .
188 '<option value="8">August</option>' . "\n" .
189 '<option value="9">September</option>' . "\n" .
190 '<option value="10">October</option>' . "\n" .
191 '<option value="11">November</option>' . "\n" .
192 '<option value="12">December</option></select>',
193 Xml::dateMenu( 2011, -1 ),
194 "Date menu with negative month for 'All'"
195 );
196 $this->assertEquals(
197 Xml::dateMenu( $curYear, $curMonth ),
198 Xml::dateMenu( '', $curMonth ),
199 "Date menu year is the current one when not specified"
200 );
201
202 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
203 $this->assertEquals(
204 Xml::dateMenu( $wantedYear, $nextMonth ),
205 Xml::dateMenu( '', $nextMonth ),
206 "Date menu next month is 11 months ago"
207 );
208
209 $this->assertEquals(
210 '<label for="year">From year (and earlier):</label> ' .
211 '<input id="year" maxlength="4" size="7" type="number" name="year"/> ' .
212 '<label for="month">From month (and earlier):</label> ' .
213 '<select name="month" id="month" class="mw-month-selector">' .
214 '<option value="-1">all</option>' . "\n" .
215 '<option value="1">January</option>' . "\n" .
216 '<option value="2">February</option>' . "\n" .
217 '<option value="3">March</option>' . "\n" .
218 '<option value="4">April</option>' . "\n" .
219 '<option value="5">May</option>' . "\n" .
220 '<option value="6">June</option>' . "\n" .
221 '<option value="7">July</option>' . "\n" .
222 '<option value="8">August</option>' . "\n" .
223 '<option value="9">September</option>' . "\n" .
224 '<option value="10">October</option>' . "\n" .
225 '<option value="11">November</option>' . "\n" .
226 '<option value="12">December</option></select>',
227 Xml::dateMenu( '', '' ),
228 "Date menu with neither year or month"
229 );
230 }
231
232 /**
233 * @covers Xml::textarea
234 */
235 public function testTextareaNoContent() {
236 $this->assertEquals(
237 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
238 Xml::textarea( 'name', '' ),
239 'textarea() with not content'
240 );
241 }
242
243 /**
244 * @covers Xml::textarea
245 */
246 public function testTextareaAttribs() {
247 $this->assertEquals(
248 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
249 Xml::textarea( 'name', '<txt>', 20, 10 ),
250 'textarea() with custom attribs'
251 );
252 }
253
254 /**
255 * @covers Xml::label
256 */
257 public function testLabelCreation() {
258 $this->assertEquals(
259 '<label for="id">name</label>',
260 Xml::label( 'name', 'id' ),
261 'label() with no attribs'
262 );
263 }
264
265 /**
266 * @covers Xml::label
267 */
268 public function testLabelAttributeCanOnlyBeClassOrTitle() {
269 $this->assertEquals(
270 '<label for="id">name</label>',
271 Xml::label( 'name', 'id', [ 'generated' => true ] ),
272 'label() can not be given a generated attribute'
273 );
274 $this->assertEquals(
275 '<label for="id" class="nice">name</label>',
276 Xml::label( 'name', 'id', [ 'class' => 'nice' ] ),
277 'label() can get a class attribute'
278 );
279 $this->assertEquals(
280 '<label for="id" title="nice tooltip">name</label>',
281 Xml::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
282 'label() can get a title attribute'
283 );
284 $this->assertEquals(
285 '<label for="id" class="nice" title="nice tooltip">name</label>',
286 Xml::label( 'name', 'id', [
287 'generated' => true,
288 'class' => 'nice',
289 'title' => 'nice tooltip',
290 'anotherattr' => 'value',
291 ]
292 ),
293 'label() skip all attributes but "class" and "title"'
294 );
295 }
296
297 /**
298 * @covers Xml::languageSelector
299 */
300 public function testLanguageSelector() {
301 $select = Xml::languageSelector( 'en', true, null,
302 [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
303 $this->assertEquals(
304 '<label for="testlang">Language:</label>',
305 $select[0]
306 );
307 }
308
309 /**
310 * @covers Xml::escapeJsString
311 */
312 public function testEscapeJsStringSpecialChars() {
313 $this->assertEquals(
314 '\\\\\r\n',
315 Xml::escapeJsString( "\\\r\n" ),
316 'escapeJsString() with special characters'
317 );
318 }
319
320 /**
321 * @covers Xml::encodeJsVar
322 */
323 public function testEncodeJsVarBoolean() {
324 $this->assertEquals(
325 'true',
326 Xml::encodeJsVar( true ),
327 'encodeJsVar() with boolean'
328 );
329 }
330
331 /**
332 * @covers Xml::encodeJsVar
333 */
334 public function testEncodeJsVarNull() {
335 $this->assertEquals(
336 'null',
337 Xml::encodeJsVar( null ),
338 'encodeJsVar() with null'
339 );
340 }
341
342 /**
343 * @covers Xml::encodeJsVar
344 */
345 public function testEncodeJsVarArray() {
346 $this->assertEquals(
347 '["a",1]',
348 Xml::encodeJsVar( [ 'a', 1 ] ),
349 'encodeJsVar() with array'
350 );
351 $this->assertEquals(
352 '{"a":"a","b":1}',
353 Xml::encodeJsVar( [ 'a' => 'a', 'b' => 1 ] ),
354 'encodeJsVar() with associative array'
355 );
356 }
357
358 /**
359 * @covers Xml::encodeJsVar
360 */
361 public function testEncodeJsVarObject() {
362 $this->assertEquals(
363 '{"a":"a","b":1}',
364 Xml::encodeJsVar( (object)[ 'a' => 'a', 'b' => 1 ] ),
365 'encodeJsVar() with object'
366 );
367 }
368
369 /**
370 * @covers Xml::encodeJsVar
371 */
372 public function testEncodeJsVarInt() {
373 $this->assertEquals(
374 '123456',
375 Xml::encodeJsVar( 123456 ),
376 'encodeJsVar() with int'
377 );
378 }
379
380 /**
381 * @covers Xml::encodeJsVar
382 */
383 public function testEncodeJsVarFloat() {
384 $this->assertEquals(
385 '1.23456',
386 Xml::encodeJsVar( 1.23456 ),
387 'encodeJsVar() with float'
388 );
389 }
390
391 /**
392 * @covers Xml::encodeJsVar
393 */
394 public function testEncodeJsVarIntString() {
395 $this->assertEquals(
396 '"123456"',
397 Xml::encodeJsVar( '123456' ),
398 'encodeJsVar() with int-like string'
399 );
400 }
401
402 /**
403 * @covers Xml::encodeJsVar
404 */
405 public function testEncodeJsVarFloatString() {
406 $this->assertEquals(
407 '"1.23456"',
408 Xml::encodeJsVar( '1.23456' ),
409 'encodeJsVar() with float-like string'
410 );
411 }
412 }