From c2f852a00bbf7bec451bafb3ea5e815589a355dd Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Fri, 26 Jul 2019 12:01:37 +0200 Subject: [PATCH] Bug 23380: GuessAuthTypeCode should check authority type exists The method GuessAuthTypeCode is used when importing from batch or z3950/SRU to find the local authority type depending on MARC flavour and authority record fields. This method should check that found type is configured. Otherwise it can generate software errors when type code is used to fetch Koha::Authority::Type. Test plan : 1) Delete an authority type, ie NP 2) Connect to a z3950/SRU authority server 3) Look for authorities of this type 4) Import an authority 5) Without patch you get error : Can't call method "auth_tag_to_report" on an undefined value at /home/koha/src/C4/AuthoritiesMarc.pm line 757. 6) With patch you see record edition and in logs "Authority type NP not configured" --- C4/AuthoritiesMarc.pm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 0d90b37d67..c49c27c611 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -19,6 +19,7 @@ package C4::AuthoritiesMarc; use strict; use warnings; +use List::MoreUtils qw(any); use C4::Context; use MARC::Record; use C4::Biblio; @@ -411,8 +412,19 @@ sub GuessAuthTypeCode { '280'=>{authtypecode=>'GENRE/FORM'}, } }; - foreach my $field (keys %{$heading_fields->{uc(C4::Context->preference('marcflavour'))} }) { - return $heading_fields->{uc(C4::Context->preference('marcflavour'))}->{$field}->{'authtypecode'} if (defined $record->field($field)); + my $marcflavour = uc( C4::Context->preference('marcflavour') ); + my @authtypes = Koha::Authority::Types->search; + foreach my $field ( keys %{ $heading_fields->{$marcflavour} } ) { + if ( defined $record->field($field) ) { + my $authtypecode = $heading_fields->{$marcflavour}->{$field}->{'authtypecode'}; + if ( any { $authtypecode eq $_->authtypecode } @authtypes ) { + return $authtypecode; + } + else { + warn "Authority type $authtypecode not configured"; + return; + } + } } return; } -- 2.17.1