Bug 20997: Add unit tests for Koha::Account::Line::apply
[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 => 3;
25 use Test::MockModule;
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use C4::Circulation;
30 use C4::Reserves qw|AddReserve|;
31 use Koha::Database;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
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 });
44
45 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
46
47 # Category with hold fee, two patrons
48 $builder->build({
49     source => 'Category',
50     value  => {
51         categorycode          => 'XYZ1',
52         reservefee            => 2,
53     },
54 });
55 my $patron1 = $builder->build({
56     source => 'Borrower',
57     value  => {
58         categorycode => 'XYZ1',
59     },
60 });
61 my $patron2 = $builder->build({
62     source => 'Borrower',
63     value  => {
64         categorycode => 'XYZ1',
65     },
66 });
67 my $patron3 = $builder->build({
68     source => 'Borrower',
69 });
70
71 # One biblio and two items
72 my $biblio = $builder->build({
73     source => 'Biblio',
74     value  => {
75         title => 'Title 1',
76     },
77 });
78 my $item1 = $builder->build({
79     source => 'Item',
80     value  => {
81         biblionumber => $biblio->{biblionumber},
82         notforloan => 0,
83     },
84 });
85 my $item2 = $builder->build({
86     source => 'Item',
87     value  => {
88         biblionumber => $biblio->{biblionumber},
89         notforloan => 0,
90     },
91 });
92
93 subtest 'GetReserveFee' => sub {
94     plan tests => 5;
95
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} );
99
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' );
105
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' );
110
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' );
114
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' );
118 };
119
120 subtest 'Integration with AddReserve' => sub {
121     plan tests => 2;
122
123     my $dbh = C4::Context->dbh;
124
125     subtest 'Items are not issued' => sub {
126         plan tests => 3;
127
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' );
133
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' );
139
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' );
145     };
146
147     subtest 'Items are issued' => sub {
148         plan tests => 3;
149
150         C4::Circulation::AddIssue( $patron2, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
151
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' );
157
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' );
164
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' );
170     };
171 };
172
173 subtest 'Integration with AddIssue' => sub {
174     plan tests => 5;
175
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} );
179
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' );
183
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' );
188
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' );
193
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' );
199 };
200
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];
204 }
205
206 sub addreserve {
207     return AddReserve(
208         $library->{branchcode},
209         $_[0],
210         $biblio->{biblionumber},
211         undef,
212         '1',
213         undef,
214         undef,
215         '',
216         $biblio->{title},
217         undef,
218         ''
219     );
220 }
221
222 $schema->storage->txn_rollback;
223