MediaWiki  1.30.0
XmlTest.php
Go to the documentation of this file.
1 <?php
2 
6 class XmlTest extends MediaWikiTestCase {
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 
40  public function testExpandAttributes() {
41  $this->assertNull( Xml::expandAttributes( null ),
42  'Converting a null list of attributes'
43  );
44  $this->assertEquals( '', Xml::expandAttributes( [] ),
45  'Converting an empty list of attributes'
46  );
47  }
48 
52  public function testExpandAttributesException() {
53  $this->setExpectedException( 'MWException' );
54  Xml::expandAttributes( 'string' );
55  }
56 
60  public function testElementOpen() {
61  $this->assertEquals(
62  '<element>',
63  Xml::element( 'element', null, null ),
64  'Opening element with no attributes'
65  );
66  }
67 
71  public function testElementEmpty() {
72  $this->assertEquals(
73  '<element />',
74  Xml::element( 'element', null, '' ),
75  'Terminated empty element'
76  );
77  }
78 
83  $this->assertEquals(
84  '<input name="name" value="0" />',
85  Xml::input( 'name', false, 0 ),
86  'Input with a value of 0 (T25797)'
87  );
88  }
89 
93  public function testElementEscaping() {
94  $this->assertEquals(
95  '<element>hello &lt;there&gt; you &amp; you</element>',
96  Xml::element( 'element', null, 'hello <there> you & you' ),
97  'Element with no attributes and content that needs escaping'
98  );
99  }
100 
104  public function testEscapeTagsOnly() {
105  $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
106  'replace " > and < with their HTML entitites'
107  );
108  }
109 
113  public function testElementAttributes() {
114  $this->assertEquals(
115  '<element key="value" <>="&lt;&gt;">',
116  Xml::element( 'element', [ 'key' => 'value', '<>' => '<>' ], null ),
117  'Element attributes, keys are not escaped'
118  );
119  }
120 
124  public function testOpenElement() {
125  $this->assertEquals(
126  '<element k="v">',
127  Xml::openElement( 'element', [ 'k' => 'v' ] ),
128  'openElement() shortcut'
129  );
130  }
131 
135  public function testCloseElement() {
136  $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
137  }
138 
142  public function testDateMenu() {
143  $curYear = intval( gmdate( 'Y' ) );
144  $prevYear = $curYear - 1;
145 
146  $curMonth = intval( gmdate( 'n' ) );
147 
148  $nextMonth = $curMonth + 1;
149  if ( $nextMonth == 13 ) {
150  $nextMonth = 1;
151  }
152 
153  $this->assertEquals(
154  '<label for="year">From year (and earlier):</label> ' .
155  '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
156  '<label for="month">From month (and earlier):</label> ' .
157  '<select name="month" id="month" class="mw-month-selector">' .
158  '<option value="-1">all</option>' . "\n" .
159  '<option value="1">January</option>' . "\n" .
160  '<option value="2" selected="">February</option>' . "\n" .
161  '<option value="3">March</option>' . "\n" .
162  '<option value="4">April</option>' . "\n" .
163  '<option value="5">May</option>' . "\n" .
164  '<option value="6">June</option>' . "\n" .
165  '<option value="7">July</option>' . "\n" .
166  '<option value="8">August</option>' . "\n" .
167  '<option value="9">September</option>' . "\n" .
168  '<option value="10">October</option>' . "\n" .
169  '<option value="11">November</option>' . "\n" .
170  '<option value="12">December</option></select>',
171  Xml::dateMenu( 2011, 02 ),
172  "Date menu for february 2011"
173  );
174  $this->assertEquals(
175  '<label for="year">From year (and earlier):</label> ' .
176  '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> ' .
177  '<label for="month">From month (and earlier):</label> ' .
178  '<select name="month" id="month" class="mw-month-selector">' .
179  '<option value="-1">all</option>' . "\n" .
180  '<option value="1">January</option>' . "\n" .
181  '<option value="2">February</option>' . "\n" .
182  '<option value="3">March</option>' . "\n" .
183  '<option value="4">April</option>' . "\n" .
184  '<option value="5">May</option>' . "\n" .
185  '<option value="6">June</option>' . "\n" .
186  '<option value="7">July</option>' . "\n" .
187  '<option value="8">August</option>' . "\n" .
188  '<option value="9">September</option>' . "\n" .
189  '<option value="10">October</option>' . "\n" .
190  '<option value="11">November</option>' . "\n" .
191  '<option value="12">December</option></select>',
192  Xml::dateMenu( 2011, -1 ),
193  "Date menu with negative month for 'All'"
194  );
195  $this->assertEquals(
196  Xml::dateMenu( $curYear, $curMonth ),
197  Xml::dateMenu( '', $curMonth ),
198  "Date menu year is the current one when not specified"
199  );
200 
201  $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
202  $this->assertEquals(
203  Xml::dateMenu( $wantedYear, $nextMonth ),
204  Xml::dateMenu( '', $nextMonth ),
205  "Date menu next month is 11 months ago"
206  );
207 
208  $this->assertEquals(
209  '<label for="year">From year (and earlier):</label> ' .
210  '<input id="year" maxlength="4" size="7" type="number" name="year"/> ' .
211  '<label for="month">From month (and earlier):</label> ' .
212  '<select name="month" id="month" class="mw-month-selector">' .
213  '<option value="-1">all</option>' . "\n" .
214  '<option value="1">January</option>' . "\n" .
215  '<option value="2">February</option>' . "\n" .
216  '<option value="3">March</option>' . "\n" .
217  '<option value="4">April</option>' . "\n" .
218  '<option value="5">May</option>' . "\n" .
219  '<option value="6">June</option>' . "\n" .
220  '<option value="7">July</option>' . "\n" .
221  '<option value="8">August</option>' . "\n" .
222  '<option value="9">September</option>' . "\n" .
223  '<option value="10">October</option>' . "\n" .
224  '<option value="11">November</option>' . "\n" .
225  '<option value="12">December</option></select>',
226  Xml::dateMenu( '', '' ),
227  "Date menu with neither year or month"
228  );
229  }
230 
234  public function testTextareaNoContent() {
235  $this->assertEquals(
236  '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
237  Xml::textarea( 'name', '' ),
238  'textarea() with not content'
239  );
240  }
241 
245  public function testTextareaAttribs() {
246  $this->assertEquals(
247  '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
248  Xml::textarea( 'name', '<txt>', 20, 10 ),
249  'textarea() with custom attribs'
250  );
251  }
252 
256  public function testLabelCreation() {
257  $this->assertEquals(
258  '<label for="id">name</label>',
259  Xml::label( 'name', 'id' ),
260  'label() with no attribs'
261  );
262  }
263 
268  $this->assertEquals(
269  '<label for="id">name</label>',
270  Xml::label( 'name', 'id', [ 'generated' => true ] ),
271  'label() can not be given a generated attribute'
272  );
273  $this->assertEquals(
274  '<label for="id" class="nice">name</label>',
275  Xml::label( 'name', 'id', [ 'class' => 'nice' ] ),
276  'label() can get a class attribute'
277  );
278  $this->assertEquals(
279  '<label for="id" title="nice tooltip">name</label>',
280  Xml::label( 'name', 'id', [ 'title' => 'nice tooltip' ] ),
281  'label() can get a title attribute'
282  );
283  $this->assertEquals(
284  '<label for="id" class="nice" title="nice tooltip">name</label>',
285  Xml::label( 'name', 'id', [
286  'generated' => true,
287  'class' => 'nice',
288  'title' => 'nice tooltip',
289  'anotherattr' => 'value',
290  ]
291  ),
292  'label() skip all attributes but "class" and "title"'
293  );
294  }
295 
299  public function testLanguageSelector() {
300  $select = Xml::languageSelector( 'en', true, null,
301  [ 'id' => 'testlang' ], wfMessage( 'yourlanguage' ) );
302  $this->assertEquals(
303  '<label for="testlang">Language:</label>',
304  $select[0]
305  );
306  }
307 
311  public function testEncodeJsVarBoolean() {
312  $this->assertEquals(
313  'true',
314  Xml::encodeJsVar( true ),
315  'encodeJsVar() with boolean'
316  );
317  }
318 
322  public function testEncodeJsVarNull() {
323  $this->assertEquals(
324  'null',
325  Xml::encodeJsVar( null ),
326  'encodeJsVar() with null'
327  );
328  }
329 
333  public function testEncodeJsVarArray() {
334  $this->assertEquals(
335  '["a",1]',
336  Xml::encodeJsVar( [ 'a', 1 ] ),
337  'encodeJsVar() with array'
338  );
339  $this->assertEquals(
340  '{"a":"a","b":1}',
341  Xml::encodeJsVar( [ 'a' => 'a', 'b' => 1 ] ),
342  'encodeJsVar() with associative array'
343  );
344  }
345 
349  public function testEncodeJsVarObject() {
350  $this->assertEquals(
351  '{"a":"a","b":1}',
352  Xml::encodeJsVar( (object)[ 'a' => 'a', 'b' => 1 ] ),
353  'encodeJsVar() with object'
354  );
355  }
356 
360  public function testEncodeJsVarInt() {
361  $this->assertEquals(
362  '123456',
363  Xml::encodeJsVar( 123456 ),
364  'encodeJsVar() with int'
365  );
366  }
367 
371  public function testEncodeJsVarFloat() {
372  $this->assertEquals(
373  '1.23456',
374  Xml::encodeJsVar( 1.23456 ),
375  'encodeJsVar() with float'
376  );
377  }
378 
382  public function testEncodeJsVarIntString() {
383  $this->assertEquals(
384  '"123456"',
385  Xml::encodeJsVar( '123456' ),
386  'encodeJsVar() with int-like string'
387  );
388  }
389 
393  public function testEncodeJsVarFloatString() {
394  $this->assertEquals(
395  '"1.23456"',
396  Xml::encodeJsVar( '1.23456' ),
397  'encodeJsVar() with float-like string'
398  );
399  }
400 
404  public function testListDropDown() {
405  $this->assertEquals(
406  '<select name="test-name" id="test-name" class="test-css" tabindex="2">' .
407  '<option value="other">other reasons</option>' . "\n" .
408  '<optgroup label="Foo">' .
409  '<option value="Foo 1">Foo 1</option>' . "\n" .
410  '<option value="Example" selected="">Example</option>' . "\n" .
411  '</optgroup>' . "\n" .
412  '<optgroup label="Bar">' .
413  '<option value="Bar 1">Bar 1</option>' . "\n" .
414  '</optgroup>' .
415  '</select>',
417  // name
418  'test-name',
419  // source list
420  "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
421  // other
422  'other reasons',
423  // selected
424  'Example',
425  // class
426  'test-css',
427  // tabindex
428  2
429  )
430  );
431  }
432 
436  public function testListDropDownOptions() {
437  $this->assertEquals(
438  [
439  'other reasons' => 'other',
440  'Foo' => [
441  'Foo 1' => 'Foo 1',
442  'Example' => 'Example',
443  ],
444  'Bar' => [
445  'Bar 1' => 'Bar 1',
446  ],
447  ],
449  "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
450  [ 'other' => 'other reasons' ]
451  )
452  );
453  }
454 
458  public function testListDropDownOptionsOoui() {
459  $this->assertEquals(
460  [
461  [ 'data' => 'other', 'label' => 'other reasons' ],
462  [ 'optgroup' => 'Foo' ],
463  [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
464  [ 'data' => 'Example', 'label' => 'Example' ],
465  [ 'optgroup' => 'Bar' ],
466  [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
467  ],
469  'other reasons' => 'other',
470  'Foo' => [
471  'Foo 1' => 'Foo 1',
472  'Example' => 'Example',
473  ],
474  'Bar' => [
475  'Bar 1' => 'Bar 1',
476  ],
477  ] )
478  );
479  }
480 
484  public function testFieldset() {
485  $this->assertEquals(
486  "<fieldset>\n",
487  Xml::fieldset(),
488  'Opening tag'
489  );
490  $this->assertEquals(
491  "<fieldset>\n",
492  Xml::fieldset( false ),
493  'Opening tag (false means no legend)'
494  );
495  $this->assertEquals(
496  "<fieldset>\n",
497  Xml::fieldset( '' ),
498  'Opening tag (empty string also means no legend)'
499  );
500  $this->assertEquals(
501  "<fieldset>\n<legend>Foo</legend>\n",
502  Xml::fieldset( 'Foo' ),
503  'Opening tag with legend'
504  );
505  $this->assertEquals(
506  "<fieldset>\n<legend>Foo</legend>\nBar\n</fieldset>\n",
507  Xml::fieldset( 'Foo', 'Bar' ),
508  'Entire element with legend'
509  );
510  $this->assertEquals(
511  "<fieldset>\n<legend>Foo</legend>\n",
512  Xml::fieldset( 'Foo', false ),
513  'Opening tag with legend (false means no content and no closing tag)'
514  );
515  $this->assertEquals(
516  "<fieldset>\n<legend>Foo</legend>\n\n</fieldset>\n",
517  Xml::fieldset( 'Foo', '' ),
518  'Entire element with legend but no content (empty string generates a closing tag)'
519  );
520  $this->assertEquals(
521  "<fieldset class=\"bar\">\n<legend>Foo</legend>\nBar\n</fieldset>\n",
522  Xml::fieldset( 'Foo', 'Bar', [ 'class' => 'bar' ] ),
523  'Opening tag with legend and attributes'
524  );
525  $this->assertEquals(
526  "<fieldset class=\"bar\">\n<legend>Foo</legend>\n",
527  Xml::fieldset( 'Foo', false, [ 'class' => 'bar' ] ),
528  'Entire element with legend and attributes'
529  );
530  }
531 }
XmlTest\testElementInputCanHaveAValueOfZero
testElementInputCanHaveAValueOfZero()
Xml::input.
Definition: XmlTest.php:82
XmlTest\testEncodeJsVarBoolean
testEncodeJsVarBoolean()
Xml::encodeJsVar.
Definition: XmlTest.php:311
XmlTest\testLabelAttributeCanOnlyBeClassOrTitle
testLabelAttributeCanOnlyBeClassOrTitle()
Xml::label.
Definition: XmlTest.php:267
Xml\expandAttributes
static expandAttributes( $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition: Xml.php:67
XmlTest\testCloseElement
testCloseElement()
Xml::closeElement.
Definition: XmlTest.php:135
XmlTest\testEncodeJsVarFloatString
testEncodeJsVarFloatString()
Xml::encodeJsVar.
Definition: XmlTest.php:393
XmlTest\testEncodeJsVarInt
testEncodeJsVarInt()
Xml::encodeJsVar.
Definition: XmlTest.php:360
Xml\listDropDownOptionsOoui
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition: Xml.php:581
Xml\label
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition: Xml.php:358
XmlTest\testListDropDownOptionsOoui
testListDropDownOptionsOoui()
Xml::listDropDownOptionsOoui.
Definition: XmlTest.php:458
XmlTest\testTextareaNoContent
testTextareaNoContent()
Xml::textarea.
Definition: XmlTest.php:234
Xml\textarea
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition: Xml.php:635
XmlTest\testLabelCreation
testLabelCreation()
Xml::label.
Definition: XmlTest.php:256
XmlTest\testTextareaAttribs
testTextareaAttribs()
Xml::textarea.
Definition: XmlTest.php:245
XmlTest\testListDropDown
testListDropDown()
Xml::listDropDown.
Definition: XmlTest.php:404
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
Xml\encodeJsVar
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition: Xml.php:659
Xml\languageSelector
static languageSelector( $selected, $customisedOnly=true, $inLanguage=null, $overrideAttrs=[], Message $msg=null)
Construct a language selector appropriate for use in a form or preferences.
Definition: Xml.php:204
php
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:35
XmlTest\testExpandAttributesException
testExpandAttributesException()
Xml::expandAttributes.
Definition: XmlTest.php:52
Xml\fieldset
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition: Xml.php:609
XmlTest\testExpandAttributes
testExpandAttributes()
Xml::expandAttributes.
Definition: XmlTest.php:40
XmlTest\testElementEscaping
testElementEscaping()
Xml::element.
Definition: XmlTest.php:93
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
XmlTest\testListDropDownOptions
testListDropDownOptions()
Xml::listDropDownOptions.
Definition: XmlTest.php:436
XmlTest\testLanguageSelector
testLanguageSelector()
Xml::languageSelector.
Definition: XmlTest.php:299
XmlTest\testOpenElement
testOpenElement()
Xml::openElement.
Definition: XmlTest.php:124
XmlTest\testElementOpen
testElementOpen()
Xml::element.
Definition: XmlTest.php:60
XmlTest\testEncodeJsVarFloat
testEncodeJsVarFloat()
Xml::encodeJsVar.
Definition: XmlTest.php:371
XmlTest
Xml.
Definition: XmlTest.php:6
XmlTest\testElementAttributes
testElementAttributes()
Xml::element.
Definition: XmlTest.php:113
XmlTest\testEncodeJsVarIntString
testEncodeJsVarIntString()
Xml::encodeJsVar.
Definition: XmlTest.php:382
XmlTest\setUp
setUp()
Definition: XmlTest.php:8
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:118
Xml\listDropDown
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
Definition: Xml.php:508
Xml\dateMenu
static dateMenu( $year, $month)
Definition: Xml.php:167
XmlTest\testDateMenu
testDateMenu()
Xml::dateMenu.
Definition: XmlTest.php:142
XmlTest\testEscapeTagsOnly
testEscapeTagsOnly()
Xml::escapeTagsOnly.
Definition: XmlTest.php:104
XmlTest\testElementEmpty
testElementEmpty()
Xml::element.
Definition: XmlTest.php:71
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
wfMessage
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 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
Xml\input
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition: Xml.php:274
XmlTest\testFieldset
testFieldset()
Xml::fieldset.
Definition: XmlTest.php:484
XmlTest\testEncodeJsVarNull
testEncodeJsVarNull()
Xml::encodeJsVar.
Definition: XmlTest.php:322
Xml\escapeTagsOnly
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition: Xml.php:747
XmlTest\testEncodeJsVarArray
testEncodeJsVarArray()
Xml::encodeJsVar.
Definition: XmlTest.php:333
Xml\listDropDownOptions
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
Definition: Xml.php:539
XmlTest\testEncodeJsVarObject
testEncodeJsVarObject()
Xml::encodeJsVar.
Definition: XmlTest.php:349