From cbc4c9634604f5d26c7f689e1bb52859c210b493 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Fri, 17 Sep 2021 16:07:13 +0200 Subject: [PATCH] Bug 18138: Refactor after using template id instead of name --- cataloguing/addbiblio.pl | 15 +++++++++------ svc/bib | 12 +++++++----- ...EditBiblioMarcModificationTemplatePreference.t | 13 ++++++++----- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index d02eeaee01f..03e527862d9 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -44,8 +44,10 @@ use C4::ClassSource qw( GetClassSources ); use C4::ImportBatch qw( GetImportRecordMarc ); use C4::Charset qw( SetMarcUnicodeFlag ); use C4::MarcModificationTemplates; +use Koha::MarcModificationTemplates; use Koha::BiblioFrameworks; use Koha::DateUtils qw( dt_from_string ); +use Carp; use Koha::Biblios; use Koha::ItemTypes; @@ -57,7 +59,6 @@ use Koha::Patrons; use MARC::File::USMARC; use MARC::File::XML; use URI::Escape qw( uri_escape_utf8 ); -use List::MoreUtils qw(firstval); if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) { MARC::File::XML->default_record_format('UNIMARC'); @@ -886,16 +887,18 @@ if ( $op eq "addbiblio" ) { # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate) if ( !$duplicatebiblionumber or $confirm_not_duplicate ) { my $oldbibitemnum; - my $marc_modification_template_name = C4::Context->preference("SaveBiblioMarcModificationTemplate"); - if ($marc_modification_template_name) { - my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates(); + + my $template_id = C4::Context->preference("SaveBiblioMarcModificationTemplate"); + if ($template_id) { + my $template = Koha::MarcModificationTemplates->find($template_id); if ($template) { - ModifyRecordWithTemplate($template->{'template_id'}, $record); + ModifyRecordWithTemplate($template_id, $record); } else { - warn "No MARC modification template exists with name \"$marc_modification_template_name\""; + croak "No MARC modification template exists with id \"$template_id\""; } } + if ( $is_a_modif ) { my $member = Koha::Patrons->find($loggedinuser); ModBiblio( diff --git a/svc/bib b/svc/bib index 38fccbbc233..bb1cca832bd 100755 --- a/svc/bib +++ b/svc/bib @@ -26,9 +26,11 @@ use C4::Auth qw( check_api_auth ); use C4::Biblio qw( GetFrameworkCode ); use C4::Items; use C4::MarcModificationTemplates; +use Koha::MarcModificationTemplates; use XML::Simple; use Koha::Biblios; use List::MoreUtils qw(firstval); +use Carp; my $query = CGI->new; binmode STDOUT, ':encoding(UTF-8)'; @@ -117,14 +119,14 @@ sub update_bib { } } - my $marc_modification_template_name = C4::Context->preference("SaveBiblioMarcModificationTemplate"); - if ($marc_modification_template_name) { - my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates(); + my $template_id = C4::Context->preference("SaveBiblioMarcModificationTemplate"); + if ($id) { + my $template = Koha::MarcModificationTemplates->find($template_id); if ($template) { - ModifyRecordWithTemplate($template->{'template_id'}, $record); + ModifyRecordWithTemplate($template_id, $record); } else { - warn "No MARC modification template exists with name \"$marc_modification_template_name\""; + croak "No MARC modification template exists with id \"$template_id\""; } } diff --git a/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t b/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t index f9d69a6427f..0d53ca6c980 100755 --- a/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t +++ b/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t @@ -23,6 +23,7 @@ require HTTP::Request; use MARC::Record; use XML::Simple; use C4::MarcModificationTemplates; +use Koha::MarcModificationTemplates; use C4::Biblio qw(AddBiblio ModBiblioMarc GetMarcBiblio); eval{ @@ -47,10 +48,12 @@ subtest 'Templates applied using simple and advanced MARC Editor' => sub { plan tests => 13; # Create "Test" MARC modification template - my $template_id = AddModificationTemplate('TEST'); - ok($template_id, 'MARC modification template successfully created'); + #my $template_id = AddModificationTemplate('TEST'); + my $template = Koha::MarcModificationTemplate->new({ name => 'TEST' })->store; + + ok($template, 'MARC modification template successfully created'); my $add_template_action_result = AddModificationTemplateAction( - $template_id, + $template->id, 'update_field', # Action 0, 250, # Field @@ -88,7 +91,7 @@ subtest 'Templates applied using simple and advanced MARC Editor' => sub { is($saved_record_250_field->subfield('a'), '250 bottles of beer on the wall', 'Field 250a has the same value passed to AddBiblio'); my $old_template_preference = C4::Context->preference('SaveBiblioMarcModificationTemplate'); - C4::Context->set_preference('SaveBiblioMarcModificationTemplate', 'TEST'); + C4::Context->set_preference('SaveBiblioMarcModificationTemplate', $template->id); my $agent = Test::WWW::Mechanize->new(autocheck => 1); @@ -149,6 +152,6 @@ EOF # Clean up DelBiblio($biblionumber); - DelModificationTemplate($template_id); + $template->delete; C4::Context->set_preference('SaveBiblioMarcModificationTemplate', $old_template_preference); }; -- 2.39.2