From c6e3f2ac0575849a0b48170506a4986a50ac8bf1 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 8 Aug 2017 14:51:41 +0200 Subject: [PATCH] Bug 19049: Testing RecordsFromMarcPlugin with a to_marc plugin This patch adds a simple to_marc plugin in t/Koha/Plugin that is used in the added subtest in ImportBatch.t. Test plan: [1] Run t/db_dependent/ImportBatch.t [2] Copy the to_marc test plugin from t to your plugin directory. Under Debian packages, you should do something like: mkdir -p /var/lib/koha/master/plugins/Koha/Plugin/ cp [yourclone]/t/Koha/Plugin/MarcFieldValues.pm /var/lib/koha/master/plugins/Koha/Plugin/ [3] Check if you see this plugin on plugins/plugins-home.pl [4] Create a text file with some fields like: (Note: The plugin needs an empty line between both "records".) 100,a = Test Author 1 245,a = Title One 100,a = Author 2 245,a = Title Two [5] Go to stage-marc-import.pl. Upload the created file. Select the plugin in the format combo and proceed. Did you create two records ? Signed-off-by: Marcel de Rooy Signed-off-by: Katrin Fischer Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart (cherry picked from commit 83f60adbab8e96f0f14ec547029582623ac3a068) Signed-off-by: Fridolin Somers --- t/Koha/Plugin/MarcFieldValues.pm | 86 ++++++++++++++++++++++++++++++++ t/db_dependent/ImportBatch.t | 38 ++++++++++++-- 2 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 t/Koha/Plugin/MarcFieldValues.pm diff --git a/t/Koha/Plugin/MarcFieldValues.pm b/t/Koha/Plugin/MarcFieldValues.pm new file mode 100644 index 0000000000..ee763b8838 --- /dev/null +++ b/t/Koha/Plugin/MarcFieldValues.pm @@ -0,0 +1,86 @@ +package Koha::Plugin::MarcFieldValues; + +use Modern::Perl; +use MARC::Field; +use MARC::Record; + +use base qw(Koha::Plugins::Base); + +our $VERSION = 1.00; +our $metadata = { + name => 'MarcFieldValues', + author => 'M. de Rooy', + class => 'Koha::Plugin::MarcFieldValues', + description => 'Convert MARC fields from plain text', + date_authored => '2017-08-08', + date_updated => '2017-08-08', + minimum_version => '16.11', + maximum_version => undef, + version => $VERSION, + input_format => 'MARC field/value pairs in plain text', +}; + +=head1 METHODS + +=head2 new + + Create new object + +=cut + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +=head2 to_marc + + Create string of MARC blobs from plain text lines in the form: + field [,ind1|,ind2|,subcode] = value + Example: + 003 = OrgCode + 100,a = Author + 245,ind2 = 0 + 245,a = Title + +=cut + +sub to_marc { + my ( $self, $args ) = @_; + # $args->{data} contains text to convert to MARC + my $retval = ''; + my @records = split /\r?\n\r?\n/, $args->{data}; + foreach my $rec ( @records ) { + my @lines = split /\r?\n/, $rec; + my $marc = MARC::Record->new; + my $inds = {}; + my $fldcount = 0; + foreach my $line ( @lines ) { + # each line is of the form field [,ind1|,ind2|,subcode] = value + my @temp = split /\s*=\s*/, $line, 2; + next if @temp < 2; + $temp[0] =~ s/^\s*//; + $temp[1] =~ s/\s*$//; + my $value = $temp[1]; + @temp = split /\s*,\s*/, $temp[0]; + if( @temp > 1 && $temp[1] =~ /ind[12]/ ) { + $inds->{$temp[0]}->{$temp[1]} = substr($value, 0, 1); + next; + } + $fldcount++; + $marc->append_fields( MARC::Field->new( + $temp[0], + $temp[0] < 10 + ? () + : ( ( $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind1} // '' : '', $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind2} // '' : ''), substr( $temp[1], 0, 1 ) ), + $value, + )); + } + $retval .= $marc->as_usmarc if $fldcount; + } + return $retval; +} + +1; diff --git a/t/db_dependent/ImportBatch.t b/t/db_dependent/ImportBatch.t index bdab996716..607c71ea1d 100644 --- a/t/db_dependent/ImportBatch.t +++ b/t/db_dependent/ImportBatch.t @@ -1,13 +1,18 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 13; +use Test::More tests => 14; +use File::Basename; +use File::Temp qw/tempfile/; -use Koha::Database; +use t::lib::Mocks; use t::lib::TestBuilder; +use Koha::Database; +use Koha::Plugins; + BEGIN { - use_ok('C4::ImportBatch'); + use_ok('C4::ImportBatch'); } # Start transaction @@ -162,4 +167,31 @@ C4::ImportBatch::DeleteBatch( $id_import_batch3 ); my $batch3_results = $dbh->do('SELECT * FROM import_batches WHERE import_batch_id = "$id_import_batch3"'); is( $batch3_results, "0E0", "Batch 3 has been deleted"); +subtest "RecordsFromMarcPlugin" => sub { + plan tests => 5; + + # Create a test file + my ( $fh, $name ) = tempfile(); + print $fh q| +003 = NLAmRIJ +100,a = Author +245,ind2 = 0 +245,a = Silence in the library +500 , a= Some note + +100,a = Another +245,a = Noise in the library|; + close $fh; + t::lib::Mocks::mock_config( 'pluginsdir', dirname(__FILE__) . '/..' ); + my ( $plugin ) = Koha::Plugins->new->GetPlugins({ metadata => { name => 'MarcFieldValues' } }); + isnt( $plugin, undef, "Plugin found" ); + my $records = C4::ImportBatch::RecordsFromMarcPlugin( $name, ref $plugin, 'UTF-8' ); + is( @$records, 2, 'Two results returned' ); + is( ref $records->[0], 'MARC::Record', 'Returned MARC::Record object' ); + is( $records->[0]->subfield('245', 'a'), 'Silence in the library', + 'Checked one field in first record' ); + is( $records->[1]->subfield('100', 'a'), 'Another', + 'Checked one field in second record' ); +}; + $schema->storage->txn_rollback; -- 2.39.5