From ba06af4974e58fb100e335ebd47fd078bb508fa9 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 26 May 2023 09:28:28 +0000 Subject: [PATCH] Bug 32478: (QA follow-up) Keep current hashref behavior Prevent a crash on wrong contents for ItemsDeniedRenewal pref as we did before. Note: Could be a provisional measure (no band aid to repeat anywhere) until we resolve this in preferences.pl. Test plan: Without this patch: Change ItemsDeniedRenewal to 'nonsense' Run perl -MKoha::Items -e'Koha::Items->find(X)->is_denied_renewal; print "OK\n"' => Replace X by a valid itemnumber Crashes with: Can't use string ("nonsense") as a HASH ref ... No OK print. Apply this patch Run perl -MKoha::Items -e'Koha::Items->find(X)->is_denied_renewal; print "OK\n"' => Replace X by a valid itemnumber Warns only with: Hashref expected for ItemsDeniedRenewal. You got OK. Clear ItemsDeniedRenewal Try again. No warning anymore. Run t/Context.t Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Context.pm | 2 ++ t/Context.t | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/C4/Context.pm b/C4/Context.pm index 35c5025745..6406f340ec 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -348,6 +348,8 @@ sub yaml_preference { return; } + # TODO Remove next line when enforced elsewhere + if( $yaml && lc($preference) eq 'itemsdeniedrenewal' and ref($yaml) ne 'HASH' ) { warn "Hashref expected for $preference"; return; } return $yaml; } diff --git a/t/Context.t b/t/Context.t index b2091f0f96..e8115df8f0 100755 --- a/t/Context.t +++ b/t/Context.t @@ -31,7 +31,7 @@ BEGIN { subtest 'yaml_preference() tests' => sub { - plan tests => 3; + plan tests => 6; my $data = [ 'uno', 'dos', { 'tres' => 'cuatro' } ]; @@ -49,6 +49,12 @@ subtest 'yaml_preference() tests' => sub { 'Invalid YAML on syspref throws a warning'; is( $pref, undef, 'Invalid YAML on syspref makes it return undef' ); + $context->mock( 'preference', sub { return '{ a : 1 }' }); + is( ref( C4::Context->new->yaml_preference('ItemsDeniedRenewal') ), 'HASH', 'Got a hash as expected' ); + $context->mock( 'preference', sub { return '[ 1, 2 ]' }); + warning_like { $pref = C4::Context->new->yaml_preference('ITEMSDENIEDRENEWAL') } qr/Hashref expected/, 'Array not accepted for ItemsDeniedRenewal'; + is( $pref, undef, 'Returned undef' ); + $context->unmock( 'preference' ); }; -- 2.39.5