Skip to content

Commit

Permalink
ACQUI-161: ControllerUtils unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mblenk committed Aug 28, 2024
1 parent 87b4ff5 commit 5b1e890
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 19 deletions.
17 changes: 0 additions & 17 deletions Koha/Plugin/Acquire/Controllers/ControllerUtils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocations.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
110 changes: 110 additions & 0 deletions t/Koha/FundManagement/FundAllocations.t
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses>.

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;
};
214 changes: 214 additions & 0 deletions t/api/ControllerUtils.t
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses>.

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;
};
2 changes: 1 addition & 1 deletion t/api/FundManagement/fund_allocations.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5b1e890

Please sign in to comment.