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
44 * @covers Html::rawElement
45 * @covers Html::openElement
46 * @covers Html::closeElement
48 public function testElementBasics() {
51 Html
::element( 'img', null, '' ),
52 'Self-closing tag for short-tag elements'
56 '<element></element>',
57 Html
::element( 'element', null, null ),
58 'Close tag for empty element (null, null)'
62 '<element></element>',
63 Html
::element( 'element', [], '' ),
64 'Close tag for empty element (array, string)'
68 public function dataXmlMimeType() {
70 // ( $mimetype, $isXmlMimeType )
71 # HTML is not an XML MimeType
72 [ 'text/html', false ],
73 # XML is an XML MimeType
75 [ 'application/xml', true ],
76 # XHTML is an XML MimeType
77 [ 'application/xhtml+xml', true ],
78 # Make sure other +xml MimeTypes are supported
79 # SVG is another random MimeType even though we don't use it
80 [ 'image/svg+xml', true ],
81 # Complete random other MimeTypes are not XML
82 [ 'text/plain', false ],
87 * @dataProvider dataXmlMimeType
88 * @covers Html::isXmlMimeType
90 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
91 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
95 * @covers Html::expandAttributes
97 public function testExpandAttributesSkipsNullAndFalse() {
100 Html
::expandAttributes( [ 'foo' => null ] ),
101 'skip keys with null value'
104 Html
::expandAttributes( [ 'foo' => false ] ),
105 'skip keys with false value'
109 Html
::expandAttributes( [ 'foo' => '' ] ),
110 'keep keys with an empty string'
115 * @covers Html::expandAttributes
117 public function testExpandAttributesForBooleans() {
120 Html
::expandAttributes( [ 'selected' => false ] ),
121 'Boolean attributes do not generates output when value is false'
125 Html
::expandAttributes( [ 'selected' => null ] ),
126 'Boolean attributes do not generates output when value is null'
131 Html
::expandAttributes( [ 'selected' => true ] ),
132 'Boolean attributes have no value when value is true'
136 Html
::expandAttributes( [ 'selected' ] ),
137 'Boolean attributes have no value when value is true (passed as numerical array)'
142 * @covers Html::expandAttributes
144 public function testExpandAttributesForNumbers() {
147 Html
::expandAttributes( [ 'value' => 1 ] ),
148 'Integer value is cast to a string'
152 Html
::expandAttributes( [ 'value' => 1.1 ] ),
153 'Float value is cast to a string'
158 * @covers Html::expandAttributes
160 public function testExpandAttributesForObjects() {
162 ' value="stringValue"',
163 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
164 'Object value is converted to a string'
169 * Test for Html::expandAttributes()
170 * Please note it output a string prefixed with a space!
171 * @covers Html::expandAttributes
173 public function testExpandAttributesVariousExpansions() {
177 Html
::expandAttributes( [ 'empty_string' => '' ] ),
178 'Empty string is always quoted'
182 Html
::expandAttributes( [ 'key' => 'value' ] ),
183 'Simple string value needs no quotes'
187 Html
::expandAttributes( [ 'one' => 1 ] ),
188 'Number 1 value needs no quotes'
192 Html
::expandAttributes( [ 'zero' => 0 ] ),
193 'Number 0 value needs no quotes'
198 * Html::expandAttributes has special features for HTML
199 * attributes that use space separated lists and also
200 * allows arrays to be used as values.
201 * @covers Html::expandAttributes
203 public function testExpandAttributesListValueAttributes() {
206 ' class="redundant spaces here"',
207 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
208 'Normalization should strip redundant spaces'
212 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
213 'Normalization should remove duplicates in string-lists'
215 # ## "EMPTY" ARRAY VALUES
218 Html
::expandAttributes( [ 'class' => [] ] ),
219 'Value with an empty array'
223 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
224 'Array with null, empty string and spaces'
226 # ## NON-EMPTY ARRAY VALUES
229 Html
::expandAttributes( [ 'class' => [
236 'Normalization should remove duplicates in the array'
240 Html
::expandAttributes( [ 'class' => [
246 'Normalization should remove duplicates in string-lists in the array'
251 * Test feature added by r96188, let pass attributes values as
252 * a PHP array. Restricted to class,rel, accesskey.
253 * @covers Html::expandAttributes
255 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
257 ' class="booltrue one"',
258 Html
::expandAttributes( [ 'class' => [
262 # Method use isset() internally, make sure we do discard
263 # attributes values which have been assigned well known values
265 'boolfalse' => false,
273 * How do we handle duplicate keys in HTML attributes expansion?
274 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
275 * The latter will take precedence.
277 * Feature added by r96188
278 * @covers Html::expandAttributes
280 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
283 Html
::expandAttributes( [ 'class' => [
292 * @covers Html::expandAttributes
293 * @expectedException MWException
295 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
296 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
297 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
298 Html
::expandAttributes( [
307 * @covers Html::namespaceSelector
308 * @covers Html::namespaceSelectorOptions
310 public function testNamespaceSelector() {
312 '<select id="namespace" name="namespace">' . "\n" .
313 '<option value="0">(Main)</option>' . "\n" .
314 '<option value="1">Talk</option>' . "\n" .
315 '<option value="2">User</option>' . "\n" .
316 '<option value="3">User talk</option>' . "\n" .
317 '<option value="4">MyWiki</option>' . "\n" .
318 '<option value="5">MyWiki Talk</option>' . "\n" .
319 '<option value="6">File</option>' . "\n" .
320 '<option value="7">File talk</option>' . "\n" .
321 '<option value="8">MediaWiki</option>' . "\n" .
322 '<option value="9">MediaWiki talk</option>' . "\n" .
323 '<option value="10">Template</option>' . "\n" .
324 '<option value="11">Template talk</option>' . "\n" .
325 '<option value="14">Category</option>' . "\n" .
326 '<option value="15">Category talk</option>' . "\n" .
327 '<option value="100">Custom</option>' . "\n" .
328 '<option value="101">Custom talk</option>' . "\n" .
330 Html
::namespaceSelector(),
331 'Basic namespace selector without custom options'
335 '<label for="mw-test-namespace">Select a namespace:</label> ' .
336 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
337 '<option value="all">all</option>' . "\n" .
338 '<option value="0">(Main)</option>' . "\n" .
339 '<option value="1">Talk</option>' . "\n" .
340 '<option value="2" selected="">User</option>' . "\n" .
341 '<option value="3">User talk</option>' . "\n" .
342 '<option value="4">MyWiki</option>' . "\n" .
343 '<option value="5">MyWiki Talk</option>' . "\n" .
344 '<option value="6">File</option>' . "\n" .
345 '<option value="7">File talk</option>' . "\n" .
346 '<option value="8">MediaWiki</option>' . "\n" .
347 '<option value="9">MediaWiki talk</option>' . "\n" .
348 '<option value="10">Template</option>' . "\n" .
349 '<option value="11">Template talk</option>' . "\n" .
350 '<option value="14">Category</option>' . "\n" .
351 '<option value="15">Category talk</option>' . "\n" .
352 '<option value="100">Custom</option>' . "\n" .
353 '<option value="101">Custom talk</option>' . "\n" .
355 Html
::namespaceSelector(
356 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
357 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
359 'Basic namespace selector with custom values'
363 '<label for="namespace">Select a namespace:</label> ' .
364 '<select id="namespace" name="namespace">' . "\n" .
365 '<option value="0">(Main)</option>' . "\n" .
366 '<option value="1">Talk</option>' . "\n" .
367 '<option value="2">User</option>' . "\n" .
368 '<option value="3">User talk</option>' . "\n" .
369 '<option value="4">MyWiki</option>' . "\n" .
370 '<option value="5">MyWiki Talk</option>' . "\n" .
371 '<option value="6">File</option>' . "\n" .
372 '<option value="7">File talk</option>' . "\n" .
373 '<option value="8">MediaWiki</option>' . "\n" .
374 '<option value="9">MediaWiki talk</option>' . "\n" .
375 '<option value="10">Template</option>' . "\n" .
376 '<option value="11">Template talk</option>' . "\n" .
377 '<option value="14">Category</option>' . "\n" .
378 '<option value="15">Category talk</option>' . "\n" .
379 '<option value="100">Custom</option>' . "\n" .
380 '<option value="101">Custom talk</option>' . "\n" .
382 Html
::namespaceSelector(
383 [ 'label' => 'Select a namespace:' ]
385 'Basic namespace selector with a custom label but no id attribtue for the <select>'
390 * @covers Html::namespaceSelector
392 public function testCanFilterOutNamespaces() {
394 '<select id="namespace" name="namespace">' . "\n" .
395 '<option value="2">User</option>' . "\n" .
396 '<option value="4">MyWiki</option>' . "\n" .
397 '<option value="5">MyWiki Talk</option>' . "\n" .
398 '<option value="6">File</option>' . "\n" .
399 '<option value="7">File talk</option>' . "\n" .
400 '<option value="8">MediaWiki</option>' . "\n" .
401 '<option value="9">MediaWiki talk</option>' . "\n" .
402 '<option value="10">Template</option>' . "\n" .
403 '<option value="11">Template talk</option>' . "\n" .
404 '<option value="14">Category</option>' . "\n" .
405 '<option value="15">Category talk</option>' . "\n" .
407 Html
::namespaceSelector(
408 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
410 'Namespace selector namespace filtering.'
415 * @covers Html::namespaceSelector
417 public function testCanDisableANamespaces() {
419 '<select id="namespace" name="namespace">' . "\n" .
420 '<option disabled="" value="0">(Main)</option>' . "\n" .
421 '<option disabled="" value="1">Talk</option>' . "\n" .
422 '<option disabled="" value="2">User</option>' . "\n" .
423 '<option disabled="" value="3">User talk</option>' . "\n" .
424 '<option disabled="" value="4">MyWiki</option>' . "\n" .
425 '<option value="5">MyWiki Talk</option>' . "\n" .
426 '<option value="6">File</option>' . "\n" .
427 '<option value="7">File talk</option>' . "\n" .
428 '<option value="8">MediaWiki</option>' . "\n" .
429 '<option value="9">MediaWiki talk</option>' . "\n" .
430 '<option value="10">Template</option>' . "\n" .
431 '<option value="11">Template talk</option>' . "\n" .
432 '<option value="14">Category</option>' . "\n" .
433 '<option value="15">Category talk</option>' . "\n" .
434 '<option value="100">Custom</option>' . "\n" .
435 '<option value="101">Custom talk</option>' . "\n" .
437 Html
::namespaceSelector( [
438 'disable' => [ 0, 1, 2, 3, 4 ]
440 'Namespace selector namespace disabling'
445 * @dataProvider provideHtml5InputTypes
446 * @covers Html::element
448 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
450 '<input type="' . $HTML5InputType . '"/>',
451 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
452 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
457 * @covers Html::warningBox
458 * @covers Html::messageBox
460 public function testWarningBox() {
462 Html
::warningBox( 'warn' ),
463 '<div class="warningbox">warn</div>'
468 * @covers Html::errorBox
469 * @covers Html::messageBox
471 public function testErrorBox() {
473 Html
::errorBox( 'err' ),
474 '<div class="errorbox">err</div>'
477 Html
::errorBox( 'err', 'heading' ),
478 '<div class="errorbox"><h2>heading</h2>err</div>'
483 * @covers Html::successBox
484 * @covers Html::messageBox
486 public function testSuccessBox() {
488 Html
::successBox( 'great' ),
489 '<div class="successbox">great</div>'
492 Html
::successBox( '<script>beware no escaping!</script>' ),
493 '<div class="successbox"><script>beware no escaping!</script></div>'
498 * List of input element types values introduced by HTML5
499 * Full list at https://www.w3.org/TR/html-markup/input.html
501 public static function provideHtml5InputTypes() {
518 foreach ( $types as $type ) {
519 $cases[] = [ $type ];
526 * Test out Html::element drops or enforces default value
527 * @covers Html::dropDefaults
528 * @dataProvider provideElementsWithAttributesHavingDefaultValues
530 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
531 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
534 public static function provideElementsWithAttributesHavingDefaultValues() {
535 # Use cases in a concise format:
536 # <expected>, <element name>, <array of attributes> [, <message>]
537 # Will be mapped to Html::element()
540 # ## Generic cases, match $attribDefault static array
541 $cases[] = [ '<area/>',
542 'area', [ 'shape' => 'rect' ]
545 $cases[] = [ '<button type="submit"></button>',
546 'button', [ 'formaction' => 'GET' ]
548 $cases[] = [ '<button type="submit"></button>',
549 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
552 $cases[] = [ '<canvas></canvas>',
553 'canvas', [ 'height' => '150' ]
555 $cases[] = [ '<canvas></canvas>',
556 'canvas', [ 'width' => '300' ]
558 # Also check with numeric values
559 $cases[] = [ '<canvas></canvas>',
560 'canvas', [ 'height' => 150 ]
562 $cases[] = [ '<canvas></canvas>',
563 'canvas', [ 'width' => 300 ]
566 $cases[] = [ '<form></form>',
567 'form', [ 'action' => 'GET' ]
569 $cases[] = [ '<form></form>',
570 'form', [ 'autocomplete' => 'on' ]
572 $cases[] = [ '<form></form>',
573 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
576 $cases[] = [ '<input/>',
577 'input', [ 'formaction' => 'GET' ]
579 $cases[] = [ '<input/>',
580 'input', [ 'type' => 'text' ]
583 $cases[] = [ '<keygen/>',
584 'keygen', [ 'keytype' => 'rsa' ]
587 $cases[] = [ '<link/>',
588 'link', [ 'media' => 'all' ]
591 $cases[] = [ '<menu></menu>',
592 'menu', [ 'type' => 'list' ]
595 $cases[] = [ '<script></script>',
596 'script', [ 'type' => 'text/javascript' ]
599 $cases[] = [ '<style></style>',
600 'style', [ 'media' => 'all' ]
602 $cases[] = [ '<style></style>',
603 'style', [ 'type' => 'text/css' ]
606 $cases[] = [ '<textarea></textarea>',
607 'textarea', [ 'wrap' => 'soft' ]
612 # <link type="text/css">
613 $cases[] = [ '<link/>',
614 'link', [ 'type' => 'text/css' ]
617 # <input> specific handling
618 $cases[] = [ '<input type="checkbox"/>',
619 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
620 'Default value "on" is stripped of checkboxes',
622 $cases[] = [ '<input type="radio"/>',
623 'input', [ 'type' => 'radio', 'value' => 'on' ],
624 'Default value "on" is stripped of radio buttons',
626 $cases[] = [ '<input type="submit" value="Submit"/>',
627 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
628 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
630 $cases[] = [ '<input type="color"/>',
631 'input', [ 'type' => 'color', 'value' => '' ],
633 $cases[] = [ '<input type="range"/>',
634 'input', [ 'type' => 'range', 'value' => '' ],
637 # <button> specific handling
638 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
639 $cases[] = [ '<button type="submit"></button>',
640 'button', [ 'type' => 'submit' ],
641 'According to standard the default type is "submit". '
642 . 'Depending on compatibility mode IE might use "button", instead.',
645 # <select> specific handling
646 $cases[] = [ '<select multiple=""></select>',
647 'select', [ 'size' => '4', 'multiple' => true ],
649 # .. with numeric value
650 $cases[] = [ '<select multiple=""></select>',
651 'select', [ 'size' => 4, 'multiple' => true ],
653 $cases[] = [ '<select></select>',
654 'select', [ 'size' => '1', 'multiple' => false ],
656 # .. with numeric value
657 $cases[] = [ '<select></select>',
658 'select', [ 'size' => 1, 'multiple' => false ],
661 # Passing an array as value
662 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
663 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
664 "dropDefaults accepts values given as an array"
667 # FIXME: doDropDefault should remove defaults given in an array
668 # Expected should be '<a></a>'
669 $cases[] = [ '<a class=""></a>',
670 'a', [ 'class' => [ '', '' ] ],
671 "dropDefaults accepts values given as an array"
674 # Craft the Html elements
676 foreach ( $cases as $case ) {
680 isset( $case[3] ) ?
$case[3] : ''
688 * @covers Html::input
690 public function testWrapperInput() {
692 '<input type="radio" value="testval" name="testname"/>',
693 Html
::input( 'testname', 'testval', 'radio' ),
694 'Input wrapper with type and value.'
697 '<input name="testname"/>',
698 Html
::input( 'testname' ),
699 'Input wrapper with all default values.'
704 * @covers Html::check
706 public function testWrapperCheck() {
708 '<input type="checkbox" value="1" name="testname"/>',
709 Html
::check( 'testname' ),
710 'Checkbox wrapper unchecked.'
713 '<input checked="" type="checkbox" value="1" name="testname"/>',
714 Html
::check( 'testname', true ),
715 'Checkbox wrapper checked.'
718 '<input type="checkbox" value="testval" name="testname"/>',
719 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
720 'Checkbox wrapper with a value override.'
725 * @covers Html::radio
727 public function testWrapperRadio() {
729 '<input type="radio" value="1" name="testname"/>',
730 Html
::radio( 'testname' ),
731 'Radio wrapper unchecked.'
734 '<input checked="" type="radio" value="1" name="testname"/>',
735 Html
::radio( 'testname', true ),
736 'Radio wrapper checked.'
739 '<input type="radio" value="testval" name="testname"/>',
740 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
741 'Radio wrapper with a value override.'
746 * @covers Html::label
748 public function testWrapperLabel() {
750 '<label for="testid">testlabel</label>',
751 Html
::label( 'testlabel', 'testid' ),
756 public static function provideSrcSetImages() {
758 [ [], '', 'when there are no images, return empty string' ],
760 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
761 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
762 'pixel depth keys may include a trailing "x"'
765 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
766 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
767 'pixel depth keys may omit a trailing "x"'
770 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
771 'small.png 1x, large.png 1.5x',
772 'omit larger duplicates'
775 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
776 'small.png 1x, large.png 1.5x',
777 'omit larger duplicates in irregular order'
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';