Bug 15896 [QA Followup] - Supress warn when running unit tests
[koha.git] / t / db_dependent / Koha_Authority.t
blob300b6a8bcf91ee1ba57f1d7bf768b21e6863a141
1 #!/usr/bin/perl
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use C4::Context;
23 use C4::Charset;
24 use C4::AuthoritiesMarc;
25 use Koha::Database;
26 use Test::More;
27 use File::Basename;
28 use MARC::Batch;
29 use MARC::File;
30 use IO::File;
31 use Koha::Authorities;
33 BEGIN {
34 use_ok('Koha::MetadataRecord::Authority');
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 # TODO Move this part to a t/lib packages
41 my $sourcedir = dirname(__FILE__) . "/data";
42 my $input_marc_file = "$sourcedir/marc21/zebraexport/authority/exported_records";
44 my $fh = IO::File->new($input_marc_file);
45 my $batch = MARC::Batch->new( 'USMARC', $fh );
46 while ( my $record = $batch->next ) {
47 C4::Charset::MarcToUTF8Record($record, 'MARC21');
48 AddAuthority($record, '', '');
51 my $record = MARC::Record->new;
52 $record->add_fields(
53 [ '001', '1234' ],
54 [ '150', ' ', ' ', a => 'Cooking' ],
55 [ '450', ' ', ' ', a => 'Cookery' ],
57 my $authority = Koha::MetadataRecord::Authority->new($record);
59 is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
61 is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
63 is_deeply($authority->record, $record, 'Saved record');
65 my $authid = Koha::Authorities->search->next->authid;
67 $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
69 is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
71 is($authority->authid, $authid, 'Object authid is correct');
73 is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
75 $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
76 is($authority, undef, 'No invalid record is retrieved');
78 SKIP:
80 my $dbh = C4::Context->dbh;
81 my $sth = $dbh->prepare("SELECT import_record_id FROM import_records WHERE record_type = 'auth' LIMIT 1;");
82 $sth->execute();
84 my $import_record_id;
85 for my $row ($sth->fetchrow_hashref) {
86 $import_record_id = $row->{'import_record_id'};
89 skip 'No authorities in reservoir', 3 unless $import_record_id;
90 $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
92 is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
94 is($authority->authid, undef, 'Records in reservoir do not have an authid');
96 is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
98 $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
99 is($authority, undef, 'No invalid record is retrieved from reservoir');
102 done_testing();