Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 5; |
21 |
|
22 |
use Test::MockModule; |
23 |
use Test::MockObject; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::Mocks; |
27 |
|
28 |
use Koha::Illrequest::Workflow::TypeDisclaimer; |
29 |
use Koha::Database; |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
$schema->storage->txn_begin; |
33 |
|
34 |
my $builder = t::lib::TestBuilder->new; |
35 |
|
36 |
use_ok('Koha::Illrequest::Workflow::TypeDisclaimer'); |
37 |
|
38 |
my $metadata = { |
39 |
title => 'This is a title', |
40 |
author => 'This is an author' |
41 |
}; |
42 |
|
43 |
# Because hashes can reorder themselves, we need to make sure ours is in a |
44 |
# predictable order |
45 |
my $sorted = {}; |
46 |
foreach my $key ( keys %{$metadata} ) { |
47 |
$sorted->{$key} = $metadata->{$key}; |
48 |
} |
49 |
|
50 |
my $type_disclaimer = |
51 |
Koha::Illrequest::Workflow::TypeDisclaimer->new( $sorted, 'staff' ); |
52 |
|
53 |
isa_ok( $type_disclaimer, 'Koha::Illrequest::Workflow::TypeDisclaimer' ); |
54 |
|
55 |
is( |
56 |
$type_disclaimer->prep_metadata($sorted), |
57 |
'eyJhdXRob3IiOiJUaGlzIGlzIGFuIGF1dGhvciIsInRpdGxlIjoiVGhpcyBpcyBhIHRpdGxlIn0%3D%0A', |
58 |
'prep_metadata works' |
59 |
); |
60 |
|
61 |
# Mock ILLBackend (as object) |
62 |
my $backend = Test::MockObject->new; |
63 |
$backend->set_isa('Koha::Illbackends::Mock'); |
64 |
$backend->set_always( 'name', 'Mock' ); |
65 |
$backend->set_always( 'capabilities', sub { return can_create_request => 1 } ); |
66 |
$backend->mock( |
67 |
'metadata', |
68 |
sub { |
69 |
my ( $self, $rq ) = @_; |
70 |
return { |
71 |
ID => $rq->illrequest_id, |
72 |
Title => $rq->patron->borrowernumber |
73 |
}; |
74 |
} |
75 |
); |
76 |
$backend->mock( 'status_graph', sub { }, ); |
77 |
|
78 |
# Mock Koha::Illrequest::load_backend (to load Mocked Backend) |
79 |
my $illreqmodule = Test::MockModule->new('Koha::Illrequest'); |
80 |
$illreqmodule->mock( 'load_backend', |
81 |
sub { my $self = shift; $self->{_my_backend} = $backend; return $self } ); |
82 |
|
83 |
# Mock ILLModuleDisclaimerByType with valid YAML |
84 |
t::lib::Mocks::mock_preference( |
85 |
'ILLModuleDisclaimerByType', "all: |
86 |
text: | |
87 |
<h2>HTML title</h2> |
88 |
<p>This is an HTML paragraph</p> |
89 |
<p>This is another HTML paragraph</p> |
90 |
av_category_code: YES_NO |
91 |
article: |
92 |
text: copyright text for all article type requests |
93 |
av_category_code: YES_NO |
94 |
bypass: 1" |
95 |
); |
96 |
|
97 |
my $req_1 = $builder->build_object( |
98 |
{ |
99 |
class => 'Koha::Illrequests', |
100 |
value => {} |
101 |
} |
102 |
); |
103 |
|
104 |
my $request = $req_1->load_backend('Mock'); |
105 |
|
106 |
is( $type_disclaimer->show_type_disclaimer($request), |
107 |
1, 'able to show type disclaimer form' ); |
108 |
|
109 |
# Mock ILLModuleDisclaimerByType with invalid YAML |
110 |
my $type_disclaimer_module = |
111 |
Test::MockModule->new('Koha::Illrequest::Workflow::TypeDisclaimer'); |
112 |
$type_disclaimer_module->mock( '_get_type_disclaimer_sys_pref', {} ); |
113 |
|
114 |
is( $type_disclaimer->show_type_disclaimer($request), |
115 |
0, 'not able to show type disclaimer form' ); |
116 |
|
117 |
$schema->storage->txn_rollback; |