2afd5c3ce7ebab4475dc388a4d7d3429253a56cb
[srvgit] / t / db_dependent / Koha / Biblio / Metadata.t
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 };