From c0dc54cbf227d4a64a5d0899dae5003e2c7fd285 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 0a2a736493..099553817c 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -21,6 +21,7 @@ package C4::AuthoritiesMarc; use strict; use warnings; use MARC::Field; +use List::MoreUtils qw(any); use C4::Context; use C4::Biblio qw( GetFrameworkCode ModBiblio ); @@ -425,8 +426,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 @authtypecodes = Koha::Authority::Types->search->get_column('authtypecode'); + foreach my $field ( keys %{ $heading_fields->{$marcflavour} } ) { + if ( defined $record->field($field) ) { + my $authtypecode = $heading_fields->{$marcflavour}->{$field}->{'authtypecode'}; + if ( any { $authtypecode eq $_ } @authtypecodes ) { + return $authtypecode; + } + else { + warn "Authority type $authtypecode not configured"; + return; + } + } } return; } -- 2.39.0