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 'wgWellFormedXml' => true,
34 'wgUseMediaWikiUIEverywhere' => false,
39 * @covers Xml::expandAttributes
41 public function testExpandAttributes() {
42 $this->assertNull( Xml
::expandAttributes( null ),
43 'Converting a null list of attributes'
45 $this->assertEquals( '', Xml
::expandAttributes( [] ),
46 'Converting an empty list of attributes'
51 * @covers Xml::expandAttributes
53 public function testExpandAttributesException() {
54 $this->setExpectedException( 'MWException' );
55 Xml
::expandAttributes( 'string' );
59 * @covers Xml::element
61 public function testElementOpen() {
64 Xml
::element( 'element', null, null ),
65 'Opening element with no attributes'
70 * @covers Xml::element
72 public function testElementEmpty() {
75 Xml
::element( 'element', null, '' ),
76 'Terminated empty element'
83 public function testElementInputCanHaveAValueOfZero() {
85 '<input name="name" value="0" />',
86 Xml
::input( 'name', false, 0 ),
87 'Input with a value of 0 (bug 23797)'
92 * @covers Xml::element
94 public function testElementEscaping() {
96 '<element>hello <there> you & you</element>',
97 Xml
::element( 'element', null, 'hello <there> you & you' ),
98 'Element with no attributes and content that needs escaping'
103 * @covers Xml::escapeTagsOnly
105 public function testEscapeTagsOnly() {
106 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
107 'replace " > and < with their HTML entitites'
112 * @covers Xml::element
114 public function testElementAttributes() {
116 '<element key="value" <>="<>">',
117 Xml
::element( 'element', [ 'key' => 'value', '<>' => '<>' ], null ),
118 'Element attributes, keys are not escaped'
123 * @covers Xml::openElement
125 public function testOpenElement() {
128 Xml
::openElement( 'element', [ 'k' => 'v' ] ),
129 'openElement() shortcut'
134 * @covers Xml::closeElement
136 public function testCloseElement() {
137 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
141 * @covers Xml::dateMenu
143 public function testDateMenu() {
144 $curYear = intval( gmdate( 'Y' ) );
145 $prevYear = $curYear - 1;
147 $curMonth = intval( gmdate( 'n' ) );
149 $nextMonth = $curMonth +
1;
150 if ( $nextMonth == 13 ) {
155 '<label for="year">From year (and earlier):</label> ' .
156 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
157 '<label for="month">From month (and earlier):</label> ' .
158 '<select name="month" id="month" class="mw-month-selector">' .
159 '<option value="-1">all</option>' . "\n" .
160 '<option value="1">January</option>' . "\n" .
161 '<option value="2" selected="">February</option>' . "\n" .
162 '<option value="3">March</option>' . "\n" .
163 '<option value="4">April</option>' . "\n" .
164 '<option value="5">May</option>' . "\n" .
165 '<option value="6">June</option>' . "\n" .
166 '<option value="7">July</option>' . "\n" .
167 '<option value="8">August</option>' . "\n" .
168 '<option value="9">September</option>' . "\n" .
169 '<option value="10">October</option>' . "\n" .
170 '<option value="11">November</option>' . "\n" .
171 '<option value="12">December</option></select>',
172 Xml
::dateMenu( 2011, 02 ),
173 "Date menu for february 2011"
176 '<label for="year">From year (and earlier):</label> ' .
177 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
178 '<label for="month">From month (and earlier):</label> ' .
179 '<select name="month" id="month" class="mw-month-selector">' .
180 '<option value="-1">all</option>' . "\n" .
181 '<option value="1">January</option>' . "\n" .
182 '<option value="2">February</option>' . "\n" .
183 '<option value="3">March</option>' . "\n" .
184 '<option value="4">April</option>' . "\n" .
185 '<option value="5">May</option>' . "\n" .
186 '<option value="6">June</option>' . "\n" .
187 '<option value="7">July</option>' . "\n" .
188 '<option value="8">August</option>' . "\n" .
189 '<option value="9">September</option>' . "\n" .
190 '<option value="10">October</option>' . "\n" .
191 '<option value="11">November</option>' . "\n" .
192 '<option value="12">December</option></select>',
193 Xml
::dateMenu( 2011, -1 ),
194 "Date menu with negative month for 'All'"
197 Xml
::dateMenu( $curYear, $curMonth ),
198 Xml
::dateMenu( '', $curMonth ),
199 "Date menu year is the current one when not specified"
202 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
204 Xml
::dateMenu( $wantedYear, $nextMonth ),
205 Xml
::dateMenu( '', $nextMonth ),
206 "Date menu next month is 11 months ago"
210 '<label for="year">From year (and earlier):</label> ' .
211 '<input id="year" maxlength="4" size="7" type="number" name="year"/> ' .
212 '<label for="month">From month (and earlier):</label> ' .
213 '<select name="month" id="month" class="mw-month-selector">' .
214 '<option value="-1">all</option>' . "\n" .
215 '<option value="1">January</option>' . "\n" .
216 '<option value="2">February</option>' . "\n" .
217 '<option value="3">March</option>' . "\n" .
218 '<option value="4">April</option>' . "\n" .
219 '<option value="5">May</option>' . "\n" .
220 '<option value="6">June</option>' . "\n" .
221 '<option value="7">July</option>' . "\n" .
222 '<option value="8">August</option>' . "\n" .
223 '<option value="9">September</option>' . "\n" .
224 '<option value="10">October</option>' . "\n" .
225 '<option value="11">November</option>' . "\n" .
226 '<option value="12">December</option></select>',
227 Xml
::dateMenu( '', '' ),
228 "Date menu with neither year or month"
233 * @covers Xml::textarea
235 public function testTextareaNoContent() {
237 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
238 Xml
::textarea( 'name', '' ),
239 'textarea() with not content'
244 * @covers Xml::textarea
246 public function testTextareaAttribs() {
248 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
249 Xml
::textarea( 'name', '<txt>', 20, 10 ),
250 'textarea() with custom attribs'
257 public function testLabelCreation() {
259 '<label for="id">name</label>',
260 Xml
::label( 'name', 'id' ),
261 'label() with no attribs'
268 public function testLabelAttributeCanOnlyBeClassOrTitle() {
270 '<label for="id">name</label>',
271 Xml
::label( 'name', 'id', [ 'generated' => true ] ),
272 'label() can not be given a generated attribute'
275 '<label for="id" class="nice">name</label>',
276 Xml
::label( 'name', 'id', [ 'class' => 'nice' ] ),
277 'label() can get a class attribute'
280 '<label for="id" title="nice tooltip">name</label>',
281 Xml
::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
282 'label() can get a title attribute'
285 '<label for="id" class="nice" title="nice tooltip">name</label>',
286 Xml
::label( 'name', 'id', [
289 'title' => 'nice tooltip',
290 'anotherattr' => 'value',
293 'label() skip all attributes but "class" and "title"'
298 * @covers Xml::languageSelector
300 public function testLanguageSelector() {
301 $select = Xml
::languageSelector( 'en', true, null,
302 [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
304 '<label for="testlang">Language:</label>',
310 * @covers Xml::escapeJsString
312 public function testEscapeJsStringSpecialChars() {
315 Xml
::escapeJsString( "\\\r\n" ),
316 'escapeJsString() with special characters'
321 * @covers Xml::encodeJsVar
323 public function testEncodeJsVarBoolean() {
326 Xml
::encodeJsVar( true ),
327 'encodeJsVar() with boolean'
332 * @covers Xml::encodeJsVar
334 public function testEncodeJsVarNull() {
337 Xml
::encodeJsVar( null ),
338 'encodeJsVar() with null'
343 * @covers Xml::encodeJsVar
345 public function testEncodeJsVarArray() {
348 Xml
::encodeJsVar( [ 'a', 1 ] ),
349 'encodeJsVar() with array'
353 Xml
::encodeJsVar( [ 'a' => 'a', 'b' => 1 ] ),
354 'encodeJsVar() with associative array'
359 * @covers Xml::encodeJsVar
361 public function testEncodeJsVarObject() {
364 Xml
::encodeJsVar( (object)[ 'a' => 'a', 'b' => 1 ] ),
365 'encodeJsVar() with object'
370 * @covers Xml::encodeJsVar
372 public function testEncodeJsVarInt() {
375 Xml
::encodeJsVar( 123456 ),
376 'encodeJsVar() with int'
381 * @covers Xml::encodeJsVar
383 public function testEncodeJsVarFloat() {
386 Xml
::encodeJsVar( 1.23456 ),
387 'encodeJsVar() with float'
392 * @covers Xml::encodeJsVar
394 public function testEncodeJsVarIntString() {
397 Xml
::encodeJsVar( '123456' ),
398 'encodeJsVar() with int-like string'
403 * @covers Xml::encodeJsVar
405 public function testEncodeJsVarFloatString() {
408 Xml
::encodeJsVar( '1.23456' ),
409 'encodeJsVar() with float-like string'