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

(-)a/t/db_dependent/Koha/Patron.t (-280 / +104 lines)
Lines 85-94 subtest 'relationships_debt() tests' => sub { Link Here
85
85
86
    t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' );
86
    t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' );
87
87
88
    my $parent_1 = $builder->build_object({ class => 'Koha::Patrons' });
88
    my $parent_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Parent 1" } });
89
    my $parent_2 = $builder->build_object({ class => 'Koha::Patrons' });
89
    my $parent_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Parent 2" } });
90
    my $child_1 = $builder->build_object({ class => 'Koha::Patrons' });
90
    my $child_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Child 1" } });
91
    my $child_2 = $builder->build_object({ class => 'Koha::Patrons' });
91
    my $child_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Child 2" } });
92
92
93
    $child_1->add_guarantor({ guarantor_id => $parent_1->borrowernumber, relationship => 'parent' });
93
    $child_1->add_guarantor({ guarantor_id => $parent_1->borrowernumber, relationship => 'parent' });
94
    $child_1->add_guarantor({ guarantor_id => $parent_2->borrowernumber, relationship => 'parent' });
94
    $child_1->add_guarantor({ guarantor_id => $parent_2->borrowernumber, relationship => 'parent' });
Lines 97-381 subtest 'relationships_debt() tests' => sub { Link Here
97
97
98
    is( $child_1->guarantor_relationships->guarantors->count, 2, 'Child 1 has correct number of guarantors' );
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' );
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' );
100
    is( $parent_1->guarantee_relationships->guarantees->count, 2, 'Parent 1 has correct number of guarantees' );
101
    is( $parent_2->guarantee_relationships->guarantees->count, 2, 'Parent 2 has correct number of guarantors' );
101
    is( $parent_2->guarantee_relationships->guarantees->count, 2, 'Parent 2 has correct number of guarantees' );
102
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
103
    my $patrons = [ $parent_1, $parent_2, $child_1, $child_2 ];
104
    is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
104
105
    is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
105
    # First test: No debt
106
    is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
106
    my ($parent1_debt, $parent2_debt, $child1_debt, $child2_debt) = (0,0,0,0);
107
    is( $parent_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
107
    _test_combinations($patrons, $parent1_debt,$parent2_debt,$child1_debt,$child2_debt);
108
    is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
108
109
    is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
109
    # Add debt to child_2
110
    is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
110
    $child2_debt = 2;
111
    is( $parent_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
111
    $child_2->account->add_debit({ type => 'ACCOUNT', amount => $child2_debt, interface => 'commandline' });
112
    is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
112
    is( $child_2->account->non_issues_charges, $child2_debt, 'Debt added to Child 2' );
113
    is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
113
    _test_combinations($patrons, $parent1_debt,$parent2_debt,$child1_debt,$child2_debt);
114
    is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
114
115
    is( $parent_2->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
115
    $parent1_debt = 3;
116
    is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
116
    $parent_1->account->add_debit({ type => 'ACCOUNT', amount => $parent1_debt, interface => 'commandline' });
117
    is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
117
    is( $parent_1->account->non_issues_charges, $parent1_debt, 'Debt added to Parent 1' );
118
    is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
118
    _test_combinations($patrons, $parent1_debt,$parent2_debt,$child1_debt,$child2_debt);
119
    is( $parent_2->relationships_debt({ only_this_guarantor => 1, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
119
120
    is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
120
    $parent2_debt = 5;
121
    is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 0, include_this_patron => 1 }), 0, 'Family debt is correct' );
121
    $parent_2->account->add_debit({ type => 'ACCOUNT', amount => $parent2_debt, interface => 'commandline' });
122
    is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 0 }), 0, 'Family debt is correct' );
122
    is( $parent_2->account->non_issues_charges, $parent2_debt, 'Parent 2 owes correct amount' );
123
    is( $child_1->relationships_debt({ only_this_guarantor => 0, include_guarantors => 1, include_this_patron => 1 }), 0, 'Family debt is correct' );
123
    _test_combinations($patrons, $parent1_debt,$parent2_debt,$child1_debt,$child2_debt);
124
124
125
    throws_ok {
125
    $child1_debt = 7;
126
        is( $child_1->relationships_debt({ only_this_guarantor => 1, include_guarantors => 0, include_this_patron => 0 }), 0, 'Family debt is correct' );
126
    $child_1->account->add_debit({ type => 'ACCOUNT', amount => $child1_debt, interface => 'commandline' });
127
    } 'Koha::Exceptions::BadParameter', 'Exception is thrown as patron is not a guarantor';
127
    is( $child_1->account->non_issues_charges, $child1_debt, 'Child 1 owes correct amount' );
128
    throws_ok {
128
    _test_combinations($patrons, $parent1_debt,$parent2_debt,$child1_debt,$child2_debt);
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
129
376
    $schema->storage->txn_rollback;
130
    $schema->storage->txn_rollback;
377
};
131
};
378
132
133
sub _test_combinations {
134
    my ( $patrons, $parent1_debt, $parent2_debt, $child1_debt, $child2_debt ) = @_;
135
136
    # Options
137
    # P1 => P1 + C1 + C2 ( - P1 ) ( + P2 )
138
    # P2 => P2 + C1 + C2 ( - P2 ) ( + P1 )
139
    # C1 => P1 + P2 + C1 + C2 ( - C1 )
140
    # C2 => P1 + P2 + C1 + C2 ( - C2 )
141
142
# 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
143
    for my $i ( 0 .. 7 ) {
144
        my ( $only_this_guarantor, $include_guarantors, $include_this_patron )
145
          = split '', sprintf( "%03b", $i );
146
        for my $patron ( @$patrons ) {
147
            if ( $only_this_guarantor
148
                && !$patron->guarantee_relationships->count )
149
            {
150
                throws_ok {
151
                    $patron->relationships_debt(
152
                        {
153
                            only_this_guarantor => $only_this_guarantor,
154
                            include_guarantors  => $include_guarantors,
155
                            include_this_patron => $include_this_patron
156
                        }
157
                    );
158
                }
159
                'Koha::Exceptions::BadParameter',
160
                  'Exception is thrown as patron is not a guarantor';
161
162
            }
163
            else {
164
165
                my $debt = 0;
166
                if ( $patron->firstname eq 'Parent 1' ) {
167
                    $debt += $parent1_debt if ($include_this_patron && $include_guarantors);
168
                    $debt += $child1_debt + $child2_debt;
169
                    $debt += $parent2_debt unless ($only_this_guarantor || !$include_guarantors);
170
                }
171
                elsif ( $patron->firstname eq 'Parent 2' ) {
172
                    $debt += $parent2_debt if ($include_this_patron & $include_guarantors);
173
                    $debt += $child1_debt + $child2_debt;
174
                    $debt += $parent1_debt unless ($only_this_guarantor || !$include_guarantors);
175
                }
176
                elsif ( $patron->firstname eq 'Child 1' ) {
177
                    $debt += $child1_debt if ($include_this_patron);
178
                    $debt += $child2_debt;
179
                    $debt += $parent1_debt + $parent2_debt if ($include_guarantors);
180
                }
181
                else {
182
                    $debt += $child2_debt if ($include_this_patron);
183
                    $debt += $child1_debt;
184
                    $debt += $parent1_debt + $parent2_debt if ($include_guarantors);
185
                }
186
187
                is(
188
                    $patron->relationships_debt(
189
                        {
190
                            only_this_guarantor => $only_this_guarantor,
191
                            include_guarantors  => $include_guarantors,
192
                            include_this_patron => $include_this_patron
193
                        }
194
                    ),
195
                    $debt,
196
                    $patron->firstname
197
                      . " debt of $debt calculated correctly for ( only_this_guarantor: $only_this_guarantor, include_guarantors: $include_guarantors, include_this_patron: $include_this_patron)"
198
                );
199
            }
200
        }
201
    }
202
}
203
379
subtest 'add_enrolment_fee_if_needed() tests' => sub {
204
subtest 'add_enrolment_fee_if_needed() tests' => sub {
380
205
381
    plan tests => 2;
206
    plan tests => 2;
382
- 

Return to bug 19382