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