From 10db559fd5550ccb4e7e28328f0e4938f45914f3 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Tue, 27 Aug 2024 17:04:02 +0100 Subject: [PATCH] ACQUI-159: Utils unit tests --- .../Acquire/lib/Koha/Acquire/Funds/Utils.pm | 7 + tests/t/Koha/FundManagement/Utils.t | 164 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 tests/t/Koha/FundManagement/Utils.t diff --git a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Utils.pm b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Utils.pm index 9b0bc05..bc6b283 100644 --- a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Utils.pm +++ b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Utils.pm @@ -24,6 +24,10 @@ use Scalar::Util qw( looks_like_number ); =head3 cascade_lib_group_visibility +This method will update the visibility if the parent visibility has changed. +This only works if library groups have been removed i.e. new groups are not automatically cascaded +to prevent data being made visible where it shouldn't be. + =cut sub cascade_lib_group_visibility { @@ -57,6 +61,9 @@ sub cascade_lib_group_visibility { =head3 cascade_status +This method will update the status if the parent status has changed +This only applies to a parent being set to "inactive". Activating a parent object again will not change the status of the child + =cut sub cascade_status { diff --git a/tests/t/Koha/FundManagement/Utils.t b/tests/t/Koha/FundManagement/Utils.t new file mode 100644 index 0000000..a6097b3 --- /dev/null +++ b/tests/t/Koha/FundManagement/Utils.t @@ -0,0 +1,164 @@ +#!/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 => 3; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Database; +use Koha::Acquire::Funds::Funds; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'cascade_lib_group_visibility' => sub { + + plan tests => 2; + + $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' + } + } + ); + + my $visibility_updated = Koha::Acquire::Funds::Utils->cascade_lib_group_visibility( + { + parent_visibility => '1', + child => $ledger + } + ); + + is( $visibility_updated, 1, 'Updated field has been cascaded to the ledger' ); + + $visibility_updated = Koha::Acquire::Funds::Utils->cascade_lib_group_visibility( + { + parent_visibility => '1|2', + child => $ledger + } + ); + + is( $visibility_updated, 0, 'An expanded range of library group visibility has not been automatically cascaded' ); + + $schema->storage->txn_rollback; +}; + +subtest 'cascade_status' => sub { + + plan tests => 2; + + $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' + } + } + ); + + my $status_updated = Koha::Acquire::Funds::Utils->cascade_status( + { + parent_status => 0, + child => $ledger + } + ); + + is( $status_updated, 1, 'Updated field has been cascaded to the ledger' ); + + $status_updated = Koha::Acquire::Funds::Utils->cascade_status( + { + parent_status => 1, + child => $ledger + } + ); + + is( $status_updated, 0, 'Child objects have not been automatically set to active' ); + + $schema->storage->txn_rollback; +}; + +subtest 'cascade_data' => sub { + + plan tests => 1; + + $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' + } + } + ); + 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 + } + } + ); + + $ledger->currency('USD'); + $ledger->owner_id('2'); + my @data_to_cascade = ( 'fiscal_period_id', 'currency', 'owner_id' ); + my $data_updated = Koha::Acquire::Funds::Utils->cascade_data( + { + parent => $ledger, + child => $fund, + properties => \@data_to_cascade + } + ); + + is( $data_updated, 1, 'Updated fields have been cascaded to the ledger' ); + + $schema->storage->txn_rollback; +};