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

(-)a/Koha/Exceptions/XSLT.pm (+50 lines)
Line 0 Link Here
1
package Koha::Exceptions::XSLT;
2
3
# Copyright 2022 Rijksmuseum, Koha Development Team
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
Koha::Exceptions::XSLT;
23
24
=head1 SYNOPSIS
25
26
Koha::Exceptions::XSLT::MissingFilename->throw;
27
28
=head1 DESCRIPTION
29
30
Defines a few exceptions for Koha::XSLT:: modules
31
32
=cut
33
34
use Modern::Perl;
35
use Koha::Exception;
36
use Exception::Class (
37
    'Koha::Exceptions::XSLT' => {
38
        isa => 'Koha::Exception',
39
    },
40
    'Koha::Exceptions::XSLT::MissingFilename' => {
41
        isa => 'Koha::Exceptions::XSLT',
42
        description => 'File name required',
43
    },
44
    'Koha::Exceptions::XSLT::FetchFailed' => {
45
        isa => 'Koha::Exceptions::XSLT',
46
        description => 'Fetching xslt file failed',
47
    },
48
);
49
50
1;
(-)a/Koha/XSLT/HTTPS.pm (+82 lines)
Line 0 Link Here
1
package Koha::XSLT::HTTPS;
2
3
# Copyright 2022 Rijksmuseum
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
Koha::XSLT::HTTPS - Helper module to resolve issues with https stylesheets
23
24
=head1 SYNOPSIS
25
26
    Koha::XSLT::HTTPS->load( $filename );
27
28
=head1 DESCRIPTION
29
30
This module collects the contents of https XSLT styleheets where
31
libxml2/libxslt fail to do so. This should be considered as a
32
temporary workaround.
33
34
A similar problem comes up with xslt include files. The module could
35
be extended to resolve these issues too. What holds me back now, is
36
the fact that we need to parse the whole xslt code.
37
38
=cut
39
40
use Modern::Perl;
41
use LWP::UserAgent;
42
43
use Koha::Exceptions::XSLT;
44
45
=head1 METHODS
46
47
=head2 load
48
49
     Koha::XSLT::HTTPS->load( $filename );
50
51
=cut
52
53
sub load {
54
    my ( $class, $filename ) = @_;
55
56
    Koha::Exceptions::XSLT::MissingFilename->throw if !$filename;
57
    return { location => $filename } if $filename !~ /^https:\/\//;
58
59
    my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } );
60
    my $resp = $ua->get( $filename );
61
    if( $resp->is_success ) {
62
        my $contents = $resp->decoded_content;
63
        # $contents = $self->_resolve_includes( $contents );
64
        return { string => $contents };
65
    }
66
    Koha::Exceptions::XSLT::FetchFailed->throw;
67
}
68
69
sub _resolve_includes {
70
# We could parse the code for includes/imports, fetch them and change refs
71
    my ( $self, $code ) = @_;
72
    # TODO Extend it
73
    return $code;
74
}
75
76
1;
77
78
=head1 AUTHOR
79
80
    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
81
82
=cut
(-)a/t/Koha_XSLT_HTTPS.t (-1 / +59 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2022 Rijksmuseum
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::Exception;
23
use Test::MockModule;
24
use Test::MockObject;
25
use Test::More tests => 1;
26
27
use Koha::XSLT::HTTPS;
28
29
subtest 'load' => sub {
30
    plan tests => 9;
31
32
    # Mock the LWP stuff
33
    my $lwp_object = Test::MockObject->new;
34
    my $response = Test::MockObject->new;
35
    $response->mock( 'is_success', sub { return 0 } );
36
    $lwp_object->mock( 'get', sub { return $response; } );
37
    my $lwp_mod = Test::MockModule->new( 'LWP::UserAgent' );
38
    $lwp_mod->mock( 'new', sub { return $lwp_object } );
39
40
    # Trivial bad input
41
    throws_ok { Koha::XSLT::HTTPS->load; } 'Koha::Exceptions::XSLT::MissingFilename', 'No filename';
42
    throws_ok { Koha::XSLT::HTTPS->load(q{}); } 'Koha::Exceptions::XSLT::MissingFilename', 'Empty filename';
43
    my $result = Koha::XSLT::HTTPS->load('filename.xsl');
44
    is( ref($result), 'HASH', 'Should return hash' );
45
    is( exists $result->{location}, 1, 'Hash key location found' );
46
    is( $result->{location}, 'filename.xsl', 'Value for location key' );
47
48
    # Mock returns no success
49
    throws_ok { Koha::XSLT::HTTPS->load('https://myfavoritexsltsite.com/test1.xsl'); }
50
        'Koha::Exceptions::XSLT::FetchFailed', 'Fetch failed';
51
52
    # Mock returns 'code'
53
    $response->mock( 'is_success', sub { return 1 } );
54
    $response->mock( 'decoded_content', sub { return 'supposed_xslt_code' } );
55
    $result = Koha::XSLT::HTTPS->load('https://myfavoritexsltsite.com/test2.xsl');
56
    is( ref($result), 'HASH', 'Should return hash' );
57
    is( exists $result->{string}, 1, 'Hash key string found' );
58
    is( $result->{string}, 'supposed_xslt_code', 'Value for string key' );
59
}

Return to bug 12758