Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / HtmlTest.php
blobb7249e5d9466a14cda5ef965be37244c463aa993
1 <?php
2 /** tests for includes/Html.php */
4 class HtmlTest extends MediaWikiTestCase {
6 protected function setUp() {
7 parent::setUp();
9 $this->setMwGlobals( [
10 'wgUseMediaWikiUIEverywhere' => false,
11 ] );
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( [
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->setUserLang( $langObj );
39 $this->setContentLang( $langObj );
42 /**
43 * @covers Html::element
45 public function testElementBasics() {
46 $this->assertEquals(
47 '<img/>',
48 Html::element( 'img', null, '' ),
49 'Self-closing tag for short-tag elements'
52 $this->assertEquals(
53 '<element></element>',
54 Html::element( 'element', null, null ),
55 'Close tag for empty element (null, null)'
58 $this->assertEquals(
59 '<element></element>',
60 Html::element( 'element', [], '' ),
61 'Close tag for empty element (array, string)'
65 public function dataXmlMimeType() {
66 return [
67 // ( $mimetype, $isXmlMimeType )
68 # HTML is not an XML MimeType
69 [ 'text/html', false ],
70 # XML is an XML MimeType
71 [ 'text/xml', true ],
72 [ 'application/xml', true ],
73 # XHTML is an XML MimeType
74 [ 'application/xhtml+xml', true ],
75 # Make sure other +xml MimeTypes are supported
76 # SVG is another random MimeType even though we don't use it
77 [ 'image/svg+xml', true ],
78 # Complete random other MimeTypes are not XML
79 [ 'text/plain', false ],
83 /**
84 * @dataProvider dataXmlMimeType
85 * @covers Html::isXmlMimeType
87 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
88 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
91 /**
92 * @covers Html::expandAttributes
94 public function testExpandAttributesSkipsNullAndFalse() {
95 # ## EMPTY ########
96 $this->assertEmpty(
97 Html::expandAttributes( [ 'foo' => null ] ),
98 'skip keys with null value'
100 $this->assertEmpty(
101 Html::expandAttributes( [ 'foo' => false ] ),
102 'skip keys with false value'
104 $this->assertEquals(
105 ' foo=""',
106 Html::expandAttributes( [ 'foo' => '' ] ),
107 'keep keys with an empty string'
112 * @covers Html::expandAttributes
114 public function testExpandAttributesForBooleans() {
115 $this->assertEquals(
117 Html::expandAttributes( [ 'selected' => false ] ),
118 'Boolean attributes do not generates output when value is false'
120 $this->assertEquals(
122 Html::expandAttributes( [ 'selected' => null ] ),
123 'Boolean attributes do not generates output when value is null'
126 $this->assertEquals(
127 ' selected=""',
128 Html::expandAttributes( [ 'selected' => true ] ),
129 'Boolean attributes have no value when value is true'
131 $this->assertEquals(
132 ' selected=""',
133 Html::expandAttributes( [ 'selected' ] ),
134 'Boolean attributes have no value when value is true (passed as numerical array)'
139 * @covers Html::expandAttributes
141 public function testExpandAttributesForNumbers() {
142 $this->assertEquals(
143 ' value="1"',
144 Html::expandAttributes( [ 'value' => 1 ] ),
145 'Integer value is cast to a string'
147 $this->assertEquals(
148 ' value="1.1"',
149 Html::expandAttributes( [ 'value' => 1.1 ] ),
150 'Float value is cast to a string'
155 * @covers Html::expandAttributes
157 public function testExpandAttributesForObjects() {
158 $this->assertEquals(
159 ' value="stringValue"',
160 Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
161 'Object value is converted to a string'
166 * Test for Html::expandAttributes()
167 * Please note it output a string prefixed with a space!
168 * @covers Html::expandAttributes
170 public function testExpandAttributesVariousExpansions() {
171 # ## NOT EMPTY ####
172 $this->assertEquals(
173 ' empty_string=""',
174 Html::expandAttributes( [ 'empty_string' => '' ] ),
175 'Empty string is always quoted'
177 $this->assertEquals(
178 ' key="value"',
179 Html::expandAttributes( [ 'key' => 'value' ] ),
180 'Simple string value needs no quotes'
182 $this->assertEquals(
183 ' one="1"',
184 Html::expandAttributes( [ 'one' => 1 ] ),
185 'Number 1 value needs no quotes'
187 $this->assertEquals(
188 ' zero="0"',
189 Html::expandAttributes( [ 'zero' => 0 ] ),
190 'Number 0 value needs no quotes'
195 * Html::expandAttributes has special features for HTML
196 * attributes that use space separated lists and also
197 * allows arrays to be used as values.
198 * @covers Html::expandAttributes
200 public function testExpandAttributesListValueAttributes() {
201 # ## STRING VALUES
202 $this->assertEquals(
203 ' class="redundant spaces here"',
204 Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
205 'Normalization should strip redundant spaces'
207 $this->assertEquals(
208 ' class="foo bar"',
209 Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
210 'Normalization should remove duplicates in string-lists'
212 # ## "EMPTY" ARRAY VALUES
213 $this->assertEquals(
214 ' class=""',
215 Html::expandAttributes( [ 'class' => [] ] ),
216 'Value with an empty array'
218 $this->assertEquals(
219 ' class=""',
220 Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
221 'Array with null, empty string and spaces'
223 # ## NON-EMPTY ARRAY VALUES
224 $this->assertEquals(
225 ' class="foo bar"',
226 Html::expandAttributes( [ 'class' => [
227 'foo',
228 'bar',
229 'foo',
230 'bar',
231 'bar',
232 ] ] ),
233 'Normalization should remove duplicates in the array'
235 $this->assertEquals(
236 ' class="foo bar"',
237 Html::expandAttributes( [ 'class' => [
238 'foo bar',
239 'bar foo',
240 'foo',
241 'bar bar',
242 ] ] ),
243 'Normalization should remove duplicates in string-lists in the array'
248 * Test feature added by r96188, let pass attributes values as
249 * a PHP array. Restricted to class,rel, accesskey.
250 * @covers Html::expandAttributes
252 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
253 $this->assertEquals(
254 ' class="booltrue one"',
255 Html::expandAttributes( [ 'class' => [
256 'booltrue' => true,
257 'one' => 1,
259 # Method use isset() internally, make sure we do discard
260 # attributes values which have been assigned well known values
261 'emptystring' => '',
262 'boolfalse' => false,
263 'zero' => 0,
264 'null' => null,
265 ] ] )
270 * How do we handle duplicate keys in HTML attributes expansion?
271 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
272 * The latter will take precedence.
274 * Feature added by r96188
275 * @covers Html::expandAttributes
277 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
278 $this->assertEquals(
279 ' class=""',
280 Html::expandAttributes( [ 'class' => [
281 'GREEN',
282 'GREEN' => false,
283 'GREEN',
284 ] ] )
289 * @covers Html::expandAttributes
290 * @expectedException MWException
292 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
293 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
294 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
295 Html::expandAttributes( [
296 'src' => [
297 'ltr' => 'ltr.svg',
298 'rtl' => 'rtl.svg'
300 ] );
304 * @covers Html::namespaceSelector
306 public function testNamespaceSelector() {
307 $this->assertEquals(
308 '<select id="namespace" name="namespace">' . "\n" .
309 '<option value="0">(Main)</option>' . "\n" .
310 '<option value="1">Talk</option>' . "\n" .
311 '<option value="2">User</option>' . "\n" .
312 '<option value="3">User talk</option>' . "\n" .
313 '<option value="4">MyWiki</option>' . "\n" .
314 '<option value="5">MyWiki Talk</option>' . "\n" .
315 '<option value="6">File</option>' . "\n" .
316 '<option value="7">File talk</option>' . "\n" .
317 '<option value="8">MediaWiki</option>' . "\n" .
318 '<option value="9">MediaWiki talk</option>' . "\n" .
319 '<option value="10">Template</option>' . "\n" .
320 '<option value="11">Template talk</option>' . "\n" .
321 '<option value="14">Category</option>' . "\n" .
322 '<option value="15">Category talk</option>' . "\n" .
323 '<option value="100">Custom</option>' . "\n" .
324 '<option value="101">Custom talk</option>' . "\n" .
325 '</select>',
326 Html::namespaceSelector(),
327 'Basic namespace selector without custom options'
330 $this->assertEquals(
331 '<label for="mw-test-namespace">Select a namespace:</label>&#160;' .
332 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
333 '<option value="all">all</option>' . "\n" .
334 '<option value="0">(Main)</option>' . "\n" .
335 '<option value="1">Talk</option>' . "\n" .
336 '<option value="2" selected="">User</option>' . "\n" .
337 '<option value="3">User talk</option>' . "\n" .
338 '<option value="4">MyWiki</option>' . "\n" .
339 '<option value="5">MyWiki Talk</option>' . "\n" .
340 '<option value="6">File</option>' . "\n" .
341 '<option value="7">File talk</option>' . "\n" .
342 '<option value="8">MediaWiki</option>' . "\n" .
343 '<option value="9">MediaWiki talk</option>' . "\n" .
344 '<option value="10">Template</option>' . "\n" .
345 '<option value="11">Template talk</option>' . "\n" .
346 '<option value="14">Category</option>' . "\n" .
347 '<option value="15">Category talk</option>' . "\n" .
348 '<option value="100">Custom</option>' . "\n" .
349 '<option value="101">Custom talk</option>' . "\n" .
350 '</select>',
351 Html::namespaceSelector(
352 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
353 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
355 'Basic namespace selector with custom values'
358 $this->assertEquals(
359 '<label for="namespace">Select a namespace:</label>&#160;' .
360 '<select id="namespace" name="namespace">' . "\n" .
361 '<option value="0">(Main)</option>' . "\n" .
362 '<option value="1">Talk</option>' . "\n" .
363 '<option value="2">User</option>' . "\n" .
364 '<option value="3">User talk</option>' . "\n" .
365 '<option value="4">MyWiki</option>' . "\n" .
366 '<option value="5">MyWiki Talk</option>' . "\n" .
367 '<option value="6">File</option>' . "\n" .
368 '<option value="7">File talk</option>' . "\n" .
369 '<option value="8">MediaWiki</option>' . "\n" .
370 '<option value="9">MediaWiki talk</option>' . "\n" .
371 '<option value="10">Template</option>' . "\n" .
372 '<option value="11">Template talk</option>' . "\n" .
373 '<option value="14">Category</option>' . "\n" .
374 '<option value="15">Category talk</option>' . "\n" .
375 '<option value="100">Custom</option>' . "\n" .
376 '<option value="101">Custom talk</option>' . "\n" .
377 '</select>',
378 Html::namespaceSelector(
379 [ 'label' => 'Select a namespace:' ]
381 'Basic namespace selector with a custom label but no id attribtue for the <select>'
385 public function testCanFilterOutNamespaces() {
386 $this->assertEquals(
387 '<select id="namespace" name="namespace">' . "\n" .
388 '<option value="2">User</option>' . "\n" .
389 '<option value="4">MyWiki</option>' . "\n" .
390 '<option value="5">MyWiki Talk</option>' . "\n" .
391 '<option value="6">File</option>' . "\n" .
392 '<option value="7">File talk</option>' . "\n" .
393 '<option value="8">MediaWiki</option>' . "\n" .
394 '<option value="9">MediaWiki talk</option>' . "\n" .
395 '<option value="10">Template</option>' . "\n" .
396 '<option value="11">Template talk</option>' . "\n" .
397 '<option value="14">Category</option>' . "\n" .
398 '<option value="15">Category talk</option>' . "\n" .
399 '</select>',
400 Html::namespaceSelector(
401 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
403 'Namespace selector namespace filtering.'
407 public function testCanDisableANamespaces() {
408 $this->assertEquals(
409 '<select id="namespace" name="namespace">' . "\n" .
410 '<option disabled="" value="0">(Main)</option>' . "\n" .
411 '<option disabled="" value="1">Talk</option>' . "\n" .
412 '<option disabled="" value="2">User</option>' . "\n" .
413 '<option disabled="" value="3">User talk</option>' . "\n" .
414 '<option disabled="" value="4">MyWiki</option>' . "\n" .
415 '<option value="5">MyWiki Talk</option>' . "\n" .
416 '<option value="6">File</option>' . "\n" .
417 '<option value="7">File talk</option>' . "\n" .
418 '<option value="8">MediaWiki</option>' . "\n" .
419 '<option value="9">MediaWiki talk</option>' . "\n" .
420 '<option value="10">Template</option>' . "\n" .
421 '<option value="11">Template talk</option>' . "\n" .
422 '<option value="14">Category</option>' . "\n" .
423 '<option value="15">Category talk</option>' . "\n" .
424 '<option value="100">Custom</option>' . "\n" .
425 '<option value="101">Custom talk</option>' . "\n" .
426 '</select>',
427 Html::namespaceSelector( [
428 'disable' => [ 0, 1, 2, 3, 4 ]
429 ] ),
430 'Namespace selector namespace disabling'
435 * @dataProvider provideHtml5InputTypes
436 * @covers Html::element
438 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
439 $this->assertEquals(
440 '<input type="' . $HTML5InputType . '"/>',
441 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
442 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
447 * List of input element types values introduced by HTML5
448 * Full list at https://www.w3.org/TR/html-markup/input.html
450 public static function provideHtml5InputTypes() {
451 $types = [
452 'datetime',
453 'datetime-local',
454 'date',
455 'month',
456 'time',
457 'week',
458 'number',
459 'range',
460 'email',
461 'url',
462 'search',
463 'tel',
464 'color',
466 $cases = [];
467 foreach ( $types as $type ) {
468 $cases[] = [ $type ];
471 return $cases;
475 * Test out Html::element drops or enforces default value
476 * @covers Html::dropDefaults
477 * @dataProvider provideElementsWithAttributesHavingDefaultValues
479 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
480 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
483 public static function provideElementsWithAttributesHavingDefaultValues() {
484 # Use cases in a concise format:
485 # <expected>, <element name>, <array of attributes> [, <message>]
486 # Will be mapped to Html::element()
487 $cases = [];
489 # ## Generic cases, match $attribDefault static array
490 $cases[] = [ '<area/>',
491 'area', [ 'shape' => 'rect' ]
494 $cases[] = [ '<button type="submit"></button>',
495 'button', [ 'formaction' => 'GET' ]
497 $cases[] = [ '<button type="submit"></button>',
498 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
501 $cases[] = [ '<canvas></canvas>',
502 'canvas', [ 'height' => '150' ]
504 $cases[] = [ '<canvas></canvas>',
505 'canvas', [ 'width' => '300' ]
507 # Also check with numeric values
508 $cases[] = [ '<canvas></canvas>',
509 'canvas', [ 'height' => 150 ]
511 $cases[] = [ '<canvas></canvas>',
512 'canvas', [ 'width' => 300 ]
515 $cases[] = [ '<form></form>',
516 'form', [ 'action' => 'GET' ]
518 $cases[] = [ '<form></form>',
519 'form', [ 'autocomplete' => 'on' ]
521 $cases[] = [ '<form></form>',
522 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
525 $cases[] = [ '<input/>',
526 'input', [ 'formaction' => 'GET' ]
528 $cases[] = [ '<input/>',
529 'input', [ 'type' => 'text' ]
532 $cases[] = [ '<keygen/>',
533 'keygen', [ 'keytype' => 'rsa' ]
536 $cases[] = [ '<link/>',
537 'link', [ 'media' => 'all' ]
540 $cases[] = [ '<menu></menu>',
541 'menu', [ 'type' => 'list' ]
544 $cases[] = [ '<script></script>',
545 'script', [ 'type' => 'text/javascript' ]
548 $cases[] = [ '<style></style>',
549 'style', [ 'media' => 'all' ]
551 $cases[] = [ '<style></style>',
552 'style', [ 'type' => 'text/css' ]
555 $cases[] = [ '<textarea></textarea>',
556 'textarea', [ 'wrap' => 'soft' ]
559 # ## SPECIFIC CASES
561 # <link type="text/css">
562 $cases[] = [ '<link/>',
563 'link', [ 'type' => 'text/css' ]
566 # <input> specific handling
567 $cases[] = [ '<input type="checkbox"/>',
568 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
569 'Default value "on" is stripped of checkboxes',
571 $cases[] = [ '<input type="radio"/>',
572 'input', [ 'type' => 'radio', 'value' => 'on' ],
573 'Default value "on" is stripped of radio buttons',
575 $cases[] = [ '<input type="submit" value="Submit"/>',
576 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
577 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
579 $cases[] = [ '<input type="color"/>',
580 'input', [ 'type' => 'color', 'value' => '' ],
582 $cases[] = [ '<input type="range"/>',
583 'input', [ 'type' => 'range', 'value' => '' ],
586 # <button> specific handling
587 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
588 $cases[] = [ '<button type="submit"></button>',
589 'button', [ 'type' => 'submit' ],
590 'According to standard the default type is "submit". '
591 . 'Depending on compatibility mode IE might use "button", instead.',
594 # <select> specific handling
595 $cases[] = [ '<select multiple=""></select>',
596 'select', [ 'size' => '4', 'multiple' => true ],
598 # .. with numeric value
599 $cases[] = [ '<select multiple=""></select>',
600 'select', [ 'size' => 4, 'multiple' => true ],
602 $cases[] = [ '<select></select>',
603 'select', [ 'size' => '1', 'multiple' => false ],
605 # .. with numeric value
606 $cases[] = [ '<select></select>',
607 'select', [ 'size' => 1, 'multiple' => false ],
610 # Passing an array as value
611 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
612 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
613 "dropDefaults accepts values given as an array"
616 # FIXME: doDropDefault should remove defaults given in an array
617 # Expected should be '<a></a>'
618 $cases[] = [ '<a class=""></a>',
619 'a', [ 'class' => [ '', '' ] ],
620 "dropDefaults accepts values given as an array"
623 # Craft the Html elements
624 $ret = [];
625 foreach ( $cases as $case ) {
626 $ret[] = [
627 $case[0],
628 $case[1], $case[2],
629 isset( $case[3] ) ? $case[3] : ''
633 return $ret;
636 public function testWrapperInput() {
637 $this->assertEquals(
638 '<input type="radio" value="testval" name="testname"/>',
639 Html::input( 'testname', 'testval', 'radio' ),
640 'Input wrapper with type and value.'
642 $this->assertEquals(
643 '<input name="testname"/>',
644 Html::input( 'testname' ),
645 'Input wrapper with all default values.'
649 public function testWrapperCheck() {
650 $this->assertEquals(
651 '<input type="checkbox" value="1" name="testname"/>',
652 Html::check( 'testname' ),
653 'Checkbox wrapper unchecked.'
655 $this->assertEquals(
656 '<input checked="" type="checkbox" value="1" name="testname"/>',
657 Html::check( 'testname', true ),
658 'Checkbox wrapper checked.'
660 $this->assertEquals(
661 '<input type="checkbox" value="testval" name="testname"/>',
662 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
663 'Checkbox wrapper with a value override.'
667 public function testWrapperRadio() {
668 $this->assertEquals(
669 '<input type="radio" value="1" name="testname"/>',
670 Html::radio( 'testname' ),
671 'Radio wrapper unchecked.'
673 $this->assertEquals(
674 '<input checked="" type="radio" value="1" name="testname"/>',
675 Html::radio( 'testname', true ),
676 'Radio wrapper checked.'
678 $this->assertEquals(
679 '<input type="radio" value="testval" name="testname"/>',
680 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
681 'Radio wrapper with a value override.'
685 public function testWrapperLabel() {
686 $this->assertEquals(
687 '<label for="testid">testlabel</label>',
688 Html::label( 'testlabel', 'testid' ),
689 'Label wrapper'
693 public static function provideSrcSetImages() {
694 return [
695 [ [], '', 'when there are no images, return empty string' ],
697 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
698 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
699 'pixel depth keys may include a trailing "x"'
702 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
703 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
704 'pixel depth keys may omit a trailing "x"'
707 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
708 'small.png 1x, large.png 1.5x',
709 'omit larger duplicates'
712 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
713 'small.png 1x, large.png 1.5x',
714 'omit larger duplicates in irregular order'
720 * @dataProvider provideSrcSetImages
721 * @covers Html::srcSet
723 public function testSrcSet( $images, $expected, $message ) {
724 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
728 class HtmlTestValue {
729 function __toString() {
730 return 'stringValue';