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 {
213 my $embed = $c->stash('koha.embed');
214 my $av_expand = $c->stash('koha.av_expand');
219 av_expand => $av_expand,
225 get '/stash_overrides' => sub {
228 $c->stash_overrides();
229 my $overrides = $c->stash('koha.overrides');
239 $args->{three} = delete $args->{tres}
240 if exists $args->{tres};
246 use Test::More tests => 7;
249 subtest 'extract_reserved_params() tests' => sub {
253 my $t = Test::Mojo->new;
255 $t->get_ok('/query?_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
256 ->json_is( '/filtered_params' =>
257 { firstname => 'Manuel', surname => 'Cohen Arazi' } )
258 ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
260 $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)
262 '/filtered_params' => {
263 firstname => 'Manuel',
264 surname => 'Cohen Arazi'
267 '/reserved_params' => {
281 subtest 'dbic_merge_sorting() tests' => sub {
285 my $t = Test::Mojo->new;
287 $t->get_ok('/dbic_merge_sorting')->status_is(200)
288 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
289 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
298 $t->get_ok('/dbic_merge_sorting_result_set')->status_is(200)
299 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
300 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
303 { -desc => 'city_zipcode' },
304 { -asc => 'city_country' },
305 { -asc => 'city_state' }
309 $t->get_ok('/dbic_merge_sorting_date')->status_is(200)
310 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
311 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
313 { -desc => 'reservedate' }
317 $t->get_ok('/dbic_merge_sorting_single')->status_is(200)
318 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
319 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
326 subtest '/dbic_merge_prefetch' => sub {
329 my $t = Test::Mojo->new;
331 $t->get_ok('/dbic_merge_prefetch')->status_is(200)
332 ->json_is( '/prefetch/0' => { 'biblio' => 'orders' } )
333 ->json_is( '/prefetch/1' => 'item' );
335 $t->get_ok('/dbic_merge_prefetch_recursive')->status_is(200)
336 ->json_is('/prefetch/0' => {
339 {housebound_profile => 'housebound_visits'},
344 $t->get_ok('/dbic_merge_prefetch_count')->status_is(200)
345 ->json_is('/prefetch/0' => 'guarantee');
348 subtest '/merge_q_params' => sub {
350 my $t = Test::Mojo->new;
352 $t->get_ok('/merge_q_params' => json => {
354 "-not_bool" => "suggestions.suggester.patron_card_lost",
358 "!=" => ["fff", "zzz", "xxx"]
361 { "suggestions.suggester.housebound_profile.frequency" => "123" },
363 "suggestions.suggester.library_id" => {"like" => "%CPL%"}
368 ->json_is( '/-and' => [
370 "-not_bool" => "suggester.lost",
382 "housebound_profile.frequency" => 123
385 "suggester.branchcode" => {
397 subtest '_build_query_params_from_api' => sub {
401 my $t = Test::Mojo->new;
404 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
406 ->json_is( '/query' =>
407 { author => { like => '%Orson%' }, title => { like => '%Ender%' } } );
409 # _match => starts_with
410 $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
412 ->json_is( '/query' =>
413 { author => { like => 'Orson%' }, title => { like => 'Ender%' } } );
415 # _match => ends_with
416 $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
418 ->json_is( '/query' =>
419 { author => { like => '%Orson' }, title => { like => '%Ender' } } );
422 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
424 ->json_is( '/query' => { author => 'Orson', title => 'Ender' } );
427 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
429 ->json_is( '/exception_msg' => 'Invalid value for _match param (blah)' )
430 ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );
434 subtest 'stash_embed() tests' => sub {
438 my $t = Test::Mojo->new;
440 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item' } )
441 ->json_is( '/embed' => { checkouts => { children => { item => { } } } } );
443 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,library' } )
444 ->json_is( '/embed' => { checkouts => { children => { item => {} } }, library => {} } );
446 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'holds+count' } )
447 ->json_is( '/embed' => { holds_count => { is_count => 1 } } );
449 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
450 ->json_is( '/embed' => {
451 checkouts => { children => { item => {} } },
455 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item+av_expand,patron+av_expand' } )
456 ->json_is( '/embed' => {
457 checkouts => { children => { item => { av_expand => 1 } } },
458 patron => { av_expand => 1 }
460 ->json_is( '/av_expand' => undef );
462 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts+av_expand,checkouts.item,patron,+av_expand' } )
463 ->json_is( '/embed' => {
464 checkouts => { children => { item => { } }, av_expand => 1 },
467 ->json_is( '/av_expand' => 1 );
470 subtest 'stash_overrides() tests' => sub {
474 my $t = Test::Mojo->new;
476 $t->get_ok( '/stash_overrides' => { 'x-koha-override' => 'any,none,some_other,any,' } )
477 ->json_is( { 'any' => 1, 'none' => 1, 'some_other' => 1 } ); # empty string and duplicates are skipped
479 $t->get_ok( '/stash_overrides' => { 'x-koha-override' => '' } )
480 ->json_is( {} ); # empty string is skipped
482 $t->get_ok( '/stash_overrides' => { } )
483 ->json_is( {} ); # x-koha-ovverride not passed is skipped