* Port tests from t/inc/
[lhc/web/wiklou.git] / tests / XmlTest.php
1 <?php
2
3 class XmlTest extends PHPUnit_Framework_TestCase {
4
5 function testElementOpen() {
6 $this->assertEquals(
7 '<element>',
8 Xml::element( 'element', null, null ),
9 'Opening element with no attributes'
10 );
11 }
12
13 function testElementEmpty() {
14 $this->assertEquals(
15 '<element />',
16 Xml::element( 'element', null, '' ),
17 'Terminated empty element'
18 );
19 }
20
21 function testElementEscaping() {
22 $this->assertEquals(
23 '<element>hello &lt;there&gt; you &amp; you</element>',
24 Xml::element( 'element', null, 'hello <there> you & you' ),
25 'Element with no attributes and content that needs escaping'
26 );
27 }
28
29 function testElementAttributes() {
30 $this->assertEquals(
31 '<element key="value" <>="&lt;&gt;">',
32 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
33 'Element attributes, keys are not escaped'
34 );
35 }
36
37 function testOpenElement() {
38 $this->assertEquals(
39 '<element k="v">',
40 Xml::openElement( 'element', array( 'k' => 'v' ) ),
41 'openElement() shortcut'
42 );
43 }
44
45 function testCloseElement() {
46 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
47 }
48
49 #
50 # textarea
51 #
52 function testTextareaNoContent() {
53 $this->assertEquals(
54 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
55 Xml::textarea( 'name', '' ),
56 'textarea() with not content'
57 );
58 }
59
60 function testTextareaAttribs() {
61 $this->assertEquals(
62 '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
63 Xml::textarea( 'name', '<txt>', 20, 10 ),
64 'textarea() with custom attribs'
65 );
66 }
67
68 #
69 # JS
70 #
71 function testEscapeJsStringSpecialChars() {
72 $this->assertEquals(
73 '\\\\\r\n',
74 Xml::escapeJsString( "\\\r\n" ),
75 'escapeJsString() with special characters'
76 );
77 }
78
79 function testEncodeJsVarBoolean() {
80 $this->assertEquals(
81 'true',
82 Xml::encodeJsVar( true ),
83 'encodeJsVar() with boolean'
84 );
85 }
86
87 function testEncodeJsVarNull() {
88 $this->assertEquals(
89 'null',
90 Xml::encodeJsVar( null ),
91 'encodeJsVar() with null'
92 );
93 }
94
95 function testEncodeJsVarArray() {
96 $this->assertEquals(
97 '["a", 1]',
98 Xml::encodeJsVar( array( 'a', 1 ) ),
99 'encodeJsVar() with array'
100 );
101 $this->assertEquals(
102 '{"a": "a", "b": 1}',
103 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
104 'encodeJsVar() with associative array'
105 );
106 }
107
108 function testEncodeJsVarObject() {
109 $this->assertEquals(
110 '{"a": "a", "b": 1}',
111 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
112 'encodeJsVar() with object'
113 );
114 }
115 }