2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
6 protected function setUp() {
10 'wgUseMediaWikiUIEverywhere' => 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)'
66 Html
::element( 'img', null, '' ),
67 'Self-closing tag for short-tag elements'
71 public function dataXmlMimeType() {
73 // ( $mimetype, $isXmlMimeType )
74 # HTML is not an XML MimeType
75 [ 'text/html', false ],
76 # XML is an XML MimeType
78 [ 'application/xml', true ],
79 # XHTML is an XML MimeType
80 [ 'application/xhtml+xml', true ],
81 # Make sure other +xml MimeTypes are supported
82 # SVG is another random MimeType even though we don't use it
83 [ 'image/svg+xml', true ],
84 # Complete random other MimeTypes are not XML
85 [ 'text/plain', false ],
90 * @dataProvider dataXmlMimeType
91 * @covers Html::isXmlMimeType
93 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
94 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
98 * @covers Html::expandAttributes
100 public function testExpandAttributesSkipsNullAndFalse() {
104 Html
::expandAttributes( [ 'foo' => null ] ),
105 'skip keys with null value'
108 Html
::expandAttributes( [ 'foo' => false ] ),
109 'skip keys with false value'
113 Html
::expandAttributes( [ 'foo' => '' ] ),
114 'keep keys with an empty string'
119 * @covers Html::expandAttributes
121 public function testExpandAttributesForBooleans() {
124 Html
::expandAttributes( [ 'selected' => false ] ),
125 'Boolean attributes do not generates output when value is false'
129 Html
::expandAttributes( [ 'selected' => null ] ),
130 'Boolean attributes do not generates output when value is null'
135 Html
::expandAttributes( [ 'selected' => true ] ),
136 'Boolean attributes have no value when value is true'
140 Html
::expandAttributes( [ 'selected' ] ),
141 'Boolean attributes have no value when value is true (passed as numerical array)'
146 Html
::expandAttributes( [ 'selected' => true ] ),
147 'Boolean attributes have empty string value when value is true'
152 * @covers Html::expandAttributes
154 public function testExpandAttributesForNumbers() {
157 Html
::expandAttributes( [ 'value' => 1 ] ),
158 'Integer value is cast to a string'
162 Html
::expandAttributes( [ 'value' => 1.1 ] ),
163 'Float value is cast to a string'
168 * @covers Html::expandAttributes
170 public function testExpandAttributesForObjects() {
172 ' value="stringValue"',
173 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
174 'Object value is converted to a string'
179 * Test for Html::expandAttributes()
180 * Please note it output a string prefixed with a space!
181 * @covers Html::expandAttributes
183 public function testExpandAttributesVariousExpansions() {
187 Html
::expandAttributes( [ 'empty_string' => '' ] ),
188 'Empty string is always quoted'
192 Html
::expandAttributes( [ 'key' => 'value' ] ),
193 'Simple string value needs no quotes'
197 Html
::expandAttributes( [ 'one' => 1 ] ),
198 'Number 1 value needs no quotes'
202 Html
::expandAttributes( [ 'zero' => 0 ] ),
203 'Number 0 value needs no quotes'
209 * Html::expandAttributes has special features for HTML
210 * attributes that use space separated lists and also
211 * allows arrays to be used as values.
212 * @covers Html::expandAttributes
214 public function testExpandAttributesListValueAttributes() {
217 ' class="redundant spaces here"',
218 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
219 'Normalization should strip redundant spaces'
223 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
224 'Normalization should remove duplicates in string-lists'
226 # ## "EMPTY" ARRAY VALUES
229 Html
::expandAttributes( [ 'class' => [] ] ),
230 'Value with an empty array'
234 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
235 'Array with null, empty string and spaces'
237 # ## NON-EMPTY ARRAY VALUES
240 Html
::expandAttributes( [ 'class' => [
247 'Normalization should remove duplicates in the array'
251 Html
::expandAttributes( [ 'class' => [
257 'Normalization should remove duplicates in string-lists in the array'
262 * Test feature added by r96188, let pass attributes values as
263 * a PHP array. Restricted to class,rel, accesskey.
264 * @covers Html::expandAttributes
266 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
268 ' class="booltrue one"',
269 Html
::expandAttributes( [ 'class' => [
273 # Method use isset() internally, make sure we do discard
274 # attributes values which have been assigned well known values
276 'boolfalse' => false,
284 * How do we handle duplicate keys in HTML attributes expansion?
285 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
286 * The later will take precedence.
288 * Feature added by r96188
289 * @covers Html::expandAttributes
291 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
294 Html
::expandAttributes( [ 'class' => [
303 * @covers Html::expandAttributes
304 * @expectedException MWException
306 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
307 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
308 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
309 Html
::expandAttributes( [
318 * @covers Html::namespaceSelector
320 public function testNamespaceSelector() {
322 '<select id="namespace" name="namespace">' . "\n" .
323 '<option value="0">(Main)</option>' . "\n" .
324 '<option value="1">Talk</option>' . "\n" .
325 '<option value="2">User</option>' . "\n" .
326 '<option value="3">User talk</option>' . "\n" .
327 '<option value="4">MyWiki</option>' . "\n" .
328 '<option value="5">MyWiki Talk</option>' . "\n" .
329 '<option value="6">File</option>' . "\n" .
330 '<option value="7">File talk</option>' . "\n" .
331 '<option value="8">MediaWiki</option>' . "\n" .
332 '<option value="9">MediaWiki talk</option>' . "\n" .
333 '<option value="10">Template</option>' . "\n" .
334 '<option value="11">Template talk</option>' . "\n" .
335 '<option value="14">Category</option>' . "\n" .
336 '<option value="15">Category talk</option>' . "\n" .
337 '<option value="100">Custom</option>' . "\n" .
338 '<option value="101">Custom talk</option>' . "\n" .
340 Html
::namespaceSelector(),
341 'Basic namespace selector without custom options'
345 '<label for="mw-test-namespace">Select a namespace:</label> ' .
346 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
347 '<option value="all">all</option>' . "\n" .
348 '<option value="0">(Main)</option>' . "\n" .
349 '<option value="1">Talk</option>' . "\n" .
350 '<option value="2" selected="">User</option>' . "\n" .
351 '<option value="3">User talk</option>' . "\n" .
352 '<option value="4">MyWiki</option>' . "\n" .
353 '<option value="5">MyWiki Talk</option>' . "\n" .
354 '<option value="6">File</option>' . "\n" .
355 '<option value="7">File talk</option>' . "\n" .
356 '<option value="8">MediaWiki</option>' . "\n" .
357 '<option value="9">MediaWiki talk</option>' . "\n" .
358 '<option value="10">Template</option>' . "\n" .
359 '<option value="11">Template talk</option>' . "\n" .
360 '<option value="14">Category</option>' . "\n" .
361 '<option value="15">Category talk</option>' . "\n" .
362 '<option value="100">Custom</option>' . "\n" .
363 '<option value="101">Custom talk</option>' . "\n" .
365 Html
::namespaceSelector(
366 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
367 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
369 'Basic namespace selector with custom values'
373 '<label for="namespace">Select a namespace:</label> ' .
374 '<select id="namespace" name="namespace">' . "\n" .
375 '<option value="0">(Main)</option>' . "\n" .
376 '<option value="1">Talk</option>' . "\n" .
377 '<option value="2">User</option>' . "\n" .
378 '<option value="3">User talk</option>' . "\n" .
379 '<option value="4">MyWiki</option>' . "\n" .
380 '<option value="5">MyWiki Talk</option>' . "\n" .
381 '<option value="6">File</option>' . "\n" .
382 '<option value="7">File talk</option>' . "\n" .
383 '<option value="8">MediaWiki</option>' . "\n" .
384 '<option value="9">MediaWiki talk</option>' . "\n" .
385 '<option value="10">Template</option>' . "\n" .
386 '<option value="11">Template talk</option>' . "\n" .
387 '<option value="14">Category</option>' . "\n" .
388 '<option value="15">Category talk</option>' . "\n" .
389 '<option value="100">Custom</option>' . "\n" .
390 '<option value="101">Custom talk</option>' . "\n" .
392 Html
::namespaceSelector(
393 [ 'label' => 'Select a namespace:' ]
395 'Basic namespace selector with a custom label but no id attribtue for the <select>'
399 public function testCanFilterOutNamespaces() {
401 '<select id="namespace" name="namespace">' . "\n" .
402 '<option value="2">User</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" .
414 Html
::namespaceSelector(
415 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
417 'Namespace selector namespace filtering.'
421 public function testCanDisableANamespaces() {
423 '<select id="namespace" name="namespace">' . "\n" .
424 '<option disabled="" value="0">(Main)</option>' . "\n" .
425 '<option disabled="" value="1">Talk</option>' . "\n" .
426 '<option disabled="" value="2">User</option>' . "\n" .
427 '<option disabled="" value="3">User talk</option>' . "\n" .
428 '<option disabled="" value="4">MyWiki</option>' . "\n" .
429 '<option value="5">MyWiki Talk</option>' . "\n" .
430 '<option value="6">File</option>' . "\n" .
431 '<option value="7">File talk</option>' . "\n" .
432 '<option value="8">MediaWiki</option>' . "\n" .
433 '<option value="9">MediaWiki talk</option>' . "\n" .
434 '<option value="10">Template</option>' . "\n" .
435 '<option value="11">Template talk</option>' . "\n" .
436 '<option value="14">Category</option>' . "\n" .
437 '<option value="15">Category talk</option>' . "\n" .
438 '<option value="100">Custom</option>' . "\n" .
439 '<option value="101">Custom talk</option>' . "\n" .
441 Html
::namespaceSelector( [
442 'disable' => [ 0, 1, 2, 3, 4 ]
444 'Namespace selector namespace disabling'
449 * @dataProvider provideHtml5InputTypes
450 * @covers Html::element
452 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
454 '<input type="' . $HTML5InputType . '"/>',
455 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
456 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
461 * List of input element types values introduced by HTML5
462 * Full list at http://www.w3.org/TR/html-markup/input.html
464 public static function provideHtml5InputTypes() {
481 foreach ( $types as $type ) {
482 $cases[] = [ $type ];
489 * Test out Html::element drops or enforces default value
490 * @covers Html::dropDefaults
491 * @dataProvider provideElementsWithAttributesHavingDefaultValues
493 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
494 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
497 public static function provideElementsWithAttributesHavingDefaultValues() {
498 # Use cases in a concise format:
499 # <expected>, <element name>, <array of attributes> [, <message>]
500 # Will be mapped to Html::element()
503 # ## Generic cases, match $attribDefault static array
504 $cases[] = [ '<area/>',
505 'area', [ 'shape' => 'rect' ]
508 $cases[] = [ '<button type="submit"></button>',
509 'button', [ 'formaction' => 'GET' ]
511 $cases[] = [ '<button type="submit"></button>',
512 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
515 $cases[] = [ '<canvas></canvas>',
516 'canvas', [ 'height' => '150' ]
518 $cases[] = [ '<canvas></canvas>',
519 'canvas', [ 'width' => '300' ]
521 # Also check with numeric values
522 $cases[] = [ '<canvas></canvas>',
523 'canvas', [ 'height' => 150 ]
525 $cases[] = [ '<canvas></canvas>',
526 'canvas', [ 'width' => 300 ]
529 $cases[] = [ '<command/>',
530 'command', [ 'type' => 'command' ]
533 $cases[] = [ '<form></form>',
534 'form', [ 'action' => 'GET' ]
536 $cases[] = [ '<form></form>',
537 'form', [ 'autocomplete' => 'on' ]
539 $cases[] = [ '<form></form>',
540 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
543 $cases[] = [ '<input/>',
544 'input', [ 'formaction' => 'GET' ]
546 $cases[] = [ '<input/>',
547 'input', [ 'type' => 'text' ]
550 $cases[] = [ '<keygen/>',
551 'keygen', [ 'keytype' => 'rsa' ]
554 $cases[] = [ '<link/>',
555 'link', [ 'media' => 'all' ]
558 $cases[] = [ '<menu></menu>',
559 'menu', [ 'type' => 'list' ]
562 $cases[] = [ '<script></script>',
563 'script', [ 'type' => 'text/javascript' ]
566 $cases[] = [ '<style></style>',
567 'style', [ 'media' => 'all' ]
569 $cases[] = [ '<style></style>',
570 'style', [ 'type' => 'text/css' ]
573 $cases[] = [ '<textarea></textarea>',
574 'textarea', [ 'wrap' => 'soft' ]
579 # <link type="text/css">
580 $cases[] = [ '<link/>',
581 'link', [ 'type' => 'text/css' ]
584 # <input> specific handling
585 $cases[] = [ '<input type="checkbox"/>',
586 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
587 'Default value "on" is stripped of checkboxes',
589 $cases[] = [ '<input type="radio"/>',
590 'input', [ 'type' => 'radio', 'value' => 'on' ],
591 'Default value "on" is stripped of radio buttons',
593 $cases[] = [ '<input type="submit" value="Submit"/>',
594 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
595 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
597 $cases[] = [ '<input type="color"/>',
598 'input', [ 'type' => 'color', 'value' => '' ],
600 $cases[] = [ '<input type="range"/>',
601 'input', [ 'type' => 'range', 'value' => '' ],
604 # <button> specific handling
605 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
606 $cases[] = [ '<button type="submit"></button>',
607 'button', [ 'type' => 'submit' ],
608 'According to standard the default type is "submit". '
609 . 'Depending on compatibility mode IE might use "button", instead.',
612 # <select> specific handling
613 $cases[] = [ '<select multiple=""></select>',
614 'select', [ 'size' => '4', 'multiple' => true ],
616 # .. with numeric value
617 $cases[] = [ '<select multiple=""></select>',
618 'select', [ 'size' => 4, 'multiple' => true ],
620 $cases[] = [ '<select></select>',
621 'select', [ 'size' => '1', 'multiple' => false ],
623 # .. with numeric value
624 $cases[] = [ '<select></select>',
625 'select', [ 'size' => 1, 'multiple' => false ],
628 # Passing an array as value
629 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
630 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
631 "dropDefaults accepts values given as an array"
634 # FIXME: doDropDefault should remove defaults given in an array
635 # Expected should be '<a></a>'
636 $cases[] = [ '<a class=""></a>',
637 'a', [ 'class' => [ '', '' ] ],
638 "dropDefaults accepts values given as an array"
641 # Craft the Html elements
643 foreach ( $cases as $case ) {
647 isset( $case[3] ) ?
$case[3] : ''
655 * @covers Html::expandAttributes
657 public function testFormValidationBlacklist() {
659 Html
::expandAttributes( [
666 'Blacklist form validation attributes.'
670 Html
::expandAttributes(
678 'Allow special case "step=any".'
683 public function testWrapperInput() {
685 '<input type="radio" value="testval" name="testname"/>',
686 Html
::input( 'testname', 'testval', 'radio' ),
687 'Input wrapper with type and value.'
690 '<input name="testname"/>',
691 Html
::input( 'testname' ),
692 'Input wrapper with all default values.'
696 public function testWrapperCheck() {
698 '<input type="checkbox" value="1" name="testname"/>',
699 Html
::check( 'testname' ),
700 'Checkbox wrapper unchecked.'
703 '<input checked="" type="checkbox" value="1" name="testname"/>',
704 Html
::check( 'testname', true ),
705 'Checkbox wrapper checked.'
708 '<input type="checkbox" value="testval" name="testname"/>',
709 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
710 'Checkbox wrapper with a value override.'
714 public function testWrapperRadio() {
716 '<input type="radio" value="1" name="testname"/>',
717 Html
::radio( 'testname' ),
718 'Radio wrapper unchecked.'
721 '<input checked="" type="radio" value="1" name="testname"/>',
722 Html
::radio( 'testname', true ),
723 'Radio wrapper checked.'
726 '<input type="radio" value="testval" name="testname"/>',
727 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
728 'Radio wrapper with a value override.'
732 public function testWrapperLabel() {
734 '<label for="testid">testlabel</label>',
735 Html
::label( 'testlabel', 'testid' ),
740 public static function provideSrcSetImages() {
742 [ [], '', 'when there are no images, return empty string' ],
744 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
745 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
746 'pixel depth keys may include a trailing "x"'
749 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
750 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
751 'pixel depth keys may omit a trailing "x"'
757 * @dataProvider provideSrcSetImages
758 * @covers Html::srcSet
760 public function testSrcSet( $images, $expected, $message ) {
761 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
765 class HtmlTestValue
{
766 function __toString() {
767 return 'stringValue';