[Unit testing] Re-order attribs to a-z to make testing more reliable
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlTest.php
1 <?php
2
3 class XmlTest extends MediaWikiTestCase {
4 private static $oldLang;
5 private static $oldNamespaces;
6
7 public function setUp() {
8 global $wgLang, $wgContLang;
9
10 self::$oldLang = $wgLang;
11 $wgLang = Language::factory( 'en' );
12
13 // Hardcode namespaces during test runs,
14 // so that html output based on existing namespaces
15 // can be properly evaluated.
16 self::$oldNamespaces = $wgContLang->namespaceNames;
17 $wgContLang->namespaceNames = array(
18 -2 => 'Media',
19 -1 => 'Special',
20 0 => '',
21 1 => 'Talk',
22 2 => 'User',
23 3 => 'User_talk',
24 4 => 'MyWiki',
25 5 => 'MyWiki_Talk',
26 6 => 'File',
27 7 => 'File_talk',
28 8 => 'MediaWiki',
29 9 => 'MediaWiki_talk',
30 10 => 'Template',
31 11 => 'Template_talk',
32 100 => 'Custom',
33 101 => 'Custom_talk',
34 );
35 }
36
37 public function tearDown() {
38 global $wgLang, $wgContLang;
39 $wgLang = self::$oldLang;
40 $wgContLang->namespaceNames = self::$oldNamespaces;
41 }
42
43 public function testExpandAttributes() {
44 $this->assertNull( Xml::expandAttributes(null),
45 'Converting a null list of attributes'
46 );
47 $this->assertEquals( '', Xml::expandAttributes( array() ),
48 'Converting an empty list of attributes'
49 );
50 }
51
52 public function testExpandAttributesException() {
53 $this->setExpectedException('MWException');
54 Xml::expandAttributes('string');
55 }
56
57 function testElementOpen() {
58 $this->assertEquals(
59 '<element>',
60 Xml::element( 'element', null, null ),
61 'Opening element with no attributes'
62 );
63 }
64
65 function testElementEmpty() {
66 $this->assertEquals(
67 '<element />',
68 Xml::element( 'element', null, '' ),
69 'Terminated empty element'
70 );
71 }
72
73 function testElementInputCanHaveAValueOfZero() {
74 $this->assertEquals(
75 '<input name="name" value="0" />',
76 Xml::input( 'name', false, 0 ),
77 'Input with a value of 0 (bug 23797)'
78 );
79 }
80 function testElementEscaping() {
81 $this->assertEquals(
82 '<element>hello &lt;there&gt; you &amp; you</element>',
83 Xml::element( 'element', null, 'hello <there> you & you' ),
84 'Element with no attributes and content that needs escaping'
85 );
86 }
87
88 public function testEscapeTagsOnly() {
89 $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
90 'replace " > and < with their HTML entitites'
91 );
92 }
93
94 function testElementAttributes() {
95 $this->assertEquals(
96 '<element key="value" <>="&lt;&gt;">',
97 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
98 'Element attributes, keys are not escaped'
99 );
100 }
101
102 function testOpenElement() {
103 $this->assertEquals(
104 '<element k="v">',
105 Xml::openElement( 'element', array( 'k' => 'v' ) ),
106 'openElement() shortcut'
107 );
108 }
109
110 function testCloseElement() {
111 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
112 }
113
114 /**
115 * @group Broken
116 */
117 public function testDateMenu( ) {
118 $curYear = intval(gmdate('Y'));
119 $prevYear = $curYear - 1;
120
121 $curMonth = intval(gmdate('n'));
122 $prevMonth = $curMonth - 1;
123 if( $prevMonth == 0 ) { $prevMonth = 12; }
124 $nextMonth = $curMonth + 1;
125 if( $nextMonth == 13 ) { $nextMonth = 1; }
126
127 $this->assertEquals(
128 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="2011" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
129 '<option value="1">January</option>' . "\n" .
130 '<option value="2" selected="">February</option>' . "\n" .
131 '<option value="3">March</option>' . "\n" .
132 '<option value="4">April</option>' . "\n" .
133 '<option value="5">May</option>' . "\n" .
134 '<option value="6">June</option>' . "\n" .
135 '<option value="7">July</option>' . "\n" .
136 '<option value="8">August</option>' . "\n" .
137 '<option value="9">September</option>' . "\n" .
138 '<option value="10">October</option>' . "\n" .
139 '<option value="11">November</option>' . "\n" .
140 '<option value="12">December</option></select>',
141 Xml::dateMenu( 2011, 02 ),
142 "Date menu for february 2011"
143 );
144 $this->assertEquals(
145 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="2011" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
146 '<option value="1">January</option>' . "\n" .
147 '<option value="2">February</option>' . "\n" .
148 '<option value="3">March</option>' . "\n" .
149 '<option value="4">April</option>' . "\n" .
150 '<option value="5">May</option>' . "\n" .
151 '<option value="6">June</option>' . "\n" .
152 '<option value="7">July</option>' . "\n" .
153 '<option value="8">August</option>' . "\n" .
154 '<option value="9">September</option>' . "\n" .
155 '<option value="10">October</option>' . "\n" .
156 '<option value="11">November</option>' . "\n" .
157 '<option value="12">December</option></select>',
158 Xml::dateMenu( 2011, -1),
159 "Date menu with negative month for 'All'"
160 );
161 $this->assertEquals(
162 Xml::dateMenu( $curYear, $curMonth ),
163 Xml::dateMenu( '' , $curMonth ),
164 "Date menu year is the current one when not specified"
165 );
166
167 // @todo FIXME: next month can be in the next year
168 // test failing because it is now december
169 $this->assertEquals(
170 Xml::dateMenu( $prevYear, $nextMonth ),
171 Xml::dateMenu( '', $nextMonth ),
172 "Date menu next month is 11 months ago"
173 );
174
175 # @todo FIXME: Please note there is no year there!
176 $this->assertEquals(
177 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
178 '<option value="1">January</option>' . "\n" .
179 '<option value="2">February</option>' . "\n" .
180 '<option value="3">March</option>' . "\n" .
181 '<option value="4">April</option>' . "\n" .
182 '<option value="5">May</option>' . "\n" .
183 '<option value="6">June</option>' . "\n" .
184 '<option value="7">July</option>' . "\n" .
185 '<option value="8">August</option>' . "\n" .
186 '<option value="9">September</option>' . "\n" .
187 '<option value="10">October</option>' . "\n" .
188 '<option value="11">November</option>' . "\n" .
189 '<option value="12">December</option></select>',
190 Xml::dateMenu( '', '' ),
191 "Date menu with neither year or month"
192 );
193 }
194
195 function testNamespaceSelector() {
196 $this->assertEquals(
197 '<select class="namespaceselector" id="namespace" name="namespace">
198 <option value="0">(Main)</option>
199 <option value="1">Talk</option>
200 <option value="2">User</option>
201 <option value="3">User talk</option>
202 <option value="4">MyWiki</option>
203 <option value="5">MyWiki Talk</option>
204 <option value="6">File</option>
205 <option value="7">File talk</option>
206 <option value="8">MediaWiki</option>
207 <option value="9">MediaWiki talk</option>
208 <option value="10">Template</option>
209 <option value="11">Template talk</option>
210 <option value="100">Custom</option>
211 <option value="101">Custom talk</option>
212 </select>',
213 Xml::namespaceSelector(),
214 'Basic namespace selector without custom options'
215 );
216 $this->assertEquals(
217 '<label for="myname">Select a namespace:</label>&#160;<select class="namespaceselector" id="namespace" name="myname">
218 <option value="all">all</option>
219 <option value="0">(Main)</option>
220 <option value="1">Talk</option>
221 <option value="2" selected="">User</option>
222 <option value="3">User talk</option>
223 <option value="4">MyWiki</option>
224 <option value="5">MyWiki Talk</option>
225 <option value="6">File</option>
226 <option value="7">File talk</option>
227 <option value="8">MediaWiki</option>
228 <option value="9">MediaWiki talk</option>
229 <option value="10">Template</option>
230 <option value="11">Template talk</option>
231 <option value="100">Custom</option>
232 <option value="101">Custom talk</option>
233 </select>',
234 Xml::namespaceSelector( $selected = '2', $all = 'all', $element_name = 'myname', $label = 'Select a namespace:' ),
235 'Basic namespace selector with custom values'
236 );
237 }
238
239
240 #
241 # textarea
242 #
243 function testTextareaNoContent() {
244 $this->assertEquals(
245 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
246 Xml::textarea( 'name', '' ),
247 'textarea() with not content'
248 );
249 }
250
251 function testTextareaAttribs() {
252 $this->assertEquals(
253 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
254 Xml::textarea( 'name', '<txt>', 20, 10 ),
255 'textarea() with custom attribs'
256 );
257 }
258
259 #
260 # input and label
261 #
262 function testLabelCreation() {
263 $this->assertEquals(
264 '<label for="id">name</label>',
265 Xml::label( 'name', 'id' ),
266 'label() with no attribs'
267 );
268 }
269 function testLabelAttributeCanOnlyBeClassOrTitle() {
270 $this->assertEquals(
271 '<label for="id">name</label>',
272 Xml::label( 'name', 'id', array( 'generated' => true ) ),
273 'label() can not be given a generated attribute'
274 );
275 $this->assertEquals(
276 '<label for="id" class="nice">name</label>',
277 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
278 'label() can get a class attribute'
279 );
280 $this->assertEquals(
281 '<label for="id" title="nice tooltip">name</label>',
282 Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
283 'label() can get a title attribute'
284 );
285 $this->assertEquals(
286 '<label for="id" class="nice" title="nice tooltip">name</label>',
287 Xml::label( 'name', 'id', array(
288 'generated' => true,
289 'class' => 'nice',
290 'title' => 'nice tooltip',
291 'anotherattr' => 'value',
292 )
293 ),
294 'label() skip all attributes but "class" and "title"'
295 );
296 }
297
298 #
299 # JS
300 #
301 function testEscapeJsStringSpecialChars() {
302 $this->assertEquals(
303 '\\\\\r\n',
304 Xml::escapeJsString( "\\\r\n" ),
305 'escapeJsString() with special characters'
306 );
307 }
308
309 function testEncodeJsVarBoolean() {
310 $this->assertEquals(
311 'true',
312 Xml::encodeJsVar( true ),
313 'encodeJsVar() with boolean'
314 );
315 }
316
317 function testEncodeJsVarNull() {
318 $this->assertEquals(
319 'null',
320 Xml::encodeJsVar( null ),
321 'encodeJsVar() with null'
322 );
323 }
324
325 function testEncodeJsVarArray() {
326 $this->assertEquals(
327 '["a", 1]',
328 Xml::encodeJsVar( array( 'a', 1 ) ),
329 'encodeJsVar() with array'
330 );
331 $this->assertEquals(
332 '{"a": "a", "b": 1}',
333 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
334 'encodeJsVar() with associative array'
335 );
336 }
337
338 function testEncodeJsVarObject() {
339 $this->assertEquals(
340 '{"a": "a", "b": 1}',
341 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
342 'encodeJsVar() with object'
343 );
344 }
345
346 function testEncodeJsVarInt() {
347 $this->assertEquals(
348 '123456',
349 Xml::encodeJsVar( 123456 ),
350 'encodeJsVar() with int'
351 );
352 }
353
354 function testEncodeJsVarFloat() {
355 $this->assertEquals(
356 '1.23456',
357 Xml::encodeJsVar( 1.23456 ),
358 'encodeJsVar() with float'
359 );
360 }
361
362 function testEncodeJsVarIntString() {
363 $this->assertEquals(
364 '"123456"',
365 Xml::encodeJsVar( '123456' ),
366 'encodeJsVar() with int-like string'
367 );
368 }
369
370 function testEncodeJsVarFloatString() {
371 $this->assertEquals(
372 '"1.23456"',
373 Xml::encodeJsVar( '1.23456' ),
374 'encodeJsVar() with float-like string'
375 );
376 }
377 }