Debian Codegraph @

tests/phpunit/includes/xml/XmlTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Xml\Xml;
5
6 /**
7 * See also \MediaWiki\Tests\Unit\XmlTest for the pure unit tests
8 *
9 * @group Xml
10 * @covers \MediaWiki\Xml\Xml
11 */
12 class XmlTest extends MediaWikiIntegrationTestCase {
13
14 protected function setUp(): void {
15 parent::setUp();
16
17 $this->overrideConfigValues( [
18 MainConfigNames::LanguageCode => 'en',
19 ] );
20
21 $langObj = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
22 $langObj->setNamespaces( [
23 -2 => 'Media',
24 -1 => 'Special',
25 0 => '',
26 1 => 'Talk',
27 2 => 'User',
28 3 => 'User_talk',
29 4 => 'MyWiki',
30 5 => 'MyWiki_Talk',
31 6 => 'File',
32 7 => 'File_talk',
33 8 => 'MediaWiki',
34 9 => 'MediaWiki_talk',
35 10 => 'Template',
36 11 => 'Template_talk',
37 100 => 'Custom',
38 101 => 'Custom_talk',
39 ] );
40
41 $this->setUserLang( $langObj );
42 }
43
44 public static function provideElement() {
45 // $expect, $element, $attribs, $contents
46 yield 'Opening element with no attributes' => [ '<element>', 'element', null, null ];
47 yield 'Terminated empty element' => [ '<element />', 'element', null, '' ];
48 yield 'Element with no attributes and content that needs escaping' => [
49 '<element>"hello &lt;there&gt; your\'s &amp; you"</element>',
50 'element',
51 null,
52 '"hello <there> your\'s & you"'
53 ];
54 yield 'Element attributes, keys are not escaped' => [
55 '<element key="value" <>="&lt;&gt;">',
56 'element',
57 [ 'key' => 'value', '<>' => '<>' ],
58 null
59 ];
60 }
61
62 /**
63 * @dataProvider provideElement
64 */
65 public function testElement( string $expect, string $element, $attribs, $content ) {
66 $this->assertEquals(
67 $expect,
68 Xml::element( $element, $attribs, $content )
69 );
70 }
71
72 public function testElementInputCanHaveAValueOfZero() {
73 $this->assertEquals(
74 '<input name="name" value="0" />',
75 Xml::input( 'name', false, 0 ),
76 'Input with a value of 0 (T25797)'
77 );
78 }
79
80 public function testOpenElement() {
81 $this->assertEquals(
82 '<element k="v">',
83 Xml::openElement( 'element', [ 'k' => 'v' ] ),
84 'openElement() shortcut'
85 );
86 }
87
88 public function testCloseElement() {
89 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
90 }
91
92 public static function provideMonthSelector() {
93 # providers are run before services are set up
94 $lang = new class() {
95 public function getMonthName( $i ) {
96 $months = [
97 'January', 'February', 'March', 'April', 'May', 'June',
98 'July', 'August', 'September', 'October', 'November',
99 'December',
100 ];
101 return $months[$i - 1] ?? 'unknown';
102 }
103 };
104
105 $header = '<select name="month" id="month" class="mw-month-selector">';
106 $header2 = '<select name="month" id="monthSelector" class="mw-month-selector">';
107 $monthsString = '';
108 for ( $i = 1; $i < 13; $i++ ) {
109 $monthName = $lang->getMonthName( $i );
110 $monthsString .= "<option value=\"{$i}\">{$monthName}</option>";
111 if ( $i !== 12 ) {
112 $monthsString .= "\n";
113 }
114 }
115 $monthsString2 = str_replace(
116 '<option value="12">December</option>',
117 '<option value="12" selected="">December</option>',
118 $monthsString
119 );
120 $end = '</select>';
121
122 $allMonths = "<option value=\"AllMonths\">all</option>\n";
123 return [
124 [ $header . $monthsString . $end, '', null, 'month' ],
125 [ $header . $monthsString2 . $end, 12, null, 'month' ],
126 [ $header2 . $monthsString . $end, '', null, 'monthSelector' ],
127 [ $header . $allMonths . $monthsString . $end, '', 'AllMonths', 'month' ],
128
129 ];
130 }
131
132 /**
133 * @dataProvider provideMonthSelector
134 */
135 public function testMonthSelector( $expected, $selected, $allmonths, $id ) {
136 $this->hideDeprecated( 'MediaWiki\Xml\Xml::monthSelector' );
137 $this->assertEquals(
138 $expected,
139 Xml::monthSelector( $selected, $allmonths, $id )
140 );
141 }
142
143 public function testSpan() {
144 $this->assertEquals(
145 '<span class="foo" id="testSpan">element</span>',
146 Xml::span( 'element', 'foo', [ 'id' => 'testSpan' ] )
147 );
148 }
149
150 public function testDateMenu() {
151 $curYear = intval( gmdate( 'Y' ) );
152 $prevYear = $curYear - 1;
153
154 $curMonth = intval( gmdate( 'n' ) );
155
156 $nextMonth = $curMonth + 1;
157 if ( $nextMonth == 13 ) {
158 $nextMonth = 1;
159 }
160
161 $this->hideDeprecated( 'MediaWiki\Xml\Xml::dateMenu' );
162 $this->hideDeprecated( 'MediaWiki\Xml\Xml::monthSelector' );
163
164 $this->assertEquals(
165 '<label for="year">From year (and earlier):</label> ' .
166 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"> ' .
167 '<label for="month">From month (and earlier):</label> ' .
168 '<select name="month" id="month" class="mw-month-selector">' .
169 '<option value="-1">all</option>' . "\n" .
170 '<option value="1">January</option>' . "\n" .
171 '<option value="2" selected="">February</option>' . "\n" .
172 '<option value="3">March</option>' . "\n" .
173 '<option value="4">April</option>' . "\n" .
174 '<option value="5">May</option>' . "\n" .
175 '<option value="6">June</option>' . "\n" .
176 '<option value="7">July</option>' . "\n" .
177 '<option value="8">August</option>' . "\n" .
178 '<option value="9">September</option>' . "\n" .
179 '<option value="10">October</option>' . "\n" .
180 '<option value="11">November</option>' . "\n" .
181 '<option value="12">December</option></select>',
182 Xml::dateMenu( 2011, 02 ),
183 "Date menu for february 2011"
184 );
185 $this->assertEquals(
186 '<label for="year">From year (and earlier):</label> ' .
187 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"> ' .
188 '<label for="month">From month (and earlier):</label> ' .
189 '<select name="month" id="month" class="mw-month-selector">' .
190 '<option value="-1">all</option>' . "\n" .
191 '<option value="1">January</option>' . "\n" .
192 '<option value="2">February</option>' . "\n" .
193 '<option value="3">March</option>' . "\n" .
194 '<option value="4">April</option>' . "\n" .
195 '<option value="5">May</option>' . "\n" .
196 '<option value="6">June</option>' . "\n" .
197 '<option value="7">July</option>' . "\n" .
198 '<option value="8">August</option>' . "\n" .
199 '<option value="9">September</option>' . "\n" .
200 '<option value="10">October</option>' . "\n" .
201 '<option value="11">November</option>' . "\n" .
202 '<option value="12">December</option></select>',
203 Xml::dateMenu( 2011, -1 ),
204 "Date menu with negative month for 'All'"
205 );
206 $this->assertEquals(
207 Xml::dateMenu( $curYear, $curMonth ),
208 Xml::dateMenu( '', $curMonth ),
209 "Date menu year is the current one when not specified"
210 );
211
212 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
213 $this->assertEquals(
214 Xml::dateMenu( $wantedYear, $nextMonth ),
215 Xml::dateMenu( '', $nextMonth ),
216 "Date menu next month is 11 months ago"
217 );
218
219 $this->assertEquals(
220 '<label for="year">From year (and earlier):</label> ' .
221 '<input id="year" maxlength="4" size="7" type="number" name="year"> ' .
222 '<label for="month">From month (and earlier):</label> ' .
223 '<select name="month" id="month" class="mw-month-selector">' .
224 '<option value="-1">all</option>' . "\n" .
225 '<option value="1">January</option>' . "\n" .
226 '<option value="2">February</option>' . "\n" .
227 '<option value="3">March</option>' . "\n" .
228 '<option value="4">April</option>' . "\n" .
229 '<option value="5">May</option>' . "\n" .
230 '<option value="6">June</option>' . "\n" .
231 '<option value="7">July</option>' . "\n" .
232 '<option value="8">August</option>' . "\n" .
233 '<option value="9">September</option>' . "\n" .
234 '<option value="10">October</option>' . "\n" .
235 '<option value="11">November</option>' . "\n" .
236 '<option value="12">December</option></select>',
237 Xml::dateMenu( '', '' ),
238 "Date menu with neither year or month"
239 );
240 }
241
242 public function testTextareaNoContent() {
243 $this->assertEquals(
244 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
245 Xml::textarea( 'name', '' ),
246 'textarea() with not content'
247 );
248 }
249
250 public function testTextareaAttribs() {
251 $this->assertEquals(
252 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
253 Xml::textarea( 'name', '<txt>', 20, 10 ),
254 'textarea() with custom attribs'
255 );
256 }
257
258 public function testLabelCreation() {
259 $this->assertEquals(
260 '<label for="id">name</label>',
261 Xml::label( 'name', 'id' ),
262 'label() with no attribs'
263 );
264 }
265
266 public function testLabelAttributeCanOnlyBeClassOrTitle() {
267 $this->assertEquals(
268 '<label for="id">name</label>',
269 Xml::label( 'name', 'id', [ 'generated' => true ] ),
270 'label() cannot be given a generated attribute'
271 );
272 $this->assertEquals(
273 '<label for="id" class="nice">name</label>',
274 Xml::label( 'name', 'id', [ 'class' => 'nice' ] ),
275 'label() can get a class attribute'
276 );
277 $this->assertEquals(
278 '<label for="id" title="nice tooltip">name</label>',
279 Xml::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
280 'label() can get a title attribute'
281 );
282 $this->assertEquals(
283 '<label for="id" class="nice" title="nice tooltip">name</label>',
284 Xml::label( 'name', 'id', [
285 'generated' => true,
286 'class' => 'nice',
287 'title' => 'nice tooltip',
288 'anotherattr' => 'value',
289 ]
290 ),
291 'label() skip all attributes but "class" and "title"'
292 );
293 }
294
295 public function testLanguageSelector() {
296 $this->hideDeprecated( 'MediaWiki\Xml\Xml::languageSelector' );
297
298 $select = Xml::languageSelector( 'en', true, null,
299 [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
300 $this->assertEquals(
301 '<label for="testlang">Language:</label>',
302 $select[0]
303 );
304 }
305
306 public function testListDropdown() {
307 $this->assertEquals(
308 '<select name="test-name" id="test-name" class="test-css" tabindex="2">' .
309 '<option value="other">other reasons</option>' . "\n" .
310 '<optgroup label="Foo">' .
311 '<option value="Foo 1">Foo 1</option>' . "\n" .
312 '<option value="Example" selected="">Example</option>' . "\n" .
313 '</optgroup>' . "\n" .
314 '<optgroup label="Bar">' .
315 '<option value="Bar 1">Bar 1</option>' . "\n" .
316 '</optgroup>' .
317 '</select>',
318 Xml::listDropdown(
319 // name
320 'test-name',
321 // source list
322 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
323 // other
324 'other reasons',
325 // selected
326 'Example',
327 // class
328 'test-css',
329 // tabindex
330 2
331 )
332 );
333 }
334
335 public function testListDropdownOptions() {
336 $this->assertEquals(
337 [
338 'other reasons' => 'other',
339 'Empty group item' => 'Empty group item',
340 'Foo' => [
341 'Foo 1' => 'Foo 1',
342 'Example' => 'Example',
343 ],
344 'Bar' => [
345 'Bar 1' => 'Bar 1',
346 ],
347 ],
348 Xml::listDropdownOptions(
349 "*\n** Empty group item\n* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
350 [ 'other' => 'other reasons' ]
351 )
352 );
353 }
354
355 public function testListDropdownOptionsOthers() {
356 // Do not use the value for 'other' as option group - T251351
357 $this->assertEquals(
358 [
359 'other reasons' => 'other',
360 'Foo 1' => 'Foo 1',
361 'Example' => 'Example',
362 'Bar' => [
363 'Bar 1' => 'Bar 1',
364 ],
365 ],
366 Xml::listDropdownOptions(
367 "* other reasons\n** Foo 1\n** Example\n* Bar\n** Bar 1",
368 [ 'other' => 'other reasons' ]
369 )
370 );
371 }
372
373 public function testListDropdownOptionsOoui() {
374 $this->assertEquals(
375 [
376 [ 'data' => 'other', 'label' => 'other reasons' ],
377 [ 'optgroup' => 'Foo' ],
378 [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
379 [ 'data' => 'Example', 'label' => 'Example' ],
380 [ 'optgroup' => 'Bar' ],
381 [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
382 ],
383 Xml::listDropdownOptionsOoui( [
384 'other reasons' => 'other',
385 'Foo' => [
386 'Foo 1' => 'Foo 1',
387 'Example' => 'Example',
388 ],
389 'Bar' => [
390 'Bar 1' => 'Bar 1',
391 ],
392 ] )
393 );
394 }
395
396 public static function provideFieldset() {
397 // $expect, [ $arg1, $arg2, ... ]
398 yield 'Opening tag' => [ "<fieldset>\n", [] ];
399 yield 'Opening tag (false means no legend)' => [ "<fieldset>\n", [ false ] ];
400 yield 'Opening tag (empty string means no legend)' => [ "<fieldset>\n", [ '' ] ];
401 yield 'Opening tag with legend' => [
402 "<fieldset>\n<legend>Foo</legend>\n",
403 [ 'Foo' ]
404 ];
405 yield 'Entire element with legend' => [
406 "<fieldset>\n<legend>Foo</legend>\nBar\n</fieldset>\n",
407 [ 'Foo', 'Bar' ]
408 ];
409 yield 'Opening tag with legend (false means no content and no closing tag)' => [
410 "<fieldset>\n<legend>Foo</legend>\n",
411 [ 'Foo', false ]
412 ];
413 yield 'Entire element with legend but no content (empty string generates a closing tag)' => [
414 "<fieldset>\n<legend>Foo</legend>\n\n</fieldset>\n",
415 [ 'Foo', '' ]
416 ];
417 yield 'Opening tag with legend and attributes' => [
418 "<fieldset class=\"bar\">\n<legend>Foo</legend>\nBar\n</fieldset>\n",
419 [ 'Foo', 'Bar', [ 'class' => 'bar' ] ]
420 ];
421 yield 'Entire element with legend and attributes' => [
422 "<fieldset class=\"bar\">\n<legend>Foo</legend>\n",
423 [ 'Foo', false, [ 'class' => 'bar' ] ]
424 ];
425 }
426
427 /**
428 * @dataProvider provideFieldset
429 */
430 public function testFieldset( string $expect, array $args ) {
431 $this->assertEquals(
432 $expect,
433 Xml::fieldset( ...$args )
434 );
435 }
436
437 public function testBuildTable() {
438 $firstRow = [ 'foo', 'bar' ];
439 $secondRow = [ 'Berlin', 'Tehran' ];
440 $headers = [ 'header1', 'header2' ];
441 $expected = '<table id="testTable"><thead id="testTable"><th>header1</th>' .
442 '<th>header2</th></thead><tr><td>foo</td><td>bar</td></tr><tr><td>Berlin</td>' .
443 '<td>Tehran</td></tr></table>';
444 $this->assertEquals(
445 $expected,
446 Xml::buildTable(
447 [ $firstRow, $secondRow ],
448 [ 'id' => 'testTable' ],
449 $headers
450 )
451 );
452 }
453
454 public function testBuildTableRow() {
455 $this->assertEquals(
456 '<tr id="testRow"><td>foo</td><td>bar</td></tr>',
457 Xml::buildTableRow( [ 'id' => 'testRow' ], [ 'foo', 'bar' ] )
458 );
459 }
460 }