From 7e5a9f93b458391d210a815db19c576376ca5137 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 23 Jan 2019 13:25:33 -0300 Subject: [PATCH] Bug 22144: (QA follow-up) Prepare the ground for other formats 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 Signed-off-by: Kyle M Hall Signed-off-by: Josef Moravec Signed-off-by: Nick Clemens --- Koha/Biblio/Metadata.pm | 55 +++++++++++++---- t/db_dependent/Koha/Biblio/Metadata.t | 85 +++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 t/db_dependent/Koha/Biblio/Metadata.t diff --git a/Koha/Biblio/Metadata.pm b/Koha/Biblio/Metadata.pm index cf553731b0..9da2ec2b49 100644 --- a/Koha/Biblio/Metadata.pm +++ b/Koha/Biblio/Metadata.pm @@ -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 exception. + +=item If it fails to create the record object, it throws a I 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 index 0000000000..2afd5c3ce7 --- /dev/null +++ b/t/db_dependent/Koha/Biblio/Metadata.t @@ -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 . + +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; +}; -- 2.39.5