Bug 23049: Update existing code to use debit_type
[koha.git] / t / db_dependent / Koha / Patron.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 2;
23 use Test::Exception;
24
25 use Koha::Database;
26 use Koha::Patrons;
27 use Koha::Patron::Relationships;
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'add_guarantor() tests' => sub {
36
37     plan tests => 6;
38
39     $schema->storage->txn_begin;
40
41     t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
42
43     my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
44     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
45
46     throws_ok
47         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); }
48         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
49         'Exception is thrown as no relationship passed';
50
51     is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
52
53     throws_ok
54         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father' }); }
55         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
56         'Exception is thrown as a wrong relationship was passed';
57
58     is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
59
60     $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' });
61
62     my $guarantors = $patron_1->guarantor_relationships;
63
64     is( $guarantors->count, 1, 'No guarantors added' );
65
66     $SIG{__WARN__} = sub {}; # FIXME: PrintError = 0 not working!
67
68     throws_ok
69         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father2' }); }
70         'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
71         'Exception is thrown for duplicated relationship';
72
73     $schema->storage->txn_rollback;
74 };
75
76 subtest 'add_enrolment_fee_if_needed() tests' => sub {
77
78     plan tests => 2;
79
80     subtest 'category has enrolment fee' => sub {
81         plan tests => 7;
82
83         $schema->storage->txn_begin;
84
85         my $category = $builder->build_object(
86             {
87                 class => 'Koha::Patron::Categories',
88                 value => {
89                     enrolmentfee => 20
90                 }
91             }
92         );
93
94         my $patron = $builder->build_object(
95             {
96                 class => 'Koha::Patrons',
97                 value => {
98                     categorycode => $category->categorycode
99                 }
100             }
101         );
102
103         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
104         is( $enrollment_fee * 1, 20, 'Enrolment fee amount is correct' );
105         my $account = $patron->account;
106         is( $patron->account->balance * 1, 20, 'Patron charged the enrolment fee' );
107         # second enrolment fee, new
108         $enrollment_fee = $patron->add_enrolment_fee_if_needed(0);
109         # third enrolment fee, renewal
110         $enrollment_fee = $patron->add_enrolment_fee_if_needed(1);
111         is( $patron->account->balance * 1, 60, 'Patron charged the enrolment fees' );
112
113         my @debits = $account->outstanding_debits;
114         is( scalar @debits, 3, '3 enrolment fees' );
115         is( $debits[0]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
116         is( $debits[1]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
117         is( $debits[2]->debit_type_code, 'ACCOUNT_RENEW', 'Account type set correctly' );
118
119         $schema->storage->txn_rollback;
120     };
121
122     subtest 'no enrolment fee' => sub {
123
124         plan tests => 3;
125
126         $schema->storage->txn_begin;
127
128         my $category = $builder->build_object(
129             {
130                 class => 'Koha::Patron::Categories',
131                 value => {
132                     enrolmentfee => 0
133                 }
134             }
135         );
136
137         my $patron = $builder->build_object(
138             {
139                 class => 'Koha::Patrons',
140                 value => {
141                     categorycode => $category->categorycode
142                 }
143             }
144         );
145
146         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
147         is( $enrollment_fee * 1, 0, 'No enrolment fee' );
148         my $account = $patron->account;
149         is( $patron->account->balance, 0, 'Patron not charged anything' );
150
151         my @debits = $account->outstanding_debits;
152         is( scalar @debits, 0, 'no debits' );
153
154         $schema->storage->txn_rollback;
155     };
156 };