Bug 19049: [QA Follow-up] Mock config, default format

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>
This commit is contained in:
Marcel de Rooy 2017-08-11 08:34:45 +02:00 committed by Jonathan Druart
parent d8f6315b6d
commit a423fcff42
3 changed files with 13 additions and 11 deletions

View 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;
}

View 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' );

View File

@ -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