Bug 14702: Unit tests for GetReserveFee and ChargeReservesFee
[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 use Test::More tests => 5;
24 use Test::MockModule;
25
26 use C4::Circulation;
27 use C4::Reserves qw|AddReserve|;
28 use t::lib::TestBuilder;
29
30 my $mContext = new Test::MockModule('C4::Context');
31 $mContext->mock( 'userenv', sub {
32     return { branch => 'CPL' };
33 });
34
35 my $builder = t::lib::TestBuilder->new();
36 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
37
38 # Category with hold fee, two patrons
39 $builder->build({
40     source => 'Category',
41     value  => {
42         categorycode          => 'XYZ1',
43         reservefee            => 2.5,
44     },
45 });
46 my $patron1 = $builder->build({
47     source => 'Borrower',
48     value  => {
49         categorycode => 'XYZ1',
50     },
51 });
52 my $patron2 = $builder->build({
53     source => 'Borrower',
54     value  => {
55         categorycode => 'XYZ1',
56     },
57 });
58
59 # One biblio and two items
60 my $biblio = $builder->build({
61     source => 'Biblio',
62     value  => {
63         title => 'Title 1',
64     },
65 });
66 my $item1 = $builder->build({
67     source => 'Item',
68     value  => {
69         biblionumber => $biblio->{biblionumber},
70     },
71 });
72 my $item2 = $builder->build({
73     source => 'Item',
74     value  => {
75         biblionumber => $biblio->{biblionumber},
76     },
77 });
78
79
80 # Actual testing starts here!
81 # Add reserve for patron1, no fee expected
82 # Note: AddReserve calls GetReserveFee and ChargeReserveFee
83 my $acc1 = acctlines( $patron1->{borrowernumber} );
84 my $res1 = addreserve( $patron1->{borrowernumber} );
85 is( acctlines( $patron1->{borrowernumber} ), $acc1, 'No fee charged for patron 1' );
86
87 # Issue item1 to patron1. Since there is still a reserve too, we should
88 # expect a charge for patron2.
89 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
90 my $acc2 = acctlines( $patron2->{borrowernumber} );
91 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
92 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
93 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->{title} );
94 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
95
96 # If we delete the reserve, there should be no charge
97 $dbh->do( "DELETE FROM reserves WHERE reserve_id=?", undef, ( $res1 ) );
98 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
99 is( $fee, 0, 'Patron 2 will not be charged now' );
100
101 # If we delete the second item, there should be a charge
102 $dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, ( $item2->{itemnumber} ) );
103 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
104 is( $fee > 0, 1, 'Patron 2 should be charged again this time' );
105 # End of tests
106
107
108 sub acctlines { #calculate number of accountlines for a patron
109     my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
110     return $temp[0];
111 }
112
113 sub addreserve {
114     return AddReserve(
115         'CPL',
116         $_[0],
117         $biblio->{biblionumber},
118         undef,
119         '1',
120         undef,
121         undef,
122         '',
123         $biblio->{title},
124         undef,
125         ''
126     );
127 }