Bug 33607: Handle default framework
[koha.git] / t / db_dependent / Reserves / GetReserveFee.t
1 #!/usr/bin/perl
2
3 # This script includes tests for GetReserveFee and ChargeReserveFee
4
5 # Copyright 2015 Rijksmuseum
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23
24 use Test::More tests => 3;
25 use Test::MockModule;
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use C4::Circulation qw( AddIssue );
30 use C4::Reserves qw( GetReserveFee ChargeReserveFee AddReserve );
31 use Koha::Database;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
36 my $builder = t::lib::TestBuilder->new();
37 my $library = $builder->build({
38     source => 'Branch',
39 });
40 my $mContext = Test::MockModule->new('C4::Context');
41 $mContext->mock( 'userenv', sub {
42     return { branch => $library->{branchcode} };
43 });
44
45 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
46
47 # Category with hold fee, two patrons
48 $builder->build({
49     source => 'Category',
50     value  => {
51         categorycode          => 'XYZ1',
52         reservefee            => 2,
53     },
54 });
55 $builder->build({
56     source => 'Category',
57     value  => {
58         categorycode          => 'XYZ2',
59         reservefee            => 0,
60     },
61 });
62 my $patron1 = $builder->build({
63     source => 'Borrower',
64     value  => {
65         categorycode => 'XYZ1',
66     },
67 });
68 my $patron2 = $builder->build({
69     source => 'Borrower',
70     value  => {
71         categorycode => 'XYZ1',
72     },
73 });
74 my $patron3 = $builder->build({
75     source => 'Borrower',
76 });
77 my $patron4 = $builder->build({
78     source => 'Borrower',
79     value  => {
80         categorycode => 'XYZ2',
81     },
82 });
83
84 # One biblio and two items
85 my $biblio = $builder->build_sample_biblio;
86 my $item1 = $builder->build_sample_item(
87     {
88         biblionumber => $biblio->biblionumber,
89         notforloan   => 0,
90     }
91 );
92 my $item2 = $builder->build_sample_item(
93     {
94         biblionumber => $biblio->biblionumber,
95         notforloan   => 0,
96     }
97 );
98
99 subtest 'GetReserveFee' => sub {
100     plan tests => 6;
101
102     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
103     C4::Circulation::AddIssue( $patron3, $item2->barcode, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
104     my $acc2 = acctlines( $patron2->{borrowernumber} );
105     my $res1 = addreserve( $patron1->{borrowernumber} );
106
107     t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
108     my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
109     is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
110     C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->title );
111     is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
112
113     # If we delete the reserve, there should be no charge
114     $dbh->do( "DELETE FROM reserves WHERE borrowernumber = ?", undef, ( $patron1->{borrowernumber}) );
115     $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
116     is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
117
118     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
119     $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
120     is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
121
122     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
123     $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
124     is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
125
126     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
127     $fee = C4::Reserves::GetReserveFee( $patron4->{borrowernumber}, $biblio->biblionumber );
128     is( $fee, 0, 'HoldFeeMode=any_time_is_placed ; fee == 0, Patron 4 should not be charged' );
129 };
130
131 subtest 'Integration with AddReserve' => sub {
132     plan tests => 2;
133
134     my $dbh = C4::Context->dbh;
135
136     subtest 'Items are not issued' => sub {
137         plan tests => 3;
138
139         t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
140         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
141         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
142         addreserve( $patron1->{borrowernumber} );
143         is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - No fee charged for patron 1 if not issued' );
144
145         t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
146         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
147         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
148         addreserve( $patron1->{borrowernumber} );
149         is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_placed - Patron should be always charged' );
150
151         t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
152         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
153         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
154         addreserve( $patron1->{borrowernumber} );
155         is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when placing a hold' );
156     };
157
158     subtest 'Items are issued' => sub {
159         plan tests => 4;
160
161         $dbh->do( "DELETE FROM issues       WHERE itemnumber=?", undef, $item1->itemnumber);
162         $dbh->do( "DELETE FROM issues       WHERE itemnumber=?", undef, $item2->itemnumber);
163         C4::Circulation::AddIssue( $patron2, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
164
165         t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
166         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
167         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
168         addreserve( $patron1->{borrowernumber} );
169         is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if items are not all checked out' );
170
171         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
172         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
173         addreserve( $patron3->{borrowernumber} );
174         addreserve( $patron1->{borrowernumber} );
175         is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if all the items are not checked out, even if 1 hold is already placed' );
176
177         C4::Circulation::AddIssue( $patron3, $item2->barcode, '2015-12-31', 0, undef, 0, {} );
178         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
179         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
180         addreserve( $patron1->{borrowernumber} );
181         is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if all items are checked out but no holds are placed' );
182
183         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
184         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
185         addreserve( $patron3->{borrowernumber} );
186         addreserve( $patron1->{borrowernumber} );
187         is( acctlines( $patron1->{borrowernumber} ), 1, 'not_always - Patron should only be charged if all items are checked out and at least 1 hold is already placed' );
188     };
189 };
190
191 subtest 'Integration with AddIssue' => sub {
192     plan tests => 5;
193
194     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
195     $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
196     $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
197
198     t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
199     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
200     is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
201
202     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
203     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
204     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
205     is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
206
207     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
208     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
209     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
210     is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when checking out an item which was not placed hold for him' );
211
212     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
213     my $id = addreserve( $patron1->{borrowernumber} );
214     is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged yet (just checking to make sure)');
215     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
216     is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_collected - Patron should not be charged when checking out an item which was not placed hold for him' );
217 };
218
219 sub acctlines { #calculate number of accountlines for a patron
220     my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
221     return $temp[0];
222 }
223
224 sub addreserve {
225     return AddReserve(
226         {
227             branchcode     => $library->{branchcode},
228             borrowernumber => $_[0],
229             biblionumber   => $biblio->biblionumber,
230             priority       => '1',
231             title          => $biblio->title,
232         }
233     );
234 }
235
236 $schema->storage->txn_rollback;
237