MediaWiki  1.23.15
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( array(
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( array(
32  'wgLang' => $langObj,
33  ) );
34  }
35 
39  public function testExpandAttributes() {
40  $this->assertNull( Xml::expandAttributes( null ),
41  'Converting a null list of attributes'
42  );
43  $this->assertEquals( '', Xml::expandAttributes( array() ),
44  'Converting an empty list of attributes'
45  );
46  }
47 
51  public function testExpandAttributesException() {
52  $this->setExpectedException( 'MWException' );
53  Xml::expandAttributes( 'string' );
54  }
55 
59  public function testElementOpen() {
60  $this->assertEquals(
61  '<element>',
62  Xml::element( 'element', null, null ),
63  'Opening element with no attributes'
64  );
65  }
66 
70  public function testElementEmpty() {
71  $this->assertEquals(
72  '<element />',
73  Xml::element( 'element', null, '' ),
74  'Terminated empty element'
75  );
76  }
77 
82  $this->assertEquals(
83  '<input name="name" value="0" />',
84  Xml::input( 'name', false, 0 ),
85  'Input with a value of 0 (bug 23797)'
86  );
87  }
88 
92  public function testElementEscaping() {
93  $this->assertEquals(
94  '<element>hello &lt;there&gt; you &amp; you</element>',
95  Xml::element( 'element', null, 'hello <there> you & you' ),
96  'Element with no attributes and content that needs escaping'
97  );
98  }
99 
103  public function testEscapeTagsOnly() {
104  $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
105  'replace " > and < with their HTML entitites'
106  );
107  }
108 
112  public function testElementAttributes() {
113  $this->assertEquals(
114  '<element key="value" <>="&lt;&gt;">',
115  Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
116  'Element attributes, keys are not escaped'
117  );
118  }
119 
123  public function testOpenElement() {
124  $this->assertEquals(
125  '<element k="v">',
126  Xml::openElement( 'element', array( 'k' => 'v' ) ),
127  'openElement() shortcut'
128  );
129  }
130 
134  public function testCloseElement() {
135  $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
136  }
137 
141  public function testDateMenu() {
142  $curYear = intval( gmdate( 'Y' ) );
143  $prevYear = $curYear - 1;
144 
145  $curMonth = intval( gmdate( 'n' ) );
146 
147  $nextMonth = $curMonth + 1;
148  if ( $nextMonth == 13 ) {
149  $nextMonth = 1;
150  }
151 
152  $this->assertEquals(
153  '<label for="year">From year (and earlier):</label> <input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
154  '<option value="1">January</option>' . "\n" .
155  '<option value="2" selected="">February</option>' . "\n" .
156  '<option value="3">March</option>' . "\n" .
157  '<option value="4">April</option>' . "\n" .
158  '<option value="5">May</option>' . "\n" .
159  '<option value="6">June</option>' . "\n" .
160  '<option value="7">July</option>' . "\n" .
161  '<option value="8">August</option>' . "\n" .
162  '<option value="9">September</option>' . "\n" .
163  '<option value="10">October</option>' . "\n" .
164  '<option value="11">November</option>' . "\n" .
165  '<option value="12">December</option></select>',
166  Xml::dateMenu( 2011, 02 ),
167  "Date menu for february 2011"
168  );
169  $this->assertEquals(
170  '<label for="year">From year (and earlier):</label> <input id="year" maxlength="4" size="7" type="number" value="2011" name="year"/> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
171  '<option value="1">January</option>' . "\n" .
172  '<option value="2">February</option>' . "\n" .
173  '<option value="3">March</option>' . "\n" .
174  '<option value="4">April</option>' . "\n" .
175  '<option value="5">May</option>' . "\n" .
176  '<option value="6">June</option>' . "\n" .
177  '<option value="7">July</option>' . "\n" .
178  '<option value="8">August</option>' . "\n" .
179  '<option value="9">September</option>' . "\n" .
180  '<option value="10">October</option>' . "\n" .
181  '<option value="11">November</option>' . "\n" .
182  '<option value="12">December</option></select>',
183  Xml::dateMenu( 2011, -1 ),
184  "Date menu with negative month for 'All'"
185  );
186  $this->assertEquals(
187  Xml::dateMenu( $curYear, $curMonth ),
188  Xml::dateMenu( '', $curMonth ),
189  "Date menu year is the current one when not specified"
190  );
191 
192  $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
193  $this->assertEquals(
194  Xml::dateMenu( $wantedYear, $nextMonth ),
195  Xml::dateMenu( '', $nextMonth ),
196  "Date menu next month is 11 months ago"
197  );
198 
199  $this->assertEquals(
200  '<label for="year">From year (and earlier):</label> <input id="year" maxlength="4" size="7" type="number" name="year"/> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" .
201  '<option value="1">January</option>' . "\n" .
202  '<option value="2">February</option>' . "\n" .
203  '<option value="3">March</option>' . "\n" .
204  '<option value="4">April</option>' . "\n" .
205  '<option value="5">May</option>' . "\n" .
206  '<option value="6">June</option>' . "\n" .
207  '<option value="7">July</option>' . "\n" .
208  '<option value="8">August</option>' . "\n" .
209  '<option value="9">September</option>' . "\n" .
210  '<option value="10">October</option>' . "\n" .
211  '<option value="11">November</option>' . "\n" .
212  '<option value="12">December</option></select>',
213  Xml::dateMenu( '', '' ),
214  "Date menu with neither year or month"
215  );
216  }
217 
221  public function testTextareaNoContent() {
222  $this->assertEquals(
223  '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
224  Xml::textarea( 'name', '' ),
225  'textarea() with not content'
226  );
227  }
228 
232  public function testTextareaAttribs() {
233  $this->assertEquals(
234  '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
235  Xml::textarea( 'name', '<txt>', 20, 10 ),
236  'textarea() with custom attribs'
237  );
238  }
239 
243  public function testLabelCreation() {
244  $this->assertEquals(
245  '<label for="id">name</label>',
246  Xml::label( 'name', 'id' ),
247  'label() with no attribs'
248  );
249  }
250 
255  $this->assertEquals(
256  '<label for="id">name</label>',
257  Xml::label( 'name', 'id', array( 'generated' => true ) ),
258  'label() can not be given a generated attribute'
259  );
260  $this->assertEquals(
261  '<label for="id" class="nice">name</label>',
262  Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
263  'label() can get a class attribute'
264  );
265  $this->assertEquals(
266  '<label for="id" title="nice tooltip">name</label>',
267  Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
268  'label() can get a title attribute'
269  );
270  $this->assertEquals(
271  '<label for="id" class="nice" title="nice tooltip">name</label>',
272  Xml::label( 'name', 'id', array(
273  'generated' => true,
274  'class' => 'nice',
275  'title' => 'nice tooltip',
276  'anotherattr' => 'value',
277  )
278  ),
279  'label() skip all attributes but "class" and "title"'
280  );
281  }
282 
286  public function testLanguageSelector() {
287  $select = Xml::languageSelector( 'en', true, null,
288  array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
289  $this->assertEquals(
290  '<label for="testlang">Language:</label>',
291  $select[0]
292  );
293  }
294 
298  public function testEscapeJsStringSpecialChars() {
299  $this->assertEquals(
300  '\\\\\r\n',
301  Xml::escapeJsString( "\\\r\n" ),
302  'escapeJsString() with special characters'
303  );
304  }
305 
309  public function testEncodeJsVarBoolean() {
310  $this->assertEquals(
311  'true',
312  Xml::encodeJsVar( true ),
313  'encodeJsVar() with boolean'
314  );
315  }
316 
320  public function testEncodeJsVarNull() {
321  $this->assertEquals(
322  'null',
323  Xml::encodeJsVar( null ),
324  'encodeJsVar() with null'
325  );
326  }
327 
331  public function testEncodeJsVarArray() {
332  $this->assertEquals(
333  '["a",1]',
334  Xml::encodeJsVar( array( 'a', 1 ) ),
335  'encodeJsVar() with array'
336  );
337  $this->assertEquals(
338  '{"a":"a","b":1}',
339  Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
340  'encodeJsVar() with associative array'
341  );
342  }
343 
347  public function testEncodeJsVarObject() {
348  $this->assertEquals(
349  '{"a":"a","b":1}',
350  Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
351  'encodeJsVar() with object'
352  );
353  }
354 
358  public function testEncodeJsVarInt() {
359  $this->assertEquals(
360  '123456',
361  Xml::encodeJsVar( 123456 ),
362  'encodeJsVar() with int'
363  );
364  }
365 
369  public function testEncodeJsVarFloat() {
370  $this->assertEquals(
371  '1.23456',
372  Xml::encodeJsVar( 1.23456 ),
373  'encodeJsVar() with float'
374  );
375  }
376 
380  public function testEncodeJsVarIntString() {
381  $this->assertEquals(
382  '"123456"',
383  Xml::encodeJsVar( '123456' ),
384  'encodeJsVar() with int-like string'
385  );
386  }
387 
391  public function testEncodeJsVarFloatString() {
392  $this->assertEquals(
393  '"1.23456"',
394  Xml::encodeJsVar( '1.23456' ),
395  'encodeJsVar() with float-like string'
396  );
397  }
398 }
XmlTest\testElementInputCanHaveAValueOfZero
testElementInputCanHaveAValueOfZero()
@covers Xml::input
Definition: XmlTest.php:81
XmlTest\testEncodeJsVarBoolean
testEncodeJsVarBoolean()
@covers Xml::encodeJsVar
Definition: XmlTest.php:309
XmlTest\testLabelAttributeCanOnlyBeClassOrTitle
testLabelAttributeCanOnlyBeClassOrTitle()
@covers Xml::label
Definition: XmlTest.php:254
Xml\escapeJsString
static escapeJsString( $string)
Returns an escaped string suitable for inclusion in a string literal for JavaScript source code.
Definition: Xml.php:609
Xml\expandAttributes
static expandAttributes( $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition: Xml.php:65
XmlTest\testCloseElement
testCloseElement()
@covers Xml::closeElement
Definition: XmlTest.php:134
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
XmlTest\testEncodeJsVarFloatString
testEncodeJsVarFloatString()
@covers Xml::encodeJsVar
Definition: XmlTest.php:391
XmlTest\testEncodeJsVarInt
testEncodeJsVarInt()
@covers Xml::encodeJsVar
Definition: XmlTest.php:358
XmlTest\testTextareaNoContent
testTextareaNoContent()
@covers Xml::textarea
Definition: XmlTest.php:221
XmlTest\testLabelCreation
testLabelCreation()
@covers Xml::label
Definition: XmlTest.php:243
XmlTest\testEscapeJsStringSpecialChars
testEscapeJsStringSpecialChars()
@covers Xml::escapeJsString
Definition: XmlTest.php:298
XmlTest\testTextareaAttribs
testTextareaAttribs()
@covers Xml::textarea
Definition: XmlTest.php:232
Xml\languageSelector
static languageSelector( $selected, $customisedOnly=true, $inLanguage=null, $overrideAttrs=array(), Message $msg=null)
Construct a language selector appropriate for use in a form or preferences.
Definition: Xml.php:225
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:647
XmlTest\testExpandAttributesException
testExpandAttributesException()
@covers Xml::expandAttributes
Definition: XmlTest.php:51
XmlTest\testExpandAttributes
testExpandAttributes()
@covers Xml::expandAttributes
Definition: XmlTest.php:39
XmlTest\testElementEscaping
testElementEscaping()
@covers Xml::element
Definition: XmlTest.php:92
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables 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\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
XmlTest\testLanguageSelector
testLanguageSelector()
@covers Xml::languageSelector
Definition: XmlTest.php:286
XmlTest\testOpenElement
testOpenElement()
@covers Xml::openElement
Definition: XmlTest.php:123
XmlTest\testElementOpen
testElementOpen()
@covers Xml::element
Definition: XmlTest.php:59
XmlTest\testEncodeJsVarFloat
testEncodeJsVarFloat()
@covers Xml::encodeJsVar
Definition: XmlTest.php:369
Xml\textarea
static textarea( $name, $content, $cols=40, $rows=5, $attribs=array())
Shortcut for creating textareas.
Definition: Xml.php:589
XmlTest
@group Xml
Definition: XmlTest.php:6
XmlTest\testElementAttributes
testElementAttributes()
@covers Xml::element
Definition: XmlTest.php:112
XmlTest\testEncodeJsVarIntString
testEncodeJsVarIntString()
@covers Xml::encodeJsVar
Definition: XmlTest.php:380
XmlTest\setUp
setUp()
Definition: XmlTest.php:8
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:118
Xml\dateMenu
static dateMenu( $year, $month)
Definition: Xml.php:188
XmlTest\testDateMenu
testDateMenu()
@covers Xml::dateMenu
Definition: XmlTest.php:141
XmlTest\testEscapeTagsOnly
testEscapeTagsOnly()
@covers Xml::escapeTagsOnly
Definition: XmlTest.php:103
XmlTest\testElementEmpty
testElementEmpty()
@covers Xml::element
Definition: XmlTest.php:70
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:184
Xml\input
static input( $name, $size=false, $value=false, $attribs=array())
Convenience function to build an HTML text input field.
Definition: Xml.php:294
Xml\label
static label( $label, $id, $attribs=array())
Convenience function to build an HTML form label.
Definition: Xml.php:374
XmlTest\testEncodeJsVarNull
testEncodeJsVarNull()
@covers Xml::encodeJsVar
Definition: XmlTest.php:320
Xml\escapeTagsOnly
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition: Xml.php:735
XmlTest\testEncodeJsVarArray
testEncodeJsVarArray()
@covers Xml::encodeJsVar
Definition: XmlTest.php:331
XmlTest\testEncodeJsVarObject
testEncodeJsVarObject()
@covers Xml::encodeJsVar
Definition: XmlTest.php:347