From c05872ff1da1a94bf7cd66d279fc02ac9748c9a3 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 23 Apr 2021 10:34:06 +0000 Subject: [PATCH] Bug 24154: (follow-up) Make parameter and variable more explicit, add tests Signed-off-by: Katrin Fischer Signed-off-by: Jonathan Druart Signed-off-by: Jonathan Druart --- Koha/Template/Plugin/CirculationRules.pm | 4 +- .../prog/en/modules/admin/smart-rules.tt | 18 ++-- .../Template/Plugin/CirculationRules.t | 92 +++++++++++++++++++ 3 files changed, 103 insertions(+), 11 deletions(-) create mode 100755 t/db_dependent/Template/Plugin/CirculationRules.t diff --git a/Koha/Template/Plugin/CirculationRules.pm b/Koha/Template/Plugin/CirculationRules.pm index 0e39e908bc..7956bc8d80 100644 --- a/Koha/Template/Plugin/CirculationRules.pm +++ b/Koha/Template/Plugin/CirculationRules.pm @@ -43,7 +43,7 @@ sub Get { } sub Search { - my ( $self, $branchcode, $categorycode, $itemtype, $rule_name, $want_rule) = @_; + my ( $self, $branchcode, $categorycode, $itemtype, $rule_name, $params) = @_; $branchcode = undef if $branchcode eq q{} or $branchcode eq q{*}; $categorycode = undef if $categorycode eq q{} or $categorycode eq q{*}; @@ -58,7 +58,7 @@ sub Search { } )->next; - return $rule if $want_rule; + return $rule if $params->{want_rule}; return $rule->rule_value if $rule; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt index d805ef208a..0557afa7d2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt @@ -543,16 +543,16 @@ Return policy Actions - [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxissueqty', 1, 1 ) %] - [% SET patron_maxonsiteissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxonsiteissueqty', 1, 1 ) %] - [% SET rule_value = CirculationRules.Search( current_branch, undef , undef, 'max_holds', 1, 1 ) %] - [% SET holdallowed = CirculationRules.Search( current_branch, undef, undef, 'holdallowed', 1 ) %] - [% SET hold_fulfillment_policy = CirculationRules.Search( current_branch, undef, undef, 'hold_fulfillment_policy', 1 ) %] - [% SET returnbranch = CirculationRules.Search( current_branch, undef, undef, 'returnbranch', 1 ) %] - [% SET dcharp = ( patron_maxissueqty || patron_maxonsiteissueqty || rule_value || holdallowed || hold_fulfillment_policy || returnbranch ) %] + [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxissueqty', { want_rule = 1 } ) %] + [% SET patron_maxonsiteissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxonsiteissueqty', { want_rule = 1 } ) %] + [% SET rule_value = CirculationRules.Search( current_branch, undef , undef, 'max_holds', { want_rule = 1 } ) %] + [% SET holdallowed = CirculationRules.Search( current_branch, undef, undef, 'holdallowed', { want_rule = 1 } ) %] + [% SET hold_fulfillment_policy = CirculationRules.Search( current_branch, undef, undef, 'hold_fulfillment_policy', { want_rule = 1 }) %] + [% SET returnbranch = CirculationRules.Search( current_branch, undef, undef, 'returnbranch', { want_rule = 1 }) %] + [% SET default_checkout_hold_and_return_policy = ( patron_maxissueqty || patron_maxonsiteissueqty || rule_value || holdallowed || hold_fulfillment_policy || returnbranch ) %] - [% IF ( dcharp ) %] + [% IF ( default_checkout_hold_and_return_policy ) %] Defaults @@ -698,7 +698,7 @@ - [% IF (dcharp) %] + [% IF ( default_checkout_hold_and_return_policy ) %] Unset [% END %] diff --git a/t/db_dependent/Template/Plugin/CirculationRules.t b/t/db_dependent/Template/Plugin/CirculationRules.t new file mode 100755 index 0000000000..50b158acee --- /dev/null +++ b/t/db_dependent/Template/Plugin/CirculationRules.t @@ -0,0 +1,92 @@ +#!/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 => 2; +use Test::MockModule; + +use Koha::CirculationRules; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +BEGIN { + use_ok('Koha::Template::Plugin::CirculationRules'); +} + +my $schema = Koha::Database->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'Basic function tests' => sub { + + plan tests => 10; + + $schema->storage->txn_begin; + + my $library_1 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library_2 = $builder->build_object({ class => 'Koha::Libraries' }); + + my $plugin = Koha::Template::Plugin::CirculationRules->new(); + ok($plugin, "initialized CirculationRules plugin"); + + my $rule_value = $plugin->Get($library_1->branchcode,'*','*','maxholds'); + is($rule_value, undef, 'Max holds not set, Get returns undef'); + + $rule_value = $plugin->Search($library_1->branchcode,'*','*','maxholds'); + is($rule_value, undef, 'Max holds not set, Search returns undef'); + + my $rule = $plugin->Search($library_1->branchcode,'*','*','maxholds', { want_rule => 1 } ); + is($rule, undef, 'Max holds not set, Search with want_rule returns undef'); + + Koha::CirculationRules->set_rule( + { + branchcode => '*', + categorycode => '*', + rule_name => 'max_holds', + rule_value => 5, + } + ); + + Koha::CirculationRules->set_rule( + { + branchcode => $library_1->branchcode, + categorycode => '*', + rule_name => 'max_holds', + rule_value => "", + } + ); + + $rule_value = $plugin->Get($library_1->branchcode,'*','*','max_holds'); + is($rule_value, "", 'Max holds set to blank string (unlimited), Get returns blank string for branch'); + + $rule = $plugin->Search($library_1->branchcode,'*','*','max_holds', { want_rule => 1 } ); + is(ref $rule, "Koha::CirculationRule" , 'Max holds set to blank string, Search with want_rule returns a circulation rules object'); + is( $rule->rule_value, "",'Max holds set to blank string (unlimited), returned rule has correct value'); + + $rule_value = $plugin->Get($library_2->branchcode,'*','*','max_holds'); + is($rule_value, 5, 'Max holds default set to 5, Get returns 5 for branch with no rule set'); + + $rule_value = $plugin->Search('*','*','*','max_holds'); + is($rule_value, 5, 'Search for all libraries max holds rule, Search returns 5'); + + $rule_value = $plugin->Search($library_1->branchcode,'*','*','max_holds'); + is($rule_value, "", 'Max holds set to blank string (unlimited), Get returns blank string for branch'); + + + + $schema->storage->txn_rollback; +}; -- 2.39.5