1d9361f21541a670d1ededdd9bc0ed6d63ce0aa4
3 class XmlTest
extends MediaWikiTestCase
{
4 private static $oldLang;
5 private static $oldNamespaces;
7 public function setUp() {
8 global $wgLang, $wgContLang;
10 self
::$oldLang = $wgLang;
11 $wgLang = Language
::factory( 'en' );
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(
29 9 => 'MediaWiki_talk',
31 11 => 'Template_talk',
37 public function tearDown() {
38 global $wgLang, $wgContLang;
39 $wgLang = self
::$oldLang;
41 $wgContLang->setNamespaces( self
::$oldNamespaces );
44 public function testExpandAttributes() {
45 $this->assertNull( Xml
::expandAttributes(null),
46 'Converting a null list of attributes'
48 $this->assertEquals( '', Xml
::expandAttributes( array() ),
49 'Converting an empty list of attributes'
53 public function testExpandAttributesException() {
54 $this->setExpectedException('MWException');
55 Xml
::expandAttributes('string');
58 function testElementOpen() {
61 Xml
::element( 'element', null, null ),
62 'Opening element with no attributes'
66 function testElementEmpty() {
69 Xml
::element( 'element', null, '' ),
70 'Terminated empty element'
74 function testElementInputCanHaveAValueOfZero() {
76 '<input name="name" value="0" />',
77 Xml
::input( 'name', false, 0 ),
78 'Input with a value of 0 (bug 23797)'
81 function testElementEscaping() {
83 '<element>hello <there> you & you</element>',
84 Xml
::element( 'element', null, 'hello <there> you & you' ),
85 'Element with no attributes and content that needs escaping'
89 public function testEscapeTagsOnly() {
90 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
91 'replace " > and < with their HTML entitites'
95 function testElementAttributes() {
97 '<element key="value" <>="<>">',
98 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
99 'Element attributes, keys are not escaped'
103 function testOpenElement() {
106 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
107 'openElement() shortcut'
111 function testCloseElement() {
112 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
118 public function testDateMenu( ) {
119 $curYear = intval(gmdate('Y'));
120 $prevYear = $curYear - 1;
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; }
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"
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'"
163 Xml
::dateMenu( $curYear, $curMonth ),
164 Xml
::dateMenu( '' , $curMonth ),
165 "Date menu year is the current one when not specified"
168 // @todo FIXME: next month can be in the next year
169 // test failing because it is now december
171 Xml
::dateMenu( $prevYear, $nextMonth ),
172 Xml
::dateMenu( '', $nextMonth ),
173 "Date menu next month is 11 months ago"
176 # @todo FIXME: Please note there is no year there!
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"
196 function testNamespaceSelector() {
198 '<select class="namespaceselector" id="namespace" name="namespace">' . "\n" .
199 '<option value="0">(Main)</option>' . "\n" .
200 '<option value="1">Talk</option>' . "\n" .
201 '<option value="2">User</option>' . "\n" .
202 '<option value="3">User talk</option>' . "\n" .
203 '<option value="4">MyWiki</option>' . "\n" .
204 '<option value="5">MyWiki Talk</option>' . "\n" .
205 '<option value="6">File</option>' . "\n" .
206 '<option value="7">File talk</option>' . "\n" .
207 '<option value="8">MediaWiki</option>' . "\n" .
208 '<option value="9">MediaWiki talk</option>' . "\n" .
209 '<option value="10">Template</option>' . "\n" .
210 '<option value="11">Template talk</option>' . "\n" .
211 '<option value="100">Custom</option>' . "\n" .
212 '<option value="101">Custom talk</option>' . "\n" .
214 Xml
::namespaceSelector(),
215 'Basic namespace selector without custom options'
218 '<label for="namespace">Select a namespace:</label>' .
219 ' <select class="namespaceselector" id="namespace" name="myname">' . "\n" .
220 '<option value="all">all</option>' . "\n" .
221 '<option value="0">(Main)</option>' . "\n" .
222 '<option value="1">Talk</option>' . "\n" .
223 '<option value="2" selected="">User</option>' . "\n" .
224 '<option value="3">User talk</option>' . "\n" .
225 '<option value="4">MyWiki</option>' . "\n" .
226 '<option value="5">MyWiki Talk</option>' . "\n" .
227 '<option value="6">File</option>' . "\n" .
228 '<option value="7">File talk</option>' . "\n" .
229 '<option value="8">MediaWiki</option>' . "\n" .
230 '<option value="9">MediaWiki talk</option>' . "\n" .
231 '<option value="10">Template</option>' . "\n" .
232 '<option value="11">Template talk</option>' . "\n" .
233 '<option value="100">Custom</option>' . "\n" .
234 '<option value="101">Custom talk</option>' . "\n" .
236 Xml
::namespaceSelector( $selected = '2', $all = 'all', $element_name = 'myname', $label = 'Select a namespace:' ),
237 'Basic namespace selector with custom values'
245 function testTextareaNoContent() {
247 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
248 Xml
::textarea( 'name', '' ),
249 'textarea() with not content'
253 function testTextareaAttribs() {
255 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
256 Xml
::textarea( 'name', '<txt>', 20, 10 ),
257 'textarea() with custom attribs'
264 function testLabelCreation() {
266 '<label for="id">name</label>',
267 Xml
::label( 'name', 'id' ),
268 'label() with no attribs'
271 function testLabelAttributeCanOnlyBeClassOrTitle() {
273 '<label for="id">name</label>',
274 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
275 'label() can not be given a generated attribute'
278 '<label for="id" class="nice">name</label>',
279 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
280 'label() can get a class attribute'
283 '<label for="id" title="nice tooltip">name</label>',
284 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
285 'label() can get a title attribute'
288 '<label for="id" class="nice" title="nice tooltip">name</label>',
289 Xml
::label( 'name', 'id', array(
292 'title' => 'nice tooltip',
293 'anotherattr' => 'value',
296 'label() skip all attributes but "class" and "title"'
303 function testEscapeJsStringSpecialChars() {
306 Xml
::escapeJsString( "\\\r\n" ),
307 'escapeJsString() with special characters'
311 function testEncodeJsVarBoolean() {
314 Xml
::encodeJsVar( true ),
315 'encodeJsVar() with boolean'
319 function testEncodeJsVarNull() {
322 Xml
::encodeJsVar( null ),
323 'encodeJsVar() with null'
327 function testEncodeJsVarArray() {
330 Xml
::encodeJsVar( array( 'a', 1 ) ),
331 'encodeJsVar() with array'
335 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
336 'encodeJsVar() with associative array'
340 function testEncodeJsVarObject() {
343 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
344 'encodeJsVar() with object'
348 function testEncodeJsVarInt() {
351 Xml
::encodeJsVar( 123456 ),
352 'encodeJsVar() with int'
356 function testEncodeJsVarFloat() {
359 Xml
::encodeJsVar( 1.23456 ),
360 'encodeJsVar() with float'
364 function testEncodeJsVarIntString() {
367 Xml
::encodeJsVar( '123456' ),
368 'encodeJsVar() with int-like string'
372 function testEncodeJsVarFloatString() {
375 Xml
::encodeJsVar( '1.23456' ),
376 'encodeJsVar() with float-like string'