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)'
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 ) { $prevMonth = 12; }
115 $nextMonth = $curMonth +
1;
116 if( $nextMonth == 13 ) { $nextMonth = 1; }
119 '<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" .
120 '<option value="1">January</option>' . "\n" .
121 '<option value="2" selected="">February</option>' . "\n" .
122 '<option value="3">March</option>' . "\n" .
123 '<option value="4">April</option>' . "\n" .
124 '<option value="5">May</option>' . "\n" .
125 '<option value="6">June</option>' . "\n" .
126 '<option value="7">July</option>' . "\n" .
127 '<option value="8">August</option>' . "\n" .
128 '<option value="9">September</option>' . "\n" .
129 '<option value="10">October</option>' . "\n" .
130 '<option value="11">November</option>' . "\n" .
131 '<option value="12">December</option></select>',
132 Xml
::dateMenu( 2011, 02 ),
133 "Date menu for february 2011"
136 '<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" .
137 '<option value="1">January</option>' . "\n" .
138 '<option value="2">February</option>' . "\n" .
139 '<option value="3">March</option>' . "\n" .
140 '<option value="4">April</option>' . "\n" .
141 '<option value="5">May</option>' . "\n" .
142 '<option value="6">June</option>' . "\n" .
143 '<option value="7">July</option>' . "\n" .
144 '<option value="8">August</option>' . "\n" .
145 '<option value="9">September</option>' . "\n" .
146 '<option value="10">October</option>' . "\n" .
147 '<option value="11">November</option>' . "\n" .
148 '<option value="12">December</option></select>',
149 Xml
::dateMenu( 2011, -1),
150 "Date menu with negative month for 'All'"
153 Xml
::dateMenu( $curYear, $curMonth ),
154 Xml
::dateMenu( '' , $curMonth ),
155 "Date menu year is the current one when not specified"
158 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
160 Xml
::dateMenu( $wantedYear, $nextMonth ),
161 Xml
::dateMenu( '', $nextMonth ),
162 "Date menu next month is 11 months ago"
166 '<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" .
167 '<option value="1">January</option>' . "\n" .
168 '<option value="2">February</option>' . "\n" .
169 '<option value="3">March</option>' . "\n" .
170 '<option value="4">April</option>' . "\n" .
171 '<option value="5">May</option>' . "\n" .
172 '<option value="6">June</option>' . "\n" .
173 '<option value="7">July</option>' . "\n" .
174 '<option value="8">August</option>' . "\n" .
175 '<option value="9">September</option>' . "\n" .
176 '<option value="10">October</option>' . "\n" .
177 '<option value="11">November</option>' . "\n" .
178 '<option value="12">December</option></select>',
179 Xml
::dateMenu( '', '' ),
180 "Date menu with neither year or month"
187 function testTextareaNoContent() {
189 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
190 Xml
::textarea( 'name', '' ),
191 'textarea() with not content'
195 function testTextareaAttribs() {
197 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
198 Xml
::textarea( 'name', '<txt>', 20, 10 ),
199 'textarea() with custom attribs'
206 function testLabelCreation() {
208 '<label for="id">name</label>',
209 Xml
::label( 'name', 'id' ),
210 'label() with no attribs'
213 function testLabelAttributeCanOnlyBeClassOrTitle() {
215 '<label for="id">name</label>',
216 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
217 'label() can not be given a generated attribute'
220 '<label for="id" class="nice">name</label>',
221 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
222 'label() can get a class attribute'
225 '<label for="id" title="nice tooltip">name</label>',
226 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
227 'label() can get a title attribute'
230 '<label for="id" class="nice" title="nice tooltip">name</label>',
231 Xml
::label( 'name', 'id', array(
234 'title' => 'nice tooltip',
235 'anotherattr' => 'value',
238 'label() skip all attributes but "class" and "title"'
242 function testLanguageSelector() {
243 $select = Xml
::languageSelector( 'en', true, null,
244 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
246 '<label for="testlang">Language:</label>',
254 function testEscapeJsStringSpecialChars() {
257 Xml
::escapeJsString( "\\\r\n" ),
258 'escapeJsString() with special characters'
262 function testEncodeJsVarBoolean() {
265 Xml
::encodeJsVar( true ),
266 'encodeJsVar() with boolean'
270 function testEncodeJsVarNull() {
273 Xml
::encodeJsVar( null ),
274 'encodeJsVar() with null'
278 function testEncodeJsVarArray() {
281 Xml
::encodeJsVar( array( 'a', 1 ) ),
282 'encodeJsVar() with array'
286 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
287 'encodeJsVar() with associative array'
291 function testEncodeJsVarObject() {
294 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
295 'encodeJsVar() with object'
299 function testEncodeJsVarInt() {
302 Xml
::encodeJsVar( 123456 ),
303 'encodeJsVar() with int'
307 function testEncodeJsVarFloat() {
310 Xml
::encodeJsVar( 1.23456 ),
311 'encodeJsVar() with float'
315 function testEncodeJsVarIntString() {
318 Xml
::encodeJsVar( '123456' ),
319 'encodeJsVar() with int-like string'
323 function testEncodeJsVarFloatString() {
326 Xml
::encodeJsVar( '1.23456' ),
327 'encodeJsVar() with float-like string'