f48232874b68c573ef09fdf494de49334fe3d57b
[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 protected function setUp() {
8 parent::setUp();
9
10 $langObj = Language::factory( 'en' );
11 $langObj->setNamespaces( array(
12 -2 => 'Media',
13 -1 => 'Special',
14 0 => '',
15 1 => 'Talk',
16 2 => 'User',
17 3 => 'User_talk',
18 4 => 'MyWiki',
19 5 => 'MyWiki_Talk',
20 6 => 'File',
21 7 => 'File_talk',
22 8 => 'MediaWiki',
23 9 => 'MediaWiki_talk',
24 10 => 'Template',
25 11 => 'Template_talk',
26 100 => 'Custom',
27 101 => 'Custom_talk',
28 ) );
29
30 $this->setMwGlobals( array(
31 'wgLang' => $langObj,
32 'wgHtml5' => true,
33 'wgWellFormedXml' => true,
34 ) );
35 }
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 public function testExpandAttributesException() {
47 $this->setExpectedException( 'MWException' );
48 Xml::expandAttributes( 'string' );
49 }
50
51 function testElementOpen() {
52 $this->assertEquals(
53 '<element>',
54 Xml::element( 'element', null, null ),
55 'Opening element with no attributes'
56 );
57 }
58
59 function testElementEmpty() {
60 $this->assertEquals(
61 '<element />',
62 Xml::element( 'element', null, '' ),
63 'Terminated empty element'
64 );
65 }
66
67 function testElementInputCanHaveAValueOfZero() {
68 $this->assertEquals(
69 '<input name="name" value="0" />',
70 Xml::input( 'name', false, 0 ),
71 'Input with a value of 0 (bug 23797)'
72 );
73 }
74
75 function testElementEscaping() {
76 $this->assertEquals(
77 '<element>hello &lt;there&gt; you &amp; you</element>',
78 Xml::element( 'element', null, 'hello <there> you & you' ),
79 'Element with no attributes and content that needs escaping'
80 );
81 }
82
83 public function testEscapeTagsOnly() {
84 $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
85 'replace " > and < with their HTML entitites'
86 );
87 }
88
89 function testElementAttributes() {
90 $this->assertEquals(
91 '<element key="value" <>="&lt;&gt;">',
92 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
93 'Element attributes, keys are not escaped'
94 );
95 }
96
97 function testOpenElement() {
98 $this->assertEquals(
99 '<element k="v">',
100 Xml::openElement( 'element', array( 'k' => 'v' ) ),
101 'openElement() shortcut'
102 );
103 }
104
105 function testCloseElement() {
106 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
107 }
108
109 public function testDateMenu() {
110 $curYear = intval( gmdate( 'Y' ) );
111 $prevYear = $curYear - 1;
112
113 $curMonth = intval( gmdate( 'n' ) );
114 $prevMonth = $curMonth - 1;
115 if ( $prevMonth == 0 ) {
116 $prevMonth = 12;
117 }
118 $nextMonth = $curMonth + 1;
119 if ( $nextMonth == 13 ) {
120 $nextMonth = 1;
121 }
122
123 $this->assertEquals(
124 '<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" .
125 '<option value="1">January</option>' . "\n" .
126 '<option value="2" selected="">February</option>' . "\n" .
127 '<option value="3">March</option>' . "\n" .
128 '<option value="4">April</option>' . "\n" .
129 '<option value="5">May</option>' . "\n" .
130 '<option value="6">June</option>' . "\n" .
131 '<option value="7">July</option>' . "\n" .
132 '<option value="8">August</option>' . "\n" .
133 '<option value="9">September</option>' . "\n" .
134 '<option value="10">October</option>' . "\n" .
135 '<option value="11">November</option>' . "\n" .
136 '<option value="12">December</option></select>',
137 Xml::dateMenu( 2011, 02 ),
138 "Date menu for february 2011"
139 );
140 $this->assertEquals(
141 '<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" .
142 '<option value="1">January</option>' . "\n" .
143 '<option value="2">February</option>' . "\n" .
144 '<option value="3">March</option>' . "\n" .
145 '<option value="4">April</option>' . "\n" .
146 '<option value="5">May</option>' . "\n" .
147 '<option value="6">June</option>' . "\n" .
148 '<option value="7">July</option>' . "\n" .
149 '<option value="8">August</option>' . "\n" .
150 '<option value="9">September</option>' . "\n" .
151 '<option value="10">October</option>' . "\n" .
152 '<option value="11">November</option>' . "\n" .
153 '<option value="12">December</option></select>',
154 Xml::dateMenu( 2011, -1 ),
155 "Date menu with negative month for 'All'"
156 );
157 $this->assertEquals(
158 Xml::dateMenu( $curYear, $curMonth ),
159 Xml::dateMenu( '', $curMonth ),
160 "Date menu year is the current one when not specified"
161 );
162
163 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
164 $this->assertEquals(
165 Xml::dateMenu( $wantedYear, $nextMonth ),
166 Xml::dateMenu( '', $nextMonth ),
167 "Date menu next month is 11 months ago"
168 );
169
170 $this->assertEquals(
171 '<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" .
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( '', '' ),
185 "Date menu with neither year or month"
186 );
187 }
188
189 #
190 # textarea
191 #
192 function testTextareaNoContent() {
193 $this->assertEquals(
194 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
195 Xml::textarea( 'name', '' ),
196 'textarea() with not content'
197 );
198 }
199
200 function testTextareaAttribs() {
201 $this->assertEquals(
202 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
203 Xml::textarea( 'name', '<txt>', 20, 10 ),
204 'textarea() with custom attribs'
205 );
206 }
207
208 #
209 # input and label
210 #
211 function testLabelCreation() {
212 $this->assertEquals(
213 '<label for="id">name</label>',
214 Xml::label( 'name', 'id' ),
215 'label() with no attribs'
216 );
217 }
218
219 function testLabelAttributeCanOnlyBeClassOrTitle() {
220 $this->assertEquals(
221 '<label for="id">name</label>',
222 Xml::label( 'name', 'id', array( 'generated' => true ) ),
223 'label() can not be given a generated attribute'
224 );
225 $this->assertEquals(
226 '<label for="id" class="nice">name</label>',
227 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
228 'label() can get a class attribute'
229 );
230 $this->assertEquals(
231 '<label for="id" title="nice tooltip">name</label>',
232 Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
233 'label() can get a title attribute'
234 );
235 $this->assertEquals(
236 '<label for="id" class="nice" title="nice tooltip">name</label>',
237 Xml::label( 'name', 'id', array(
238 'generated' => true,
239 'class' => 'nice',
240 'title' => 'nice tooltip',
241 'anotherattr' => 'value',
242 )
243 ),
244 'label() skip all attributes but "class" and "title"'
245 );
246 }
247
248 function testLanguageSelector() {
249 $select = Xml::languageSelector( 'en', true, null,
250 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
251 $this->assertEquals(
252 '<label for="testlang">Language:</label>',
253 $select[0]
254 );
255 }
256
257 #
258 # JS
259 #
260 function testEscapeJsStringSpecialChars() {
261 $this->assertEquals(
262 '\\\\\r\n',
263 Xml::escapeJsString( "\\\r\n" ),
264 'escapeJsString() with special characters'
265 );
266 }
267
268 function testEncodeJsVarBoolean() {
269 $this->assertEquals(
270 'true',
271 Xml::encodeJsVar( true ),
272 'encodeJsVar() with boolean'
273 );
274 }
275
276 function testEncodeJsVarNull() {
277 $this->assertEquals(
278 'null',
279 Xml::encodeJsVar( null ),
280 'encodeJsVar() with null'
281 );
282 }
283
284 function testEncodeJsVarArray() {
285 $this->assertEquals(
286 '["a",1]',
287 Xml::encodeJsVar( array( 'a', 1 ) ),
288 'encodeJsVar() with array'
289 );
290 $this->assertEquals(
291 '{"a":"a","b":1}',
292 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
293 'encodeJsVar() with associative array'
294 );
295 }
296
297 function testEncodeJsVarObject() {
298 $this->assertEquals(
299 '{"a":"a","b":1}',
300 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
301 'encodeJsVar() with object'
302 );
303 }
304
305 function testEncodeJsVarInt() {
306 $this->assertEquals(
307 '123456',
308 Xml::encodeJsVar( 123456 ),
309 'encodeJsVar() with int'
310 );
311 }
312
313 function testEncodeJsVarFloat() {
314 $this->assertEquals(
315 '1.23456',
316 Xml::encodeJsVar( 1.23456 ),
317 'encodeJsVar() with float'
318 );
319 }
320
321 function testEncodeJsVarIntString() {
322 $this->assertEquals(
323 '"123456"',
324 Xml::encodeJsVar( '123456' ),
325 'encodeJsVar() with int-like string'
326 );
327 }
328
329 function testEncodeJsVarFloatString() {
330 $this->assertEquals(
331 '"1.23456"',
332 Xml::encodeJsVar( '1.23456' ),
333 'encodeJsVar() with float-like string'
334 );
335 }
336 }