View | Details | Raw Unified | Return to bug 24052
Collapse All | Expand All

(-)a/t/db_dependent/Breeding.t (-3 / +37 lines)
Lines 25-34 Link Here
25
# TODO We need additional tests for Z3950SearchAuth, BreedingSearch
25
# TODO We need additional tests for Z3950SearchAuth, BreedingSearch
26
26
27
use Modern::Perl;
27
use Modern::Perl;
28
28
use File::Temp qw/tempfile/;
29
use FindBin;
30
use Test::More tests => 5;
29
use Test::More tests => 5;
31
use Test::Warn;
30
use Test::Warn;
31
32
use t::lib::Mocks qw( mock_preference );
32
use t::lib::Mocks qw( mock_preference );
33
33
34
use C4::Context;
34
use C4::Context;
Lines 206-212 sub test_do_xslt { Link Here
206
        MARC::Field->new('100', ' ', ' ', a => 'John Writer'),
206
        MARC::Field->new('100', ' ', ' ', a => 'John Writer'),
207
        MARC::Field->new('245', ' ', ' ', a => 'Just a title'),
207
        MARC::Field->new('245', ' ', ' ', a => 'Just a title'),
208
    );
208
    );
209
    my $file= $FindBin::Bin.'/XSLT_Handler/test01.xsl';
209
    my $file= xsl_file();
210
    my $server= { add_xslt => $file };
210
    my $server= { add_xslt => $file };
211
    my $engine=Koha::XSLT::Base->new;
211
    my $engine=Koha::XSLT::Base->new;
212
212
Lines 266-268 sub test_add_rowdata { Link Here
266
   # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_row_data
266
   # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_row_data
267
   is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags");
267
   is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags");
268
}
268
}
269
270
sub xsl_file {
271
    return mytempfile( q{<xsl:stylesheet version="1.0"
272
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
273
    xmlns:marc="http://www.loc.gov/MARC21/slim"
274
>
275
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
276
277
  <xsl:template match="record|marc:record">
278
      <record>
279
      <xsl:apply-templates/>
280
      <datafield tag="990" ind1='' ind2=''>
281
        <subfield code="a">
282
          <xsl:text>I saw you</xsl:text>
283
        </subfield>
284
      </datafield>
285
      </record>
286
  </xsl:template>
287
288
  <xsl:template match="node()">
289
    <xsl:copy select=".">
290
      <xsl:copy-of select="@*"/>
291
      <xsl:apply-templates/>
292
    </xsl:copy>
293
  </xsl:template>
294
</xsl:stylesheet>} );
295
}
296
297
sub mytempfile {
298
    my ( $fh, $fn ) = tempfile( SUFFIX => '.xsl', UNLINK => 1 );
299
    print $fh $_[0]//'';
300
    close $fh;
301
    return $fn;
302
}
(-)a/t/db_dependent/Koha/XSLT/Base.t (-42 / +112 lines)
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
}
(-)a/t/db_dependent/XSLT_Handler/test01.xsl (-24 lines)
Lines 1-24 Link Here
1
<xsl:stylesheet version="1.0"
2
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
    xmlns:marc="http://www.loc.gov/MARC21/slim"
4
>
5
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
6
7
  <xsl:template match="record|marc:record">
8
      <record>
9
      <xsl:apply-templates/>
10
      <datafield tag="990" ind1='' ind2=''>
11
        <subfield code="a">
12
          <xsl:text>I saw you</xsl:text>
13
        </subfield>
14
      </datafield>
15
      </record>
16
  </xsl:template>
17
18
  <xsl:template match="node()">
19
    <xsl:copy select=".">
20
      <xsl:copy-of select="@*"/>
21
      <xsl:apply-templates/>
22
    </xsl:copy>
23
  </xsl:template>
24
</xsl:stylesheet>
(-)a/t/db_dependent/XSLT_Handler/test02.xsl (-13 lines)
Lines 1-13 Link Here
1
<!-- This is BAD coded xslt stylesheet to test XSLT_Handler -->
2
<xsl:stylesheet version="1.0"
3
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
    xmlns:marc="http://www.loc.gov/MARC21/slim"
5
>
6
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
7
8
  <xsl:variable name="redefine" select="0"/>
9
  <xsl:variable name="redefine" select="1"/>
10
      <!-- Intentional redefine to generate parsing error -->
11
  <xsl:template match="record">
12
  </xsl:template>
13
</xsl:stylesheet>
(-)a/t/db_dependent/XSLT_Handler/test03.xsl (-17 lines)
Lines 1-17 Link Here
1
<xsl:stylesheet version="1.0"
2
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
    xmlns:marc="http://www.loc.gov/MARC21/slim"
4
>
5
  <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
6
7
  <xsl:template match="/">
8
      <xsl:apply-templates/>
9
  </xsl:template>
10
11
  <xsl:template match="node()">
12
    <xsl:copy select=".">
13
      <xsl:copy-of select="@*"/>
14
      <xsl:apply-templates/>
15
    </xsl:copy>
16
  </xsl:template>
17
</xsl:stylesheet>
(-)a/t/db_dependent/XSLT_Handler/test04.xsl (-19 lines)
Lines 1-18 Link Here
1
<xsl:stylesheet version="1.0"
2
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
    xmlns:marc="http://www.loc.gov/MARC21/slim"
4
>
5
  <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
6
  <xsl:param name="injected_variable" />
7
8
  <xsl:template match="/">
9
      <xsl:apply-templates/>
10
  </xsl:template>
11
12
  <xsl:template match="node()">
13
    <xsl:copy>
14
   <xsl:value-of select="$injected_variable"/>
15
    </xsl:copy>
16
  </xsl:template>
17
18
</xsl:stylesheet>
19
- 

Return to bug 24052