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