Merge "Adding description how to use Special:Redirect"
[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 $prevMonth = $curMonth - 1;
148 if ( $prevMonth == 0 ) {
149 $prevMonth = 12;
150 }
151 $nextMonth = $curMonth + 1;
152 if ( $nextMonth == 13 ) {
153 $nextMonth = 1;
154 }
155
156 $this->assertEquals(
157 '<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" .
158 '<option value="1">January</option>' . "\n" .
159 '<option value="2" selected="">February</option>' . "\n" .
160 '<option value="3">March</option>' . "\n" .
161 '<option value="4">April</option>' . "\n" .
162 '<option value="5">May</option>' . "\n" .
163 '<option value="6">June</option>' . "\n" .
164 '<option value="7">July</option>' . "\n" .
165 '<option value="8">August</option>' . "\n" .
166 '<option value="9">September</option>' . "\n" .
167 '<option value="10">October</option>' . "\n" .
168 '<option value="11">November</option>' . "\n" .
169 '<option value="12">December</option></select>',
170 Xml::dateMenu( 2011, 02 ),
171 "Date menu for february 2011"
172 );
173 $this->assertEquals(
174 '<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" .
175 '<option value="1">January</option>' . "\n" .
176 '<option value="2">February</option>' . "\n" .
177 '<option value="3">March</option>' . "\n" .
178 '<option value="4">April</option>' . "\n" .
179 '<option value="5">May</option>' . "\n" .
180 '<option value="6">June</option>' . "\n" .
181 '<option value="7">July</option>' . "\n" .
182 '<option value="8">August</option>' . "\n" .
183 '<option value="9">September</option>' . "\n" .
184 '<option value="10">October</option>' . "\n" .
185 '<option value="11">November</option>' . "\n" .
186 '<option value="12">December</option></select>',
187 Xml::dateMenu( 2011, -1 ),
188 "Date menu with negative month for 'All'"
189 );
190 $this->assertEquals(
191 Xml::dateMenu( $curYear, $curMonth ),
192 Xml::dateMenu( '', $curMonth ),
193 "Date menu year is the current one when not specified"
194 );
195
196 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
197 $this->assertEquals(
198 Xml::dateMenu( $wantedYear, $nextMonth ),
199 Xml::dateMenu( '', $nextMonth ),
200 "Date menu next month is 11 months ago"
201 );
202
203 $this->assertEquals(
204 '<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" .
205 '<option value="1">January</option>' . "\n" .
206 '<option value="2">February</option>' . "\n" .
207 '<option value="3">March</option>' . "\n" .
208 '<option value="4">April</option>' . "\n" .
209 '<option value="5">May</option>' . "\n" .
210 '<option value="6">June</option>' . "\n" .
211 '<option value="7">July</option>' . "\n" .
212 '<option value="8">August</option>' . "\n" .
213 '<option value="9">September</option>' . "\n" .
214 '<option value="10">October</option>' . "\n" .
215 '<option value="11">November</option>' . "\n" .
216 '<option value="12">December</option></select>',
217 Xml::dateMenu( '', '' ),
218 "Date menu with neither year or month"
219 );
220 }
221
222 /**
223 * @covers Xml::textarea
224 */
225 public function testTextareaNoContent() {
226 $this->assertEquals(
227 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
228 Xml::textarea( 'name', '' ),
229 'textarea() with not content'
230 );
231 }
232
233 /**
234 * @covers Xml::textarea
235 */
236 public function testTextareaAttribs() {
237 $this->assertEquals(
238 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
239 Xml::textarea( 'name', '<txt>', 20, 10 ),
240 'textarea() with custom attribs'
241 );
242 }
243
244 /**
245 * @covers Xml::label
246 */
247 public function testLabelCreation() {
248 $this->assertEquals(
249 '<label for="id">name</label>',
250 Xml::label( 'name', 'id' ),
251 'label() with no attribs'
252 );
253 }
254
255 /**
256 * @covers Xml::label
257 */
258 public function testLabelAttributeCanOnlyBeClassOrTitle() {
259 $this->assertEquals(
260 '<label for="id">name</label>',
261 Xml::label( 'name', 'id', array( 'generated' => true ) ),
262 'label() can not be given a generated attribute'
263 );
264 $this->assertEquals(
265 '<label for="id" class="nice">name</label>',
266 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
267 'label() can get a class attribute'
268 );
269 $this->assertEquals(
270 '<label for="id" title="nice tooltip">name</label>',
271 Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
272 'label() can get a title attribute'
273 );
274 $this->assertEquals(
275 '<label for="id" class="nice" title="nice tooltip">name</label>',
276 Xml::label( 'name', 'id', array(
277 'generated' => true,
278 'class' => 'nice',
279 'title' => 'nice tooltip',
280 'anotherattr' => 'value',
281 )
282 ),
283 'label() skip all attributes but "class" and "title"'
284 );
285 }
286
287 /**
288 * @covers Xml::languageSelector
289 */
290 public function testLanguageSelector() {
291 $select = Xml::languageSelector( 'en', true, null,
292 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
293 $this->assertEquals(
294 '<label for="testlang">Language:</label>',
295 $select[0]
296 );
297 }
298
299 /**
300 * @covers Xml::escapeJsString
301 */
302 public function testEscapeJsStringSpecialChars() {
303 $this->assertEquals(
304 '\\\\\r\n',
305 Xml::escapeJsString( "\\\r\n" ),
306 'escapeJsString() with special characters'
307 );
308 }
309
310 /**
311 * @covers Xml::encodeJsVar
312 */
313 public function testEncodeJsVarBoolean() {
314 $this->assertEquals(
315 'true',
316 Xml::encodeJsVar( true ),
317 'encodeJsVar() with boolean'
318 );
319 }
320
321 /**
322 * @covers Xml::encodeJsVar
323 */
324 public function testEncodeJsVarNull() {
325 $this->assertEquals(
326 'null',
327 Xml::encodeJsVar( null ),
328 'encodeJsVar() with null'
329 );
330 }
331
332 /**
333 * @covers Xml::encodeJsVar
334 */
335 public function testEncodeJsVarArray() {
336 $this->assertEquals(
337 '["a",1]',
338 Xml::encodeJsVar( array( 'a', 1 ) ),
339 'encodeJsVar() with array'
340 );
341 $this->assertEquals(
342 '{"a":"a","b":1}',
343 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
344 'encodeJsVar() with associative array'
345 );
346 }
347
348 /**
349 * @covers Xml::encodeJsVar
350 */
351 public function testEncodeJsVarObject() {
352 $this->assertEquals(
353 '{"a":"a","b":1}',
354 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
355 'encodeJsVar() with object'
356 );
357 }
358
359 /**
360 * @covers Xml::encodeJsVar
361 */
362 public function testEncodeJsVarInt() {
363 $this->assertEquals(
364 '123456',
365 Xml::encodeJsVar( 123456 ),
366 'encodeJsVar() with int'
367 );
368 }
369
370 /**
371 * @covers Xml::encodeJsVar
372 */
373 public function testEncodeJsVarFloat() {
374 $this->assertEquals(
375 '1.23456',
376 Xml::encodeJsVar( 1.23456 ),
377 'encodeJsVar() with float'
378 );
379 }
380
381 /**
382 * @covers Xml::encodeJsVar
383 */
384 public function testEncodeJsVarIntString() {
385 $this->assertEquals(
386 '"123456"',
387 Xml::encodeJsVar( '123456' ),
388 'encodeJsVar() with int-like string'
389 );
390 }
391
392 /**
393 * @covers Xml::encodeJsVar
394 */
395 public function testEncodeJsVarFloatString() {
396 $this->assertEquals(
397 '"1.23456"',
398 Xml::encodeJsVar( '1.23456' ),
399 'encodeJsVar() with float-like string'
400 );
401 }
402 }