6 class XmlTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $langObj = Language
::factory( 'en' );
12 $langObj->setNamespaces( array(
24 9 => 'MediaWiki_talk',
26 11 => 'Template_talk',
31 $this->setMwGlobals( array(
33 'wgWellFormedXml' => true,
38 * @covers Xml::expandAttributes
40 public function testExpandAttributes() {
41 $this->assertNull( Xml
::expandAttributes( null ),
42 'Converting a null list of attributes'
44 $this->assertEquals( '', Xml
::expandAttributes( array() ),
45 'Converting an empty list of attributes'
50 * @covers Xml::expandAttributes
52 public function testExpandAttributesException() {
53 $this->setExpectedException( 'MWException' );
54 Xml
::expandAttributes( 'string' );
58 * @covers Xml::element
60 public function testElementOpen() {
63 Xml
::element( 'element', null, null ),
64 'Opening element with no attributes'
69 * @covers Xml::element
71 public function testElementEmpty() {
74 Xml
::element( 'element', null, '' ),
75 'Terminated empty element'
82 public function testElementInputCanHaveAValueOfZero() {
84 '<input name="name" value="0" />',
85 Xml
::input( 'name', false, 0 ),
86 'Input with a value of 0 (bug 23797)'
91 * @covers Xml::element
93 public function testElementEscaping() {
95 '<element>hello <there> you & you</element>',
96 Xml
::element( 'element', null, 'hello <there> you & you' ),
97 'Element with no attributes and content that needs escaping'
102 * @covers Xml::escapeTagsOnly
104 public function testEscapeTagsOnly() {
105 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
106 'replace " > and < with their HTML entitites'
111 * @covers Xml::element
113 public function testElementAttributes() {
115 '<element key="value" <>="<>">',
116 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
117 'Element attributes, keys are not escaped'
122 * @covers Xml::openElement
124 public function testOpenElement() {
127 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
128 'openElement() shortcut'
133 * @covers Xml::closeElement
135 public function testCloseElement() {
136 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
140 * @covers Xml::dateMenu
142 public function testDateMenu() {
143 $curYear = intval( gmdate( 'Y' ) );
144 $prevYear = $curYear - 1;
146 $curMonth = intval( gmdate( 'n' ) );
148 $nextMonth = $curMonth +
1;
149 if ( $nextMonth == 13 ) {
154 '<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" .
155 '<option value="1">January</option>' . "\n" .
156 '<option value="2" selected="">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( 2011, 02 ),
168 "Date menu for february 2011"
171 '<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" .
172 '<option value="1">January</option>' . "\n" .
173 '<option value="2">February</option>' . "\n" .
174 '<option value="3">March</option>' . "\n" .
175 '<option value="4">April</option>' . "\n" .
176 '<option value="5">May</option>' . "\n" .
177 '<option value="6">June</option>' . "\n" .
178 '<option value="7">July</option>' . "\n" .
179 '<option value="8">August</option>' . "\n" .
180 '<option value="9">September</option>' . "\n" .
181 '<option value="10">October</option>' . "\n" .
182 '<option value="11">November</option>' . "\n" .
183 '<option value="12">December</option></select>',
184 Xml
::dateMenu( 2011, -1 ),
185 "Date menu with negative month for 'All'"
188 Xml
::dateMenu( $curYear, $curMonth ),
189 Xml
::dateMenu( '', $curMonth ),
190 "Date menu year is the current one when not specified"
193 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
195 Xml
::dateMenu( $wantedYear, $nextMonth ),
196 Xml
::dateMenu( '', $nextMonth ),
197 "Date menu next month is 11 months ago"
201 '<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" .
202 '<option value="1">January</option>' . "\n" .
203 '<option value="2">February</option>' . "\n" .
204 '<option value="3">March</option>' . "\n" .
205 '<option value="4">April</option>' . "\n" .
206 '<option value="5">May</option>' . "\n" .
207 '<option value="6">June</option>' . "\n" .
208 '<option value="7">July</option>' . "\n" .
209 '<option value="8">August</option>' . "\n" .
210 '<option value="9">September</option>' . "\n" .
211 '<option value="10">October</option>' . "\n" .
212 '<option value="11">November</option>' . "\n" .
213 '<option value="12">December</option></select>',
214 Xml
::dateMenu( '', '' ),
215 "Date menu with neither year or month"
220 * @covers Xml::textarea
222 public function testTextareaNoContent() {
224 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
225 Xml
::textarea( 'name', '' ),
226 'textarea() with not content'
231 * @covers Xml::textarea
233 public function testTextareaAttribs() {
235 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
236 Xml
::textarea( 'name', '<txt>', 20, 10 ),
237 'textarea() with custom attribs'
244 public function testLabelCreation() {
246 '<label for="id">name</label>',
247 Xml
::label( 'name', 'id' ),
248 'label() with no attribs'
255 public function testLabelAttributeCanOnlyBeClassOrTitle() {
257 '<label for="id">name</label>',
258 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
259 'label() can not be given a generated attribute'
262 '<label for="id" class="nice">name</label>',
263 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
264 'label() can get a class attribute'
267 '<label for="id" title="nice tooltip">name</label>',
268 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
269 'label() can get a title attribute'
272 '<label for="id" class="nice" title="nice tooltip">name</label>',
273 Xml
::label( 'name', 'id', array(
276 'title' => 'nice tooltip',
277 'anotherattr' => 'value',
280 'label() skip all attributes but "class" and "title"'
285 * @covers Xml::languageSelector
287 public function testLanguageSelector() {
288 $select = Xml
::languageSelector( 'en', true, null,
289 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
291 '<label for="testlang">Language:</label>',
297 * @covers Xml::escapeJsString
299 public function testEscapeJsStringSpecialChars() {
302 Xml
::escapeJsString( "\\\r\n" ),
303 'escapeJsString() with special characters'
308 * @covers Xml::encodeJsVar
310 public function testEncodeJsVarBoolean() {
313 Xml
::encodeJsVar( true ),
314 'encodeJsVar() with boolean'
319 * @covers Xml::encodeJsVar
321 public function testEncodeJsVarNull() {
324 Xml
::encodeJsVar( null ),
325 'encodeJsVar() with null'
330 * @covers Xml::encodeJsVar
332 public function testEncodeJsVarArray() {
335 Xml
::encodeJsVar( array( 'a', 1 ) ),
336 'encodeJsVar() with array'
340 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
341 'encodeJsVar() with associative array'
346 * @covers Xml::encodeJsVar
348 public function testEncodeJsVarObject() {
351 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
352 'encodeJsVar() with object'
357 * @covers Xml::encodeJsVar
359 public function testEncodeJsVarInt() {
362 Xml
::encodeJsVar( 123456 ),
363 'encodeJsVar() with int'
368 * @covers Xml::encodeJsVar
370 public function testEncodeJsVarFloat() {
373 Xml
::encodeJsVar( 1.23456 ),
374 'encodeJsVar() with float'
379 * @covers Xml::encodeJsVar
381 public function testEncodeJsVarIntString() {
384 Xml
::encodeJsVar( '123456' ),
385 'encodeJsVar() with int-like string'
390 * @covers Xml::encodeJsVar
392 public function testEncodeJsVarFloatString() {
395 Xml
::encodeJsVar( '1.23456' ),
396 'encodeJsVar() with float-like string'