From c53e2602a87e0f675556b2e190e11ac326bc7165 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 Signed-off-by: Phil Ringnalda --- 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 34e0e640e0..1ac9d3b4ea 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -45,8 +45,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; @@ -59,7 +61,6 @@ use Koha::UI::Form::Builder::Biblio; 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'); @@ -661,16 +662,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 38fccbbc23..bb1cca832b 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 f9d69a6427..0d53ca6c98 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.40.0