2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
6 protected function setUp() {
10 'wgWellFormedXml' => false,
13 $langObj = Language
::factory( 'en' );
15 // Hardcode namespaces during test runs,
16 // so that html output based on existing namespaces
17 // can be properly evaluated.
18 $langObj->setNamespaces( [
30 9 => 'MediaWiki_talk',
32 11 => 'Template_talk',
34 15 => 'Category_talk',
38 $this->setUserLang( $langObj );
39 $this->setContentLang( $langObj );
43 * @covers Html::element
45 public function testElementBasics() {
48 Html
::element( 'img', null, '' ),
49 'No close tag for short-tag elements'
53 '<element></element>',
54 Html
::element( 'element', null, null ),
55 'Close tag for empty element (null, null)'
59 '<element></element>',
60 Html
::element( 'element', [], '' ),
61 'Close tag for empty element (array, string)'
64 $this->setMwGlobals( 'wgWellFormedXml', true );
68 Html
::element( 'img', null, '' ),
69 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
73 public function dataXmlMimeType() {
75 // ( $mimetype, $isXmlMimeType )
76 # HTML is not an XML MimeType
77 [ 'text/html', false ],
78 # XML is an XML MimeType
80 [ 'application/xml', true ],
81 # XHTML is an XML MimeType
82 [ 'application/xhtml+xml', true ],
83 # Make sure other +xml MimeTypes are supported
84 # SVG is another random MimeType even though we don't use it
85 [ 'image/svg+xml', true ],
86 # Complete random other MimeTypes are not XML
87 [ 'text/plain', false ],
92 * @dataProvider dataXmlMimeType
93 * @covers Html::isXmlMimeType
95 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
96 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
100 * @covers Html::expandAttributes
102 public function testExpandAttributesSkipsNullAndFalse() {
106 Html
::expandAttributes( [ 'foo' => null ] ),
107 'skip keys with null value'
110 Html
::expandAttributes( [ 'foo' => false ] ),
111 'skip keys with false value'
115 Html
::expandAttributes( [ 'foo' => '' ] ),
116 'keep keys with an empty string'
121 * @covers Html::expandAttributes
123 public function testExpandAttributesForBooleans() {
126 Html
::expandAttributes( [ 'selected' => false ] ),
127 'Boolean attributes do not generates output when value is false'
131 Html
::expandAttributes( [ 'selected' => null ] ),
132 'Boolean attributes do not generates output when value is null'
137 Html
::expandAttributes( [ 'selected' => true ] ),
138 'Boolean attributes have no value when value is true'
142 Html
::expandAttributes( [ 'selected' ] ),
143 'Boolean attributes have no value when value is true (passed as numerical array)'
146 $this->setMwGlobals( 'wgWellFormedXml', true );
150 Html
::expandAttributes( [ 'selected' => true ] ),
151 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
156 * @covers Html::expandAttributes
158 public function testExpandAttributesForNumbers() {
161 Html
::expandAttributes( [ 'value' => 1 ] ),
162 'Integer value is cast to a string'
166 Html
::expandAttributes( [ 'value' => 1.1 ] ),
167 'Float value is cast to a string'
172 * @covers Html::expandAttributes
174 public function testExpandAttributesForObjects() {
176 ' value=stringValue',
177 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
178 'Object value is converted to a string'
183 * Test for Html::expandAttributes()
184 * Please note it output a string prefixed with a space!
185 * @covers Html::expandAttributes
187 public function testExpandAttributesVariousExpansions() {
191 Html
::expandAttributes( [ 'empty_string' => '' ] ),
192 'Empty string is always quoted'
196 Html
::expandAttributes( [ 'key' => 'value' ] ),
197 'Simple string value needs no quotes'
201 Html
::expandAttributes( [ 'one' => 1 ] ),
202 'Number 1 value needs no quotes'
206 Html
::expandAttributes( [ 'zero' => 0 ] ),
207 'Number 0 value needs no quotes'
210 $this->setMwGlobals( 'wgWellFormedXml', true );
214 Html
::expandAttributes( [ 'empty_string' => '' ] ),
215 'Attribute values are always quoted (wgWellFormedXml): Empty string'
219 Html
::expandAttributes( [ 'key' => 'value' ] ),
220 'Attribute values are always quoted (wgWellFormedXml): Simple string'
224 Html
::expandAttributes( [ 'one' => 1 ] ),
225 'Attribute values are always quoted (wgWellFormedXml): Number 1'
229 Html
::expandAttributes( [ 'zero' => 0 ] ),
230 'Attribute values are always quoted (wgWellFormedXml): Number 0'
235 * Html::expandAttributes has special features for HTML
236 * attributes that use space separated lists and also
237 * allows arrays to be used as values.
238 * @covers Html::expandAttributes
240 public function testExpandAttributesListValueAttributes() {
243 ' class="redundant spaces here"',
244 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
245 'Normalization should strip redundant spaces'
249 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
250 'Normalization should remove duplicates in string-lists'
252 # ## "EMPTY" ARRAY VALUES
255 Html
::expandAttributes( [ 'class' => [] ] ),
256 'Value with an empty array'
260 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
261 'Array with null, empty string and spaces'
263 # ## NON-EMPTY ARRAY VALUES
266 Html
::expandAttributes( [ 'class' => [
273 'Normalization should remove duplicates in the array'
277 Html
::expandAttributes( [ 'class' => [
283 'Normalization should remove duplicates in string-lists in the array'
288 * Test feature added by r96188, let pass attributes values as
289 * a PHP array. Restricted to class,rel, accesskey.
290 * @covers Html::expandAttributes
292 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
294 ' class="booltrue one"',
295 Html
::expandAttributes( [ 'class' => [
299 # Method use isset() internally, make sure we do discard
300 # attributes values which have been assigned well known values
302 'boolfalse' => false,
310 * How do we handle duplicate keys in HTML attributes expansion?
311 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
312 * The later will take precedence.
314 * Feature added by r96188
315 * @covers Html::expandAttributes
317 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
320 Html
::expandAttributes( [ 'class' => [
329 * @covers Html::expandAttributes
330 * @expectedException MWException
332 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
333 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
334 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
335 Html
::expandAttributes( [
344 * @covers Html::namespaceSelector
346 public function testNamespaceSelector() {
348 '<select id=namespace name=namespace>' . "\n" .
349 '<option value=0>(Main)</option>' . "\n" .
350 '<option value=1>Talk</option>' . "\n" .
351 '<option value=2>User</option>' . "\n" .
352 '<option value=3>User talk</option>' . "\n" .
353 '<option value=4>MyWiki</option>' . "\n" .
354 '<option value=5>MyWiki Talk</option>' . "\n" .
355 '<option value=6>File</option>' . "\n" .
356 '<option value=7>File talk</option>' . "\n" .
357 '<option value=8>MediaWiki</option>' . "\n" .
358 '<option value=9>MediaWiki talk</option>' . "\n" .
359 '<option value=10>Template</option>' . "\n" .
360 '<option value=11>Template talk</option>' . "\n" .
361 '<option value=14>Category</option>' . "\n" .
362 '<option value=15>Category talk</option>' . "\n" .
363 '<option value=100>Custom</option>' . "\n" .
364 '<option value=101>Custom talk</option>' . "\n" .
366 Html
::namespaceSelector(),
367 'Basic namespace selector without custom options'
371 '<label for=mw-test-namespace>Select a namespace:</label> ' .
372 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
373 '<option value=all>all</option>' . "\n" .
374 '<option value=0>(Main)</option>' . "\n" .
375 '<option value=1>Talk</option>' . "\n" .
376 '<option value=2 selected>User</option>' . "\n" .
377 '<option value=3>User talk</option>' . "\n" .
378 '<option value=4>MyWiki</option>' . "\n" .
379 '<option value=5>MyWiki Talk</option>' . "\n" .
380 '<option value=6>File</option>' . "\n" .
381 '<option value=7>File talk</option>' . "\n" .
382 '<option value=8>MediaWiki</option>' . "\n" .
383 '<option value=9>MediaWiki talk</option>' . "\n" .
384 '<option value=10>Template</option>' . "\n" .
385 '<option value=11>Template talk</option>' . "\n" .
386 '<option value=14>Category</option>' . "\n" .
387 '<option value=15>Category talk</option>' . "\n" .
388 '<option value=100>Custom</option>' . "\n" .
389 '<option value=101>Custom talk</option>' . "\n" .
391 Html
::namespaceSelector(
392 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
393 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
395 'Basic namespace selector with custom values'
399 '<label for=namespace>Select a namespace:</label> ' .
400 '<select id=namespace name=namespace>' . "\n" .
401 '<option value=0>(Main)</option>' . "\n" .
402 '<option value=1>Talk</option>' . "\n" .
403 '<option value=2>User</option>' . "\n" .
404 '<option value=3>User talk</option>' . "\n" .
405 '<option value=4>MyWiki</option>' . "\n" .
406 '<option value=5>MyWiki Talk</option>' . "\n" .
407 '<option value=6>File</option>' . "\n" .
408 '<option value=7>File talk</option>' . "\n" .
409 '<option value=8>MediaWiki</option>' . "\n" .
410 '<option value=9>MediaWiki talk</option>' . "\n" .
411 '<option value=10>Template</option>' . "\n" .
412 '<option value=11>Template talk</option>' . "\n" .
413 '<option value=14>Category</option>' . "\n" .
414 '<option value=15>Category talk</option>' . "\n" .
415 '<option value=100>Custom</option>' . "\n" .
416 '<option value=101>Custom talk</option>' . "\n" .
418 Html
::namespaceSelector(
419 [ 'label' => 'Select a namespace:' ]
421 'Basic namespace selector with a custom label but no id attribtue for the <select>'
425 public function testCanFilterOutNamespaces() {
427 '<select id=namespace name=namespace>' . "\n" .
428 '<option value=2>User</option>' . "\n" .
429 '<option value=4>MyWiki</option>' . "\n" .
430 '<option value=5>MyWiki Talk</option>' . "\n" .
431 '<option value=6>File</option>' . "\n" .
432 '<option value=7>File talk</option>' . "\n" .
433 '<option value=8>MediaWiki</option>' . "\n" .
434 '<option value=9>MediaWiki talk</option>' . "\n" .
435 '<option value=10>Template</option>' . "\n" .
436 '<option value=11>Template talk</option>' . "\n" .
437 '<option value=14>Category</option>' . "\n" .
438 '<option value=15>Category talk</option>' . "\n" .
440 Html
::namespaceSelector(
441 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
443 'Namespace selector namespace filtering.'
447 public function testCanDisableANamespaces() {
449 '<select id=namespace name=namespace>' . "\n" .
450 '<option disabled value=0>(Main)</option>' . "\n" .
451 '<option disabled value=1>Talk</option>' . "\n" .
452 '<option disabled value=2>User</option>' . "\n" .
453 '<option disabled value=3>User talk</option>' . "\n" .
454 '<option disabled value=4>MyWiki</option>' . "\n" .
455 '<option value=5>MyWiki Talk</option>' . "\n" .
456 '<option value=6>File</option>' . "\n" .
457 '<option value=7>File talk</option>' . "\n" .
458 '<option value=8>MediaWiki</option>' . "\n" .
459 '<option value=9>MediaWiki talk</option>' . "\n" .
460 '<option value=10>Template</option>' . "\n" .
461 '<option value=11>Template talk</option>' . "\n" .
462 '<option value=14>Category</option>' . "\n" .
463 '<option value=15>Category talk</option>' . "\n" .
464 '<option value=100>Custom</option>' . "\n" .
465 '<option value=101>Custom talk</option>' . "\n" .
467 Html
::namespaceSelector( [
468 'disable' => [ 0, 1, 2, 3, 4 ]
470 'Namespace selector namespace disabling'
475 * @dataProvider provideHtml5InputTypes
476 * @covers Html::element
478 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
480 '<input type=' . $HTML5InputType . '>',
481 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
482 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
487 * List of input element types values introduced by HTML5
488 * Full list at http://www.w3.org/TR/html-markup/input.html
490 public static function provideHtml5InputTypes() {
507 foreach ( $types as $type ) {
508 $cases[] = [ $type ];
515 * Test out Html::element drops or enforces default value
516 * @covers Html::dropDefaults
517 * @dataProvider provideElementsWithAttributesHavingDefaultValues
519 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
520 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
523 public static function provideElementsWithAttributesHavingDefaultValues() {
524 # Use cases in a concise format:
525 # <expected>, <element name>, <array of attributes> [, <message>]
526 # Will be mapped to Html::element()
529 # ## Generic cases, match $attribDefault static array
530 $cases[] = [ '<area>',
531 'area', [ 'shape' => 'rect' ]
534 $cases[] = [ '<button type=submit></button>',
535 'button', [ 'formaction' => 'GET' ]
537 $cases[] = [ '<button type=submit></button>',
538 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
541 $cases[] = [ '<canvas></canvas>',
542 'canvas', [ 'height' => '150' ]
544 $cases[] = [ '<canvas></canvas>',
545 'canvas', [ 'width' => '300' ]
547 # Also check with numeric values
548 $cases[] = [ '<canvas></canvas>',
549 'canvas', [ 'height' => 150 ]
551 $cases[] = [ '<canvas></canvas>',
552 'canvas', [ 'width' => 300 ]
555 $cases[] = [ '<command>',
556 'command', [ 'type' => 'command' ]
559 $cases[] = [ '<form></form>',
560 'form', [ 'action' => 'GET' ]
562 $cases[] = [ '<form></form>',
563 'form', [ 'autocomplete' => 'on' ]
565 $cases[] = [ '<form></form>',
566 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
569 $cases[] = [ '<input>',
570 'input', [ 'formaction' => 'GET' ]
572 $cases[] = [ '<input>',
573 'input', [ 'type' => 'text' ]
576 $cases[] = [ '<keygen>',
577 'keygen', [ 'keytype' => 'rsa' ]
580 $cases[] = [ '<link>',
581 'link', [ 'media' => 'all' ]
584 $cases[] = [ '<menu></menu>',
585 'menu', [ 'type' => 'list' ]
588 $cases[] = [ '<script></script>',
589 'script', [ 'type' => 'text/javascript' ]
592 $cases[] = [ '<style></style>',
593 'style', [ 'media' => 'all' ]
595 $cases[] = [ '<style></style>',
596 'style', [ 'type' => 'text/css' ]
599 $cases[] = [ '<textarea></textarea>',
600 'textarea', [ 'wrap' => 'soft' ]
605 # <link type="text/css">
606 $cases[] = [ '<link>',
607 'link', [ 'type' => 'text/css' ]
610 # <input> specific handling
611 $cases[] = [ '<input type=checkbox>',
612 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
613 'Default value "on" is stripped of checkboxes',
615 $cases[] = [ '<input type=radio>',
616 'input', [ 'type' => 'radio', 'value' => 'on' ],
617 'Default value "on" is stripped of radio buttons',
619 $cases[] = [ '<input type=submit value=Submit>',
620 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
621 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
623 $cases[] = [ '<input type=color>',
624 'input', [ 'type' => 'color', 'value' => '' ],
626 $cases[] = [ '<input type=range>',
627 'input', [ 'type' => 'range', 'value' => '' ],
630 # <button> specific handling
631 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
632 $cases[] = [ '<button type=submit></button>',
633 'button', [ 'type' => 'submit' ],
634 'According to standard the default type is "submit". '
635 . 'Depending on compatibility mode IE might use "button", instead.',
638 # <select> specific handling
639 $cases[] = [ '<select multiple></select>',
640 'select', [ 'size' => '4', 'multiple' => true ],
642 # .. with numeric value
643 $cases[] = [ '<select multiple></select>',
644 'select', [ 'size' => 4, 'multiple' => true ],
646 $cases[] = [ '<select></select>',
647 'select', [ 'size' => '1', 'multiple' => false ],
649 # .. with numeric value
650 $cases[] = [ '<select></select>',
651 'select', [ 'size' => 1, 'multiple' => false ],
654 # Passing an array as value
655 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
656 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
657 "dropDefaults accepts values given as an array"
660 # FIXME: doDropDefault should remove defaults given in an array
661 # Expected should be '<a></a>'
662 $cases[] = [ '<a class=""></a>',
663 'a', [ 'class' => [ '', '' ] ],
664 "dropDefaults accepts values given as an array"
667 # Craft the Html elements
669 foreach ( $cases as $case ) {
673 isset( $case[3] ) ?
$case[3] : ''
681 * @covers Html::expandAttributes
683 public function testFormValidationBlacklist() {
685 Html
::expandAttributes( [
692 'Blacklist form validation attributes.'
696 Html
::expandAttributes(
704 'Allow special case "step=any".'
709 public function testWrapperInput() {
711 '<input type=radio value=testval name=testname>',
712 Html
::input( 'testname', 'testval', 'radio' ),
713 'Input wrapper with type and value.'
716 '<input name=testname>',
717 Html
::input( 'testname' ),
718 'Input wrapper with all default values.'
722 public function testWrapperCheck() {
724 '<input type=checkbox value=1 name=testname>',
725 Html
::check( 'testname' ),
726 'Checkbox wrapper unchecked.'
729 '<input checked type=checkbox value=1 name=testname>',
730 Html
::check( 'testname', true ),
731 'Checkbox wrapper checked.'
734 '<input type=checkbox value=testval name=testname>',
735 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
736 'Checkbox wrapper with a value override.'
740 public function testWrapperRadio() {
742 '<input type=radio value=1 name=testname>',
743 Html
::radio( 'testname' ),
744 'Radio wrapper unchecked.'
747 '<input checked type=radio value=1 name=testname>',
748 Html
::radio( 'testname', true ),
749 'Radio wrapper checked.'
752 '<input type=radio value=testval name=testname>',
753 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
754 'Radio wrapper with a value override.'
758 public function testWrapperLabel() {
760 '<label for=testid>testlabel</label>',
761 Html
::label( 'testlabel', 'testid' ),
766 public static function provideSrcSetImages() {
768 [ [], '', 'when there are no images, return empty string' ],
770 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
771 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
772 'pixel depth keys may include a trailing "x"'
775 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
776 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
777 'pixel depth keys may omit a trailing "x"'
783 * @dataProvider provideSrcSetImages
784 * @covers Html::srcSet
786 public function testSrcSet( $images, $expected, $message ) {
787 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
791 class HtmlTestValue
{
792 function __toString() {
793 return 'stringValue';