Bug 32650: Prevent Koha/Holds.t to fail randomly
[koha.git] / t / Koha_XSLT_HTTPS.t
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 }