Bug 32178: Remove security breach introduced in bug 31378
[koha.git] / t / AuthoritiesMarc_MARC21.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!  
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8
9 use Test::MockModule;
10 use Test::MockObject;
11 use Test::More tests => 5;
12 use t::lib::Mocks;
13 use MARC::Record;
14
15 use C4::AuthoritiesMarc qw( FindDuplicateAuthority );
16
17 BEGIN {
18         use_ok('C4::AuthoritiesMarc::MARC21', qw( default_auth_type_location fix_marc21_auth_type_location ));
19 }
20
21 my @result = C4::AuthoritiesMarc::MARC21::default_auth_type_location();
22 ok($result[0] eq '942', "testing default_auth_type_location has first value '942'");
23 ok($result[1] eq 'a', "testing default_auth_type_location has first value 'a'");
24
25 my $marc_record = MARC::Record->new();
26 is(C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($marc_record, '', ''), undef, "testing fix_marc21_auth_type_location returns undef with empty MARC record");
27
28 subtest "FindDuplicateAuthority tests" => sub {
29     plan tests => 2;
30     my $zebra_search_module = Test::MockModule->new( 'C4::Search' );
31     $zebra_search_module->mock( 'SimpleSearch', sub {
32         my $query = shift;
33         return ( undef, [$query] );
34     });
35     $zebra_search_module->mock( 'new_record_from_zebra', sub {
36         my (undef, $query ) = @_;
37         my $marc = MARC::Record->new;
38         $marc->append_fields(
39             MARC::Field->new( '001', $query ),
40         );
41         return $marc;
42     });
43     my $es_search_module = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch::Search' );
44     $es_search_module->mock( 'simple_search_compat', sub {
45         my (undef, $query) = @_;
46         return ( undef, [$query] );
47     });
48
49     my $record = MARC::Record->new;
50     $record->append_fields(
51         MARC::Field->new('155', '', '', a => 'Potato' ),
52     );
53
54     t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' );
55     my ($query) = FindDuplicateAuthority( $record, "GENRE/FORM" );
56     is( $query, q{at:"GENRE/FORM"  AND he:"Potato"}, "Query formed correctly for Zebra");
57
58     t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' );
59     ($query) = FindDuplicateAuthority( $record, "GENRE/FORM" );
60     is( $query, q{at:"GENRE/FORM"  AND he:"Potato"}, "Query formed correctly for Elasticsearch");
61
62 };