3 class XmlTest
extends MediaWikiTestCase
{
4 private static $oldLang;
6 public function setUp() {
7 global $wgLang, $wgLanguageCode;
9 self
::$oldLang = $wgLang;
10 $wgLanguageCode = 'en';
11 $wgLang = Language
::factory( $wgLanguageCode );
14 public function tearDown() {
15 global $wgLang, $wgLanguageCode;
16 $wgLang = self
::$oldLang;
17 $wgLanguageCode = $wgLang->getCode();
20 public function testExpandAttributes() {
21 $this->assertNull( Xml
::expandAttributes(null),
22 'Converting a null list of attributes'
24 $this->assertEquals( '', Xml
::expandAttributes( array() ),
25 'Converting an empty list of attributes'
29 public function testExpandAttributesException() {
30 $this->setExpectedException('MWException');
31 Xml
::expandAttributes('string');
34 function testElementOpen() {
37 Xml
::element( 'element', null, null ),
38 'Opening element with no attributes'
42 function testElementEmpty() {
45 Xml
::element( 'element', null, '' ),
46 'Terminated empty element'
50 function testElementInputCanHaveAValueOfZero() {
52 '<input name="name" value="0" />',
53 Xml
::input( 'name', false, 0 ),
54 'Input with a value of 0 (bug 23797)'
57 function testElementEscaping() {
59 '<element>hello <there> you & you</element>',
60 Xml
::element( 'element', null, 'hello <there> you & you' ),
61 'Element with no attributes and content that needs escaping'
65 public function testEscapeTagsOnly() {
66 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
67 'replace " > and < with their HTML entitites'
71 function testElementAttributes() {
73 '<element key="value" <>="<>">',
74 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
75 'Element attributes, keys are not escaped'
79 function testOpenElement() {
82 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
83 'openElement() shortcut'
87 function testCloseElement() {
88 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
91 public function testDateMenu( ) {
92 $curYear = intval(gmdate('Y'));
93 $prevYear = $curYear - 1;
95 $curMonth = intval(gmdate('n'));
96 $prevMonth = $curMonth - 1;
97 if( $prevMonth == 0 ) { $prevMonth = 12; }
98 $nextMonth = $curMonth +
1;
99 if( $nextMonth == 13 ) { $nextMonth = 1; }
103 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="2011" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
104 '<option value="1">January</option>' . "\n" .
105 '<option value="2" selected="">February</option>' . "\n" .
106 '<option value="3">March</option>' . "\n" .
107 '<option value="4">April</option>' . "\n" .
108 '<option value="5">May</option>' . "\n" .
109 '<option value="6">June</option>' . "\n" .
110 '<option value="7">July</option>' . "\n" .
111 '<option value="8">August</option>' . "\n" .
112 '<option value="9">September</option>' . "\n" .
113 '<option value="10">October</option>' . "\n" .
114 '<option value="11">November</option>' . "\n" .
115 '<option value="12">December</option></select>',
116 Xml
::dateMenu( 2011, 02 ),
117 "Date menu for february 2011"
120 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="2011" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
121 '<option value="1">January</option>' . "\n" .
122 '<option value="2">February</option>' . "\n" .
123 '<option value="3">March</option>' . "\n" .
124 '<option value="4">April</option>' . "\n" .
125 '<option value="5">May</option>' . "\n" .
126 '<option value="6">June</option>' . "\n" .
127 '<option value="7">July</option>' . "\n" .
128 '<option value="8">August</option>' . "\n" .
129 '<option value="9">September</option>' . "\n" .
130 '<option value="10">October</option>' . "\n" .
131 '<option value="11">November</option>' . "\n" .
132 '<option value="12">December</option></select>',
133 Xml
::dateMenu( 2011, -1),
134 "Date menu with negative month for 'All'"
137 Xml
::dateMenu( $curYear, $curMonth ),
138 Xml
::dateMenu( '' , $curMonth ),
139 "Date menu year is the current one when not specified"
142 // @todo FIXME: next month can be in the next year
143 // test failing because it is now december
144 /*$this->assertEquals(
145 Xml::dateMenu( $prevYear, $nextMonth ),
146 Xml::dateMenu( '', $nextMonth ),
147 "Date menu next month is 11 months ago"
150 # @todo FIXME: Please note there is no year there!
152 '<label for="year">From year (and earlier):</label> <input name="year" size="4" value="" id="year" maxlength="4" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
153 '<option value="1">January</option>' . "\n" .
154 '<option value="2">February</option>' . "\n" .
155 '<option value="3">March</option>' . "\n" .
156 '<option value="4">April</option>' . "\n" .
157 '<option value="5">May</option>' . "\n" .
158 '<option value="6">June</option>' . "\n" .
159 '<option value="7">July</option>' . "\n" .
160 '<option value="8">August</option>' . "\n" .
161 '<option value="9">September</option>' . "\n" .
162 '<option value="10">October</option>' . "\n" .
163 '<option value="11">November</option>' . "\n" .
164 '<option value="12">December</option></select>',
165 Xml
::dateMenu( '', '' ),
166 "Date menu with neither year or month"
173 function testTextareaNoContent() {
175 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
176 Xml
::textarea( 'name', '' ),
177 'textarea() with not content'
181 function testTextareaAttribs() {
183 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
184 Xml
::textarea( 'name', '<txt>', 20, 10 ),
185 'textarea() with custom attribs'
192 function testLabelCreation() {
194 '<label for="id">name</label>',
195 Xml
::label( 'name', 'id' ),
196 'label() with no attribs'
199 function testLabelAttributeCanOnlyBeClassOrTitle() {
201 '<label for="id">name</label>',
202 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
203 'label() can not be given a generated attribute'
206 '<label for="id" class="nice">name</label>',
207 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
208 'label() can get a class attribute'
211 '<label for="id" title="nice tooltip">name</label>',
212 Xml
::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
213 'label() can get a title attribute'
216 '<label for="id" class="nice" title="nice tooltip">name</label>',
217 Xml
::label( 'name', 'id', array(
220 'title' => 'nice tooltip',
221 'anotherattr' => 'value',
224 'label() skip all attributes but "class" and "title"'
231 function testEscapeJsStringSpecialChars() {
234 Xml
::escapeJsString( "\\\r\n" ),
235 'escapeJsString() with special characters'
239 function testEncodeJsVarBoolean() {
242 Xml
::encodeJsVar( true ),
243 'encodeJsVar() with boolean'
247 function testEncodeJsVarNull() {
250 Xml
::encodeJsVar( null ),
251 'encodeJsVar() with null'
255 function testEncodeJsVarArray() {
258 Xml
::encodeJsVar( array( 'a', 1 ) ),
259 'encodeJsVar() with array'
262 '{"a": "a", "b": 1}',
263 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
264 'encodeJsVar() with associative array'
268 function testEncodeJsVarObject() {
270 '{"a": "a", "b": 1}',
271 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
272 'encodeJsVar() with object'
276 function testEncodeJsVarInt() {
279 Xml
::encodeJsVar( 123456 ),
280 'encodeJsVar() with int'
284 function testEncodeJsVarFloat() {
287 Xml
::encodeJsVar( 1.23456 ),
288 'encodeJsVar() with float'
292 function testEncodeJsVarIntString() {
295 Xml
::encodeJsVar( '123456' ),
296 'encodeJsVar() with int-like string'
300 function testEncodeJsVarFloatString() {
303 Xml
::encodeJsVar( '1.23456' ),
304 'encodeJsVar() with float-like string'