Bug 24146: Add test cases
[koha.git] / t / db_dependent / XSLT_Handler.t
blob349d6375517204ce08b81597415b617609f2b9b4
1 #!/usr/bin/perl
3 # Copyright 2014 Rijksmuseum
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use FindBin;
23 use File::Slurp;
24 use Test::More tests => 35;
25 use Test::Warn;
27 use Koha::XSLT_Handler;
29 my $engine=Koha::XSLT_Handler->new;
30 is( ref $engine, 'Koha::XSLT_Handler', 'Testing creation of handler object' );
32 $engine->transform('');
33 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_1, 'Engine returns error on no file' );
35 $engine->transform( '', 'thisfileshouldnotexist.%$#@' );
36 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_2, 'Engine returns error on bad file' );
37 is( $engine->refresh( 'asdjhaskjh'), 0, 'Test on invalid refresh' );
39 #check first test xsl
40 my $path= $FindBin::Bin.'/XSLT_Handler/';
41 my $xsltfile_1 = 'test01.xsl';
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;
46 #Testing not-xml strings (undef, empty, some text, malformed xml
47 my $output;
49 # Undefined text tests
50 $output = $engine->transform( undef, $xsltfile_1 );
51 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_7, 'Engine returns error on undefined text' );
53 # Empty string tests
54 $output = $engine->transform( '', $xsltfile_1 );
55 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_5, 'Engine returns error on empty string' );
57 # Non-XML tests
58 $engine->print_warns(1);
59 warning_like { $output = $engine->transform( 'abcdef', $xsltfile_1 ) }
60 qr{parser error : Start tag expected, '<' not found},
61 "Non-XML warning correctly displayed";
62 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_5, 'Engine returns error on non-xml' );
64 # Malformed XML tests
65 warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) }
66 qr{parser error : Opening and ending tag mismatch: a line 1 and b},
67 "Malformed XML warning correctly displayed";
68 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_5, 'Engine returns error on malformed xml' );
70 #Test not returning source on failure when asked for
71 #Include passing do_not_return via constructor on second engine
72 my $secondengine=Koha::XSLT_Handler->new( {
73 do_not_return_source => 'very_true',
74 some_unknown_attrib => 'just_for_fun',
75 });
76 $engine->do_not_return_source(1);
77 warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) }
78 qr{parser error : Opening and ending tag mismatch: a line 1 and b},
79 "Malformed XML warning correctly displayed";
80 is( defined $output? 1: 0, 0, 'Engine respects do_not_return_source==1');
81 $secondengine->print_warns(1);
82 warning_like { $output = $secondengine->transform( '<a></b>', $xsltfile_1 ) }
83 qr{parser error : Opening and ending tag mismatch: a line 1 and b},
84 "Malformed XML warning correctly displayed";
85 is( defined $output? 1: 0, 0, 'Second engine respects it too');
86 undef $secondengine; #bye
87 $engine->do_not_return_source(0);
88 warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) }
89 qr{parser error : Opening and ending tag mismatch: a line 1 and b},
90 "Malformed XML warning correctly displayed";
91 is( defined $output? 1: 0, 1, 'Engine respects do_not_return_source==0');
93 #Testing valid refresh now
94 is( $engine->refresh($xsltfile_1), 1, 'Test on valid refresh' );
95 #A second time (for all) should return 0 now
96 is( $engine->refresh, 0, 'Test on repeated refresh' );
98 #Testing a string that should not change too much
99 my $xml_1=<<'EOT';
100 <just_a_tagname>
101 </just_a_tagname>
103 $output= $engine->transform( $xml_1, $xsltfile_1 );
104 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
107 #Test of adding a new datafield to rudimentary 'marc record'
108 my $xml_2=<<'EOT';
109 <?xml version="1.0" encoding="UTF-8"?>
110 <collection>
111 <record>
112 <controlfield tag="001">1234</controlfield>
113 <datafield tag="245" ind1="1" ind2="0"><subfield tag="a">My favorite title</subfield></datafield>
114 </record>
115 </collection>
117 $output= $engine->transform( $xml_2 );
118 #note: second parameter (file) not passed again
119 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
121 #Test alternative parameter passing
122 my $output2;
123 $output2 = $engine->transform( { file => $xsltfile_1, xml => $xml_2 } );
124 is( $output, $output2, 'Try hash parameter file');
125 my $code = read_file( $xsltfile_1 );
126 $output2 = $engine->transform( { code => $code, xml => $xml_2 } );
127 is( $output, $output2, 'Try hash parameter code');
128 #Check rerun on last code
129 $output2 = $engine->transform( $xml_2 );
130 is( $output, $output2, 'Rerun on previous passed code');
131 #Check format xmldoc
132 is( ref $engine->transform({
133 file => $xsltfile_1, xml => $xml_2, format => 'xmldoc',
134 }), 'XML::LibXML::Document',
135 'Format parameter returns a xml document object' );
137 #The second test xsl contains bad code
138 my $xsltfile_2 = 'test02.xsl';
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;
143 $engine->print_warns(0);
144 $output = $engine->transform( $xml_2, $xsltfile_2 );
145 is( $engine->err, Koha::XSLT_Handler::XSLTH_ERR_4, 'Engine returned error for parsing bad xsl' );
147 #The third test xsl is okay again; main use is clearing two items from cache
148 my $xsltfile_3 = 'test03.xsl';
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 );
153 is( $engine->err, undef, 'Unexpected error on transform with third xsl' );
154 is( $engine->refresh, 3, 'Final test on clearing cache' );
156 my $xsltfile_4 = 'test04.xsl';
157 is( -e $path.$xsltfile_4, 1, "Found my test stylesheet $xsltfile_4" );
158 exit if !-e $path.$xsltfile_4;
159 $xsltfile_4 = $path.$xsltfile_4;
161 my $parameters = { injected_variable => "'this is a test'",};
162 $output = $engine->transform({
163 xml => $xml_1,
164 file => $xsltfile_4,
165 parameters => $parameters,
167 require XML::LibXML;
168 my $dom = XML::LibXML->load_xml(string => $output);
169 my $result = $dom->find( '/just_a_tagname' );
170 is ( $result->to_literal(), 'this is a test', "Successfully injected string into XSLT parameter/variable");
172 $output = $engine->transform({
173 xml => $xml_1,
174 file => $xsltfile_4,
176 $dom = XML::LibXML->load_xml(string => $output);
177 $result = $dom->find( '/just_a_tagname' );
178 is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added");
179 #End of tests