From 6af3a096794d97ff9675549fdcbe4a6da3bd226b Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
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 <katrin.fischer.83@web.de>
---
 Koha/Template/Plugin/CirculationRules.pm           |  4 +-
 .../prog/en/modules/admin/smart-rules.tt           | 18 ++---
 t/db_dependent/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 @@
                     <th>Return policy</th>
                     <th class="noExport">Actions</th>
                 </tr>
-                [% 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 ) %]
                 <tr>
                     <td>
-                        [% IF ( dcharp ) %]
+                        [% IF ( default_checkout_hold_and_return_policy ) %]
                             <em>
                             Defaults
                             </em>
@@ -698,7 +698,7 @@
                     </td>
                     <td class="actions">
                         <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-save"></i> Save</button>
-                        [% IF (dcharp) %]
+                        [% IF ( default_checkout_hold_and_return_policy ) %]
                             <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=*&amp;branch=[% current_branch | html %]" id="unset"><i class="fa fa-undo"></i> Unset</a>
                         [% END %]
                     </td>
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 <http://www.gnu.org/licenses>.
+
+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.11.0