Bug 20946: (QA follow-up) make outstanding_debits return the account lines only
[koha.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 => 1;
23
24 use Koha::Account;
25 use Koha::Account::Lines;
26
27 use t::lib::TestBuilder;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'outstanding_debits() tests' => sub {
33
34     plan tests => 12;
35
36     $schema->storage->txn_begin;
37
38     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
39
40     my @generated_lines;
41     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
42     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
43     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
44     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
45
46     my $account = $patron->account;
47     my $lines   = $account->outstanding_debits();
48
49     is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
50
51     my $i = 0;
52     foreach my $line ( @{ $lines->as_list } ) {
53         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
54         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
55         $i++;
56     }
57
58     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
59     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
60     my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding =>  3 })->store;
61     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
62     $lines = $patron_2->account->outstanding_debits();
63     is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
64     is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
65     my $the_line = Koha::Account::Lines->find( $just_one->id );
66     is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
67
68     my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
69     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
70     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
71     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
72     $lines = $patron_3->account->outstanding_debits();
73     is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
74     is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
75
76     my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
77     $lines = $patron_4->account->outstanding_debits();
78     is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
79     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
80
81     $schema->storage->txn_rollback;
82 };