Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2014 Rijksmuseum |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
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. |
11 |
# |
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. |
15 |
# |
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. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use FindBin; |
23 |
use Test::More tests => 24; |
24 |
|
25 |
use Koha::XSLT_Handler; |
26 |
|
27 |
my $engine=Koha::XSLT_Handler->new; |
28 |
is( ref $engine, 'Koha::XSLT_Handler', 'Testing creation of handler object' ); |
29 |
|
30 |
$engine->transform(''); #we passed no file at first time |
31 |
is( $engine->err, 1, 'Engine returns error on no file' ); |
32 |
|
33 |
$engine->transform( '', 'thisfileshouldnotexist.%$#@' ); |
34 |
is( $engine->err, 2, 'Engine returns error on bad file' ); |
35 |
|
36 |
is( $engine->refresh( 'asdjhaskjh'), 0, 'Test on invalid refresh' ); |
37 |
|
38 |
#check first test xsl |
39 |
my $path= $FindBin::Bin.'/XSLT_Handler/'; |
40 |
my $xsltfile_1 = 'test01.xsl'; |
41 |
is( -e $path.$xsltfile_1, 1, "Found my test stylesheet $xsltfile_1" ); |
42 |
exit if !-e $path.$xsltfile_1; |
43 |
$xsltfile_1= $path.$xsltfile_1; |
44 |
|
45 |
#Testing not-xml strings (undef, empty, some text, malformed xml |
46 |
my $output= $engine->transform( undef, $xsltfile_1 ); |
47 |
is( $engine->err, 7, 'Engine returns error on undefined text' ); |
48 |
$output= $engine->transform( '', $xsltfile_1 ); |
49 |
is( $engine->err, 5, 'Engine returns error on empty string' ); |
50 |
$output= $engine->transform( 'abcdef', $xsltfile_1 ); |
51 |
is( $engine->err, 5, 'Engine returns error on non-xml' ); |
52 |
$output= $engine->transform( '<a></b>', $xsltfile_1 ); |
53 |
is( $engine->err, 5, 'Engine returns error on malformed xml' ); |
54 |
|
55 |
#Test not returning source on failure when asked for |
56 |
#Include passing do_not_return via constructor on second engine |
57 |
my $secondengine=Koha::XSLT_Handler->new( { |
58 |
do_not_return_source => 'very_true', |
59 |
some_unknown_attrib => 'just_for_fun', |
60 |
}); |
61 |
$engine->do_not_return_source(1); |
62 |
$output= $engine->transform( '<a></b>', $xsltfile_1 ); |
63 |
is( defined $output? 1: 0, 0, 'Engine respects do_not_return_source==1'); |
64 |
$output= $secondengine->transform( '<a></b>', $xsltfile_1 ); |
65 |
is( defined $output? 1: 0, 0, 'Second engine respects it too'); |
66 |
undef $secondengine; #bye |
67 |
$engine->do_not_return_source(0); |
68 |
$output= $engine->transform( '<a></b>', $xsltfile_1 ); |
69 |
is( defined $output? 1: 0, 1, 'Engine respects do_not_return_source==0'); |
70 |
|
71 |
#Testing valid refresh now |
72 |
is( $engine->refresh($xsltfile_1), 1, 'Test on valid refresh' ); |
73 |
#A second time (for all) should return 0 now |
74 |
is( $engine->refresh, 0, 'Test on repeated refresh' ); |
75 |
|
76 |
#Testing a string that should not change too much |
77 |
my $xml_1=<<'EOT'; |
78 |
<just_a_tagname> |
79 |
</just_a_tagname> |
80 |
EOT |
81 |
$output= $engine->transform( $xml_1, $xsltfile_1 ); |
82 |
is( $engine->err, undef, 'Engine returned no error for xml_1' ); |
83 |
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 |
84 |
|
85 |
#Test of adding a new datafield to rudimentary 'marc record' |
86 |
my $xml_2=<<'EOT'; |
87 |
<?xml version="1.0" encoding="UTF-8"?> |
88 |
<collection> |
89 |
<record> |
90 |
<controlfield tag="001">1234</controlfield> |
91 |
<datafield tag="245" ind1="1" ind2="0"><subfield tag="a">My favorite title</subfield></datafield> |
92 |
</record> |
93 |
</collection> |
94 |
EOT |
95 |
$output= $engine->transform( $xml_2 ); |
96 |
#note: second parameter (file) not passed again |
97 |
is( $engine->err, undef, 'Engine returned no error for xml_2' ); |
98 |
is( index($output,'I saw you')>0, 1, 'Saw the expected change for xml_2' ); #Just very simple check if new datafield was added |
99 |
|
100 |
#The second test xsl contains bad code |
101 |
my $xsltfile_2 = 'test02.xsl'; |
102 |
is( -e $path.$xsltfile_2, 1, "Found my test stylesheet $xsltfile_2" ); |
103 |
exit if !-e $path.$xsltfile_2; |
104 |
$xsltfile_2= $path.$xsltfile_2; |
105 |
|
106 |
$output= $engine->transform( $xml_2, $xsltfile_2 ); |
107 |
is( $engine->err, 4, 'Engine returned error for parsing bad xsl' ); |
108 |
is( defined($engine->errstr), 1, 'Error string contains text'); |
109 |
|
110 |
#The third test xsl is okay again; main use is clearing two items from cache |
111 |
my $xsltfile_3 = 'test03.xsl'; |
112 |
is( -e $path.$xsltfile_3, 1, "Found my test stylesheet $xsltfile_3" ); |
113 |
exit if !-e $path.$xsltfile_3; |
114 |
$xsltfile_3= $path.$xsltfile_3; |
115 |
$output= $engine->transform( $xml_2, $xsltfile_3 ); |
116 |
is( $engine->err, undef, 'Unexpected error on transform with third xsl' ); |
117 |
is( $engine->refresh, 2, 'Final test on clearing cache' ); |
118 |
|
119 |
#End of tests |