Bug 32351: Fix t/db_dependent/Circulation/NoIssuesChargeGuarantees.t
[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::Circulation qw( CanBookBeIssued );
26 use Koha::Account;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29 use Koha::Patron::Relationship;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder = t::lib::TestBuilder->new();
35
36 my $item = $builder->build_sample_item;
37
38 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
39 my $patron = $builder->build_object(
40     {
41         class => 'Koha::Patrons',
42         value => {
43             categorycode => $patron_category->{categorycode},
44         }
45     }
46 );
47
48 my $guarantee = $builder->build_object(
49     {
50         class => 'Koha::Patrons',
51         value => {
52             categorycode => $patron_category->{categorycode},
53         }
54     }
55 );
56
57 my $r = $builder->build_object(
58     {
59         class => 'Koha::Patron::Relationships',
60         value => {
61             guarantor_id => $patron->id,
62             guarantee_id => $guarantee->id,
63             relationship => 'parent',
64         }
65     }
66 );
67
68 t::lib::Mocks::mock_preference( 'NoIssuesChargeGuarantees', '5.00' );
69 t::lib::Mocks::mock_preference( 'AllowFineOverride', '' );
70
71 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode );
72 is( $issuingimpossible->{DEBT_GUARANTEES}, undef, "Patron can check out item" );
73
74 my $account = Koha::Account->new( { patron_id => $guarantee->id } );
75 $account->add_debit({ amount => 10.00, type => 'LOST', interface => 'test' });
76 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode );
77 is( $issuingimpossible->{DEBT_GUARANTEES} + 0, '10.00' + 0, "Patron cannot check out item due to debt for guarantee" );
78
79 my $accountline = Koha::Account::Lines->search({ borrowernumber => $guarantee->id })->next();
80 is( $accountline->amountoutstanding+0, 10, "Found 10.00 amount outstanding" );
81 is( $accountline->debit_type_code, "LOST", "Debit type is LOST" );
82
83 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id })->next();
84 is( $offset->type, 'CREATE', 'Got CREATE offset type' );
85 is( $offset->amount+0, 10, 'Got amount of $10.00' );
86
87 $schema->storage->txn_rollback;
88