Bug 24052: Fix Koha/XSLT/Base.t

Moving the separate small attachments into the test script.
Creating them now as temporary files or code fragments.
Deleting original files.

Since Breeding.t also used one of these files, a similar change was made in
that script.

Test plan:
Run t/db_dependent/Koha/XSLT/Base.t. Should pass now.
Run t/db_dependent/Breeding.t again.
Git grep on XSLT_Handler. You should only find release notes.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Marcel de Rooy 2020-02-25 14:56:15 +00:00 committed by Martin Renvoize
parent 2bf171acaf
commit fc2c8c3767
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
6 changed files with 147 additions and 115 deletions

View File

@ -25,10 +25,10 @@
# TODO We need additional tests for Z3950SearchAuth, BreedingSearch
use Modern::Perl;
use FindBin;
use File::Temp qw/tempfile/;
use Test::More tests => 5;
use Test::Warn;
use t::lib::Mocks qw( mock_preference );
use C4::Context;
@ -206,7 +206,7 @@ sub test_do_xslt {
MARC::Field->new('100', ' ', ' ', a => 'John Writer'),
MARC::Field->new('245', ' ', ' ', a => 'Just a title'),
);
my $file= $FindBin::Bin.'/XSLT_Handler/test01.xsl';
my $file= xsl_file();
my $server= { add_xslt => $file };
my $engine=Koha::XSLT::Base->new;
@ -266,3 +266,37 @@ sub test_add_rowdata {
# Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_row_data
is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags");
}
sub xsl_file {
return mytempfile( q{<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="record|marc:record">
<record>
<xsl:apply-templates/>
<datafield tag="990" ind1='' ind2=''>
<subfield code="a">
<xsl:text>I saw you</xsl:text>
</subfield>
</datafield>
</record>
</xsl:template>
<xsl:template match="node()">
<xsl:copy select=".">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>} );
}
sub mytempfile {
my ( $fh, $fn ) = tempfile( SUFFIX => '.xsl', UNLINK => 1 );
print $fh $_[0]//'';
close $fh;
return $fn;
}

View File

@ -19,9 +19,8 @@
use Modern::Perl;
use FindBin;
use File::Slurp;
use Test::More tests => 35;
use File::Temp qw/tempfile/;
use Test::More tests => 32;
use Test::Warn;
use Koha::XSLT::Base;
@ -37,32 +36,52 @@ is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_2, 'Engine returns error on bad fi
is( $engine->refresh( 'asdjhaskjh'), 0, 'Test on invalid refresh' );
#check first test xsl
my $path= $FindBin::Bin.'/XSLT_Handler/';
my $xsltfile_1 = 'test01.xsl';
is( -e $path.$xsltfile_1, 1, "Found my test stylesheet $xsltfile_1" );
exit if !-e $path.$xsltfile_1;
$xsltfile_1= $path.$xsltfile_1;
my $xsl_1 = <<'XSL_1';
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
#Testing not-xml strings (undef, empty, some text, malformed xml
my $output;
<xsl:template match="record|marc:record">
<record>
<xsl:apply-templates/>
<datafield tag="990" ind1='' ind2=''>
<subfield code="a">
<xsl:text>I saw you</xsl:text>
</subfield>
</datafield>
</record>
</xsl:template>
<xsl:template match="node()">
<xsl:copy select=".">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XSL_1
# Testing not-xml strings (undef, empty, some text, malformed xml
# Undefined text tests
$output = $engine->transform( undef, $xsltfile_1 );
my $output;
$output = $engine->transform({ xml => undef, code => $xsl_1 });
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_7, 'Engine returns error on undefined text' );
# Empty string tests
$output = $engine->transform( '', $xsltfile_1 );
$output = $engine->transform({ xml => '', code => $xsl_1 });
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on empty string' );
# Non-XML tests
$engine->print_warns(1);
warning_like { $output = $engine->transform( 'abcdef', $xsltfile_1 ) }
warning_like { $output = $engine->transform({ xml => 'abcdef', code => $xsl_1 }) }
qr{parser error : Start tag expected, '<' not found},
"Non-XML warning correctly displayed";
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on non-xml' );
# Malformed XML tests
warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) }
warning_like { $output = $engine->transform({ xml => '<a></b>', code => $xsl_1 }) }
qr{parser error : Opening and ending tag mismatch: a line 1 and b},
"Malformed XML warning correctly displayed";
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_5, 'Engine returns error on malformed xml' );
@ -74,33 +93,35 @@ my $secondengine=Koha::XSLT::Base->new( {
some_unknown_attrib => 'just_for_fun',
});
$engine->do_not_return_source(1);
warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) }
warning_like { $output = $engine->transform({ xml => '<a></b>', code => $xsl_1 }) }
qr{parser error : Opening and ending tag mismatch: a line 1 and b},
"Malformed XML warning correctly displayed";
is( defined $output? 1: 0, 0, 'Engine respects do_not_return_source==1');
$secondengine->print_warns(1);
warning_like { $output = $secondengine->transform( '<a></b>', $xsltfile_1 ) }
warning_like { $output = $secondengine->transform({ xml => '<a></b>', code => $xsl_1 }) }
qr{parser error : Opening and ending tag mismatch: a line 1 and b},
"Malformed XML warning correctly displayed";
is( defined $output? 1: 0, 0, 'Second engine respects it too');
undef $secondengine; #bye
$engine->do_not_return_source(0);
warning_like { $output = $engine->transform( '<a></b>', $xsltfile_1 ) }
warning_like { $output = $engine->transform({ xml => '<a></b>', code => $xsl_1 }) }
qr{parser error : Opening and ending tag mismatch: a line 1 and b},
"Malformed XML warning correctly displayed";
is( defined $output? 1: 0, 1, 'Engine respects do_not_return_source==0');
#Testing valid refresh now
my $xsltfile_1 = mytempfile($xsl_1);
$output = $engine->transform( '<records/>', $xsltfile_1 );
is( $engine->refresh($xsltfile_1), 1, 'Test on valid refresh' );
#A second time (for all) should return 0 now
is( $engine->refresh, 0, 'Test on repeated refresh' );
is( $engine->refresh, 1, 'Second refresh returns 1 for code xsl_1' );
is( $engine->refresh, 0, 'Third refresh: nothing left' );
#Testing a string that should not change too much
my $xml_1=<<'EOT';
<just_a_tagname>
</just_a_tagname>
EOT
$output= $engine->transform( $xml_1, $xsltfile_1 );
$output= $engine->transform({ xml => $xml_1, code => $xsl_1 });
is( $engine->err, undef, 'Engine returned no error for xml_1' );
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
@ -115,15 +136,14 @@ my $xml_2=<<'EOT';
</collection>
EOT
$output= $engine->transform( $xml_2 );
#note: second parameter (file) not passed again
#note: second parameter not passed again
is( $engine->err, undef, 'Engine returned no error for xml_2' );
is( index($output,'I saw you')>0, 1, 'Saw the expected change for xml_2' ); #Just very simple check if new datafield was added
#Test alternative parameter passing
my $output2;
$output2 = $engine->transform( { file => $xsltfile_1, xml => $xml_2 } );
is( $output, $output2, 'Try hash parameter file');
my $code = read_file( $xsltfile_1 );
$output2 = $engine->transform( { code => $code, xml => $xml_2 } );
$output2 = $engine->transform( { code => $xsl_1, xml => $xml_2 } );
is( $output, $output2, 'Try hash parameter code');
#Check rerun on last code
$output2 = $engine->transform( $xml_2 );
@ -135,33 +155,77 @@ is( ref $engine->transform({
'Format parameter returns a xml document object' );
#The second test xsl contains bad code
my $xsltfile_2 = 'test02.xsl';
is( -e $path.$xsltfile_2, 1, "Found my test stylesheet $xsltfile_2" );
exit if !-e $path.$xsltfile_2;
$xsltfile_2= $path.$xsltfile_2;
my $xsl_2 = <<'XSL_2';
<!-- This is BAD coded xslt stylesheet -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="redefine" select="0"/>
<xsl:variable name="redefine" select="1"/>
<!-- Intentional redefine to generate parsing error -->
<xsl:template match="record">
</xsl:template>
</xsl:stylesheet>
XSL_2
$engine->print_warns(0);
$output = $engine->transform( $xml_2, $xsltfile_2 );
$output = $engine->transform({ xml => $xml_2, code => $xsl_2 });
is( $engine->err, Koha::XSLT::Base::XSLTH_ERR_4, 'Engine returned error for parsing bad xsl' );
#The third test xsl is okay again; main use is clearing two items from cache
my $xsltfile_3 = 'test03.xsl';
is( -e $path.$xsltfile_3, 1, "Found my test stylesheet $xsltfile_3" );
exit if !-e $path.$xsltfile_3;
$xsltfile_3= $path.$xsltfile_3;
$output= $engine->transform( $xml_2, $xsltfile_3 );
my $xsl_3 = <<'XSL_3';
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:copy select=".">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XSL_3
$output= $engine->transform({ xml => $xml_2, code => $xsl_3 });
is( $engine->err, undef, 'Unexpected error on transform with third xsl' );
is( $engine->refresh, 3, 'Final test on clearing cache' );
my $xsltfile_4 = 'test04.xsl';
is( -e $path.$xsltfile_4, 1, "Found my test stylesheet $xsltfile_4" );
exit if !-e $path.$xsltfile_4;
$xsltfile_4 = $path.$xsltfile_4;
# Test xsl no 4
my $xsl_4 = <<'XSL_4';
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
<xsl:param name="injected_variable" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:value-of select="$injected_variable"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XSL_4
my $parameters = { injected_variable => "'this is a test'",};
$output = $engine->transform({
xml => $xml_1,
file => $xsltfile_4,
code => $xsl_4,
parameters => $parameters,
});
require XML::LibXML;
@ -171,9 +235,15 @@ is ( $result->to_literal(), 'this is a test', "Successfully injected string into
$output = $engine->transform({
xml => $xml_1,
file => $xsltfile_4,
code => $xsl_4,
});
$dom = XML::LibXML->load_xml(string => $output);
$result = $dom->find( '/just_a_tagname' );
is ( $result->to_literal(), '', "As expected, no XSLT parameters/variables were added");
#End of tests
sub mytempfile {
my ( $fh, $fn ) = tempfile( SUFFIX => '.xsl', UNLINK => 1 );
print $fh $_[0]//'';
close $fh;
return $fn;
}

View File

@ -1,24 +0,0 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="record|marc:record">
<record>
<xsl:apply-templates/>
<datafield tag="990" ind1='' ind2=''>
<subfield code="a">
<xsl:text>I saw you</xsl:text>
</subfield>
</datafield>
</record>
</xsl:template>
<xsl:template match="node()">
<xsl:copy select=".">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

View File

@ -1,13 +0,0 @@
<!-- This is BAD coded xslt stylesheet to test XSLT_Handler -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="redefine" select="0"/>
<xsl:variable name="redefine" select="1"/>
<!-- Intentional redefine to generate parsing error -->
<xsl:template match="record">
</xsl:template>
</xsl:stylesheet>

View File

@ -1,17 +0,0 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:copy select=".">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

View File

@ -1,18 +0,0 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:marc="http://www.loc.gov/MARC21/slim"
>
<xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
<xsl:param name="injected_variable" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:value-of select="$injected_variable"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>