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