From b35a47795a5dfd1bfe941b7ee9b1a9368de59d23 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 18 Dec 2023 14:23:25 -0500 Subject: [PATCH] Bug 28530: Add unit tests --- t/db_dependent/Circulation/Returns.t | 89 ++++++++++++++- t/db_dependent/Koha/Library/FloatLimits.t | 133 ++++++++++++++++++++++ 2 files changed, 221 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/Library/FloatLimits.t diff --git a/t/db_dependent/Circulation/Returns.t b/t/db_dependent/Circulation/Returns.t index db25fda549f..48c19e9d9e8 100755 --- a/t/db_dependent/Circulation/Returns.t +++ b/t/db_dependent/Circulation/Returns.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use Test::MockModule; use Test::Warn; @@ -375,4 +375,91 @@ subtest 'Backdated returns should reduce fine if needed' => sub { is( $fine, undef, "Fine was removed correctly with a backdated return" ); }; +subtest 'Test library float limits' => sub { + plan tests => 2; + + my $library1 = $builder->build( { source => 'Branch' } ); + my $library2 = $builder->build( { source => 'Branch' } ); + my $library3 = $builder->build( { source => 'Branch' } ); + my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); + + my $biblio = $builder->build_sample_biblio(); + + for ( 1 .. 5 ) { + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library1->{branchcode}, + holdingbranch => $library1->{branchcode}, + itype => $itemtype->itemtype, + } + ); + } + + for ( 1 .. 10 ) { + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode}, + itype => $itemtype->itemtype, + } + ); + } + + for ( 1 .. 15 ) { + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library3->{branchcode}, + holdingbranch => $library3->{branchcode}, + itype => $itemtype->itemtype, + } + ); + } + + my $item = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library1->{branchcode}, + holdingbranch => $library1->{branchcode}, + itype => $itemtype->itemtype, + } + ); + + my $limit1 = Koha::Library::FloatLimit->new( + { + branchcode => $library1->{branchcode}, + itemtype => $itemtype->itemtype, + limit => 1, + } + )->store(); + + my $limit2 = Koha::Library::FloatLimit->new( + { + branchcode => $library2->{branchcode}, + itemtype => $itemtype->itemtype, + limit => 100, + } + )->store(); + + my $limit3 = Koha::Library::FloatLimit->new( + { + branchcode => $library3->{branchcode}, + itemtype => $itemtype->itemtype, + limit => 1000, + } + )->store(); + + t::lib::Mocks::mock_preference( 'UseLibraryFloatLimits', '0' ); + + my ( $doreturn, $messages, $iteminformation, $borrower ) = AddReturn( $item->barcode, $item->holdingbranch ); + is( $messages->{TransferTrigger}, undef, "Library float limit not triggered if syspref is not enabled" ); + + t::lib::Mocks::mock_preference( 'UseLibraryFloatLimits', '1' ); + + ( $doreturn, $messages, $iteminformation, $borrower ) = AddReturn( $item->barcode, $item->holdingbranch ); + is( $messages->{TransferTrigger}, 'LibraryFloatLimit', "Library float limit is triggered if syspref is enabled" ); +}; + $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Library/FloatLimits.t b/t/db_dependent/Koha/Library/FloatLimits.t new file mode 100755 index 00000000000..fa8af003ae0 --- /dev/null +++ b/t/db_dependent/Koha/Library/FloatLimits.t @@ -0,0 +1,133 @@ +#!/usr/bin/perl + +# Copyright 2023 ByWater Solutions +# +# 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 => 5; + +use Koha::Database; +use C4::Circulation qw(CreateBranchTransferLimit); + +use t::lib::Mocks; +use t::lib::TestBuilder; + +BEGIN { + use_ok('Koha::Library::FloatLimit'); + use_ok('Koha::Library::FloatLimits'); +} + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $library1 = $builder->build( { source => 'Branch' } ); +my $library2 = $builder->build( { source => 'Branch' } ); +my $library3 = $builder->build( { source => 'Branch' } ); +my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); + +my $biblio = $builder->build_sample_biblio(); + +for ( 1 .. 5 ) { + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library1->{branchcode}, + holdingbranch => $library1->{branchcode}, + itype => $itemtype->itemtype, + } + ); +} + +for ( 1 .. 10 ) { + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode}, + itype => $itemtype->itemtype, + } + ); +} + +for ( 1 .. 15 ) { + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library3->{branchcode}, + holdingbranch => $library3->{branchcode}, + itype => $itemtype->itemtype, + } + ); +} + +my $item = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + homebranch => $library1->{branchcode}, + holdingbranch => $library1->{branchcode}, + itype => $itemtype->itemtype, + } +); + +my $limit1 = Koha::Library::FloatLimit->new( + { + branchcode => $library1->{branchcode}, + itemtype => $itemtype->itemtype, + limit => 1, + } +)->store(); + +my $limit2 = Koha::Library::FloatLimit->new( + { + branchcode => $library2->{branchcode}, + itemtype => $itemtype->itemtype, + limit => 100, + } +)->store(); + +my $limit3 = Koha::Library::FloatLimit->new( + { + branchcode => $library3->{branchcode}, + itemtype => $itemtype->itemtype, + limit => 1000, + } +)->store(); + +is( + Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} ), $library3->{branchcode}, + "Correct library selected for float limit transfer" +); + +t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' ); +t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' ); + +my $to = $library3->{branchcode}; +my $from = $item->holdingbranch; +my $code = $itemtype->itemtype; +CreateBranchTransferLimit( $to, $from, $code ); + +is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" ); +say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )"; + +is( + Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} ), $library2->{branchcode}, + "Correct library selected for float limit transfer when best cannot be used" +); + +$schema->storage->txn_rollback; -- 2.30.2