Bug 19529: Prevent NoIssuesChargeGuarantees.t to fail randomly
[koha.git] / t / db_dependent / Circulation / NoIssuesChargeGuarantees.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use C4::Accounts qw( manualinvoice );
26 use C4::Circulation qw( CanBookBeIssued );
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder = t::lib::TestBuilder->new();
34
35 my $item = $builder->build(
36     {
37         source => 'Item',
38         value  => {
39             notforloan => 0,
40             withdrawn  => 0
41         }
42     }
43 );
44
45 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
46 my $patron = $builder->build(
47     {
48         source => 'Borrower',
49         value => {
50             patron_category => $patron_category->{categorycode},
51         }
52     }
53 );
54 my $guarantee = $builder->build(
55     {
56         source => 'Borrower',
57         value  => {
58             guarantorid => $patron->{borrowernumber},
59             patron_category => $patron_category->{categorycode},
60         }
61     }
62 );
63
64 t::lib::Mocks::mock_preference( 'NoIssuesChargeGuarantees', '5.00' );
65 t::lib::Mocks::mock_preference( 'AllowFineOverride', '' );
66
67 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->{barcode} );
68 is( $issuingimpossible->{DEBT_GUARANTEES}, undef, "Patron can check out item" );
69
70 manualinvoice( $guarantee->{borrowernumber}, undef, undef, 'L', 10.00 );
71 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->{barcode} );
72 is( $issuingimpossible->{DEBT_GUARANTEES} + 0, '10.00' + 0, "Patron cannot check out item due to debt for guarantee" );
73
74 my $accountline = Koha::Account::Lines->search({ borrowernumber => $guarantee->{borrowernumber} })->next();
75 is( $accountline->amountoutstanding, "10.000000", "Found 10.00 amount outstanding" );
76 is( $accountline->accounttype, "L", "Account type is L" );
77
78 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id })->next();
79 is( $offset->type, 'Manual Debit', 'Got correct offset type' );
80 is( $offset->amount, '10.000000', 'Got amount of $10.00' );
81
82 $schema->storage->txn_rollback;
83