Bug 22521: (RM follow-up) Adjust tests to use new accounttype
[koha-equinox.git] / t / db_dependent / Koha / Account.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 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 => 8;
23 use Test::MockModule;
24 use Test::Exception;
25
26 use Koha::Account;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29
30
31 use t::lib::Mocks;
32 use t::lib::TestBuilder;
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36 C4::Context->interface('commandline');
37
38 subtest 'new' => sub {
39
40     plan tests => 2;
41
42     $schema->storage->txn_begin;
43
44     throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
45
46     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
47     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
48     is( defined $account, 1, "Account is defined" );
49
50     $schema->storage->txn_rollback;
51 };
52
53 subtest 'outstanding_debits() tests' => sub {
54
55     plan tests => 22;
56
57     $schema->storage->txn_begin;
58
59     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
60     my $account = $patron->account;
61
62     my @generated_lines;
63     push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
64     push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
65     push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
66     push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
67
68     my $lines     = $account->outstanding_debits();
69     my @lines_arr = $account->outstanding_debits();
70
71     is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
72     is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
73
74     my $i = 0;
75     foreach my $line ( @{ $lines->as_list } ) {
76         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
77         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
78         is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
79         is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
80         $i++;
81     }
82     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
83     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2, interface => 'commandline' })->store;
84     my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => 3, amountoutstanding =>  3, interface => 'commandline' })->store;
85     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => -6, amountoutstanding => -6, interface => 'commandline' })->store;
86     $lines = $patron_2->account->outstanding_debits();
87     is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
88     is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
89     my $the_line = Koha::Account::Lines->find( $just_one->id );
90     is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
91
92     my $patron_3  = $builder->build_object({ class => 'Koha::Patrons' });
93     my $account_3 = $patron_3->account;
94     $account_3->add_credit( { amount => 2,   interface => 'commandline' } );
95     $account_3->add_credit( { amount => 20,  interface => 'commandline' } );
96     $account_3->add_credit( { amount => 200, interface => 'commandline' } );
97     $lines = $account_3->outstanding_debits();
98     is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
99     is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
100
101     my $patron_4  = $builder->build_object({ class => 'Koha::Patrons' });
102     my $account_4 = $patron_4->account;
103     $lines = $account_4->outstanding_debits();
104     is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
105     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
106
107     # create a pathological credit with amountoutstanding > 0 (BZ 14591)
108     Koha::Account::Line->new({ borrowernumber => $patron_4->id, amount => -3, amountoutstanding => 3, interface => 'commandline' })->store();
109     $lines = $account_4->outstanding_debits();
110     is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
111
112     $schema->storage->txn_rollback;
113 };
114
115 subtest 'outstanding_credits() tests' => sub {
116
117     plan tests => 17;
118
119     $schema->storage->txn_begin;
120
121     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
122     my $account = $patron->account;
123
124     my @generated_lines;
125     push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
126     push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
127     push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
128     push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
129
130     my $lines     = $account->outstanding_credits();
131     my @lines_arr = $account->outstanding_credits();
132
133     is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
134     is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
135
136     my $i = 0;
137     foreach my $line ( @{ $lines->as_list } ) {
138         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
139         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
140         is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
141         is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
142         $i++;
143     }
144
145     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
146     $account  = $patron_2->account;
147     $lines       = $account->outstanding_credits();
148     is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
149     is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
150
151     # create a pathological debit with amountoutstanding < 0 (BZ 14591)
152     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => 2, amountoutstanding => -3, interface => 'commandline' })->store();
153     $lines = $account->outstanding_credits();
154     is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
155
156     $schema->storage->txn_rollback;
157 };
158
159 subtest 'add_credit() tests' => sub {
160
161     plan tests => 16;
162
163     $schema->storage->txn_begin;
164
165     # delete logs and statistics
166     my $action_logs = $schema->resultset('ActionLog')->search()->count;
167     my $statistics = $schema->resultset('Statistic')->search()->count;
168
169     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
170     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
171
172     is( $account->balance, 0, 'Patron has no balance' );
173
174     # Disable logs
175     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
176
177     throws_ok {
178         $account->add_credit(
179             {   amount      => 25,
180                 description => 'Payment of 25',
181                 library_id  => $patron->branchcode,
182                 note        => 'not really important',
183                 type        => 'payment',
184                 user_id     => $patron->id
185             }
186         );
187     }
188     'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
189
190     my $line_1 = $account->add_credit(
191         {   amount      => 25,
192             description => 'Payment of 25',
193             library_id  => $patron->branchcode,
194             note        => 'not really important',
195             type        => 'payment',
196             user_id     => $patron->id,
197             interface   => 'commandline'
198         }
199     );
200
201     is( $account->balance, -25, 'Patron has a balance of -25' );
202     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
203     is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
204     is( $line_1->accounttype, $Koha::Account::account_type_credit->{'payment'}, 'Account type is correctly set' );
205
206     # Enable logs
207     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
208
209     my $sip_code = "1";
210     my $line_2 = $account->add_credit(
211         {   amount      => 37,
212             description => 'Payment of 37',
213             library_id  => $patron->branchcode,
214             note        => 'not really important',
215             user_id     => $patron->id,
216             sip         => $sip_code,
217             interface   => 'commandline'
218         }
219     );
220
221     is( $account->balance, -62, 'Patron has a balance of -25' );
222     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
223     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
224     is( $line_2->accounttype, $Koha::Account::account_type_credit->{'payment'} . $sip_code, 'Account type is correctly set' );
225
226     # offsets have the credit_id set to accountlines_id, and debit_id is undef
227     my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
228     my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
229
230     is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
231     is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
232     is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
233     is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
234
235     my $line_3 = $account->add_credit(
236         {   amount      => 20,
237             description => 'Manual credit applied',
238             library_id  => $patron->branchcode,
239             user_id     => $patron->id,
240             type        => 'forgiven',
241             interface   => 'commandline'
242         }
243     );
244
245     is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
246     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
247
248     $schema->storage->txn_rollback;
249 };
250
251 subtest 'add_debit() tests' => sub {
252
253     plan tests => 14;
254
255     $schema->storage->txn_begin;
256
257     # delete logs and statistics
258     my $action_logs = $schema->resultset('ActionLog')->search()->count;
259     my $statistics  = $schema->resultset('Statistic')->search()->count;
260
261     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
262     my $account =
263       Koha::Account->new( { patron_id => $patron->borrowernumber } );
264
265     is( $account->balance, 0, 'Patron has no balance' );
266
267     throws_ok {
268     $account->add_debit(
269         {
270             amount      => -5,
271             description => 'amount validation failure',
272             library_id  => $patron->branchcode,
273             note        => 'this should fail anyway',
274             type        => 'rent',
275             user_id     => $patron->id,
276             interface   => 'commandline'
277         }
278     ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
279
280     throws_ok {
281     $account->add_debit(
282         {
283             amount      => 5,
284             description => 'type validation failure',
285             library_id  => $patron->branchcode,
286             note        => 'this should fail anyway',
287             type        => 'failure',
288             user_id     => $patron->id,
289             interface   => 'commandline'
290         }
291     ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
292
293     throws_ok {
294     $account->add_debit(
295         {
296             amount      => 25,
297             description => 'Rental charge of 25',
298             library_id  => $patron->branchcode,
299             note        => 'not really important',
300             type        => 'rent',
301             user_id     => $patron->id
302         }
303     ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
304
305     # Disable logs
306     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
307
308     my $line_1 = $account->add_debit(
309         {
310             amount      => 25,
311             description => 'Rental charge of 25',
312             library_id  => $patron->branchcode,
313             note        => 'not really important',
314             type        => 'rent',
315             user_id     => $patron->id,
316             interface   => 'commandline'
317         }
318     );
319
320     is( $account->balance, 25, 'Patron has a balance of 25' );
321     is(
322         $schema->resultset('ActionLog')->count(),
323         $action_logs + 0,
324         'No log was added'
325     );
326     is(
327         $line_1->accounttype,
328         $Koha::Account::account_type_debit->{'rent'},
329         'Account type is correctly set'
330     );
331
332     # Enable logs
333     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
334
335     my $sip_code = "1";
336     my $line_2   = $account->add_debit(
337         {
338             amount      => 37,
339             description => 'Rental charge of 37',
340             library_id  => $patron->branchcode,
341             note        => 'not really important',
342             type        => 'rent',
343             user_id     => $patron->id,
344             interface   => 'commandline'
345         }
346     );
347
348     is( $account->balance, 62, 'Patron has a balance of 62' );
349     is(
350         $schema->resultset('ActionLog')->count(),
351         $action_logs + 1,
352         'Log was added'
353     );
354     is(
355         $line_2->accounttype,
356         $Koha::Account::account_type_debit->{'rent'},
357         'Account type is correctly set'
358     );
359
360     # offsets have the debit_id set to accountlines_id, and credit_id is undef
361     my $offset_1 =
362       Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
363     my $offset_2 =
364       Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
365
366     is( $offset_1->debit_id,  $line_1->id, 'debit_id is set for debit 1' );
367     is( $offset_1->credit_id, undef,       'credit_id is not set for debit 1' );
368     is( $offset_2->debit_id,  $line_2->id, 'debit_id is set for debit 2' );
369     is( $offset_2->credit_id, undef,       'credit_id is not set for debit 2' );
370
371     $schema->storage->txn_rollback;
372 };
373
374 subtest 'lines() tests' => sub {
375
376     plan tests => 1;
377
378     $schema->storage->txn_begin;
379
380     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
381     my $account = $patron->account;
382
383     # Add Credits
384     $account->add_credit({ amount => 1, interface => 'commandline' });
385     $account->add_credit({ amount => 2, interface => 'commandline' });
386     $account->add_credit({ amount => 3, interface => 'commandline' });
387     $account->add_credit({ amount => 4, interface => 'commandline' });
388
389     # Add Debits
390     $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
391     $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
392     $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
393     $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
394
395     # Paid Off
396     $account->add_credit( { amount => 1, interface => 'commandline' } )
397         ->apply( { debits => scalar $account->outstanding_debits } );
398
399     my $lines = $account->lines;
400     is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
401
402     $schema->storage->txn_rollback;
403 };
404
405 subtest 'reconcile_balance' => sub {
406
407     plan tests => 4;
408
409     subtest 'more credit than debit' => sub {
410
411         plan tests => 6;
412
413         $schema->storage->txn_begin;
414
415         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
416         my $account = $patron->account;
417
418         # Add Credits
419         $account->add_credit({ amount => 1, interface => 'commandline' });
420         $account->add_credit({ amount => 2, interface => 'commandline' });
421         $account->add_credit({ amount => 3, interface => 'commandline' });
422         $account->add_credit({ amount => 4, interface => 'commandline' });
423         $account->add_credit({ amount => 5, interface => 'commandline' });
424
425         # Add Debits
426         $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
427         $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
428         $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
429         $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
430
431         # Paid Off
432         Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
433         Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
434
435         is( $account->balance(), -5, "Account balance is -5" );
436         is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
437         is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
438
439         $account->reconcile_balance();
440
441         is( $account->balance(), -5, "Account balance is -5" );
442         is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
443         is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
444
445         $schema->storage->txn_rollback;
446     };
447
448     subtest 'same debit as credit' => sub {
449
450         plan tests => 6;
451
452         $schema->storage->txn_begin;
453
454         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
455         my $account = $patron->account;
456
457         # Add Credits
458         $account->add_credit({ amount => 1, interface => 'commandline' });
459         $account->add_credit({ amount => 2, interface => 'commandline' });
460         $account->add_credit({ amount => 3, interface => 'commandline' });
461         $account->add_credit({ amount => 4, interface => 'commandline' });
462
463         # Add Debits
464         $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
465         $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
466         $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
467         $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
468
469         # Paid Off
470         Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
471         Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
472
473         is( $account->balance(), 0, "Account balance is 0" );
474         is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
475         is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
476
477         $account->reconcile_balance();
478
479         is( $account->balance(), 0, "Account balance is 0" );
480         is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
481         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
482
483         $schema->storage->txn_rollback;
484     };
485
486     subtest 'more debit than credit' => sub {
487
488         plan tests => 6;
489
490         $schema->storage->txn_begin;
491
492         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
493         my $account = $patron->account;
494
495         # Add Credits
496         $account->add_credit({ amount => 1, interface => 'commandline' });
497         $account->add_credit({ amount => 2, interface => 'commandline' });
498         $account->add_credit({ amount => 3, interface => 'commandline' });
499         $account->add_credit({ amount => 4, interface => 'commandline' });
500
501         # Add Debits
502         $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
503         $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
504         $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
505         $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
506         $account->add_debit({ amount => 5, interface => 'commandline', type => 'overdue' });
507
508         # Paid Off
509         Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
510         Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
511
512         is( $account->balance(), 5, "Account balance is 5" );
513         is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
514         is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
515
516         $account->reconcile_balance();
517
518         is( $account->balance(), 5, "Account balance is 5" );
519         is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
520         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
521
522         $schema->storage->txn_rollback;
523     };
524
525     subtest 'credits are applied to older debits first' => sub {
526
527         plan tests => 9;
528
529         $schema->storage->txn_begin;
530
531         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
532         my $account = $patron->account;
533
534         # Add Credits
535         $account->add_credit({ amount => 1, interface => 'commandline' });
536         $account->add_credit({ amount => 3, interface => 'commandline' });
537
538         # Add Debits
539         my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
540         my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
541         my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
542
543         is( $account->balance(), 2, "Account balance is 2" );
544         is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
545         is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
546
547         $account->reconcile_balance();
548
549         is( $account->balance(), 2, "Account balance is 2" );
550         is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
551         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
552
553         $debit_1->discard_changes;
554         is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
555         $debit_2->discard_changes;
556         is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
557         $debit_3->discard_changes;
558         is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
559
560         $schema->storage->txn_rollback;
561     };
562 };
563
564 subtest 'pay() tests' => sub {
565
566     plan tests => 2;
567
568     $schema->storage->txn_begin;
569
570     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
571     my $library = $builder->build_object({ class => 'Koha::Libraries' });
572     my $account = $patron->account;
573
574     my $context = Test::MockModule->new('C4::Context');
575     $context->mock( 'userenv', { branch => $library->id } );
576
577     my $credit_1_id = $account->pay({ amount => 200 });
578     my $credit_1    = Koha::Account::Lines->find( $credit_1_id );
579
580     is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
581
582     my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id });
583     my $credit_2    = Koha::Account::Lines->find( $credit_2_id );
584
585     is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
586
587     $schema->storage->txn_rollback;
588 };