[Html] Unit test + bugfix Html::namespaceSelector
[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 private static $oldNamespaces;
8
9 public function setUp() {
10 global $wgLang, $wgContLang, $wgLanguageCode;
11
12 self::$oldLang = $wgLang;
13 self::$oldContLang = $wgContLang;
14
15 $wgLanguageCode = 'en';
16 $wgContLang = $wgLang = Language::factory( $wgLanguageCode );
17
18 // Hardcode namespaces during test runs,
19 // so that html output based on existing namespaces
20 // can be properly evaluated.
21 self::$oldNamespaces = $wgContLang->namespaceNames;
22 $wgContLang->namespaceNames = array(
23 -2 => 'Media',
24 -1 => 'Special',
25 0 => '',
26 1 => 'Talk',
27 2 => 'User',
28 3 => 'User_talk',
29 4 => 'MyWiki',
30 5 => 'MyWiki_Talk',
31 6 => 'File',
32 7 => 'File_talk',
33 8 => 'MediaWiki',
34 9 => 'MediaWiki_talk',
35 10 => 'Template',
36 11 => 'Template_talk',
37 100 => 'Custom',
38 101 => 'Custom_talk',
39 );
40 }
41
42 public function tearDown() {
43 global $wgLang, $wgContLang, $wgLanguageCode;
44 $wgLang = self::$oldLang;
45 $wgContLang = self::$oldContLang;
46 $wgLanguageCode = $wgContLang->getCode();
47 $wgContLang->namespaceNames = self::$oldNamespaces;
48 }
49
50 public function testExpandAttributesSkipsNullAndFalse() {
51
52 ### EMPTY ########
53 $this->AssertEmpty(
54 Html::expandAttributes( array( 'foo' => null ) ),
55 'skip keys with null value'
56 );
57 $this->AssertEmpty(
58 Html::expandAttributes( array( 'foo' => false ) ),
59 'skip keys with false value'
60 );
61 $this->AssertNotEmpty(
62 Html::expandAttributes( array( 'foo' => '' ) ),
63 'keep keys with an empty string'
64 );
65 }
66
67 public function testExpandAttributesForBooleans() {
68 global $wgHtml5;
69 $this->AssertEquals(
70 '',
71 Html::expandAttributes( array( 'selected' => false ) ),
72 'Boolean attributes do not generates output when value is false'
73 );
74 $this->AssertEquals(
75 '',
76 Html::expandAttributes( array( 'selected' => null ) ),
77 'Boolean attributes do not generates output when value is null'
78 );
79
80 $this->AssertEquals(
81 $wgHtml5 ? ' selected=""' : ' selected="selected"',
82 Html::expandAttributes( array( 'selected' => true ) ),
83 'Boolean attributes skip value output'
84 );
85 $this->AssertEquals(
86 $wgHtml5 ? ' selected=""' : ' selected="selected"',
87 Html::expandAttributes( array( 'selected' ) ),
88 'Boolean attributes (ex: selected) do not need a value'
89 );
90 }
91
92 /**
93 * Test for Html::expandAttributes()
94 * Please note it output a string prefixed with a space!
95 */
96 public function testExpandAttributesVariousExpansions() {
97 ### NOT EMPTY ####
98 $this->AssertEquals(
99 ' empty_string=""',
100 Html::expandAttributes( array( 'empty_string' => '' ) ),
101 'Value with an empty string'
102 );
103 $this->AssertEquals(
104 ' key="value"',
105 Html::expandAttributes( array( 'key' => 'value' ) ),
106 'Value is a string'
107 );
108 $this->AssertEquals(
109 ' one="1"',
110 Html::expandAttributes( array( 'one' => 1 ) ),
111 'Value is a numeric one'
112 );
113 $this->AssertEquals(
114 ' zero="0"',
115 Html::expandAttributes( array( 'zero' => 0 ) ),
116 'Value is a numeric zero'
117 );
118 }
119
120 /**
121 * Html::expandAttributes has special features for HTML
122 * attributes that use space separated lists and also
123 * allows arrays to be used as values.
124 */
125 public function testExpandAttributesListValueAttributes() {
126 ### STRING VALUES
127 $this->AssertEquals(
128 ' class="redundant spaces here"',
129 Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
130 'Normalization should strip redundant spaces'
131 );
132 $this->AssertEquals(
133 ' class="foo bar"',
134 Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
135 'Normalization should remove duplicates in string-lists'
136 );
137 ### "EMPTY" ARRAY VALUES
138 $this->AssertEquals(
139 ' class=""',
140 Html::expandAttributes( array( 'class' => array() ) ),
141 'Value with an empty array'
142 );
143 $this->AssertEquals(
144 ' class=""',
145 Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
146 'Array with null, empty string and spaces'
147 );
148 ### NON-EMPTY ARRAY VALUES
149 $this->AssertEquals(
150 ' class="foo bar"',
151 Html::expandAttributes( array( 'class' => array(
152 'foo',
153 'bar',
154 'foo',
155 'bar',
156 'bar',
157 ) ) ),
158 'Normalization should remove duplicates in the array'
159 );
160 $this->AssertEquals(
161 ' class="foo bar"',
162 Html::expandAttributes( array( 'class' => array(
163 'foo bar',
164 'bar foo',
165 'foo',
166 'bar bar',
167 ) ) ),
168 'Normalization should remove duplicates in string-lists in the array'
169 );
170 }
171
172 /**
173 * Test feature added by r96188, let pass attributes values as
174 * a PHP array. Restricted to class,rel, accesskey.
175 */
176 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
177 $this->assertEquals(
178 ' class="booltrue one"',
179 Html::expandAttributes( array( 'class' => array(
180 'booltrue' => true,
181 'one' => 1,
182
183 # Method use isset() internally, make sure we do discard
184 # attributes values which have been assigned well known values
185 'emptystring' => '',
186 'boolfalse' => false,
187 'zero' => 0,
188 'null' => null,
189 )))
190 );
191 }
192
193 /**
194 * How do we handle duplicate keys in HTML attributes expansion?
195 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
196 * The later will take precedence.
197 *
198 * Feature added by r96188
199 */
200 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
201 $this->assertEquals(
202 ' class=""',
203 Html::expandAttributes( array( 'class' => array(
204 'GREEN',
205 'GREEN' => false,
206 'GREEN',
207 )))
208 );
209 }
210
211 function testNamespaceSelector() {
212 $this->assertEquals(
213 '<select id="namespace" name="namespace">
214 <option value="0">(Main)</option>
215 <option value="1">Talk</option>
216 <option value="2">User</option>
217 <option value="3">User talk</option>
218 <option value="4">MyWiki</option>
219 <option value="5">MyWiki Talk</option>
220 <option value="6">File</option>
221 <option value="7">File talk</option>
222 <option value="8">MediaWiki</option>
223 <option value="9">MediaWiki talk</option>
224 <option value="10">Template</option>
225 <option value="11">Template talk</option>
226 <option value="100">Custom</option>
227 <option value="101">Custom talk</option>
228 </select>',
229 Html::namespaceSelector(),
230 'Basic namespace selector without custom options'
231 );
232 $this->assertEquals(
233 '<label for="mw-test-namespace">Select a namespace:</label>&#160;<select id="mw-test-namespace" name="wpNamespace">
234 <option value="all">all</option>
235 <option value="0">(Main)</option>
236 <option value="1">Talk</option>
237 <option value="2" selected="">User</option>
238 <option value="3">User talk</option>
239 <option value="4">MyWiki</option>
240 <option value="5">MyWiki Talk</option>
241 <option value="6">File</option>
242 <option value="7">File talk</option>
243 <option value="8">MediaWiki</option>
244 <option value="9">MediaWiki talk</option>
245 <option value="10">Template</option>
246 <option value="11">Template talk</option>
247 <option value="100">Custom</option>
248 <option value="101">Custom talk</option>
249 </select>',
250 Html::namespaceSelector(
251 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
252 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
253 ),
254 'Basic namespace selector with custom values'
255 );
256 }
257 }