Per wikitech-l discussion: Move tests from maintenance/tests/ to tests/. They're...
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlTest.php
1 <?php
2
3 class XmlTest extends PHPUnit_Framework_TestCase {
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 #
77 # textarea
78 #
79 function testTextareaNoContent() {
80 $this->assertEquals(
81 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
82 Xml::textarea( 'name', '' ),
83 'textarea() with not content'
84 );
85 }
86
87 function testTextareaAttribs() {
88 $this->assertEquals(
89 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
90 Xml::textarea( 'name', '<txt>', 20, 10 ),
91 'textarea() with custom attribs'
92 );
93 }
94
95 #
96 # input and label
97 #
98 function testLabelCreation() {
99 $this->assertEquals(
100 '<label for="id">name</label>',
101 Xml::label( 'name', 'id' ),
102 'label() with no attribs'
103 );
104 }
105 function testLabelAttributeCanOnlyBeClass() {
106 $this->assertEquals(
107 '<label for="id">name</label>',
108 Xml::label( 'name', 'id', array( 'generated' => true ) ),
109 'label() can not be given a generated attribute'
110 );
111 $this->assertEquals(
112 '<label for="id" class="nice">name</label>',
113 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
114 'label() can get a class attribute'
115 );
116 $this->assertEquals(
117 '<label for="id" class="nice">name</label>',
118 Xml::label( 'name', 'id', array(
119 'generated' => true,
120 'class' => 'nice',
121 'anotherattr' => 'value',
122 )
123 ),
124 'label() skip all attributes but "class"'
125 );
126 }
127
128 #
129 # JS
130 #
131 function testEscapeJsStringSpecialChars() {
132 $this->assertEquals(
133 '\\\\\r\n',
134 Xml::escapeJsString( "\\\r\n" ),
135 'escapeJsString() with special characters'
136 );
137 }
138
139 function testEncodeJsVarBoolean() {
140 $this->assertEquals(
141 'true',
142 Xml::encodeJsVar( true ),
143 'encodeJsVar() with boolean'
144 );
145 }
146
147 function testEncodeJsVarNull() {
148 $this->assertEquals(
149 'null',
150 Xml::encodeJsVar( null ),
151 'encodeJsVar() with null'
152 );
153 }
154
155 function testEncodeJsVarArray() {
156 $this->assertEquals(
157 '["a", 1]',
158 Xml::encodeJsVar( array( 'a', 1 ) ),
159 'encodeJsVar() with array'
160 );
161 $this->assertEquals(
162 '{"a": "a", "b": 1}',
163 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
164 'encodeJsVar() with associative array'
165 );
166 }
167
168 function testEncodeJsVarObject() {
169 $this->assertEquals(
170 '{"a": "a", "b": 1}',
171 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
172 'encodeJsVar() with object'
173 );
174 }
175 }
176
177 // TODO
178 class XmlSelectTest extends PHPUnit_Framework_TestCase {
179 }