0a8f4cf57dabee83c2fd727225b1e8e28ccb4a76
[koha.git] / t / db_dependent / Koha / SearchEngine / Elasticsearch / QueryBuilder.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
20 use Test::More tests => 2;
21 use Test::Exception;
22
23 use Koha::Database;
24 use Koha::SearchEngine::Elasticsearch::QueryBuilder
25
26 subtest 'build_authorities_query_compat() tests' => sub {
27     plan tests => 37;
28
29     my $qb;
30
31     ok(
32         $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
33         'Creating new query builder object for authorities'
34     );
35
36     my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
37     my $search_term = 'a';
38     foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
39         my $query = $qb->build_authorities_query_compat( [ $koha_name ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
40         if ( $koha_name eq 'all' ) {
41             is( $query->{query}->{bool}->{must}[0]->{wildcard}->{"_all.phrase"},
42                 "*a*");
43         } else {
44             is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
45                 "*a*");
46         }
47     }
48
49     $search_term = 'Donald Duck';
50     foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
51         my $query = $qb->build_authorities_query_compat( [ $koha_name ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
52         if ( $koha_name eq 'all' ) {
53             is( $query->{query}->{bool}->{must}[0]->{wildcard}->{"_all.phrase"},
54                 "*donald*");
55             is( $query->{query}->{bool}->{must}[1]->{wildcard}->{"_all.phrase"},
56                 "*duck*");
57         } else {
58             is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
59                 "*donald*");
60             is( $query->{query}->{bool}->{must}[1]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
61                 "*duck*");
62         }
63     }
64
65     foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
66         my $query = $qb->build_authorities_query_compat( [ $koha_name ],  undef, undef, ['is'], [$search_term], 'AUTH_TYPE', 'asc' );
67         if ( $koha_name eq 'all' ) {
68             is( $query->{query}->{bool}->{must}[0]->{term}->{"_all.phrase"},
69                 "donald duck");
70         } else {
71             is( $query->{query}->{bool}->{must}[0]->{term}->{$koha_to_index_name->{$koha_name}.".phrase"},
72                 "donald duck");
73         }
74     }
75
76     foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
77         my $query = $qb->build_authorities_query_compat( [ $koha_name ],  undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'asc' );
78         if ( $koha_name eq 'all' ) {
79             is( $query->{query}->{bool}->{must}[0]->{prefix}->{"_all.lc_raw"},
80                 "donald duck");
81         } else {
82             is( $query->{query}->{bool}->{must}[0]->{prefix}->{$koha_to_index_name->{$koha_name}.".lc_raw"},
83                 "donald duck");
84         }
85     }
86
87     # Failing case
88     throws_ok {
89         $qb->build_authorities_query_compat( [ 'tomas' ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
90     }
91     'Koha::Exceptions::WrongParameter',
92         'Exception thrown on invalid value in the marclist param';
93 };
94
95 subtest 'build query from form subtests' => sub {
96     plan tests => 5;
97
98     my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
99     #when searching for authorities from a record the form returns marclist with blanks for unentered terms
100     my @marclist = ('mainmainentry','mainentry','match', 'all');
101     my @values   = ( undef,         'Hamilton',  undef,   undef);
102     my @operator = ( 'contains', 'contains', 'contains', 'contains');
103
104     my $query = $builder->build_authorities_query_compat( \@marclist, undef,
105                     undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
106     is($query->{query}->{bool}->{must}[0]->{wildcard}->{'Heading.phrase'}, "*hamilton*","Expected search is populated");
107     is( scalar @{ $query->{query}->{bool}->{must} }, 1,"Only defined search is populated");
108
109     @values[2] = 'Jefferson';
110     $query = $builder->build_authorities_query_compat( \@marclist, undef,
111                     undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
112     is($query->{query}->{bool}->{must}[0]->{wildcard}->{'Heading.phrase'}, "*hamilton*","First index searched as expected");
113     is($query->{query}->{bool}->{must}[1]->{wildcard}->{'Match.phrase'}, "*jefferson*","Second index searched when populated");
114     is( scalar @{ $query->{query}->{bool}->{must} }, 2,"Only defined searches are populated");
115
116
117 };