Bug 20997: Add unit tests for Koha::Account::Line::apply
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 26 Jun 2018 14:51:41 +0000 (11:51 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 10 Aug 2018 12:30:32 +0000 (12:30 +0000)
This patch adds unit tests for the mentioned method.

To test:
- Apply this patch
- Run:
  $ khell
 k$ prove t/db_dependent/Koha/Account/Lines.t
=> FAIL: The method is not already implemented!

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
t/db_dependent/Koha/Account/Lines.t

index 11c8df1..da09ef5 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 2;
+use Test::More tests => 3;
+use Test::Exception;
 
+use Koha::Account;
 use Koha::Account::Lines;
+use Koha::Account::Offsets;
 use Koha::Items;
 
 use t::lib::TestBuilder;
@@ -29,7 +32,7 @@ use t::lib::TestBuilder;
 my $schema = Koha::Database->new->schema;
 my $builder = t::lib::TestBuilder->new;
 
-subtest 'item' => sub {
+subtest 'item() tests' => sub {
 
     plan tests => 2;
 
@@ -63,7 +66,7 @@ subtest 'item' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'total_outstanding' => sub {
+subtest 'total_outstanding() tests' => sub {
 
     plan tests => 5;
 
@@ -128,3 +131,99 @@ subtest 'total_outstanding' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'is_credit() tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $account = $patron->account;
+
+    my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
+
+    ok( $credit->is_credit, 'is_credit detects credits' );
+
+    my $debit = Koha::Account::Line->new(
+    {
+        borrowernumber => $patron->id,
+        accounttype    => "F",
+        amount         => 10,
+    })->store;
+
+    ok( !$debit->is_credit, 'is_credit() detects debits' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'apply() tests' => sub {
+
+    plan tests => 12;
+
+    $schema->storage->txn_begin;
+
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $account = $patron->account;
+
+    my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
+
+    my $debit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "F",
+            amount            => 10,
+            amountoutstanding => 10
+        }
+    )->store;
+
+    my $debit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "F",
+            amount            => 100,
+            amountoutstanding => 100
+        }
+    )->store;
+
+    $credit->discard_changes;
+    $debit_1->discard_changes;
+
+    my $remaining_credit = $credit->apply( { debit => $debit_1, offset_type => 'Manual Credit' } );
+    is( $remaining_credit, 90, 'Remaining credit is correctly calculated' );
+    $credit->discard_changes;
+    is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
+
+    # re-read debit info
+    $debit_1->discard_changes;
+    is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
+
+    my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
+    is( $offsets->count, 1, 'Only one offset is generated' );
+    my $THE_offest = $offsets->next;
+    is( $THE_offest->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' );
+    is( $THE_offest->type, 'Manual Credit', 'Passed type stored correctly' );
+
+    $remaining_credit = $credit->apply( { debit => $debit_2, offset_type => 'Manual Credit' } );
+    is( $remaining_credit, 0, 'No remaining credit left' );
+    $credit->discard_changes;
+    is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
+    $debit_2->discard_changes;
+    is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
+
+    throws_ok
+        { $debit_1->apply({ debit => $credit }); }
+        'Koha::Exceptions::Account::IsNotCredit',
+        '->apply() can only be used with credits';
+
+    throws_ok
+        { $credit->apply({ debit => $credit }); }
+        'Koha::Exceptions::Account::IsNotDebit',
+        '->apply() can only be applied to credits';
+
+    throws_ok
+        { $credit->apply({ debit => $debit_1 }); }
+        'Koha::Exceptions::Account::NoAvailableCredit',
+        '->apply() can only be used with outstanding credits';
+
+
+    $schema->storage->txn_rollback;
+};