|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Test::More tests => 4; |
| 5 |
use Test::MockModule; |
| 6 |
|
| 7 |
use C4::Circulation; |
| 8 |
use C4::Reserves qw|AddReserve|; |
| 9 |
use t::lib::TestBuilder; |
| 10 |
|
| 11 |
my $mContext = new Test::MockModule('C4::Context'); |
| 12 |
$mContext->mock( 'userenv', sub { |
| 13 |
return { branch => 'CPL' }; |
| 14 |
}); |
| 15 |
|
| 16 |
my $builder = t::lib::TestBuilder->new(); |
| 17 |
my $dbh = C4::Context->dbh; # after start transaction of testbuilder |
| 18 |
|
| 19 |
# Category with hold fee, two patrons |
| 20 |
$builder->build({ |
| 21 |
source => 'Category', |
| 22 |
value => { |
| 23 |
categorycode => 'XYZ1', |
| 24 |
reservefee => 2.5, |
| 25 |
}, |
| 26 |
}); |
| 27 |
my $patron1 = $builder->build({ |
| 28 |
source => 'Borrower', |
| 29 |
value => { |
| 30 |
categorycode => 'XYZ1', |
| 31 |
}, |
| 32 |
}); |
| 33 |
my $patron2 = $builder->build({ |
| 34 |
source => 'Borrower', |
| 35 |
value => { |
| 36 |
categorycode => 'XYZ1', |
| 37 |
}, |
| 38 |
}); |
| 39 |
|
| 40 |
# One biblio and two items |
| 41 |
my $biblio = $builder->build({ |
| 42 |
source => 'Biblio', |
| 43 |
value => { |
| 44 |
title => 'Title 1', |
| 45 |
}, |
| 46 |
}); |
| 47 |
my $item1 = $builder->build({ |
| 48 |
source => 'Item', |
| 49 |
value => { |
| 50 |
biblionumber => $biblio->{biblionumber}, |
| 51 |
}, |
| 52 |
}); |
| 53 |
my $item2 = $builder->build({ |
| 54 |
source => 'Item', |
| 55 |
value => { |
| 56 |
biblionumber => $biblio->{biblionumber}, |
| 57 |
}, |
| 58 |
}); |
| 59 |
|
| 60 |
# Add reserve for patron1, no fee expected |
| 61 |
my $acc1 = acctlines( $patron1->{borrowernumber} ); |
| 62 |
my $res1 = addreserve( $patron1->{borrowernumber} ); |
| 63 |
is( acctlines( $patron1->{borrowernumber} ), $acc1, 'No fee charged for patron 1' ); |
| 64 |
|
| 65 |
# Issue item1 to patron1. Since there is still a reserve too, we should |
| 66 |
# expect a charge for patron2. |
| 67 |
C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter |
| 68 |
my $acc2 = acctlines( $patron1->{borrowernumber} ); |
| 69 |
C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber}, $biblio->{title} ); |
| 70 |
is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 charged' ); |
| 71 |
|
| 72 |
# If we delete the reserve, there should be no charge |
| 73 |
$dbh->do( "DELETE FROM reserves WHERE reserve_id=?", undef, ( $res1 ) ); |
| 74 |
C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber}, $biblio->{title} ); |
| 75 |
is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 not charged' ); |
| 76 |
|
| 77 |
# If we delete the second item, there should be a charge |
| 78 |
$dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, ( $item2->{itemnumber} ) ); |
| 79 |
C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber}, $biblio->{title} ); |
| 80 |
is( acctlines( $patron2->{borrowernumber} ), $acc2 + 2, 'Patron 2 charged again' ); |
| 81 |
# End of tests |
| 82 |
|
| 83 |
sub acctlines { |
| 84 |
my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) ); |
| 85 |
return $temp[0]; |
| 86 |
} |
| 87 |
|
| 88 |
sub addreserve { |
| 89 |
return AddReserve( |
| 90 |
'CPL', |
| 91 |
$_[0], |
| 92 |
$biblio->{biblionumber}, |
| 93 |
undef, |
| 94 |
'1', |
| 95 |
undef, |
| 96 |
undef, |
| 97 |
'', |
| 98 |
$biblio->{title}, |
| 99 |
undef, |
| 100 |
'' |
| 101 |
); |
| 102 |
} |