Bug 17989: Extend bad template check
[koha.git] / t / db_dependent / Koha_SearchEngine_Elasticsearch_Search.t
index 2a77dad..b96a8b3 100644 (file)
@@ -17,7 +17,8 @@
 
 use Modern::Perl;
 
-use Test::More tests => 11;
+use Test::More tests => 12;
+use t::lib::Mocks;
 
 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
 
@@ -79,4 +80,117 @@ subtest 'json2marc' => sub {
 
 };
 
-1;
+subtest 'build_query tests' => sub {
+    plan tests => 18;
+
+    t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
+    my $query = $builder->build_query();
+    ok( defined $query->{aggregations}{homebranch},
+        'homebranch added to facets if DisplayLibraryFacets=both' );
+    ok( defined $query->{aggregations}{holdingbranch},
+        'holdingbranch added to facets if DisplayLibraryFacets=both' );
+    t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
+    $query = $builder->build_query();
+    ok( !defined $query->{aggregations}{homebranch},
+        'homebranch not added to facets if DisplayLibraryFacets=holding' );
+    ok( defined $query->{aggregations}{holdingbranch},
+        'holdingbranch added to facets if DisplayLibraryFacets=holding' );
+    t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
+    $query = $builder->build_query();
+    ok( defined $query->{aggregations}{homebranch},
+        'homebranch added to facets if DisplayLibraryFacets=home' );
+    ok( !defined $query->{aggregations}{holdingbranch},
+        'holdingbranch not added to facets if DisplayLibraryFacets=home' );
+
+    t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
+    is(
+        $query->{query}{query_string}{query},
+        "(donald duck)",
+        "query not altered if QueryAutoTruncate disabled"
+    );
+
+    t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
+    is(
+        $query->{query}{query_string}{query},
+        "(donald* duck*)",
+        "simple query is auto truncated when QueryAutoTruncate enabled"
+    );
+
+    # Ensure reserved words are not truncated
+    ( undef, $query ) = $builder->build_query_compat( undef,
+        ['donald or duck and mickey not mouse'] );
+    is(
+        $query->{query}{query_string}{query},
+        "(donald* or duck* and mickey* not mouse*)",
+        "reserved words are not affected by QueryAutoTruncate"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
+    is(
+        $query->{query}{query_string}{query},
+        "(donald* duck*)",
+        "query with '*' is unaltered when QueryAutoTruncate is enabled"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
+    is(
+        $query->{query}{query_string}{query},
+        "(donald* duck* and the* mouse*)",
+        "individual words are all truncated and stopwords ignored"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
+    is(
+        $query->{query}{query_string}{query},
+        "(*)",
+        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
+    is(
+        $query->{query}{query_string}{query},
+        '("donald duck")',
+        "query with quotes is unaltered when QueryAutoTruncate is enabled"
+    );
+
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
+    is(
+        $query->{query}{query_string}{query},
+        '("donald duck" and "the mouse")',
+        "all quoted strings are unaltered if more than one in query"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
+    is(
+        $query->{query}{query_string}{query},
+        '(barcode:123456*)',
+        "query of specific field is truncated"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck")',
+        "query of specific field is not truncated when surrouned by quotes"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck") AND suppress:0',
+        "query of specific field is added AND suppress:0"
+    );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck")',
+        "query of specific field is not added AND suppress:0"
+    );
+};
+