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