From 5a4465eefaafae5edfd820e632c5fd7deacc644c Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 11 Aug 2017 08:34:45 +0200 Subject: [PATCH] Bug 19049: [QA Follow-up] Mock config, default format Content-Type: text/plain; charset=utf-8 As requested by QA: [1] Mock_config enable_plugins in the test. [2] Fallback to MARC when format is empty. Remove die statement. Added: [3] Remove $marc. This variable got obsolete during development. [4] Add test on $input_file and $plugin_class. Test $text before calling Handler or processing $text. No need to split undef if somehow Handler returned undef, etc. If the routine returns an empty arrayref, stage-marc-import will do fine. Signed-off-by: Marcel de Rooy --- C4/ImportBatch.pm | 17 ++++++++++------- t/db_dependent/ImportBatch.t | 1 + tools/stage-marc-import.pl | 6 ++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 82d6af2..9b066c7 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -1556,10 +1556,11 @@ sub RecordsFromMARCXMLFile { sub RecordsFromMarcPlugin { my ($input_file, $plugin_class, $encoding) = @_; + my ( $text, @return ); + return \@return if !$input_file || !$plugin_class; # Read input file open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n"; - my ( $text, $marc, @return ); $/ = "\035"; while () { s/^\s+//; @@ -1574,14 +1575,16 @@ sub RecordsFromMarcPlugin { class => $plugin_class, method => 'to_marc', params => { data => $text }, - }); + }) if $text; # Convert to array of MARC records - my $marc_type = C4::Context->preference('marcflavour'); - foreach my $blob ( split(/\x1D/, $text) ) { - next if $blob =~ /^\s*$/; - my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding); - push @return, $marcrecord; + if( $text ) { + my $marc_type = C4::Context->preference('marcflavour'); + foreach my $blob ( split(/\x1D/, $text) ) { + next if $blob =~ /^\s*$/; + my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding); + push @return, $marcrecord; + } } return \@return; } diff --git a/t/db_dependent/ImportBatch.t b/t/db_dependent/ImportBatch.t index 9c62975..788be3f 100644 --- a/t/db_dependent/ImportBatch.t +++ b/t/db_dependent/ImportBatch.t @@ -187,6 +187,7 @@ subtest "RecordsFromMarcPlugin" => sub { 245,a = Noise in the library|; close $fh; + t::lib::Mocks::mock_config( 'enable_plugins', 1 ); my ( $plugin ) = Koha::Plugins->new->GetPlugins({ metadata => { name => 'MarcFieldValues' } }); isnt( $plugin, undef, "Plugin found" ); my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' ); diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index 0ad0128..c845060 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -57,7 +57,7 @@ my $item_action = $input->param('item_action'); my $comments = $input->param('comments'); my $record_type = $input->param('record_type'); my $encoding = $input->param('encoding') || 'UTF-8'; -my $format = $input->param('format'); +my $format = $input->param('format') || 'MARC'; my $marc_modification_template = $input->param('marc_modification_template_id'); my ( $template, $loggedinuser, $cookie ) = get_template_and_user( @@ -93,11 +93,9 @@ if ($completedJobID) { ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding); } elsif( $format eq 'MARC' ) { ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding ); - } elsif( $format ) { # plugin + } else { # plugin based $errors = []; $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( $file, $format, $encoding ); - } else { - die "No format specified"; } warn "$filename: " . ( join ',', @$errors ) if @$errors; # no need to exit if we have no records (or only errors) here -- 2.1.4