Bug 19049: [QA Follow-up] Mock config, default format
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 11 Aug 2017 06:34:45 +0000 (08:34 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 15 Aug 2017 15:17:42 +0000 (12:17 -0300)
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 <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

C4/ImportBatch.pm
t/db_dependent/ImportBatch.t
tools/stage-marc-import.pl

index 82d6af2..9b066c7 100644 (file)
@@ -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 (<IN>) {
         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;
 }
index 9c62975..788be3f 100644 (file)
@@ -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' );
index 0ad0128..c845060 100755 (executable)
@@ -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