Bug 32478: Remove Koha::Config::SysPref->find since bypasses cache

get_yaml_pref_hash also allows invalid YAML and only parses a limited
subset so remove this method to avoid future issues.

To test):

Since tests already exists for C4::Context->yaml_preference and this
is a trivial change, do we really need a test plan for this?

Sponsored-by: Gothenburg University Library
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
David Gustafsson 2022-09-29 17:18:40 +02:00 committed by Tomas Cohen Arazi
parent 52b807544c
commit e9738a8370
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
4 changed files with 3 additions and 53 deletions

View file

@ -2137,7 +2137,7 @@ sub AddReturn {
my $borrowernumber = $patron ? $patron->borrowernumber : undef; # we don't know if we had a borrower or not
my $patron_unblessed = $patron ? $patron->unblessed : {};
my $update_loc_rules = Koha::Config::SysPrefs->find('UpdateItemLocationOnCheckin')->get_yaml_pref_hash();
my $update_loc_rules = C4::Context->yaml_preference('UpdateItemLocationOnCheckin');
map { $update_loc_rules->{$_} = $update_loc_rules->{$_}[0] } keys %$update_loc_rules; #We can only move to one location so we flatten the arrays
if ($update_loc_rules) {
if (defined $update_loc_rules->{_ALL_}) {

View file

@ -36,34 +36,6 @@ Koha::Config::SysPref - Koha System Preference Object class
=cut
=head3 get_yaml_pref_hash
Turn a pref defined via YAML as a hash
=cut
sub get_yaml_pref_hash {
my ( $self ) = @_;
return if !defined( $self );
# We want to use C4::Context->preference in any cases
# It's cached, and mock_preference still works from tests
my @lines = split /\n/, C4::Context->preference($self->variable) // '';
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;
}
=head3 store

View file

@ -2062,8 +2062,7 @@ rules set in the ItemsDeniedRenewal system preference.
sub is_denied_renewal {
my ( $self ) = @_;
my $denyingrules = Koha::Config::SysPrefs->find('ItemsDeniedRenewal')->get_yaml_pref_hash();
my $denyingrules = C4::Context->yaml_preference('ItemsDeniedRenewal');
return 0 unless $denyingrules;
foreach my $field (keys %$denyingrules) {
my $val = $self->$field;

View file

@ -16,7 +16,7 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 4;
use Test::More tests => 3;
use t::lib::Mocks;
use t::lib::TestBuilder;
@ -43,25 +43,4 @@ is( $retrieved_pref->value, $new_pref->value, 'Find a pref by variable should re
$retrieved_pref->delete;
is( Koha::Config::SysPrefs->search->count, $nb_of_prefs, 'Delete should have deleted the pref' );
subtest 'get_yaml_pref_hash' => sub {
plan tests => 1;
my $the_pref = Koha::Config::SysPrefs->find({variable=>'ItemsDeniedRenewal'});
t::lib::Mocks::mock_preference('ItemsDeniedRenewal', 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 = $the_pref->get_yaml_pref_hash();
is_deeply($got_hash,$expected_hash,"Pref fetched and converted correctly");
};
$schema->storage->txn_rollback;