Bug 21450: (QA follow-up) remove a couple of unused variables to make QA script happy
[koha.git] / t / db_dependent / Linker_Default.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Test::More tests => 2;
20
21 use MARC::Record;
22 use MARC::Field;
23 use MARC::File::XML;
24 use C4::Heading;
25 use C4::Linker::FirstMatch;
26 use Test::MockModule;
27 use t::lib::Mocks qw( mock_preference );
28 use t::lib::TestBuilder;
29
30 BEGIN {
31     use_ok('C4::Linker');
32 }
33
34 # Mock C4::Heading->authorities() so tests will all pass.
35 # This completely bypasses any search engine calls.
36 my $authid=0;
37 my $mock_heading = Test::MockModule->new('C4::Heading');
38 $mock_heading->mock( authorities => sub { return [ { authid => $authid++ } ]; } );
39
40 my $builder = t::lib::TestBuilder->new();
41 my $schema  = $builder->schema();
42 $schema->storage->txn_begin;
43
44 subtest 'Test caching in get_link and update_cache' => sub {
45     plan tests => 6;
46
47     my @tags = C4::Context->preference('marcflavour') eq 'UNIMARC' ? (601,'j',602,'a') : (650,'a',655,'a');
48
49     my $subject_field = MARC::Field->new($tags[0],0,2,$tags[1]=>'Science fiction');
50     my $subject_field2 = MARC::Field->new($tags[0],0,2,$tags[1]=>'Science fiction');
51     my $genre_field = MARC::Field->new($tags[2],0,2,$tags[3]=>'Science fiction');
52     # Can we build a heading from it?
53     my $subject_heading = C4::Heading->new_from_bib_field( $subject_field, q{} );
54     my $subject_heading2 = C4::Heading->new_from_bib_field( $subject_field, q{} );
55     my $genre_heading = C4::Heading->new_from_bib_field( $genre_field, q{} );
56
57
58     # Now test to see if C4::Linker can find it.
59     my $linker = C4::Linker::Default->new();
60
61     $linker->get_link($subject_heading);
62     is( keys %{$linker->{cache}},1, "First term added to cache");
63
64     $linker->get_link($genre_heading);
65     is( keys %{$linker->{cache}},2, "Second (matching) term added to cache because of different type");
66
67     $linker->get_link($subject_heading2);
68     is( keys %{$linker->{cache}},2, "Third (matching) term not added to cache because of matching type");
69
70
71     $linker->update_cache($subject_heading,32);
72     is( $linker->{cache}->{$subject_heading->search_form.$subject_heading->auth_type}->{authid}, 32, "Linker cache is correctly updated by 'update_cache'");
73     my ( $authid, undef ) = $linker->get_link($subject_heading);
74     is( $authid, 32, "Correct id is retrieved from the cache" );
75     ( $authid, undef ) = $linker->get_link($genre_heading);
76     isnt( $authid, 32, "Genre term is not updated by update_cache");
77 };
78
79 $schema->storage->txn_rollback;