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