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
;
28 app
->log->level('error');
30 plugin
'Koha::REST::Plugin::Query';
34 $c->render( json
=> undef, status
=> 200 );
39 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($c->req->params->to_hash);
42 filtered_params
=> $filtered_params,
43 reserved_params
=> $reserved_params
49 get
'/query_full/:id/:subid' => sub {
51 my $params = $c->req->params->to_hash;
52 $params->{id
} = $c->stash->{id
};
53 $params->{subid
} = $c->stash->{subid
};
54 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($params);
57 filtered_params
=> $filtered_params,
58 reserved_params
=> $reserved_params,
59 path_params
=> $path_params
65 get
'/dbic_merge_sorting' => sub {
67 my $attributes = { a
=> 'a', b
=> 'b' };
68 $attributes = $c->dbic_merge_sorting(
70 attributes
=> $attributes,
71 params
=> { _match
=> 'exact', _order_by
=> [ 'uno', '-dos', '+tres', ' cuatro' ] }
74 $c->render( json
=> $attributes, status
=> 200 );
77 get
'/dbic_merge_sorting_single' => sub {
79 my $attributes = { a
=> 'a', b
=> 'b' };
80 $attributes = $c->dbic_merge_sorting(
82 attributes
=> $attributes,
83 params
=> { _match
=> 'exact', _order_by
=> '-uno' }
86 $c->render( json
=> $attributes, status
=> 200 );
89 get
'/dbic_merge_sorting_result_set' => sub {
91 my $attributes = { a
=> 'a', b
=> 'b' };
92 my $result_set = Koha
::Cities
->new;
93 $attributes = $c->dbic_merge_sorting(
95 attributes
=> $attributes,
96 params
=> { _match
=> 'exact', _order_by
=> [ 'name', '-postal_code', '+country', ' state' ] },
97 result_set
=> $result_set
100 $c->render( json
=> $attributes, status
=> 200 );
103 get
'/dbic_merge_sorting_date' => sub {
105 my $attributes = { a
=> 'a', b
=> 'b' };
106 my $result_set = Koha
::Holds
->new;
107 $attributes = $c->dbic_merge_sorting(
109 attributes
=> $attributes,
110 params
=> { _match
=> 'exact', _order_by
=> [ '-hold_date' ] },
111 result_set
=> $result_set
114 $c->render( json
=> $attributes, status
=> 200 );
117 get
'/dbic_merge_prefetch' => sub {
120 my $result_set = Koha
::Holds
->new;
121 $c->stash('koha.embed', {
130 $c->dbic_merge_prefetch({
131 attributes
=> $attributes,
132 result_set
=> $result_set
135 $c->render( json
=> $attributes, status
=> 200 );
138 get
'/merge_q_params' => sub {
140 my $filtered_params = {'biblio_id' => 1};
141 my $result_set = Koha
::Biblios
->new;
142 $filtered_params = $c->merge_q_params($filtered_params, $c->req->json->{q
}, $result_set);
144 $c->render( json
=> $filtered_params, status
=> 200 );
147 get
'/build_query' => sub {
149 my ( $filtered_params, $reserved_params ) =
150 $c->extract_reserved_params( $c->req->params->to_hash );
153 $query = $c->build_query_params( $filtered_params, $reserved_params );
154 $c->render( json
=> { query
=> $query }, status
=> 200 );
158 json
=> { exception_msg
=> $_->message, exception_type
=> ref($_) },
164 get
'/stash_embed' => sub {
183 json
=> $c->stash( 'koha.embed' )
189 json
=> { error
=> "$_" }
194 get
'/stash_embed_no_spec' => sub {
198 $c->stash_embed({ spec
=> {} });
202 json
=> $c->stash( 'koha.embed' )
208 json
=> { error
=> "$_" }
215 $args->{three
} = delete $args->{tres
}
216 if exists $args->{tres
};
222 use Test
::More tests
=> 6;
225 subtest
'extract_reserved_params() tests' => sub {
229 my $t = Test
::Mojo
->new;
231 $t->get_ok('/query?_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
232 ->json_is( '/filtered_params' =>
233 { firstname
=> 'Manuel', surname
=> 'Cohen Arazi' } )
234 ->json_is( '/reserved_params' => { _page
=> 2, _per_page
=> 3 } );
236 $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)
238 '/filtered_params' => {
239 firstname
=> 'Manuel',
240 surname
=> 'Cohen Arazi'
243 '/reserved_params' => {
257 subtest
'dbic_merge_sorting() tests' => sub {
261 my $t = Test
::Mojo
->new;
263 $t->get_ok('/dbic_merge_sorting')->status_is(200)
264 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
265 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
274 $t->get_ok('/dbic_merge_sorting_result_set')->status_is(200)
275 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
276 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
279 { -desc
=> 'city_zipcode' },
280 { -asc
=> 'city_country' },
281 { -asc
=> 'city_state' }
285 $t->get_ok('/dbic_merge_sorting_date')->status_is(200)
286 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
287 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
289 { -desc
=> 'reservedate' }
293 $t->get_ok('/dbic_merge_sorting_single')->status_is(200)
294 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
295 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
296 '/order_by' => { '-desc' => 'uno' }
300 subtest
'/dbic_merge_prefetch' => sub {
303 my $t = Test
::Mojo
->new;
305 $t->get_ok('/dbic_merge_prefetch')->status_is(200)
306 ->json_like( '/prefetch/0' => qr/item|biblio/ )
307 ->json_like( '/prefetch/1' => qr/item|biblio/ );
310 subtest
'/merge_q_params' => sub {
312 my $t = Test
::Mojo
->new;
314 $t->get_ok('/merge_q_params' => json
=> {
316 "-not_bool" => "suggestions.suggester.patron_card_lost",
320 "!=" => ["fff", "zzz", "xxx"]
323 { "suggestions.suggester.housebound_profile.frequency" => "123" },
325 "suggestions.suggester.library_id" => {"like" => "%CPL%"}
330 ->json_is( '/-and' => [
332 "-not_bool" => "suggester.lost",
344 "housebound_profile.frequency" => 123
347 "suggester.branchcode" => {
359 subtest
'_build_query_params_from_api' => sub {
363 my $t = Test
::Mojo
->new;
366 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
368 ->json_is( '/query' =>
369 { author
=> { like
=> '%Orson%' }, title
=> { like
=> '%Ender%' } } );
371 # _match => starts_with
372 $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
374 ->json_is( '/query' =>
375 { author
=> { like
=> 'Orson%' }, title
=> { like
=> 'Ender%' } } );
377 # _match => ends_with
378 $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
380 ->json_is( '/query' =>
381 { author
=> { like
=> '%Orson' }, title
=> { like
=> '%Ender' } } );
384 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
386 ->json_is( '/query' => { author
=> 'Orson', title
=> 'Ender' } );
389 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
391 ->json_is( '/exception_msg' => 'Invalid value for _match param (blah)' )
392 ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );
396 subtest
'stash_embed() tests' => sub {
400 my $t = Test
::Mojo
->new;
402 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item' } )
404 ->json_is( { checkouts
=> { children
=> { item
=> {} } } } );
406 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,library' } )
408 ->json_is( { checkouts
=> { children
=> { item
=> {} } }, library
=> {} } );
410 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'holds+count' } )
412 ->json_is( { holds_count
=> { is_count
=> 1 } } );
414 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
418 error
=> 'Embeding patron is not authorised. Check your x-koha-embed headers or remove it.'
422 $t->get_ok( '/stash_embed_no_spec' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
426 error
=> 'Embedding objects is not allowed on this endpoint.'