Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2
3 class HtmlTest extends MediaWikiTestCase {
4 private $restoreWarnings;
5
6 protected function setUp() {
7 parent::setUp();
8
9 $this->setMwGlobals( [
10 'wgUseMediaWikiUIEverywhere' => false,
11 ] );
12
13 $contLangObj = Language::factory( 'en' );
14
15 // Hardcode namespaces during test runs,
16 // so that html output based on existing namespaces
17 // can be properly evaluated.
18 $contLangObj->setNamespaces( [
19 -2 => 'Media',
20 -1 => 'Special',
21 0 => '',
22 1 => 'Talk',
23 2 => 'User',
24 3 => 'User_talk',
25 4 => 'MyWiki',
26 5 => 'MyWiki_Talk',
27 6 => 'File',
28 7 => 'File_talk',
29 8 => 'MediaWiki',
30 9 => 'MediaWiki_talk',
31 10 => 'Template',
32 11 => 'Template_talk',
33 14 => 'Category',
34 15 => 'Category_talk',
35 100 => 'Custom',
36 101 => 'Custom_talk',
37 ] );
38 $this->setContentLang( $contLangObj );
39
40 $userLangObj = Language::factory( 'es' );
41 $userLangObj->setNamespaces( [
42 -2 => "Medio",
43 -1 => "Especial",
44 0 => "",
45 1 => "Discusión",
46 2 => "Usuario",
47 3 => "Usuario discusión",
48 4 => "Wiki",
49 5 => "Wiki discusión",
50 6 => "Archivo",
51 7 => "Archivo discusión",
52 8 => "MediaWiki",
53 9 => "MediaWiki discusión",
54 10 => "Plantilla",
55 11 => "Plantilla discusión",
56 12 => "Ayuda",
57 13 => "Ayuda discusión",
58 14 => "Categoría",
59 15 => "Categoría discusión",
60 100 => "Personalizado",
61 101 => "Personalizado discusión",
62 ] );
63 $this->setUserLang( $userLangObj );
64
65 $this->restoreWarnings = false;
66 }
67
68 protected function tearDown() {
69 Language::factory( 'en' )->resetNamespaces();
70
71 if ( $this->restoreWarnings ) {
72 $this->restoreWarnings = false;
73 Wikimedia\restoreWarnings();
74 }
75
76 parent::tearDown();
77 }
78
79 /**
80 * @expectedException PHPUnit_Framework_Error_Notice
81 * @expectedExceptionMessage given element name with space
82 * @covers Html::openElement
83 */
84 public function testOpenElement() {
85 Html::openElement( 'span id="x"' );
86 }
87
88 /**
89 * @covers Html::element
90 * @covers Html::rawElement
91 * @covers Html::openElement
92 * @covers Html::closeElement
93 */
94 public function testElementBasics() {
95 $this->assertEquals(
96 '<img/>',
97 Html::element( 'img', null, '' ),
98 'Self-closing tag for short-tag elements'
99 );
100
101 $this->assertEquals(
102 '<element></element>',
103 Html::element( 'element', null, null ),
104 'Close tag for empty element (null, null)'
105 );
106
107 $this->assertEquals(
108 '<element></element>',
109 Html::element( 'element', [], '' ),
110 'Close tag for empty element (array, string)'
111 );
112 }
113
114 public function dataXmlMimeType() {
115 return [
116 // ( $mimetype, $isXmlMimeType )
117 # HTML is not an XML MimeType
118 [ 'text/html', false ],
119 # XML is an XML MimeType
120 [ 'text/xml', true ],
121 [ 'application/xml', true ],
122 # XHTML is an XML MimeType
123 [ 'application/xhtml+xml', true ],
124 # Make sure other +xml MimeTypes are supported
125 # SVG is another random MimeType even though we don't use it
126 [ 'image/svg+xml', true ],
127 # Complete random other MimeTypes are not XML
128 [ 'text/plain', false ],
129 ];
130 }
131
132 /**
133 * @dataProvider dataXmlMimeType
134 * @covers Html::isXmlMimeType
135 */
136 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
137 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
138 }
139
140 /**
141 * @covers Html::expandAttributes
142 */
143 public function testExpandAttributesSkipsNullAndFalse() {
144 # ## EMPTY ########
145 $this->assertEmpty(
146 Html::expandAttributes( [ 'foo' => null ] ),
147 'skip keys with null value'
148 );
149 $this->assertEmpty(
150 Html::expandAttributes( [ 'foo' => false ] ),
151 'skip keys with false value'
152 );
153 $this->assertEquals(
154 ' foo=""',
155 Html::expandAttributes( [ 'foo' => '' ] ),
156 'keep keys with an empty string'
157 );
158 }
159
160 /**
161 * @covers Html::expandAttributes
162 */
163 public function testExpandAttributesForBooleans() {
164 $this->assertSame(
165 '',
166 Html::expandAttributes( [ 'selected' => false ] ),
167 'Boolean attributes do not generates output when value is false'
168 );
169 $this->assertSame(
170 '',
171 Html::expandAttributes( [ 'selected' => null ] ),
172 'Boolean attributes do not generates output when value is null'
173 );
174
175 $this->assertEquals(
176 ' selected=""',
177 Html::expandAttributes( [ 'selected' => true ] ),
178 'Boolean attributes have no value when value is true'
179 );
180 $this->assertEquals(
181 ' selected=""',
182 Html::expandAttributes( [ 'selected' ] ),
183 'Boolean attributes have no value when value is true (passed as numerical array)'
184 );
185 }
186
187 /**
188 * @covers Html::expandAttributes
189 */
190 public function testExpandAttributesForNumbers() {
191 $this->assertEquals(
192 ' value="1"',
193 Html::expandAttributes( [ 'value' => 1 ] ),
194 'Integer value is cast to a string'
195 );
196 $this->assertEquals(
197 ' value="1.1"',
198 Html::expandAttributes( [ 'value' => 1.1 ] ),
199 'Float value is cast to a string'
200 );
201 }
202
203 /**
204 * @covers Html::expandAttributes
205 */
206 public function testExpandAttributesForObjects() {
207 $this->assertEquals(
208 ' value="stringValue"',
209 Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
210 'Object value is converted to a string'
211 );
212 }
213
214 /**
215 * Please note it output a string prefixed with a space!
216 * @covers Html::expandAttributes
217 */
218 public function testExpandAttributesVariousExpansions() {
219 # ## NOT EMPTY ####
220 $this->assertEquals(
221 ' empty_string=""',
222 Html::expandAttributes( [ 'empty_string' => '' ] ),
223 'Empty string is always quoted'
224 );
225 $this->assertEquals(
226 ' key="value"',
227 Html::expandAttributes( [ 'key' => 'value' ] ),
228 'Simple string value needs no quotes'
229 );
230 $this->assertEquals(
231 ' one="1"',
232 Html::expandAttributes( [ 'one' => 1 ] ),
233 'Number 1 value needs no quotes'
234 );
235 $this->assertEquals(
236 ' zero="0"',
237 Html::expandAttributes( [ 'zero' => 0 ] ),
238 'Number 0 value needs no quotes'
239 );
240 }
241
242 /**
243 * Html::expandAttributes has special features for HTML
244 * attributes that use space separated lists and also
245 * allows arrays to be used as values.
246 * @covers Html::expandAttributes
247 */
248 public function testExpandAttributesListValueAttributes() {
249 # ## STRING VALUES
250 $this->assertEquals(
251 ' class="redundant spaces here"',
252 Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
253 'Normalization should strip redundant spaces'
254 );
255 $this->assertEquals(
256 ' class="foo bar"',
257 Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
258 'Normalization should remove duplicates in string-lists'
259 );
260 # ## "EMPTY" ARRAY VALUES
261 $this->assertEquals(
262 ' class=""',
263 Html::expandAttributes( [ 'class' => [] ] ),
264 'Value with an empty array'
265 );
266 $this->assertEquals(
267 ' class=""',
268 Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
269 'Array with null, empty string and spaces'
270 );
271 # ## NON-EMPTY ARRAY VALUES
272 $this->assertEquals(
273 ' class="foo bar"',
274 Html::expandAttributes( [ 'class' => [
275 'foo',
276 'bar',
277 'foo',
278 'bar',
279 'bar',
280 ] ] ),
281 'Normalization should remove duplicates in the array'
282 );
283 $this->assertEquals(
284 ' class="foo bar"',
285 Html::expandAttributes( [ 'class' => [
286 'foo bar',
287 'bar foo',
288 'foo',
289 'bar bar',
290 ] ] ),
291 'Normalization should remove duplicates in string-lists in the array'
292 );
293 }
294
295 /**
296 * Test feature added by r96188, let pass attributes values as
297 * a PHP array. Restricted to class,rel, accesskey.
298 * @covers Html::expandAttributes
299 */
300 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
301 $this->assertEquals(
302 ' class="booltrue one"',
303 Html::expandAttributes( [ 'class' => [
304 'booltrue' => true,
305 'one' => 1,
306
307 # Method use isset() internally, make sure we do discard
308 # attributes values which have been assigned well known values
309 'emptystring' => '',
310 'boolfalse' => false,
311 'zero' => 0,
312 'null' => null,
313 ] ] )
314 );
315 }
316
317 /**
318 * How do we handle duplicate keys in HTML attributes expansion?
319 * We could pass a "class" the values: 'GREEN' and [ 'GREEN' => false ]
320 * The latter will take precedence.
321 *
322 * Feature added by r96188
323 * @covers Html::expandAttributes
324 */
325 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
326 $this->assertEquals(
327 ' class=""',
328 Html::expandAttributes( [ 'class' => [
329 'GREEN',
330 'GREEN' => false,
331 'GREEN',
332 ] ] )
333 );
334 }
335
336 /**
337 * @covers Html::expandAttributes
338 * @expectedException MWException
339 */
340 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
341 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
342 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
343 Html::expandAttributes( [
344 'src' => [
345 'ltr' => 'ltr.svg',
346 'rtl' => 'rtl.svg'
347 ]
348 ] );
349 }
350
351 /**
352 * @covers Html::namespaceSelector
353 * @covers Html::namespaceSelectorOptions
354 */
355 public function testNamespaceSelector() {
356 $this->assertEquals(
357 '<select id="namespace" name="namespace">' . "\n" .
358 '<option value="0">(Principal)</option>' . "\n" .
359 '<option value="1">Talk</option>' . "\n" .
360 '<option value="2">User</option>' . "\n" .
361 '<option value="3">User talk</option>' . "\n" .
362 '<option value="4">MyWiki</option>' . "\n" .
363 '<option value="5">MyWiki Talk</option>' . "\n" .
364 '<option value="6">File</option>' . "\n" .
365 '<option value="7">File talk</option>' . "\n" .
366 '<option value="8">MediaWiki</option>' . "\n" .
367 '<option value="9">MediaWiki talk</option>' . "\n" .
368 '<option value="10">Template</option>' . "\n" .
369 '<option value="11">Template talk</option>' . "\n" .
370 '<option value="14">Category</option>' . "\n" .
371 '<option value="15">Category talk</option>' . "\n" .
372 '<option value="100">Custom</option>' . "\n" .
373 '<option value="101">Custom talk</option>' . "\n" .
374 '</select>',
375 Html::namespaceSelector(),
376 'Basic namespace selector without custom options'
377 );
378
379 $this->assertEquals(
380 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
381 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
382 '<option value="all">todos</option>' . "\n" .
383 '<option value="0">(Principal)</option>' . "\n" .
384 '<option value="1">Talk</option>' . "\n" .
385 '<option value="2" selected="">User</option>' . "\n" .
386 '<option value="3">User talk</option>' . "\n" .
387 '<option value="4">MyWiki</option>' . "\n" .
388 '<option value="5">MyWiki Talk</option>' . "\n" .
389 '<option value="6">File</option>' . "\n" .
390 '<option value="7">File talk</option>' . "\n" .
391 '<option value="8">MediaWiki</option>' . "\n" .
392 '<option value="9">MediaWiki talk</option>' . "\n" .
393 '<option value="10">Template</option>' . "\n" .
394 '<option value="11">Template talk</option>' . "\n" .
395 '<option value="14">Category</option>' . "\n" .
396 '<option value="15">Category talk</option>' . "\n" .
397 '<option value="100">Custom</option>' . "\n" .
398 '<option value="101">Custom talk</option>' . "\n" .
399 '</select>',
400 Html::namespaceSelector(
401 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
402 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
403 ),
404 'Basic namespace selector with custom values'
405 );
406
407 $this->assertEquals(
408 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
409 '<select id="namespace" name="namespace">' . "\n" .
410 '<option value="0">(Principal)</option>' . "\n" .
411 '<option value="1">Talk</option>' . "\n" .
412 '<option value="2">User</option>' . "\n" .
413 '<option value="3">User talk</option>' . "\n" .
414 '<option value="4">MyWiki</option>' . "\n" .
415 '<option value="5">MyWiki Talk</option>' . "\n" .
416 '<option value="6">File</option>' . "\n" .
417 '<option value="7">File talk</option>' . "\n" .
418 '<option value="8">MediaWiki</option>' . "\n" .
419 '<option value="9">MediaWiki talk</option>' . "\n" .
420 '<option value="10">Template</option>' . "\n" .
421 '<option value="11">Template talk</option>' . "\n" .
422 '<option value="14">Category</option>' . "\n" .
423 '<option value="15">Category talk</option>' . "\n" .
424 '<option value="100">Custom</option>' . "\n" .
425 '<option value="101">Custom talk</option>' . "\n" .
426 '</select>',
427 Html::namespaceSelector(
428 [ 'label' => 'Select a namespace:' ]
429 ),
430 'Basic namespace selector with a custom label but no id attribtue for the <select>'
431 );
432
433 $this->assertEquals(
434 '<select id="namespace" name="namespace">' . "\n" .
435 '<option value="0">(Principal)</option>' . "\n" .
436 '<option value="1">Discusión</option>' . "\n" .
437 '<option value="2">Usuario</option>' . "\n" .
438 '<option value="3">Usuario discusión</option>' . "\n" .
439 '<option value="4">Wiki</option>' . "\n" .
440 '<option value="5">Wiki discusión</option>' . "\n" .
441 '<option value="6">Archivo</option>' . "\n" .
442 '<option value="7">Archivo discusión</option>' . "\n" .
443 '<option value="8">MediaWiki</option>' . "\n" .
444 '<option value="9">MediaWiki discusión</option>' . "\n" .
445 '<option value="10">Plantilla</option>' . "\n" .
446 '<option value="11">Plantilla discusión</option>' . "\n" .
447 '<option value="12">Ayuda</option>' . "\n" .
448 '<option value="13">Ayuda discusión</option>' . "\n" .
449 '<option value="14">Categoría</option>' . "\n" .
450 '<option value="15">Categoría discusión</option>' . "\n" .
451 '<option value="100">Personalizado</option>' . "\n" .
452 '<option value="101">Personalizado discusión</option>' . "\n" .
453 '</select>',
454 Html::namespaceSelector(
455 [ 'in-user-lang' => true ]
456 ),
457 'Basic namespace selector in user language'
458 );
459 }
460
461 /**
462 * @covers Html::namespaceSelector
463 */
464 public function testCanFilterOutNamespaces() {
465 $this->assertEquals(
466 '<select id="namespace" name="namespace">' . "\n" .
467 '<option value="2">User</option>' . "\n" .
468 '<option value="4">MyWiki</option>' . "\n" .
469 '<option value="5">MyWiki Talk</option>' . "\n" .
470 '<option value="6">File</option>' . "\n" .
471 '<option value="7">File talk</option>' . "\n" .
472 '<option value="8">MediaWiki</option>' . "\n" .
473 '<option value="9">MediaWiki talk</option>' . "\n" .
474 '<option value="10">Template</option>' . "\n" .
475 '<option value="11">Template talk</option>' . "\n" .
476 '<option value="14">Category</option>' . "\n" .
477 '<option value="15">Category talk</option>' . "\n" .
478 '</select>',
479 Html::namespaceSelector(
480 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
481 ),
482 'Namespace selector namespace filtering.'
483 );
484 $this->assertEquals(
485 '<select id="namespace" name="namespace">' . "\n" .
486 '<option value="" selected="">todos</option>' . "\n" .
487 '<option value="2">User</option>' . "\n" .
488 '<option value="4">MyWiki</option>' . "\n" .
489 '<option value="5">MyWiki Talk</option>' . "\n" .
490 '<option value="6">File</option>' . "\n" .
491 '<option value="7">File talk</option>' . "\n" .
492 '<option value="8">MediaWiki</option>' . "\n" .
493 '<option value="9">MediaWiki talk</option>' . "\n" .
494 '<option value="10">Template</option>' . "\n" .
495 '<option value="11">Template talk</option>' . "\n" .
496 '<option value="14">Category</option>' . "\n" .
497 '<option value="15">Category talk</option>' . "\n" .
498 '</select>',
499 Html::namespaceSelector(
500 [ 'exclude' => [ 0, 1, 3, 100, 101 ], 'all' => '' ]
501 ),
502 'Namespace selector namespace filtering with empty custom "all" option.'
503 );
504 }
505
506 /**
507 * @covers Html::namespaceSelector
508 */
509 public function testCanDisableANamespaces() {
510 $this->assertEquals(
511 '<select id="namespace" name="namespace">' . "\n" .
512 '<option disabled="" value="0">(Principal)</option>' . "\n" .
513 '<option disabled="" value="1">Talk</option>' . "\n" .
514 '<option disabled="" value="2">User</option>' . "\n" .
515 '<option disabled="" value="3">User talk</option>' . "\n" .
516 '<option disabled="" value="4">MyWiki</option>' . "\n" .
517 '<option value="5">MyWiki Talk</option>' . "\n" .
518 '<option value="6">File</option>' . "\n" .
519 '<option value="7">File talk</option>' . "\n" .
520 '<option value="8">MediaWiki</option>' . "\n" .
521 '<option value="9">MediaWiki talk</option>' . "\n" .
522 '<option value="10">Template</option>' . "\n" .
523 '<option value="11">Template talk</option>' . "\n" .
524 '<option value="14">Category</option>' . "\n" .
525 '<option value="15">Category talk</option>' . "\n" .
526 '<option value="100">Custom</option>' . "\n" .
527 '<option value="101">Custom talk</option>' . "\n" .
528 '</select>',
529 Html::namespaceSelector( [
530 'disable' => [ 0, 1, 2, 3, 4 ]
531 ] ),
532 'Namespace selector namespace disabling'
533 );
534 }
535
536 /**
537 * @dataProvider provideHtml5InputTypes
538 * @covers Html::element
539 */
540 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
541 $this->assertEquals(
542 '<input type="' . $HTML5InputType . '"/>',
543 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
544 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
545 );
546 }
547
548 /**
549 * @covers Html::warningBox
550 * @covers Html::messageBox
551 */
552 public function testWarningBox() {
553 $this->assertEquals(
554 Html::warningBox( 'warn' ),
555 '<div class="warningbox">warn</div>'
556 );
557 }
558
559 /**
560 * @covers Html::errorBox
561 * @covers Html::messageBox
562 */
563 public function testErrorBox() {
564 $this->assertEquals(
565 Html::errorBox( 'err' ),
566 '<div class="errorbox">err</div>'
567 );
568 $this->assertEquals(
569 Html::errorBox( 'err', 'heading' ),
570 '<div class="errorbox"><h2>heading</h2>err</div>'
571 );
572 $this->assertEquals(
573 Html::errorBox( 'err', '0' ),
574 '<div class="errorbox"><h2>0</h2>err</div>'
575 );
576 }
577
578 /**
579 * @covers Html::successBox
580 * @covers Html::messageBox
581 */
582 public function testSuccessBox() {
583 $this->assertEquals(
584 Html::successBox( 'great' ),
585 '<div class="successbox">great</div>'
586 );
587 $this->assertEquals(
588 Html::successBox( '<script>beware no escaping!</script>' ),
589 '<div class="successbox"><script>beware no escaping!</script></div>'
590 );
591 }
592
593 /**
594 * List of input element types values introduced by HTML5
595 * Full list at https://www.w3.org/TR/html-markup/input.html
596 */
597 public static function provideHtml5InputTypes() {
598 $types = [
599 'datetime',
600 'datetime-local',
601 'date',
602 'month',
603 'time',
604 'week',
605 'number',
606 'range',
607 'email',
608 'url',
609 'search',
610 'tel',
611 'color',
612 ];
613 $cases = [];
614 foreach ( $types as $type ) {
615 $cases[] = [ $type ];
616 }
617
618 return $cases;
619 }
620
621 /**
622 * Test out Html::element drops or enforces default value
623 * @covers Html::dropDefaults
624 * @dataProvider provideElementsWithAttributesHavingDefaultValues
625 */
626 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
627 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
628 }
629
630 public static function provideElementsWithAttributesHavingDefaultValues() {
631 # Use cases in a concise format:
632 # <expected>, <element name>, <array of attributes> [, <message>]
633 # Will be mapped to Html::element()
634 $cases = [];
635
636 # ## Generic cases, match $attribDefault static array
637 $cases[] = [ '<area/>',
638 'area', [ 'shape' => 'rect' ]
639 ];
640
641 $cases[] = [ '<button type="submit"></button>',
642 'button', [ 'formaction' => 'GET' ]
643 ];
644 $cases[] = [ '<button type="submit"></button>',
645 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
646 ];
647
648 $cases[] = [ '<canvas></canvas>',
649 'canvas', [ 'height' => '150' ]
650 ];
651 $cases[] = [ '<canvas></canvas>',
652 'canvas', [ 'width' => '300' ]
653 ];
654 # Also check with numeric values
655 $cases[] = [ '<canvas></canvas>',
656 'canvas', [ 'height' => 150 ]
657 ];
658 $cases[] = [ '<canvas></canvas>',
659 'canvas', [ 'width' => 300 ]
660 ];
661
662 $cases[] = [ '<form></form>',
663 'form', [ 'action' => 'GET' ]
664 ];
665 $cases[] = [ '<form></form>',
666 'form', [ 'autocomplete' => 'on' ]
667 ];
668 $cases[] = [ '<form></form>',
669 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
670 ];
671
672 $cases[] = [ '<input/>',
673 'input', [ 'formaction' => 'GET' ]
674 ];
675 $cases[] = [ '<input/>',
676 'input', [ 'type' => 'text' ]
677 ];
678
679 $cases[] = [ '<keygen/>',
680 'keygen', [ 'keytype' => 'rsa' ]
681 ];
682
683 $cases[] = [ '<link/>',
684 'link', [ 'media' => 'all' ]
685 ];
686
687 $cases[] = [ '<menu></menu>',
688 'menu', [ 'type' => 'list' ]
689 ];
690
691 $cases[] = [ '<script></script>',
692 'script', [ 'type' => 'text/javascript' ]
693 ];
694
695 $cases[] = [ '<style></style>',
696 'style', [ 'media' => 'all' ]
697 ];
698 $cases[] = [ '<style></style>',
699 'style', [ 'type' => 'text/css' ]
700 ];
701
702 $cases[] = [ '<textarea></textarea>',
703 'textarea', [ 'wrap' => 'soft' ]
704 ];
705
706 # ## SPECIFIC CASES
707
708 # <link type="text/css">
709 $cases[] = [ '<link/>',
710 'link', [ 'type' => 'text/css' ]
711 ];
712
713 # <input> specific handling
714 $cases[] = [ '<input type="checkbox"/>',
715 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
716 'Default value "on" is stripped of checkboxes',
717 ];
718 $cases[] = [ '<input type="radio"/>',
719 'input', [ 'type' => 'radio', 'value' => 'on' ],
720 'Default value "on" is stripped of radio buttons',
721 ];
722 $cases[] = [ '<input type="submit" value="Submit"/>',
723 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
724 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
725 ];
726 $cases[] = [ '<input type="color"/>',
727 'input', [ 'type' => 'color', 'value' => '' ],
728 ];
729 $cases[] = [ '<input type="range"/>',
730 'input', [ 'type' => 'range', 'value' => '' ],
731 ];
732
733 # <button> specific handling
734 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
735 $cases[] = [ '<button type="submit"></button>',
736 'button', [ 'type' => 'submit' ],
737 'According to standard the default type is "submit". '
738 . 'Depending on compatibility mode IE might use "button", instead.',
739 ];
740
741 # <select> specific handling
742 $cases[] = [ '<select multiple=""></select>',
743 'select', [ 'size' => '4', 'multiple' => true ],
744 ];
745 # .. with numeric value
746 $cases[] = [ '<select multiple=""></select>',
747 'select', [ 'size' => 4, 'multiple' => true ],
748 ];
749 $cases[] = [ '<select></select>',
750 'select', [ 'size' => '1', 'multiple' => false ],
751 ];
752 # .. with numeric value
753 $cases[] = [ '<select></select>',
754 'select', [ 'size' => 1, 'multiple' => false ],
755 ];
756
757 # Passing an array as value
758 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
759 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
760 "dropDefaults accepts values given as an array"
761 ];
762
763 # FIXME: doDropDefault should remove defaults given in an array
764 # Expected should be '<a></a>'
765 $cases[] = [ '<a class=""></a>',
766 'a', [ 'class' => [ '', '' ] ],
767 "dropDefaults accepts values given as an array"
768 ];
769
770 # Craft the Html elements
771 $ret = [];
772 foreach ( $cases as $case ) {
773 $ret[] = [
774 $case[0],
775 $case[1], $case[2],
776 $case[3] ?? ''
777 ];
778 }
779
780 return $ret;
781 }
782
783 /**
784 * @covers Html::input
785 */
786 public function testWrapperInput() {
787 $this->assertEquals(
788 '<input type="radio" value="testval" name="testname"/>',
789 Html::input( 'testname', 'testval', 'radio' ),
790 'Input wrapper with type and value.'
791 );
792 $this->assertEquals(
793 '<input name="testname"/>',
794 Html::input( 'testname' ),
795 'Input wrapper with all default values.'
796 );
797 }
798
799 /**
800 * @covers Html::check
801 */
802 public function testWrapperCheck() {
803 $this->assertEquals(
804 '<input type="checkbox" value="1" name="testname"/>',
805 Html::check( 'testname' ),
806 'Checkbox wrapper unchecked.'
807 );
808 $this->assertEquals(
809 '<input checked="" type="checkbox" value="1" name="testname"/>',
810 Html::check( 'testname', true ),
811 'Checkbox wrapper checked.'
812 );
813 $this->assertEquals(
814 '<input type="checkbox" value="testval" name="testname"/>',
815 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
816 'Checkbox wrapper with a value override.'
817 );
818 }
819
820 /**
821 * @covers Html::radio
822 */
823 public function testWrapperRadio() {
824 $this->assertEquals(
825 '<input type="radio" value="1" name="testname"/>',
826 Html::radio( 'testname' ),
827 'Radio wrapper unchecked.'
828 );
829 $this->assertEquals(
830 '<input checked="" type="radio" value="1" name="testname"/>',
831 Html::radio( 'testname', true ),
832 'Radio wrapper checked.'
833 );
834 $this->assertEquals(
835 '<input type="radio" value="testval" name="testname"/>',
836 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
837 'Radio wrapper with a value override.'
838 );
839 }
840
841 /**
842 * @covers Html::label
843 */
844 public function testWrapperLabel() {
845 $this->assertEquals(
846 '<label for="testid">testlabel</label>',
847 Html::label( 'testlabel', 'testid' ),
848 'Label wrapper'
849 );
850 }
851
852 public static function provideSrcSetImages() {
853 return [
854 [ [], '', 'when there are no images, return empty string' ],
855 [
856 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
857 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
858 'pixel depth keys may include a trailing "x"'
859 ],
860 [
861 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
862 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
863 'pixel depth keys may omit a trailing "x"'
864 ],
865 [
866 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
867 'small.png 1x, large.png 1.5x',
868 'omit larger duplicates'
869 ],
870 [
871 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
872 'small.png 1x, large.png 1.5x',
873 'omit larger duplicates in irregular order'
874 ],
875 ];
876 }
877
878 /**
879 * @dataProvider provideSrcSetImages
880 * @covers Html::srcSet
881 */
882 public function testSrcSet( $images, $expected, $message ) {
883 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
884 }
885
886 public static function provideInlineScript() {
887 return [
888 'Empty' => [
889 '',
890 '<script></script>'
891 ],
892 'Simple' => [
893 'EXAMPLE.label("foo");',
894 '<script>EXAMPLE.label("foo");</script>'
895 ],
896 'Ampersand' => [
897 'EXAMPLE.is(a && b);',
898 '<script>EXAMPLE.is(a && b);</script>'
899 ],
900 'HTML' => [
901 'EXAMPLE.label("<a>");',
902 '<script>EXAMPLE.label("<a>");</script>'
903 ],
904 'Script closing string (lower)' => [
905 'EXAMPLE.label("</script>");',
906 '<script>/* ERROR: Invalid script */</script>',
907 true,
908 ],
909 'Script closing with non-standard attributes (mixed)' => [
910 'EXAMPLE.label("</SCriPT and STyLE>");',
911 '<script>/* ERROR: Invalid script */</script>',
912 true,
913 ],
914 'HTML-comment-open and script-open' => [
915 // In HTML, <script> contents aren't just plain CDATA until </script>,
916 // there are levels of escaping modes, and the below sequence puts an
917 // HTML parser in a state where </script> would *not* close the script.
918 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
919 'var a = "<!--<script>";',
920 '<script>/* ERROR: Invalid script */</script>',
921 true,
922 ],
923 ];
924 }
925
926 /**
927 * @dataProvider provideInlineScript
928 * @covers Html::inlineScript
929 */
930 public function testInlineScript( $code, $expected, $error = false ) {
931 if ( $error ) {
932 Wikimedia\suppressWarnings();
933 $this->restoreWarnings = true;
934 }
935 $this->assertSame( Html::inlineScript( $code ), $expected );
936 }
937 }
938
939 class HtmlTestValue {
940 function __toString() {
941 return 'stringValue';
942 }
943 }