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 <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Marcel de Rooy 2023-05-26 09:28:28 +00:00 committed by Tomas Cohen Arazi
parent c50dda0d47
commit ba06af4974
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
2 changed files with 9 additions and 1 deletions

View file

@ -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;
}

View file

@ -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' );
};