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