3 # This file is part of Koha.
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.
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.
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>.
19 use Test::More tests => 2;
24 use C4::Heading qw( authorities field new_from_field auth_type search_form );
25 use C4::Linker::Default;
27 use t::lib::Mocks qw( mock_preference );
28 use t::lib::TestBuilder;
34 # Mock C4::Heading->authorities() so tests will all pass.
35 # This completely bypasses any search engine calls.
37 my $mock_heading = Test::MockModule->new('C4::Heading');
38 $mock_heading->mock( authorities => sub { return [ { authid => $authid++ } ]; } );
40 my $builder = t::lib::TestBuilder->new();
41 my $schema = $builder->schema();
42 $schema->storage->txn_begin;
44 subtest 'Test caching in get_link and update_cache' => sub {
47 my @tags = C4::Context->preference('marcflavour') eq 'UNIMARC' ? (601,'j',602,'a') : (650,'a',655,'a');
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 $subject_field3 = MARC::Field->new($tags[0],0,0,$tags[1]=>'Science fiction');
52 my $subject_field4 = MARC::Field->new($tags[0],0,7,$tags[1]=>'Science fiction','2'=>'bnb');
53 my $subject_field5 = MARC::Field->new($tags[0],0,7,$tags[1]=>'Science fiction','2'=>'sao');
54 my $subject_field6 = MARC::Field->new($tags[0],0,7,$tags[1]=>'Science fiction','2'=>'sao');
55 my $subject_field7 = MARC::Field->new($tags[0],0,4,$tags[1]=>'Science fiction');
56 my $genre_field = MARC::Field->new($tags[2],0,2,$tags[3]=>'Science fiction');
57 # Can we build a heading from it?
58 my $subject_heading = C4::Heading->new_from_field( $subject_field, q{} );
59 my $subject_heading2 = C4::Heading->new_from_field( $subject_field2, q{} );
60 my $subject_heading3 = C4::Heading->new_from_field( $subject_field3, q{} );
61 my $subject_heading4 = C4::Heading->new_from_field( $subject_field4, q{} );
62 my $subject_heading5 = C4::Heading->new_from_field( $subject_field5, q{} );
63 my $subject_heading6 = C4::Heading->new_from_field( $subject_field6, q{} );
64 my $subject_heading7 = C4::Heading->new_from_field( $subject_field7, q{} );
65 my $genre_heading = C4::Heading->new_from_field( $genre_field, q{} );
68 # Now test to see if C4::Linker can find it.
69 my $linker = C4::Linker::Default->new();
71 $linker->get_link($subject_heading);
72 is( keys %{$linker->{cache}},1, "First term added to cache");
74 $linker->get_link($genre_heading);
75 is( keys %{$linker->{cache}},2, "Second (matching) term added to cache because of different type");
77 $linker->get_link($subject_heading2);
78 is( keys %{$linker->{cache}},2, "Third (matching) term not added to cache because of matching type");
80 $linker->get_link($subject_heading3);
81 is( keys %{$linker->{cache}},3, "Fourth (matching) term added to cache because of different thesaurus (lcsh)");
83 $linker->get_link($subject_heading4);
84 is( keys %{$linker->{cache}},4, "Fifth (matching) term added to cache because of different thesaurus (bnb)");
86 $linker->get_link($subject_heading5);
87 is( keys %{$linker->{cache}},5, "Sixth (matching) term added to cache because of different thesaurus (sao)");
89 $linker->get_link($subject_heading6);
90 is( keys %{$linker->{cache}},5, "Seventh (matching) term not added to cache because of matching type and thesaurus (sao)");
92 $linker->get_link($subject_heading7);
93 is( keys %{$linker->{cache}},6, "Eighth (matching) term added to cache because of thesaurus source not specified (2nd indicator is 4)");
95 $linker->update_cache($subject_heading,32);
96 is( $linker->{cache}->{$subject_heading->search_form.$subject_heading->auth_type.$subject_heading->{'thesaurus'}}->{authid}, 32, "Linker cache is correctly updated by 'update_cache'");
97 my ( $authid, undef ) = $linker->get_link($subject_heading);
98 is( $authid, 32, "Correct id is retrieved from the cache" );
99 ( $authid, undef ) = $linker->get_link($genre_heading);
100 isnt( $authid, 32, "Genre term is not updated by update_cache");
101 ( $authid, undef ) = $linker->get_link($subject_heading5);
102 is( $authid, 4, "Correct id for sao term is retrieved from the cache" );
103 $linker->update_cache($subject_heading4,78);
104 is( $linker->{cache}->{$subject_heading4->search_form.$subject_heading4->auth_type.$subject_heading4->{'thesaurus'}}->{authid}, 78, "Linker cache for the bnb record is correctly updated by 'update_cache'");
107 $schema->storage->txn_rollback;