3 # Copyright 2013 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 5;
28 use_ok('Koha::MetadataRecord');
31 my $marcrecord = MARC::Record->new;
33 $marcrecord->add_fields(
35 [ '150', ' ', ' ', a => 'Cooking' ],
36 [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
38 my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
40 is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
65 'value' => 'Instructional manuals',
75 my $hash = $record->createMergeHash();
77 foreach my $field (@$hash) {
78 $fieldkeys{delete $field->{'key'}}++;
79 if (defined $field->{'subfield'}) {
80 foreach my $subfield (@{$field->{'subfield'}}) {
81 $fieldkeys{delete $subfield->{'subkey'}}++;
86 is_deeply($hash, $samplehash, 'Generated hash correctly');
87 my $dupkeys = grep { $_ > 1 } values %fieldkeys;
88 is($dupkeys, 0, 'No duplicate keys');
91 subtest "new() tests" => sub {
95 # Test default values with a MARC::Record record
96 my $record = MARC::Record->new();
99 warning_is { $metadata_record = Koha::MetadataRecord->new({
100 record => $record }) }
101 { carped => 'No schema passed' },
102 "Metadata schema is mandatory, raise a carped warning if omitted";
103 is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted");
105 $metadata_record = Koha::MetadataRecord->new({
110 is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
111 is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved');
112 is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21');
113 is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc');
114 is( $metadata_record->id, undef, 'id is optional, undef if unspecifid');
116 # Test passed values, also no constraint on record type
117 my $weird_record = {};
118 bless $weird_record, 'Weird::Class';
120 $metadata_record = Koha::MetadataRecord->new({
121 record => $weird_record,
122 schema => 'something',
127 is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
128 is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved');
129 is( $metadata_record->schema, 'something', 'Metadata schema correctly set');
130 is( $metadata_record->format, 'else', 'Serializacion format correctly set');
131 is( $metadata_record->id, 'an id', 'The id correctly set');
133 # Having a record object is mandatory
134 warning_is { $metadata_record = Koha::MetadataRecord->new({
136 schema => 'something',
140 { carped => 'No record passed' },
141 'Undefined record raises carped warning';
143 is( $metadata_record, undef, 'record object mandatory')