From 4196d3e02d9ee22233e16984f0359adb6a0d34af Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Mon, 28 Sep 2020 15:12:45 +0100 Subject: [PATCH] Bug 24190: (follow-up) Add tests In response to Jonathan's request for tests, I have now added additional tests for the methods in C4/Budgets.pm that do logging --- t/db_dependent/Budgets.t | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index bf3855db01..19af6d2452 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -1,6 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 144; +use Test::More tests => 147; +use JSON; BEGIN { use_ok('C4::Budgets') @@ -9,6 +10,7 @@ use C4::Context; use C4::Biblio; use C4::Acquisition; +use Koha::ActionLogs; use Koha::Acquisition::Booksellers; use Koha::Acquisition::Orders; use Koha::Acquisition::Funds; @@ -24,6 +26,7 @@ use YAML; use t::lib::Mocks; t::lib::Mocks::mock_preference('OrderPriceRounding',''); +t::lib::Mocks::mock_preference('AcqLog','1'); my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -127,10 +130,11 @@ $bpid = AddBudgetPeriod($my_budgetperiod); #this is an active budget my $my_budget = { budget_code => 'ABCD', budget_amount => '123.132000', + budget_expend => '789', budget_name => 'Periodiques', budget_notes => 'This is a note', budget_period_id => $bpid, - budget_encumb => '', # Bug 21604 + budget_encumb => '456', # Bug 21604 }; my $budget_id = AddBudget($my_budget); isnt( $budget_id, undef, 'AddBudget does not returns undef' ); @@ -141,10 +145,25 @@ is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the bud is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' ); is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' ); +my @create_logs = Koha::ActionLogs->find({ module =>'ACQUISITIONS', action => 'CREATE_FUND', object => $budget->{budget_id} }); + +my $expected_create_payload = { + budget_amount => $my_budget->{budget_amount}, + budget_expend => $my_budget->{budget_expend}, + budget_encumb => $my_budget->{budget_encumb}, +}; + +my $actual_create_payload = from_json($create_logs[0]->info); + +is_deeply ($actual_create_payload, $expected_create_payload, 'ModBudget logs a budget creation with the correct payload'); + +my $before = $budget; $my_budget = { budget_code => 'EFG', budget_amount => '321.231000', + budget_encumb => '567', + budget_expend => '890', budget_name => 'Modified name', budget_notes => 'This is a modified note', budget_period_id => $bpid, @@ -162,6 +181,18 @@ is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the bu is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' ); is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' ); +my @mod_logs = Koha::ActionLogs->find({ module =>'ACQUISITIONS', action => 'MODIFY_FUND', object => $budget->{budget_id} }); +my $expected_mod_payload = { + budget_amount_new => $my_budget->{budget_amount}, + budget_encumb_new => $my_budget->{budget_encumb}, + budget_expend_new => $my_budget->{budget_expend}, + budget_amount_old => $before->{budget_amount}, + budget_encumb_old => $before->{budget_encumb}, + budget_expend_old => $before->{budget_expend}, + budget_amount_change => 0 - ($before->{budget_amount} - $my_budget->{budget_amount}) +}; +my $actual_mod_payload = from_json($mod_logs[0]->info); +is_deeply ($actual_mod_payload, $expected_mod_payload, 'ModBudget logs a budget modification with the correct payload'); $budgets = GetBudgets(); is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' ); @@ -221,6 +252,9 @@ is( DelBudget($budget_id), 1, 'DelBudget returns true' ); $budgets = GetBudgets(); is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' ); +my @delete_logs = Koha::ActionLogs->find({ module =>'ACQUISITIONS', action => 'DELETE_FUND', object => $budget_id }); + +is (scalar @delete_logs, 1, 'DelBudget logs a budget deletion'); # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered my $budget_period_total = 10_000; -- 2.20.1