Bug 16330: Move patches to OpenAPI
[koha.git] / t / db_dependent / Koha_SearchEngine_Elasticsearch_Search.t
1 # Copyright 2015 Catalyst IT
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 => 13;
21 use t::lib::Mocks;
22
23 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
24
25 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
26
27 use_ok('Koha::SearchEngine::Elasticsearch::Search');
28
29 ok(
30     my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
31         { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
32     ),
33     'Creating a Koha::SearchEngine::Elasticsearch::Search object'
34 );
35
36 is( $searcher->index, 'mydb', 'Testing basic accessor' );
37
38 ok( my $query = $builder->build_query('easy'), 'Build a search query');
39
40 SKIP: {
41
42     eval { $builder->get_elasticsearch_params; };
43
44     skip 'ElasticSeatch configuration not available', 6
45         if $@;
46
47     ok( my $results = $searcher->search( $query) , 'Do a search ' );
48
49     ok( my $marc = $searcher->json2marc( $results->first ), 'Convert JSON to MARC');
50
51     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
52
53     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
54
55     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
56
57     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
58
59 }
60
61 subtest 'json2marc' => sub {
62     plan tests => 4;
63     my $leader = '00626nam a2200193   4500';
64     my $_001 = 42;
65     my $_010a = '123456789';
66     my $_010d = 145;
67     my $_200a = 'a title';
68     my $json = [ # It's not a JSON, see the POD of json2marc
69         [ 'LDR', undef, undef, '_', $leader ],
70         [ '001', undef, undef, '_', $_001 ],
71         [ '010', ' ', ' ', 'a', $_010a, 'd', $_010d ],
72         [ '200', '1', ' ', 'a', $_200a, ], # Yes UNIMARC but we don't mind here
73     ];
74
75     my $marc = $searcher->json2marc( $json );
76     is( $marc->leader, $leader, );
77     is( $marc->field('001')->data, $_001, );
78     is( $marc->subfield('010', 'a'), $_010a, );
79     is( $marc->subfield('200', 'a'), $_200a, );
80
81 };
82
83 subtest 'build_query tests' => sub {
84     plan tests => 23;
85
86     t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
87     my $query = $builder->build_query();
88     ok( defined $query->{aggregations}{homebranch},
89         'homebranch added to facets if DisplayLibraryFacets=both' );
90     ok( defined $query->{aggregations}{holdingbranch},
91         'holdingbranch added to facets if DisplayLibraryFacets=both' );
92     t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
93     $query = $builder->build_query();
94     ok( !defined $query->{aggregations}{homebranch},
95         'homebranch not added to facets if DisplayLibraryFacets=holding' );
96     ok( defined $query->{aggregations}{holdingbranch},
97         'holdingbranch added to facets if DisplayLibraryFacets=holding' );
98     t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
99     $query = $builder->build_query();
100     ok( defined $query->{aggregations}{homebranch},
101         'homebranch added to facets if DisplayLibraryFacets=home' );
102     ok( !defined $query->{aggregations}{holdingbranch},
103         'holdingbranch not added to facets if DisplayLibraryFacets=home' );
104
105     t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
106
107     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
108     is(
109         $query->{query}{query_string}{query},
110         "(donald duck)",
111         "query not altered if QueryAutoTruncate disabled"
112     );
113
114     t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
115
116     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
117     is(
118         $query->{query}{query_string}{query},
119         "(donald* duck*)",
120         "simple query is auto truncated when QueryAutoTruncate enabled"
121     );
122
123     # Ensure reserved words are not truncated
124     ( undef, $query ) = $builder->build_query_compat( undef,
125         ['donald or duck and mickey not mouse'] );
126     is(
127         $query->{query}{query_string}{query},
128         "(donald* or duck* and mickey* not mouse*)",
129         "reserved words are not affected by QueryAutoTruncate"
130     );
131
132     ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
133     is(
134         $query->{query}{query_string}{query},
135         "(donald* duck*)",
136         "query with '*' is unaltered when QueryAutoTruncate is enabled"
137     );
138
139     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
140     is(
141         $query->{query}{query_string}{query},
142         "(donald* duck* and the* mouse*)",
143         "individual words are all truncated and stopwords ignored"
144     );
145
146     ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
147     is(
148         $query->{query}{query_string}{query},
149         "(*)",
150         "query of just '*' is unaltered when QueryAutoTruncate is enabled"
151     );
152
153     ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
154     is(
155         $query->{query}{query_string}{query},
156         '("donald duck")',
157         "query with quotes is unaltered when QueryAutoTruncate is enabled"
158     );
159
160
161     ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
162     is(
163         $query->{query}{query_string}{query},
164         '("donald duck" and "the mouse")',
165         "all quoted strings are unaltered if more than one in query"
166     );
167
168     ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
169     is(
170         $query->{query}{query_string}{query},
171         '(barcode:123456*)',
172         "query of specific field is truncated"
173     );
174
175     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:"123456"'] );
176     is(
177         $query->{query}{query_string}{query},
178         '(Local-number:"123456")',
179         "query of specific field including hyphen and quoted is not truncated"
180     );
181
182     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:123456'] );
183     is(
184         $query->{query}{query_string}{query},
185         '(Local-number:123456*)',
186         "query of specific field including hyphen and not quoted is truncated"
187     );
188
189     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:123456'] );
190     is(
191         $query->{query}{query_string}{query},
192         '(Local-number.raw:123456*)',
193         "query of specific field including period and not quoted is truncated"
194     );
195
196     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:"123456"'] );
197     is(
198         $query->{query}{query_string}{query},
199         '(Local-number.raw:"123456")',
200         "query of specific field including period and quoted is not truncated"
201     );
202
203     ( undef, $query ) = $builder->build_query_compat( undef, ['J.R.R'] );
204     is(
205         $query->{query}{query_string}{query},
206         '(J.R.R*)',
207         "query including period is truncated but not split at periods"
208     );
209
210     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
211     is(
212         $query->{query}{query_string}{query},
213         '(title:"donald duck")',
214         "query of specific field is not truncated when surrouned by quotes"
215     );
216
217     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
218     is(
219         $query->{query}{query_string}{query},
220         '(title:"donald duck") AND suppress:0',
221         "query of specific field is added AND suppress:0"
222     );
223
224     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
225     is(
226         $query->{query}{query_string}{query},
227         '(title:"donald duck")',
228         "query of specific field is not added AND suppress:0"
229     );
230 };
231
232 subtest "_convert_sort_fields" => sub {
233     plan tests => 2;
234     my @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_dsc ));
235     is_deeply(
236         \@sort_by,
237         [
238             { field => 'callnum', direction => 'asc' },
239             { field => 'author',  direction => 'desc' }
240         ],
241         'sort fields should have been split correctly'
242     );
243
244     # We could expect this to pass, but direction is undef instead of 'desc'
245     @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_desc ));
246     is_deeply(
247         \@sort_by,
248         [
249             { field => 'callnum', direction => 'asc' },
250             { field => 'author',  direction => 'desc' }
251         ],
252         'sort fields should have been split correctly'
253     );
254 };