From 0f05ffd0b39acc14ffc81c09b5aeff453c125328 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Wed, 12 Jun 2019 15:18:54 +0100 Subject: [PATCH] Bug 23051: Add unit tests This patch adds unit tests for all modules affected by this bug --- t/db_dependent/Circulation.t | 11 +++++- t/db_dependent/Koha/Account.t | 72 ++++++++++++++++++++++++++++++++++++- t/db_dependent/Koha/Account/Lines.t | 52 ++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index b7acf2a617..610be3812d 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -3093,7 +3093,7 @@ subtest 'AddReturn should clear items.onloan for unissued items' => sub { subtest 'AddRenewal and AddIssuingCharge tests' => sub { - plan tests => 10; + plan tests => 11; t::lib::Mocks::mock_preference('item-level_itypes', 1); @@ -3176,6 +3176,15 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub { $new_log_size = Koha::ActionLogs->count( \%params_renewal ); is( $new_log_size, $old_log_size + 1, 'renew log successfully added' ); + 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 { diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index e3b7472f7a..2b60d3ba7d 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -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; @@ -564,6 +566,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; @@ -581,6 +586,7 @@ subtest 'pay() tests' => sub { is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' ); + $schema->storage->txn_rollback; }; @@ -728,6 +734,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; diff --git a/t/db_dependent/Koha/Account/Lines.t b/t/db_dependent/Koha/Account/Lines.t index 9b8e4fa724..d3f7a2d24e 100755 --- a/t/db_dependent/Koha/Account/Lines.t +++ b/t/db_dependent/Koha/Account/Lines.t @@ -21,12 +21,16 @@ use Modern::Perl; use Test::More tests => 9; 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; @@ -209,7 +213,7 @@ subtest 'is_credit() and is_debit() tests' => sub { subtest 'apply() tests' => sub { - plan tests => 24; + plan tests => 25; $schema->storage->txn_begin; @@ -320,6 +324,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; }; -- 2.11.0