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(
32 'wgWellFormedXml' => true,
36 public function testExpandAttributes() {
37 $this->assertNull( Xml
::expandAttributes( null ),
38 'Converting a null list of attributes'
40 $this->assertEquals( '', Xml
::expandAttributes( array() ),
41 'Converting an empty list of attributes'
45 public function testExpandAttributesException() {
46 $this->setExpectedException( 'MWException' );
47 Xml
::expandAttributes( 'string' );
50 function testElementOpen() {
53 Xml
::element( 'element', null, null ),
54 'Opening element with no attributes'
58 function testElementEmpty() {
61 Xml
::element( 'element', null, '' ),
62 'Terminated empty element'
66 function testElementInputCanHaveAValueOfZero() {
68 '<input name="name" value="0" />',
69 Xml
::input( 'name', false, 0 ),
70 'Input with a value of 0 (bug 23797)'
74 function testElementEscaping() {
76 '<element>hello <there> you & you</element>',
77 Xml
::element( 'element', null, 'hello <there> you & you' ),
78 'Element with no attributes and content that needs escaping'
82 public function testEscapeTagsOnly() {
83 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
84 'replace " > and < with their HTML entitites'
88 function testElementAttributes() {
90 '<element key="value" <>="<>">',
91 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
92 'Element attributes, keys are not escaped'
96 function testOpenElement() {
99 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
100 'openElement() shortcut'
104 function testCloseElement() {
105 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
108 public function testDateMenu() {
109 $curYear = intval( gmdate( 'Y' ) );
110 $prevYear = $curYear - 1;
112 $curMonth = intval( gmdate( 'n' ) );
113 $prevMonth = $curMonth - 1;
114 if ( $prevMonth == 0 ) {
117 $nextMonth = $curMonth +
1;
118 if ( $nextMonth == 13 ) {
123 '<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" .
124 '<option value="1">January</option>' . "\n" .
125 '<option value="2" selected="">February</option>' . "\n" .
126 '<option value="3">March</option>' . "\n" .
127 '<option value="4">April</option>' . "\n" .
128 '<option value="5">May</option>' . "\n" .
129 '<option value="6">June</option>' . "\n" .
130 '<option value="7">July</option>' . "\n" .
131 '<option value="8">August</option>' . "\n" .
132 '<option value="9">September</option>' . "\n" .
133 '<option value="10">October</option>' . "\n" .
134 '<option value="11">November</option>' . "\n" .
135 '<option value="12">December</option></select>',
136 Xml
::dateMenu( 2011, 02 ),
137 "Date menu for february 2011"
140 '<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" .
141 '<option value="1">January</option>' . "\n" .
142 '<option value="2">February</option>' . "\n" .
143 '<option value="3">March</option>' . "\n" .
144 '<option value="4">April</option>' . "\n" .
145 '<option value="5">May</option>' . "\n" .
146 '<option value="6">June</option>' . "\n" .
147 '<option value="7">July</option>' . "\n" .
148 '<option value="8">August</option>' . "\n" .
149 '<option value="9">September</option>' . "\n" .
150 '<option value="10">October</option>' . "\n" .
151 '<option value="11">November</option>' . "\n" .
152 '<option value="12">December</option></select>',
153 Xml
::dateMenu( 2011, -1 ),
154 "Date menu with negative month for 'All'"
157 Xml
::dateMenu( $curYear, $curMonth ),
158 Xml
::dateMenu( '', $curMonth ),
159 "Date menu year is the current one when not specified"
162 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
164 Xml
::dateMenu( $wantedYear, $nextMonth ),
165 Xml
::dateMenu( '', $nextMonth ),
166 "Date menu next month is 11 months ago"
170 '<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" .
171 '<option value="1">January</option>' . "\n" .
172 '<option value="2">February</option>' . "\n" .
173 '<option value="3">March</option>' . "\n" .
174 '<option value="4">April</option>' . "\n" .
175 '<option value="5">May</option>' . "\n" .
176 '<option value="6">June</option>' . "\n" .
177 '<option value="7">July</option>' . "\n" .
178 '<option value="8">August</option>' . "\n" .
179 '<option value="9">September</option>' . "\n" .
180 '<option value="10">October</option>' . "\n" .
181 '<option value="11">November</option>' . "\n" .
182 '<option value="12">December</option></select>',
183 Xml
::dateMenu( '', '' ),
184 "Date menu with neither year or month"
191 function testTextareaNoContent() {
193 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
194 Xml
::textarea( 'name', '' ),
195 'textarea() with not content'
199 function testTextareaAttribs() {
201 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
202 Xml
::textarea( 'name', '<txt>', 20, 10 ),
203 'textarea() with custom attribs'
210 function testLabelCreation() {
212 '<label for="id">name</label>',
213 Xml
::label( 'name', 'id' ),
214 'label() with no attribs'
218 function testLabelAttributeCanOnlyBeClassOrTitle() {
220 '<label for="id">name</label>',
221 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
222 'label() can not be given a generated attribute'
225 '<label for="id" class="nice">name</label>',
226 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
227 'label() can get a class attribute'
230 '<label for="id" title="nice tooltip">name</label>',
231 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
232 'label() can get a title attribute'
235 '<label for="id" class="nice" title="nice tooltip">name</label>',
236 Xml
::label( 'name', 'id', array(
239 'title' => 'nice tooltip',
240 'anotherattr' => 'value',
243 'label() skip all attributes but "class" and "title"'
247 function testLanguageSelector() {
248 $select = Xml
::languageSelector( 'en', true, null,
249 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
251 '<label for="testlang">Language:</label>',
259 function testEscapeJsStringSpecialChars() {
262 Xml
::escapeJsString( "\\\r\n" ),
263 'escapeJsString() with special characters'
267 function testEncodeJsVarBoolean() {
270 Xml
::encodeJsVar( true ),
271 'encodeJsVar() with boolean'
275 function testEncodeJsVarNull() {
278 Xml
::encodeJsVar( null ),
279 'encodeJsVar() with null'
283 function testEncodeJsVarArray() {
286 Xml
::encodeJsVar( array( 'a', 1 ) ),
287 'encodeJsVar() with array'
291 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
292 'encodeJsVar() with associative array'
296 function testEncodeJsVarObject() {
299 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
300 'encodeJsVar() with object'
304 function testEncodeJsVarInt() {
307 Xml
::encodeJsVar( 123456 ),
308 'encodeJsVar() with int'
312 function testEncodeJsVarFloat() {
315 Xml
::encodeJsVar( 1.23456 ),
316 'encodeJsVar() with float'
320 function testEncodeJsVarIntString() {
323 Xml
::encodeJsVar( '123456' ),
324 'encodeJsVar() with int-like string'
328 function testEncodeJsVarFloatString() {
331 Xml
::encodeJsVar( '1.23456' ),
332 'encodeJsVar() with float-like string'