Skip to content

Commit 9bb526c

Browse files
author
Melvyn Sopacua
committed
(xslt tests) Add test for new backend API (005.phpt), new function
(006.phpt) and a crash test (007.phpt) 006.phpt also tests handling of public entities, which is in essence new to the extension, since there was no way to turn it on. # These new functions and backend will be added shortly. # TODO: test for xslt_set_object
1 parent 778615b commit 9bb526c

File tree

7 files changed

+219
-0
lines changed

7 files changed

+219
-0
lines changed

ext/xslt/tests/005.phpt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Various ways to provide xml and xslt arguments and params
3+
--SKIPIF--
4+
<?php
5+
include("skipif.inc");
6+
if(!function_exists('utf8_encode')) {
7+
die("skip\n");
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
error_reporting(E_ALL);
13+
$xmlfile = 'ext/xslt/tests/test.xml';
14+
$xslfile = 'ext/xslt/tests/args.xsl';
15+
$xmldata = @implode('', @file($xmlfile));
16+
$xslsheet = @implode('', @file($xslfile));
17+
18+
$xh = xslt_create();
19+
$result = xslt_process($xh, $xmlfile, $xslfile);
20+
print "$result\n";
21+
$result = xslt_process($xh, 'arg:/_xml', $xslfile, NULL, array('/_xml' => $xmldata));
22+
print "$result\n";
23+
$result = xslt_process($xh, $xmlfile, 'arg:/_xsl', NULL, array('/_xsl' => $xslsheet));
24+
print "$result\n";
25+
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, array('/_xml' => $xmldata, '/_xsl' => $xslsheet));
26+
print "$result\n";
27+
28+
// The same, with params
29+
$xslfile = 'ext/xslt/tests/param.xsl';
30+
$xslsheet = implode('', file($xslfile));
31+
$params = array("Test has passed", "PHP QA®");
32+
33+
foreach($params AS $val)
34+
{
35+
$val = utf8_encode($val);
36+
$result = xslt_process($xh, $xmlfile, $xslfile, NULL, NULL, array('insertion' => $val));
37+
print "$result\n";
38+
$result = xslt_process($xh, 'arg:/_xml', $xslfile, NULL, array('/_xml' => $xmldata), array('insertion' => $val));
39+
print "$result\n";
40+
$result = xslt_process($xh, $xmlfile, 'arg:/_xsl', NULL, array('/_xsl' => $xslsheet), array('insertion' => $val));
41+
print "$result\n";
42+
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, array('/_xml' => $xmldata, '/_xsl' => $xslsheet), array('insertion' => $val));
43+
print "$result\n";
44+
}
45+
46+
xslt_free($xh);
47+
?>
48+
--EXPECT--
49+
Test has passed
50+
Test has passed
51+
Test has passed
52+
Test has passed
53+
Test has passed
54+
Test has passed
55+
Test has passed
56+
Test has passed
57+
PHP QA®
58+
PHP QA®
59+
PHP QA®
60+
PHP QA®

ext/xslt/tests/006.phpt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
Crash xslt_process with reused handler (this test may take a while)
3+
--SKIPIF--
4+
<?php
5+
include("skipif.inc");
6+
if(!function_exists('utf8_encode')) {
7+
die("skip\n");
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
error_reporting(E_ALL);
13+
$xmlfile = 'ext/xslt/tests/test.xml';
14+
$xslfile = 'ext/xslt/tests/param.xsl';
15+
$xmldata = @implode('', @file($xmlfile));
16+
$xslsheet = @implode('', @file($xslfile));
17+
18+
/*
19+
* Tested on a Cyrix 200MMX/128MB took 2 secs. Should be a reasonable margin.
20+
*
21+
* It's not meant as an actual speed test, but if it's slower than this,
22+
* there must be something significantly off in the php/sablot/expat trio.
23+
* Emulation OS's come to mind...
24+
*/
25+
$want_time = 6;
26+
27+
function make_param()
28+
{
29+
$ret_val = '';
30+
$numchars = mt_rand(2,16);
31+
$illegal = array(0,256,512);
32+
for($i=0;$i<$numchars;$i++)
33+
{
34+
$char=0;
35+
while(in_array($char, $illegal))
36+
{
37+
$char .= mt_rand(32, 512);
38+
}
39+
$ret_val .= chr($char);
40+
}
41+
42+
return utf8_encode($ret_val);
43+
}
44+
45+
function decode($string)
46+
{
47+
$ret_val = '';
48+
for($i=0; $i<strlen($string);$i++)
49+
{
50+
$ret_val .= ord(substr($string,$i,1)) . " ";
51+
}
52+
return $ret_val;
53+
}
54+
55+
56+
$xh = xslt_create();
57+
58+
$t1 = time();
59+
for ($i=0; $i<50; $i++)
60+
{
61+
$val = make_param();
62+
$result = xslt_process($xh, $xmlfile, $xslfile, NULL, NULL, array('insertion' => $val));
63+
if(!$result or $result != utf8_decode($val))
64+
print "Failed $i / ".utf8_decode($val).": $result\n\tDecode: " . decode(utf8_decode($val)) . "\n" ;
65+
}
66+
print "OK\n";
67+
xslt_free($xh);
68+
$t2 = time();
69+
$op_time = $t2 - $t1;
70+
if($op_time > $want_time)
71+
print "This test took more than $want_time seconds. Either you have a very slow / busy machine, or there's something very wrong with the speed. Your time: $op_time";
72+
?>
73+
--EXPECT--
74+
OK

ext/xslt/tests/007.phpt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
xslt_set_opt function and public entities
3+
--SKIPIF--
4+
<?php
5+
include("skipif.inc");
6+
if(!function_exists('xslt_setopt')) {
7+
die("skip\n");
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
error_reporting(E_ALL);
13+
$xmlfile = 'ext/xslt/tests/public.xml';
14+
$xslfile = 'ext/xslt/tests/args.xsl';
15+
16+
$xh = xslt_create();
17+
// Tell Sablotron to process public entities
18+
xslt_setopt($xh, XSLT_SAB_PARSE_PUBLIC_ENTITIES);
19+
20+
$result = xslt_process($xh, $xmlfile, $xslfile);
21+
print "$result\n";
22+
23+
$xslstring = implode('', file($xslfile));
24+
$xslstring = str_replace('method="text"', 'method="html"', $xslstring);
25+
$xslstring = str_replace('<xsl:value-of select="." />', '<html><head><title>foo</title></head><body><p><xsl:value-of select="." /></p></body></html>', $xslstring);
26+
// DEBUG: print $xslstring;
27+
28+
xslt_setopt($xh, XSLT_SAB_PARSE_PUBLIC_ENTITIES | XSLT_SAB_DISABLE_ADDING_META);
29+
$result_nometa = xslt_process($xh, $xmlfile, 'arg:/_xsl', NULL, array('/_xsl' => $xslstring));
30+
// DEBUG: print "$result_nometa\n";
31+
32+
xslt_setopt($xh, XSLT_SAB_PARSE_PUBLIC_ENTITIES);
33+
$result_meta = xslt_process($xh, $xmlfile, 'arg:/_xsl', NULL, array('/_xsl' => $xslstring));
34+
// DEBUG: print "$result_meta\n";
35+
36+
/* Also test if they're equal. That would mean, that disable-adding-meta is set to off
37+
at compile time and our call to xslt_setopt failed to reset that */
38+
if($result_meta != $result_nometa && FALSE === stristr($result_nometa, '<meta http-equiv="Content-Type"'))
39+
{
40+
print "OK\n";
41+
}
42+
43+
xslt_free($xh);
44+
?>
45+
--EXPECT--
46+
PHP QA®
47+
OK

ext/xslt/tests/args.xsl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3+
<xsl:output encoding="ISO-8859-1" method="text" omit-xml-declaration="yes" />
4+
<xsl:template match="/qa">
5+
<xsl:apply-templates select="test" />
6+
</xsl:template>
7+
<xsl:template match="test[@type = 'simple']">
8+
<xsl:value-of select="." />
9+
</xsl:template>
10+
</xsl:stylesheet>

ext/xslt/tests/param.xsl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3+
<xsl:output method="text" omit-xml-declaration="yes" encoding="ISO-8859-1" />
4+
<xsl:param name="insertion">Test failed</xsl:param>
5+
<xsl:template match="/qa">
6+
<xsl:apply-templates select="test" />
7+
</xsl:template>
8+
<xsl:template match="test">
9+
<xsl:choose>
10+
<xsl:when test="@type != 'simple'">
11+
<xsl:value-of select="$insertion" />
12+
</xsl:when>
13+
</xsl:choose>
14+
</xsl:template>
15+
</xsl:stylesheet>

ext/xslt/tests/qa.dtd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!ELEMENT qa (test+)>
3+
4+
<!ELEMENT test (#PCDATA)>
5+
<!ATTLIST test
6+
type CDATA #IMPLIED
7+
>
8+
<!ENTITY reg "&#174;">

ext/xslt/tests/test.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<qa>
3+
<test type="simple">Test has passed</test>
4+
<test type="complex" />
5+
</qa>

0 commit comments

Comments
 (0)