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

(-)a/t/db_dependent/Linker_FirstMatch.t (-26 / +110 lines)
Lines 1-31 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;
29
use Test::MockModule;
30
use t::lib::Database;
31
use t::lib::Mocks qw( mock_preference );
32
use t::lib::TestBuilder;
14
33
15
34
16
BEGIN {
35
BEGIN {
17
        use_ok('C4::Linker');
36
        use_ok('C4::Linker');
18
}
37
}
19
my $dbh = C4::Context->dbh;
38
20
39
# This will make DBI and DBIx play friendly,
21
my $query = "SELECT authid, marc FROM auth_header LIMIT 1;";
40
# plus roll back automatically.
22
my $sth = $dbh->prepare($query);
41
my $dbh = t::lib::Database->dbh;
23
$sth->execute();
42
24
my ($authid, $marc) = $sth->fetchrow_array();
43
# Set up just a single authority record to find and use.
25
SKIP: {
44
my $builder = t::lib::TestBuilder->new();
26
    skip "No authorities", 2 unless defined $authid;
45
$builder->delete({ source => 'AuthHeader' });
27
    my $linker = C4::Linker::FirstMatch->new();
46
28
    my $auth = MARC::Record->new_from_usmarc($marc);
47
my $auth_header = $builder->build({
48
    source => 'AuthHeader'
49
});
50
my $authid = $auth_header->{authid};
51
52
# Mock C4::Heading->authorities() so tests will all pass.
53
my $mock_heading = Test::MockModule->new('C4::Heading');
54
$mock_heading->mock( authorities => sub { return [{ authid => $authid }]; } );
55
56
# Run tests for both logic cases (UNIMARC / non-UNIMARC)
57
subtest 'MARC21' => sub {
58
    plan tests => 2;
59
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
60
    run_tests();
61
};
62
63
subtest 'UNIMARC' => sub {
64
    plan tests => 2;
65
    t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
66
    run_tests();
67
};
68
69
sub run_tests {
70
71
    # Set the data up to match nicely.
72
    my $fake_xml = generateFakeMarcXML($authid);
73
    my $auth = MARC::File::XML->decode( $fake_xml );
74
    my $fake_marc = $auth->as_usmarc();
75
    my $sth = $dbh->prepare( q{UPDATE auth_header SET marc=?,marcxml=? WHERE authid=?} );
76
    $sth->execute( $fake_marc, $fake_xml, $authid );
77
78
    # Find a particular series field.
29
    my $fieldmatch;
79
    my $fieldmatch;
30
    if (C4::Context->preference('MARCFlavour') eq 'UNIMARC') {
80
    if (C4::Context->preference('MARCFlavour') eq 'UNIMARC') {
31
        $fieldmatch = '2..';
81
        $fieldmatch = '2..';
Lines 33-52 SKIP: { Link Here
33
        $fieldmatch = '1..';
83
        $fieldmatch = '1..';
34
    }
84
    }
35
    my $bibfield = $auth->field($fieldmatch);
85
    my $bibfield = $auth->field($fieldmatch);
86
87
    # Convert it to a 6xx series field.
36
    my $tag = $bibfield->tag();
88
    my $tag = $bibfield->tag();
37
    $tag =~ s/^./6/;
89
    my $new_tag = $tag;
38
    $bibfield->update(tag => $tag);
90
    $new_tag =~ s/^./6/;
39
    my $heading;
91
    my @subfields = $bibfield->subfields();
40
    ok(defined ($heading = C4::Heading->new_from_bib_field($bibfield, '')), "Creating heading from bib field");
92
    my $new_bibfield = MARC::Field->new($new_tag,$bibfield->indicator(1),$bibfield->indicator(2),@subfields);
41
93
42
    # If Zebra is not running, or authorities have not been indexed, test 3
94
    # Can we build a heading from it?
43
    # will fail. Skip it if we are unable to retrieve a list of headings from
95
    my $heading;
44
    # Zebra.
96
    ok(defined ($heading = C4::Heading->new_from_bib_field($new_bibfield, '')), "Creating heading from bib field");
45
    my @authids = $heading->authorities(1);
46
    skip "Unable to search Zebra", 1 unless $#authids > 0;
47
97
98
    # Now test to see if C4::Linker can find it.
48
    my $authmatch;
99
    my $authmatch;
49
    my $fuzzy;
100
    my $fuzzy;
101
    my $linker = C4::Linker::FirstMatch->new();
50
    ($authmatch, $fuzzy) = $linker->get_link($heading);
102
    ($authmatch, $fuzzy) = $linker->get_link($heading);
51
    is($authmatch, $authid, "Matched existing heading");
103
    is($authmatch, $authid, "Matched existing heading");
104
    return;
105
}
106
107
sub generateFakeMarcXML {
108
    my ($auth_id) = @_;
109
    my $marc_xml;
110
111
    $marc_xml = << 'BLOCK';
112
<?xml version="1.0" encoding="UTF-8"?>
113
<record
114
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
115
    xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
116
    xmlns="http://www.loc.gov/MARC21/slim">
117
118
  <leader>01954cz  a2200289n  4500</leader>
119
  <controlfield tag="001">
120
BLOCK
121
    $marc_xml .= $auth_id . "</controlfield>\n";
122
    if (C4::Context->preference('MARCFlavour') eq 'UNIMARC') {
123
        $marc_xml .= '  <datafield tag="200" ind1="1" ind2=" ">';
124
    } else {
125
        $marc_xml .= '  <datafield tag="100" ind1="1" ind2=" ">';
126
    }
127
128
    $marc_xml .= "\n";
129
    $marc_xml .= << 'DATA';
130
    <subfield code="a">Geisel, Theodor Seuss,</subfield>
131
    <subfield code="d">1904-1991</subfield>
132
  </datafield>
133
DATA
134
135
    $marc_xml .= "</record>\n";
136
    return $marc_xml;
52
}
137
}
53
- 

Return to bug 16868