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>.
20 use Test::More tests => 2;
24 use Koha::SearchEngine::Elasticsearch::QueryBuilder
26 subtest 'build_authorities_query_compat() tests' => sub {
31 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
32 'Creating new query builder object for authorities'
35 my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
36 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}->{should}[0]->{match}->{_all},
44 is( $query->{query}->{bool}->{should}[0]->{match}->{$koha_to_index_name->{$koha_name}},
51 $qb->build_authorities_query_compat( [ 'tomas' ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
53 'Koha::Exceptions::WrongParameter',
54 'Exception thrown on invalid value in the marclist param';
57 subtest 'build query from form subtests' => sub {
60 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
61 #when searching for authorities from a record the form returns marclist with blanks for unentered terms
62 my @marclist = ('mainmainentry','mainentry','match', 'all');
63 my @values = ( undef, 'Hamilton', undef, undef);
64 my @operator = ( 'contains', 'contains', 'contains', 'contains');
66 my $query = $builder->build_authorities_query_compat( \@marclist, undef,
67 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
68 is($query->{query}->{bool}->{should}[0]->{match}->{'Heading'}, "Hamilton","Expected search is populated");
69 is( scalar @{ $query->{query}->{bool}->{should} }, 1,"Only defined search is populated");
71 @values[2] = 'Jefferson';
72 $query = $builder->build_authorities_query_compat( \@marclist, undef,
73 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
74 is($query->{query}->{bool}->{should}[0]->{match}->{'Heading'}, "Hamilton","First index searched as expected");
75 is($query->{query}->{bool}->{should}[1]->{match}->{'Match'}, "Jefferson","Second index searched when populated");
76 is( scalar @{ $query->{query}->{bool}->{should} }, 2,"Only defined searches are populated");