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