Merge "Remove 'Browser default' editfont option"
[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 'wgUseMediaWikiUIEverywhere' => false,
34 ] );
35 }
36
37 protected function tearDown() {
38 Language::factory( 'en' )->resetNamespaces();
39 parent::tearDown();
40 }
41
42 /**
43 * @covers Xml::expandAttributes
44 */
45 public function testExpandAttributes() {
46 $this->assertNull( Xml::expandAttributes( null ),
47 'Converting a null list of attributes'
48 );
49 $this->assertEquals( '', Xml::expandAttributes( [] ),
50 'Converting an empty list of attributes'
51 );
52 }
53
54 /**
55 * @covers Xml::expandAttributes
56 */
57 public function testExpandAttributesException() {
58 $this->setExpectedException( 'MWException' );
59 Xml::expandAttributes( 'string' );
60 }
61
62 /**
63 * @covers Xml::element
64 */
65 public function testElementOpen() {
66 $this->assertEquals(
67 '<element>',
68 Xml::element( 'element', null, null ),
69 'Opening element with no attributes'
70 );
71 }
72
73 /**
74 * @covers Xml::element
75 */
76 public function testElementEmpty() {
77 $this->assertEquals(
78 '<element />',
79 Xml::element( 'element', null, '' ),
80 'Terminated empty element'
81 );
82 }
83
84 /**
85 * @covers Xml::input
86 */
87 public function testElementInputCanHaveAValueOfZero() {
88 $this->assertEquals(
89 '<input name="name" value="0" />',
90 Xml::input( 'name', false, 0 ),
91 'Input with a value of 0 (T25797)'
92 );
93 }
94
95 /**
96 * @covers Xml::element
97 */
98 public function testElementEscaping() {
99 $this->assertEquals(
100 '<element>hello &lt;there&gt; you &amp; you</element>',
101 Xml::element( 'element', null, 'hello <there> you & you' ),
102 'Element with no attributes and content that needs escaping'
103 );
104 }
105
106 /**
107 * @covers Xml::escapeTagsOnly
108 */
109 public function testEscapeTagsOnly() {
110 $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
111 'replace " > and < with their HTML entitites'
112 );
113 }
114
115 /**
116 * @covers Xml::element
117 */
118 public function testElementAttributes() {
119 $this->assertEquals(
120 '<element key="value" <>="&lt;&gt;">',
121 Xml::element( 'element', [ 'key' => 'value', '<>' => '<>' ], null ),
122 'Element attributes, keys are not escaped'
123 );
124 }
125
126 /**
127 * @covers Xml::openElement
128 */
129 public function testOpenElement() {
130 $this->assertEquals(
131 '<element k="v">',
132 Xml::openElement( 'element', [ 'k' => 'v' ] ),
133 'openElement() shortcut'
134 );
135 }
136
137 /**
138 * @covers Xml::closeElement
139 */
140 public function testCloseElement() {
141 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
142 }
143
144 /**
145 * @covers Xml::dateMenu
146 */
147 public function testDateMenu() {
148 $curYear = intval( gmdate( 'Y' ) );
149 $prevYear = $curYear - 1;
150
151 $curMonth = intval( gmdate( 'n' ) );
152
153 $nextMonth = $curMonth + 1;
154 if ( $nextMonth == 13 ) {
155 $nextMonth = 1;
156 }
157
158 $this->assertEquals(
159 '<label for="year">From year (and earlier):</label> ' .
160 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
161 '<label for="month">From month (and earlier):</label> ' .
162 '<select name="month" id="month" class="mw-month-selector">' .
163 '<option value="-1">all</option>' . "\n" .
164 '<option value="1">January</option>' . "\n" .
165 '<option value="2" selected="">February</option>' . "\n" .
166 '<option value="3">March</option>' . "\n" .
167 '<option value="4">April</option>' . "\n" .
168 '<option value="5">May</option>' . "\n" .
169 '<option value="6">June</option>' . "\n" .
170 '<option value="7">July</option>' . "\n" .
171 '<option value="8">August</option>' . "\n" .
172 '<option value="9">September</option>' . "\n" .
173 '<option value="10">October</option>' . "\n" .
174 '<option value="11">November</option>' . "\n" .
175 '<option value="12">December</option></select>',
176 Xml::dateMenu( 2011, 02 ),
177 "Date menu for february 2011"
178 );
179 $this->assertEquals(
180 '<label for="year">From year (and earlier):</label> ' .
181 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
182 '<label for="month">From month (and earlier):</label> ' .
183 '<select name="month" id="month" class="mw-month-selector">' .
184 '<option value="-1">all</option>' . "\n" .
185 '<option value="1">January</option>' . "\n" .
186 '<option value="2">February</option>' . "\n" .
187 '<option value="3">March</option>' . "\n" .
188 '<option value="4">April</option>' . "\n" .
189 '<option value="5">May</option>' . "\n" .
190 '<option value="6">June</option>' . "\n" .
191 '<option value="7">July</option>' . "\n" .
192 '<option value="8">August</option>' . "\n" .
193 '<option value="9">September</option>' . "\n" .
194 '<option value="10">October</option>' . "\n" .
195 '<option value="11">November</option>' . "\n" .
196 '<option value="12">December</option></select>',
197 Xml::dateMenu( 2011, -1 ),
198 "Date menu with negative month for 'All'"
199 );
200 $this->assertEquals(
201 Xml::dateMenu( $curYear, $curMonth ),
202 Xml::dateMenu( '', $curMonth ),
203 "Date menu year is the current one when not specified"
204 );
205
206 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
207 $this->assertEquals(
208 Xml::dateMenu( $wantedYear, $nextMonth ),
209 Xml::dateMenu( '', $nextMonth ),
210 "Date menu next month is 11 months ago"
211 );
212
213 $this->assertEquals(
214 '<label for="year">From year (and earlier):</label> ' .
215 '<input id="year" maxlength="4" size="7" type="number" name="year"/> ' .
216 '<label for="month">From month (and earlier):</label> ' .
217 '<select name="month" id="month" class="mw-month-selector">' .
218 '<option value="-1">all</option>' . "\n" .
219 '<option value="1">January</option>' . "\n" .
220 '<option value="2">February</option>' . "\n" .
221 '<option value="3">March</option>' . "\n" .
222 '<option value="4">April</option>' . "\n" .
223 '<option value="5">May</option>' . "\n" .
224 '<option value="6">June</option>' . "\n" .
225 '<option value="7">July</option>' . "\n" .
226 '<option value="8">August</option>' . "\n" .
227 '<option value="9">September</option>' . "\n" .
228 '<option value="10">October</option>' . "\n" .
229 '<option value="11">November</option>' . "\n" .
230 '<option value="12">December</option></select>',
231 Xml::dateMenu( '', '' ),
232 "Date menu with neither year or month"
233 );
234 }
235
236 /**
237 * @covers Xml::textarea
238 */
239 public function testTextareaNoContent() {
240 $this->assertEquals(
241 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
242 Xml::textarea( 'name', '' ),
243 'textarea() with not content'
244 );
245 }
246
247 /**
248 * @covers Xml::textarea
249 */
250 public function testTextareaAttribs() {
251 $this->assertEquals(
252 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
253 Xml::textarea( 'name', '<txt>', 20, 10 ),
254 'textarea() with custom attribs'
255 );
256 }
257
258 /**
259 * @covers Xml::label
260 */
261 public function testLabelCreation() {
262 $this->assertEquals(
263 '<label for="id">name</label>',
264 Xml::label( 'name', 'id' ),
265 'label() with no attribs'
266 );
267 }
268
269 /**
270 * @covers Xml::label
271 */
272 public function testLabelAttributeCanOnlyBeClassOrTitle() {
273 $this->assertEquals(
274 '<label for="id">name</label>',
275 Xml::label( 'name', 'id', [ 'generated' => true ] ),
276 'label() can not be given a generated attribute'
277 );
278 $this->assertEquals(
279 '<label for="id" class="nice">name</label>',
280 Xml::label( 'name', 'id', [ 'class' => 'nice' ] ),
281 'label() can get a class attribute'
282 );
283 $this->assertEquals(
284 '<label for="id" title="nice tooltip">name</label>',
285 Xml::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
286 'label() can get a title attribute'
287 );
288 $this->assertEquals(
289 '<label for="id" class="nice" title="nice tooltip">name</label>',
290 Xml::label( 'name', 'id', [
291 'generated' => true,
292 'class' => 'nice',
293 'title' => 'nice tooltip',
294 'anotherattr' => 'value',
295 ]
296 ),
297 'label() skip all attributes but "class" and "title"'
298 );
299 }
300
301 /**
302 * @covers Xml::languageSelector
303 */
304 public function testLanguageSelector() {
305 $select = Xml::languageSelector( 'en', true, null,
306 [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
307 $this->assertEquals(
308 '<label for="testlang">Language:</label>',
309 $select[0]
310 );
311 }
312
313 /**
314 * @covers Xml::encodeJsVar
315 */
316 public function testEncodeJsVarBoolean() {
317 $this->assertEquals(
318 'true',
319 Xml::encodeJsVar( true ),
320 'encodeJsVar() with boolean'
321 );
322 }
323
324 /**
325 * @covers Xml::encodeJsVar
326 */
327 public function testEncodeJsVarNull() {
328 $this->assertEquals(
329 'null',
330 Xml::encodeJsVar( null ),
331 'encodeJsVar() with null'
332 );
333 }
334
335 /**
336 * @covers Xml::encodeJsVar
337 */
338 public function testEncodeJsVarArray() {
339 $this->assertEquals(
340 '["a",1]',
341 Xml::encodeJsVar( [ 'a', 1 ] ),
342 'encodeJsVar() with array'
343 );
344 $this->assertEquals(
345 '{"a":"a","b":1}',
346 Xml::encodeJsVar( [ 'a' => 'a', 'b' => 1 ] ),
347 'encodeJsVar() with associative array'
348 );
349 }
350
351 /**
352 * @covers Xml::encodeJsVar
353 */
354 public function testEncodeJsVarObject() {
355 $this->assertEquals(
356 '{"a":"a","b":1}',
357 Xml::encodeJsVar( (object)[ 'a' => 'a', 'b' => 1 ] ),
358 'encodeJsVar() with object'
359 );
360 }
361
362 /**
363 * @covers Xml::encodeJsVar
364 */
365 public function testEncodeJsVarInt() {
366 $this->assertEquals(
367 '123456',
368 Xml::encodeJsVar( 123456 ),
369 'encodeJsVar() with int'
370 );
371 }
372
373 /**
374 * @covers Xml::encodeJsVar
375 */
376 public function testEncodeJsVarFloat() {
377 $this->assertEquals(
378 '1.23456',
379 Xml::encodeJsVar( 1.23456 ),
380 'encodeJsVar() with float'
381 );
382 }
383
384 /**
385 * @covers Xml::encodeJsVar
386 */
387 public function testEncodeJsVarIntString() {
388 $this->assertEquals(
389 '"123456"',
390 Xml::encodeJsVar( '123456' ),
391 'encodeJsVar() with int-like string'
392 );
393 }
394
395 /**
396 * @covers Xml::encodeJsVar
397 */
398 public function testEncodeJsVarFloatString() {
399 $this->assertEquals(
400 '"1.23456"',
401 Xml::encodeJsVar( '1.23456' ),
402 'encodeJsVar() with float-like string'
403 );
404 }
405
406 /**
407 * @covers Xml::listDropDown
408 */
409 public function testListDropDown() {
410 $this->assertEquals(
411 '<select name="test-name" id="test-name" class="test-css" tabindex="2">' .
412 '<option value="other">other reasons</option>' . "\n" .
413 '<optgroup label="Foo">' .
414 '<option value="Foo 1">Foo 1</option>' . "\n" .
415 '<option value="Example" selected="">Example</option>' . "\n" .
416 '</optgroup>' . "\n" .
417 '<optgroup label="Bar">' .
418 '<option value="Bar 1">Bar 1</option>' . "\n" .
419 '</optgroup>' .
420 '</select>',
421 Xml::listDropDown(
422 // name
423 'test-name',
424 // source list
425 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
426 // other
427 'other reasons',
428 // selected
429 'Example',
430 // class
431 'test-css',
432 // tabindex
433 2
434 )
435 );
436 }
437
438 /**
439 * @covers Xml::listDropDownOptions
440 */
441 public function testListDropDownOptions() {
442 $this->assertEquals(
443 [
444 'other reasons' => 'other',
445 'Foo' => [
446 'Foo 1' => 'Foo 1',
447 'Example' => 'Example',
448 ],
449 'Bar' => [
450 'Bar 1' => 'Bar 1',
451 ],
452 ],
453 Xml::listDropDownOptions(
454 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
455 [ 'other' => 'other reasons' ]
456 )
457 );
458 }
459
460 /**
461 * @covers Xml::listDropDownOptionsOoui
462 */
463 public function testListDropDownOptionsOoui() {
464 $this->assertEquals(
465 [
466 [ 'data' => 'other', 'label' => 'other reasons' ],
467 [ 'optgroup' => 'Foo' ],
468 [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
469 [ 'data' => 'Example', 'label' => 'Example' ],
470 [ 'optgroup' => 'Bar' ],
471 [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
472 ],
473 Xml::listDropDownOptionsOoui( [
474 'other reasons' => 'other',
475 'Foo' => [
476 'Foo 1' => 'Foo 1',
477 'Example' => 'Example',
478 ],
479 'Bar' => [
480 'Bar 1' => 'Bar 1',
481 ],
482 ] )
483 );
484 }
485
486 /**
487 * @covers Xml::fieldset
488 */
489 public function testFieldset() {
490 $this->assertEquals(
491 "<fieldset>\n",
492 Xml::fieldset(),
493 'Opening tag'
494 );
495 $this->assertEquals(
496 "<fieldset>\n",
497 Xml::fieldset( false ),
498 'Opening tag (false means no legend)'
499 );
500 $this->assertEquals(
501 "<fieldset>\n",
502 Xml::fieldset( '' ),
503 'Opening tag (empty string also means no legend)'
504 );
505 $this->assertEquals(
506 "<fieldset>\n<legend>Foo</legend>\n",
507 Xml::fieldset( 'Foo' ),
508 'Opening tag with legend'
509 );
510 $this->assertEquals(
511 "<fieldset>\n<legend>Foo</legend>\nBar\n</fieldset>\n",
512 Xml::fieldset( 'Foo', 'Bar' ),
513 'Entire element with legend'
514 );
515 $this->assertEquals(
516 "<fieldset>\n<legend>Foo</legend>\n",
517 Xml::fieldset( 'Foo', false ),
518 'Opening tag with legend (false means no content and no closing tag)'
519 );
520 $this->assertEquals(
521 "<fieldset>\n<legend>Foo</legend>\n\n</fieldset>\n",
522 Xml::fieldset( 'Foo', '' ),
523 'Entire element with legend but no content (empty string generates a closing tag)'
524 );
525 $this->assertEquals(
526 "<fieldset class=\"bar\">\n<legend>Foo</legend>\nBar\n</fieldset>\n",
527 Xml::fieldset( 'Foo', 'Bar', [ 'class' => 'bar' ] ),
528 'Opening tag with legend and attributes'
529 );
530 $this->assertEquals(
531 "<fieldset class=\"bar\">\n<legend>Foo</legend>\n",
532 Xml::fieldset( 'Foo', false, [ 'class' => 'bar' ] ),
533 'Entire element with legend and attributes'
534 );
535 }
536 }