Bug 24902: Join different mc- limits with AND (elasticsearch)
[koha-equinox.git] / t / Koha / SearchEngine / ElasticSearch / QueryBuilder.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21 use t::lib::Mocks;
22
23 use_ok('Koha::SearchEngine::Elasticsearch::QueryBuilder');
24
25 subtest 'query_regex_escape_options' => sub {
26     plan tests => 12;
27
28     t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'dont_escape');
29
30     my $query_with_regexp = "query /with regexp/";
31
32     my $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
33     is(
34         $processed_query,
35         $query_with_regexp,
36         "Unescaped query regexp has not been escaped when escaping is disabled"
37     );
38
39     t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'escape');
40
41     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
42     is(
43         $processed_query,
44         "query \\/with regexp\\/",
45         "Unescaped query regexp has been escaped when escaping is enabled"
46     );
47
48     my $query_with_escaped_regex = "query \\/with regexp\\/";
49     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_escaped_regex);
50     is(
51         $processed_query,
52         $query_with_escaped_regex,
53         "Escaped query regexp has been left unmodified when escaping is enabled"
54     );
55
56     my $query_with_even_preceding_escapes_regex = "query \\\\/with regexp\\\\/";
57     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_even_preceding_escapes_regex);
58     is(
59         $processed_query,
60         "query \\\\\\/with regexp\\\\\\/",
61         "Query regexp with even preceding escapes, thus unescaped, has been escaped when escaping is enabled"
62     );
63
64     my $query_with_odd_preceding_escapes_regex = 'query \\\\\\/with regexp\\\\\\/';
65     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_odd_preceding_escapes_regex);
66     is(
67         $processed_query,
68         $query_with_odd_preceding_escapes_regex,
69         "Query regexp with odd preceding escapes, thus escaped, has been left unmodified when escaping is enabled"
70     );
71
72     my $query_with_quoted_slash = "query with / and \"/ within quotes\"";
73     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_quoted_slash);
74     is(
75         $processed_query,
76         "query with \\/ and \"/ within quotes\"",
77         "Unescaped slash outside of quotes has been escaped while unescaped slash within quotes is left as is when escaping is enabled."
78     );
79
80     t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'unescape_escaped');
81
82     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
83     is(
84         $processed_query,
85         "query \\/with regexp\\/",
86         "Unescaped query regexp has been escaped when unescape escaping is enabled"
87     );
88
89     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_escaped_regex);
90     is(
91         $processed_query,
92         "query /with regexp/",
93         "Escaped query regexp has been unescaped when unescape escaping is enabled"
94     );
95
96     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_even_preceding_escapes_regex);
97     is(
98         $processed_query,
99         "query \\\\\\/with regexp\\\\\\/",
100         "Query regexp with even preceding escapes, thus unescaped, has been escaped when unescape escaping is enabled"
101     );
102
103     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_odd_preceding_escapes_regex);
104     is(
105         $processed_query,
106         "query \\\\/with regexp\\\\/",
107         "Query regexp with odd preceding escapes, thus escaped, has been unescaped when unescape escaping is enabled"
108     );
109
110     my $regexp_at_start_of_string_with_odd_preceding_escapes_regex = '\\\\\\/regexp\\\\\\/';
111     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($regexp_at_start_of_string_with_odd_preceding_escapes_regex);
112     is(
113         $processed_query,
114         "\\\\/regexp\\\\/",
115         "Regexp at start of string with odd preceding escapes, thus escaped, has been unescaped when unescape escaping is enabled"
116     );
117
118     my $query_with_quoted_escaped_slash = "query with \\/ and \"\\/ within quotes\"";
119     $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_quoted_escaped_slash);
120     is(
121         $processed_query,
122         "query with / and \"\\/ within quotes\"",
123         "Escaped slash outside of quotes has been unescaped while escaped slash within quotes is left as is when unescape escaping is enabled."
124     );
125 };
126
127 subtest '_truncate_terms() tests' => sub {
128     plan tests => 7;
129
130     my $qb;
131     ok(
132         $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
133         'Creating a new QueryBuilder object'
134     );
135
136     my $res = $qb->_truncate_terms('donald');
137     is_deeply($res, 'donald*', 'single search term returned correctly');
138
139     $res = $qb->_truncate_terms('donald duck');
140     is_deeply($res, 'donald* duck*', 'two search terms returned correctly');
141
142     $res = $qb->_truncate_terms(' donald   duck ');
143     is_deeply($res, 'donald* duck*', 'two search terms surrounded by spaces returned correctly');
144
145     $res = $qb->_truncate_terms('"donald duck"');
146     is_deeply($res, '"donald duck"', 'quoted search term returned correctly');
147
148     $res = $qb->_truncate_terms('"donald, duck"');
149     is_deeply($res, '"donald, duck"', 'quoted search term with comma returned correctly');
150
151     $res = $qb->_truncate_terms(' "donald   duck" ');
152     is_deeply($res, '"donald   duck"', 'quoted search terms surrounded by spaces correctly');
153 };
154
155 subtest '_split_query() tests' => sub {
156     plan tests => 7;
157
158     my $qb;
159     ok(
160         $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
161         'Creating a new QueryBuilder object'
162     );
163
164     my @res = $qb->_split_query('donald');
165     my @exp = 'donald';
166     is_deeply(\@res, \@exp, 'single search term returned correctly');
167
168     @res = $qb->_split_query('donald duck');
169     @exp = ('donald', 'duck');
170     is_deeply(\@res, \@exp, 'two search terms returned correctly');
171
172     @res = $qb->_split_query(' donald   duck ');
173     @exp = ('donald', 'duck');
174     is_deeply(\@res, \@exp, 'two search terms surrounded by spaces returned correctly');
175
176     @res = $qb->_split_query('"donald duck"');
177     @exp = ( '"donald duck"' );
178     is_deeply(\@res, \@exp, 'quoted search term returned correctly');
179
180     @res = $qb->_split_query('"donald, duck"');
181     @exp = ( '"donald, duck"' );
182     is_deeply(\@res, \@exp, 'quoted search term with comma returned correctly');
183
184     @res = $qb->_split_query(' "donald   duck" ');
185     @exp = ( '"donald   duck"' );
186     is_deeply(\@res, \@exp, 'quoted search terms surrounded by spaces correctly');
187 };
188
189 subtest '_clean_search_term() tests' => sub {
190     plan tests => 10;
191
192     my $qb;
193     ok(
194         $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
195         'Creating a new QueryBuilder object'
196     );
197
198     my $res = $qb->_clean_search_term('an=123');
199     is($res, 'koha-auth-number:123', 'equals sign replaced with colon');
200
201     $res = $qb->_clean_search_term('"balanced quotes"');
202     is($res, '"balanced quotes"', 'balanced quotes returned correctly');
203
204     $res = $qb->_clean_search_term('unbalanced quotes"');
205     is($res, 'unbalanced quotes ', 'unbalanced quotes removed');
206
207     $res = $qb->_clean_search_term('"unbalanced "quotes"');
208     is($res, ' unbalanced  quotes ', 'unbalanced quotes removed');
209
210     $res = $qb->_clean_search_term('test : query');
211     is($res, 'test query', 'dangling colon removed');
212
213     $res = $qb->_clean_search_term('test :: query');
214     is($res, 'test query', 'dangling double colon removed');
215
216     $res = $qb->_clean_search_term('test "another : query"');
217     is($res, 'test "another : query"', 'quoted dangling colon not removed');
218
219     $res = $qb->_clean_search_term('test {another part}');
220     is($res, 'test "another part"', 'curly brackets replaced correctly');
221
222     $res = $qb->_clean_search_term('test {another part');
223     is($res, 'test  another part', 'unbalanced curly brackets replaced correctly');
224 };
225
226 subtest '_join_queries' => sub {
227     plan tests => 6;
228
229     my $params = {
230         index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX,
231     };
232     my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new($params);
233
234     my $query;
235
236     $query = $qb->_join_queries('foo');
237     is($query, 'foo', 'should work with a single param');
238
239     $query = $qb->_join_queries(undef, '', 'foo', '', undef);
240     is($query, 'foo', 'should ignore undef or empty queries');
241
242     $query = $qb->_join_queries('foo', 'bar');
243     is($query, '(foo) AND (bar)', 'should join queries with an AND');
244
245     $query = $qb->_join_queries('homebranch:foo', 'onloan:false');
246     is($query, '(homebranch:foo) AND (onloan:false)', 'should also work when field is specified');
247
248     $query = $qb->_join_queries('homebranch:foo', 'mc-itype:BOOK', 'mc-itype:EBOOK');
249     is($query, '(homebranch:foo) AND itype:(BOOK OR EBOOK)', 'should join with OR when using an "mc-" field');
250
251     $query = $qb->_join_queries('homebranch:foo', 'mc-itype:BOOK', 'mc-itype:EBOOK', 'mc-location:SHELF');
252     is($query, '(homebranch:foo) AND itype:(BOOK OR EBOOK) AND location:(SHELF)', 'should join "mc-" parts with AND if not the same field');
253 };
254
255 1;