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>.
24 use Test::More tests => 5;
26 use t::lib::TestBuilder;
29 use C4::Reserves qw|AddReserve|;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new();
36 my $library = $builder->build({
39 my $mContext = new Test::MockModule('C4::Context');
40 $mContext->mock( 'userenv', sub {
41 return { branch => $library->{branchcode} };
44 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
46 # Category with hold fee, two patrons
50 categorycode => 'XYZ1',
54 my $patron1 = $builder->build({
57 categorycode => 'XYZ1',
60 my $patron2 = $builder->build({
63 categorycode => 'XYZ1',
67 # One biblio and two items
68 my $biblio = $builder->build({
74 my $item1 = $builder->build({
77 biblionumber => $biblio->{biblionumber},
80 my $item2 = $builder->build({
83 biblionumber => $biblio->{biblionumber},
88 # Actual testing starts here!
89 # Add reserve for patron1, no fee expected
90 # Note: AddReserve calls GetReserveFee and ChargeReserveFee
91 my $acc1 = acctlines( $patron1->{borrowernumber} );
92 my $res1 = addreserve( $patron1->{borrowernumber} );
93 is( acctlines( $patron1->{borrowernumber} ), $acc1, 'No fee charged for patron 1' );
95 # Issue item1 to patron1. Since there is still a reserve too, we should
96 # expect a charge for patron2.
97 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
98 my $acc2 = acctlines( $patron2->{borrowernumber} );
99 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
100 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
101 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->{title} );
102 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
104 # If we delete the reserve, there should be no charge
105 $dbh->do( "DELETE FROM reserves WHERE reserve_id=?", undef, ( $res1 ) );
106 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
107 is( $fee, 0, 'Patron 2 will not be charged now' );
109 # If we delete the second item, there should be a charge
110 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, ( $item2->{itemnumber} ) );
111 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
112 is( $fee > 0, 1, 'Patron 2 should be charged again this time' );
115 sub acctlines { #calculate number of accountlines for a patron
116 my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
122 $library->{branchcode},
124 $biblio->{biblionumber},
136 $schema->storage->txn_rollback;