Bug 19382: (QA follow-up) Throw exception if only_this_guarantor is set for a non...
[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 => 6;
23 use Test::Exception;
24
25 use Koha::Database;
26 use Koha::DateUtils qw(dt_from_string);
27 use Koha::Patrons;
28 use Koha::Patron::Relationships;
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'add_guarantor() tests' => sub {
37
38     plan tests => 6;
39
40     $schema->storage->txn_begin;
41
42     t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
43
44     my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
45     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
46
47     throws_ok
48         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); }
49         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
50         'Exception is thrown as no relationship passed';
51
52     is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
53
54     throws_ok
55         { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father' }); }
56         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
57         'Exception is thrown as a wrong relationship was passed';
58
59     is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
60
61     $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' });
62
63     my $guarantors = $patron_1->guarantor_relationships;
64
65     is( $guarantors->count, 1, 'No guarantors added' );
66
67     {
68         local *STDERR;
69         open STDERR, '>', '/dev/null';
70         throws_ok
71             { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father2' }); }
72             'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
73             'Exception is thrown for duplicated relationship';
74         close STDERR;
75     }
76
77     $schema->storage->txn_rollback;
78 };
79
80 subtest 'relationships_debt() tests' => sub {
81
82     plan tests => 168;
83
84     $schema->storage->txn_begin;
85
86     t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' );
87
88     my $parent_1 = $builder->build_object({ class => 'Koha::Patrons' });
89     my $parent_2 = $builder->build_object({ class => 'Koha::Patrons' });
90     my $child_1 = $builder->build_object({ class => 'Koha::Patrons' });
91     my $child_2 = $builder->build_object({ class => 'Koha::Patrons' });
92
93     $child_1->add_guarantor({ guarantor_id => $parent_1->borrowernumber, relationship => 'parent' });
94     $child_1->add_guarantor({ guarantor_id => $parent_2->borrowernumber, relationship => 'parent' });
95     $child_2->add_guarantor({ guarantor_id => $parent_1->borrowernumber, relationship => 'parent' });
96     $child_2->add_guarantor({ guarantor_id => $parent_2->borrowernumber, relationship => 'parent' });
97
98     is( $child_1->guarantor_relationships->guarantors->count, 2, 'Child 1 has correct number of guarantors' );
99     is( $child_2->guarantor_relationships->guarantors->count, 2, 'Child 2 has correct number of guarantors' );
100     is( $parent_1->guarantee_relationships->guarantees->count, 2, 'Parent 1 has correct number of guarantors' );
101     is( $parent_2->guarantee_relationships->guarantees->count, 2, 'Parent 2 has correct number of guarantors' );
102
103     # 3 params, count from 0 to 6 in binary ( 3 places ) to get the set of switches, then do that 4 times, one for each parent and child
104     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
105     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
106     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
107     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
108     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
109     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
110     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
111     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
112     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
113     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
114     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
115     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
116     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
117     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
118     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
119     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
120     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
121     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
122     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
123     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
124
125     throws_ok {
126         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
127     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
128     throws_ok {
129         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
130     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
131     throws_ok {
132         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
133     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
134     throws_ok {
135         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
136     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
137
138     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
139     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
140     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
141     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
142
143     throws_ok {
144         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
145     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
146     throws_ok {
147         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
148     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
149     throws_ok {
150         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
151     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
152     throws_ok {
153         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
154     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
155
156     $child_2->account->add_debit({ type => 'ACCOUNT', amount => 10, interface => 'commandline' });
157     is( $child_2->account->non_issues_charges, 10, 'Child 2 owes correct amount' );
158
159     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
160     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
161     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
162     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
163     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
164     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
165     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
166     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
167     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
168     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
169     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
170     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
171     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
172     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
173     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
174     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
175     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
176     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
177     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
178     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
179
180     throws_ok {
181         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
182     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
183     throws_ok {
184         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
185     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
186     throws_ok {
187         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
188     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
189     throws_ok {
190         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
191     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
192
193     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
194     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
195     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
196     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
197
198     throws_ok {
199         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
200     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
201     throws_ok {
202         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
203     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
204     throws_ok {
205         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
206     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
207     throws_ok {
208         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
209     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
210
211     $parent_1->account->add_debit({ type => 'ACCOUNT', amount => 10, interface => 'commandline' });
212     is( $parent_1->account->non_issues_charges, 10, 'Parent 1 owes correct amount' );
213
214     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
215     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
216     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
217     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
218     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
219     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
220     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
221     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
222     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
223     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
224     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
225     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
226     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
227     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
228     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
229     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
230     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
231     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
232     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
233     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
234
235     throws_ok {
236         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
237     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
238     throws_ok {
239         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
240     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
241     throws_ok {
242         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
243     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
244     throws_ok {
245         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
246     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
247
248     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
249     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
250     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
251     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
252
253     throws_ok {
254         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
255     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
256     throws_ok {
257         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
258     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
259     throws_ok {
260         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
261     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
262     throws_ok {
263         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
264     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
265
266     $parent_2->account->add_debit({ type => 'ACCOUNT', amount => 10, interface => 'commandline' });
267     is( $parent_2->account->non_issues_charges, 10, 'Parent 2 owes correct amount' );
268
269     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
270     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
271     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
272     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 30, 'Family debt is correct' );
273     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
274     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
275     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
276     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
277     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
278     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
279     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
280     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 30, 'Family debt is correct' );
281     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
282     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
283     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 10, 'Family debt is correct' );
284     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 20, 'Family debt is correct' );
285     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
286     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
287     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 30, 'Family debt is correct' );
288     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 30, 'Family debt is correct' );
289
290     throws_ok {
291         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
292     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
293     throws_ok {
294         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
295     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
296     throws_ok {
297         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
298     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
299     throws_ok {
300         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
301     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
302
303     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
304     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 10, 'Family debt is correct' );
305     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
306     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 30, 'Family debt is correct' );
307
308     throws_ok {
309         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
310     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
311     throws_ok {
312         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
313     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
314     throws_ok {
315         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
316     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
317     throws_ok {
318         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
319     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
320
321     $child_1->account->add_debit({ type => 'ACCOUNT', amount => 10, interface => 'commandline' });
322     is( $child_1->account->non_issues_charges, 10, 'Child 1 owes correct amount' );
323
324     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 20, 'Family debt is correct' );
325     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 20, 'Family debt is correct' );
326     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 30, 'Family debt is correct' );
327     is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 40, 'Family debt is correct' );
328     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 20, 'Family debt is correct' );
329     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 20, 'Family debt is correct' );
330     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
331     is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 30, 'Family debt is correct' );
332     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 20, 'Family debt is correct' );
333     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 20, 'Family debt is correct' );
334     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 30, 'Family debt is correct' );
335     is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 40, 'Family debt is correct' );
336     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 20, 'Family debt is correct' );
337     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 20, 'Family debt is correct' );
338     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 20, 'Family debt is correct' );
339     is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 30, 'Family debt is correct' );
340     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
341     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 20, 'Family debt is correct' );
342     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 30, 'Family debt is correct' );
343     is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 40, 'Family debt is correct' );
344
345     throws_ok {
346         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
347     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
348     throws_ok {
349         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
350     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
351     throws_ok {
352         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
353     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
354     throws_ok {
355         is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
356     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
357
358     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 10, 'Family debt is correct' );
359     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 20, 'Family debt is correct' );
360     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 30, 'Family debt is correct' );
361     is( $child_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 40, 'Family debt is correct' );
362
363     throws_ok {
364         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
365     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
366     throws_ok {
367         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
368     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
369     throws_ok {
370         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
371     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
372     throws_ok {
373         is( $child_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 10, 'Family debt is correct' );
374     } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
375
376     $schema->storage->txn_rollback;
377 };
378
379 subtest 'add_enrolment_fee_if_needed() tests' => sub {
380
381     plan tests => 2;
382
383     subtest 'category has enrolment fee' => sub {
384         plan tests => 7;
385
386         $schema->storage->txn_begin;
387
388         my $category = $builder->build_object(
389             {
390                 class => 'Koha::Patron::Categories',
391                 value => {
392                     enrolmentfee => 20
393                 }
394             }
395         );
396
397         my $patron = $builder->build_object(
398             {
399                 class => 'Koha::Patrons',
400                 value => {
401                     categorycode => $category->categorycode
402                 }
403             }
404         );
405
406         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
407         is( $enrollment_fee * 1, 20, 'Enrolment fee amount is correct' );
408         my $account = $patron->account;
409         is( $patron->account->balance * 1, 20, 'Patron charged the enrolment fee' );
410         # second enrolment fee, new
411         $enrollment_fee = $patron->add_enrolment_fee_if_needed(0);
412         # third enrolment fee, renewal
413         $enrollment_fee = $patron->add_enrolment_fee_if_needed(1);
414         is( $patron->account->balance * 1, 60, 'Patron charged the enrolment fees' );
415
416         my @debits = $account->outstanding_debits;
417         is( scalar @debits, 3, '3 enrolment fees' );
418         is( $debits[0]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
419         is( $debits[1]->debit_type_code, 'ACCOUNT', 'Account type set correctly' );
420         is( $debits[2]->debit_type_code, 'ACCOUNT_RENEW', 'Account type set correctly' );
421
422         $schema->storage->txn_rollback;
423     };
424
425     subtest 'no enrolment fee' => sub {
426
427         plan tests => 3;
428
429         $schema->storage->txn_begin;
430
431         my $category = $builder->build_object(
432             {
433                 class => 'Koha::Patron::Categories',
434                 value => {
435                     enrolmentfee => 0
436                 }
437             }
438         );
439
440         my $patron = $builder->build_object(
441             {
442                 class => 'Koha::Patrons',
443                 value => {
444                     categorycode => $category->categorycode
445                 }
446             }
447         );
448
449         my $enrollment_fee = $patron->add_enrolment_fee_if_needed();
450         is( $enrollment_fee * 1, 0, 'No enrolment fee' );
451         my $account = $patron->account;
452         is( $patron->account->balance, 0, 'Patron not charged anything' );
453
454         my @debits = $account->outstanding_debits;
455         is( scalar @debits, 0, 'no debits' );
456
457         $schema->storage->txn_rollback;
458     };
459 };
460
461 subtest 'to_api() tests' => sub {
462
463     plan tests => 6;
464
465     $schema->storage->txn_begin;
466
467     my $patron_class = Test::MockModule->new('Koha::Patron');
468     $patron_class->mock(
469         'algo',
470         sub { return 'algo' }
471     );
472
473     my $patron = $builder->build_object(
474         {
475             class => 'Koha::Patrons',
476             value => {
477                 debarred => undef
478             }
479         }
480     );
481
482     my $restricted = $patron->to_api->{restricted};
483     ok( defined $restricted, 'restricted is defined' );
484     ok( !$restricted, 'debarred is undef, restricted evaluates to false' );
485
486     $patron->debarred( dt_from_string->add( days => 1 ) )->store->discard_changes;
487     $restricted = $patron->to_api->{restricted};
488     ok( defined $restricted, 'restricted is defined' );
489     ok( $restricted, 'debarred is defined, restricted evaluates to true' );
490
491     my $patron_json = $patron->to_api({ embed => { algo => {} } });
492     ok( exists $patron_json->{algo} );
493     is( $patron_json->{algo}, 'algo' );
494
495     $schema->storage->txn_rollback;
496 };
497
498 subtest 'login_attempts tests' => sub {
499     plan tests => 1;
500
501     $schema->storage->txn_begin;
502
503     my $patron = $builder->build_object(
504         {
505             class => 'Koha::Patrons',
506         }
507     );
508     my $patron_info = $patron->unblessed;
509     $patron->delete;
510     delete $patron_info->{login_attempts};
511     my $new_patron = Koha::Patron->new($patron_info)->store;
512     is( $new_patron->discard_changes->login_attempts, 0, "login_attempts defaults to 0 as expected");
513
514     $schema->storage->txn_rollback;
515 };
516
517 subtest 'is_superlibrarian() tests' => sub {
518
519     plan tests => 3;
520
521     $schema->storage->txn_begin;
522
523     my $patron = $builder->build_object(
524         {
525             class => 'Koha::Patrons',
526
527             value => {
528                 flags => 16
529             }
530         }
531     );
532
533     is( $patron->is_superlibrarian, 0, 'Patron is not a superlibrarian and the method returns the correct value' );
534
535     $patron->flags(1)->store->discard_changes;
536     is( $patron->is_superlibrarian, 1, 'Patron is a superlibrarian and the method returns the correct value' );
537
538     $patron->flags(0)->store->discard_changes;
539     is( $patron->is_superlibrarian, 0, 'Patron is not a superlibrarian and the method returns the correct value' );
540
541     $schema->storage->txn_rollback;
542 };