Bug 22144: (QA follow-up) Prepare the ground for other formats
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 23 Jan 2019 16:25:33 +0000 (13:25 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 30 Jan 2019 11:50:48 +0000 (11:50 +0000)
This patch refactors the original implementation so it is ready for
supporting other formats. It also adds some error handling.

To test:
- Apply this patch
- Run:
  $ kshell
 k$ prove t/db_dependent/Koha/Biblio/Metadata.t
=> SUCCESS: Tests pass!
- Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Koha/Biblio/Metadata.pm
t/db_dependent/Koha/Biblio/Metadata.t [new file with mode: 0644]

index cf55373..9da2ec2 100644 (file)
@@ -17,11 +17,11 @@ package Koha::Biblio::Metadata;
 
 use Modern::Perl;
 
-use Carp;
-
-use C4::Biblio qw();
+use MARC::Record;
+use MARC::File::XML;
 
 use Koha::Database;
+use Koha::Exceptions::Metadata;
 
 use base qw(Koha::Object);
 
@@ -31,33 +31,62 @@ Koha::Metadata - Koha Metadata Object class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
 
 =cut
 
 =head3 record
 
-my @record = $biblio->record($params);
+my $record = $metadata->record;
+
+Returns an object representing the metadata record. The expected record type
+corresponds to this table:
+
+    -------------------------------
+    | format     | object type    |
+    -------------------------------
+    | marcxml    | MARC::Record   |
+    -------------------------------
+
+=head4 Error handling
 
-Returns a MARC::Record object for a record.
+=over
 
-This method accepts the same paramters as C4::Biblio::GetMarcBiblio,
-but does not require the 'biblionumber' parameter.
+=item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
+
+=item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
+
+=back
 
 =cut
 
 sub record {
-    my ( $self, $params ) = @_;
-
-    $params->{biblionumber} = $self->biblionumber;
 
-    my $record = C4::Biblio::GetMarcBiblio($params);
+    my ($self) = @_;
+
+    my $record;
+
+    if ( $self->format eq 'marcxml' ) {
+        $record = eval { MARC::Record::new_from_xml( $self->metadata, 'utf-8', $self->schema ); };
+        unless ($record) {
+            Koha::Exceptions::Metadata::Invalid->throw(
+                id     => $self->id,
+                format => $self->format,
+                schema => $self->schema
+            );
+        }
+    }
+    else {
+        Koha::Exceptions::Metadata->throw(
+            'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format );
+    }
 
     return $record;
 }
 
+=head2 Internal methods
 
-=head3 type
+=head3 _type
 
 =cut
 
diff --git a/t/db_dependent/Koha/Biblio/Metadata.t b/t/db_dependent/Koha/Biblio/Metadata.t
new file mode 100644 (file)
index 0000000..2afd5c3
--- /dev/null
@@ -0,0 +1,85 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 2;
+use Test::Exception;
+
+use t::lib::TestBuilder;
+
+use C4::Biblio;
+use Koha::Database;
+
+BEGIN {
+    use_ok('Koha::Biblio::Metadatas');
+}
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'record() tests' => sub {
+
+    plan tests => 8;
+
+    $schema->storage->txn_begin;
+
+    my $title = 'Oranges and Peaches';
+
+    # Create a valid record
+    my $record = MARC::Record->new();
+    my $field  = MARC::Field->new( '245', '', '', 'a' => $title );
+    $record->append_fields($field);
+    my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
+
+    my $metadata = Koha::Biblios->find($biblio_id)->metadata;
+    my $record2  = $metadata->record;
+
+    is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
+    is( $record2->field('245')->subfield("a"),
+        $title, 'Title in 245$a matches title from original record object' );
+
+    my $bad_data = $builder->build_object(
+        {   class => 'Koha::Biblio::Metadatas',
+            value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
+        }
+    );
+
+    throws_ok { $bad_data->record; }
+    'Koha::Exceptions::Metadata::Invalid', 'Exception thrown on bad record';
+
+    my $exception = $@;
+    is( $exception->id,     $bad_data->id, 'id passed correctly to exception' );
+    is( $exception->format, 'marcxml',     'format passed correctly to exception' );
+    is( $exception->schema, 'MARC21',      'schema passed correctly to exception' );
+
+    my $bad_format = $builder->build_object(
+        {   class => 'Koha::Biblio::Metadatas',
+            value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
+        }
+    );
+
+    throws_ok { $bad_format->record; }
+    'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
+
+    is( "$@",
+        'Koha::Biblio::Metadata->record called on unhandled format: mij',
+        'Exception message built correctly'
+    );
+
+    $schema->storage->txn_rollback;
+};