Bug 21909: Unit tests
[srvgit] / t / db_dependent / Koha / Account.t
index 8a92c24..c828469 100755 (executable)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 2;
+use Test::More tests => 5;
 
 use Koha::Account;
 use Koha::Account::Lines;
@@ -33,34 +33,38 @@ my $builder = t::lib::TestBuilder->new;
 
 subtest 'outstanding_debits() tests' => sub {
 
-    plan tests => 12;
+    plan tests => 22;
 
     $schema->storage->txn_begin;
 
-    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $account = $patron->account;
 
     my @generated_lines;
-    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
-    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
-    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
-    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
 
-    my $account = $patron->account;
-    my $lines   = $account->outstanding_debits();
+    my $lines     = $account->outstanding_debits();
+    my @lines_arr = $account->outstanding_debits();
 
+    is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
     is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
 
     my $i = 0;
     foreach my $line ( @{ $lines->as_list } ) {
         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
+        is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
+        is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
         $i++;
     }
 
     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
-    my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding =>  3 })->store;
-    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
+    my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => 3, amountoutstanding =>  3 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => -6, amountoutstanding => -6 })->store;
     $lines = $patron_2->account->outstanding_debits();
     is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
     is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
@@ -68,24 +72,74 @@ subtest 'outstanding_debits() tests' => sub {
     is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
 
     my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
-    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
-    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
-    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => -2,   amountoutstanding => -2 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => -20,  amountoutstanding => -20 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => -200, amountoutstanding => -200 })->store;
     $lines = $patron_3->account->outstanding_debits();
     is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
     is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
 
-    my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
-    $lines = $patron_4->account->outstanding_debits();
+    my $patron_4  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $account_4 = $patron_4->account;
+    $lines = $account_4->outstanding_debits();
     is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
 
+    # create a pathological credit with amountoutstanding > 0 (BZ 14591)
+    Koha::Account::Line->new({ borrowernumber => $patron_4->id, amount => -3, amountoutstanding => 3 })->store();
+    $lines = $account_4->outstanding_debits();
+    is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'outstanding_credits() tests' => sub {
+
+    plan tests => 17;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $account = $patron->account;
+
+    my @generated_lines;
+    push @generated_lines, $account->add_credit({ amount => 1 });
+    push @generated_lines, $account->add_credit({ amount => 2 });
+    push @generated_lines, $account->add_credit({ amount => 3 });
+    push @generated_lines, $account->add_credit({ amount => 4 });
+
+    my $lines     = $account->outstanding_credits();
+    my @lines_arr = $account->outstanding_credits();
+
+    is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
+    is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
+
+    my $i = 0;
+    foreach my $line ( @{ $lines->as_list } ) {
+        my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
+        is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
+        is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
+        is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
+        $i++;
+    }
+
+    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
+    $account  = $patron_2->account;
+    $lines       = $account->outstanding_credits();
+    is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
+    is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
+
+    # create a pathological debit with amountoutstanding < 0 (BZ 14591)
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => 2, amountoutstanding => -3 })->store();
+    $lines = $account->outstanding_credits();
+    is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
+
     $schema->storage->txn_rollback;
 };
 
 subtest 'add_credit() tests' => sub {
 
-    plan tests => 13;
+    plan tests => 15;
 
     $schema->storage->txn_begin;
 
@@ -153,8 +207,200 @@ subtest 'add_credit() tests' => sub {
         }
     );
 
-    is( $schema->resultset('ActionLog')->count(), 2, 'Log was added' );
-    is( $schema->resultset('Statistic')->count(), 2, 'No action added to statistics, because of credit type' );
+    is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
+    is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'lines() tests' => sub {
+
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $account = $patron->account;
+
+    my @generated_lines;
+
+    # Add Credits
+    push @generated_lines, $account->add_credit({ amount => 1 });
+    push @generated_lines, $account->add_credit({ amount => 2 });
+    push @generated_lines, $account->add_credit({ amount => 3 });
+    push @generated_lines, $account->add_credit({ amount => 4 });
+
+    # Add Debits
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
+
+    # Paid Off
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 0 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 0 })->store;
+
+    my $lines = $account->lines;
+    is( $lines->_resultset->count, 10, "All accountlines (debits, credits and paid off) were fetched");
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'reconcile_balance' => sub {
+
+    plan tests => 4;
+
+    subtest 'more credit than debit' => sub {
+
+        plan tests => 6;
+
+        $schema->storage->txn_begin;
+
+        my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+        my $account = $patron->account;
+
+        # Add Credits
+        $account->add_credit({ amount => 1 });
+        $account->add_credit({ amount => 2 });
+        $account->add_credit({ amount => 3 });
+        $account->add_credit({ amount => 4 });
+        $account->add_credit({ amount => 5 });
+
+        # Add Debits TODO: replace for calls to add_debit when time comes
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
+
+        # Paid Off
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+
+        is( $account->balance(), -5, "Account balance is -5" );
+        is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
+        is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
+
+        $account->reconcile_balance();
+
+        is( $account->balance(), -5, "Account balance is -5" );
+        is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
+        is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
+
+        $schema->storage->txn_rollback;
+    };
+
+    subtest 'same debit as credit' => sub {
+
+        plan tests => 6;
+
+        $schema->storage->txn_begin;
+
+        my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+        my $account = $patron->account;
+
+        # Add Credits
+        $account->add_credit({ amount => 1 });
+        $account->add_credit({ amount => 2 });
+        $account->add_credit({ amount => 3 });
+        $account->add_credit({ amount => 4 });
+
+        # Add Debits TODO: replace for calls to add_debit when time comes
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
+
+        # Paid Off
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+
+        is( $account->balance(), 0, "Account balance is 0" );
+        is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
+        is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
+
+        $account->reconcile_balance();
+
+        is( $account->balance(), 0, "Account balance is 0" );
+        is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
+        is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
+
+        $schema->storage->txn_rollback;
+    };
+
+    subtest 'more debit than credit' => sub {
+
+        plan tests => 6;
+
+        $schema->storage->txn_begin;
+
+        my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+        my $account = $patron->account;
+
+        # Add Credits
+        $account->add_credit({ amount => 1 });
+        $account->add_credit({ amount => 2 });
+        $account->add_credit({ amount => 3 });
+        $account->add_credit({ amount => 4 });
+
+        # Add Debits TODO: replace for calls to add_debit when time comes
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 5, amountoutstanding => 5 })->store;
+
+        # Paid Off
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+        Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+
+        is( $account->balance(), 5, "Account balance is 5" );
+        is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
+        is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
+
+        $account->reconcile_balance();
+
+        is( $account->balance(), 5, "Account balance is 5" );
+        is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
+        is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
+
+        $schema->storage->txn_rollback;
+    };
+
+    subtest 'credits are applied to older debits first' => sub {
+
+        plan tests => 9;
+
+        $schema->storage->txn_begin;
+
+        my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+        my $account = $patron->account;
+
+        # Add Credits
+        $account->add_credit({ amount => 1 });
+        $account->add_credit({ amount => 3 });
+
+        # Add Debits TODO: replace for calls to add_debit when time comes
+        my $debit_1 = Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+        my $debit_2 = Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+        my $debit_3 = Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+
+        is( $account->balance(), 2, "Account balance is 2" );
+        is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
+        is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
+
+        $account->reconcile_balance();
+
+        is( $account->balance(), 2, "Account balance is 2" );
+        is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
+        is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
+
+        $debit_1->discard_changes;
+        is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
+        $debit_2->discard_changes;
+        is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
+        $debit_3->discard_changes;
+        is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
+
+        $schema->storage->txn_rollback;
+    };
+};