Complete coverage of Xml::DateMenu()
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlTest.php
1 <?php
2
3 class XmlTest extends MediaWikiTestCase {
4
5 public function testExpandAttributes() {
6 $this->assertNull( Xml::expandAttributes(null),
7 'Converting a null list of attributes'
8 );
9 $this->assertEquals( '', Xml::expandAttributes( array() ),
10 'Converting an empty list of attributes'
11 );
12 }
13
14 public function testExpandAttributesException() {
15 $this->setExpectedException('MWException');
16 Xml::expandAttributes('string');
17 }
18
19 function testElementOpen() {
20 $this->assertEquals(
21 '<element>',
22 Xml::element( 'element', null, null ),
23 'Opening element with no attributes'
24 );
25 }
26
27 function testElementEmpty() {
28 $this->assertEquals(
29 '<element />',
30 Xml::element( 'element', null, '' ),
31 'Terminated empty element'
32 );
33 }
34
35 function testElementInputCanHaveAValueOfZero() {
36 $this->assertEquals(
37 '<input name="name" value="0" />',
38 Xml::input( 'name', false, 0 ),
39 'Input with a value of 0 (bug 23797)'
40 );
41 }
42 function testElementEscaping() {
43 $this->assertEquals(
44 '<element>hello &lt;there&gt; you &amp; you</element>',
45 Xml::element( 'element', null, 'hello <there> you & you' ),
46 'Element with no attributes and content that needs escaping'
47 );
48 }
49
50 public function testEscapeTagsOnly() {
51 $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
52 'replace " > and < with their HTML entitites'
53 );
54 }
55
56 function testElementAttributes() {
57 $this->assertEquals(
58 '<element key="value" <>="&lt;&gt;">',
59 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
60 'Element attributes, keys are not escaped'
61 );
62 }
63
64 function testOpenElement() {
65 $this->assertEquals(
66 '<element k="v">',
67 Xml::openElement( 'element', array( 'k' => 'v' ) ),
68 'openElement() shortcut'
69 );
70 }
71
72 function testCloseElement() {
73 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
74 }
75
76 public function testDateMenu( ) {
77 $curYear = intval(gmdate('Y'));
78 $prevYear = $curYear - 1;
79
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; }
85
86
87 $this->assertEquals(
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"
103 );
104 $this->assertEquals(
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'"
120 );
121 $this->assertEquals(
122 Xml::dateMenu( $curYear, $curMonth ),
123 Xml::dateMenu( '' , $curMonth ),
124 "Date menu year is the current one when not specified"
125 );
126 $this->assertEquals(
127 Xml::dateMenu( $prevYear, $nextMonth ),
128 Xml::dateMenu( '', $nextMonth ),
129 "Date menu next month is 11 months ago"
130 );
131
132 # FIXME: please note there is no year there!
133 $this->assertEquals(
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"
149 );
150 }
151
152 #
153 # textarea
154 #
155 function testTextareaNoContent() {
156 $this->assertEquals(
157 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
158 Xml::textarea( 'name', '' ),
159 'textarea() with not content'
160 );
161 }
162
163 function testTextareaAttribs() {
164 $this->assertEquals(
165 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
166 Xml::textarea( 'name', '<txt>', 20, 10 ),
167 'textarea() with custom attribs'
168 );
169 }
170
171 #
172 # input and label
173 #
174 function testLabelCreation() {
175 $this->assertEquals(
176 '<label for="id">name</label>',
177 Xml::label( 'name', 'id' ),
178 'label() with no attribs'
179 );
180 }
181 function testLabelAttributeCanOnlyBeClass() {
182 $this->assertEquals(
183 '<label for="id">name</label>',
184 Xml::label( 'name', 'id', array( 'generated' => true ) ),
185 'label() can not be given a generated attribute'
186 );
187 $this->assertEquals(
188 '<label for="id" class="nice">name</label>',
189 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
190 'label() can get a class attribute'
191 );
192 $this->assertEquals(
193 '<label for="id" class="nice">name</label>',
194 Xml::label( 'name', 'id', array(
195 'generated' => true,
196 'class' => 'nice',
197 'anotherattr' => 'value',
198 )
199 ),
200 'label() skip all attributes but "class"'
201 );
202 }
203
204 #
205 # JS
206 #
207 function testEscapeJsStringSpecialChars() {
208 $this->assertEquals(
209 '\\\\\r\n',
210 Xml::escapeJsString( "\\\r\n" ),
211 'escapeJsString() with special characters'
212 );
213 }
214
215 function testEncodeJsVarBoolean() {
216 $this->assertEquals(
217 'true',
218 Xml::encodeJsVar( true ),
219 'encodeJsVar() with boolean'
220 );
221 }
222
223 function testEncodeJsVarNull() {
224 $this->assertEquals(
225 'null',
226 Xml::encodeJsVar( null ),
227 'encodeJsVar() with null'
228 );
229 }
230
231 function testEncodeJsVarArray() {
232 $this->assertEquals(
233 '["a", 1]',
234 Xml::encodeJsVar( array( 'a', 1 ) ),
235 'encodeJsVar() with array'
236 );
237 $this->assertEquals(
238 '{"a": "a", "b": 1}',
239 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
240 'encodeJsVar() with associative array'
241 );
242 }
243
244 function testEncodeJsVarObject() {
245 $this->assertEquals(
246 '{"a": "a", "b": 1}',
247 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
248 'encodeJsVar() with object'
249 );
250 }
251 }