Skip to content

Commit

Permalink
ACQUI-159: Utils unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mblenk committed Aug 27, 2024
1 parent aeca959 commit 10db559
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
164 changes: 164 additions & 0 deletions tests/t/Koha/FundManagement/Utils.t
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses>.

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;
};

0 comments on commit 10db559

Please sign in to comment.