Bug 11439: (follow up) add missing rollback call
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use C4::Context;
24 use Test::More;
25
26 BEGIN {
27         use_ok('Koha::Authority');
28 }
29
30 my $record = MARC::Record->new;
31
32 $record->add_fields(
33         [ '001', '1234' ],
34         [ '150', ' ', ' ', a => 'Cooking' ],
35         [ '450', ' ', ' ', a => 'Cookery' ],
36         );
37 my $authority = Koha::Authority->new($record);
38
39 is(ref($authority), 'Koha::Authority', 'Created valid Koha::Authority object');
40
41 is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
42
43 is_deeply($authority->record, $record, 'Saved record');
44
45 SKIP:
46 {
47     my $dbh = C4::Context->dbh;
48     my $sth = $dbh->prepare("SELECT authid FROM auth_header LIMIT 1;");
49     $sth->execute();
50
51     my $authid;
52     for my $row ($sth->fetchrow_hashref) {
53         $authid = $row->{'authid'};
54     }
55     skip 'No authorities', 3 unless $authid;
56     $authority = Koha::Authority->get_from_authid($authid);
57
58     is(ref($authority), 'Koha::Authority', 'Retrieved valid Koha::Authority object');
59
60     is($authority->authid, $authid, 'Object authid is correct');
61
62     is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
63
64     $authority = Koha::Authority->get_from_authid('alphabetsoup');
65     is($authority, undef, 'No invalid record is retrieved');
66 }
67
68 SKIP:
69 {
70     my $dbh = C4::Context->dbh;
71     my $sth = $dbh->prepare("SELECT import_record_id FROM import_records WHERE record_type = 'auth' LIMIT 1;");
72     $sth->execute();
73
74     my $import_record_id;
75     for my $row ($sth->fetchrow_hashref) {
76         $import_record_id = $row->{'import_record_id'};
77     }
78
79     skip 'No authorities in reservoir', 3 unless $import_record_id;
80     $authority = Koha::Authority->get_from_breeding($import_record_id);
81
82     is(ref($authority), 'Koha::Authority', 'Retrieved valid Koha::Authority object');
83
84     is($authority->authid, undef, 'Records in reservoir do not have an authid');
85
86     is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
87
88     $authority = Koha::Authority->get_from_breeding('alphabetsoup');
89     is($authority, undef, 'No invalid record is retrieved from reservoir');
90 }
91
92 done_testing();