MediaWiki REL1_33
XmlTest.php
Go to the documentation of this file.
1<?php
2
7
8 protected function setUp() {
9 parent::setUp();
10
11 $langObj = Language::factory( 'en' );
12 $langObj->setNamespaces( [
13 -2 => 'Media',
14 -1 => 'Special',
15 0 => '',
16 1 => 'Talk',
17 2 => 'User',
18 3 => 'User_talk',
19 4 => 'MyWiki',
20 5 => 'MyWiki_Talk',
21 6 => 'File',
22 7 => 'File_talk',
23 8 => 'MediaWiki',
24 9 => 'MediaWiki_talk',
25 10 => 'Template',
26 11 => 'Template_talk',
27 100 => 'Custom',
28 101 => 'Custom_talk',
29 ] );
30
31 $this->setMwGlobals( [
32 'wgLang' => $langObj,
33 'wgUseMediaWikiUIEverywhere' => false,
34 ] );
35 }
36
37 protected function tearDown() {
38 Language::factory( 'en' )->resetNamespaces();
39 parent::tearDown();
40 }
41
45 public function testExpandAttributes() {
46 $this->assertNull( Xml::expandAttributes( null ),
47 'Converting a null list of attributes'
48 );
49 $this->assertEquals( '', Xml::expandAttributes( [] ),
50 'Converting an empty list of attributes'
51 );
52 }
53
58 $this->setExpectedException( MWException::class );
59 Xml::expandAttributes( 'string' );
60 }
61
65 public function testElementOpen() {
66 $this->assertEquals(
67 '<element>',
68 Xml::element( 'element', null, null ),
69 'Opening element with no attributes'
70 );
71 }
72
76 public function testElementEmpty() {
77 $this->assertEquals(
78 '<element />',
79 Xml::element( 'element', null, '' ),
80 'Terminated empty element'
81 );
82 }
83
88 $this->assertEquals(
89 '<input name="name" value="0" />',
90 Xml::input( 'name', false, 0 ),
91 'Input with a value of 0 (T25797)'
92 );
93 }
94
98 public function testElementEscaping() {
99 $this->assertEquals(
100 '<element>"hello &lt;there&gt; your\'s &amp; you"</element>',
101 Xml::element( 'element', null, '"hello <there> your\'s & you"' ),
102 'Element with no attributes and content that needs escaping'
103 );
104 }
105
109 public function testEscapeTagsOnly() {
110 $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
111 'replace " > and < with their HTML entitites'
112 );
113 }
114
118 public function testElementAttributes() {
119 $this->assertEquals(
120 '<element key="value" <>="&lt;&gt;">',
121 Xml::element( 'element', [ 'key' => 'value', '<>' => '<>' ], null ),
122 'Element attributes, keys are not escaped'
123 );
124 }
125
129 public function testOpenElement() {
130 $this->assertEquals(
131 '<element k="v">',
132 Xml::openElement( 'element', [ 'k' => 'v' ] ),
133 'openElement() shortcut'
134 );
135 }
136
140 public function testCloseElement() {
141 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
142 }
143
144 public function provideMonthSelector() {
145 global $wgLang;
146
147 $header = '<select name="month" id="month" class="mw-month-selector">';
148 $header2 = '<select name="month" id="monthSelector" class="mw-month-selector">';
149 $monthsString = '';
150 for ( $i = 1; $i < 13; $i++ ) {
151 $monthName = $wgLang->getMonthName( $i );
152 $monthsString .= "<option value=\"{$i}\">{$monthName}</option>";
153 if ( $i !== 12 ) {
154 $monthsString .= "\n";
155 }
156 }
157 $monthsString2 = str_replace(
158 '<option value="12">December</option>',
159 '<option value="12" selected="">December</option>',
160 $monthsString
161 );
162 $end = '</select>';
163
164 $allMonths = "<option value=\"AllMonths\">all</option>\n";
165 return [
166 [ $header . $monthsString . $end, '', null, 'month' ],
167 [ $header . $monthsString2 . $end, 12, null, 'month' ],
168 [ $header2 . $monthsString . $end, '', null, 'monthSelector' ],
169 [ $header . $allMonths . $monthsString . $end, '', 'AllMonths', 'month' ],
170
171 ];
172 }
173
178 public function testMonthSelector( $expected, $selected, $allmonths, $id ) {
179 $this->assertEquals(
180 $expected,
181 Xml::monthSelector( $selected, $allmonths, $id )
182 );
183 }
184
188 public function testSpan() {
189 $this->assertEquals(
190 '<span class="foo" id="testSpan">element</span>',
191 Xml::span( 'element', 'foo', [ 'id' => 'testSpan' ] )
192 );
193 }
194
198 public function testDateMenu() {
199 $curYear = intval( gmdate( 'Y' ) );
200 $prevYear = $curYear - 1;
201
202 $curMonth = intval( gmdate( 'n' ) );
203
204 $nextMonth = $curMonth + 1;
205 if ( $nextMonth == 13 ) {
206 $nextMonth = 1;
207 }
208
209 $this->assertEquals(
210 '<label for="year">From year (and earlier):</label> ' .
211 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
212 '<label for="month">From month (and earlier):</label> ' .
213 '<select name="month" id="month" class="mw-month-selector">' .
214 '<option value="-1">all</option>' . "\n" .
215 '<option value="1">January</option>' . "\n" .
216 '<option value="2" selected="">February</option>' . "\n" .
217 '<option value="3">March</option>' . "\n" .
218 '<option value="4">April</option>' . "\n" .
219 '<option value="5">May</option>' . "\n" .
220 '<option value="6">June</option>' . "\n" .
221 '<option value="7">July</option>' . "\n" .
222 '<option value="8">August</option>' . "\n" .
223 '<option value="9">September</option>' . "\n" .
224 '<option value="10">October</option>' . "\n" .
225 '<option value="11">November</option>' . "\n" .
226 '<option value="12">December</option></select>',
227 Xml::dateMenu( 2011, 02 ),
228 "Date menu for february 2011"
229 );
230 $this->assertEquals(
231 '<label for="year">From year (and earlier):</label> ' .
232 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
233 '<label for="month">From month (and earlier):</label> ' .
234 '<select name="month" id="month" class="mw-month-selector">' .
235 '<option value="-1">all</option>' . "\n" .
236 '<option value="1">January</option>' . "\n" .
237 '<option value="2">February</option>' . "\n" .
238 '<option value="3">March</option>' . "\n" .
239 '<option value="4">April</option>' . "\n" .
240 '<option value="5">May</option>' . "\n" .
241 '<option value="6">June</option>' . "\n" .
242 '<option value="7">July</option>' . "\n" .
243 '<option value="8">August</option>' . "\n" .
244 '<option value="9">September</option>' . "\n" .
245 '<option value="10">October</option>' . "\n" .
246 '<option value="11">November</option>' . "\n" .
247 '<option value="12">December</option></select>',
248 Xml::dateMenu( 2011, -1 ),
249 "Date menu with negative month for 'All'"
250 );
251 $this->assertEquals(
252 Xml::dateMenu( $curYear, $curMonth ),
253 Xml::dateMenu( '', $curMonth ),
254 "Date menu year is the current one when not specified"
255 );
256
257 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
258 $this->assertEquals(
259 Xml::dateMenu( $wantedYear, $nextMonth ),
260 Xml::dateMenu( '', $nextMonth ),
261 "Date menu next month is 11 months ago"
262 );
263
264 $this->assertEquals(
265 '<label for="year">From year (and earlier):</label> ' .
266 '<input id="year" maxlength="4" size="7" type="number" name="year"/> ' .
267 '<label for="month">From month (and earlier):</label> ' .
268 '<select name="month" id="month" class="mw-month-selector">' .
269 '<option value="-1">all</option>' . "\n" .
270 '<option value="1">January</option>' . "\n" .
271 '<option value="2">February</option>' . "\n" .
272 '<option value="3">March</option>' . "\n" .
273 '<option value="4">April</option>' . "\n" .
274 '<option value="5">May</option>' . "\n" .
275 '<option value="6">June</option>' . "\n" .
276 '<option value="7">July</option>' . "\n" .
277 '<option value="8">August</option>' . "\n" .
278 '<option value="9">September</option>' . "\n" .
279 '<option value="10">October</option>' . "\n" .
280 '<option value="11">November</option>' . "\n" .
281 '<option value="12">December</option></select>',
282 Xml::dateMenu( '', '' ),
283 "Date menu with neither year or month"
284 );
285 }
286
290 public function testTextareaNoContent() {
291 $this->assertEquals(
292 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
293 Xml::textarea( 'name', '' ),
294 'textarea() with not content'
295 );
296 }
297
301 public function testTextareaAttribs() {
302 $this->assertEquals(
303 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
304 Xml::textarea( 'name', '<txt>', 20, 10 ),
305 'textarea() with custom attribs'
306 );
307 }
308
312 public function testLabelCreation() {
313 $this->assertEquals(
314 '<label for="id">name</label>',
315 Xml::label( 'name', 'id' ),
316 'label() with no attribs'
317 );
318 }
319
324 $this->assertEquals(
325 '<label for="id">name</label>',
326 Xml::label( 'name', 'id', [ 'generated' => true ] ),
327 'label() can not be given a generated attribute'
328 );
329 $this->assertEquals(
330 '<label for="id" class="nice">name</label>',
331 Xml::label( 'name', 'id', [ 'class' => 'nice' ] ),
332 'label() can get a class attribute'
333 );
334 $this->assertEquals(
335 '<label for="id" title="nice tooltip">name</label>',
336 Xml::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
337 'label() can get a title attribute'
338 );
339 $this->assertEquals(
340 '<label for="id" class="nice" title="nice tooltip">name</label>',
341 Xml::label( 'name', 'id', [
342 'generated' => true,
343 'class' => 'nice',
344 'title' => 'nice tooltip',
345 'anotherattr' => 'value',
346 ]
347 ),
348 'label() skip all attributes but "class" and "title"'
349 );
350 }
351
355 public function testLanguageSelector() {
356 $select = Xml::languageSelector( 'en', true, null,
357 [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
358 $this->assertEquals(
359 '<label for="testlang">Language:</label>',
360 $select[0]
361 );
362 }
363
367 public function testEncodeJsVarBoolean() {
368 $this->assertEquals(
369 'true',
370 Xml::encodeJsVar( true ),
371 'encodeJsVar() with boolean'
372 );
373 }
374
378 public function testEncodeJsVarNull() {
379 $this->assertEquals(
380 'null',
381 Xml::encodeJsVar( null ),
382 'encodeJsVar() with null'
383 );
384 }
385
389 public function testEncodeJsVarArray() {
390 $this->assertEquals(
391 '["a",1]',
392 Xml::encodeJsVar( [ 'a', 1 ] ),
393 'encodeJsVar() with array'
394 );
395 $this->assertEquals(
396 '{"a":"a","b":1}',
397 Xml::encodeJsVar( [ 'a' => 'a', 'b' => 1 ] ),
398 'encodeJsVar() with associative array'
399 );
400 }
401
405 public function testEncodeJsVarObject() {
406 $this->assertEquals(
407 '{"a":"a","b":1}',
408 Xml::encodeJsVar( (object)[ 'a' => 'a', 'b' => 1 ] ),
409 'encodeJsVar() with object'
410 );
411 }
412
416 public function testEncodeJsVarInt() {
417 $this->assertEquals(
418 '123456',
419 Xml::encodeJsVar( 123456 ),
420 'encodeJsVar() with int'
421 );
422 }
423
427 public function testEncodeJsVarFloat() {
428 $this->assertEquals(
429 '1.23456',
430 Xml::encodeJsVar( 1.23456 ),
431 'encodeJsVar() with float'
432 );
433 }
434
438 public function testEncodeJsVarIntString() {
439 $this->assertEquals(
440 '"123456"',
441 Xml::encodeJsVar( '123456' ),
442 'encodeJsVar() with int-like string'
443 );
444 }
445
449 public function testEncodeJsVarFloatString() {
450 $this->assertEquals(
451 '"1.23456"',
452 Xml::encodeJsVar( '1.23456' ),
453 'encodeJsVar() with float-like string'
454 );
455 }
456
460 public function testXmlJsCode() {
461 $code = 'function () { foo( 42 ); }';
462 $this->assertEquals(
463 $code,
464 Xml::encodeJsVar( new XmlJsCode( $code ) )
465 );
466 }
467
472 public function testEncodeObject() {
473 $codeA = 'function () { foo( 42 ); }';
474 $codeB = 'function ( jQuery ) { bar( 142857 ); }';
476 'a' => new XmlJsCode( $codeA ),
477 'b' => new XmlJsCode( $codeB )
478 ] );
479 $this->assertEquals(
480 "{\"a\":$codeA,\"b\":$codeB}",
481 Xml::encodeJsVar( $obj )
482 );
483 }
484
488 public function testListDropDown() {
489 $this->assertEquals(
490 '<select name="test-name" id="test-name" class="test-css" tabindex="2">' .
491 '<option value="other">other reasons</option>' . "\n" .
492 '<optgroup label="Foo">' .
493 '<option value="Foo 1">Foo 1</option>' . "\n" .
494 '<option value="Example" selected="">Example</option>' . "\n" .
495 '</optgroup>' . "\n" .
496 '<optgroup label="Bar">' .
497 '<option value="Bar 1">Bar 1</option>' . "\n" .
498 '</optgroup>' .
499 '</select>',
500 Xml::listDropDown(
501 // name
502 'test-name',
503 // source list
504 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
505 // other
506 'other reasons',
507 // selected
508 'Example',
509 // class
510 'test-css',
511 // tabindex
512 2
513 )
514 );
515 }
516
520 public function testListDropDownOptions() {
521 $this->assertEquals(
522 [
523 'other reasons' => 'other',
524 'Foo' => [
525 'Foo 1' => 'Foo 1',
526 'Example' => 'Example',
527 ],
528 'Bar' => [
529 'Bar 1' => 'Bar 1',
530 ],
531 ],
532 Xml::listDropDownOptions(
533 "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
534 [ 'other' => 'other reasons' ]
535 )
536 );
537 }
538
542 public function testListDropDownOptionsOoui() {
543 $this->assertEquals(
544 [
545 [ 'data' => 'other', 'label' => 'other reasons' ],
546 [ 'optgroup' => 'Foo' ],
547 [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
548 [ 'data' => 'Example', 'label' => 'Example' ],
549 [ 'optgroup' => 'Bar' ],
550 [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
551 ],
552 Xml::listDropDownOptionsOoui( [
553 'other reasons' => 'other',
554 'Foo' => [
555 'Foo 1' => 'Foo 1',
556 'Example' => 'Example',
557 ],
558 'Bar' => [
559 'Bar 1' => 'Bar 1',
560 ],
561 ] )
562 );
563 }
564
568 public function testFieldset() {
569 $this->assertEquals(
570 "<fieldset>\n",
571 Xml::fieldset(),
572 'Opening tag'
573 );
574 $this->assertEquals(
575 "<fieldset>\n",
576 Xml::fieldset( false ),
577 'Opening tag (false means no legend)'
578 );
579 $this->assertEquals(
580 "<fieldset>\n",
581 Xml::fieldset( '' ),
582 'Opening tag (empty string also means no legend)'
583 );
584 $this->assertEquals(
585 "<fieldset>\n<legend>Foo</legend>\n",
586 Xml::fieldset( 'Foo' ),
587 'Opening tag with legend'
588 );
589 $this->assertEquals(
590 "<fieldset>\n<legend>Foo</legend>\nBar\n</fieldset>\n",
591 Xml::fieldset( 'Foo', 'Bar' ),
592 'Entire element with legend'
593 );
594 $this->assertEquals(
595 "<fieldset>\n<legend>Foo</legend>\n",
596 Xml::fieldset( 'Foo', false ),
597 'Opening tag with legend (false means no content and no closing tag)'
598 );
599 $this->assertEquals(
600 "<fieldset>\n<legend>Foo</legend>\n\n</fieldset>\n",
601 Xml::fieldset( 'Foo', '' ),
602 'Entire element with legend but no content (empty string generates a closing tag)'
603 );
604 $this->assertEquals(
605 "<fieldset class=\"bar\">\n<legend>Foo</legend>\nBar\n</fieldset>\n",
606 Xml::fieldset( 'Foo', 'Bar', [ 'class' => 'bar' ] ),
607 'Opening tag with legend and attributes'
608 );
609 $this->assertEquals(
610 "<fieldset class=\"bar\">\n<legend>Foo</legend>\n",
611 Xml::fieldset( 'Foo', false, [ 'class' => 'bar' ] ),
612 'Entire element with legend and attributes'
613 );
614 }
615
619 public function testBuildTable() {
620 $firstRow = [ 'foo', 'bar' ];
621 $secondRow = [ 'Berlin', 'Tehran' ];
622 $headers = [ 'header1', 'header2' ];
623 $expected = '<table id="testTable"><thead id="testTable"><th>header1</th>' .
624 '<th>header2</th></thead><tr><td>foo</td><td>bar</td></tr><tr><td>Berlin</td>' .
625 '<td>Tehran</td></tr></table>';
626 $this->assertEquals(
627 $expected,
628 Xml::buildTable(
629 [ $firstRow, $secondRow ],
630 [ 'id' => 'testTable' ],
631 $headers
632 )
633 );
634 }
635
639 public function testBuildTableRow() {
640 $this->assertEquals(
641 '<tr id="testRow"><td>foo</td><td>bar</td></tr>',
642 Xml::buildTableRow( [ 'id' => 'testRow' ], [ 'foo', 'bar' ] )
643 );
644 }
645}
$wgLang
Definition Setup.php:875
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
Definition XmlJsCode.php:40
static encodeObject( $obj, $pretty=false)
Encode an object containing XmlJsCode objects.
Definition XmlJsCode.php:58
Xml.
Definition XmlTest.php:6
testEncodeJsVarIntString()
Xml::encodeJsVar.
Definition XmlTest.php:438
testDateMenu()
Xml::dateMenu.
Definition XmlTest.php:198
testCloseElement()
Xml::closeElement.
Definition XmlTest.php:140
testTextareaNoContent()
Xml::textarea.
Definition XmlTest.php:290
testLanguageSelector()
Xml::languageSelector.
Definition XmlTest.php:355
testExpandAttributes()
Xml::expandAttributes.
Definition XmlTest.php:45
testBuildTableRow()
Xml::buildTableRow.
Definition XmlTest.php:639
testEncodeJsVarArray()
Xml::encodeJsVar.
Definition XmlTest.php:389
testLabelAttributeCanOnlyBeClassOrTitle()
Xml::label.
Definition XmlTest.php:323
testEncodeJsVarObject()
Xml::encodeJsVar.
Definition XmlTest.php:405
testXmlJsCode()
Xml::encodeJsVar.
Definition XmlTest.php:460
setUp()
Definition XmlTest.php:8
testEncodeJsVarFloat()
Xml::encodeJsVar.
Definition XmlTest.php:427
testFieldset()
Xml::fieldset.
Definition XmlTest.php:568
testListDropDownOptions()
Xml::listDropDownOptions.
Definition XmlTest.php:520
testTextareaAttribs()
Xml::textarea.
Definition XmlTest.php:301
testSpan()
Xml::span.
Definition XmlTest.php:188
testEncodeJsVarBoolean()
Xml::encodeJsVar.
Definition XmlTest.php:367
testListDropDownOptionsOoui()
Xml::listDropDownOptionsOoui.
Definition XmlTest.php:542
testMonthSelector( $expected, $selected, $allmonths, $id)
Xml::monthSelector provideMonthSelector.
Definition XmlTest.php:178
testExpandAttributesException()
Xml::expandAttributes.
Definition XmlTest.php:57
testElementInputCanHaveAValueOfZero()
Xml::input.
Definition XmlTest.php:87
testLabelCreation()
Xml::label.
Definition XmlTest.php:312
testListDropDown()
Xml::listDropDown.
Definition XmlTest.php:488
testEncodeObject()
Xml::encodeJsVar XmlJsCode::encodeObject.
Definition XmlTest.php:472
testEncodeJsVarInt()
Xml::encodeJsVar.
Definition XmlTest.php:416
testBuildTable()
Xml::buildTable.
Definition XmlTest.php:619
testElementOpen()
Xml::element.
Definition XmlTest.php:65
testEscapeTagsOnly()
Xml::escapeTagsOnly.
Definition XmlTest.php:109
testOpenElement()
Xml::openElement.
Definition XmlTest.php:129
testEncodeJsVarFloatString()
Xml::encodeJsVar.
Definition XmlTest.php:449
testElementEscaping()
Xml::element.
Definition XmlTest.php:98
testEncodeJsVarNull()
Xml::encodeJsVar.
Definition XmlTest.php:378
testElementAttributes()
Xml::element.
Definition XmlTest.php:118
tearDown()
Definition XmlTest.php:37
testElementEmpty()
Xml::element.
Definition XmlTest.php:76
provideMonthSelector()
Definition XmlTest.php:144
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:856
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$header