Bug 26157: Hide expected DBI warnings from tests
[koha.git] / t / db_dependent / Linker_FirstMatch.t
blobcfcd752437ea22ec1cceb0fd423193a2731e9124
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2011 Jared Camins-Esakov <jcamins@cpbibliography.com>
6 # Copyright (C) 2016 Mark Tompsett <mtompset@hotmail.com>
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.
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.
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>.
21 use Modern::Perl;
22 use Test::More tests => 3;
24 use MARC::Record;
25 use MARC::Field;
26 use MARC::File::XML;
27 use C4::Heading;
28 use C4::Linker::FirstMatch;
29 use Test::MockModule;
30 use t::lib::Mocks qw( mock_preference );
31 use t::lib::TestBuilder;
33 BEGIN {
34 use_ok('C4::Linker');
37 # Mock C4::Heading->authorities() so tests will all pass.
38 # This completely bypasses any search engine calls.
39 my $authid;
40 my $mock_heading = Test::MockModule->new('C4::Heading');
41 $mock_heading->mock( authorities => sub { return [ { authid => $authid } ]; } );
43 # Run tests for both logic cases (UNIMARC / non-UNIMARC)
44 subtest 'MARC21' => sub {
45 plan tests => 2;
46 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
47 run_tests();
50 subtest 'UNIMARC' => sub {
51 plan tests => 2;
52 t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
53 run_tests();
56 sub run_tests {
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;
63 $builder->delete( { source => 'AuthHeader' } );
65 my $auth_header = $builder->build(
67 source => 'AuthHeader'
71 $authid = $auth_header->{authid};
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);
79 my $auth_header_record = $schema->resultset('AuthHeader')->find(
81 authid => $authid
84 $auth_header_record->marcxml($fake_xml);
85 $auth_header_record->marc($fake_marc);
86 $auth_header_record->update;
88 # Find a particular series field.
89 my $fieldmatch;
90 if ( C4::Context->preference('MARCFlavour') eq 'UNIMARC' ) {
91 $fieldmatch = '2..';
93 else {
94 $fieldmatch = '1..';
96 my $bibfield = $auth->field($fieldmatch);
98 # Convert it to a 6xx series field.
99 my $tag = $bibfield->tag();
100 my $new_tag = $tag;
101 $new_tag =~ s/^./6/xsm;
102 my @subfields = $bibfield->subfields();
103 my $new_bibfield = MARC::Field->new(
104 $new_tag,
105 $bibfield->indicator(1),
106 $bibfield->indicator(2), @subfields
109 # Can we build a heading from it?
110 my $heading;
112 defined(
113 $heading = C4::Heading->new_from_field( $new_bibfield, q{} )
115 'Creating heading from bib field'
118 # Now test to see if C4::Linker can find it.
119 my $authmatch;
120 my $fuzzy;
121 my $linker = C4::Linker::FirstMatch->new();
122 ( $authmatch, $fuzzy ) = $linker->get_link($heading);
123 is( $authmatch, $authid, 'Matched existing heading' );
125 $schema->storage->txn_rollback;
127 return;
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'
142 return $auth;