2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
5 private static $oldLang;
6 private static $oldContLang;
8 public function setUp() {
9 global $wgLang, $wgContLang, $wgLanguageCode;
11 self
::$oldLang = $wgLang;
12 self
::$oldContLang = $wgContLang;
14 $wgLanguageCode = 'en';
15 $wgContLang = $wgLang = Language
::factory( $wgLanguageCode );
18 public function tearDown() {
19 global $wgLang, $wgContLang, $wgLanguageCode;
20 $wgLang = self
::$oldLang;
21 $wgContLang = self
::$oldContLang;
22 $wgLanguageCode = $wgContLang->getCode();
25 public function testExpandAttributesSkipsNullAndFalse() {
29 Html
::expandAttributes( array( 'foo' => null ) ),
30 'skip keys with null value'
33 Html
::expandAttributes( array( 'foo' => false ) ),
34 'skip keys with false value'
36 $this->AssertNotEmpty(
37 Html
::expandAttributes( array( 'foo' => '' ) ),
38 'keep keys with an empty string'
42 public function testExpandAttributesForBooleans() {
46 Html
::expandAttributes( array( 'selected' => false ) ),
47 'Boolean attributes do not generates output when value is false'
51 Html
::expandAttributes( array( 'selected' => null ) ),
52 'Boolean attributes do not generates output when value is null'
56 $wgHtml5 ?
' selected=""' : ' selected="selected"',
57 Html
::expandAttributes( array( 'selected' => true ) ),
58 'Boolean attributes skip value output'
61 $wgHtml5 ?
' selected=""' : ' selected="selected"',
62 Html
::expandAttributes( array( 'selected' ) ),
63 'Boolean attributes (ex: selected) do not need a value'
68 * Test for Html::expandAttributes()
69 * Please note it output a string prefixed with a space!
71 public function testExpandAttributesVariousExpansions() {
75 Html
::expandAttributes( array( 'empty_string' => '' ) ),
76 'Value with an empty string'
80 Html
::expandAttributes( array( 'key' => 'value' ) ),
85 Html
::expandAttributes( array( 'one' => 1 ) ),
86 'Value is a numeric one'
90 Html
::expandAttributes( array( 'zero' => 0 ) ),
91 'Value is a numeric zero'
96 * Html::expandAttributes has special features for HTML
97 * attributes that use space separated lists and also
98 * allows arrays to be used as values.
100 public function testExpandAttributesListValueAttributes() {
103 ' class="redundant spaces here"',
104 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
105 'Normalization should strip redundant spaces'
109 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
110 'Normalization should remove duplicates in string-lists'
112 ### "EMPTY" ARRAY VALUES
115 Html
::expandAttributes( array( 'class' => array() ) ),
116 'Value with an empty array'
120 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
121 'Array with null, empty string and spaces'
123 ### NON-EMPTY ARRAY VALUES
126 Html
::expandAttributes( array( 'class' => array(
133 'Normalization should remove duplicates in the array'
137 Html
::expandAttributes( array( 'class' => array(
143 'Normalization should remove duplicates in string-lists in the array'
148 * Test feature added by r96188, let pass attributes values as
149 * a PHP array. Restricted to class,rel, accesskey.
151 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
153 ' class="booltrue one"',
154 Html
::expandAttributes( array( 'class' => array(
158 # Method use isset() internally, make sure we do discard
159 # attributes values which have been assigned well known values
161 'boolfalse' => false,
169 * How do we handle duplicate keys in HTML attributes expansion?
170 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
171 * The later will take precedence.
173 * Feature added by r96188
175 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
178 HTML
::expandAttributes( array( 'class' => array(