Use canonical case
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2 /** tests for includes/Html.php */
3
4 class HtmlTest extends MediaWikiTestCase {
5 private static $oldLang;
6 private static $oldContLang;
7
8 public function setUp() {
9 global $wgLang, $wgContLang, $wgLanguageCode;
10
11 self::$oldLang = $wgLang;
12 self::$oldContLang = $wgContLang;
13
14 $wgLanguageCode = 'en';
15 $wgContLang = $wgLang = Language::factory( $wgLanguageCode );
16 }
17
18 public function tearDown() {
19 global $wgLang, $wgContLang, $wgLanguageCode;
20 $wgLang = self::$oldLang;
21 $wgContLang = self::$oldContLang;
22 $wgLanguageCode = $wgContLang->getCode();
23 }
24
25 public function testExpandAttributesSkipsNullAndFalse() {
26
27 ### EMPTY ########
28 $this->AssertEmpty(
29 Html::expandAttributes( array( 'foo' => null ) ),
30 'skip keys with null value'
31 );
32 $this->AssertEmpty(
33 Html::expandAttributes( array( 'foo' => false ) ),
34 'skip keys with false value'
35 );
36 $this->AssertNotEmpty(
37 Html::expandAttributes( array( 'foo' => '' ) ),
38 'keep keys with an empty string'
39 );
40 }
41
42 public function testExpandAttributesForBooleans() {
43 global $wgHtml5;
44 $this->AssertEquals(
45 '',
46 Html::expandAttributes( array( 'selected' => false ) ),
47 'Boolean attributes do not generates output when value is false'
48 );
49 $this->AssertEquals(
50 '',
51 Html::expandAttributes( array( 'selected' => null ) ),
52 'Boolean attributes do not generates output when value is null'
53 );
54
55 $this->AssertEquals(
56 $wgHtml5 ? ' selected=""' : ' selected="selected"',
57 Html::expandAttributes( array( 'selected' => true ) ),
58 'Boolean attributes skip value output'
59 );
60 $this->AssertEquals(
61 $wgHtml5 ? ' selected=""' : ' selected="selected"',
62 Html::expandAttributes( array( 'selected' ) ),
63 'Boolean attributes (ex: selected) do not need a value'
64 );
65 }
66
67 /**
68 * Test for Html::expandAttributes()
69 * Please note it output a string prefixed with a space!
70 */
71 public function testExpandAttributesVariousExpansions() {
72 ### NOT EMPTY ####
73 $this->AssertEquals(
74 ' empty_string=""',
75 Html::expandAttributes( array( 'empty_string' => '' ) ),
76 'Value with an empty string'
77 );
78 $this->AssertEquals(
79 ' key="value"',
80 Html::expandAttributes( array( 'key' => 'value' ) ),
81 'Value is a string'
82 );
83 $this->AssertEquals(
84 ' one="1"',
85 Html::expandAttributes( array( 'one' => 1 ) ),
86 'Value is a numeric one'
87 );
88 $this->AssertEquals(
89 ' zero="0"',
90 Html::expandAttributes( array( 'zero' => 0 ) ),
91 'Value is a numeric zero'
92 );
93 }
94
95 /**
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.
99 */
100 public function testExpandAttributesListValueAttributes() {
101 ### STRING VALUES
102 $this->AssertEquals(
103 ' class="redundant spaces here"',
104 Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
105 'Normalization should strip redundant spaces'
106 );
107 $this->AssertEquals(
108 ' class="foo bar"',
109 Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
110 'Normalization should remove duplicates in string-lists'
111 );
112 ### "EMPTY" ARRAY VALUES
113 $this->AssertEquals(
114 ' class=""',
115 Html::expandAttributes( array( 'class' => array() ) ),
116 'Value with an empty array'
117 );
118 $this->AssertEquals(
119 ' class=""',
120 Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
121 'Array with null, empty string and spaces'
122 );
123 ### NON-EMPTY ARRAY VALUES
124 $this->AssertEquals(
125 ' class="foo bar"',
126 Html::expandAttributes( array( 'class' => array(
127 'foo',
128 'bar',
129 'foo',
130 'bar',
131 'bar',
132 ) ) ),
133 'Normalization should remove duplicates in the array'
134 );
135 $this->AssertEquals(
136 ' class="foo bar"',
137 Html::expandAttributes( array( 'class' => array(
138 'foo bar',
139 'bar foo',
140 'foo',
141 'bar bar',
142 ) ) ),
143 'Normalization should remove duplicates in string-lists in the array'
144 );
145 }
146
147 /**
148 * Test feature added by r96188, let pass attributes values as
149 * a PHP array. Restricted to class,rel, accesskey.
150 */
151 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
152 $this->assertEquals(
153 ' class="booltrue one"',
154 Html::expandAttributes( array( 'class' => array(
155 'booltrue' => true,
156 'one' => 1,
157
158 # Method use isset() internally, make sure we do discard
159 # attributes values which have been assigned well known values
160 'emptystring' => '',
161 'boolfalse' => false,
162 'zero' => 0,
163 'null' => null,
164 )))
165 );
166 }
167
168 /**
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.
172 *
173 * Feature added by r96188
174 */
175 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
176 $this->assertEquals(
177 ' class=""',
178 Html::expandAttributes( array( 'class' => array(
179 'GREEN',
180 'GREEN' => false,
181 'GREEN',
182 )))
183 );
184 }
185 }