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>.
20 use Test::More tests => 6;
23 use_ok('Koha::SearchEngine::Elasticsearch::QueryBuilder');
25 subtest 'query_regex_escape_options' => sub {
28 t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'dont_escape');
30 my $query_with_regexp = "query /with regexp/";
32 my $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
36 "Unescaped query regexp has not been escaped when escaping is disabled"
39 t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'escape');
41 $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
44 "query \\/with regexp\\/",
45 "Unescaped query regexp has been escaped when escaping is enabled"
48 my $query_with_escaped_regex = "query \\/with regexp\\/";
49 $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_escaped_regex);
52 $query_with_escaped_regex,
53 "Escaped query regexp has been left unmodified when escaping is enabled"
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);
60 "query \\\\\\/with regexp\\\\\\/",
61 "Query regexp with even preceding escapes, thus unescaped, has been escaped when escaping is enabled"
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);
68 $query_with_odd_preceding_escapes_regex,
69 "Query regexp with odd preceding escapes, thus escaped, has been left unmodified when escaping is enabled"
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);
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."
80 t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'unescape_escaped');
82 $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
85 "query \\/with regexp\\/",
86 "Unescaped query regexp has been escaped when unescape escaping is enabled"
89 $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_escaped_regex);
92 "query /with regexp/",
93 "Escaped query regexp has been unescaped when unescape escaping is enabled"
96 $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_even_preceding_escapes_regex);
99 "query \\\\\\/with regexp\\\\\\/",
100 "Query regexp with even preceding escapes, thus unescaped, has been escaped when unescape escaping is enabled"
103 $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_odd_preceding_escapes_regex);
106 "query \\\\/with regexp\\\\/",
107 "Query regexp with odd preceding escapes, thus escaped, has been unescaped when unescape escaping is enabled"
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);
115 "Regexp at start of string with odd preceding escapes, thus escaped, has been unescaped when unescape escaping is enabled"
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);
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."
127 subtest '_truncate_terms() tests' => sub {
132 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
133 'Creating a new QueryBuilder object'
136 my $res = $qb->_truncate_terms('donald');
137 is_deeply($res, 'donald*', 'single search term returned correctly');
139 $res = $qb->_truncate_terms('donald duck');
140 is_deeply($res, 'donald* duck*', 'two search terms returned correctly');
142 $res = $qb->_truncate_terms(' donald duck ');
143 is_deeply($res, 'donald* duck*', 'two search terms surrounded by spaces returned correctly');
145 $res = $qb->_truncate_terms('"donald duck"');
146 is_deeply($res, '"donald duck"', 'quoted search term returned correctly');
148 $res = $qb->_truncate_terms('"donald, duck"');
149 is_deeply($res, '"donald, duck"', 'quoted search term with comma returned correctly');
151 $res = $qb->_truncate_terms(' "donald duck" ');
152 is_deeply($res, '"donald duck"', 'quoted search terms surrounded by spaces correctly');
155 subtest '_split_query() tests' => sub {
160 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
161 'Creating a new QueryBuilder object'
164 my @res = $qb->_split_query('donald');
166 is_deeply(\@res, \@exp, 'single search term returned correctly');
168 @res = $qb->_split_query('donald duck');
169 @exp = ('donald', 'duck');
170 is_deeply(\@res, \@exp, 'two search terms returned correctly');
172 @res = $qb->_split_query(' donald duck ');
173 @exp = ('donald', 'duck');
174 is_deeply(\@res, \@exp, 'two search terms surrounded by spaces returned correctly');
176 @res = $qb->_split_query('"donald duck"');
177 @exp = ( '"donald duck"' );
178 is_deeply(\@res, \@exp, 'quoted search term returned correctly');
180 @res = $qb->_split_query('"donald, duck"');
181 @exp = ( '"donald, duck"' );
182 is_deeply(\@res, \@exp, 'quoted search term with comma returned correctly');
184 @res = $qb->_split_query(' "donald duck" ');
185 @exp = ( '"donald duck"' );
186 is_deeply(\@res, \@exp, 'quoted search terms surrounded by spaces correctly');
189 subtest 'clean_search_term() tests' => sub {
194 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
195 'Creating a new QueryBuilder object'
198 t::lib::Mocks::mock_preference('QueryAutoTruncate', 0);
200 my $res = $qb->clean_search_term('an=123');
201 is($res, 'koha-auth-number:123', 'equals sign replaced with colon');
203 $res = $qb->clean_search_term('"balanced quotes"');
204 is($res, '"balanced quotes"', 'balanced quotes returned correctly');
206 $res = $qb->clean_search_term('unbalanced quotes"');
207 is($res, 'unbalanced quotes ', 'unbalanced quotes removed');
209 $res = $qb->clean_search_term('"unbalanced "quotes"');
210 is($res, ' unbalanced quotes ', 'unbalanced quotes removed');
212 $res = $qb->clean_search_term(':test query');
213 is($res, 'test query', 'remove colon at the start');
215 $res = $qb->clean_search_term('test query\:');
216 is($res, 'test query', 'remove colon at the end');
218 $res = $qb->clean_search_term('test : query');
219 is($res, 'test query', 'dangling colon removed');
221 $res = $qb->clean_search_term('test :: query');
222 is($res, 'test query', 'dangling double colon removed');
224 $res = $qb->clean_search_term('test "another : query"');
225 is($res, 'test "another : query"', 'quoted dangling colon not removed');
227 $res = $qb->clean_search_term('host-item:test:n');
228 is($res, 'host-item:test\:n', 'screen colons properly');
230 $res = $qb->clean_search_term('host-item:test:n:test:and more');
231 is($res, 'host-item:test\:n\:test\:and more', 'screen multiple colons properly');
233 $res = $qb->clean_search_term('host-item:te st:n');
234 is($res, 'host-item:te st:n', 'leave colons as they are');
236 $res = $qb->clean_search_term('test!');
237 is($res, 'test', 'remove exclamation sign at the end of the line');
239 $res = $qb->clean_search_term('test! and more');
240 is($res, 'test and more', 'remove exclamation sign at with space after it');
242 $res = $qb->clean_search_term('test! and more (and more!)');
243 is($res, 'test and more (and more)', 'remove exclamation sign followed by close parentheses');
245 $res = $qb->clean_search_term('!test');
246 is($res, '!test', 'exclamation sign left untouched');
248 $res = $qb->clean_search_term('test [123 TO 345]');
249 is($res, 'test [123 TO 345]', 'keep inculsive range untouched');
251 $res = $qb->clean_search_term('test [test TO TEST} [and] {123 TO 456]');
252 is($res, 'test [test TO TEST} \[and\] {123 TO 456]', 'keep exclusive range untouched');
254 $res = $qb->clean_search_term('test [test TO TEST} ["[and] {123 TO 456]" "[balanced]"]');
255 is($res, 'test [test TO TEST} \["[and] {123 TO 456]" "[balanced]"\]', 'keep exclusive range untouched');
257 $res = $qb->clean_search_term('test[]test TO TEST] [ {123 to 345}}');
258 is($res, 'test\[\]test TO TEST\] \[ \{123 to 345\}\}', 'screen all square and curly brackets');
260 t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'escape');
262 $res = $qb->clean_search_term('test inside regexps /this [a-z]/ and \/not [a-z]\/ and that [a-z] [a TO z]');
263 is($res, 'test inside regexps \/this \[a-z\]\/ and \/not \[a-z\]\/ and that \[a-z\] [a TO z]', 'behaviour with QueryRegexEscapeOptions set to "escape"');
265 t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'dont_escape');
267 $res = $qb->clean_search_term('test inside regexps /this [a-z]/ /this2 [a-z]/ [but] /this3 [a-z]/ and \/not [a-z]\/ and that [a-z] [a TO z]');
268 is($res, 'test inside regexps /this [a-z]/ /this2 [a-z]/ \[but\] /this3 [a-z]/ and \/not \[a-z\]\/ and that \[a-z\] [a TO z]', 'behaviour with QueryRegexEscapeOptions set to "dont_escape"');
270 $res = $qb->clean_search_term('ti:test AND kw:test');
271 is($res, 'title:test AND test', 'ti converted to title, kw converted to empty string, dangling colon removed with space preserved');
273 $res = $qb->clean_search_term('kw:test');
274 is($res, 'test', 'kw converted to empty string, dangling colon is removed');
277 subtest '_join_queries' => sub {
281 index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX,
283 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new($params);
287 $query = $qb->_join_queries('foo');
288 is($query, 'foo', 'should work with a single param');
290 $query = $qb->_join_queries(undef, '', 'foo', '', undef);
291 is($query, 'foo', 'should ignore undef or empty queries');
293 $query = $qb->_join_queries('foo', 'bar');
294 is($query, '(foo) AND (bar)', 'should join queries with an AND');
296 $query = $qb->_join_queries('homebranch:foo', 'onloan:false');
297 is($query, '(homebranch:foo) AND (onloan:false)', 'should also work when field is specified');
299 $query = $qb->_join_queries('homebranch:foo', 'mc-itype:BOOK', 'mc-itype:EBOOK');
300 is($query, '(homebranch:foo) AND itype:(BOOK OR EBOOK)', 'should join with OR when using an "mc-" field');
302 $query = $qb->_join_queries('homebranch:foo', 'mc-itype:BOOK', 'mc-itype:EBOOK', 'mc-location:SHELF');
303 is($query, '(homebranch:foo) AND itype:(BOOK OR EBOOK) AND location:(SHELF)', 'should join "mc-" parts with AND if not the same field');