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