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