Bug 9755 QA follow-up: move MARC-specific functionality to utility class
[koha.git] / t / Koha_Util_MARC.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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 # Note that at present this test is almost identical to the one testing
21 # the encapsulating method in Koha::MetadataRecord.
22
23 use strict;
24 use warnings;
25
26 use Test::More tests => 3;
27
28 BEGIN {
29         use_ok('Koha::Util::MARC');
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 $samplehash = [
40     {
41         'field' => [
42             {
43                 'value' => '1234',
44                 'tag'   => '001',
45             }
46         ]
47     },
48     {
49         'field' => [
50             {
51                 'subfield' => [
52                     {
53                         'value'  => 'Cooking',
54                         'subtag' => 'a'
55                     }
56                 ],
57                 'indicator2' => ' ',
58                 'tag'        => 150,
59                 'indicator1' => ' ',
60             }
61         ]
62     },
63     {
64         'field' => [
65             {
66                 'subfield' => [
67                     {
68                         'value'  => 'Cookery',
69                         'subtag' => 'a'
70                     },
71                     {
72                         'value' => 'Instructional manuals',
73                         'subtag' => 'z'
74                     }
75                 ],
76                 'indicator2' => ' ',
77                 'tag'        => 450,
78                 'indicator1' => ' ',
79             }
80         ]
81     }
82 ];
83
84 my $hash = Koha::Util::MARC::createMergeHash($marcrecord);
85 my %fieldkeys;
86 foreach my $field (@$hash) {
87     $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
88     if (defined $field->{'field'}->[0]->{'subfield'}) {
89         foreach my $subfield (@{$field->{'field'}->[0]->{'subfield'}}) {
90             $fieldkeys{delete $subfield->{'subkey'}}++;
91         }
92     }
93 }
94
95 is_deeply($hash, $samplehash, 'Generated hash correctly');
96 my $dupkeys = grep { $_ > 1 } values %fieldkeys;
97 is($dupkeys, 0, 'No duplicate keys');