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 {
32 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
33 'Creating new query builder object for authorities'
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"},
44 is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
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"},
55 is( $query->{query}->{bool}->{must}[1]->{wildcard}->{"_all.phrase"},
58 is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
60 is( $query->{query}->{bool}->{must}[1]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
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"},
71 is( $query->{query}->{bool}->{must}[0]->{term}->{$koha_to_index_name->{$koha_name}.".phrase"},
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"},
82 is( $query->{query}->{bool}->{must}[0]->{prefix}->{$koha_to_index_name->{$koha_name}.".lc_raw"},
89 $qb->build_authorities_query_compat( [ 'tomas' ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
91 'Koha::Exceptions::WrongParameter',
92 'Exception thrown on invalid value in the marclist param';
95 subtest 'build query from form subtests' => sub {
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');
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");
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");