Bug 17560: Update current code
[koha.git] / t / db_dependent / Reserves / GetReserveFee.t
blobb86204f06edb509fb9992da32a7b230849390176
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 => 2;
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 });
68 # One biblio and two items
69 my $biblio = $builder->build({
70 source => 'Biblio',
71 value => {
72 title => 'Title 1',
74 });
75 my $item1 = $builder->build({
76 source => 'Item',
77 value => {
78 biblionumber => $biblio->{biblionumber},
80 });
81 my $item2 = $builder->build({
82 source => 'Item',
83 value => {
84 biblionumber => $biblio->{biblionumber},
86 });
89 # Actual testing starts here!
90 # Add reserve for patron1, no fee expected
91 # Note: AddReserve calls GetReserveFee and ChargeReserveFee
92 my $acc1 = acctlines( $patron1->{borrowernumber} );
93 my $res1 = addreserve( $patron1->{borrowernumber} );
94 is( acctlines( $patron1->{borrowernumber} ), $acc1, 'No fee charged for patron 1' );
96 subtest 'GetReserveFee' => sub {
97 plan tests => 7;
99 # Issue item1 to patron1. Since there is still a reserve too, we should
100 # expect a charge for patron2.
101 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
102 my $acc2 = acctlines( $patron2->{borrowernumber} );
104 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
105 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
106 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
107 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->{title} );
108 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
110 # If we delete the reserve, there should be no charge
111 $dbh->do( "DELETE FROM reserves WHERE reserve_id=?", undef, ( $res1 ) );
112 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
113 is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
115 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
116 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
117 is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
119 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
120 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
121 is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
123 # If we delete the second item, there should be a charge
124 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
125 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, ( $item2->{itemnumber} ) );
126 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
127 is( int($fee), 2, 'Patron 2 should be charged again this time' );
129 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
130 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
131 is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
134 sub acctlines { #calculate number of accountlines for a patron
135 my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
136 return $temp[0];
139 sub addreserve {
140 return AddReserve(
141 $library->{branchcode},
142 $_[0],
143 $biblio->{biblionumber},
144 undef,
145 '1',
146 undef,
147 undef,
149 $biblio->{title},
150 undef,
155 $schema->storage->txn_rollback;