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> ' .
155 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year" /> ' .
156 '<label for="month">From month (and earlier):</label> ' .
157 '<select name="month" id="month" class="mw-month-selector">' .
158 '<option value="-1">all</option>' . "\n" .
159 '<option value="1">January</option>' . "\n" .
160 '<option value="2" selected="">February</option>' . "\n" .
161 '<option value="3">March</option>' . "\n" .
162 '<option value="4">April</option>' . "\n" .
163 '<option value="5">May</option>' . "\n" .
164 '<option value="6">June</option>' . "\n" .
165 '<option value="7">July</option>' . "\n" .
166 '<option value="8">August</option>' . "\n" .
167 '<option value="9">September</option>' . "\n" .
168 '<option value="10">October</option>' . "\n" .
169 '<option value="11">November</option>' . "\n" .
170 '<option value="12">December</option></select>',
171 Xml
::dateMenu( 2011, 02 ),
172 "Date menu for february 2011"
175 '<label for="year">From year (and earlier):</label> ' .
176 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year" /> ' .
177 '<label for="month">From month (and earlier):</label> ' .
178 '<select name="month" id="month" class="mw-month-selector">' .
179 '<option value="-1">all</option>' . "\n" .
180 '<option value="1">January</option>' . "\n" .
181 '<option value="2">February</option>' . "\n" .
182 '<option value="3">March</option>' . "\n" .
183 '<option value="4">April</option>' . "\n" .
184 '<option value="5">May</option>' . "\n" .
185 '<option value="6">June</option>' . "\n" .
186 '<option value="7">July</option>' . "\n" .
187 '<option value="8">August</option>' . "\n" .
188 '<option value="9">September</option>' . "\n" .
189 '<option value="10">October</option>' . "\n" .
190 '<option value="11">November</option>' . "\n" .
191 '<option value="12">December</option></select>',
192 Xml
::dateMenu( 2011, -1 ),
193 "Date menu with negative month for 'All'"
196 Xml
::dateMenu( $curYear, $curMonth ),
197 Xml
::dateMenu( '', $curMonth ),
198 "Date menu year is the current one when not specified"
201 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
203 Xml
::dateMenu( $wantedYear, $nextMonth ),
204 Xml
::dateMenu( '', $nextMonth ),
205 "Date menu next month is 11 months ago"
209 '<label for="year">From year (and earlier):</label> ' .
210 '<input id="year" maxlength="4" size="7" type="number" name="year" /> ' .
211 '<label for="month">From month (and earlier):</label> ' .
212 '<select name="month" id="month" class="mw-month-selector">' .
213 '<option value="-1">all</option>' . "\n" .
214 '<option value="1">January</option>' . "\n" .
215 '<option value="2">February</option>' . "\n" .
216 '<option value="3">March</option>' . "\n" .
217 '<option value="4">April</option>' . "\n" .
218 '<option value="5">May</option>' . "\n" .
219 '<option value="6">June</option>' . "\n" .
220 '<option value="7">July</option>' . "\n" .
221 '<option value="8">August</option>' . "\n" .
222 '<option value="9">September</option>' . "\n" .
223 '<option value="10">October</option>' . "\n" .
224 '<option value="11">November</option>' . "\n" .
225 '<option value="12">December</option></select>',
226 Xml
::dateMenu( '', '' ),
227 "Date menu with neither year or month"
232 * @covers Xml::textarea
234 public function testTextareaNoContent() {
236 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
237 Xml
::textarea( 'name', '' ),
238 'textarea() with not content'
243 * @covers Xml::textarea
245 public function testTextareaAttribs() {
247 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
248 Xml
::textarea( 'name', '<txt>', 20, 10 ),
249 'textarea() with custom attribs'
256 public function testLabelCreation() {
258 '<label for="id">name</label>',
259 Xml
::label( 'name', 'id' ),
260 'label() with no attribs'
267 public function testLabelAttributeCanOnlyBeClassOrTitle() {
269 '<label for="id">name</label>',
270 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
271 'label() can not be given a generated attribute'
274 '<label for="id" class="nice">name</label>',
275 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
276 'label() can get a class attribute'
279 '<label for="id" title="nice tooltip">name</label>',
280 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
281 'label() can get a title attribute'
284 '<label for="id" class="nice" title="nice tooltip">name</label>',
285 Xml
::label( 'name', 'id', array(
288 'title' => 'nice tooltip',
289 'anotherattr' => 'value',
292 'label() skip all attributes but "class" and "title"'
297 * @covers Xml::languageSelector
299 public function testLanguageSelector() {
300 $select = Xml
::languageSelector( 'en', true, null,
301 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
303 '<label for="testlang">Language:</label>',
309 * @covers Xml::escapeJsString
311 public function testEscapeJsStringSpecialChars() {
314 Xml
::escapeJsString( "\\\r\n" ),
315 'escapeJsString() with special characters'
320 * @covers Xml::encodeJsVar
322 public function testEncodeJsVarBoolean() {
325 Xml
::encodeJsVar( true ),
326 'encodeJsVar() with boolean'
331 * @covers Xml::encodeJsVar
333 public function testEncodeJsVarNull() {
336 Xml
::encodeJsVar( null ),
337 'encodeJsVar() with null'
342 * @covers Xml::encodeJsVar
344 public function testEncodeJsVarArray() {
347 Xml
::encodeJsVar( array( 'a', 1 ) ),
348 'encodeJsVar() with array'
352 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
353 'encodeJsVar() with associative array'
358 * @covers Xml::encodeJsVar
360 public function testEncodeJsVarObject() {
363 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
364 'encodeJsVar() with object'
369 * @covers Xml::encodeJsVar
371 public function testEncodeJsVarInt() {
374 Xml
::encodeJsVar( 123456 ),
375 'encodeJsVar() with int'
380 * @covers Xml::encodeJsVar
382 public function testEncodeJsVarFloat() {
385 Xml
::encodeJsVar( 1.23456 ),
386 'encodeJsVar() with float'
391 * @covers Xml::encodeJsVar
393 public function testEncodeJsVarIntString() {
396 Xml
::encodeJsVar( '123456' ),
397 'encodeJsVar() with int-like string'
402 * @covers Xml::encodeJsVar
404 public function testEncodeJsVarFloatString() {
407 Xml
::encodeJsVar( '1.23456' ),
408 'encodeJsVar() with float-like string'