Bug 20946: Additional unit tests for 0 lines edge case
[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 => 7;
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 = Koha::Account->new({ patron_id => $patron->id });
47     my ( $total, $lines ) = $account->outstanding_debits();
48
49     is( $total, 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     ( $total, $lines ) =  Koha::Account->new({ patron_id => 'InvalidBorrowernumber' })->outstanding_debits();
59     is( $total, 0, "Total if no outstanding debits is 0" );
60     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
61
62
63     $schema->storage->txn_rollback;
64 };