From 6af91c6af482183e08520e2b105dcd75a213a947 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Mon, 20 Mar 2017 15:09:13 +0100 Subject: [PATCH] Bug 18138: Use MARC modification template when adding/updating bibliographic records Add a preference that enables a specific marc modification template to be applied on bibliographic records saved using simple or advanced MARC editor or the HTTP API. To test: 1) Create a MARC modification template containg one or more actions. 2) Under "Global system preferences" and "Cataloging" set the "EditBiblioMarcModificationTemplate" preference to the name of the previously created template. 3) Edit (or create) a bibliographic record using either the simple or advanced MARC editor. 4) Save the record. 5) Verify that the action(s) have been applied. Sponsored-by: Gothenburg University Library Signed-off-by: Frank Hansen --- cataloguing/addbiblio.pl | 12 ++ ...t-biblio-marc-modification-template-syspref.sql | 1 + .../en/modules/admin/preferences/cataloguing.pref | 5 + svc/bib | 13 ++ .../EditBiblioMarcModificationTemplatePreference.t | 154 +++++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 installer/data/mysql/atomicupdate/bug_18138-edit-biblio-marc-modification-template-syspref.sql create mode 100644 t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index c4ed49da87..9758d6f85f 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -34,6 +34,7 @@ use C4::Koha; use C4::ClassSource; use C4::ImportBatch; use C4::Charset; +use C4::MarcModificationTemplates; use Koha::BiblioFrameworks; use Koha::DateUtils; @@ -46,6 +47,7 @@ use Date::Calc qw(Today); use MARC::File::USMARC; use MARC::File::XML; use URI::Escape; +use List::MoreUtils qw(firstval); if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) { MARC::File::XML->default_record_format('UNIMARC'); @@ -829,6 +831,16 @@ 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("EditBiblioMarcModificationTemplate"); + if ($marc_modification_template_name) { + my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates(); + if ($template) { + ModifyRecordWithTemplate($template->{'template_id'}, $record); + } + else { + warn "No MARC modification template exists with name \"$marc_modification_template_name\""; + } + } if ( $is_a_modif ) { ModBiblio( $record, $biblionumber, $frameworkcode ); } diff --git a/installer/data/mysql/atomicupdate/bug_18138-edit-biblio-marc-modification-template-syspref.sql b/installer/data/mysql/atomicupdate/bug_18138-edit-biblio-marc-modification-template-syspref.sql new file mode 100644 index 0000000000..0756e581b8 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_18138-edit-biblio-marc-modification-template-syspref.sql @@ -0,0 +1 @@ +INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type`) VALUES ('EditBiblioMarcModificationTemplate', '', '', 'MARC modification template applied when saving bibliographic record in staff client or HTTP API', 'Free'); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref index 99e79e8222..72dfefd4b8 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref @@ -149,6 +149,11 @@ Cataloging: - and record's last modifier name in MARC subfield - pref: MarcFieldForModifierName - "
NOTE: Use a dollar sign between field and subfield like 123$a." + - + - Apply the MARC modification template with name + - pref: EditBiblioMarcModificationTemplate + class: short + - on records saved using the staff client simple MARC editor, advanced editor or HTTP API. Display: - - 'Separate multiple displayed authors, series or subjects with ' diff --git a/svc/bib b/svc/bib index 1cb176e777..32ee7be42a 100755 --- a/svc/bib +++ b/svc/bib @@ -25,7 +25,9 @@ use CGI qw ( -utf8 ); use C4::Auth qw/check_api_auth/; use C4::Biblio; use C4::Items; +use C4::MarcModificationTemplates; use XML::Simple; +use List::MoreUtils qw(firstval); my $query = new CGI; binmode STDOUT, ':encoding(UTF-8)'; @@ -114,6 +116,17 @@ sub update_bib { } } + my $marc_modification_template_name = C4::Context->preference("EditBiblioMarcModificationTemplate"); + if ($marc_modification_template_name) { + my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates(); + if ($template) { + ModifyRecordWithTemplate($template->{'template_id'}, $record); + } + else { + warn "No MARC modification template exists with name \"$marc_modification_template_name\""; + } + } + ModBiblio( $record, $biblionumber, $frameworkcode ); my $new_record = GetMarcBiblio({ biblionumber => $biblionumber, diff --git a/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t b/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t new file mode 100644 index 0000000000..2e0772a563 --- /dev/null +++ b/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t @@ -0,0 +1,154 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . +use Modern::Perl; +use Test::More tests => 1; +use Test::MockModule; +use Test::Warn; +use Test::WWW::Mechanize; +require HTTP::Request; +use MARC::Record; +use XML::Simple; +use C4::MarcModificationTemplates; +use C4::Biblio; + +eval{ + use C4::Context; +}; +if ($@) { + plan skip_all => "Tests skip. You must have a working Context\n"; +} + +my $koha_conf = $ENV{KOHA_CONF}; +my $koha_conf_xml = XMLin($koha_conf); + +my $user = $ENV{KOHA_USER} || $koha_conf_xml->{config}->{user}; +my $password = $ENV{KOHA_PASS} || $koha_conf_xml->{config}->{pass}; +my $intranet = $ENV{KOHA_INTRANET_URL}; + +if (not defined $intranet) { + plan skip_all => "Tests skipped. You must set environment variable KOHA_INTRANET_URL to run tests\n"; +} + +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 $add_template_action_result = AddModificationTemplateAction( + $template_id, + 'update_field', # Action + 0, + 250, # Field + 'a', # Subfield + '251 bottles of beer on the wall', # Value + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + 0, + '' + ); + ok($add_template_action_result, 'MARC modification template action successfully created'); + my $record = MARC::Record->new(); + $record->leader(' nam a22 7a 4500'); + $record->append_fields( + MARC::Field->new('001', '12345'), + MARC::Field->new('003', 'TEST'), + MARC::Field->new('245', '1', '0', 'a' => 'TEST'), + MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall'), + # @FIXME: Create test item type? Not super safe to rely on existing 'BK' type + MARC::Field->new('942', '','', 'c' => 'BK'), + ); + my ($biblionumber) = AddBiblio($record, ''); + + my $saved_record = GetMarcBiblio($biblionumber, 0); + my $saved_record_250_field = $saved_record->field('250'); + isa_ok($saved_record_250_field, 'MARC::Field', 'Field with tag 250 has been saved'); + 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('EditBiblioMarcModificationTemplate'); + C4::Context->set_preference('EditBiblioMarcModificationTemplate', 'TEST'); + + my $agent = Test::WWW::Mechanize->new(autocheck => 1); + + $agent->get_ok("$intranet/cgi-bin/koha/mainpage.pl", 'Connect to intranet'); + $agent->form_name('loginform'); + $agent->field('password', $password); + $agent->field('userid', $user); + $agent->field('branch', ''); + $agent->click_ok('', 'Login to staff client'); + + $agent->get_ok("$intranet/cgi-bin/koha/mainpage.pl", 'Load main page'); #FIXME: Remove? + $agent->get_ok("$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber", 'Load bibliographic record in simple MARC editor'); + $agent->submit_form_ok( + { + form_id => 'f', + button => '', + }, + 'Save bibliographic record using simple MARC editor' + ); + + $saved_record = GetMarcBiblio($biblionumber, 0); + $saved_record_250_field = $saved_record->field('250'); + is($saved_record_250_field->subfield('a'), '251 bottles of beer on the wall', 'Field with tag 250 has been modified by MARC modification template'); + + # FIXME: Don't really need to to this, but could be useful so + # test below does not succed by accident + $saved_record->delete_fields($saved_record_250_field); + $saved_record->insert_fields_ordered(MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall')); + $biblionumber = ModBiblioMarc($saved_record, $biblionumber); + ok($biblionumber, 'Field with tag 250 has been restored to original value'); + + # @FIXME: Leader record status n or c? + my $record_xml = <<'EOF'; + + + cam a22 7a 4500 + 12345 + TEST + + TEST + + + 250 bottles of beer on the wall + + + BK + ddc + +EOF + my $headers = HTTP::Headers->new( + Content_Type => 'text/xml', + ); + my $request = HTTP::Request->new('POST', "$intranet/cgi-bin/koha/svc/bib/$biblionumber", $headers, $record_xml); + my $response = $agent->request($request); + + is($response->code, '200', 'Emulate save in advanced MARC editor, Koha REST API responded with 200 for update biblio request'); + like($response->decoded_content, qr/251 bottles of beer on the wall/, 'Field with tag 250 has been modified by MARC modification template'); + + # Clean up + DelBiblio($biblionumber); + DelModificationTemplate($template_id); + C4::Context->set_preference('EditBiblioMarcModificationTemplate', $old_template_preference); +}; -- 2.11.0