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>.
24 use t::lib::TestBuilder;
25 use Test::More tests => 7;
27 use List::Util qw( all );
30 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
36 $se->mock( 'get_elasticsearch_mappings', sub {
54 'subject-heading-thesaurus' => {
64 sortablenumber__sort => {
82 'match-heading-see-from' => {
88 $all_mappings{$self->index} = $mappings;
99 $self->sort_fields($sort_fields->{$self->index});
101 return $all_mappings{$self->index};
104 subtest 'build_authorities_query_compat() tests' => sub {
111 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
112 'Creating new query builder object for authorities'
115 my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
116 my $search_term = 'a';
117 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
118 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
119 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
120 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
123 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
126 is( $query->{query}->{bool}->{must}[0]->{query_string}->{analyze_wildcard}, JSON::true, 'Set analyze_wildcard true' );
127 is( $query->{query}->{bool}->{must}[0]->{query_string}->{lenient}, JSON::true, 'Set lenient true' );
130 $search_term = 'Donald Duck';
131 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
132 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
133 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query}, "(Donald*) AND (Duck*)" );
134 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
135 isa_ok( $query->{query}->{bool}->{must}[0]->{query_string}->{fields}, 'ARRAY')
137 is( $query->{query}->{bool}->{must}[0]->{query_string}->{default_field}, $koha_to_index_name->{$koha_name} );
141 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
142 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['is'], [$search_term], 'AUTH_TYPE', 'asc' );
143 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
145 $query->{query}->{bool}->{must}[0]->{multi_match}->{query},
148 my $all_matches = all { /\.ci_raw$/ }
149 @{$query->{query}->{bool}->{must}[0]->{multi_match}->{fields}};
150 ok( $all_matches, 'Correct fields parameter for "is" query in "any" or "all"' );
153 $query->{query}->{bool}->{must}[0]->{term}->{$koha_to_index_name->{$koha_name} . ".ci_raw"},
159 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
160 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'asc' );
161 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
162 my $all_matches = all { (%{$_->{prefix}})[0] =~ /\.ci_raw$/ && (%{$_->{prefix}})[1] eq "Donald Duck" }
163 @{$query->{query}->{bool}->{must}[0]->{bool}->{should}};
164 ok( $all_matches, "Correct multiple prefix query" );
166 is( $query->{query}->{bool}->{must}[0]->{prefix}->{$koha_to_index_name->{$koha_name} . ".ci_raw"}, "Donald Duck" );
171 my $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'HeadingAsc' );
176 'heading__sort' => 'asc'
179 "ascending sort parameter properly formed"
181 $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'HeadingDsc' );
186 'heading__sort' => 'desc'
189 "descending sort parameter properly formed"
193 $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
195 $query->{query}->{bool}->{filter},
196 { term => { 'authtype.raw' => 'AUTH_TYPE' } },
197 "authorities type code is used as filter"
200 # Authorities marclist check
202 $query = $qb->build_authorities_query_compat( [ 'tomas','mainentry' ], undef, undef, ['contains'], [$search_term,$search_term], 'AUTH_TYPE', 'asc' )
204 qr/Unknown search field tomas/,
205 "Warning for unknown field in marclist";
207 $query->{query}->{bool}->{must}[0]->{query_string}->{default_field},
209 "If no mapping for marclist the index is passed through as defined"
212 $query->{query}->{bool}->{must}[1]->{query_string}{default_field},
214 "If mapping found for marclist the index is passed through converted"
219 subtest 'build_query tests' => sub {
225 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
226 'Creating new query builder object for biblios'
229 my @sort_by = 'title_asc';
230 my @sort_params = $qb->_convert_sort_fields(@sort_by);
232 $options{sort} = \@sort_params;
233 my $query = $qb->build_query('test', %options);
244 "sort parameter properly formed"
247 t::lib::Mocks::mock_preference('FacetMaxCount','37');
248 t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
249 $query = $qb->build_query('test', %options);
250 ok( defined $query->{aggregations}{ccode}{terms}{size},'we need to ask for a size or we get only 5 facet' );
251 is( $query->{aggregations}{ccode}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount');
252 is( $query->{aggregations}{homebranch}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount for homebranch');
253 is( $query->{aggregations}{holdingbranch}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount for holdingbranch');
255 t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
256 $query = $qb->build_query();
257 ok( defined $query->{aggregations}{homebranch},
258 'homebranch added to facets if DisplayLibraryFacets=both' );
259 ok( defined $query->{aggregations}{holdingbranch},
260 'holdingbranch added to facets if DisplayLibraryFacets=both' );
261 t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
262 $query = $qb->build_query();
263 ok( !defined $query->{aggregations}{homebranch},
264 'homebranch not added to facets if DisplayLibraryFacets=holding' );
265 ok( defined $query->{aggregations}{holdingbranch},
266 'holdingbranch added to facets if DisplayLibraryFacets=holding' );
267 t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
268 $query = $qb->build_query();
269 ok( defined $query->{aggregations}{homebranch},
270 'homebranch added to facets if DisplayLibraryFacets=home' );
271 ok( !defined $query->{aggregations}{holdingbranch},
272 'holdingbranch not added to facets if DisplayLibraryFacets=home' );
274 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
276 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
278 $query->{query}{query_string}{query},
280 "query not altered if QueryAutoTruncate disabled"
283 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
285 $query->{query}{query_string}{query},
287 "keyword as phrase correctly quotes search term and strips index"
290 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
292 $query->{query}{query_string}{query},
293 '(title:(donald duck))',
294 'multiple words in a query term are enclosed in parenthesis'
297 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
299 $query->{query}{query_string}{query},
300 '(title:(donald duck)) AND (author:disney)',
301 'multiple query terms are enclosed in parenthesis while a single one is not'
304 my ($simple_query, $query_cgi, $query_desc);
305 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['"donald duck"', 'walt disney'], ['ti', 'au'] );
306 is($query_cgi, 'idx=ti&q=%22donald%20duck%22&idx=au&q=walt%20disney', 'query cgi ok for multiterm query');
307 is($query_desc, '(title:("donald duck")) (author:(walt disney))', 'query desc ok for multiterm query');
309 ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
311 $query->{query}{query_string}{query},
312 '(date-of-publication:2019)',
313 'Year in an st-year search is handled properly'
316 ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
318 $query->{query}{query_string}{query},
319 '(date-of-publication:[2018 TO 2019])',
320 'Year range in an st-year search is handled properly'
323 ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
325 $query->{query}{query_string}{query},
326 '(date-of-publication:[* TO 2019])',
327 'Open start year in year range of an st-year search is handled properly'
330 ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
332 $query->{query}{query_string}{query},
333 '(date-of-publication:[2019 TO *])',
334 'Open end year in year range of an st-year search is handled properly'
337 ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
339 $query->{query}{query_string}{query},
340 '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
341 'Open end year in year range of an st-year search is handled properly'
344 # Enable auto-truncation
345 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
347 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
349 $query->{query}{query_string}{query},
351 "simple query is auto truncated when QueryAutoTruncate enabled"
354 # Ensure reserved words are not truncated
355 ( undef, $query ) = $qb->build_query_compat( undef,
356 ['donald or duck and mickey not mouse'] );
358 $query->{query}{query_string}{query},
359 "(donald* or duck* and mickey* not mouse*)",
360 "reserved words are not affected by QueryAutoTruncate"
363 ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
365 $query->{query}{query_string}{query},
367 "query with '*' is unaltered when QueryAutoTruncate is enabled"
370 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
372 $query->{query}{query_string}{query},
373 "(donald* duck* and the* mouse*)",
374 "individual words are all truncated and stopwords ignored"
377 ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
379 $query->{query}{query_string}{query},
381 "query of just '*' is unaltered when QueryAutoTruncate is enabled"
384 ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
386 $query->{query}{query_string}{query},
387 '("donald duck") AND onloan:false',
388 "query with quotes is unaltered when QueryAutoTruncate is enabled"
392 ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
394 $query->{query}{query_string}{query},
395 '("donald duck" and "the mouse")',
396 "all quoted strings are unaltered if more than one in query"
399 ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
401 $query->{query}{query_string}{query},
403 "query of specific field is truncated"
406 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:"123456"'] );
408 $query->{query}{query_string}{query},
409 '(local-number:"123456")',
410 "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
413 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
415 $query->{query}{query_string}{query},
416 '(local-number:123456*)',
417 "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
420 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:123456'] );
422 $query->{query}{query_string}{query},
423 '(local-number.raw:123456*)',
424 "query of specific field including period and not quoted is truncated, field name is converted to lower case"
427 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:"123456"'] );
429 $query->{query}{query_string}{query},
430 '(local-number.raw:"123456")',
431 "query of specific field including period and quoted is not truncated, field name is converted to lower case"
434 ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
436 $query->{query}{query_string}{query},
438 "query including period is truncated but not split at periods"
441 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
443 $query->{query}{query_string}{query},
444 '(title:"donald duck")',
445 "query of specific field is not truncated when surrounded by quotes"
448 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
450 $query->{query}{query_string}{query},
451 '(title:(donald* duck*))',
452 'words of a multi-word term are properly truncated'
455 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
457 $query->{query}{query_string}{query},
458 '(title:(donald* duck*)) AND (author:disney*)',
459 'words of a multi-word term and single-word term are properly truncated'
462 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
464 $query->{query}{query_string}{query},
465 '(title:"donald duck") AND suppress:false',
466 "query of specific field is added AND suppress:false"
469 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
471 $query->{query}{query_string}{query},
472 '(title:"donald duck")',
473 "query of specific field is not added AND suppress:0"
476 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
478 $query->{query}{query_string}{query},
479 '(title:"donald duck") AND author:("Dillinger Escaplan")',
480 "Simple query with limit term quoted in parentheses"
483 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
485 $query->{query}{query_string}{query},
486 '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
487 "Simple query with each limit's term quoted in parentheses"
489 is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
490 is($query_desc, 'title:"donald duck"', 'query desc ok');
492 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
494 $query->{query}{query_string}{query},
495 '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
496 "Limits quoted correctly when passed as phrase"
500 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
502 $query->{query}{query_string}{query},
504 "scan query is properly formed"
507 $query->{aggregations}{'author'}{'terms'},
509 field => 'author__facet',
510 order => { '_key' => 'asc' },
511 include => '[nN][eE][wW].*'
513 "scan aggregation request is properly formed"
515 is($query_cgi, 'idx=au&q=new&scan=1', 'query cgi');
516 is($query_desc, 'new', 'query desc ok');
518 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], [], undef, undef, 1 );
520 $query->{query}{query_string}{query},
522 "scan query is properly formed"
525 $query->{aggregations}{'subject'}{'terms'},
527 field => 'subject__facet',
528 order => { '_key' => 'asc' },
529 include => '[nN][eE][wW].*'
531 "scan aggregation request is properly formed"
533 is($query_cgi, 'idx=&q=new&scan=1', 'query cgi');
534 is($query_desc, 'new', 'query desc ok');
536 my( $limit, $limit_cgi, $limit_desc );
537 ( undef, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( ['AND'], ['kw:""'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
538 is( $limit, '(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))', "Limit formed correctly when no search terms");
539 is( $limit_cgi,'&limit=author%3ADillinger%20Escaplan&limit=mc-itype%2Cphr%3ABOOK&limit=mc-itype%2Cphr%3ACD', "Limit CGI formed correctly when no search terms");
540 is( $limit_desc,'(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',"Limit desc formed correctly when no search terms");
544 subtest 'build query from form subtests' => sub {
547 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
548 #when searching for authorities from a record the form returns marclist with blanks for unentered terms
549 my @marclist = ('mainmainentry','mainentry','match', 'all');
550 my @values = ( undef, 'Hamilton', undef, undef);
551 my @operator = ( 'contains', 'contains', 'contains', 'contains');
553 my $query = $qb->build_authorities_query_compat( \@marclist, undef,
554 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
555 is($query->{query}->{bool}->{must}[0]->{query_string}->{query}, "Hamilton*","Expected search is populated");
556 is( scalar @{ $query->{query}->{bool}->{must} }, 1,"Only defined search is populated");
558 @values[2] = 'Jefferson';
559 $query = $qb->build_authorities_query_compat( \@marclist, undef,
560 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
561 is($query->{query}->{bool}->{must}[0]->{query_string}->{query}, "Hamilton*","First index searched as expected");
562 is($query->{query}->{bool}->{must}[1]->{query_string}->{query}, "Jefferson*","Second index searched when populated");
563 is( scalar @{ $query->{query}->{bool}->{must} }, 2,"Only defined searches are populated");
568 subtest 'build_query with weighted fields tests' => sub {
571 $se->mock( '_load_elasticsearch_mappings', sub {
581 marc_type => 'marc21',
585 label => 'headingmain',
591 marc_type => 'marc21',
603 marc_type => 'marc21',
612 marc_field => '952d',
613 marc_type => 'marc21',
616 marc_field => '9955',
617 marc_type => 'marc21',
628 marc_type => 'marc21'
637 marc_field => '600a',
638 marc_type => 'marc21'
645 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
646 Koha::SearchFields->search({})->delete;
647 Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
650 $search_field = Koha::SearchFields->find({ name => 'title' });
651 $search_field->update({ weight => 25.0 });
652 $search_field = Koha::SearchFields->find({ name => 'subject' });
653 $search_field->update({ weight => 15.5 });
654 Koha::SearchEngine::Elasticsearch->clear_search_fields_cache();
656 my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
657 undef, undef, undef, { weighted_fields => 1 });
659 my $fields = $query->{query}{query_string}{fields};
661 is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
663 my @found = grep { $_ eq 'title^25.00' } @{$fields};
664 is(@found, 1, 'Search field title has correct weight');
666 @found = grep { $_ eq 'subject^15.50' } @{$fields};
667 is(@found, 1, 'Search field subject has correct weight');
669 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
670 undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
672 $fields = $query->{query}{query_string}{fields};
677 'Only OPAC search fields are used when opac search is performed'
680 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'authorities' } );
681 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
682 undef, undef, undef, { weighted_fields => 1 });
683 $fields = $query->{query}{query_string}{fields};
684 is_deeply( [sort @$fields], ['heading','headingmain'],'Authorities fields retrieve for authorities index');
686 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
687 undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
688 $fields = $query->{query}{query_string}{fields};
689 is_deeply($fields,['headingmain'],'Only opac authorities fields retrieved for authorities index is is_opac');
693 subtest 'build_query_compat() SearchLimitLibrary tests' => sub {
697 $schema->storage->txn_begin;
699 my $builder = t::lib::TestBuilder->new;
701 my $branch_1 = $builder->build_object({ class => 'Koha::Libraries' });
702 my $branch_2 = $builder->build_object({ class => 'Koha::Libraries' });
703 my $group = $builder->build_object({ class => 'Koha::Library::Groups', value => {
704 ft_search_groups_opac => 1,
705 ft_search_groups_staff => 1,
710 my $group_1 = $builder->build_object({ class => 'Koha::Library::Groups', value => {
711 parent_id => $group->id,
712 branchcode => $branch_1->id
715 my $group_2 = $builder->build_object({ class => 'Koha::Library::Groups', value => {
716 parent_id => $group->id,
717 branchcode => $branch_2->id
720 my $groupid = $group->id;
721 my @branchcodes = sort { $a cmp $b } ( $branch_1->id, $branch_2->id );
724 my $query_builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
725 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'both');
726 my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
727 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
728 is( $limit, "(homebranch: CPL OR holdingbranch: CPL)", "Branch limit expanded to home/holding branch");
729 is( $limit_desc, "(homebranch: CPL OR holdingbranch: CPL)", "Limit description correctly expanded");
730 is( $limit_cgi, "&limit=branch%3ACPL", "Limit cgi does not get expanded");
731 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
732 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
733 is( $limit, "(homebranch: $branchcodes[0] OR homebranch: $branchcodes[1] OR holdingbranch: $branchcodes[0] OR holdingbranch: $branchcodes[1])", "Multibranch limit expanded to home/holding branches");
734 is( $limit_desc, "(homebranch: $branchcodes[0] OR homebranch: $branchcodes[1] OR holdingbranch: $branchcodes[0] OR holdingbranch: $branchcodes[1])", "Multibranch limit description correctly expanded");
735 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Multibranch limit cgi does not get expanded");
737 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'homebranch');
738 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
739 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
740 is( $limit, "(homebranch: CPL)", "branch limit expanded to home branch");
741 is( $limit_desc, "(homebranch: CPL)", "limit description correctly expanded");
742 is( $limit_cgi, "&limit=branch%3ACPL", "limit cgi does not get expanded");
743 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
744 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
745 is( $limit, "(homebranch: $branchcodes[0] OR homebranch: $branchcodes[1])", "branch limit expanded to home branch");
746 is( $limit_desc, "(homebranch: $branchcodes[0] OR homebranch: $branchcodes[1])", "limit description correctly expanded");
747 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
749 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'holdingbranch');
750 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
751 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
752 is( $limit, "(holdingbranch: CPL)", "branch limit expanded to holding branch");
753 is( $limit_desc, "(holdingbranch: CPL)", "Limit description correctly expanded");
754 is( $limit_cgi, "&limit=branch%3ACPL", "Limit cgi does not get expanded");
755 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
756 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
757 is( $limit, "(holdingbranch: $branchcodes[0] OR holdingbranch: $branchcodes[1])", "branch limit expanded to holding branch");
758 is( $limit_desc, "(holdingbranch: $branchcodes[0] OR holdingbranch: $branchcodes[1])", "Limit description correctly expanded");
759 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
763 subtest "_convert_sort_fields() tests" => sub {
769 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
770 'Creating new query builder object for biblios'
773 my @sort_by = $qb->_convert_sort_fields(qw( call_number_asc author_dsc ));
777 { field => 'cn-sort', direction => 'asc' },
778 { field => 'author', direction => 'desc' }
780 'sort fields should have been split correctly'
783 # We could expect this to pass, but direction is undef instead of 'desc'
784 @sort_by = $qb->_convert_sort_fields(qw( call_number_asc author_desc ));
788 { field => 'cn-sort', direction => 'asc' },
789 { field => 'author', direction => 'desc' }
791 'sort fields should have been split correctly'
795 subtest "_sort_field() tests" => sub {
801 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
802 'Creating new query builder object for biblios'
805 my $f = $qb->_sort_field('title');
809 'title sort mapped correctly'
812 $f = $qb->_sort_field('subject');
816 'subject sort mapped correctly'
819 $f = $qb->_sort_field('itemnumber');
823 'itemnumber sort mapped correctly'
826 $f = $qb->_sort_field('sortablenumber');
829 'sortablenumber__sort',
830 'sortablenumber sort mapped correctly'
834 $schema->storage->txn_rollback;