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 # Dummy app for testing the plugin
21 use Mojolicious
::Lite
;
27 use Koha
::Patron
::Relationship
;
29 app
->log->level('error');
31 plugin
'Koha::REST::Plugin::Query';
35 $c->render( json
=> undef, status
=> 200 );
40 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($c->req->params->to_hash);
43 filtered_params
=> $filtered_params,
44 reserved_params
=> $reserved_params
50 get
'/query_full/:id/:subid' => sub {
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);
58 filtered_params
=> $filtered_params,
59 reserved_params
=> $reserved_params,
60 path_params
=> $path_params
66 get
'/dbic_merge_sorting' => sub {
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 {
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 {
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 {
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 {
121 my $result_set = Koha
::Holds
->new;
122 $c->stash('koha.embed', {
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 {
142 my $result_set = Koha
::Patron
::Relationship
->new;
143 $c->stash('koha.embed', {
146 "article_requests" => {},
147 "housebound_profile" => {
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 {
168 my $result_set = Koha
::Patron
::Relationship
->new;
169 $c->stash('koha.embed', {
170 "guarantee_count" => {
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 {
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 {
194 my ( $filtered_params, $reserved_params ) =
195 $c->extract_reserved_params( $c->req->params->to_hash );
198 $query = $c->build_query_params( $filtered_params, $reserved_params );
199 $c->render( json
=> { query
=> $query }, status
=> 200 );
203 json
=> { exception_msg
=> $_->message, exception_type
=> ref($_) },
209 get
'/stash_embed' => sub {
228 json
=> $c->stash( 'koha.embed' )
234 json
=> { error
=> "$_" }
239 get
'/stash_embed_no_spec' => sub {
243 $c->stash_embed({ spec
=> {} });
247 json
=> $c->stash( 'koha.embed' )
253 json
=> { error
=> "$_" }
260 $args->{three
} = delete $args->{tres
}
261 if exists $args->{tres
};
267 use Test
::More tests
=> 6;
270 subtest
'extract_reserved_params() tests' => sub {
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)
283 '/filtered_params' => {
284 firstname
=> 'Manuel',
285 surname
=> 'Cohen Arazi'
288 '/reserved_params' => {
302 subtest
'dbic_merge_sorting() tests' => sub {
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(
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(
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(
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 {
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' => {
358 {housebound_profile
=> 'housebound_visits'},
363 $t->get_ok('/dbic_merge_prefetch_count')->status_is(200)
364 ->json_is('/prefetch/0' => 'guarantee');
367 subtest
'/merge_q_params' => sub {
369 my $t = Test
::Mojo
->new;
371 $t->get_ok('/merge_q_params' => json
=> {
373 "-not_bool" => "suggestions.suggester.patron_card_lost",
377 "!=" => ["fff", "zzz", "xxx"]
380 { "suggestions.suggester.housebound_profile.frequency" => "123" },
382 "suggestions.suggester.library_id" => {"like" => "%CPL%"}
387 ->json_is( '/-and' => [
389 "-not_bool" => "suggester.lost",
401 "housebound_profile.frequency" => 123
404 "suggester.branchcode" => {
416 subtest
'_build_query_params_from_api' => sub {
420 my $t = Test
::Mojo
->new;
423 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
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')
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')
437 ->json_is( '/query' =>
438 { author
=> { like
=> '%Orson' }, title
=> { like
=> '%Ender' } } );
441 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
443 ->json_is( '/query' => { author
=> 'Orson', title
=> 'Ender' } );
446 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
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 {
457 my $t = Test
::Mojo
->new;
459 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item' } )
461 ->json_is( { checkouts
=> { children
=> { item
=> {} } } } );
463 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,library' } )
465 ->json_is( { checkouts
=> { children
=> { item
=> {} } }, library
=> {} } );
467 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'holds+count' } )
469 ->json_is( { holds_count
=> { is_count
=> 1 } } );
471 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
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' } )
483 error
=> 'Embedding objects is not allowed on this endpoint.'