Use consistent notation for "@todo FIXME". Should update http://svn.wikimedia.org...
[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 public function testDateMenu( ) {
92 $curYear = intval(gmdate('Y'));
93 $prevYear = $curYear - 1;
94
95 $curMonth = intval(gmdate('n'));
96 $prevMonth = $curMonth - 1;
97 if( $prevMonth == 0 ) { $prevMonth = 12; }
98 $nextMonth = $curMonth + 1;
99 if( $nextMonth == 13 ) { $nextMonth = 1; }
100
101
102 $this->assertEquals(
103 '<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>
104 <option value="1">January</option>
105 <option value="2" selected="selected">February</option>
106 <option value="3">March</option>
107 <option value="4">April</option>
108 <option value="5">May</option>
109 <option value="6">June</option>
110 <option value="7">July</option>
111 <option value="8">August</option>
112 <option value="9">September</option>
113 <option value="10">October</option>
114 <option value="11">November</option>
115 <option value="12">December</option></select>',
116 Xml::dateMenu( 2011, 02 ),
117 "Date menu for february 2011"
118 );
119 $this->assertEquals(
120 '<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>
121 <option value="1">January</option>
122 <option value="2">February</option>
123 <option value="3">March</option>
124 <option value="4">April</option>
125 <option value="5">May</option>
126 <option value="6">June</option>
127 <option value="7">July</option>
128 <option value="8">August</option>
129 <option value="9">September</option>
130 <option value="10">October</option>
131 <option value="11">November</option>
132 <option value="12">December</option></select>',
133 Xml::dateMenu( 2011, -1),
134 "Date menu with negative month for 'All'"
135 );
136 $this->assertEquals(
137 Xml::dateMenu( $curYear, $curMonth ),
138 Xml::dateMenu( '' , $curMonth ),
139 "Date menu year is the current one when not specified"
140 );
141 $this->assertEquals(
142 Xml::dateMenu( $prevYear, $nextMonth ),
143 Xml::dateMenu( '', $nextMonth ),
144 "Date menu next month is 11 months ago"
145 );
146
147 # @todo FIXME: Please note there is no year there!
148 $this->assertEquals(
149 '<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>
150 <option value="1">January</option>
151 <option value="2">February</option>
152 <option value="3">March</option>
153 <option value="4">April</option>
154 <option value="5">May</option>
155 <option value="6">June</option>
156 <option value="7">July</option>
157 <option value="8">August</option>
158 <option value="9">September</option>
159 <option value="10">October</option>
160 <option value="11">November</option>
161 <option value="12">December</option></select>',
162 Xml::dateMenu( '', ''),
163 "Date menu with neither year or month"
164 );
165 }
166
167 #
168 # textarea
169 #
170 function testTextareaNoContent() {
171 $this->assertEquals(
172 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
173 Xml::textarea( 'name', '' ),
174 'textarea() with not content'
175 );
176 }
177
178 function testTextareaAttribs() {
179 $this->assertEquals(
180 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
181 Xml::textarea( 'name', '<txt>', 20, 10 ),
182 'textarea() with custom attribs'
183 );
184 }
185
186 #
187 # input and label
188 #
189 function testLabelCreation() {
190 $this->assertEquals(
191 '<label for="id">name</label>',
192 Xml::label( 'name', 'id' ),
193 'label() with no attribs'
194 );
195 }
196 function testLabelAttributeCanOnlyBeClass() {
197 $this->assertEquals(
198 '<label for="id">name</label>',
199 Xml::label( 'name', 'id', array( 'generated' => true ) ),
200 'label() can not be given a generated attribute'
201 );
202 $this->assertEquals(
203 '<label for="id" class="nice">name</label>',
204 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
205 'label() can get a class attribute'
206 );
207 $this->assertEquals(
208 '<label for="id" class="nice">name</label>',
209 Xml::label( 'name', 'id', array(
210 'generated' => true,
211 'class' => 'nice',
212 'anotherattr' => 'value',
213 )
214 ),
215 'label() skip all attributes but "class"'
216 );
217 }
218
219 #
220 # JS
221 #
222 function testEscapeJsStringSpecialChars() {
223 $this->assertEquals(
224 '\\\\\r\n',
225 Xml::escapeJsString( "\\\r\n" ),
226 'escapeJsString() with special characters'
227 );
228 }
229
230 function testEncodeJsVarBoolean() {
231 $this->assertEquals(
232 'true',
233 Xml::encodeJsVar( true ),
234 'encodeJsVar() with boolean'
235 );
236 }
237
238 function testEncodeJsVarNull() {
239 $this->assertEquals(
240 'null',
241 Xml::encodeJsVar( null ),
242 'encodeJsVar() with null'
243 );
244 }
245
246 function testEncodeJsVarArray() {
247 $this->assertEquals(
248 '["a", 1]',
249 Xml::encodeJsVar( array( 'a', 1 ) ),
250 'encodeJsVar() with array'
251 );
252 $this->assertEquals(
253 '{"a": "a", "b": 1}',
254 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
255 'encodeJsVar() with associative array'
256 );
257 }
258
259 function testEncodeJsVarObject() {
260 $this->assertEquals(
261 '{"a": "a", "b": 1}',
262 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
263 'encodeJsVar() with object'
264 );
265 }
266
267 function testEncodeJsVarInt() {
268 $this->assertEquals(
269 '123456',
270 Xml::encodeJsVar( 123456 ),
271 'encodeJsVar() with int'
272 );
273 }
274
275 function testEncodeJsVarFloat() {
276 $this->assertEquals(
277 '1.23456',
278 Xml::encodeJsVar( 1.23456 ),
279 'encodeJsVar() with float'
280 );
281 }
282
283 function testEncodeJsVarIntString() {
284 $this->assertEquals(
285 '"123456"',
286 Xml::encodeJsVar( '123456' ),
287 'encodeJsVar() with int-like string'
288 );
289 }
290
291 function testEncodeJsVarFloatString() {
292 $this->assertEquals(
293 '"1.23456"',
294 Xml::encodeJsVar( '1.23456' ),
295 'encodeJsVar() with float-like string'
296 );
297 }
298 }