Drop support for XHTML 1.0
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2 /** tests for includes/Html.php */
3
4 class HtmlTest extends MediaWikiTestCase {
5
6 protected function setUp() {
7 parent::setUp();
8
9 $langCode = 'en';
10 $langObj = Language::factory( $langCode );
11
12 // Hardcode namespaces during test runs,
13 // so that html output based on existing namespaces
14 // can be properly evaluated.
15 $langObj->setNamespaces( array(
16 -2 => 'Media',
17 -1 => 'Special',
18 0 => '',
19 1 => 'Talk',
20 2 => 'User',
21 3 => 'User_talk',
22 4 => 'MyWiki',
23 5 => 'MyWiki_Talk',
24 6 => 'File',
25 7 => 'File_talk',
26 8 => 'MediaWiki',
27 9 => 'MediaWiki_talk',
28 10 => 'Template',
29 11 => 'Template_talk',
30 14 => 'Category',
31 15 => 'Category_talk',
32 100 => 'Custom',
33 101 => 'Custom_talk',
34 ) );
35
36 $this->setMwGlobals( array(
37 'wgLanguageCode' => $langCode,
38 'wgContLang' => $langObj,
39 'wgLang' => $langObj,
40 'wgWellFormedXml' => false,
41 ) );
42 }
43
44 public function testElementBasics() {
45 $this->assertEquals(
46 '<img>',
47 Html::element( 'img', null, '' ),
48 'No close tag for short-tag elements'
49 );
50
51 $this->assertEquals(
52 '<element></element>',
53 Html::element( 'element', null, null ),
54 'Close tag for empty element (null, null)'
55 );
56
57 $this->assertEquals(
58 '<element></element>',
59 Html::element( 'element', array(), '' ),
60 'Close tag for empty element (array, string)'
61 );
62
63 $this->setMwGlobals( 'wgWellFormedXml', true );
64
65 $this->assertEquals(
66 '<img />',
67 Html::element( 'img', null, '' ),
68 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
69 );
70 }
71
72 public function dataXmlMimeType() {
73 return array(
74 // ( $mimetype, $isXmlMimeType )
75 # HTML is not an XML MimeType
76 array( 'text/html', false ),
77 # XML is an XML MimeType
78 array( 'text/xml', true ),
79 array( 'application/xml', true ),
80 # XHTML is an XML MimeType
81 array( 'application/xhtml+xml', true ),
82 # Make sure other +xml MimeTypes are supported
83 # SVG is another random MimeType even though we don't use it
84 array( 'image/svg+xml', true ),
85 # Complete random other MimeTypes are not XML
86 array( 'text/plain', false ),
87 );
88 }
89
90 /**
91 * @dataProvider dataXmlMimeType
92 */
93 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
94 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
95 }
96
97 public function testExpandAttributesSkipsNullAndFalse() {
98
99 ### EMPTY ########
100 $this->assertEmpty(
101 Html::expandAttributes( array( 'foo' => null ) ),
102 'skip keys with null value'
103 );
104 $this->assertEmpty(
105 Html::expandAttributes( array( 'foo' => false ) ),
106 'skip keys with false value'
107 );
108 $this->assertNotEmpty(
109 Html::expandAttributes( array( 'foo' => '' ) ),
110 'keep keys with an empty string'
111 );
112 }
113
114 public function testExpandAttributesForBooleans() {
115 $this->assertEquals(
116 '',
117 Html::expandAttributes( array( 'selected' => false ) ),
118 'Boolean attributes do not generates output when value is false'
119 );
120 $this->assertEquals(
121 '',
122 Html::expandAttributes( array( 'selected' => null ) ),
123 'Boolean attributes do not generates output when value is null'
124 );
125
126 $this->assertEquals(
127 ' selected',
128 Html::expandAttributes( array( 'selected' => true ) ),
129 'Boolean attributes have no value when value is true'
130 );
131 $this->assertEquals(
132 ' selected',
133 Html::expandAttributes( array( 'selected' ) ),
134 'Boolean attributes have no value when value is true (passed as numerical array)'
135 );
136
137 $this->setMwGlobals( 'wgWellFormedXml', true );
138
139 $this->assertEquals(
140 ' selected=""',
141 Html::expandAttributes( array( 'selected' => true ) ),
142 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
143 );
144 }
145
146 /**
147 * Test for Html::expandAttributes()
148 * Please note it output a string prefixed with a space!
149 */
150 public function testExpandAttributesVariousExpansions() {
151 ### NOT EMPTY ####
152 $this->assertEquals(
153 ' empty_string=""',
154 Html::expandAttributes( array( 'empty_string' => '' ) ),
155 'Empty string is always quoted'
156 );
157 $this->assertEquals(
158 ' key=value',
159 Html::expandAttributes( array( 'key' => 'value' ) ),
160 'Simple string value needs no quotes'
161 );
162 $this->assertEquals(
163 ' one=1',
164 Html::expandAttributes( array( 'one' => 1 ) ),
165 'Number 1 value needs no quotes'
166 );
167 $this->assertEquals(
168 ' zero=0',
169 Html::expandAttributes( array( 'zero' => 0 ) ),
170 'Number 0 value needs no quotes'
171 );
172
173 $this->setMwGlobals( 'wgWellFormedXml', true );
174
175 $this->assertEquals(
176 ' empty_string=""',
177 Html::expandAttributes( array( 'empty_string' => '' ) ),
178 'Attribute values are always quoted (wgWellFormedXml): Empty string'
179 );
180 $this->assertEquals(
181 ' key="value"',
182 Html::expandAttributes( array( 'key' => 'value' ) ),
183 'Attribute values are always quoted (wgWellFormedXml): Simple string'
184 );
185 $this->assertEquals(
186 ' one="1"',
187 Html::expandAttributes( array( 'one' => 1 ) ),
188 'Attribute values are always quoted (wgWellFormedXml): Number 1'
189 );
190 $this->assertEquals(
191 ' zero="0"',
192 Html::expandAttributes( array( 'zero' => 0 ) ),
193 'Attribute values are always quoted (wgWellFormedXml): Number 0'
194 );
195 }
196
197 /**
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 */
202 public function testExpandAttributesListValueAttributes() {
203 ### STRING VALUES
204 $this->assertEquals(
205 ' class="redundant spaces here"',
206 Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
207 'Normalization should strip redundant spaces'
208 );
209 $this->assertEquals(
210 ' class="foo bar"',
211 Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
212 'Normalization should remove duplicates in string-lists'
213 );
214 ### "EMPTY" ARRAY VALUES
215 $this->assertEquals(
216 ' class=""',
217 Html::expandAttributes( array( 'class' => array() ) ),
218 'Value with an empty array'
219 );
220 $this->assertEquals(
221 ' class=""',
222 Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
223 'Array with null, empty string and spaces'
224 );
225 ### NON-EMPTY ARRAY VALUES
226 $this->assertEquals(
227 ' class="foo bar"',
228 Html::expandAttributes( array( 'class' => array(
229 'foo',
230 'bar',
231 'foo',
232 'bar',
233 'bar',
234 ) ) ),
235 'Normalization should remove duplicates in the array'
236 );
237 $this->assertEquals(
238 ' class="foo bar"',
239 Html::expandAttributes( array( 'class' => array(
240 'foo bar',
241 'bar foo',
242 'foo',
243 'bar bar',
244 ) ) ),
245 'Normalization should remove duplicates in string-lists in the array'
246 );
247 }
248
249 /**
250 * Test feature added by r96188, let pass attributes values as
251 * a PHP array. Restricted to class,rel, accesskey.
252 */
253 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
254 $this->assertEquals(
255 ' class="booltrue one"',
256 Html::expandAttributes( array( 'class' => array(
257 'booltrue' => true,
258 'one' => 1,
259
260 # Method use isset() internally, make sure we do discard
261 # attributes values which have been assigned well known values
262 'emptystring' => '',
263 'boolfalse' => false,
264 'zero' => 0,
265 'null' => null,
266 ) ) )
267 );
268 }
269
270 /**
271 * How do we handle duplicate keys in HTML attributes expansion?
272 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
273 * The later will take precedence.
274 *
275 * Feature added by r96188
276 */
277 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
278 $this->assertEquals(
279 ' class=""',
280 Html::expandAttributes( array( 'class' => array(
281 'GREEN',
282 'GREEN' => false,
283 'GREEN',
284 ) ) )
285 );
286 }
287
288 function testNamespaceSelector() {
289 $this->assertEquals(
290 '<select id=namespace name=namespace>' . "\n" .
291 '<option value=0>(Main)</option>' . "\n" .
292 '<option value=1>Talk</option>' . "\n" .
293 '<option value=2>User</option>' . "\n" .
294 '<option value=3>User talk</option>' . "\n" .
295 '<option value=4>MyWiki</option>' . "\n" .
296 '<option value=5>MyWiki Talk</option>' . "\n" .
297 '<option value=6>File</option>' . "\n" .
298 '<option value=7>File talk</option>' . "\n" .
299 '<option value=8>MediaWiki</option>' . "\n" .
300 '<option value=9>MediaWiki talk</option>' . "\n" .
301 '<option value=10>Template</option>' . "\n" .
302 '<option value=11>Template talk</option>' . "\n" .
303 '<option value=14>Category</option>' . "\n" .
304 '<option value=15>Category talk</option>' . "\n" .
305 '<option value=100>Custom</option>' . "\n" .
306 '<option value=101>Custom talk</option>' . "\n" .
307 '</select>',
308 Html::namespaceSelector(),
309 'Basic namespace selector without custom options'
310 );
311
312 $this->assertEquals(
313 '<label for=mw-test-namespace>Select a namespace:</label>&#160;' .
314 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
315 '<option value=all>all</option>' . "\n" .
316 '<option value=0>(Main)</option>' . "\n" .
317 '<option value=1>Talk</option>' . "\n" .
318 '<option value=2 selected>User</option>' . "\n" .
319 '<option value=3>User talk</option>' . "\n" .
320 '<option value=4>MyWiki</option>' . "\n" .
321 '<option value=5>MyWiki Talk</option>' . "\n" .
322 '<option value=6>File</option>' . "\n" .
323 '<option value=7>File talk</option>' . "\n" .
324 '<option value=8>MediaWiki</option>' . "\n" .
325 '<option value=9>MediaWiki talk</option>' . "\n" .
326 '<option value=10>Template</option>' . "\n" .
327 '<option value=11>Template talk</option>' . "\n" .
328 '<option value=14>Category</option>' . "\n" .
329 '<option value=15>Category talk</option>' . "\n" .
330 '<option value=100>Custom</option>' . "\n" .
331 '<option value=101>Custom talk</option>' . "\n" .
332 '</select>',
333 Html::namespaceSelector(
334 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
335 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
336 ),
337 'Basic namespace selector with custom values'
338 );
339
340 $this->assertEquals(
341 '<label for=namespace>Select a namespace:</label>&#160;' .
342 '<select id=namespace name=namespace>' . "\n" .
343 '<option value=0>(Main)</option>' . "\n" .
344 '<option value=1>Talk</option>' . "\n" .
345 '<option value=2>User</option>' . "\n" .
346 '<option value=3>User talk</option>' . "\n" .
347 '<option value=4>MyWiki</option>' . "\n" .
348 '<option value=5>MyWiki Talk</option>' . "\n" .
349 '<option value=6>File</option>' . "\n" .
350 '<option value=7>File talk</option>' . "\n" .
351 '<option value=8>MediaWiki</option>' . "\n" .
352 '<option value=9>MediaWiki talk</option>' . "\n" .
353 '<option value=10>Template</option>' . "\n" .
354 '<option value=11>Template talk</option>' . "\n" .
355 '<option value=14>Category</option>' . "\n" .
356 '<option value=15>Category talk</option>' . "\n" .
357 '<option value=100>Custom</option>' . "\n" .
358 '<option value=101>Custom talk</option>' . "\n" .
359 '</select>',
360 Html::namespaceSelector(
361 array( 'label' => 'Select a namespace:' )
362 ),
363 'Basic namespace selector with a custom label but no id attribtue for the <select>'
364 );
365 }
366
367 function testCanFilterOutNamespaces() {
368 $this->assertEquals(
369 '<select id=namespace name=namespace>' . "\n" .
370 '<option value=2>User</option>' . "\n" .
371 '<option value=4>MyWiki</option>' . "\n" .
372 '<option value=5>MyWiki Talk</option>' . "\n" .
373 '<option value=6>File</option>' . "\n" .
374 '<option value=7>File talk</option>' . "\n" .
375 '<option value=8>MediaWiki</option>' . "\n" .
376 '<option value=9>MediaWiki talk</option>' . "\n" .
377 '<option value=10>Template</option>' . "\n" .
378 '<option value=11>Template talk</option>' . "\n" .
379 '<option value=14>Category</option>' . "\n" .
380 '<option value=15>Category talk</option>' . "\n" .
381 '</select>',
382 Html::namespaceSelector(
383 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
384 ),
385 'Namespace selector namespace filtering.'
386 );
387 }
388
389 function testCanDisableANamespaces() {
390 $this->assertEquals(
391 '<select id=namespace name=namespace>' . "\n" .
392 '<option disabled value=0>(Main)</option>' . "\n" .
393 '<option disabled value=1>Talk</option>' . "\n" .
394 '<option disabled value=2>User</option>' . "\n" .
395 '<option disabled value=3>User talk</option>' . "\n" .
396 '<option disabled 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" .
406 '<option value=100>Custom</option>' . "\n" .
407 '<option value=101>Custom talk</option>' . "\n" .
408 '</select>',
409 Html::namespaceSelector( array(
410 'disable' => array( 0, 1, 2, 3, 4 )
411 ) ),
412 'Namespace selector namespace disabling'
413 );
414 }
415
416 /**
417 * @dataProvider provideHtml5InputTypes
418 */
419 function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
420 $this->assertEquals(
421 '<input type=' . $HTML5InputType . '>',
422 Html::element( 'input', array( 'type' => $HTML5InputType ) ),
423 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
424 );
425 }
426
427 /**
428 * List of input element types values introduced by HTML5
429 * Full list at http://www.w3.org/TR/html-markup/input.html
430 */
431 public static function provideHtml5InputTypes() {
432 $types = array(
433 'datetime',
434 'datetime-local',
435 'date',
436 'month',
437 'time',
438 'week',
439 'number',
440 'range',
441 'email',
442 'url',
443 'search',
444 'tel',
445 'color',
446 );
447 $cases = array();
448 foreach ( $types as $type ) {
449 $cases[] = array( $type );
450 }
451
452 return $cases;
453 }
454
455 /**
456 * Test out Html::element drops or enforces default value
457 * @covers Html::dropDefaults
458 * @dataProvider provideElementsWithAttributesHavingDefaultValues
459 */
460 function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
461 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
462 }
463
464 public static function provideElementsWithAttributesHavingDefaultValues() {
465 # Use cases in a concise format:
466 # <expected>, <element name>, <array of attributes> [, <message>]
467 # Will be mapped to Html::element()
468 $cases = array();
469
470 ### Generic cases, match $attribDefault static array
471 $cases[] = array( '<area>',
472 'area', array( 'shape' => 'rect' )
473 );
474
475 $cases[] = array( '<button type=submit></button>',
476 'button', array( 'formaction' => 'GET' )
477 );
478 $cases[] = array( '<button type=submit></button>',
479 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
480 );
481
482 $cases[] = array( '<canvas></canvas>',
483 'canvas', array( 'height' => '150' )
484 );
485 $cases[] = array( '<canvas></canvas>',
486 'canvas', array( 'width' => '300' )
487 );
488 # Also check with numeric values
489 $cases[] = array( '<canvas></canvas>',
490 'canvas', array( 'height' => 150 )
491 );
492 $cases[] = array( '<canvas></canvas>',
493 'canvas', array( 'width' => 300 )
494 );
495
496 $cases[] = array( '<command>',
497 'command', array( 'type' => 'command' )
498 );
499
500 $cases[] = array( '<form></form>',
501 'form', array( 'action' => 'GET' )
502 );
503 $cases[] = array( '<form></form>',
504 'form', array( 'autocomplete' => 'on' )
505 );
506 $cases[] = array( '<form></form>',
507 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
508 );
509
510 $cases[] = array( '<input>',
511 'input', array( 'formaction' => 'GET' )
512 );
513 $cases[] = array( '<input>',
514 'input', array( 'type' => 'text' )
515 );
516
517 $cases[] = array( '<keygen>',
518 'keygen', array( 'keytype' => 'rsa' )
519 );
520
521 $cases[] = array( '<link>',
522 'link', array( 'media' => 'all' )
523 );
524
525 $cases[] = array( '<menu></menu>',
526 'menu', array( 'type' => 'list' )
527 );
528
529 $cases[] = array( '<script></script>',
530 'script', array( 'type' => 'text/javascript' )
531 );
532
533 $cases[] = array( '<style></style>',
534 'style', array( 'media' => 'all' )
535 );
536 $cases[] = array( '<style></style>',
537 'style', array( 'type' => 'text/css' )
538 );
539
540 $cases[] = array( '<textarea></textarea>',
541 'textarea', array( 'wrap' => 'soft' )
542 );
543
544 ### SPECIFIC CASES
545
546 # <link type="text/css">
547 $cases[] = array( '<link>',
548 'link', array( 'type' => 'text/css' )
549 );
550
551 # <input> specific handling
552 $cases[] = array( '<input type=checkbox>',
553 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
554 'Default value "on" is stripped of checkboxes',
555 );
556 $cases[] = array( '<input type=radio>',
557 'input', array( 'type' => 'radio', 'value' => 'on' ),
558 'Default value "on" is stripped of radio buttons',
559 );
560 $cases[] = array( '<input type=submit value=Submit>',
561 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
562 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
563 );
564 $cases[] = array( '<input type=color>',
565 'input', array( 'type' => 'color', 'value' => '' ),
566 );
567 $cases[] = array( '<input type=range>',
568 'input', array( 'type' => 'range', 'value' => '' ),
569 );
570
571 # <button> specific handling
572 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
573 $cases[] = array( '<button type=submit></button>',
574 'button', array( 'type' => 'submit' ),
575 'According to standard the default type is "submit". Depending on compatibility mode IE might use "button", instead.',
576 );
577
578 # <select> specifc handling
579 $cases[] = array( '<select multiple></select>',
580 'select', array( 'size' => '4', 'multiple' => true ),
581 );
582 # .. with numeric value
583 $cases[] = array( '<select multiple></select>',
584 'select', array( 'size' => 4, 'multiple' => true ),
585 );
586 $cases[] = array( '<select></select>',
587 'select', array( 'size' => '1', 'multiple' => false ),
588 );
589 # .. with numeric value
590 $cases[] = array( '<select></select>',
591 'select', array( 'size' => 1, 'multiple' => false ),
592 );
593
594 # Passing an array as value
595 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
596 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
597 "dropDefaults accepts values given as an array"
598 );
599
600 # FIXME: doDropDefault should remove defaults given in an array
601 # Expected should be '<a></a>'
602 $cases[] = array( '<a class=""></a>',
603 'a', array( 'class' => array( '', '' ) ),
604 "dropDefaults accepts values given as an array"
605 );
606
607 # Craft the Html elements
608 $ret = array();
609 foreach ( $cases as $case ) {
610 $ret[] = array(
611 $case[0],
612 $case[1], $case[2],
613 isset( $case[3] ) ? $case[3] : ''
614 );
615 }
616
617 return $ret;
618 }
619
620 public function testFormValidationBlacklist() {
621 $this->assertEmpty(
622 Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 2 ) ),
623 'Blacklist form validation attributes.'
624 );
625 $this->assertEquals(
626 ' step=any',
627 Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 'any' ) ),
628 'Allow special case "step=any".'
629 );
630 }
631 }