View | Details | Raw Unified | Return to bug 22144
Collapse All | Expand All

(-)a/Koha/Biblio/Metadata.pm (-13 / +42 lines)
Lines 17-27 package Koha::Biblio::Metadata; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Carp;
20
use MARC::Record;
21
21
use MARC::File::XML;
22
use C4::Biblio qw();
23
22
24
use Koha::Database;
23
use Koha::Database;
24
use Koha::Exceptions::Metadata;
25
25
26
use base qw(Koha::Object);
26
use base qw(Koha::Object);
27
27
Lines 31-63 Koha::Metadata - Koha Metadata Object class Link Here
31
31
32
=head1 API
32
=head1 API
33
33
34
=head2 Class Methods
34
=head2 Class methods
35
35
36
=cut
36
=cut
37
37
38
=head3 record
38
=head3 record
39
39
40
my @record = $biblio->record($params);
40
my $record = $metadata->record;
41
42
Returns an object representing the metadata record. The expected record type
43
corresponds to this table:
44
45
    -------------------------------
46
    | format     | object type    |
47
    -------------------------------
48
    | marcxml    | MARC::Record   |
49
    -------------------------------
50
51
=head4 Error handling
41
52
42
Returns a MARC::Record object for a record.
53
=over
43
54
44
This method accepts the same paramters as C4::Biblio::GetMarcBiblio,
55
=item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
45
but does not require the 'biblionumber' parameter.
56
57
=item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
58
59
=back
46
60
47
=cut
61
=cut
48
62
49
sub record {
63
sub record {
50
    my ( $self, $params ) = @_;
51
52
    $params->{biblionumber} = $self->biblionumber;
53
64
54
    my $record = C4::Biblio::GetMarcBiblio($params);
65
    my ($self) = @_;
66
67
    my $record;
68
69
    if ( $self->format eq 'marcxml' ) {
70
        $record = eval { MARC::Record::new_from_xml( $self->metadata, 'utf-8', $self->schema ); };
71
        unless ($record) {
72
            Koha::Exceptions::Metadata::Invalid->throw(
73
                id     => $self->id,
74
                format => $self->format,
75
                schema => $self->schema
76
            );
77
        }
78
    }
79
    else {
80
        Koha::Exceptions::Metadata->throw(
81
            'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format );
82
    }
55
83
56
    return $record;
84
    return $record;
57
}
85
}
58
86
87
=head2 Internal methods
59
88
60
=head3 type
89
=head3 _type
61
90
62
=cut
91
=cut
63
92
(-)a/t/db_dependent/Koha/Biblio/Metadata.t (-1 / +85 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
use Test::Exception;
22
23
use t::lib::TestBuilder;
24
25
use C4::Biblio;
26
use Koha::Database;
27
28
BEGIN {
29
    use_ok('Koha::Biblio::Metadatas');
30
}
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
subtest 'record() tests' => sub {
36
37
    plan tests => 8;
38
39
    $schema->storage->txn_begin;
40
41
    my $title = 'Oranges and Peaches';
42
43
    # Create a valid record
44
    my $record = MARC::Record->new();
45
    my $field  = MARC::Field->new( '245', '', '', 'a' => $title );
46
    $record->append_fields($field);
47
    my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
48
49
    my $metadata = Koha::Biblios->find($biblio_id)->metadata;
50
    my $record2  = $metadata->record;
51
52
    is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
53
    is( $record2->field('245')->subfield("a"),
54
        $title, 'Title in 245$a matches title from original record object' );
55
56
    my $bad_data = $builder->build_object(
57
        {   class => 'Koha::Biblio::Metadatas',
58
            value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
59
        }
60
    );
61
62
    throws_ok { $bad_data->record; }
63
    'Koha::Exceptions::Metadata::Invalid', 'Exception thrown on bad record';
64
65
    my $exception = $@;
66
    is( $exception->id,     $bad_data->id, 'id passed correctly to exception' );
67
    is( $exception->format, 'marcxml',     'format passed correctly to exception' );
68
    is( $exception->schema, 'MARC21',      'schema passed correctly to exception' );
69
70
    my $bad_format = $builder->build_object(
71
        {   class => 'Koha::Biblio::Metadatas',
72
            value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
73
        }
74
    );
75
76
    throws_ok { $bad_format->record; }
77
    'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
78
79
    is( "$@",
80
        'Koha::Biblio::Metadata->record called on unhandled format: mij',
81
        'Exception message built correctly'
82
    );
83
84
    $schema->storage->txn_rollback;
85
};

Return to bug 22144