View | Details | Raw Unified | Return to bug 14702
Collapse All | Expand All

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

Return to bug 14702