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