e5764579f45e7ad927048142b5847de662be61ff
[koha.git] / t / db_dependent / Koha_Authority.t
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Context;
23 use C4::Charset qw( MarcToUTF8Record );
24 use C4::AuthoritiesMarc qw( AddAuthority );
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;
32
33 BEGIN {
34     use_ok('Koha::MetadataRecord::Authority');
35 }
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39
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";
43
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, '', '');
49 }
50
51 my $record = MARC::Record->new;
52 $record->add_fields(
53         [ '001', '1234' ],
54         [ '150', ' ', ' ', a => 'Cooking' ],
55         [ '450', ' ', ' ', a => 'Cookery' ],
56         );
57 my $authority = Koha::MetadataRecord::Authority->new($record);
58
59 is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
60
61 is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
62
63 is_deeply($authority->record, $record, 'Saved record');
64
65 my $authid = Koha::Authorities->search->next->authid;
66
67 $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
68
69 is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
70
71 is($authority->authid, $authid, 'Object authid is correct');
72
73 is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
74
75 $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
76 is($authority, undef, 'No invalid record is retrieved');
77
78 SKIP:
79 {
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();
83
84     my $import_record_id;
85     for my $row ($sth->fetchrow_hashref) {
86         $import_record_id = $row->{'import_record_id'};
87     }
88
89     skip 'No authorities in reservoir', 3 unless $import_record_id;
90     $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
91
92     is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
93
94     is($authority->authid, undef, 'Records in reservoir do not have an authid');
95
96     is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
97
98     $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
99     is($authority, undef, 'No invalid record is retrieved from reservoir');
100 }
101
102 done_testing();