6 class XmlTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $langObj = Language
::factory( 'en' );
12 $langObj->setNamespaces( [
24 9 => 'MediaWiki_talk',
26 11 => 'Template_talk',
31 $this->setMwGlobals( [
33 'wgUseMediaWikiUIEverywhere' => false,
37 protected function tearDown() {
38 Language
::factory( 'en' )->resetNamespaces();
43 * @covers Xml::expandAttributes
45 public function testExpandAttributes() {
46 $this->assertNull( Xml
::expandAttributes( null ),
47 'Converting a null list of attributes'
49 $this->assertEquals( '', Xml
::expandAttributes( [] ),
50 'Converting an empty list of attributes'
55 * @covers Xml::expandAttributes
57 public function testExpandAttributesException() {
58 $this->setExpectedException( 'MWException' );
59 Xml
::expandAttributes( 'string' );
63 * @covers Xml::element
65 public function testElementOpen() {
68 Xml
::element( 'element', null, null ),
69 'Opening element with no attributes'
74 * @covers Xml::element
76 public function testElementEmpty() {
79 Xml
::element( 'element', null, '' ),
80 'Terminated empty element'
87 public function testElementInputCanHaveAValueOfZero() {
89 '<input name="name" value="0" />',
90 Xml
::input( 'name', false, 0 ),
91 'Input with a value of 0 (T25797)'
96 * @covers Xml::element
98 public function testElementEscaping() {
100 '<element>hello <there> you & you</element>',
101 Xml
::element( 'element', null, 'hello <there> you & you' ),
102 'Element with no attributes and content that needs escaping'
107 * @covers Xml::escapeTagsOnly
109 public function testEscapeTagsOnly() {
110 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
111 'replace " > and < with their HTML entitites'
116 * @covers Xml::element
118 public function testElementAttributes() {
120 '<element key="value" <>="<>">',
121 Xml
::element( 'element', [ 'key' => 'value', '<>' => '<>' ], null ),
122 'Element attributes, keys are not escaped'
127 * @covers Xml::openElement
129 public function testOpenElement() {
132 Xml
::openElement( 'element', [ 'k' => 'v' ] ),
133 'openElement() shortcut'
138 * @covers Xml::closeElement
140 public function testCloseElement() {
141 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
145 * @covers Xml::dateMenu
147 public function testDateMenu() {
148 $curYear = intval( gmdate( 'Y' ) );
149 $prevYear = $curYear - 1;
151 $curMonth = intval( gmdate( 'n' ) );
153 $nextMonth = $curMonth +
1;
154 if ( $nextMonth == 13 ) {
159 '<label for="year">From year (and earlier):</label> ' .
160 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
161 '<label for="month">From month (and earlier):</label> ' .
162 '<select name="month" id="month" class="mw-month-selector">' .
163 '<option value="-1">all</option>' . "\n" .
164 '<option value="1">January</option>' . "\n" .
165 '<option value="2" selected="">February</option>' . "\n" .
166 '<option value="3">March</option>' . "\n" .
167 '<option value="4">April</option>' . "\n" .
168 '<option value="5">May</option>' . "\n" .
169 '<option value="6">June</option>' . "\n" .
170 '<option value="7">July</option>' . "\n" .
171 '<option value="8">August</option>' . "\n" .
172 '<option value="9">September</option>' . "\n" .
173 '<option value="10">October</option>' . "\n" .
174 '<option value="11">November</option>' . "\n" .
175 '<option value="12">December</option></select>',
176 Xml
::dateMenu( 2011, 02 ),
177 "Date menu for february 2011"
180 '<label for="year">From year (and earlier):</label> ' .
181 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
182 '<label for="month">From month (and earlier):</label> ' .
183 '<select name="month" id="month" class="mw-month-selector">' .
184 '<option value="-1">all</option>' . "\n" .
185 '<option value="1">January</option>' . "\n" .
186 '<option value="2">February</option>' . "\n" .
187 '<option value="3">March</option>' . "\n" .
188 '<option value="4">April</option>' . "\n" .
189 '<option value="5">May</option>' . "\n" .
190 '<option value="6">June</option>' . "\n" .
191 '<option value="7">July</option>' . "\n" .
192 '<option value="8">August</option>' . "\n" .
193 '<option value="9">September</option>' . "\n" .
194 '<option value="10">October</option>' . "\n" .
195 '<option value="11">November</option>' . "\n" .
196 '<option value="12">December</option></select>',
197 Xml
::dateMenu( 2011, -1 ),
198 "Date menu with negative month for 'All'"
201 Xml
::dateMenu( $curYear, $curMonth ),
202 Xml
::dateMenu( '', $curMonth ),
203 "Date menu year is the current one when not specified"
206 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
208 Xml
::dateMenu( $wantedYear, $nextMonth ),
209 Xml
::dateMenu( '', $nextMonth ),
210 "Date menu next month is 11 months ago"
214 '<label for="year">From year (and earlier):</label> ' .
215 '<input id="year" maxlength="4" size="7" type="number" name="year"/> ' .
216 '<label for="month">From month (and earlier):</label> ' .
217 '<select name="month" id="month" class="mw-month-selector">' .
218 '<option value="-1">all</option>' . "\n" .
219 '<option value="1">January</option>' . "\n" .
220 '<option value="2">February</option>' . "\n" .
221 '<option value="3">March</option>' . "\n" .
222 '<option value="4">April</option>' . "\n" .
223 '<option value="5">May</option>' . "\n" .
224 '<option value="6">June</option>' . "\n" .
225 '<option value="7">July</option>' . "\n" .
226 '<option value="8">August</option>' . "\n" .
227 '<option value="9">September</option>' . "\n" .
228 '<option value="10">October</option>' . "\n" .
229 '<option value="11">November</option>' . "\n" .
230 '<option value="12">December</option></select>',
231 Xml
::dateMenu( '', '' ),
232 "Date menu with neither year or month"
237 * @covers Xml::textarea
239 public function testTextareaNoContent() {
241 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
242 Xml
::textarea( 'name', '' ),
243 'textarea() with not content'
248 * @covers Xml::textarea
250 public function testTextareaAttribs() {
252 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
253 Xml
::textarea( 'name', '<txt>', 20, 10 ),
254 'textarea() with custom attribs'
261 public function testLabelCreation() {
263 '<label for="id">name</label>',
264 Xml
::label( 'name', 'id' ),
265 'label() with no attribs'
272 public function testLabelAttributeCanOnlyBeClassOrTitle() {
274 '<label for="id">name</label>',
275 Xml
::label( 'name', 'id', [ 'generated' => true ] ),
276 'label() can not be given a generated attribute'
279 '<label for="id" class="nice">name</label>',
280 Xml
::label( 'name', 'id', [ 'class' => 'nice' ] ),
281 'label() can get a class attribute'
284 '<label for="id" title="nice tooltip">name</label>',
285 Xml
::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
286 'label() can get a title attribute'
289 '<label for="id" class="nice" title="nice tooltip">name</label>',
290 Xml
::label( 'name', 'id', [
293 'title' => 'nice tooltip',
294 'anotherattr' => 'value',
297 'label() skip all attributes but "class" and "title"'
302 * @covers Xml::languageSelector
304 public function testLanguageSelector() {
305 $select = Xml
::languageSelector( 'en', true, null,
306 [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
308 '<label for="testlang">Language:</label>',
314 * @covers Xml::encodeJsVar
316 public function testEncodeJsVarBoolean() {
319 Xml
::encodeJsVar( true ),
320 'encodeJsVar() with boolean'
325 * @covers Xml::encodeJsVar
327 public function testEncodeJsVarNull() {
330 Xml
::encodeJsVar( null ),
331 'encodeJsVar() with null'
336 * @covers Xml::encodeJsVar
338 public function testEncodeJsVarArray() {
341 Xml
::encodeJsVar( [ 'a', 1 ] ),
342 'encodeJsVar() with array'
346 Xml
::encodeJsVar( [ 'a' => 'a', 'b' => 1 ] ),
347 'encodeJsVar() with associative array'
352 * @covers Xml::encodeJsVar
354 public function testEncodeJsVarObject() {
357 Xml
::encodeJsVar( (object)[ 'a' => 'a', 'b' => 1 ] ),
358 'encodeJsVar() with object'
363 * @covers Xml::encodeJsVar
365 public function testEncodeJsVarInt() {
368 Xml
::encodeJsVar( 123456 ),
369 'encodeJsVar() with int'
374 * @covers Xml::encodeJsVar
376 public function testEncodeJsVarFloat() {
379 Xml
::encodeJsVar( 1.23456 ),
380 'encodeJsVar() with float'
385 * @covers Xml::encodeJsVar
387 public function testEncodeJsVarIntString() {
390 Xml
::encodeJsVar( '123456' ),
391 'encodeJsVar() with int-like string'
396 * @covers Xml::encodeJsVar
398 public function testEncodeJsVarFloatString() {
401 Xml
::encodeJsVar( '1.23456' ),
402 'encodeJsVar() with float-like string'
407 * @covers Xml::listDropDown
409 public function testListDropDown() {
411 '<select name="test-name" id="test-name" class="test-css" tabindex="2">' .
412 '<option value="other">other reasons</option>' . "\n" .
413 '<optgroup label="Foo">' .
414 '<option value="Foo 1">Foo 1</option>' . "\n" .
415 '<option value="Example" selected="">Example</option>' . "\n" .
416 '</optgroup>' . "\n" .
417 '<optgroup label="Bar">' .
418 '<option value="Bar 1">Bar 1</option>' . "\n" .
425 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
439 * @covers Xml::listDropDownOptions
441 public function testListDropDownOptions() {
444 'other reasons' => 'other',
447 'Example' => 'Example',
453 Xml
::listDropDownOptions(
454 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
455 [ 'other' => 'other reasons' ]
461 * @covers Xml::listDropDownOptionsOoui
463 public function testListDropDownOptionsOoui() {
466 [ 'data' => 'other', 'label' => 'other reasons' ],
467 [ 'optgroup' => 'Foo' ],
468 [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
469 [ 'data' => 'Example', 'label' => 'Example' ],
470 [ 'optgroup' => 'Bar' ],
471 [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
473 Xml
::listDropDownOptionsOoui( [
474 'other reasons' => 'other',
477 'Example' => 'Example',
487 * @covers Xml::fieldset
489 public function testFieldset() {
497 Xml
::fieldset( false ),
498 'Opening tag (false means no legend)'
503 'Opening tag (empty string also means no legend)'
506 "<fieldset>\n<legend>Foo</legend>\n",
507 Xml
::fieldset( 'Foo' ),
508 'Opening tag with legend'
511 "<fieldset>\n<legend>Foo</legend>\nBar\n</fieldset>\n",
512 Xml
::fieldset( 'Foo', 'Bar' ),
513 'Entire element with legend'
516 "<fieldset>\n<legend>Foo</legend>\n",
517 Xml
::fieldset( 'Foo', false ),
518 'Opening tag with legend (false means no content and no closing tag)'
521 "<fieldset>\n<legend>Foo</legend>\n\n</fieldset>\n",
522 Xml
::fieldset( 'Foo', '' ),
523 'Entire element with legend but no content (empty string generates a closing tag)'
526 "<fieldset class=\"bar\">\n<legend>Foo</legend>\nBar\n</fieldset>\n",
527 Xml
::fieldset( 'Foo', 'Bar', [ 'class' => 'bar' ] ),
528 'Opening tag with legend and attributes'
531 "<fieldset class=\"bar\">\n<legend>Foo</legend>\n",
532 Xml
::fieldset( 'Foo', false, [ 'class' => 'bar' ] ),
533 'Entire element with legend and attributes'