Bug 14878: Tests - Create the branchcodes when needed
[koha.git] / t / db_dependent / Reserves / GetReserveFee.t
blob2a7a50cf83618cc96b0609ecd0d8ee70b6891dbb
1 #!/usr/bin/perl
3 # This script includes tests for GetReserveFee and ChargeReserveFee
5 # Copyright 2015 Rijksmuseum
7 # This file is part of Koha.
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.
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.
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>.
22 use Modern::Perl;
24 use Test::More tests => 5;
25 use Test::MockModule;
26 use t::lib::TestBuilder;
28 use C4::Circulation;
29 use C4::Reserves qw|AddReserve|;
30 use Koha::Database;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
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 });
44 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
46 # Category with hold fee, two patrons
47 $builder->build({
48 source => 'Category',
49 value => {
50 categorycode => 'XYZ1',
51 reservefee => 2.5,
53 });
54 my $patron1 = $builder->build({
55 source => 'Borrower',
56 value => {
57 categorycode => 'XYZ1',
59 });
60 my $patron2 = $builder->build({
61 source => 'Borrower',
62 value => {
63 categorycode => 'XYZ1',
65 });
67 # One biblio and two items
68 my $biblio = $builder->build({
69 source => 'Biblio',
70 value => {
71 title => 'Title 1',
73 });
74 my $item1 = $builder->build({
75 source => 'Item',
76 value => {
77 biblionumber => $biblio->{biblionumber},
79 });
80 my $item2 = $builder->build({
81 source => 'Item',
82 value => {
83 biblionumber => $biblio->{biblionumber},
85 });
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' );
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' );
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' );
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
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];
120 sub addreserve {
121 return AddReserve(
122 $library->{branchcode},
123 $_[0],
124 $biblio->{biblionumber},
125 undef,
126 '1',
127 undef,
128 undef,
130 $biblio->{title},
131 undef,
136 $schema->storage->txn_rollback;