3 class XmlTest
extends MediaWikiTestCase
{
4 private static $oldLang;
5 private static $oldNamespaces;
7 protected function setUp() {
10 $langObj = Language
::factory( 'en' );
11 $langObj->setNamespaces( array(
23 9 => 'MediaWiki_talk',
25 11 => 'Template_talk',
30 $this->setMwGlobals( array(
33 'wgWellFormedXml' => true,
37 public function testExpandAttributes() {
38 $this->assertNull( Xml
::expandAttributes( null ),
39 'Converting a null list of attributes'
41 $this->assertEquals( '', Xml
::expandAttributes( array() ),
42 'Converting an empty list of attributes'
46 public function testExpandAttributesException() {
47 $this->setExpectedException( 'MWException' );
48 Xml
::expandAttributes( 'string' );
51 function testElementOpen() {
54 Xml
::element( 'element', null, null ),
55 'Opening element with no attributes'
59 function testElementEmpty() {
62 Xml
::element( 'element', null, '' ),
63 'Terminated empty element'
67 function testElementInputCanHaveAValueOfZero() {
69 '<input name="name" value="0" />',
70 Xml
::input( 'name', false, 0 ),
71 'Input with a value of 0 (bug 23797)'
75 function testElementEscaping() {
77 '<element>hello <there> you & you</element>',
78 Xml
::element( 'element', null, 'hello <there> you & you' ),
79 'Element with no attributes and content that needs escaping'
83 public function testEscapeTagsOnly() {
84 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
85 'replace " > and < with their HTML entitites'
89 function testElementAttributes() {
91 '<element key="value" <>="<>">',
92 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
93 'Element attributes, keys are not escaped'
97 function testOpenElement() {
100 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
101 'openElement() shortcut'
105 function testCloseElement() {
106 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
109 public function testDateMenu() {
110 $curYear = intval( gmdate( 'Y' ) );
111 $prevYear = $curYear - 1;
113 $curMonth = intval( gmdate( 'n' ) );
114 $prevMonth = $curMonth - 1;
115 if ( $prevMonth == 0 ) {
118 $nextMonth = $curMonth +
1;
119 if ( $nextMonth == 13 ) {
124 '<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" .
125 '<option value="1">January</option>' . "\n" .
126 '<option value="2" selected="">February</option>' . "\n" .
127 '<option value="3">March</option>' . "\n" .
128 '<option value="4">April</option>' . "\n" .
129 '<option value="5">May</option>' . "\n" .
130 '<option value="6">June</option>' . "\n" .
131 '<option value="7">July</option>' . "\n" .
132 '<option value="8">August</option>' . "\n" .
133 '<option value="9">September</option>' . "\n" .
134 '<option value="10">October</option>' . "\n" .
135 '<option value="11">November</option>' . "\n" .
136 '<option value="12">December</option></select>',
137 Xml
::dateMenu( 2011, 02 ),
138 "Date menu for february 2011"
141 '<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" .
142 '<option value="1">January</option>' . "\n" .
143 '<option value="2">February</option>' . "\n" .
144 '<option value="3">March</option>' . "\n" .
145 '<option value="4">April</option>' . "\n" .
146 '<option value="5">May</option>' . "\n" .
147 '<option value="6">June</option>' . "\n" .
148 '<option value="7">July</option>' . "\n" .
149 '<option value="8">August</option>' . "\n" .
150 '<option value="9">September</option>' . "\n" .
151 '<option value="10">October</option>' . "\n" .
152 '<option value="11">November</option>' . "\n" .
153 '<option value="12">December</option></select>',
154 Xml
::dateMenu( 2011, -1 ),
155 "Date menu with negative month for 'All'"
158 Xml
::dateMenu( $curYear, $curMonth ),
159 Xml
::dateMenu( '', $curMonth ),
160 "Date menu year is the current one when not specified"
163 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
165 Xml
::dateMenu( $wantedYear, $nextMonth ),
166 Xml
::dateMenu( '', $nextMonth ),
167 "Date menu next month is 11 months ago"
171 '<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" .
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( '', '' ),
185 "Date menu with neither year or month"
192 function testTextareaNoContent() {
194 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
195 Xml
::textarea( 'name', '' ),
196 'textarea() with not content'
200 function testTextareaAttribs() {
202 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
203 Xml
::textarea( 'name', '<txt>', 20, 10 ),
204 'textarea() with custom attribs'
211 function testLabelCreation() {
213 '<label for="id">name</label>',
214 Xml
::label( 'name', 'id' ),
215 'label() with no attribs'
219 function testLabelAttributeCanOnlyBeClassOrTitle() {
221 '<label for="id">name</label>',
222 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
223 'label() can not be given a generated attribute'
226 '<label for="id" class="nice">name</label>',
227 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
228 'label() can get a class attribute'
231 '<label for="id" title="nice tooltip">name</label>',
232 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
233 'label() can get a title attribute'
236 '<label for="id" class="nice" title="nice tooltip">name</label>',
237 Xml
::label( 'name', 'id', array(
240 'title' => 'nice tooltip',
241 'anotherattr' => 'value',
244 'label() skip all attributes but "class" and "title"'
248 function testLanguageSelector() {
249 $select = Xml
::languageSelector( 'en', true, null,
250 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
252 '<label for="testlang">Language:</label>',
260 function testEscapeJsStringSpecialChars() {
263 Xml
::escapeJsString( "\\\r\n" ),
264 'escapeJsString() with special characters'
268 function testEncodeJsVarBoolean() {
271 Xml
::encodeJsVar( true ),
272 'encodeJsVar() with boolean'
276 function testEncodeJsVarNull() {
279 Xml
::encodeJsVar( null ),
280 'encodeJsVar() with null'
284 function testEncodeJsVarArray() {
287 Xml
::encodeJsVar( array( 'a', 1 ) ),
288 'encodeJsVar() with array'
292 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
293 'encodeJsVar() with associative array'
297 function testEncodeJsVarObject() {
300 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
301 'encodeJsVar() with object'
305 function testEncodeJsVarInt() {
308 Xml
::encodeJsVar( 123456 ),
309 'encodeJsVar() with int'
313 function testEncodeJsVarFloat() {
316 Xml
::encodeJsVar( 1.23456 ),
317 'encodeJsVar() with float'
321 function testEncodeJsVarIntString() {
324 Xml
::encodeJsVar( '123456' ),
325 'encodeJsVar() with int-like string'
329 function testEncodeJsVarFloatString() {
332 Xml
::encodeJsVar( '1.23456' ),
333 'encodeJsVar() with float-like string'