Bug 25669: (follow-up) Minor fixes
[koha-ffzg.git] / t / db_dependent / Koha / SearchEngine / Elasticsearch / QueryBuilder.t
old mode 100644 (file)
new mode 100755 (executable)
index a9d886e..4e8964e
@@ -22,7 +22,7 @@ use Test::Exception;
 use Test::Warn;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
-use Test::More tests => 6;
+use Test::More tests => 7;
 
 use List::Util qw( all );
 
@@ -39,50 +39,48 @@ $se->mock( 'get_elasticsearch_mappings', sub {
     my %all_mappings;
 
     my $mappings = {
-        data => {
-            properties => {
-                title => {
-                    type => 'text'
-                },
-                title__sort => {
-                    type => 'text'
-                },
-                subject => {
-                    type => 'text',
-                    facet => 1
-                },
-                'subject-heading-thesaurus' => {
-                    type => 'text',
-                    facet => 1
-                },
-                itemnumber => {
-                    type => 'integer'
-                },
-                sortablenumber => {
-                    type => 'integer'
-                },
-                sortablenumber__sort => {
-                    type => 'integer'
-                },
-                heading => {
-                    type => 'text'
-                },
-                'heading-main' => {
-                    type => 'text'
-                },
-                heading__sort => {
-                    type => 'text'
-                },
-                match => {
-                    type => 'text'
-                },
-                'match-heading' => {
-                    type => 'text'
-                },
-                'match-heading-see-from' => {
-                    type => 'text'
-                },
-            }
+        properties => {
+            title => {
+                type => 'text'
+            },
+            title__sort => {
+                type => 'text'
+            },
+            subject => {
+                type => 'text',
+                facet => 1
+            },
+            'subject-heading-thesaurus' => {
+                type => 'text',
+                facet => 1
+            },
+            itemnumber => {
+                type => 'integer'
+            },
+            sortablenumber => {
+                type => 'integer'
+            },
+            sortablenumber__sort => {
+                type => 'integer'
+            },
+            heading => {
+                type => 'text'
+            },
+            'heading-main' => {
+                type => 'text'
+            },
+            heading__sort => {
+                type => 'text'
+            },
+            match => {
+                type => 'text'
+            },
+            'match-heading' => {
+                type => 'text'
+            },
+            'match-heading-see-from' => {
+                type => 'text'
+            },
         }
     };
     $all_mappings{$self->index} = $mappings;
@@ -103,7 +101,7 @@ $se->mock( 'get_elasticsearch_mappings', sub {
 
 subtest 'build_authorities_query_compat() tests' => sub {
 
-    plan tests => 57;
+    plan tests => 65;
 
     my $qb;
 
@@ -124,6 +122,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
                 "a*");
         }
         is( $query->{query}->{bool}->{must}[0]->{query_string}->{analyze_wildcard}, JSON::true, 'Set analyze_wildcard true' );
+        is( $query->{query}->{bool}->{must}[0]->{query_string}->{lenient}, JSON::true, 'Set lenient true' );
     }
 
     $search_term = 'Donald Duck';
@@ -216,7 +215,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
 };
 
 subtest 'build_query tests' => sub {
-    plan tests => 52;
+    plan tests => 57;
 
     my $qb;
 
@@ -279,6 +278,13 @@ subtest 'build_query tests' => sub {
         "query not altered if QueryAutoTruncate disabled"
     );
 
+    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
+    is(
+        $query->{query}{query_string}{query},
+        '("donald duck")',
+        "keyword as phrase correctly quotes search term and strips index"
+    );
+
     ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
     is(
         $query->{query}{query_string}{query},
@@ -329,7 +335,7 @@ subtest 'build_query tests' => sub {
     ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
     is(
         $query->{query}{query_string}{query},
-        '(date-of-publication:[2019 TO *]) AND copydate:[* TO 2019]',
+        '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
         'Open end year in year range of an st-year search is handled properly'
     );
 
@@ -481,6 +487,13 @@ subtest 'build_query tests' => sub {
     is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
     is($query_desc, 'title:"donald duck"', 'query desc ok');
 
+    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
+        "Limits quoted correctly when passed as phrase"
+    );
+
     # Scan queries
     ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
     is(
@@ -492,7 +505,7 @@ subtest 'build_query tests' => sub {
         $query->{aggregations}{'author'}{'terms'},
         {
             field => 'author__facet',
-            order => { '_term' => 'asc' },
+            order => { '_key' => 'asc' },
             include => '[nN][eE][wW].*'
         },
         "scan aggregation request is properly formed"
@@ -510,13 +523,19 @@ subtest 'build_query tests' => sub {
         $query->{aggregations}{'subject'}{'terms'},
         {
             field => 'subject__facet',
-            order => { '_term' => 'asc' },
+            order => { '_key' => 'asc' },
             include => '[nN][eE][wW].*'
         },
         "scan aggregation request is properly formed"
     );
     is($query_cgi, 'idx=&q=new&scan=1', 'query cgi');
     is($query_desc, 'new', 'query desc ok');
+
+    my( $limit, $limit_cgi, $limit_desc );
+    ( 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'] );
+    is( $limit, '(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))', "Limit formed correctly when no search terms");
+    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");
+    is( $limit_desc,'(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',"Limit desc formed correctly when no search terms");
 };
 
 
@@ -669,6 +688,76 @@ subtest 'build_query with weighted fields tests' => sub {
 
 };
 
+subtest 'build_query_compat() SearchLimitLibrary tests' => sub {
+
+    plan tests => 18;
+
+    $schema->storage->txn_begin;
+
+    my $builder = t::lib::TestBuilder->new;
+
+    my $branch_1 = $builder->build_object({ class => 'Koha::Libraries' });
+    my $branch_2 = $builder->build_object({ class => 'Koha::Libraries' });
+    my $group    = $builder->build_object({ class => 'Koha::Library::Groups', value => {
+            ft_search_groups_opac => 1,
+            ft_search_groups_staff => 1,
+            parent_id => undef,
+            branchcode => undef
+        }
+    });
+    my $group_1  = $builder->build_object({ class => 'Koha::Library::Groups', value => {
+            parent_id => $group->id,
+            branchcode => $branch_1->id
+        }
+    });
+    my $group_2  = $builder->build_object({ class => 'Koha::Library::Groups', value => {
+            parent_id => $group->id,
+            branchcode => $branch_2->id
+        }
+    });
+    my $groupid = $group->id;
+    my @branchcodes = sort { $a cmp $b } ( $branch_1->id, $branch_2->id );
+
+
+    my $query_builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
+    t::lib::Mocks::mock_preference('SearchLimitLibrary', 'both');
+    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
+        $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
+    is( $limit, '(homebranch: "CPL" OR holdingbranch: "CPL")', "Branch limit expanded to home/holding branch");
+    is( $limit_desc, '(homebranch: "CPL" OR holdingbranch: "CPL")', "Limit description correctly expanded");
+    is( $limit_cgi, '&limit=branch%3ACPL', "Limit cgi does not get expanded");
+    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
+        $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
+    is( $limit, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\" OR holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "Multibranch limit expanded to home/holding branches");
+    is( $limit_desc, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\" OR holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "Multibranch limit description correctly expanded");
+    is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Multibranch limit cgi does not get expanded");
+
+    t::lib::Mocks::mock_preference('SearchLimitLibrary', 'homebranch');
+    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
+        $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
+    is( $limit, "(homebranch: \"CPL\")", "branch limit expanded to home branch");
+    is( $limit_desc, "(homebranch: \"CPL\")", "limit description correctly expanded");
+    is( $limit_cgi, "&limit=branch%3ACPL", "limit cgi does not get expanded");
+    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
+        $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
+    is( $limit, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\")", "branch limit expanded to home branch");
+    is( $limit_desc, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\")", "limit description correctly expanded");
+    is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
+
+    t::lib::Mocks::mock_preference('SearchLimitLibrary', 'holdingbranch');
+    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
+        $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
+    is( $limit, "(holdingbranch: \"CPL\")", "branch limit expanded to holding branch");
+    is( $limit_desc, "(holdingbranch: \"CPL\")", "Limit description correctly expanded");
+    is( $limit_cgi, "&limit=branch%3ACPL", "Limit cgi does not get expanded");
+    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
+        $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
+    is( $limit, "(holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "branch limit expanded to holding branch");
+    is( $limit_desc, "(holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "Limit description correctly expanded");
+    is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
+
+};
+
 subtest "_convert_sort_fields() tests" => sub {
     plan tests => 3;
 
@@ -683,7 +772,7 @@ subtest "_convert_sort_fields() tests" => sub {
     is_deeply(
         \@sort_by,
         [
-            { field => 'local-classification', direction => 'asc' },
+            { field => 'cn-sort', direction => 'asc' },
             { field => 'author',  direction => 'desc' }
         ],
         'sort fields should have been split correctly'
@@ -694,7 +783,7 @@ subtest "_convert_sort_fields() tests" => sub {
     is_deeply(
         \@sort_by,
         [
-            { field => 'local-classification', direction => 'asc' },
+            { field => 'cn-sort', direction => 'asc' },
             { field => 'author',  direction => 'desc' }
         ],
         'sort fields should have been split correctly'