From 461bf3b5445c94ca95b695f1fcd5b34dbd965b39 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 8 Aug 2017 14:51:41 +0200 Subject: [PATCH] Bug 19049: Tests Content-Type: text/plain; charset=utf-8 --- t/Koha/Plugin/MarcFieldValues.pm | 84 ++++++++++++++++++++++++++++++++++++++++ t/db_dependent/ImportBatch.t | 38 ++++++++++++++++-- 2 files changed, 119 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 0000000..13ee03b --- /dev/null +++ b/t/Koha/Plugin/MarcFieldValues.pm @@ -0,0 +1,84 @@ +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 => '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 MARC record 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 "\n\n", $args->{data}; + foreach my $rec ( @records ) { + my @lines = split "\n", $rec; + my $marc = MARC::Record->new; + my $inds = {}; + 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; + } + $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 . "\n"; + } + return $retval; +} + +1; diff --git a/t/db_dependent/ImportBatch.t b/t/db_dependent/ImportBatch.t index bdab996..607c71e 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.1.4