XmlParserTest.php
11 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?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: XmlParserTest.php 743 2009-09-30 01:02:29Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
PHPTAL::setIncludePath();
require_once 'PHPTAL/Dom/DocumentBuilder.php';
PHPTAL::restoreIncludePath();
class XmlParserTest extends PHPTAL_TestCase
{
public function testSimpleParse(){
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$parser->parseFile($builder = new MyDocumentBuilder(),'input/xml.01.xml')->getResult();
$expected = trim(join('', file('input/xml.01.xml')));
$this->assertEquals($expected, $builder->result);
$this->assertEquals(7, $builder->elementStarts);
$this->assertEquals(7, $builder->elementCloses);
}
public function testXMLStylesheet() {
$tpl = $this->newPHPTAL();
$tpl->baz = 'quz';
$tpl->setSource('<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="/css" ?>
<?xml-stylesheet type="text/css" href="/${baz}" ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl"/>');
$this->assertEquals(trim_string('<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="/css" ?>
<?xml-stylesheet type="text/css" href="/quz" ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl"></html>'),trim_string($tpl->execute()));
}
public function testPHPBlocksNotInterpolated() {
$tpl = $this->newPHPTAL();
$tpl->setSource('<?php echo \'${baz}\' ?><html></html>');
$this->assertEquals('${baz}<html></html>',$tpl->execute());
}
public function testCharactersBeforeBegining() {
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
try {
$parser->parseFile($builder = new MyDocumentBuilder(),'input/xml.02.xml')->getResult();
$this->assertTrue( false );
}
catch (Exception $e) {
$this->assertTrue( true );
}
}
public function testAllowGtAndLtInTextNodes() {
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$parser->parseFile($builder = new MyDocumentBuilder(),'input/xml.03.xml')->getResult();
$this->assertEquals(trim_file('output/xml.03.xml'), trim_string($builder->result));
$this->assertEquals(3, $builder->elementStarts);
$this->assertEquals(3, $builder->elementCloses);
// a '<' character withing some text data make the parser call 2 times
// the onElementData() method
$this->assertEquals(7, $builder->datas);
}
/**
* @expectedException PHPTAL_ParserException
*/
public function testRejectsInvalidAttributes1()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$parser->parseString($builder = new MyDocumentBuilder(),'<foo bar="bar"baz="baz"/>')->getResult();
$this->fail($builder->result);
}
/**
* @expectedException PHPTAL_ParserException
*/
public function testRejectsInvalidAttributes2()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$parser->parseString($builder = new MyDocumentBuilder(),'<foo bar;="bar"/>')->getResult();
$this->fail($builder->result);
}
public function testSkipsBom()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$parser->parseString($builder = new MyDocumentBuilder(),"\xef\xbb\xbf<foo/>")->getResult();
$this->assertEquals("<foo></foo>", $builder->result);
}
public function testAllowsTrickyQnames()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$parser->parseString($builder = new MyDocumentBuilder(),"\xef\xbb\xbf<_.:_ xmlns:_.='tricky'/>")->getResult();
$this->assertEquals("<_.:_ xmlns:_.=\"tricky\"></_.:_>", $builder->result);
}
public function testAllowsXMLStylesheet()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$src = "<foo>
<?xml-stylesheet href='foo1' ?>
<?xml-stylesheet href='foo2' ?>
</foo>";
$parser->parseString($builder = new MyDocumentBuilder(),$src)->getResult();
$this->assertEquals($src, $builder->result);
}
public function testFixOrRejectCDATAClose()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$src = '<a> ]]> </a>';
try
{
$parser->parseString($builder = new MyDocumentBuilder(),$src)->getResult();
$this->assertEquals('<a> ]]> </a>', $builder->result);
}
catch(PHPTAL_ParserException $e) { /* ok - rejecting is one way to do it */ }
}
/**
* @expectedException PHPTAL_ParserException
*/
public function testSelfClosingSyntaxError()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$src = '<a / >';
$parser->parseString($builder = new MyDocumentBuilder(),$src)->getResult();
}
public function testFixOrRejectEntities()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
$src = '<a href="?foo=1&bar=baz©=true®=x"> & ; Ā &--;</a>';
try
{
$parser->parseString($builder = new MyDocumentBuilder(),$src)->getResult();
$this->assertEquals('<a href="?foo=1&bar=baz&copy=true&reg=x"> & ; Ā &--;</a>', $builder->result);
}
catch(PHPTAL_ParserException $e) { /* ok - rejecting is one way to do it */ }
}
public function testLineAccuracy()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
try
{
$parser->parseString(new PHPTAL_Dom_DocumentBuilder(),
"<x>1
3
4
<!-- 5 -->
<x:y/> error in line 6!
</x>
");
$this->fail("Accepted invalid XML");
}
catch(PHPTAL_ParserException $e)
{
$this->assertEquals(6,$e->srcLine);
}
}
public function testLineAccuracy2()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
try
{
$parser->parseString(new PHPTAL_Dom_DocumentBuilder(),
"<x foo1='
2'
bar4='baz'
/>
<!------->
");
$this->fail("Accepted invalid XML");
}
catch(PHPTAL_ParserException $e)
{
$this->assertEquals(7,$e->srcLine);
}
}
public function testLineAccuracy3()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
try
{
$parser->parseString(new PHPTAL_Dom_DocumentBuilder(),
"
<x foo1='
2'
bar4='baz'
xxxx/>
");
$this->fail("Accepted invalid XML");
}
catch(PHPTAL_ParserException $e)
{
$this->assertEquals(8,$e->srcLine);
}
}
public function testClosingRoot()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
try
{
$parser->parseString(new PHPTAL_Dom_DocumentBuilder(),"<imrootelement/></ishallnotbeclosed>");
$this->fail("Accepted invalid XML");
}
catch(PHPTAL_ParserException $e)
{
$this->assertContains('ishallnotbeclosed',$e->getMessage());
$this->assertNotContains('imrootelement',$e->getMessage());
$this->assertNotContains("documentElement",$e->getMessage());
}
}
public function testNotClosing()
{
$parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
try
{
$parser->parseString(new PHPTAL_Dom_DocumentBuilder(),"<element_a><element_b><element_x/><element_c><element_d><element_e>");
$this->fail("Accepted invalid XML");
}
catch(PHPTAL_ParserException $e)
{
$this->assertNotContains("documentElement",$e->getMessage());
$this->assertRegExp("/element_e.*element_d.*element_c.*element_b.*element_a/",$e->getMessage());
}
}
public function testSPRY()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<html>
<body>
<div metal:define-macro="SomeMacro" xmlns:spry="http://ns.adobe.com/spry" >
<div spry:region="someSpryRegion">
<p spry:if="\'{someRegion::element}\' != \'hello\'">{someSpryRegion::element}</p>
</div>
</div>
</body>
</html>');
$tpl->prepare();
}
public function testSPRY2()
{
$tpl = $this->newPHPTAL();
$tpl->phptal_var = 'ok';
$tpl->setSource('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:spry="http://ns.adobe.com/spry"><body><form>
<input type="text" value="${phptal_var}" spry:if="i == 1" /></form></body></html>');
$this->assertEquals(trim_string('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:spry="http://ns.adobe.com/spry"><body><form>
<input type="text" value="ok" spry:if="i == 1"/></form></body></html>'),trim_string($tpl->execute()));
}
}
class MyDocumentBuilder extends PHPTAL_Dom_DocumentBuilder
{
public $result;
public $elementStarts = 0;
public $elementCloses = 0;
public $specifics = 0;
public $datas = 0;
public $allow_xmldec = true;
public function __construct() {
$this->result = '';
parent::__construct();
}
public function onDoctype($dt) {
$this->specifics++;
$this->allow_xmldec = false;
$this->result .= $dt;
}
public function onXmlDecl($decl){
if (!$this->allow_xmldec) throw new Exception("more than one xml decl");
$this->specifics++;
$this->allow_xmldec = false;
$this->result .= $decl;
}
public function onCDATASection($data) {
$this->onProcessingInstruction('<![CDATA['.$data.']]>');
}
public function onProcessingInstruction($data)
{
$this->specifics++;
$this->allow_xmldec = false;
$this->result .= $data;
}
public function onComment($data) {
$this->onProcessingInstruction('<!--'.$data.'-->');
}
public function onElementStart($name, array $attributes) {
$this->allow_xmldec = false;
$this->elementStarts++;
$this->result .= "<$name";
$pairs = array();
foreach ($attributes as $key=>$value) $pairs[] = "$key=\"$value\"";
if (count($pairs) > 0) {
$this->result .= ' ' . join(' ', $pairs);
}
$this->result .= '>';
}
public function onElementClose($name){
$this->allow_xmldec = false;
$this->elementCloses++;
$this->result .= "</$name>";
}
public function onElementData($data){
$this->datas++;
$this->result .= $data;
}
public function onDocumentStart(){}
public function onDocumentEnd(){}
}