Bug 18309: Add UNIMARC field 214 and its subfields
[koha.git] / t / db_dependent / Reserves / GetReserveFee.t
blob80d428b0b528fe829d2dca8abea25895aa7e6941
1 #!/usr/bin/perl
3 # This script includes tests for GetReserveFee and ChargeReserveFee
5 # Copyright 2015 Rijksmuseum
7 # This file is part of Koha.
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.
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.
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>.
22 use Modern::Perl;
24 use Test::More tests => 3;
25 use Test::MockModule;
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
29 use C4::Circulation;
30 use C4::Reserves qw|AddReserve|;
31 use Koha::Database;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new();
37 my $library = $builder->build({
38 source => 'Branch',
39 });
40 my $mContext = new Test::MockModule('C4::Context');
41 $mContext->mock( 'userenv', sub {
42 return { branch => $library->{branchcode} };
43 });
45 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
47 # Category with hold fee, two patrons
48 $builder->build({
49 source => 'Category',
50 value => {
51 categorycode => 'XYZ1',
52 reservefee => 2,
54 });
55 my $patron1 = $builder->build({
56 source => 'Borrower',
57 value => {
58 categorycode => 'XYZ1',
60 });
61 my $patron2 = $builder->build({
62 source => 'Borrower',
63 value => {
64 categorycode => 'XYZ1',
66 });
67 my $patron3 = $builder->build({
68 source => 'Borrower',
69 });
71 # One biblio and two items
72 my $biblio = $builder->build({
73 source => 'Biblio',
74 value => {
75 title => 'Title 1',
77 });
78 my $item1 = $builder->build({
79 source => 'Item',
80 value => {
81 biblionumber => $biblio->{biblionumber},
82 notforloan => 0,
84 });
85 my $item2 = $builder->build({
86 source => 'Item',
87 value => {
88 biblionumber => $biblio->{biblionumber},
89 notforloan => 0,
91 });
93 subtest 'GetReserveFee' => sub {
94 plan tests => 5;
96 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
97 my $acc2 = acctlines( $patron2->{borrowernumber} );
98 my $res1 = addreserve( $patron1->{borrowernumber} );
100 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
101 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
102 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
103 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->{title} );
104 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
106 # If we delete the reserve, there should be no charge
107 $dbh->do( "DELETE FROM reserves WHERE borrowernumber = ?", undef, ( $patron1->{borrowernumber}) );
108 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
109 is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
111 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
112 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
113 is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
115 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
116 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
117 is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
120 subtest 'Integration with AddReserve' => sub {
121 plan tests => 2;
123 my $dbh = C4::Context->dbh;
125 subtest 'Items are not issued' => sub {
126 plan tests => 3;
128 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
129 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
130 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
131 addreserve( $patron1->{borrowernumber} );
132 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - No fee charged for patron 1 if not issued' );
134 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
135 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
136 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
137 addreserve( $patron1->{borrowernumber} );
138 is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_placed - Patron should be always charged' );
140 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
141 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
142 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
143 addreserve( $patron1->{borrowernumber} );
144 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when placing a hold' );
147 subtest 'Items are issued' => sub {
148 plan tests => 3;
150 C4::Circulation::AddIssue( $patron2, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
152 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
153 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
154 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
155 addreserve( $patron1->{borrowernumber} );
156 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if items are not all checked out' );
158 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
159 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
160 addreserve( $patron3->{borrowernumber} );
161 addreserve( $patron1->{borrowernumber} );
162 # FIXME Are we sure it's the expected behavior?
163 is( acctlines( $patron1->{borrowernumber} ), 1, 'not_always - Patron should be charged if all the items are not checked out and at least 1 hold is already placed' );
165 C4::Circulation::AddIssue( $patron3, $item2->{barcode}, '2015-12-31', 0, undef, 0, {} );
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} ), 1, 'not_always - Patron should be charged if all items are checked out' );
173 subtest 'Integration with AddIssue' => sub {
174 plan tests => 5;
176 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
177 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
178 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
180 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
181 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
182 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
184 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
185 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
186 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
187 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
189 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
190 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
191 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
192 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' );
194 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
195 my $id = addreserve( $patron1->{borrowernumber} );
196 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged yet (just checking to make sure)');
197 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
198 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' );
201 sub acctlines { #calculate number of accountlines for a patron
202 my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
203 return $temp[0];
206 sub addreserve {
207 return AddReserve(
208 $library->{branchcode},
209 $_[0],
210 $biblio->{biblionumber},
211 undef,
212 '1',
213 undef,
214 undef,
216 $biblio->{title},
217 undef,
222 $schema->storage->txn_rollback;