From 445ac324d019640ac0aed573eaacd6b4f8b4f9cb Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Fri, 29 Sep 2017 16:12:23 +0200
Subject: [PATCH] Bug 18374: (QA follow-up) Tidy up code

Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>
---
 Koha/SearchEngine/Elasticsearch/QueryBuilder.pm    | 16 ++++-----
 .../Koha_SearchEngine_Elasticsearch_Search.t       | 38 +++++++++++++++++-----
 2 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
index 9e8210982c..5635c85064 100644
--- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
+++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
@@ -715,14 +715,14 @@ to ensure those parts are correct.
 sub _clean_search_term {
     my ( $self, $term ) = @_;
 
-    my $auto_truncation  = C4::Context->preference("QueryAutoTruncate")    || 0;
+    my $auto_truncation = C4::Context->preference("QueryAutoTruncate") || 0;
 
     # Some hardcoded searches (like with authorities) produce things like
     # 'an=123', when it ought to be 'an:123' for our purposes.
     $term =~ s/=/:/g;
     $term = $self->_convert_index_strings_freeform($term);
     $term =~ s/[{}]/"/g;
-    $term = $self->_truncate_terms($term) if ( $auto_truncation );
+    $term = $self->_truncate_terms($term) if ($auto_truncation);
     return $term;
 }
 
@@ -793,16 +793,16 @@ operands.
 =cut
 
 sub _truncate_terms {
-    my ($self, $query) = @_;
+    my ( $self, $query ) = @_;
     my @stops = qw/and or not/;
     my @new_terms;
-    my @split_query = split /[\(\s\)]/, $query ;
-    foreach  my $term ( @split_query ) {
-        next if ($term eq '' || $term eq ' ' ) ;
-        $term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ($term =~ /\*$/ ) );
+    my @split_query = split /[\(\s\)]/, $query;
+    foreach my $term (@split_query) {
+        next if ( $term eq '' || $term eq ' ' );
+        $term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) );
         push @new_terms, $term;
     }
-    $query=join ' ' ,@new_terms;
+    $query = join ' ', @new_terms;
     return $query;
 }
 
diff --git a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t b/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t
index 8afa74bc1a..f547fcbc17 100644
--- a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t
+++ b/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t
@@ -102,18 +102,38 @@ subtest 'build_query tests' => sub {
     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', '' );
 
-    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" );
-    ( 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" ); #Ensure reserved words are not truncated
-    ( 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" );
+    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"
+    );
 };
 
-- 
2.11.0