|
Lines 20-27
Link Here
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use FindBin; |
22 |
use FindBin; |
|
|
23 |
use File::Temp qw/tempfile/; |
| 23 |
use File::Slurp; |
24 |
use File::Slurp; |
| 24 |
use Test::More tests => 35; |
25 |
use Test::More tests => 32; |
| 25 |
use Test::Warn; |
26 |
use Test::Warn; |
| 26 |
|
27 |
|
| 27 |
use Koha::XSLT::Base; |
28 |
use Koha::XSLT::Base; |
|
Lines 37-47
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_2, 'Engine returns error on bad fi
Link Here
|
| 37 |
is( $engine->refresh( 'asdjhaskjh'), 0, 'Test on invalid refresh' ); |
38 |
is( $engine->refresh( 'asdjhaskjh'), 0, 'Test on invalid refresh' ); |
| 38 |
|
39 |
|
| 39 |
#check first test xsl |
40 |
#check first test xsl |
| 40 |
my $path= $FindBin::Bin.'/XSLT_Handler/'; |
41 |
my $xsltfile_1 = xsl_file(1); |
| 41 |
my $xsltfile_1 = 'test01.xsl'; |
42 |
is( -e $xsltfile_1, 1, "Found my test stylesheet $xsltfile_1" ); |
| 42 |
is( -e $path.$xsltfile_1, 1, "Found my test stylesheet $xsltfile_1" ); |
|
|
| 43 |
exit if !-e $path.$xsltfile_1; |
| 44 |
$xsltfile_1= $path.$xsltfile_1; |
| 45 |
|
43 |
|
| 46 |
#Testing not-xml strings (undef, empty, some text, malformed xml |
44 |
#Testing not-xml strings (undef, empty, some text, malformed xml |
| 47 |
my $output; |
45 |
my $output; |
|
Lines 135-163
is( ref $engine->transform({
Link Here
|
| 135 |
'Format parameter returns a xml document object' ); |
133 |
'Format parameter returns a xml document object' ); |
| 136 |
|
134 |
|
| 137 |
#The second test xsl contains bad code |
135 |
#The second test xsl contains bad code |
| 138 |
my $xsltfile_2 = 'test02.xsl'; |
136 |
my $xsltfile_2 = xsl_file(2); |
| 139 |
is( -e $path.$xsltfile_2, 1, "Found my test stylesheet $xsltfile_2" ); |
|
|
| 140 |
exit if !-e $path.$xsltfile_2; |
| 141 |
$xsltfile_2= $path.$xsltfile_2; |
| 142 |
|
| 143 |
$engine->print_warns(0); |
137 |
$engine->print_warns(0); |
| 144 |
$output = $engine->transform( $xml_2, $xsltfile_2 ); |
138 |
$output = $engine->transform( $xml_2, $xsltfile_2 ); |
| 145 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_4, 'Engine returned error for parsing bad xsl' ); |
139 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_4, 'Engine returned error for parsing bad xsl' ); |
| 146 |
|
140 |
|
| 147 |
#The third test xsl is okay again; main use is clearing two items from cache |
141 |
#The third test xsl is okay again; main use is clearing two items from cache |
| 148 |
my $xsltfile_3 = 'test03.xsl'; |
142 |
my $xsltfile_3 = xsl_file(3); |
| 149 |
is( -e $path.$xsltfile_3, 1, "Found my test stylesheet $xsltfile_3" ); |
|
|
| 150 |
exit if !-e $path.$xsltfile_3; |
| 151 |
$xsltfile_3= $path.$xsltfile_3; |
| 152 |
$output= $engine->transform( $xml_2, $xsltfile_3 ); |
143 |
$output= $engine->transform( $xml_2, $xsltfile_3 ); |
| 153 |
is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); |
144 |
is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); |
| 154 |
is( $engine->refresh, 3, 'Final test on clearing cache' ); |
145 |
is( $engine->refresh, 3, 'Final test on clearing cache' ); |
| 155 |
|
146 |
|
| 156 |
my $xsltfile_4 = 'test04.xsl'; |
147 |
# File no 4 |
| 157 |
is( -e $path.$xsltfile_4, 1, "Found my test stylesheet $xsltfile_4" ); |
148 |
my $xsltfile_4 = xsl_file(4); |
| 158 |
exit if !-e $path.$xsltfile_4; |
|
|
| 159 |
$xsltfile_4 = $path.$xsltfile_4; |
| 160 |
|
| 161 |
my $parameters = { injected_variable => "'this is a test'",}; |
149 |
my $parameters = { injected_variable => "'this is a test'",}; |
| 162 |
$output = $engine->transform({ |
150 |
$output = $engine->transform({ |
| 163 |
xml => $xml_1, |
151 |
xml => $xml_1, |
|
Lines 176-179
$output = $engine->transform({
Link Here
|
| 176 |
$dom = XML::LibXML->load_xml(string => $output); |
164 |
$dom = XML::LibXML->load_xml(string => $output); |
| 177 |
$result = $dom->find( '/just_a_tagname' ); |
165 |
$result = $dom->find( '/just_a_tagname' ); |
| 178 |
is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added"); |
166 |
is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added"); |
| 179 |
#End of tests |
167 |
|
|
|
168 |
sub xsl_file { |
| 169 |
my $i = shift; |
| 170 |
return mytempfile( q{<xsl:stylesheet version="1.0" |
| 171 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 172 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 173 |
> |
| 174 |
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> |
| 175 |
|
| 176 |
<xsl:template match="record|marc:record"> |
| 177 |
<record> |
| 178 |
<xsl:apply-templates/> |
| 179 |
<datafield tag="990" ind1='' ind2=''> |
| 180 |
<subfield code="a"> |
| 181 |
<xsl:text>I saw you</xsl:text> |
| 182 |
</subfield> |
| 183 |
</datafield> |
| 184 |
</record> |
| 185 |
</xsl:template> |
| 186 |
|
| 187 |
<xsl:template match="node()"> |
| 188 |
<xsl:copy select="."> |
| 189 |
<xsl:copy-of select="@*"/> |
| 190 |
<xsl:apply-templates/> |
| 191 |
</xsl:copy> |
| 192 |
</xsl:template> |
| 193 |
</xsl:stylesheet>} ) if $i == 1; |
| 194 |
|
| 195 |
return mytempfile( q{<!-- This is BAD coded xslt stylesheet --> |
| 196 |
<xsl:stylesheet version="1.0" |
| 197 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 198 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 199 |
> |
| 200 |
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> |
| 201 |
|
| 202 |
<xsl:variable name="redefine" select="0"/> |
| 203 |
<xsl:variable name="redefine" select="1"/> |
| 204 |
<!-- Intentional redefine to generate parsing error --> |
| 205 |
<xsl:template match="record"> |
| 206 |
</xsl:template> |
| 207 |
</xsl:stylesheet>} ) if $i == 2; |
| 208 |
|
| 209 |
return mytempfile( q{<xsl:stylesheet version="1.0" |
| 210 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 211 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 212 |
> |
| 213 |
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/> |
| 214 |
|
| 215 |
<xsl:template match="/"> |
| 216 |
<xsl:apply-templates/> |
| 217 |
</xsl:template> |
| 218 |
|
| 219 |
<xsl:template match="node()"> |
| 220 |
<xsl:copy select="."> |
| 221 |
<xsl:copy-of select="@*"/> |
| 222 |
<xsl:apply-templates/> |
| 223 |
</xsl:copy> |
| 224 |
</xsl:template> |
| 225 |
</xsl:stylesheet>} ) if $i == 3; |
| 226 |
|
| 227 |
return mytempfile( q{<xsl:stylesheet version="1.0" |
| 228 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 229 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 230 |
> |
| 231 |
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/> |
| 232 |
<xsl:param name="injected_variable" /> |
| 233 |
|
| 234 |
<xsl:template match="/"> |
| 235 |
<xsl:apply-templates/> |
| 236 |
</xsl:template> |
| 237 |
|
| 238 |
<xsl:template match="node()"> |
| 239 |
<xsl:copy> |
| 240 |
<xsl:value-of select="$injected_variable"/> |
| 241 |
</xsl:copy> |
| 242 |
</xsl:template> |
| 243 |
|
| 244 |
</xsl:stylesheet>} ) if $i == 4; |
| 245 |
} |
| 246 |
|
| 247 |
sub mytempfile { |
| 248 |
my ( $fh, $fn ) = tempfile( SUFFIX => '.xsl', UNLINK => 1 ); |
| 249 |
print $fh $_[0]//''; |
| 250 |
close $fh; |
| 251 |
return $fn; |
| 252 |
} |