View | Details | Raw Unified | Return to bug 30756
Collapse All | Expand All

(-)a/t/db_dependent/Koha_Authority.t (-53 / +57 lines)
Lines 18-102 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 3;
21
22
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;
23
use File::Basename;
28
use MARC::Batch;
24
use MARC::Batch;
29
use MARC::File;
25
use MARC::File;
30
use IO::File;
26
use IO::File;
27
28
use C4::Context;
29
use C4::Charset qw( MarcToUTF8Record );
30
use C4::AuthoritiesMarc qw( AddAuthority );
31
use Koha::Database;
31
use Koha::Authorities;
32
use Koha::Authorities;
32
33
33
BEGIN {
34
BEGIN {
34
    use_ok('Koha::MetadataRecord::Authority');
35
    use_ok('Koha::MetadataRecord::Authority');
35
}
36
}
36
37
37
my $schema = Koha::Database->new->schema;
38
our $schema = Koha::Database->new->schema;
38
$schema->storage->txn_begin;
39
$schema->storage->txn_begin;
40
our $dbh = C4::Context->dbh;
41
42
subtest 'Part 1' => sub {
43
    # TODO Move this part to a t/lib packages
44
    my $sourcedir = dirname(__FILE__) . "/data";
45
    my $input_marc_file = "$sourcedir/marc21/zebraexport/authority/exported_records";
46
47
    my $fh = IO::File->new($input_marc_file);
48
    my $batch = MARC::Batch->new( 'USMARC', $fh );
49
    while ( my $record = $batch->next ) {
50
        C4::Charset::MarcToUTF8Record($record, 'MARC21');
51
        AddAuthority($record, '', '');
52
    }
39
53
40
# TODO Move this part to a t/lib packages
54
    my $record = MARC::Record->new;
41
my $sourcedir = dirname(__FILE__) . "/data";
55
    $record->add_fields(
42
my $input_marc_file = "$sourcedir/marc21/zebraexport/authority/exported_records";
56
            [ '001', '1234' ],
57
            [ '150', ' ', ' ', a => 'Cooking' ],
58
            [ '450', ' ', ' ', a => 'Cookery' ],
59
            );
60
    my $authority = Koha::MetadataRecord::Authority->new($record);
43
61
44
my $fh = IO::File->new($input_marc_file);
62
    is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
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
63
51
my $record = MARC::Record->new;
64
    is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
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
65
59
is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
66
    is_deeply($authority->record, $record, 'Saved record');
60
67
61
is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
68
    my $authid = Koha::Authorities->search->next->authid;
62
69
63
is_deeply($authority->record, $record, 'Saved record');
70
    $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
64
71
65
my $authid = Koha::Authorities->search->next->authid;
72
    is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
66
73
67
$authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
74
    is($authority->authid, $authid, 'Object authid is correct');
68
75
69
is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
76
    is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
70
77
71
is($authority->authid, $authid, 'Object authid is correct');
78
    $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
79
    is($authority, undef, 'No invalid record is retrieved');
80
};
72
81
73
is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
82
subtest 'Part2' => sub {
83
    SKIP: {
84
        my $sth = $dbh->prepare("SELECT import_record_id FROM import_records WHERE record_type = 'auth' LIMIT 1;");
85
        $sth->execute();
74
86
75
$authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
87
        my $import_record_id;
76
is($authority, undef, 'No invalid record is retrieved');
88
        for my $row ($sth->fetchrow_hashref) {
89
            $import_record_id = $row->{'import_record_id'};
90
        }
77
91
78
SKIP:
92
        skip 'No authorities in reservoir', 3 unless $import_record_id;
79
{
93
        my $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
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
94
84
    my $import_record_id;
95
        is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
85
    for my $row ($sth->fetchrow_hashref) {
86
        $import_record_id = $row->{'import_record_id'};
87
    }
88
96
89
    skip 'No authorities in reservoir', 3 unless $import_record_id;
97
        is($authority->authid, undef, 'Records in reservoir do not have an authid');
90
    $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
91
98
92
    is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
99
        is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
93
100
94
    is($authority->authid, undef, 'Records in reservoir do not have an authid');
101
        $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
95
102
        is($authority, undef, 'No invalid record is retrieved from reservoir');
96
    is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
103
    }
97
104
    done_testing();
98
    $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
105
};
99
    is($authority, undef, 'No invalid record is retrieved from reservoir');
100
}
101
106
102
done_testing();
107
$schema->storage->txn_rollback;
103
- 

Return to bug 30756