3 # Copyright 2022 Koha Development Team, Marcel de Rooy
4 # Copyright 2012 C & P Bibliography Services
6 # This file is part of Koha.
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.
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.
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>.
22 use Test::More tests => 3;
26 use t::lib::TestBuilder;
29 use Koha::Authorities;
32 use_ok('Koha::MetadataRecord::Authority');
35 our $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37 our $builder = t::lib::TestBuilder->new;
39 our $record1 = MARC::Record->new;
42 [ '150', ' ', ' ', a => 'Cooking' ],
43 [ '450', ' ', ' ', a => 'Cookery' ],
45 our $record2 = MARC::Record->new;
48 [ '150', ' ', ' ', a => 'Baking' ],
49 [ '450', ' ', ' ', a => 'Bakery' ],
52 subtest 'Test new, authorized_heading, authid, get_from_authid' => sub {
55 my $auth1 = $builder->build_object({ class => 'Koha::Authorities',
56 value => { marcxml => $record1->as_xml },
58 my $auth2 = $builder->build_object({ class => 'Koha::Authorities',
59 value => { marcxml => $record2->as_xml },
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');
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
72 $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
73 is($authority, undef, 'No invalid record is retrieved');
76 subtest 'Test get_from_breeding' => sub {
79 my $import = $builder->build({ source => 'ImportRecord',
80 value => { marcxml => $record1->as_xml, record_type => 'auth' },
82 my $import_record_id = $import->{import_record_id};
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');
89 $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
90 is($authority, undef, 'No invalid record is retrieved from reservoir');
93 $schema->storage->txn_rollback;