From cf28911099d34e294ca867a8af322796fd341fa0 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 21 Sep 2018 20:16:24 +0000 Subject: [PATCH] Bug 15494: Move yaml syspref code to its own sub To test: 1 - prove -v t/db_dependent/Koha/Util/SystemPreferences.t --- C4/Circulation.pm | 14 +----- Koha/Util/SystemPreferences.pm | 69 ++++++++++++++++++++++++++++ t/db_dependent/Koha/Util/SystemPreferences.t | 42 +++++++++++++++++ 3 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 Koha/Util/SystemPreferences.pm create mode 100644 t/db_dependent/Koha/Util/SystemPreferences.t diff --git a/C4/Circulation.pm b/C4/Circulation.pm index e782ce5..a04112d 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -57,6 +57,7 @@ use Koha::RefundLostItemFeeRule; use Koha::RefundLostItemFeeRules; use Koha::Account::Lines; use Koha::Account::Offsets; +use Koha::Util::SystemPreferences; use Carp; use List::MoreUtils qw( uniq any ); use Scalar::Util qw( looks_like_number ); @@ -4108,18 +4109,7 @@ sub _item_denied_renewal { my $item = $params->{item}; return unless $item; - my @lines = split /\n/, C4::Context->preference('ItemsDeniedRenewal')//''; - my $denyingrules; - foreach my $line (@lines){ - my ($field,$array) = split /:/, $line; - next if !$array; - $field =~ s/^\s*|\s*$//g; - $array =~ s/[ [\]\r]//g; - my @array = split /,/, $array; - @array = map { $_ eq '""' || $_ eq "''" ? '' : $_ } @array; - @array = map { $_ eq 'NULL' ? undef : $_ } @array; - $denyingrules->{$field} = \@array; - } + my $denyingrules = get_yaml_pref_hash('ItemsDeniedRenewal'); return unless $denyingrules; foreach my $field (keys %$denyingrules) { my $val = $item->{$field}; diff --git a/Koha/Util/SystemPreferences.pm b/Koha/Util/SystemPreferences.pm new file mode 100644 index 0000000..a57c98b --- /dev/null +++ b/Koha/Util/SystemPreferences.pm @@ -0,0 +1,69 @@ +package Koha::Util::SystemPreferences; + +# Copyright 2018 Koha Development Team +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use parent qw( Exporter ); + +our @EXPORT = qw( + get_yaml_pref_hash +); + +=head1 NAME + +Koha::Util::SystemPreferences - utility class with System Preference routines + +=head1 METHODS + +=head2 get_yaml_pref_hash + +Turn a pref defined via YAML as a hash + +=cut + +sub get_yaml_pref_hash { + my ( $pref ) = @_; + return if !defined( $pref ); + + my @lines = split /\n/, C4::Context->preference($pref)//''; + my $pref_as_hash; + foreach my $line (@lines){ + my ($field,$array) = split /:/, $line; + next if !$array; + $field =~ s/^\s*|\s*$//g; + $array =~ s/[ [\]\r]//g; + my @array = split /,/, $array; + @array = map { $_ eq '""' || $_ eq "''" ? '' : $_ } @array; + @array = map { $_ eq 'NULL' ? undef : $_ } @array; + $pref_as_hash->{$field} = \@array; + } + + return $pref_as_hash; +} + +1; +__END__ + +=head1 AUTHOR + +Koha Development Team + +Nick Clemens + +=cut diff --git a/t/db_dependent/Koha/Util/SystemPreferences.t b/t/db_dependent/Koha/Util/SystemPreferences.t new file mode 100644 index 0000000..4ca7612 --- /dev/null +++ b/t/db_dependent/Koha/Util/SystemPreferences.t @@ -0,0 +1,42 @@ +# This file is part of Koha. +# +# Copyright 2018 Koha Development Team +# +# 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 => 1; + +use t::lib::Mocks; +use Koha::Util::SystemPreferences; + +subtest 'get_yaml_pref_hash' => sub { + + plan tests => 1; + + t::lib::Mocks::mock_preference('ItemDeniedRenewal',q{ + nulled: [NULL,''] + this: [just_that] + multi_this: [that,another] + }); + my $expected_hash = { + nulled => [undef,""], + this => ['just_that'], + multi_this => ['that','another'], + }; + my $got_hash = get_yaml_pref_hash('ItemDeniedRenewal'); + is_deeply($got_hash,$expected_hash,"Pref fetched and converted correctly"); + +}; + -- 2.1.4