EscapeHTMLTest.php
7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id: EscapeHTMLTest.php 618 2009-05-24 22:26:06Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
class EscapeHTMLTest extends PHPTAL_TestCase {
private function executeString($str, $params = array())
{
$tpl = $this->newPHPTAL();
foreach ($params as $k => $v) $tpl->set($k,$v);
$tpl->setSource($str);
return $tpl->execute();
}
function testDoesEscapeHTMLContent(){
$tpl = $this->newPHPTAL('input/escape.html');
$exp = trim_file('output/escape.html');
$res = trim_string($tpl->execute());
$this->assertEquals($exp, $res);
}
function testEntityDecodingPath1()
{
$res = $this->executeString('<div title=""" class=\'"\' tal:content="\'" quote character\'" />');
$this->assertNotContains('&',$res);
}
function testEntityDecodingBeforePHP()
{
/* PHP block in attributes gets raw input (that's not XML style, but PHP style) */
$res = $this->executeString('<div title="${php:strlen(\'"&\')}" class="<?php echo strlen(\'"&\')?>">'.
'<tal:block tal:content="php:strlen(\'"&\')" />,${php:strlen(\'"&\')}</div>');
$this->assertEquals('<div title="2" class="11">2,2</div>',$res);
}
function testEntityEncodingAfterPHP()
{
$res = $this->executeString('<div title="${php:urldecode(\'%26%22%3C\')}"><tal:block tal:content="php:urldecode(\'%26%22%3C\')" />,${php:urldecode(\'%26%22%3C\')}</div>');
$this->assertEquals('<div title="&"<">&"<,&"<</div>',$res);
}
function testNoEntityEncodingAfterStructurePHP()
{
$res = $this->executeString('<div title="${structure php:urldecode(\'%26%20%3E%27\')}" class="<?php echo urldecode(\'%26%20%3E%27\')?>">'.
'<tal:block tal:content="structure php:urldecode(\'%26%20%3E%22\')" />,${structure php:urldecode(\'%26%20%3E%22\')},<?php echo urldecode(\'%26%20%3E%22\')?></div>');
$this->assertEquals('<div title="& >\'" class="& >\'">& >",& >",& >"</div>',$res);
}
function testDecodingBeforeStructure()
{
$res = $this->executeString('<div tal:content="structure php:\'& quote character\'" />');
$this->assertNotContains('&',$res);
}
function testEntityDecodingPHP1()
{
$res = $this->executeString('<div tal:content="php:\'" quote character\'" />');
$this->assertNotContains('&',$res);
}
function testEntityDecodingPath2()
{
$res = $this->executeString('<div tal:attributes="title \'" quote character\'" />');
$this->assertNotContains('&',$res);
}
function testEntityDecodingPHP2()
{
$res = $this->executeString('<div tal:attributes="title php:\'" quote character\'" />');
$this->assertNotContains('&',$res);
}
function testEntityDecodingPath3()
{
$res = $this->executeString('<p>${\'" quote character\'}</p>');
$this->assertNotContains('&',$res);
}
function testEntityDecodingPHP3()
{
$res = $this->executeString('<p>${php:\'" quote character\'}</p>');
$this->assertNotContains('&',$res);
}
function testEntityEncodingPath1()
{
$res = $this->executeString('<div tal:content="\'& ampersand character\'" />');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingPHP1()
{
$res = $this->executeString('<div tal:content="php:\'& ampersand character\'" />');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingPath2()
{
$res = $this->executeString('<div tal:attributes="title \'& ampersand character\'" />');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingVariables()
{
$res = $this->executeString('<div tal:attributes="title variable; class variable">${variable}${php:variable}</div>',
array('variable'=>'& = ampersand, " = quote, \' = apostrophe'));
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingAttributesDefault1()
{
$res = $this->executeString('<div tal:attributes="title idontexist | default" title=\'& ampersand character\' />');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingAttributesDefault2()
{
$res = $this->executeString('<div tal:attributes="title idontexist | default" title=\'"'\' />');
$this->assertNotContains('&',$res);
$this->assertContains('"',$res); // or apos...
}
function testEntityEncodingPHP2()
{
$res = $this->executeString('<div tal:attributes="title php:\'& ampersand character\'" />');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingPath3()
{
$res = $this->executeString('<p>${\'& ampersand character\'}</p>');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testEntityEncodingPHP3()
{
$res = $this->executeString('<p>&{php:\'& ampersand character\'}</p>');
$this->assertContains('&',$res);
$this->assertNotContains('&amp;',$res);
$this->assertNotContains('&&',$res);
}
function testSimpleXML()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>${x} ${y}</p>');
$simplexml = new SimpleXMLElement('<foo title="bar&<" empty="">foo&<</foo>');
$tpl->x = $simplexml['title'];
$tpl->y = $simplexml['empty'];
$this->assertEquals('<p>bar&< </p>',$tpl->execute());
}
function testStructureSimpleXML()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>${structure x} ${structure y}</p>');
$simplexml = new SimpleXMLElement('<foo title="bar&<" empty="">foo&<</foo>');
$tpl->x = $simplexml['title'];
$tpl->y = $simplexml['empty'];
$this->assertEquals('<p>bar&< </p>',$tpl->execute());
}
function testUnicodeUnescaped()
{
$tpl = $this->newPHPTAL();
$tpl->World = '${World}'; // a quine! ;)
$tpl->setSource($src = '<p>Hello “${World}!”</p>');
$this->assertEquals($src, $tpl->execute());
}
}