From 5b1e890f9d357fe1c497dc1eb170b07cd28b6344 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Wed, 28 Aug 2024 14:16:24 +0100 Subject: [PATCH] ACQUI-161: ControllerUtils unit tests --- .../Acquire/Controllers/ControllerUtils.pm | 17 -- .../FundManagement/FundAllocations.pm | 2 +- .../lib/Koha/Acquire/Funds/FundAllocations.pm | 17 ++ t/Koha/FundManagement/FundAllocations.t | 110 +++++++++ t/api/ControllerUtils.t | 214 ++++++++++++++++++ t/api/FundManagement/fund_allocations.t | 2 +- 6 files changed, 343 insertions(+), 19 deletions(-) create mode 100644 t/Koha/FundManagement/FundAllocations.t create mode 100644 t/api/ControllerUtils.t diff --git a/Koha/Plugin/Acquire/Controllers/ControllerUtils.pm b/Koha/Plugin/Acquire/Controllers/ControllerUtils.pm index 69ed9d3..7d69e6e 100644 --- a/Koha/Plugin/Acquire/Controllers/ControllerUtils.pm +++ b/Koha/Plugin/Acquire/Controllers/ControllerUtils.pm @@ -148,21 +148,4 @@ sub add_accounting_values_to_ledgers_or_fund_groups_or_funds { return $data; } -sub add_totals_to_fund_allocations { - my ($self, $args) = @_; - - my $allocations = $args->{allocations}; - my @sorted_allocations = sort { $a->{allocation_amount} <=> $b->{allocation_amount} } @$allocations; - - my $total = 0; - foreach my $allocation_index ( 1 .. scalar(@sorted_allocations) ) { - my $allocation = $sorted_allocations[ $allocation_index - 1 ]; - $allocation->{allocation_index} = $allocation_index; - $total += $allocation->{allocation_amount}; - $allocation->{new_fund_value} = $total; - } - - return \@sorted_allocations; -} - 1; diff --git a/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm b/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm index 7a7312e..f96e433 100644 --- a/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm +++ b/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm @@ -49,7 +49,7 @@ sub list { my $filtered_fund_allocations = Koha::Plugin::Acquire::Controllers::ControllerUtils->filter_data_by_group( { dataset => $fund_allocations } ); - my $sorted_allocations = Koha::Plugin::Acquire::Controllers::ControllerUtils->add_totals_to_fund_allocations( { allocations => $filtered_fund_allocations } ); + my $sorted_allocations = Koha::Acquire::Funds::FundAllocations->add_totals_to_fund_allocations( { allocations => $filtered_fund_allocations } ); return $c->render( status => 200, openapi => $sorted_allocations ); } catch { diff --git a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocations.pm b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocations.pm index a51cc69..2da7e7c 100644 --- a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocations.pm +++ b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocations.pm @@ -25,6 +25,23 @@ use JSON qw ( encode_json ); use Koha::Acquire::Funds::FundAllocation; +sub add_totals_to_fund_allocations { + my ( $self, $args ) = @_; + + my $allocations = $args->{allocations}; + # my @sorted_allocations = sort { $a->{allocation_amount} <=> $b->{allocation_amount} } @$allocations; + + my $total = 0; + foreach my $allocation_index ( 1 .. scalar(@$allocations) ) { + my $allocation = @$allocations[ $allocation_index - 1 ]; + $allocation->{allocation_index} = $allocation_index; + $total += $allocation->{allocation_amount}; + $allocation->{new_fund_value} = $total; + } + + return $allocations; +} + =head2 Internal methods =head3 _type diff --git a/t/Koha/FundManagement/FundAllocations.t b/t/Koha/FundManagement/FundAllocations.t new file mode 100644 index 0000000..d6dd763 --- /dev/null +++ b/t/Koha/FundManagement/FundAllocations.t @@ -0,0 +1,110 @@ +#!/usr/bin/perl + +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Database; +use Koha::Acquire::Funds::FundAllocations; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'add_totals_to_fund_allocations' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $fiscal_period = $builder->build_object( + { class => 'Koha::Acquire::Funds::FiscalPeriods', value => { status => 1, visible_to => '1|2' } } ); + my $ledger = $builder->build_object( + { + class => 'Koha::Acquire::Funds::Ledgers', + value => { + fiscal_period_id => $fiscal_period->fiscal_period_id, + visible_to => $fiscal_period->visible_to, + status => $fiscal_period->status, + currency => 'GBP', + owner_id => '1', + ledger_value => 0 + } + } + ); + my $fund = $builder->build_object( + { + class => 'Koha::Acquire::Funds::Funds', + value => { + fiscal_period_id => $fiscal_period->fiscal_period_id, + ledger_id => $ledger->ledger_id, + visible_to => $fiscal_period->visible_to, + status => $fiscal_period->status, + currency => $ledger->currency, + owner_id => $ledger->owner_id, + fund_value => 0 + } + } + ); + + my $allocation = Koha::Acquire::Funds::FundAllocation->new( + { + fund_id => $fund->fund_id, + sub_fund_id => undef, + ledger_id => $ledger->ledger_id, + fiscal_period_id => $fiscal_period->fiscal_period_id, + allocation_amount => 100, + is_transfer => 0 + } + )->store(); + my $allocation2 = Koha::Acquire::Funds::FundAllocation->new( + { + fund_id => $fund->fund_id, + sub_fund_id => undef, + ledger_id => $ledger->ledger_id, + fiscal_period_id => $fiscal_period->fiscal_period_id, + allocation_amount => 50, + is_transfer => 0 + } + )->store(); + my $allocation3 = Koha::Acquire::Funds::FundAllocation->new( + { + fund_id => $fund->fund_id, + sub_fund_id => undef, + ledger_id => $ledger->ledger_id, + fiscal_period_id => $fiscal_period->fiscal_period_id, + allocation_amount => 75, + is_transfer => 0 + } + )->store(); + + my $sorted_allocations = Koha::Acquire::Funds::FundAllocations->add_totals_to_fund_allocations( + { allocations => [ $allocation->unblessed, $allocation2->unblessed, $allocation3->unblessed ] } ); + + is( @{$sorted_allocations}[0]->{allocation_index}, 1, 'Allocation index set correctly' ); + is( @{$sorted_allocations}[1]->{allocation_index}, 2, 'Allocation index set correctly' ); + is( @{$sorted_allocations}[2]->{allocation_index}, 3, 'Allocation index set correctly' ); + + is( @{$sorted_allocations}[0]->{new_fund_value}, 100, 'Fund value set correctly' ); + is( @{$sorted_allocations}[1]->{new_fund_value}, 150, 'Fund value set correctly' ); + is( @{$sorted_allocations}[2]->{new_fund_value}, 225, 'Fund value set correctly' ); + + $schema->storage->txn_rollback; +}; diff --git a/t/api/ControllerUtils.t b/t/api/ControllerUtils.t new file mode 100644 index 0000000..0226d2d --- /dev/null +++ b/t/api/ControllerUtils.t @@ -0,0 +1,214 @@ +#!/usr/bin/env perl + +# Copyright 2024 PTFS Europe + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 3; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +my $t = Test::Mojo->new('Koha::REST::V1'); +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +subtest 'filter_data_by_group' => sub { + + plan tests => 12; + + $schema->storage->txn_begin; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**28 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $lib_group = Koha::Library::Group->new( { title => "Test root group" } )->store(); + my $group_library = + Koha::Library::Group->new( { parent_id => $lib_group->id, branchcode => $library->branchcode } )->store(); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $lib_group2 = Koha::Library::Group->new( { title => "Test root group 2" } )->store(); + my $group_library2 = + Koha::Library::Group->new( { parent_id => $lib_group2->id, branchcode => $library2->branchcode } )->store(); + + + my $ledger = $builder->build_object( + { class => 'Koha::Acquire::Funds::Ledgers', value => { visible_to => $lib_group->id } } ); + my $ledger2 = $builder->build_object( + { class => 'Koha::Acquire::Funds::Ledgers', value => { visible_to => $lib_group2->id } } ); + + my $module = Test::MockModule->new('C4::Context'); + $module->mock( + 'mybranch', + sub { + return $library->branchcode; + } + ); + + $t->get_ok("//$userid:$password@/api/v1/contrib/acquire/ledgers")->status_is(200)->json_is( [ $ledger->to_api ] ); + + $module->mock( + 'mybranch', + sub { + return $library2->branchcode; + } + ); + $t->get_ok("//$userid:$password@/api/v1/contrib/acquire/ledgers")->status_is(200)->json_is( [ $ledger2->to_api ] ); + + my $ledger3 = $builder->build_object( + { class => 'Koha::Acquire::Funds::Ledgers', value => { visible_to => $lib_group2->id } } ); + + $t->get_ok("//$userid:$password@/api/v1/contrib/acquire/ledgers")->status_is(200)->json_is( [ $ledger2->to_api, $ledger3->to_api ] ); + + $module->mock( + 'mybranch', + sub { + return $library->branchcode; + } + ); + $t->get_ok("//$userid:$password@/api/v1/contrib/acquire/ledgers")->status_is(200)->json_is( [ $ledger->to_api ] ); + + $schema->storage->txn_rollback; +}; + +subtest 'add_lib_group_data' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**28 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $lib_group = $builder->build_object( { class => 'Koha::Library::Groups' } ); + my $group_library = + Koha::Library::Group->new( { parent_id => $lib_group->id, branchcode => $library->branchcode } )->store(); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $lib_group2 = $builder->build_object( { class => 'Koha::Library::Groups' } ); + my $group_library2 = + Koha::Library::Group->new( { parent_id => $lib_group2->id, branchcode => $library2->branchcode } )->store(); + + my $lib_group_1_id = $lib_group->id; + my $lib_group_2_id = $lib_group2->id; + + my $ledger = $builder->build_object( + { class => 'Koha::Acquire::Funds::Ledgers', value => { visible_to => "$lib_group_1_id|$lib_group_2_id" } } ); + + my $module = Test::MockModule->new('C4::Context'); + $module->mock( + 'mybranch', + sub { + return $library->branchcode; + } + ); + + my $ledger_id = $ledger->id; + $t->get_ok("//$userid:$password@/api/v1/contrib/acquire/ledgers/$ledger_id")->status_is(200)->json_is( '/lib_groups' => [ $lib_group->unblessed, $lib_group2->unblessed ] ); + + $schema->storage->txn_rollback; +}; + +subtest 'add_accounting_values_to_ledgers_or_fund_groups_or_funds' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $test_object = { + koha_plugin_acquire_funds => [ + { + koha_plugin_acquire_fund_allocations => [ + { allocation_amount => 10, is_transfer => 0 }, + { allocation_amount => 20, is_transfer => 0 }, + { allocation_amount => 30, is_transfer => 1 }, + ] + }, + { + koha_plugin_acquire_fund_allocations => [ + { allocation_amount => 10, is_transfer => 0 }, + { allocation_amount => 50, is_transfer => 0 }, + { allocation_amount => -30, is_transfer => 1 }, + ] + }, + ], + koha_plugin_acquire_sub_funds => [ + { + koha_plugin_acquire_fund_allocations => [ + { allocation_amount => 10, is_transfer => 0 }, + { allocation_amount => 20, is_transfer => 0 }, + { allocation_amount => -10, is_transfer => 0 }, + ] + } + ], + + koha_plugin_acquire_fund_allocations => [ + { allocation_amount => 40, is_transfer => 0 }, + { allocation_amount => 20, is_transfer => 1 }, + { allocation_amount => -10, is_transfer => 0 }, + ] + }; + + my $data_to_test = Koha::Plugin::Acquire::Controllers::ControllerUtils->add_accounting_values_to_ledgers_or_fund_groups_or_funds({ data => $test_object}); + is($data_to_test->{total_allocation}, 160, 'Total allocation correctly calculated'); + is($data_to_test->{allocation_increase}, 210, 'Total allocation increase correctly calculated'); + is($data_to_test->{allocation_decrease}, -50, 'Total allocation decrease correctly calculated'); + is($data_to_test->{net_transfers}, 20, 'Total transfers correctly calculated'); + + + $schema->storage->txn_rollback; +}; diff --git a/t/api/FundManagement/fund_allocations.t b/t/api/FundManagement/fund_allocations.t index 727b576..5d2a8ce 100644 --- a/t/api/FundManagement/fund_allocations.t +++ b/t/api/FundManagement/fund_allocations.t @@ -80,7 +80,7 @@ subtest 'list() tests' => sub { my $fund_allocation = $builder->build_object( { class => 'Koha::Acquire::Funds::FundAllocations', value => { visible_to => $lib_group->id } } ); - my $module2 = Test::MockModule->new('Koha::Plugin::Acquire::Controllers::ControllerUtils'); + my $module2 = Test::MockModule->new('Koha::Acquire::Funds::FundAllocations'); $module2->mock( 'add_totals_to_fund_allocations', sub {