Bug 23051: Add unit tests
authorAndrew Isherwood <andrew.isherwood@ptfs-europe.com>
Wed, 12 Jun 2019 14:18:54 +0000 (15:18 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 6 Mar 2020 10:01:00 +0000 (10:01 +0000)
This patch adds unit tests for all modules affected by this bug

Sponsored-by: Loughborough University

Signed-off-by: Lucy Harrison <L.M.Harrison@lboro.ac.uk>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

t/db_dependent/Circulation.t
t/db_dependent/Koha/Account.t
t/db_dependent/Koha/Account/Line.t
t/db_dependent/Koha/Account/Lines.t

index 121c176..1cbfe51 100755 (executable)
@@ -3419,6 +3419,15 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
     is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
     is( $new_stats_size, $old_stats_size + 1, 'renew statistic successfully added with passed branch' );
 
+    AddReturn( $item->id, $library->id, undef, $date );
+    AddIssue( $patron->unblessed, $item->barcode, dt_from_string() );
+    AddRenewal( $patron->id, $item->id, $library->id, undef, undef, 1 );
+    my $lines_skipped = Koha::Account::Lines->search({
+        borrowernumber => $patron->id,
+        itemnumber     => $item->id
+    });
+    is( $lines_skipped->count, 5, 'Passing skipfinecalc causes fine calculation on renewal to be skipped' );
+
 };
 
 subtest 'ProcessOfflinePayment() tests' => sub {
index 5437f16..baeb005 100755 (executable)
@@ -23,10 +23,12 @@ use Test::More tests => 11;
 use Test::MockModule;
 use Test::Exception;
 
+use DateTime;
+
 use Koha::Account;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
-
+use Koha::DateUtils qw( dt_from_string );
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -673,6 +675,9 @@ subtest 'pay() tests' => sub {
 
     $schema->storage->txn_begin;
 
+    # Disable renewing upon fine payment
+    t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
+
     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
     my $library = $builder->build_object({ class => 'Koha::Libraries' });
     my $account = $patron->account;
@@ -897,6 +902,70 @@ subtest 'pay() handles lost items when paying by amount ( not specifying the los
     $schema->storage->txn_rollback;
 };
 
+subtest 'pay() renews items when appropriate' => sub {
+
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $account = $patron->account;
+
+    my $context = Test::MockModule->new('C4::Context');
+    $context->mock( 'userenv', { branch => $library->id } );
+
+    my $biblio = $builder->build_sample_biblio();
+    my $item =
+      $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
+
+    my $now = dt_from_string();
+    my $seven_weeks = DateTime::Duration->new(weeks => 7);
+    my $five_weeks = DateTime::Duration->new(weeks => 5);
+    my $seven_weeks_ago = $now - $seven_weeks;
+    my $five_weeks_ago = $now - $five_weeks;
+
+    my $checkout = Koha::Checkout->new(
+        {
+            borrowernumber => $patron->id,
+            itemnumber     => $item->id,
+            date_due       => $five_weeks_ago,
+            branchcode     => $patron->branchcode,
+            issuedate      => $seven_weeks_ago
+        }
+    )->store();
+
+    my $accountline = Koha::Account::Line->new(
+        {
+            issue_id       => $checkout->id,
+            borrowernumber => $patron->id,
+            itemnumber     => $item->id,
+            date           => \'NOW()',
+            accounttype    => 'OVERDUE',
+            status         => 'UNRETURNED',
+            interface      => 'cli',
+            amount => '1',
+            amountoutstanding => '1',
+        }
+    )->store();
+
+    # Enable renewing upon fine payment
+    t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
+    my $called = 0;
+    my $module = new Test::MockModule('C4::Circulation');
+    $module->mock('AddRenewal', sub { $called = 1; });
+    $account->pay(
+        {
+            amount     => '1',
+            library_id => $library->id,
+        }
+    );
+
+    is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
 
     plan tests => 4;
index 1c28f56..99d0d73 100644 (file)
@@ -21,12 +21,16 @@ use Modern::Perl;
 
 use Test::More tests => 11;
 use Test::Exception;
+use Test::MockModule;
+
+use DateTime;
 
 use C4::Circulation qw/AddIssue AddReturn/;
 use Koha::Account;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
 use Koha::Items;
+use Koha::DateUtils qw( dt_from_string );
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -132,7 +136,7 @@ subtest 'is_credit() and is_debit() tests' => sub {
 
 subtest 'apply() tests' => sub {
 
-    plan tests => 24;
+    plan tests => 25;
 
     $schema->storage->txn_begin;
 
@@ -243,6 +247,52 @@ subtest 'apply() tests' => sub {
     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
     is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
 
+    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $biblio = $builder->build_sample_biblio();
+    my $item =
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
+    my $now = dt_from_string();
+    my $seven_weeks = DateTime::Duration->new(weeks => 7);
+    my $five_weeks = DateTime::Duration->new(weeks => 5);
+    my $seven_weeks_ago = $now - $seven_weeks;
+    my $five_weeks_ago = $now - $five_weeks;
+
+    my $checkout = Koha::Checkout->new(
+        {
+            borrowernumber => $patron->id,
+            itemnumber     => $item->id,
+            date_due       => $five_weeks_ago,
+            branchcode     => $library->id,
+            issuedate      => $seven_weeks_ago
+        }
+    )->store();
+
+    my $accountline = Koha::Account::Line->new(
+        {
+            issue_id       => $checkout->id,
+            borrowernumber => $patron->id,
+            itemnumber     => $item->id,
+            branchcode     => $library->id,
+            date           => \'NOW()',
+            accounttype    => 'OVERDUE',
+            status         => 'UNRETURNED',
+            interface      => 'cli',
+            amount => '1',
+            amountoutstanding => '1',
+        }
+    )->store();
+
+    # Enable renewing upon fine payment
+    t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
+    my $called = 0;
+    my $module = new Test::MockModule('C4::Circulation');
+    $module->mock('AddRenewal', sub { $called = 1; });
+    my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
+    my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id });
+    $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
+
+    is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
+
     $schema->storage->txn_rollback;
 };
 
@@ -286,7 +336,7 @@ subtest 'Keep account info when related patron, staff, item or cash_register is
 
     $patron->delete;
     $line = $line->get_from_storage;
-    is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
+    is( $line->borro1wernumber, undef, "The account line should not be deleted when the related patron is delete");
 
     $register->delete;
     $line = $line->get_from_storage;
index afc88fc..c9151f4 100755 (executable)
@@ -21,6 +21,9 @@ use Modern::Perl;
 
 use Test::More tests => 4;
 use Test::Exception;
+use Test::MockModule;
+
+use DateTime;
 
 use Koha::Account;
 use Koha::Account::Lines;