Merge "[LockManager] Cleaned up DBLockManager and reduced code duplication."
[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 /**
116 * @group Broken
117 */
118 public function testDateMenu( ) {
119 $curYear = intval(gmdate('Y'));
120 $prevYear = $curYear - 1;
121
122 $curMonth = intval(gmdate('n'));
123 $prevMonth = $curMonth - 1;
124 if( $prevMonth == 0 ) { $prevMonth = 12; }
125 $nextMonth = $curMonth + 1;
126 if( $nextMonth == 13 ) { $nextMonth = 1; }
127
128 $this->assertEquals(
129 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="2011" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
130 '<option value="1">January</option>' . "\n" .
131 '<option value="2" selected="">February</option>' . "\n" .
132 '<option value="3">March</option>' . "\n" .
133 '<option value="4">April</option>' . "\n" .
134 '<option value="5">May</option>' . "\n" .
135 '<option value="6">June</option>' . "\n" .
136 '<option value="7">July</option>' . "\n" .
137 '<option value="8">August</option>' . "\n" .
138 '<option value="9">September</option>' . "\n" .
139 '<option value="10">October</option>' . "\n" .
140 '<option value="11">November</option>' . "\n" .
141 '<option value="12">December</option></select>',
142 Xml::dateMenu( 2011, 02 ),
143 "Date menu for february 2011"
144 );
145 $this->assertEquals(
146 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="2011" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
147 '<option value="1">January</option>' . "\n" .
148 '<option value="2">February</option>' . "\n" .
149 '<option value="3">March</option>' . "\n" .
150 '<option value="4">April</option>' . "\n" .
151 '<option value="5">May</option>' . "\n" .
152 '<option value="6">June</option>' . "\n" .
153 '<option value="7">July</option>' . "\n" .
154 '<option value="8">August</option>' . "\n" .
155 '<option value="9">September</option>' . "\n" .
156 '<option value="10">October</option>' . "\n" .
157 '<option value="11">November</option>' . "\n" .
158 '<option value="12">December</option></select>',
159 Xml::dateMenu( 2011, -1),
160 "Date menu with negative month for 'All'"
161 );
162 $this->assertEquals(
163 Xml::dateMenu( $curYear, $curMonth ),
164 Xml::dateMenu( '' , $curMonth ),
165 "Date menu year is the current one when not specified"
166 );
167
168 // @todo FIXME: next month can be in the next year
169 // test failing because it is now december
170 $this->assertEquals(
171 Xml::dateMenu( $prevYear, $nextMonth ),
172 Xml::dateMenu( '', $nextMonth ),
173 "Date menu next month is 11 months ago"
174 );
175
176 # @todo FIXME: Please note there is no year there!
177 $this->assertEquals(
178 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
179 '<option value="1">January</option>' . "\n" .
180 '<option value="2">February</option>' . "\n" .
181 '<option value="3">March</option>' . "\n" .
182 '<option value="4">April</option>' . "\n" .
183 '<option value="5">May</option>' . "\n" .
184 '<option value="6">June</option>' . "\n" .
185 '<option value="7">July</option>' . "\n" .
186 '<option value="8">August</option>' . "\n" .
187 '<option value="9">September</option>' . "\n" .
188 '<option value="10">October</option>' . "\n" .
189 '<option value="11">November</option>' . "\n" .
190 '<option value="12">December</option></select>',
191 Xml::dateMenu( '', '' ),
192 "Date menu with neither year or month"
193 );
194 }
195
196 #
197 # textarea
198 #
199 function testTextareaNoContent() {
200 $this->assertEquals(
201 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
202 Xml::textarea( 'name', '' ),
203 'textarea() with not content'
204 );
205 }
206
207 function testTextareaAttribs() {
208 $this->assertEquals(
209 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
210 Xml::textarea( 'name', '<txt>', 20, 10 ),
211 'textarea() with custom attribs'
212 );
213 }
214
215 #
216 # input and label
217 #
218 function testLabelCreation() {
219 $this->assertEquals(
220 '<label for="id">name</label>',
221 Xml::label( 'name', 'id' ),
222 'label() with no attribs'
223 );
224 }
225 function testLabelAttributeCanOnlyBeClassOrTitle() {
226 $this->assertEquals(
227 '<label for="id">name</label>',
228 Xml::label( 'name', 'id', array( 'generated' => true ) ),
229 'label() can not be given a generated attribute'
230 );
231 $this->assertEquals(
232 '<label for="id" class="nice">name</label>',
233 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
234 'label() can get a class attribute'
235 );
236 $this->assertEquals(
237 '<label for="id" title="nice tooltip">name</label>',
238 Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
239 'label() can get a title attribute'
240 );
241 $this->assertEquals(
242 '<label for="id" class="nice" title="nice tooltip">name</label>',
243 Xml::label( 'name', 'id', array(
244 'generated' => true,
245 'class' => 'nice',
246 'title' => 'nice tooltip',
247 'anotherattr' => 'value',
248 )
249 ),
250 'label() skip all attributes but "class" and "title"'
251 );
252 }
253
254 function testLanguageSelector() {
255 $select = Xml::languageSelector( 'en', true, null,
256 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
257 $this->assertEquals(
258 '<label for="testlang">Language:</label>',
259 $select[0]
260 );
261 }
262
263 #
264 # JS
265 #
266 function testEscapeJsStringSpecialChars() {
267 $this->assertEquals(
268 '\\\\\r\n',
269 Xml::escapeJsString( "\\\r\n" ),
270 'escapeJsString() with special characters'
271 );
272 }
273
274 function testEncodeJsVarBoolean() {
275 $this->assertEquals(
276 'true',
277 Xml::encodeJsVar( true ),
278 'encodeJsVar() with boolean'
279 );
280 }
281
282 function testEncodeJsVarNull() {
283 $this->assertEquals(
284 'null',
285 Xml::encodeJsVar( null ),
286 'encodeJsVar() with null'
287 );
288 }
289
290 function testEncodeJsVarArray() {
291 $this->assertEquals(
292 '["a",1]',
293 Xml::encodeJsVar( array( 'a', 1 ) ),
294 'encodeJsVar() with array'
295 );
296 $this->assertEquals(
297 '{"a":"a","b":1}',
298 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
299 'encodeJsVar() with associative array'
300 );
301 }
302
303 function testEncodeJsVarObject() {
304 $this->assertEquals(
305 '{"a":"a","b":1}',
306 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
307 'encodeJsVar() with object'
308 );
309 }
310
311 function testEncodeJsVarInt() {
312 $this->assertEquals(
313 '123456',
314 Xml::encodeJsVar( 123456 ),
315 'encodeJsVar() with int'
316 );
317 }
318
319 function testEncodeJsVarFloat() {
320 $this->assertEquals(
321 '1.23456',
322 Xml::encodeJsVar( 1.23456 ),
323 'encodeJsVar() with float'
324 );
325 }
326
327 function testEncodeJsVarIntString() {
328 $this->assertEquals(
329 '"123456"',
330 Xml::encodeJsVar( '123456' ),
331 'encodeJsVar() with int-like string'
332 );
333 }
334
335 function testEncodeJsVarFloatString() {
336 $this->assertEquals(
337 '"1.23456"',
338 Xml::encodeJsVar( '1.23456' ),
339 'encodeJsVar() with float-like string'
340 );
341 }
342 }