]> git.koha-community.org Git - koha.git/blob - t/Koha/Util/MARC.t
Bug 32030: Add users to licenses - Preparation
[koha.git] / t / Koha / Util / MARC.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 => 3;
21 use MARC::Record;
22 use MARC::Field;
23
24 BEGIN { use_ok('Koha::Util::MARC'); }
25
26 subtest 'set_marc_field' => sub {
27     plan tests => 6;
28
29     my $record = MARC::Record->new();
30
31     Koha::Util::MARC::set_marc_field($record, '999$9', 'foobar');
32     my @fields = $record->field('999');
33     is(scalar @fields, 1, 'Created one field');
34     my @subfields = $fields[0]->subfield('9');
35     is(scalar @subfields, 1, 'Created one subfield');
36     is($subfields[0], 'foobar', 'Created subfield has correct value');
37
38     Koha::Util::MARC::set_marc_field($record, '999$9', 'foobaz');
39     @fields = $record->field('999');
40     is(scalar @fields, 1, 'No additional field created');
41     @subfields = $fields[0]->subfield('9');
42     is(scalar @subfields, 1, 'No additional subfield created');
43     is($subfields[0], 'foobaz', 'Subfield value has been changed');
44 };
45
46 subtest 'find_marc_info, strip_orgcode, oclc_number' => sub {
47     plan tests => 9;
48
49     my $record = MARC::Record->new;
50     $record->append_fields(
51         MARC::Field->new( '003', 'some_data' ),
52         MARC::Field->new( '035', '', '', a => '(test)123', a => '(change)456' ),
53         MARC::Field->new( '035', '', '', a => '(test) 567', a => '(change) 567' ),
54     );
55     is( scalar Koha::Util::MARC::find_marc_info({
56         record => $record, field => '003',
57     }), 'some_data', 'control field, scalar' );
58     is( ( Koha::Util::MARC::find_marc_info({
59         record => $record, field => '003',
60     }))[0], 'some_data', 'control field, list' );
61
62     is( scalar Koha::Util::MARC::find_marc_info({
63         record => $record, field => '035', subfield => 'a', match => qr/56/,
64     }), '(change)456', '035a, match, scalar' );
65     my @list = Koha::Util::MARC::find_marc_info({
66         record => $record, field => '035', subfield => 'a', match => qr/c.*56/,
67     });
68     is_deeply( \@list, [ '(change)456', '(change) 567' ], '035a, match, list' );
69
70     @list = map { Koha::Util::MARC::strip_orgcode($_) } @list;
71     is_deeply( \@list, [ '456', '567' ], 'strip the orgcodes' );
72     @list = map { Koha::Util::MARC::strip_orgcode($_) } ( '() a', '(a)(b) c', '(abc', ' (a)b' );
73     is_deeply( \@list, [ 'a', '(b) c', '(abc', ' (a)b' ], 'edge cases for strip_orgcode' );
74
75     is( Koha::Util::MARC::oclc_number(), undef, 'No arg for oclc_number' );
76     $record->append_fields(
77         MARC::Field->new( '035', '', '', a => '(OCoLC) 678' ),
78     );
79     is( Koha::Util::MARC::oclc_number($record), '678', 'orgcode mixed case' );
80     $record->insert_fields_ordered(
81         MARC::Field->new( '035', '', '', a => '(ocolc) 789' ),
82     );
83     is( Koha::Util::MARC::oclc_number($record), '789', 'orgcode lower case' );
84
85 };