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