3 class XmlTest
extends MediaWikiTestCase
{
5 public function testExpandAttributes() {
6 $this->assertNull( Xml
::expandAttributes(null),
7 'Converting a null list of attributes'
9 $this->assertEquals( '', Xml
::expandAttributes( array() ),
10 'Converting an empty list of attributes'
14 public function testExpandAttributesException() {
15 $this->setExpectedException('MWException');
16 Xml
::expandAttributes('string');
19 function testElementOpen() {
22 Xml
::element( 'element', null, null ),
23 'Opening element with no attributes'
27 function testElementEmpty() {
30 Xml
::element( 'element', null, '' ),
31 'Terminated empty element'
35 function testElementInputCanHaveAValueOfZero() {
37 '<input name="name" value="0" />',
38 Xml
::input( 'name', false, 0 ),
39 'Input with a value of 0 (bug 23797)'
42 function testElementEscaping() {
44 '<element>hello <there> you & you</element>',
45 Xml
::element( 'element', null, 'hello <there> you & you' ),
46 'Element with no attributes and content that needs escaping'
50 public function testEscapeTagsOnly() {
51 $this->assertEquals( '"><', Xml
::escapeTagsOnly( '"><' ),
52 'replace " > and < with their HTML entitites'
56 function testElementAttributes() {
58 '<element key="value" <>="<>">',
59 Xml
::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
60 'Element attributes, keys are not escaped'
64 function testOpenElement() {
67 Xml
::openElement( 'element', array( 'k' => 'v' ) ),
68 'openElement() shortcut'
72 function testCloseElement() {
73 $this->assertEquals( '</element>', Xml
::closeElement( 'element' ), 'closeElement() shortcut' );
76 public function testDateMenu( ) {
77 $curYear = intval(gmdate('Y'));
78 $prevYear = $curYear - 1;
80 $curMonth = intval(gmdate('n'));
81 $prevMonth = $curMonth - 1;
82 if( $prevMonth == 0 ) { $prevMonth = 12; }
83 $nextMonth = $curMonth +
1;
84 if( $nextMonth == 13 ) { $nextMonth = 1; }
88 '<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>
89 <option value="1">January</option>
90 <option value="2" selected="selected">February</option>
91 <option value="3">March</option>
92 <option value="4">April</option>
93 <option value="5">May</option>
94 <option value="6">June</option>
95 <option value="7">July</option>
96 <option value="8">August</option>
97 <option value="9">September</option>
98 <option value="10">October</option>
99 <option value="11">November</option>
100 <option value="12">December</option></select>',
101 Xml
::dateMenu( 2011, 02 ),
102 "Date menu for february 2011"
105 '<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>
106 <option value="1">January</option>
107 <option value="2">February</option>
108 <option value="3">March</option>
109 <option value="4">April</option>
110 <option value="5">May</option>
111 <option value="6">June</option>
112 <option value="7">July</option>
113 <option value="8">August</option>
114 <option value="9">September</option>
115 <option value="10">October</option>
116 <option value="11">November</option>
117 <option value="12">December</option></select>',
118 Xml
::dateMenu( 2011, -1),
119 "Date menu with negative month for 'All'"
122 Xml
::dateMenu( $curYear, $curMonth ),
123 Xml
::dateMenu( '' , $curMonth ),
124 "Date menu year is the current one when not specified"
127 Xml
::dateMenu( $prevYear, $nextMonth ),
128 Xml
::dateMenu( '', $nextMonth ),
129 "Date menu next month is 11 months ago"
132 # FIXME: please note there is no year there!
134 '<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>
135 <option value="1">January</option>
136 <option value="2">February</option>
137 <option value="3">March</option>
138 <option value="4">April</option>
139 <option value="5">May</option>
140 <option value="6">June</option>
141 <option value="7">July</option>
142 <option value="8">August</option>
143 <option value="9">September</option>
144 <option value="10">October</option>
145 <option value="11">November</option>
146 <option value="12">December</option></select>',
147 Xml
::dateMenu( '', ''),
148 "Date menu with neither year or month"
155 function testTextareaNoContent() {
157 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
158 Xml
::textarea( 'name', '' ),
159 'textarea() with not content'
163 function testTextareaAttribs() {
165 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
166 Xml
::textarea( 'name', '<txt>', 20, 10 ),
167 'textarea() with custom attribs'
174 function testLabelCreation() {
176 '<label for="id">name</label>',
177 Xml
::label( 'name', 'id' ),
178 'label() with no attribs'
181 function testLabelAttributeCanOnlyBeClass() {
183 '<label for="id">name</label>',
184 Xml
::label( 'name', 'id', array( 'generated' => true ) ),
185 'label() can not be given a generated attribute'
188 '<label for="id" class="nice">name</label>',
189 Xml
::label( 'name', 'id', array( 'class' => 'nice' ) ),
190 'label() can get a class attribute'
193 '<label for="id" class="nice">name</label>',
194 Xml
::label( 'name', 'id', array(
197 'anotherattr' => 'value',
200 'label() skip all attributes but "class"'
207 function testEscapeJsStringSpecialChars() {
210 Xml
::escapeJsString( "\\\r\n" ),
211 'escapeJsString() with special characters'
215 function testEncodeJsVarBoolean() {
218 Xml
::encodeJsVar( true ),
219 'encodeJsVar() with boolean'
223 function testEncodeJsVarNull() {
226 Xml
::encodeJsVar( null ),
227 'encodeJsVar() with null'
231 function testEncodeJsVarArray() {
234 Xml
::encodeJsVar( array( 'a', 1 ) ),
235 'encodeJsVar() with array'
238 '{"a": "a", "b": 1}',
239 Xml
::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
240 'encodeJsVar() with associative array'
244 function testEncodeJsVarObject() {
246 '{"a": "a", "b": 1}',
247 Xml
::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
248 'encodeJsVar() with object'