Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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 qw( AddIssue );
30 use C4::Reserves qw( GetReserveFee ChargeReserveFee 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 = Test::MockModule->new('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_sample_biblio;
73 my $item1 = $builder->build_sample_item(
74     {
75         biblionumber => $biblio->biblionumber,
76         notforloan   => 0,
77     }
78 );
79 my $item2 = $builder->build_sample_item(
80     {
81         biblionumber => $biblio->biblionumber,
82         notforloan   => 0,
83     }
84 );
85
86 subtest 'GetReserveFee' => sub {
87     plan tests => 5;
88
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 $res1 = addreserve( $patron1->{borrowernumber} );
92
93     t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
94     my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
95     is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
96     C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->title );
97     is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
98
99     # If we delete the reserve, there should be no charge
100     $dbh->do( "DELETE FROM reserves WHERE borrowernumber = ?", undef, ( $patron1->{borrowernumber}) );
101     $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
102     is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
103
104     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
105     $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
106     is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
107
108     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
109     $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
110     is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
111 };
112
113 subtest 'Integration with AddReserve' => sub {
114     plan tests => 2;
115
116     my $dbh = C4::Context->dbh;
117
118     subtest 'Items are not issued' => sub {
119         plan tests => 3;
120
121         t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
122         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
123         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
124         addreserve( $patron1->{borrowernumber} );
125         is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - No fee charged for patron 1 if not issued' );
126
127         t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
128         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
129         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
130         addreserve( $patron1->{borrowernumber} );
131         is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_placed - Patron should be always charged' );
132
133         t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
134         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
135         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
136         addreserve( $patron1->{borrowernumber} );
137         is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when placing a hold' );
138     };
139
140     subtest 'Items are issued' => sub {
141         plan tests => 3;
142
143         C4::Circulation::AddIssue( $patron2, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
144
145         t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
146         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
147         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
148         addreserve( $patron1->{borrowernumber} );
149         is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if items are not all checked out' );
150
151         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
152         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
153         addreserve( $patron3->{borrowernumber} );
154         addreserve( $patron1->{borrowernumber} );
155         # FIXME Are we sure it's the expected behavior?
156         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' );
157
158         C4::Circulation::AddIssue( $patron3, $item2->barcode, '2015-12-31', 0, undef, 0, {} );
159         $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
160         $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
161         addreserve( $patron1->{borrowernumber} );
162         is( acctlines( $patron1->{borrowernumber} ), 1, 'not_always - Patron should be charged if all items are checked out' );
163     };
164 };
165
166 subtest 'Integration with AddIssue' => sub {
167     plan tests => 5;
168
169     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
170     $dbh->do( "DELETE FROM reserves     WHERE biblionumber=?", undef, $biblio->biblionumber );
171     $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
172
173     t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
174     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
175     is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
176
177     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
178     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
179     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
180     is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
181
182     t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
183     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
184     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
185     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' );
186
187     $dbh->do( "DELETE FROM issues       WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
188     my $id = addreserve( $patron1->{borrowernumber} );
189     is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged yet (just checking to make sure)');
190     C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
191     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' );
192 };
193
194 sub acctlines { #calculate number of accountlines for a patron
195     my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
196     return $temp[0];
197 }
198
199 sub addreserve {
200     return AddReserve(
201         {
202             branchcode     => $library->{branchcode},
203             borrowernumber => $_[0],
204             biblionumber   => $biblio->biblionumber,
205             priority       => '1',
206             title          => $biblio->title,
207         }
208     );
209 }
210
211 $schema->storage->txn_rollback;
212