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