Increment version for 3.22.6 release
[koha.git] / t / Koha_MetadataRecord.t
blob48d9c40bd6bdad944c7aca670fa06db3e7c9c6a0
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 5;
23 use Test::Warn;
25 BEGIN {
26 use_ok('Koha::MetadataRecord');
29 my $marcrecord = MARC::Record->new;
31 $marcrecord->add_fields(
32 [ '001', '1234' ],
33 [ '150', ' ', ' ', a => 'Cooking' ],
34 [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
36 my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
38 is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
40 my $samplehash = [
42 'value' => '1234',
43 'tag' => '001',
46 'subfield' => [
48 'value' => 'Cooking',
49 'subtag' => 'a'
52 'indicator2' => ' ',
53 'tag' => 150,
54 'indicator1' => ' ',
57 'subfield' => [
59 'value' => 'Cookery',
60 'subtag' => 'a'
63 'value' => 'Instructional manuals',
64 'subtag' => 'z'
67 'indicator2' => ' ',
68 'tag' => 450,
69 'indicator1' => ' ',
73 my $hash = $record->createMergeHash();
74 my %fieldkeys;
75 foreach my $field (@$hash) {
76 $fieldkeys{delete $field->{'key'}}++;
77 if (defined $field->{'subfield'}) {
78 foreach my $subfield (@{$field->{'subfield'}}) {
79 $fieldkeys{delete $subfield->{'subkey'}}++;
84 is_deeply($hash, $samplehash, 'Generated hash correctly');
85 my $dupkeys = grep { $_ > 1 } values %fieldkeys;
86 is($dupkeys, 0, 'No duplicate keys');
89 subtest "new() tests" => sub {
91 plan tests => 14;
93 # Test default values with a MARC::Record record
94 my $record = MARC::Record->new();
95 my $metadata_record;
97 warning_is { $metadata_record = new Koha::MetadataRecord({
98 record => $record }) }
99 { carped => 'No schema passed' },
100 "Metadata schema is mandatory, raise a carped warning if omitted";
101 is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted");
103 $metadata_record = new Koha::MetadataRecord({
104 record => $record,
105 schema => 'marc21'
108 is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
109 is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved');
110 is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21');
111 is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc');
112 is( $metadata_record->id, undef, 'id is optional, undef if unspecifid');
114 # Test passed values, also no constraint on record type
115 my $weird_record = {};
116 bless $weird_record, 'Weird::Class';
118 $metadata_record = new Koha::MetadataRecord({
119 record => $weird_record,
120 schema => 'something',
121 format => 'else',
122 id => 'an id'
125 is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
126 is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved');
127 is( $metadata_record->schema, 'something', 'Metadata schema correctly set');
128 is( $metadata_record->format, 'else', 'Serializacion format correctly set');
129 is( $metadata_record->id, 'an id', 'The id correctly set');
131 # Having a record object is mandatory
132 warning_is { $metadata_record = new Koha::MetadataRecord({
133 record => undef,
134 schema => 'something',
135 format => 'else',
136 id => 'an id'
137 }) }
138 { carped => 'No record passed' },
139 'Undefined record raises carped warning';
141 is( $metadata_record, undef, 'record object mandatory')