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

(-)a/t/db_dependent/Linker_Default.t (-1 / +84 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
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>.
20
21
use Modern::Perl;
22
use Test::More tests => 2;
23
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;
32
33
BEGIN {
34
    use_ok('C4::Linker');
35
}
36
37
# Mock C4::Heading->authorities() so tests will all pass.
38
# This completely bypasses any search engine calls.
39
my $authid=0;
40
my $mock_heading = Test::MockModule->new('C4::Heading');
41
$mock_heading->mock( authorities => sub { return [ { authid => $authid++ } ]; } );
42
43
my $builder = t::lib::TestBuilder->new();
44
my $schema  = $builder->schema();
45
$schema->storage->txn_begin;
46
47
subtest 'Test caching in get_link and update_cache' => sub {
48
    plan tests => 6;
49
50
    my @tags = C4::Context->preference('marcflavour') eq 'UNIMARC' ? (601,'j',602,'a') : (650,'a',655,'a');
51
52
    my $subject_field = MARC::Field->new($tags[0],0,2,$tags[1]=>'Science fiction');
53
    my $subject_field2 = MARC::Field->new($tags[0],0,2,$tags[1]=>'Science fiction');
54
    my $genre_field = MARC::Field->new($tags[2],0,2,$tags[3]=>'Science fiction');
55
    # Can we build a heading from it?
56
    my $subject_heading = C4::Heading->new_from_bib_field( $subject_field, q{} );
57
    my $subject_heading2 = C4::Heading->new_from_bib_field( $subject_field, q{} );
58
    my $genre_heading = C4::Heading->new_from_bib_field( $genre_field, q{} );
59
60
61
    # Now test to see if C4::Linker can find it.
62
    my $authmatch;
63
    my $fuzzy;
64
    my $linker = C4::Linker::Default->new();
65
66
    $linker->get_link($subject_heading);
67
    is( keys %{$linker->{cache}},1, "First term added to cache");
68
69
    $linker->get_link($genre_heading);
70
    is( keys %{$linker->{cache}},2, "Second (matching) term added to cache because of different type");
71
72
    $linker->get_link($subject_heading2);
73
    is( keys %{$linker->{cache}},2, "Third (matching) term not added to cache because of matching type");
74
75
76
    $linker->update_cache($subject_heading,32);
77
    is( $linker->{cache}->{$subject_heading->search_form.$subject_heading->auth_type}->{authid}, 32, "Linker cache is correctly updated by 'update_cache'");
78
    my ( $authid, undef ) = $linker->get_link($subject_heading);
79
    is( $authid, 32, "Correct id is retrieved from the cache" );
80
    ( $authid, undef ) = $linker->get_link($genre_heading);
81
    isnt( $authid, 32, "Genre term is not updated by update_cache");
82
};
83
84
$schema->storage->txn_rollback;

Return to bug 21450