Bug 29897: Move code to private method for reusability
[koha.git] / t / db_dependent / Koha_MetadataRecord_Authority.t
1 #!/usr/bin/perl
2
3 # Copyright 2022 Koha Development Team, Marcel de Rooy
4 # Copyright 2012 C & P Bibliography Services
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use Test::More tests => 3;
23
24 use MARC::File::XML;
25
26 use t::lib::TestBuilder;
27
28 use Koha::Database;
29 use Koha::Authorities;
30
31 BEGIN {
32     use_ok('Koha::MetadataRecord::Authority');
33 }
34
35 our $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37 our $builder = t::lib::TestBuilder->new;
38
39 our $record1 = MARC::Record->new;
40 $record1->add_fields(
41     [ '001', '1234' ],
42     [ '150', ' ', ' ', a => 'Cooking' ],
43     [ '450', ' ', ' ', a => 'Cookery' ],
44 );
45 our $record2 = MARC::Record->new;
46 $record2->add_fields(
47     [ '001', '2345' ],
48     [ '150', ' ', ' ', a => 'Baking' ],
49     [ '450', ' ', ' ', a => 'Bakery' ],
50 );
51
52 subtest 'Test new, authorized_heading, authid, get_from_authid' => sub {
53     plan tests => 7;
54
55     my $auth1 = $builder->build_object({ class => 'Koha::Authorities',
56         value => { marcxml => $record1->as_xml },
57     });
58     my $auth2 = $builder->build_object({ class => 'Koha::Authorities',
59         value => { marcxml => $record2->as_xml },
60     });
61
62     my $authority = Koha::MetadataRecord::Authority->new( $record1 );
63     is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
64     is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
65     is_deeply($authority->record, $record1, 'Saved record');
66
67     $authority = Koha::MetadataRecord::Authority->get_from_authid( $auth2->id );
68     is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
69     is($authority->authid, $auth2->id, 'Object authid is correct');
70     is($authority->record->field('001')->data(), '2345', 'Retrieved original 001'); # Note: not created via AddAuthority
71
72     $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
73     is($authority, undef, 'No invalid record is retrieved');
74 };
75
76 subtest 'Test get_from_breeding' => sub {
77     plan tests => 4;
78
79     my $import = $builder->build({ source => 'ImportRecord',
80         value => { marcxml => $record1->as_xml, record_type => 'auth' },
81     });
82     my $import_record_id = $import->{import_record_id};
83
84     my $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
85     is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
86     is($authority->authid, undef, 'Records in reservoir do not have an authid');
87     is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
88
89     $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
90     is($authority, undef, 'No invalid record is retrieved from reservoir');
91 };
92
93 $schema->storage->txn_rollback;