Update release notes for 22.05.22 release
[koha.git] / t / db_dependent / AuthoritiesMarc / MARC21.t
1 #!/usr/bin/perl
2 #
3
4 use strict;
5 use warnings;
6
7 use Test::MockModule;
8 use Test::More tests => 2;
9 use MARC::Field;
10 use MARC::Record;
11
12 use t::lib::Mocks;
13 use t::lib::TestBuilder;
14
15 BEGIN {
16     use_ok('C4::AuthoritiesMarc::MARC21');
17 }
18
19 my $schema  = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22 my $builder = t::lib::TestBuilder->new;
23
24 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
25
26 subtest 'CompareFieldWithAuthority tests' => sub {
27     plan tests => 3;
28
29     # We are now going to be testing the authorities hierarchy code, and
30     # therefore need to pretend that we have consistent data in our database
31     my $module = Test::MockModule->new('C4::AuthoritiesMarc');
32     $module->mock('GetHeaderAuthority', sub {
33         return {'authtrees' => ''};
34     });
35     $module->mock('AddAuthorityTrees', sub {
36         return;
37     });
38     $module->mock('GetAuthority', sub {
39         my ($authid) = @_;
40         my $record = MARC::Record->new();
41         if ($authid eq '1') {
42             $record->add_fields(
43                 [ '001', '1' ],
44                 [ '151', ' ', ' ', a => 'United States' ]
45             );
46         } elsif ($authid eq '2') {
47             $record->add_fields(
48                 [ '001', '2' ],
49                 [ '151', ' ', ' ', a => 'New York (State)' ],
50                 [ '551', ' ', ' ', a => 'United States', w => 'g', 9 => '1' ]
51             );
52         } elsif ($authid eq '3') {
53             $record->add_fields(
54                 [ '001', '3' ],
55                 [ '151', ' ', ' ', a => 'New York (City)' ],
56                 [ '551', ' ', ' ', a => 'New York (State)', w => 'g', 9 => '2' ]
57             );
58         } elsif ($authid eq '4') {
59             $record->add_fields(
60                 [ '001', '4' ],
61                 [ '151', ' ', ' ', a => 'New York (City)' ],
62                 [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
63             );
64         } elsif ($authid eq '5') {
65             $record->add_fields(
66                 [ '001', '5' ],
67                 [ '100', ' ', ' ', a => 'Lastname, Firstname', b => 'b', c => 'c', i => 'i' ]
68             );
69         } else {
70             undef $record;
71         }
72         return $record;
73     });
74
75     $dbh->do('DELETE FROM auth_types');
76     $builder->build({ source => 'AuthType', value => { authtypecode => 'PERSO_NAME' }});
77
78     my $field = MARC::Field->new('100', 0, 0, a => 'Lastname, Firstname', b => 'b', c => 'c');
79
80     ok(C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Authority matches');
81
82     $field->add_subfields(i => 'X');
83
84     ok(C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Compare ignores unlisted subfields');
85
86     $field->add_subfields(d => 'd');
87
88     ok(!C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Authority does not match');
89 };
90
91 $schema->storage->txn_rollback;