Bug 25044: (follow-up) Fix wrong tests
[koha.git] / t / Koha / REST / Plugin / Query.t
blobb504f3ba5f2f9ad3be2e326510fd83908c79501f
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 # Dummy app for testing the plugin
21 use Mojolicious::Lite;
22 use Try::Tiny;
24 use Koha::Cities;
25 use Koha::Holds;
26 use Koha::Biblios;
27 use Koha::Patron::Relationship;
29 app->log->level('error');
31 plugin 'Koha::REST::Plugin::Query';
33 get '/empty' => sub {
34 my $c = shift;
35 $c->render( json => undef, status => 200 );
38 get '/query' => sub {
39 my $c = shift;
40 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($c->req->params->to_hash);
41 $c->render(
42 json => {
43 filtered_params => $filtered_params,
44 reserved_params => $reserved_params
46 status => 200
50 get '/query_full/:id/:subid' => sub {
51 my $c = shift;
52 my $params = $c->req->params->to_hash;
53 $params->{id} = $c->stash->{id};
54 $params->{subid} = $c->stash->{subid};
55 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($params);
56 $c->render(
57 json => {
58 filtered_params => $filtered_params,
59 reserved_params => $reserved_params,
60 path_params => $path_params
62 status => 200
66 get '/dbic_merge_sorting' => sub {
67 my $c = shift;
68 my $attributes = { a => 'a', b => 'b' };
69 $attributes = $c->dbic_merge_sorting(
71 attributes => $attributes,
72 params => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] }
75 $c->render( json => $attributes, status => 200 );
78 get '/dbic_merge_sorting_single' => sub {
79 my $c = shift;
80 my $attributes = { a => 'a', b => 'b' };
81 $attributes = $c->dbic_merge_sorting(
83 attributes => $attributes,
84 params => { _match => 'exact', _order_by => '-uno' }
87 $c->render( json => $attributes, status => 200 );
90 get '/dbic_merge_sorting_result_set' => sub {
91 my $c = shift;
92 my $attributes = { a => 'a', b => 'b' };
93 my $result_set = Koha::Cities->new;
94 $attributes = $c->dbic_merge_sorting(
96 attributes => $attributes,
97 params => { _match => 'exact', _order_by => [ 'name', '-postal_code', '+country', ' state' ] },
98 result_set => $result_set
101 $c->render( json => $attributes, status => 200 );
104 get '/dbic_merge_sorting_date' => sub {
105 my $c = shift;
106 my $attributes = { a => 'a', b => 'b' };
107 my $result_set = Koha::Holds->new;
108 $attributes = $c->dbic_merge_sorting(
110 attributes => $attributes,
111 params => { _match => 'exact', _order_by => [ '-hold_date' ] },
112 result_set => $result_set
115 $c->render( json => $attributes, status => 200 );
118 get '/dbic_merge_prefetch' => sub {
119 my $c = shift;
120 my $attributes = {};
121 my $result_set = Koha::Holds->new;
122 $c->stash('koha.embed', {
123 "item" => {},
124 "biblio" => {
125 children => {
126 "orders" => {}
131 $c->dbic_merge_prefetch({
132 attributes => $attributes,
133 result_set => $result_set
136 $c->render( json => $attributes, status => 200 );
139 get '/dbic_merge_prefetch_recursive' => sub {
140 my $c = shift;
141 my $attributes = {};
142 my $result_set = Koha::Patron::Relationship->new;
143 $c->stash('koha.embed', {
144 "guarantee" => {
145 "children" => {
146 "article_requests" => {},
147 "housebound_profile" => {
148 "children" => {
149 "housebound_visits" => {}
152 "housebound_role" => {}
157 $c->dbic_merge_prefetch({
158 attributes => $attributes,
159 result_set => $result_set
162 $c->render( json => $attributes, status => 200 );
165 get '/dbic_merge_prefetch_count' => sub {
166 my $c = shift;
167 my $attributes = {};
168 my $result_set = Koha::Patron::Relationship->new;
169 $c->stash('koha.embed', {
170 "guarantee_count" => {
171 "is_count" => 1
175 $c->dbic_merge_prefetch({
176 attributes => $attributes,
177 result_set => $result_set
180 $c->render( json => $attributes, status => 200 );
183 get '/merge_q_params' => sub {
184 my $c = shift;
185 my $filtered_params = {'biblio_id' => 1};
186 my $result_set = Koha::Biblios->new;
187 $filtered_params = $c->merge_q_params($filtered_params, $c->req->json->{q}, $result_set);
189 $c->render( json => $filtered_params, status => 200 );
192 get '/build_query' => sub {
193 my $c = shift;
194 my ( $filtered_params, $reserved_params ) =
195 $c->extract_reserved_params( $c->req->params->to_hash );
196 my $query;
197 try {
198 $query = $c->build_query_params( $filtered_params, $reserved_params );
199 $c->render( json => { query => $query }, status => 200 );
201 catch {
202 $c->render(
203 json => { exception_msg => $_->message, exception_type => ref($_) },
204 status => 400
209 get '/stash_embed' => sub {
210 my $c = shift;
212 try {
213 $c->stash_embed(
215 spec => {
216 'x-koha-embed' => [
217 'checkouts',
218 'checkouts.item',
219 'library',
220 'holds+count'
226 $c->render(
227 status => 200,
228 json => $c->stash( 'koha.embed' )
231 catch {
232 $c->render(
233 status => 400,
234 json => { error => "$_" }
239 get '/stash_embed_no_spec' => sub {
240 my $c = shift;
242 try {
243 $c->stash_embed({ spec => {} });
245 $c->render(
246 status => 200,
247 json => $c->stash( 'koha.embed' )
250 catch {
251 $c->render(
252 status => 400,
253 json => { error => "$_" }
258 sub to_model {
259 my ($args) = @_;
260 $args->{three} = delete $args->{tres}
261 if exists $args->{tres};
262 return $args;
265 # The tests
267 use Test::More tests => 6;
268 use Test::Mojo;
270 subtest 'extract_reserved_params() tests' => sub {
272 plan tests => 9;
274 my $t = Test::Mojo->new;
276 $t->get_ok('/query?_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
277 ->json_is( '/filtered_params' =>
278 { firstname => 'Manuel', surname => 'Cohen Arazi' } )
279 ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
281 $t->get_ok('/query_full/with/path?_match=exact&_order_by=blah&_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
282 ->json_is(
283 '/filtered_params' => {
284 firstname => 'Manuel',
285 surname => 'Cohen Arazi'
287 ->json_is(
288 '/reserved_params' => {
289 _page => 2,
290 _per_page => 3,
291 _match => 'exact',
292 _order_by => 'blah'
294 ->json_is(
295 '/path_params' => {
296 id => 'with',
297 subid => 'path'
298 } );
302 subtest 'dbic_merge_sorting() tests' => sub {
304 plan tests => 20;
306 my $t = Test::Mojo->new;
308 $t->get_ok('/dbic_merge_sorting')->status_is(200)
309 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
310 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
311 '/order_by' => [
312 'uno',
313 { -desc => 'dos' },
314 { -asc => 'tres' },
315 { -asc => 'cuatro' }
319 $t->get_ok('/dbic_merge_sorting_result_set')->status_is(200)
320 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
321 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
322 '/order_by' => [
323 'city_name',
324 { -desc => 'city_zipcode' },
325 { -asc => 'city_country' },
326 { -asc => 'city_state' }
330 $t->get_ok('/dbic_merge_sorting_date')->status_is(200)
331 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
332 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
333 '/order_by' => [
334 { -desc => 'reservedate' }
338 $t->get_ok('/dbic_merge_sorting_single')->status_is(200)
339 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
340 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
341 '/order_by' => { '-desc' => 'uno' }
345 subtest '/dbic_merge_prefetch' => sub {
346 plan tests => 10;
348 my $t = Test::Mojo->new;
350 $t->get_ok('/dbic_merge_prefetch')->status_is(200)
351 ->json_is( '/prefetch/0' => { 'biblio' => 'orders' } )
352 ->json_is( '/prefetch/1' => 'item' );
354 $t->get_ok('/dbic_merge_prefetch_recursive')->status_is(200)
355 ->json_is('/prefetch/0' => {
356 guarantee => [
357 'article_requests',
358 {housebound_profile => 'housebound_visits'},
359 'housebound_role'
363 $t->get_ok('/dbic_merge_prefetch_count')->status_is(200)
364 ->json_is('/prefetch/0' => 'guarantee');
367 subtest '/merge_q_params' => sub {
368 plan tests => 3;
369 my $t = Test::Mojo->new;
371 $t->get_ok('/merge_q_params' => json => {
372 q => {
373 "-not_bool" => "suggestions.suggester.patron_card_lost",
374 "-or" => [
376 "creation_date" => {
377 "!=" => ["fff", "zzz", "xxx"]
380 { "suggestions.suggester.housebound_profile.frequency" => "123" },
382 "suggestions.suggester.library_id" => {"like" => "%CPL%"}
386 })->status_is(200)
387 ->json_is( '/-and' => [
389 "-not_bool" => "suggester.lost",
390 "-or" => [
392 "datecreated" => {
393 "!=" => [
394 "fff",
395 "zzz",
396 "xxx"
401 "housebound_profile.frequency" => 123
404 "suggester.branchcode" => {
405 "like" => "\%CPL\%"
411 "biblio_id" => 1
416 subtest '_build_query_params_from_api' => sub {
418 plan tests => 16;
420 my $t = Test::Mojo->new;
422 # _match => contains
423 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
424 ->status_is(200)
425 ->json_is( '/query' =>
426 { author => { like => '%Orson%' }, title => { like => '%Ender%' } } );
428 # _match => starts_with
429 $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
430 ->status_is(200)
431 ->json_is( '/query' =>
432 { author => { like => 'Orson%' }, title => { like => 'Ender%' } } );
434 # _match => ends_with
435 $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
436 ->status_is(200)
437 ->json_is( '/query' =>
438 { author => { like => '%Orson' }, title => { like => '%Ender' } } );
440 # _match => exact
441 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
442 ->status_is(200)
443 ->json_is( '/query' => { author => 'Orson', title => 'Ender' } );
445 # _match => blah
446 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
447 ->status_is(400)
448 ->json_is( '/exception_msg' => 'Invalid value for _match param (blah)' )
449 ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );
453 subtest 'stash_embed() tests' => sub {
455 plan tests => 15;
457 my $t = Test::Mojo->new;
459 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item' } )
460 ->status_is(200)
461 ->json_is( { checkouts => { children => { item => {} } } } );
463 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,library' } )
464 ->status_is(200)
465 ->json_is( { checkouts => { children => { item => {} } }, library => {} } );
467 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'holds+count' } )
468 ->status_is(200)
469 ->json_is( { holds_count => { is_count => 1 } } );
471 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
472 ->status_is(400)
473 ->json_is(
475 error => 'Embeding patron is not authorised. Check your x-koha-embed headers or remove it.'
479 $t->get_ok( '/stash_embed_no_spec' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
480 ->status_is(400)
481 ->json_is(
483 error => 'Embedding objects is not allowed on this endpoint.'