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>.
22 use Test
::More tests
=> 3;
28 use C4
::Linker
::FirstMatch
;
30 use t
::lib
::Mocks
qw( mock_preference );
31 use t
::lib
::TestBuilder
;
37 # Mock C4::Heading->authorities() so tests will all pass.
38 # This completely bypasses any search engine calls.
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 {
46 t
::lib
::Mocks
::mock_preference
( 'marcflavour', 'MARC21' );
50 subtest
'UNIMARC' => sub {
52 t
::lib
::Mocks
::mock_preference
( 'marcflavour', 'UNIMARC' );
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(
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.
90 if ( C4
::Context
->preference('MARCFlavour') eq 'UNIMARC' ) {
96 my $bibfield = $auth->field($fieldmatch);
98 # Convert it to a 6xx series field.
99 my $tag = $bibfield->tag();
101 $new_tag =~ s/^./6/xsm;
102 my @subfields = $bibfield->subfields();
103 my $new_bibfield = MARC
::Field
->new(
105 $bibfield->indicator(1),
106 $bibfield->indicator(2), @subfields
109 # Can we build a heading from it?
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.
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;
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 ),
137 $main_heading_field, q{ }, q{ },
138 a
=> 'Geisel, Theodor Seuss,',