Add warning if you give Html::openElement a name with a space
[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->assertEquals(
165 '',
166 Html::expandAttributes( [ 'selected' => false ] ),
167 'Boolean attributes do not generates output when value is false'
168 );
169 $this->assertEquals(
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 * Test for Html::expandAttributes()
216 * Please note it output a string prefixed with a space!
217 * @covers Html::expandAttributes
218 */
219 public function testExpandAttributesVariousExpansions() {
220 # ## NOT EMPTY ####
221 $this->assertEquals(
222 ' empty_string=""',
223 Html::expandAttributes( [ 'empty_string' => '' ] ),
224 'Empty string is always quoted'
225 );
226 $this->assertEquals(
227 ' key="value"',
228 Html::expandAttributes( [ 'key' => 'value' ] ),
229 'Simple string value needs no quotes'
230 );
231 $this->assertEquals(
232 ' one="1"',
233 Html::expandAttributes( [ 'one' => 1 ] ),
234 'Number 1 value needs no quotes'
235 );
236 $this->assertEquals(
237 ' zero="0"',
238 Html::expandAttributes( [ 'zero' => 0 ] ),
239 'Number 0 value needs no quotes'
240 );
241 }
242
243 /**
244 * Html::expandAttributes has special features for HTML
245 * attributes that use space separated lists and also
246 * allows arrays to be used as values.
247 * @covers Html::expandAttributes
248 */
249 public function testExpandAttributesListValueAttributes() {
250 # ## STRING VALUES
251 $this->assertEquals(
252 ' class="redundant spaces here"',
253 Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
254 'Normalization should strip redundant spaces'
255 );
256 $this->assertEquals(
257 ' class="foo bar"',
258 Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
259 'Normalization should remove duplicates in string-lists'
260 );
261 # ## "EMPTY" ARRAY VALUES
262 $this->assertEquals(
263 ' class=""',
264 Html::expandAttributes( [ 'class' => [] ] ),
265 'Value with an empty array'
266 );
267 $this->assertEquals(
268 ' class=""',
269 Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
270 'Array with null, empty string and spaces'
271 );
272 # ## NON-EMPTY ARRAY VALUES
273 $this->assertEquals(
274 ' class="foo bar"',
275 Html::expandAttributes( [ 'class' => [
276 'foo',
277 'bar',
278 'foo',
279 'bar',
280 'bar',
281 ] ] ),
282 'Normalization should remove duplicates in the array'
283 );
284 $this->assertEquals(
285 ' class="foo bar"',
286 Html::expandAttributes( [ 'class' => [
287 'foo bar',
288 'bar foo',
289 'foo',
290 'bar bar',
291 ] ] ),
292 'Normalization should remove duplicates in string-lists in the array'
293 );
294 }
295
296 /**
297 * Test feature added by r96188, let pass attributes values as
298 * a PHP array. Restricted to class,rel, accesskey.
299 * @covers Html::expandAttributes
300 */
301 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
302 $this->assertEquals(
303 ' class="booltrue one"',
304 Html::expandAttributes( [ 'class' => [
305 'booltrue' => true,
306 'one' => 1,
307
308 # Method use isset() internally, make sure we do discard
309 # attributes values which have been assigned well known values
310 'emptystring' => '',
311 'boolfalse' => false,
312 'zero' => 0,
313 'null' => null,
314 ] ] )
315 );
316 }
317
318 /**
319 * How do we handle duplicate keys in HTML attributes expansion?
320 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
321 * The latter will take precedence.
322 *
323 * Feature added by r96188
324 * @covers Html::expandAttributes
325 */
326 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
327 $this->assertEquals(
328 ' class=""',
329 Html::expandAttributes( [ 'class' => [
330 'GREEN',
331 'GREEN' => false,
332 'GREEN',
333 ] ] )
334 );
335 }
336
337 /**
338 * @covers Html::expandAttributes
339 * @expectedException MWException
340 */
341 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
342 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
343 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
344 Html::expandAttributes( [
345 'src' => [
346 'ltr' => 'ltr.svg',
347 'rtl' => 'rtl.svg'
348 ]
349 ] );
350 }
351
352 /**
353 * @covers Html::namespaceSelector
354 * @covers Html::namespaceSelectorOptions
355 */
356 public function testNamespaceSelector() {
357 $this->assertEquals(
358 '<select id="namespace" name="namespace">' . "\n" .
359 '<option value="0">(Principal)</option>' . "\n" .
360 '<option value="1">Talk</option>' . "\n" .
361 '<option value="2">User</option>' . "\n" .
362 '<option value="3">User talk</option>' . "\n" .
363 '<option value="4">MyWiki</option>' . "\n" .
364 '<option value="5">MyWiki Talk</option>' . "\n" .
365 '<option value="6">File</option>' . "\n" .
366 '<option value="7">File talk</option>' . "\n" .
367 '<option value="8">MediaWiki</option>' . "\n" .
368 '<option value="9">MediaWiki talk</option>' . "\n" .
369 '<option value="10">Template</option>' . "\n" .
370 '<option value="11">Template talk</option>' . "\n" .
371 '<option value="14">Category</option>' . "\n" .
372 '<option value="15">Category talk</option>' . "\n" .
373 '<option value="100">Custom</option>' . "\n" .
374 '<option value="101">Custom talk</option>' . "\n" .
375 '</select>',
376 Html::namespaceSelector(),
377 'Basic namespace selector without custom options'
378 );
379
380 $this->assertEquals(
381 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
382 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
383 '<option value="all">todos</option>' . "\n" .
384 '<option value="0">(Principal)</option>' . "\n" .
385 '<option value="1">Talk</option>' . "\n" .
386 '<option value="2" selected="">User</option>' . "\n" .
387 '<option value="3">User talk</option>' . "\n" .
388 '<option value="4">MyWiki</option>' . "\n" .
389 '<option value="5">MyWiki Talk</option>' . "\n" .
390 '<option value="6">File</option>' . "\n" .
391 '<option value="7">File talk</option>' . "\n" .
392 '<option value="8">MediaWiki</option>' . "\n" .
393 '<option value="9">MediaWiki talk</option>' . "\n" .
394 '<option value="10">Template</option>' . "\n" .
395 '<option value="11">Template talk</option>' . "\n" .
396 '<option value="14">Category</option>' . "\n" .
397 '<option value="15">Category talk</option>' . "\n" .
398 '<option value="100">Custom</option>' . "\n" .
399 '<option value="101">Custom talk</option>' . "\n" .
400 '</select>',
401 Html::namespaceSelector(
402 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
403 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
404 ),
405 'Basic namespace selector with custom values'
406 );
407
408 $this->assertEquals(
409 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
410 '<select id="namespace" name="namespace">' . "\n" .
411 '<option value="0">(Principal)</option>' . "\n" .
412 '<option value="1">Talk</option>' . "\n" .
413 '<option value="2">User</option>' . "\n" .
414 '<option value="3">User talk</option>' . "\n" .
415 '<option value="4">MyWiki</option>' . "\n" .
416 '<option value="5">MyWiki Talk</option>' . "\n" .
417 '<option value="6">File</option>' . "\n" .
418 '<option value="7">File talk</option>' . "\n" .
419 '<option value="8">MediaWiki</option>' . "\n" .
420 '<option value="9">MediaWiki talk</option>' . "\n" .
421 '<option value="10">Template</option>' . "\n" .
422 '<option value="11">Template talk</option>' . "\n" .
423 '<option value="14">Category</option>' . "\n" .
424 '<option value="15">Category talk</option>' . "\n" .
425 '<option value="100">Custom</option>' . "\n" .
426 '<option value="101">Custom talk</option>' . "\n" .
427 '</select>',
428 Html::namespaceSelector(
429 [ 'label' => 'Select a namespace:' ]
430 ),
431 'Basic namespace selector with a custom label but no id attribtue for the <select>'
432 );
433
434 $this->assertEquals(
435 '<select id="namespace" name="namespace">' . "\n" .
436 '<option value="0">(Principal)</option>' . "\n" .
437 '<option value="1">Discusión</option>' . "\n" .
438 '<option value="2">Usuario</option>' . "\n" .
439 '<option value="3">Usuario discusión</option>' . "\n" .
440 '<option value="4">Wiki</option>' . "\n" .
441 '<option value="5">Wiki discusión</option>' . "\n" .
442 '<option value="6">Archivo</option>' . "\n" .
443 '<option value="7">Archivo discusión</option>' . "\n" .
444 '<option value="8">MediaWiki</option>' . "\n" .
445 '<option value="9">MediaWiki discusión</option>' . "\n" .
446 '<option value="10">Plantilla</option>' . "\n" .
447 '<option value="11">Plantilla discusión</option>' . "\n" .
448 '<option value="12">Ayuda</option>' . "\n" .
449 '<option value="13">Ayuda discusión</option>' . "\n" .
450 '<option value="14">Categoría</option>' . "\n" .
451 '<option value="15">Categoría discusión</option>' . "\n" .
452 '<option value="100">Personalizado</option>' . "\n" .
453 '<option value="101">Personalizado discusión</option>' . "\n" .
454 '</select>',
455 Html::namespaceSelector(
456 [ 'in-user-lang' => true ]
457 ),
458 'Basic namespace selector in user language'
459 );
460 }
461
462 /**
463 * @covers Html::namespaceSelector
464 */
465 public function testCanFilterOutNamespaces() {
466 $this->assertEquals(
467 '<select id="namespace" name="namespace">' . "\n" .
468 '<option value="2">User</option>' . "\n" .
469 '<option value="4">MyWiki</option>' . "\n" .
470 '<option value="5">MyWiki Talk</option>' . "\n" .
471 '<option value="6">File</option>' . "\n" .
472 '<option value="7">File talk</option>' . "\n" .
473 '<option value="8">MediaWiki</option>' . "\n" .
474 '<option value="9">MediaWiki talk</option>' . "\n" .
475 '<option value="10">Template</option>' . "\n" .
476 '<option value="11">Template talk</option>' . "\n" .
477 '<option value="14">Category</option>' . "\n" .
478 '<option value="15">Category talk</option>' . "\n" .
479 '</select>',
480 Html::namespaceSelector(
481 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
482 ),
483 'Namespace selector namespace filtering.'
484 );
485 }
486
487 /**
488 * @covers Html::namespaceSelector
489 */
490 public function testCanDisableANamespaces() {
491 $this->assertEquals(
492 '<select id="namespace" name="namespace">' . "\n" .
493 '<option disabled="" value="0">(Principal)</option>' . "\n" .
494 '<option disabled="" value="1">Talk</option>' . "\n" .
495 '<option disabled="" value="2">User</option>' . "\n" .
496 '<option disabled="" value="3">User talk</option>' . "\n" .
497 '<option disabled="" value="4">MyWiki</option>' . "\n" .
498 '<option value="5">MyWiki Talk</option>' . "\n" .
499 '<option value="6">File</option>' . "\n" .
500 '<option value="7">File talk</option>' . "\n" .
501 '<option value="8">MediaWiki</option>' . "\n" .
502 '<option value="9">MediaWiki talk</option>' . "\n" .
503 '<option value="10">Template</option>' . "\n" .
504 '<option value="11">Template talk</option>' . "\n" .
505 '<option value="14">Category</option>' . "\n" .
506 '<option value="15">Category talk</option>' . "\n" .
507 '<option value="100">Custom</option>' . "\n" .
508 '<option value="101">Custom talk</option>' . "\n" .
509 '</select>',
510 Html::namespaceSelector( [
511 'disable' => [ 0, 1, 2, 3, 4 ]
512 ] ),
513 'Namespace selector namespace disabling'
514 );
515 }
516
517 /**
518 * @dataProvider provideHtml5InputTypes
519 * @covers Html::element
520 */
521 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
522 $this->assertEquals(
523 '<input type="' . $HTML5InputType . '"/>',
524 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
525 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
526 );
527 }
528
529 /**
530 * @covers Html::warningBox
531 * @covers Html::messageBox
532 */
533 public function testWarningBox() {
534 $this->assertEquals(
535 Html::warningBox( 'warn' ),
536 '<div class="warningbox">warn</div>'
537 );
538 }
539
540 /**
541 * @covers Html::errorBox
542 * @covers Html::messageBox
543 */
544 public function testErrorBox() {
545 $this->assertEquals(
546 Html::errorBox( 'err' ),
547 '<div class="errorbox">err</div>'
548 );
549 $this->assertEquals(
550 Html::errorBox( 'err', 'heading' ),
551 '<div class="errorbox"><h2>heading</h2>err</div>'
552 );
553 $this->assertEquals(
554 Html::errorBox( 'err', '0' ),
555 '<div class="errorbox"><h2>0</h2>err</div>'
556 );
557 }
558
559 /**
560 * @covers Html::successBox
561 * @covers Html::messageBox
562 */
563 public function testSuccessBox() {
564 $this->assertEquals(
565 Html::successBox( 'great' ),
566 '<div class="successbox">great</div>'
567 );
568 $this->assertEquals(
569 Html::successBox( '<script>beware no escaping!</script>' ),
570 '<div class="successbox"><script>beware no escaping!</script></div>'
571 );
572 }
573
574 /**
575 * List of input element types values introduced by HTML5
576 * Full list at https://www.w3.org/TR/html-markup/input.html
577 */
578 public static function provideHtml5InputTypes() {
579 $types = [
580 'datetime',
581 'datetime-local',
582 'date',
583 'month',
584 'time',
585 'week',
586 'number',
587 'range',
588 'email',
589 'url',
590 'search',
591 'tel',
592 'color',
593 ];
594 $cases = [];
595 foreach ( $types as $type ) {
596 $cases[] = [ $type ];
597 }
598
599 return $cases;
600 }
601
602 /**
603 * Test out Html::element drops or enforces default value
604 * @covers Html::dropDefaults
605 * @dataProvider provideElementsWithAttributesHavingDefaultValues
606 */
607 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
608 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
609 }
610
611 public static function provideElementsWithAttributesHavingDefaultValues() {
612 # Use cases in a concise format:
613 # <expected>, <element name>, <array of attributes> [, <message>]
614 # Will be mapped to Html::element()
615 $cases = [];
616
617 # ## Generic cases, match $attribDefault static array
618 $cases[] = [ '<area/>',
619 'area', [ 'shape' => 'rect' ]
620 ];
621
622 $cases[] = [ '<button type="submit"></button>',
623 'button', [ 'formaction' => 'GET' ]
624 ];
625 $cases[] = [ '<button type="submit"></button>',
626 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
627 ];
628
629 $cases[] = [ '<canvas></canvas>',
630 'canvas', [ 'height' => '150' ]
631 ];
632 $cases[] = [ '<canvas></canvas>',
633 'canvas', [ 'width' => '300' ]
634 ];
635 # Also check with numeric values
636 $cases[] = [ '<canvas></canvas>',
637 'canvas', [ 'height' => 150 ]
638 ];
639 $cases[] = [ '<canvas></canvas>',
640 'canvas', [ 'width' => 300 ]
641 ];
642
643 $cases[] = [ '<form></form>',
644 'form', [ 'action' => 'GET' ]
645 ];
646 $cases[] = [ '<form></form>',
647 'form', [ 'autocomplete' => 'on' ]
648 ];
649 $cases[] = [ '<form></form>',
650 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
651 ];
652
653 $cases[] = [ '<input/>',
654 'input', [ 'formaction' => 'GET' ]
655 ];
656 $cases[] = [ '<input/>',
657 'input', [ 'type' => 'text' ]
658 ];
659
660 $cases[] = [ '<keygen/>',
661 'keygen', [ 'keytype' => 'rsa' ]
662 ];
663
664 $cases[] = [ '<link/>',
665 'link', [ 'media' => 'all' ]
666 ];
667
668 $cases[] = [ '<menu></menu>',
669 'menu', [ 'type' => 'list' ]
670 ];
671
672 $cases[] = [ '<script></script>',
673 'script', [ 'type' => 'text/javascript' ]
674 ];
675
676 $cases[] = [ '<style></style>',
677 'style', [ 'media' => 'all' ]
678 ];
679 $cases[] = [ '<style></style>',
680 'style', [ 'type' => 'text/css' ]
681 ];
682
683 $cases[] = [ '<textarea></textarea>',
684 'textarea', [ 'wrap' => 'soft' ]
685 ];
686
687 # ## SPECIFIC CASES
688
689 # <link type="text/css">
690 $cases[] = [ '<link/>',
691 'link', [ 'type' => 'text/css' ]
692 ];
693
694 # <input> specific handling
695 $cases[] = [ '<input type="checkbox"/>',
696 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
697 'Default value "on" is stripped of checkboxes',
698 ];
699 $cases[] = [ '<input type="radio"/>',
700 'input', [ 'type' => 'radio', 'value' => 'on' ],
701 'Default value "on" is stripped of radio buttons',
702 ];
703 $cases[] = [ '<input type="submit" value="Submit"/>',
704 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
705 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
706 ];
707 $cases[] = [ '<input type="color"/>',
708 'input', [ 'type' => 'color', 'value' => '' ],
709 ];
710 $cases[] = [ '<input type="range"/>',
711 'input', [ 'type' => 'range', 'value' => '' ],
712 ];
713
714 # <button> specific handling
715 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
716 $cases[] = [ '<button type="submit"></button>',
717 'button', [ 'type' => 'submit' ],
718 'According to standard the default type is "submit". '
719 . 'Depending on compatibility mode IE might use "button", instead.',
720 ];
721
722 # <select> specific handling
723 $cases[] = [ '<select multiple=""></select>',
724 'select', [ 'size' => '4', 'multiple' => true ],
725 ];
726 # .. with numeric value
727 $cases[] = [ '<select multiple=""></select>',
728 'select', [ 'size' => 4, 'multiple' => true ],
729 ];
730 $cases[] = [ '<select></select>',
731 'select', [ 'size' => '1', 'multiple' => false ],
732 ];
733 # .. with numeric value
734 $cases[] = [ '<select></select>',
735 'select', [ 'size' => 1, 'multiple' => false ],
736 ];
737
738 # Passing an array as value
739 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
740 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
741 "dropDefaults accepts values given as an array"
742 ];
743
744 # FIXME: doDropDefault should remove defaults given in an array
745 # Expected should be '<a></a>'
746 $cases[] = [ '<a class=""></a>',
747 'a', [ 'class' => [ '', '' ] ],
748 "dropDefaults accepts values given as an array"
749 ];
750
751 # Craft the Html elements
752 $ret = [];
753 foreach ( $cases as $case ) {
754 $ret[] = [
755 $case[0],
756 $case[1], $case[2],
757 $case[3] ?? ''
758 ];
759 }
760
761 return $ret;
762 }
763
764 /**
765 * @covers Html::input
766 */
767 public function testWrapperInput() {
768 $this->assertEquals(
769 '<input type="radio" value="testval" name="testname"/>',
770 Html::input( 'testname', 'testval', 'radio' ),
771 'Input wrapper with type and value.'
772 );
773 $this->assertEquals(
774 '<input name="testname"/>',
775 Html::input( 'testname' ),
776 'Input wrapper with all default values.'
777 );
778 }
779
780 /**
781 * @covers Html::check
782 */
783 public function testWrapperCheck() {
784 $this->assertEquals(
785 '<input type="checkbox" value="1" name="testname"/>',
786 Html::check( 'testname' ),
787 'Checkbox wrapper unchecked.'
788 );
789 $this->assertEquals(
790 '<input checked="" type="checkbox" value="1" name="testname"/>',
791 Html::check( 'testname', true ),
792 'Checkbox wrapper checked.'
793 );
794 $this->assertEquals(
795 '<input type="checkbox" value="testval" name="testname"/>',
796 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
797 'Checkbox wrapper with a value override.'
798 );
799 }
800
801 /**
802 * @covers Html::radio
803 */
804 public function testWrapperRadio() {
805 $this->assertEquals(
806 '<input type="radio" value="1" name="testname"/>',
807 Html::radio( 'testname' ),
808 'Radio wrapper unchecked.'
809 );
810 $this->assertEquals(
811 '<input checked="" type="radio" value="1" name="testname"/>',
812 Html::radio( 'testname', true ),
813 'Radio wrapper checked.'
814 );
815 $this->assertEquals(
816 '<input type="radio" value="testval" name="testname"/>',
817 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
818 'Radio wrapper with a value override.'
819 );
820 }
821
822 /**
823 * @covers Html::label
824 */
825 public function testWrapperLabel() {
826 $this->assertEquals(
827 '<label for="testid">testlabel</label>',
828 Html::label( 'testlabel', 'testid' ),
829 'Label wrapper'
830 );
831 }
832
833 public static function provideSrcSetImages() {
834 return [
835 [ [], '', 'when there are no images, return empty string' ],
836 [
837 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
838 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
839 'pixel depth keys may include a trailing "x"'
840 ],
841 [
842 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
843 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
844 'pixel depth keys may omit a trailing "x"'
845 ],
846 [
847 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
848 'small.png 1x, large.png 1.5x',
849 'omit larger duplicates'
850 ],
851 [
852 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
853 'small.png 1x, large.png 1.5x',
854 'omit larger duplicates in irregular order'
855 ],
856 ];
857 }
858
859 /**
860 * @dataProvider provideSrcSetImages
861 * @covers Html::srcSet
862 */
863 public function testSrcSet( $images, $expected, $message ) {
864 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
865 }
866
867 public static function provideInlineScript() {
868 return [
869 'Empty' => [
870 '',
871 '<script></script>'
872 ],
873 'Simple' => [
874 'EXAMPLE.label("foo");',
875 '<script>EXAMPLE.label("foo");</script>'
876 ],
877 'Ampersand' => [
878 'EXAMPLE.is(a && b);',
879 '<script>EXAMPLE.is(a && b);</script>'
880 ],
881 'HTML' => [
882 'EXAMPLE.label("<a>");',
883 '<script>EXAMPLE.label("<a>");</script>'
884 ],
885 'Script closing string (lower)' => [
886 'EXAMPLE.label("</script>");',
887 '<script>/* ERROR: Invalid script */</script>',
888 true,
889 ],
890 'Script closing with non-standard attributes (mixed)' => [
891 'EXAMPLE.label("</SCriPT and STyLE>");',
892 '<script>/* ERROR: Invalid script */</script>',
893 true,
894 ],
895 'HTML-comment-open and script-open' => [
896 // In HTML, <script> contents aren't just plain CDATA until </script>,
897 // there are levels of escaping modes, and the below sequence puts an
898 // HTML parser in a state where </script> would *not* close the script.
899 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
900 'var a = "<!--<script>";',
901 '<script>/* ERROR: Invalid script */</script>',
902 true,
903 ],
904 ];
905 }
906
907 /**
908 * @dataProvider provideInlineScript
909 * @covers Html::inlineScript
910 */
911 public function testInlineScript( $code, $expected, $error = false ) {
912 if ( $error ) {
913 Wikimedia\suppressWarnings();
914 $this->restoreWarnings = true;
915 }
916 $this->assertSame( Html::inlineScript( $code ), $expected );
917 }
918 }
919
920 class HtmlTestValue {
921 function __toString() {
922 return 'stringValue';
923 }
924 }