]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Reserves/GetReserveFee.t
Bug 14878: Tests - Create the branchcodes when needed
[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 => 5;
25 use Test::MockModule;
26 use t::lib::TestBuilder;
27
28 use C4::Circulation;
29 use C4::Reserves qw|AddReserve|;
30 use Koha::Database;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new();
36 my $library = $builder->build({
37     source => 'Branch',
38 });
39 my $mContext = new Test::MockModule('C4::Context');
40 $mContext->mock( 'userenv', sub {
41     return { branch => $library->{branchcode} };
42 });
43
44 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
45
46 # Category with hold fee, two patrons
47 $builder->build({
48     source => 'Category',
49     value  => {
50         categorycode          => 'XYZ1',
51         reservefee            => 2.5,
52     },
53 });
54 my $patron1 = $builder->build({
55     source => 'Borrower',
56     value  => {
57         categorycode => 'XYZ1',
58     },
59 });
60 my $patron2 = $builder->build({
61     source => 'Borrower',
62     value  => {
63         categorycode => 'XYZ1',
64     },
65 });
66
67 # One biblio and two items
68 my $biblio = $builder->build({
69     source => 'Biblio',
70     value  => {
71         title => 'Title 1',
72     },
73 });
74 my $item1 = $builder->build({
75     source => 'Item',
76     value  => {
77         biblionumber => $biblio->{biblionumber},
78     },
79 });
80 my $item2 = $builder->build({
81     source => 'Item',
82     value  => {
83         biblionumber => $biblio->{biblionumber},
84     },
85 });
86
87
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' );
94
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' );
103
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' );
108
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' );
113 # End of tests
114
115 sub acctlines { #calculate number of accountlines for a patron
116     my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
117     return $temp[0];
118 }
119
120 sub addreserve {
121     return AddReserve(
122         $library->{branchcode},
123         $_[0],
124         $biblio->{biblionumber},
125         undef,
126         '1',
127         undef,
128         undef,
129         '',
130         $biblio->{title},
131         undef,
132         ''
133     );
134 }
135
136 $schema->storage->txn_rollback;
137
138 1;