Bug 32565: (follow-up) Tidy
[koha.git] / t / Koha_MetadataRecord.t
1 #!/usr/bin/perl
2
3 # Copyright 2013 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 5;
23 use Test::Warn;
24
25 use Koha::RecordProcessor;
26 use MARC::Record;
27
28 BEGIN {
29     use_ok('Koha::MetadataRecord');
30 }
31
32 my $marcrecord = MARC::Record->new;
33
34 $marcrecord->add_fields(
35         [ '001', '1234' ],
36         [ '150', ' ', ' ', a => 'Cooking' ],
37         [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
38         );
39 my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
40
41 is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
42
43 my $samplehash = [
44     {
45         'value' => '1234',
46         'tag'   => '001',
47     },
48     {
49         'subfield' => [
50             {
51                 'value'  => 'Cooking',
52                 'subtag' => 'a'
53             }
54         ],
55         'indicator2' => ' ',
56         'tag'        => 150,
57         'indicator1' => ' ',
58     },
59     {
60         'subfield' => [
61             {
62                 'value'  => 'Cookery',
63                 'subtag' => 'a'
64             },
65             {
66                 'value'  => 'Instructional manuals',
67                 'subtag' => 'z'
68             }
69         ],
70         'indicator2' => ' ',
71         'tag'        => 450,
72         'indicator1' => ' ',
73     }
74 ];
75
76 my $hash = $record->createMergeHash();
77 my %fieldkeys;
78 foreach my $field (@$hash) {
79     $fieldkeys{delete $field->{'key'}}++;
80     if (defined $field->{'subfield'}) {
81         foreach my $subfield (@{$field->{'subfield'}}) {
82             $fieldkeys{delete $subfield->{'subkey'}}++;
83         }
84     }
85 }
86
87 is_deeply($hash, $samplehash, 'Generated hash correctly');
88 my $dupkeys = grep { $_ > 1 } values %fieldkeys;
89 is($dupkeys, 0, 'No duplicate keys');
90
91
92 subtest "new() tests" => sub {
93
94     plan tests => 14;
95
96     # Test default values with a MARC::Record record
97     my $record = MARC::Record->new();
98     my $metadata_record;
99
100     warning_is { $metadata_record = Koha::MetadataRecord->new({
101                         record => $record }) }
102                { carped => 'No schema passed' },
103         "Metadata schema is mandatory, raise a carped warning if omitted";
104     is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted");
105
106     $metadata_record = Koha::MetadataRecord->new({
107         record => $record,
108         schema => 'marc21'
109     });
110
111     is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
112     is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved');
113     is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21');
114     is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc');
115     is( $metadata_record->id, undef, 'id is optional, undef if unspecifid');
116
117     # Test passed values, also no constraint on record type
118     my $weird_record = {};
119     bless $weird_record, 'Weird::Class';
120
121     $metadata_record = Koha::MetadataRecord->new({
122         record => $weird_record,
123         schema => 'something',
124         format => 'else',
125         id     => 'an id'
126     });
127
128     is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
129     is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved');
130     is( $metadata_record->schema, 'something', 'Metadata schema correctly set');
131     is( $metadata_record->format, 'else', 'Serializacion format correctly set');
132     is( $metadata_record->id, 'an id', 'The id correctly set');
133
134     # Having a record object is mandatory
135     warning_is { $metadata_record = Koha::MetadataRecord->new({
136                                         record => undef,
137                                         schema => 'something',
138                                         format => 'else',
139                                         id     => 'an id'
140                                     }) }
141                 { carped => 'No record passed' },
142                 'Undefined record raises carped warning';
143
144     is( $metadata_record, undef, 'record object mandatory')
145 };