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(
35 public function testExpandAttributes() {
36 $this->assertNull( Xml
::expandAttributes(null),
37 'Converting a null list of attributes'
39 $this->assertEquals( '', Xml
::expandAttributes( array() ),
40 'Converting an empty list of attributes'
44 public function testExpandAttributesException() {
45 $this->setExpectedException('MWException');
46 Xml
::expandAttributes('string');
49 function testElementOpen() {
52 Xml
::element( 'element', null, null ),
53 'Opening element with no attributes'
57 function testElementEmpty() {
60 Xml
::element( 'element', null, '' ),
61 'Terminated empty element'
65 function testElementInputCanHaveAValueOfZero() {
67 '<input name="name" value="0" />',
68 Xml
::input( 'name', false, 0 ),
69 'Input with a value of 0 (bug 23797)'
72 function testElementEscaping() {
74 '<element>hello <there> you & you</element>',
75 Xml
::element( 'element', null, 'hello <there> you & you' ),
76 'Element with no attributes and content that needs escaping'
80 public function testEscapeTagsOnly() {
81 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
82 'replace " > and < with their HTML entitites'
86 function testElementAttributes() {
88 '<element key="value" <>="<>">',
89 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
90 'Element attributes, keys are not escaped'
94 function testOpenElement() {
97 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
98 'openElement() shortcut'
102 function testCloseElement() {
103 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
106 public function testDateMenu( ) {
107 $curYear = intval(gmdate('Y'));
108 $prevYear = $curYear - 1;
110 $curMonth = intval(gmdate('n'));
111 $prevMonth = $curMonth - 1;
112 if( $prevMonth == 0 ) { $prevMonth = 12; }
113 $nextMonth = $curMonth +
1;
114 if( $nextMonth == 13 ) { $nextMonth = 1; }
117 '<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" .
118 '<option value="1">January</option>' . "\n" .
119 '<option value="2" selected="">February</option>' . "\n" .
120 '<option value="3">March</option>' . "\n" .
121 '<option value="4">April</option>' . "\n" .
122 '<option value="5">May</option>' . "\n" .
123 '<option value="6">June</option>' . "\n" .
124 '<option value="7">July</option>' . "\n" .
125 '<option value="8">August</option>' . "\n" .
126 '<option value="9">September</option>' . "\n" .
127 '<option value="10">October</option>' . "\n" .
128 '<option value="11">November</option>' . "\n" .
129 '<option value="12">December</option></select>',
130 Xml
::dateMenu( 2011, 02 ),
131 "Date menu for february 2011"
134 '<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" .
135 '<option value="1">January</option>' . "\n" .
136 '<option value="2">February</option>' . "\n" .
137 '<option value="3">March</option>' . "\n" .
138 '<option value="4">April</option>' . "\n" .
139 '<option value="5">May</option>' . "\n" .
140 '<option value="6">June</option>' . "\n" .
141 '<option value="7">July</option>' . "\n" .
142 '<option value="8">August</option>' . "\n" .
143 '<option value="9">September</option>' . "\n" .
144 '<option value="10">October</option>' . "\n" .
145 '<option value="11">November</option>' . "\n" .
146 '<option value="12">December</option></select>',
147 Xml
::dateMenu( 2011, -1),
148 "Date menu with negative month for 'All'"
151 Xml
::dateMenu( $curYear, $curMonth ),
152 Xml
::dateMenu( '' , $curMonth ),
153 "Date menu year is the current one when not specified"
156 $wantedYear = $nextMonth == 1 ?
$curYear : $prevYear;
158 Xml
::dateMenu( $wantedYear, $nextMonth ),
159 Xml
::dateMenu( '', $nextMonth ),
160 "Date menu next month is 11 months ago"
164 '<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" .
165 '<option value="1">January</option>' . "\n" .
166 '<option value="2">February</option>' . "\n" .
167 '<option value="3">March</option>' . "\n" .
168 '<option value="4">April</option>' . "\n" .
169 '<option value="5">May</option>' . "\n" .
170 '<option value="6">June</option>' . "\n" .
171 '<option value="7">July</option>' . "\n" .
172 '<option value="8">August</option>' . "\n" .
173 '<option value="9">September</option>' . "\n" .
174 '<option value="10">October</option>' . "\n" .
175 '<option value="11">November</option>' . "\n" .
176 '<option value="12">December</option></select>',
177 Xml
::dateMenu( '', '' ),
178 "Date menu with neither year or month"
185 function testTextareaNoContent() {
187 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
188 Xml
::textarea( 'name', '' ),
189 'textarea() with not content'
193 function testTextareaAttribs() {
195 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
196 Xml
::textarea( 'name', '<txt>', 20, 10 ),
197 'textarea() with custom attribs'
204 function testLabelCreation() {
206 '<label for="id">name</label>',
207 Xml
::label( 'name', 'id' ),
208 'label() with no attribs'
211 function testLabelAttributeCanOnlyBeClassOrTitle() {
213 '<label for="id">name</label>',
214 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
215 'label() can not be given a generated attribute'
218 '<label for="id" class="nice">name</label>',
219 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
220 'label() can get a class attribute'
223 '<label for="id" title="nice tooltip">name</label>',
224 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
225 'label() can get a title attribute'
228 '<label for="id" class="nice" title="nice tooltip">name</label>',
229 Xml
::label( 'name', 'id', array(
232 'title' => 'nice tooltip',
233 'anotherattr' => 'value',
236 'label() skip all attributes but "class" and "title"'
240 function testLanguageSelector() {
241 $select = Xml
::languageSelector( 'en', true, null,
242 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
244 '<label for="testlang">Language:</label>',
252 function testEscapeJsStringSpecialChars() {
255 Xml
::escapeJsString( "\\\r\n" ),
256 'escapeJsString() with special characters'
260 function testEncodeJsVarBoolean() {
263 Xml
::encodeJsVar( true ),
264 'encodeJsVar() with boolean'
268 function testEncodeJsVarNull() {
271 Xml
::encodeJsVar( null ),
272 'encodeJsVar() with null'
276 function testEncodeJsVarArray() {
279 Xml
::encodeJsVar( array( 'a', 1 ) ),
280 'encodeJsVar() with array'
284 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
285 'encodeJsVar() with associative array'
289 function testEncodeJsVarObject() {
292 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
293 'encodeJsVar() with object'
297 function testEncodeJsVarInt() {
300 Xml
::encodeJsVar( 123456 ),
301 'encodeJsVar() with int'
305 function testEncodeJsVarFloat() {
308 Xml
::encodeJsVar( 1.23456 ),
309 'encodeJsVar() with float'
313 function testEncodeJsVarIntString() {
316 Xml
::encodeJsVar( '123456' ),
317 'encodeJsVar() with int-like string'
321 function testEncodeJsVarFloatString() {
324 Xml
::encodeJsVar( '1.23456' ),
325 'encodeJsVar() with float-like string'