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

(-)a/t/db_dependent/Linker_FirstMatch.t (-33 / +123 lines)
Lines 1-52 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
#
2
#
3
# This Koha test module is a stub!
3
# This file is part of Koha.
4
# Add more tests here!!!
4
#
5
# Copyright (C) 2011  Jared Camins-Esakov <jcamins@cpbibliography.com>
6
# Copyright (C) 2016  Mark Tompsett <mtompset@hotmail.com>
7
#
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.
12
#
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.
17
#
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>.
5
20
6
use strict;
21
use Modern::Perl;
7
use warnings;
8
use Test::More tests => 3;
22
use Test::More tests => 3;
9
use C4::Context;
23
10
use C4::Heading;
11
use MARC::Record;
24
use MARC::Record;
12
use MARC::Field;
25
use MARC::Field;
26
use MARC::File::XML;
27
use C4::Heading;
13
use C4::Linker::FirstMatch;
28
use C4::Linker::FirstMatch;
14
29
use Test::MockModule;
30
use t::lib::Mocks qw( mock_preference );
31
use t::lib::TestBuilder;
15
32
16
BEGIN {
33
BEGIN {
17
        use_ok('C4::Linker');
34
    use_ok('C4::Linker');
18
}
35
}
19
my $dbh = C4::Context->dbh;
36
20
37
# Mock C4::Heading->authorities() so tests will all pass.
21
my $query = "SELECT authid, marc FROM auth_header LIMIT 1;";
38
# This completely bypasses any search engine calls.
22
my $sth = $dbh->prepare($query);
39
my $authid;
23
$sth->execute();
40
my $mock_heading = Test::MockModule->new('C4::Heading');
24
my ($authid, $marc) = $sth->fetchrow_array();
41
$mock_heading->mock( authorities => sub { return [ { authid => $authid } ]; } );
25
SKIP: {
42
26
    skip "No authorities", 2 unless defined $authid;
43
# Run tests for both logic cases (UNIMARC / non-UNIMARC)
27
    my $linker = C4::Linker::FirstMatch->new();
44
subtest 'MARC21' => sub {
28
    my $auth = MARC::Record->new_from_usmarc($marc);
45
    plan tests => 2;
46
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
47
    run_tests();
48
};
49
50
subtest 'UNIMARC' => sub {
51
    plan tests => 2;
52
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
53
    run_tests();
54
};
55
56
sub run_tests {
57
58
    # Set up just a single authority record to find and use.
59
    my $builder = t::lib::TestBuilder->new();
60
    my $schema  = $builder->schema();
61
    $schema->storage->txn_begin;
62
63
    $builder->delete( { source => 'AuthHeader' } );
64
65
    my $auth_header = $builder->build(
66
        {
67
            source => 'AuthHeader'
68
        }
69
    );
70
71
    $authid = $auth_header->{authid};
72
73
    # Set the data up to match nicely.
74
    my $marc_flavour = C4::Context->preference('MARCFlavour');
75
    my $auth         = get_authority_record( $marc_flavour, $authid );
76
    my $fake_marc    = $auth->as_usmarc();
77
    my $fake_xml     = $auth->as_xml($authid);
78
79
    my $auth_header_record = $schema->resultset('AuthHeader')->find(
80
        {
81
            authid => $authid
82
        }
83
    );
84
    $auth_header_record->marcxml($fake_xml);
85
    $auth_header_record->marc($fake_marc);
86
    $auth_header_record->update;
87
88
    # Find a particular series field.
29
    my $fieldmatch;
89
    my $fieldmatch;
30
    if (C4::Context->preference('MARCFlavour') eq 'UNIMARC') {
90
    if ( C4::Context->preference('MARCFlavour') eq 'UNIMARC' ) {
31
        $fieldmatch = '2..';
91
        $fieldmatch = '2..';
32
    } else {
92
    }
93
    else {
33
        $fieldmatch = '1..';
94
        $fieldmatch = '1..';
34
    }
95
    }
35
    my $bibfield = $auth->field($fieldmatch);
96
    my $bibfield = $auth->field($fieldmatch);
36
    my $tag = $bibfield->tag();
37
    $tag =~ s/^./6/;
38
    $bibfield->update(tag => $tag);
39
    my $heading;
40
    ok(defined ($heading = C4::Heading->new_from_bib_field($bibfield, '')), "Creating heading from bib field");
41
97
42
    # If Zebra is not running, or authorities have not been indexed, test 3
98
    # Convert it to a 6xx series field.
43
    # will fail. Skip it if we are unable to retrieve a list of headings from
99
    my $tag     = $bibfield->tag();
44
    # Zebra.
100
    my $new_tag = $tag;
45
    my @authids = $heading->authorities(1);
101
    $new_tag =~ s/^./6/xsm;
46
    skip "Unable to search Zebra", 1 unless $#authids > 0;
102
    my @subfields    = $bibfield->subfields();
103
    my $new_bibfield = MARC::Field->new(
104
        $new_tag,
105
        $bibfield->indicator(1),
106
        $bibfield->indicator(2), @subfields
107
    );
47
108
109
    # Can we build a heading from it?
110
    my $heading;
111
    ok(
112
        defined(
113
            $heading = C4::Heading->new_from_bib_field( $new_bibfield, q{} )
114
        ),
115
        'Creating heading from bib field'
116
    );
117
118
    # Now test to see if C4::Linker can find it.
48
    my $authmatch;
119
    my $authmatch;
49
    my $fuzzy;
120
    my $fuzzy;
50
    ($authmatch, $fuzzy) = $linker->get_link($heading);
121
    my $linker = C4::Linker::FirstMatch->new();
51
    is($authmatch, $authid, "Matched existing heading");
122
    ( $authmatch, $fuzzy ) = $linker->get_link($heading);
123
    is( $authmatch, $authid, 'Matched existing heading' );
124
125
    $schema->storage->txn_rollback;
126
127
    return;
128
}
129
130
sub get_authority_record {
131
    my ( $marc_flavour, $auth_id ) = @_;
132
    my $main_heading_field = ( $marc_flavour eq 'UNIMARC' ) ? '200' : '100';
133
    my $auth = MARC::Record->new();
134
    $auth->append_fields(
135
        MARC::Field->new( '001', $auth_id ),
136
        MARC::Field->new(
137
            $main_heading_field, q{ }, q{ },
138
            a => 'Geisel, Theodor Seuss,',
139
            d => '1904-1991'
140
        )
141
    );
142
    return $auth;
52
}
143
}
53
- 

Return to bug 16868