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