|
Lines 19-27
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use FindBin; |
22 |
use File::Temp qw/tempfile/; |
| 23 |
use File::Slurp; |
23 |
use Test::More tests => 32; |
| 24 |
use Test::More tests => 35; |
|
|
| 25 |
use Test::Warn; |
24 |
use Test::Warn; |
| 26 |
|
25 |
|
| 27 |
use Koha::XSLT::Base; |
26 |
use Koha::XSLT::Base; |
|
Lines 37-68
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' ); |
36 |
is( $engine->refresh( 'asdjhaskjh'), 0, 'Test on invalid refresh' ); |
| 38 |
|
37 |
|
| 39 |
#check first test xsl |
38 |
#check first test xsl |
| 40 |
my $path= $FindBin::Bin.'/XSLT_Handler/'; |
39 |
my $xsl_1 = <<'XSL_1'; |
| 41 |
my $xsltfile_1 = 'test01.xsl'; |
40 |
<xsl:stylesheet version="1.0" |
| 42 |
is( -e $path.$xsltfile_1, 1, "Found my test stylesheet $xsltfile_1" ); |
41 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 43 |
exit if !-e $path.$xsltfile_1; |
42 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 44 |
$xsltfile_1= $path.$xsltfile_1; |
43 |
> |
| 45 |
|
44 |
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> |
| 46 |
#Testing not-xml strings (undef, empty, some text, malformed xml |
45 |
|
| 47 |
my $output; |
46 |
<xsl:template match="record|marc:record"> |
| 48 |
|
47 |
<record> |
|
|
48 |
<xsl:apply-templates/> |
| 49 |
<datafield tag="990" ind1='' ind2=''> |
| 50 |
<subfield code="a"> |
| 51 |
<xsl:text>I saw you</xsl:text> |
| 52 |
</subfield> |
| 53 |
</datafield> |
| 54 |
</record> |
| 55 |
</xsl:template> |
| 56 |
|
| 57 |
<xsl:template match="node()"> |
| 58 |
<xsl:copy select="."> |
| 59 |
<xsl:copy-of select="@*"/> |
| 60 |
<xsl:apply-templates/> |
| 61 |
</xsl:copy> |
| 62 |
</xsl:template> |
| 63 |
</xsl:stylesheet> |
| 64 |
XSL_1 |
| 65 |
|
| 66 |
# Testing not-xml strings (undef, empty, some text, malformed xml |
| 49 |
# Undefined text tests |
67 |
# Undefined text tests |
| 50 |
$output = $engine->transform( undef, $xsltfile_1 ); |
68 |
my $output; |
|
|
69 |
$output = $engine->transform({ xml => undef, code => $xsl_1 }); |
| 51 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_7, 'Engine returns error on undefined text' ); |
70 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_7, 'Engine returns error on undefined text' ); |
| 52 |
|
71 |
|
| 53 |
# Empty string tests |
72 |
# Empty string tests |
| 54 |
$output = $engine->transform( '', $xsltfile_1 ); |
73 |
$output = $engine->transform({ xml => '', code => $xsl_1 }); |
| 55 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on empty string' ); |
74 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on empty string' ); |
| 56 |
|
75 |
|
| 57 |
# Non-XML tests |
76 |
# Non-XML tests |
| 58 |
$engine->print_warns(1); |
77 |
$engine->print_warns(1); |
| 59 |
warning_like { $output = $engine->transform( 'abcdef', $xsltfile_1 ) } |
78 |
warning_like { $output = $engine->transform({ xml => 'abcdef', code => $xsl_1 }) } |
| 60 |
qr{parser error : Start tag expected, '<' not found}, |
79 |
qr{parser error : Start tag expected, '<' not found}, |
| 61 |
"Non-XML warning correctly displayed"; |
80 |
"Non-XML warning correctly displayed"; |
| 62 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on non-xml' ); |
81 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on non-xml' ); |
| 63 |
|
82 |
|
| 64 |
# Malformed XML tests |
83 |
# Malformed XML tests |
| 65 |
warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) } |
84 |
warning_like { $output = $engine->transform({ xml => '<a></b>', code => $xsl_1 }) } |
| 66 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
85 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
| 67 |
"Malformed XML warning correctly displayed"; |
86 |
"Malformed XML warning correctly displayed"; |
| 68 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on malformed xml' ); |
87 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on malformed xml' ); |
|
Lines 74-106
my $secondengine=Koha::XSLT::Base->new( {
Link Here
|
| 74 |
some_unknown_attrib => 'just_for_fun', |
93 |
some_unknown_attrib => 'just_for_fun', |
| 75 |
}); |
94 |
}); |
| 76 |
$engine->do_not_return_source(1); |
95 |
$engine->do_not_return_source(1); |
| 77 |
warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) } |
96 |
warning_like { $output = $engine->transform({ xml => '<a></b>', code => $xsl_1 }) } |
| 78 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
97 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
| 79 |
"Malformed XML warning correctly displayed"; |
98 |
"Malformed XML warning correctly displayed"; |
| 80 |
is( defined $output? 1: 0, 0, 'Engine respects do_not_return_source==1'); |
99 |
is( defined $output? 1: 0, 0, 'Engine respects do_not_return_source==1'); |
| 81 |
$secondengine->print_warns(1); |
100 |
$secondengine->print_warns(1); |
| 82 |
warning_like { $output = $secondengine->transform( '<a></b>', $xsltfile_1 ) } |
101 |
warning_like { $output = $secondengine->transform({ xml => '<a></b>', code => $xsl_1 }) } |
| 83 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
102 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
| 84 |
"Malformed XML warning correctly displayed"; |
103 |
"Malformed XML warning correctly displayed"; |
| 85 |
is( defined $output? 1: 0, 0, 'Second engine respects it too'); |
104 |
is( defined $output? 1: 0, 0, 'Second engine respects it too'); |
| 86 |
undef $secondengine; #bye |
105 |
undef $secondengine; #bye |
| 87 |
$engine->do_not_return_source(0); |
106 |
$engine->do_not_return_source(0); |
| 88 |
warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) } |
107 |
warning_like { $output = $engine->transform({ xml => '<a></b>', code => $xsl_1 }) } |
| 89 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
108 |
qr{parser error : Opening and ending tag mismatch: a line 1 and b}, |
| 90 |
"Malformed XML warning correctly displayed"; |
109 |
"Malformed XML warning correctly displayed"; |
| 91 |
is( defined $output? 1: 0, 1, 'Engine respects do_not_return_source==0'); |
110 |
is( defined $output? 1: 0, 1, 'Engine respects do_not_return_source==0'); |
| 92 |
|
111 |
|
| 93 |
#Testing valid refresh now |
112 |
#Testing valid refresh now |
|
|
113 |
my $xsltfile_1 = mytempfile($xsl_1); |
| 114 |
$output = $engine->transform( '<records/>', $xsltfile_1 ); |
| 94 |
is( $engine->refresh($xsltfile_1), 1, 'Test on valid refresh' ); |
115 |
is( $engine->refresh($xsltfile_1), 1, 'Test on valid refresh' ); |
| 95 |
#A second time (for all) should return 0 now |
116 |
is( $engine->refresh, 1, 'Second refresh returns 1 for code xsl_1' ); |
| 96 |
is( $engine->refresh, 0, 'Test on repeated refresh' ); |
117 |
is( $engine->refresh, 0, 'Third refresh: nothing left' ); |
| 97 |
|
118 |
|
| 98 |
#Testing a string that should not change too much |
119 |
#Testing a string that should not change too much |
| 99 |
my $xml_1=<<'EOT'; |
120 |
my $xml_1=<<'EOT'; |
| 100 |
<just_a_tagname> |
121 |
<just_a_tagname> |
| 101 |
</just_a_tagname> |
122 |
</just_a_tagname> |
| 102 |
EOT |
123 |
EOT |
| 103 |
$output= $engine->transform( $xml_1, $xsltfile_1 ); |
124 |
$output= $engine->transform({ xml => $xml_1, code => $xsl_1 }); |
| 104 |
is( $engine->err, undef, 'Engine returned no error for xml_1' ); |
125 |
is( $engine->err, undef, 'Engine returned no error for xml_1' ); |
| 105 |
is( index($output,'<just_a_tagname>')>0, 1, 'No real change expected for xml_1' ); #Just very simple check if the tag was still there |
126 |
is( index($output,'<just_a_tagname>')>0, 1, 'No real change expected for xml_1' ); #Just very simple check if the tag was still there |
| 106 |
|
127 |
|
|
Lines 115-129
my $xml_2=<<'EOT';
Link Here
|
| 115 |
</collection> |
136 |
</collection> |
| 116 |
EOT |
137 |
EOT |
| 117 |
$output= $engine->transform( $xml_2 ); |
138 |
$output= $engine->transform( $xml_2 ); |
| 118 |
#note: second parameter (file) not passed again |
139 |
#note: second parameter not passed again |
| 119 |
is( $engine->err, undef, 'Engine returned no error for xml_2' ); |
140 |
is( $engine->err, undef, 'Engine returned no error for xml_2' ); |
| 120 |
is( index($output,'I saw you')>0, 1, 'Saw the expected change for xml_2' ); #Just very simple check if new datafield was added |
141 |
is( index($output,'I saw you')>0, 1, 'Saw the expected change for xml_2' ); #Just very simple check if new datafield was added |
| 121 |
#Test alternative parameter passing |
142 |
#Test alternative parameter passing |
| 122 |
my $output2; |
143 |
my $output2; |
| 123 |
$output2 = $engine->transform( { file => $xsltfile_1, xml => $xml_2 } ); |
144 |
$output2 = $engine->transform( { file => $xsltfile_1, xml => $xml_2 } ); |
| 124 |
is( $output, $output2, 'Try hash parameter file'); |
145 |
is( $output, $output2, 'Try hash parameter file'); |
| 125 |
my $code = read_file( $xsltfile_1 ); |
146 |
$output2 = $engine->transform( { code => $xsl_1, xml => $xml_2 } ); |
| 126 |
$output2 = $engine->transform( { code => $code, xml => $xml_2 } ); |
|
|
| 127 |
is( $output, $output2, 'Try hash parameter code'); |
147 |
is( $output, $output2, 'Try hash parameter code'); |
| 128 |
#Check rerun on last code |
148 |
#Check rerun on last code |
| 129 |
$output2 = $engine->transform( $xml_2 ); |
149 |
$output2 = $engine->transform( $xml_2 ); |
|
Lines 135-167
is( ref $engine->transform({
Link Here
|
| 135 |
'Format parameter returns a xml document object' ); |
155 |
'Format parameter returns a xml document object' ); |
| 136 |
|
156 |
|
| 137 |
#The second test xsl contains bad code |
157 |
#The second test xsl contains bad code |
| 138 |
my $xsltfile_2 = 'test02.xsl'; |
158 |
my $xsl_2 = <<'XSL_2'; |
| 139 |
is( -e $path.$xsltfile_2, 1, "Found my test stylesheet $xsltfile_2" ); |
159 |
<!-- This is BAD coded xslt stylesheet --> |
| 140 |
exit if !-e $path.$xsltfile_2; |
160 |
<xsl:stylesheet version="1.0" |
| 141 |
$xsltfile_2= $path.$xsltfile_2; |
161 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
|
|
162 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 163 |
> |
| 164 |
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> |
| 165 |
|
| 166 |
<xsl:variable name="redefine" select="0"/> |
| 167 |
<xsl:variable name="redefine" select="1"/> |
| 168 |
<!-- Intentional redefine to generate parsing error --> |
| 169 |
<xsl:template match="record"> |
| 170 |
</xsl:template> |
| 171 |
</xsl:stylesheet> |
| 172 |
XSL_2 |
| 142 |
|
173 |
|
| 143 |
$engine->print_warns(0); |
174 |
$engine->print_warns(0); |
| 144 |
$output = $engine->transform( $xml_2, $xsltfile_2 ); |
175 |
$output = $engine->transform({ xml => $xml_2, code => $xsl_2 }); |
| 145 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_4, 'Engine returned error for parsing bad xsl' ); |
176 |
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_4, 'Engine returned error for parsing bad xsl' ); |
| 146 |
|
177 |
|
| 147 |
#The third test xsl is okay again; main use is clearing two items from cache |
178 |
#The third test xsl is okay again; main use is clearing two items from cache |
| 148 |
my $xsltfile_3 = 'test03.xsl'; |
179 |
my $xsl_3 = <<'XSL_3'; |
| 149 |
is( -e $path.$xsltfile_3, 1, "Found my test stylesheet $xsltfile_3" ); |
180 |
<xsl:stylesheet version="1.0" |
| 150 |
exit if !-e $path.$xsltfile_3; |
181 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 151 |
$xsltfile_3= $path.$xsltfile_3; |
182 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 152 |
$output= $engine->transform( $xml_2, $xsltfile_3 ); |
183 |
> |
|
|
184 |
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/> |
| 185 |
|
| 186 |
<xsl:template match="/"> |
| 187 |
<xsl:apply-templates/> |
| 188 |
</xsl:template> |
| 189 |
|
| 190 |
<xsl:template match="node()"> |
| 191 |
<xsl:copy select="."> |
| 192 |
<xsl:copy-of select="@*"/> |
| 193 |
<xsl:apply-templates/> |
| 194 |
</xsl:copy> |
| 195 |
</xsl:template> |
| 196 |
</xsl:stylesheet> |
| 197 |
XSL_3 |
| 198 |
|
| 199 |
$output= $engine->transform({ xml => $xml_2, code => $xsl_3 }); |
| 153 |
is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); |
200 |
is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); |
| 154 |
is( $engine->refresh, 3, 'Final test on clearing cache' ); |
201 |
is( $engine->refresh, 3, 'Final test on clearing cache' ); |
| 155 |
|
202 |
|
| 156 |
my $xsltfile_4 = 'test04.xsl'; |
203 |
# Test xsl no 4 |
| 157 |
is( -e $path.$xsltfile_4, 1, "Found my test stylesheet $xsltfile_4" ); |
204 |
my $xsl_4 = <<'XSL_4'; |
| 158 |
exit if !-e $path.$xsltfile_4; |
205 |
<xsl:stylesheet version="1.0" |
| 159 |
$xsltfile_4 = $path.$xsltfile_4; |
206 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
|
|
207 |
xmlns:marc="http://www.loc.gov/MARC21/slim" |
| 208 |
> |
| 209 |
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/> |
| 210 |
<xsl:param name="injected_variable" /> |
| 211 |
|
| 212 |
<xsl:template match="/"> |
| 213 |
<xsl:apply-templates/> |
| 214 |
</xsl:template> |
| 215 |
|
| 216 |
<xsl:template match="node()"> |
| 217 |
<xsl:copy> |
| 218 |
<xsl:value-of select="$injected_variable"/> |
| 219 |
</xsl:copy> |
| 220 |
</xsl:template> |
| 221 |
|
| 222 |
</xsl:stylesheet> |
| 223 |
XSL_4 |
| 160 |
|
224 |
|
| 161 |
my $parameters = { injected_variable => "'this is a test'",}; |
225 |
my $parameters = { injected_variable => "'this is a test'",}; |
| 162 |
$output = $engine->transform({ |
226 |
$output = $engine->transform({ |
| 163 |
xml => $xml_1, |
227 |
xml => $xml_1, |
| 164 |
file => $xsltfile_4, |
228 |
code => $xsl_4, |
| 165 |
parameters => $parameters, |
229 |
parameters => $parameters, |
| 166 |
}); |
230 |
}); |
| 167 |
require XML::LibXML; |
231 |
require XML::LibXML; |
|
Lines 171-179
is ( $result->to_literal(), 'this is a test', "Successfully injected string into
Link Here
|
| 171 |
|
235 |
|
| 172 |
$output = $engine->transform({ |
236 |
$output = $engine->transform({ |
| 173 |
xml => $xml_1, |
237 |
xml => $xml_1, |
| 174 |
file => $xsltfile_4, |
238 |
code => $xsl_4, |
| 175 |
}); |
239 |
}); |
| 176 |
$dom = XML::LibXML->load_xml(string => $output); |
240 |
$dom = XML::LibXML->load_xml(string => $output); |
| 177 |
$result = $dom->find( '/just_a_tagname' ); |
241 |
$result = $dom->find( '/just_a_tagname' ); |
| 178 |
is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added"); |
242 |
is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added"); |
| 179 |
#End of tests |
243 |
|
|
|
244 |
sub mytempfile { |
| 245 |
my ( $fh, $fn ) = tempfile( SUFFIX => '.xsl', UNLINK => 1 ); |
| 246 |
print $fh $_[0]//''; |
| 247 |
close $fh; |
| 248 |
return $fn; |
| 249 |
} |