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;
26 app->log->level('error');
28 plugin 'Koha::REST::Plugin::Query';
32 $c->render( json => undef, status => 200 );
40 firstname => 'Manuel',
41 surname => 'Cohen Arazi'
43 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
46 filtered_params => $filtered_params,
47 reserved_params => $reserved_params
53 get '/query_full' => sub {
60 firstname => 'Manuel',
61 surname => 'Cohen Arazi'
63 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
66 filtered_params => $filtered_params,
67 reserved_params => $reserved_params
73 get '/dbic_merge_sorting' => sub {
75 my $attributes = { a => 'a', b => 'b' };
76 $attributes = $c->dbic_merge_sorting(
78 attributes => $attributes,
79 params => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] }
82 $c->render( json => $attributes, status => 200 );
85 get '/dbic_merge_sorting_single' => sub {
87 my $attributes = { a => 'a', b => 'b' };
88 $attributes = $c->dbic_merge_sorting(
90 attributes => $attributes,
91 params => { _match => 'exact', _order_by => '-uno' }
94 $c->render( json => $attributes, status => 200 );
97 get '/dbic_merge_sorting_result_set' => sub {
99 my $attributes = { a => 'a', b => 'b' };
100 my $result_set = Koha::Cities->new;
101 $attributes = $c->dbic_merge_sorting(
103 attributes => $attributes,
104 params => { _match => 'exact', _order_by => [ 'name', '-postal_code', '+country', ' state' ] },
105 result_set => $result_set
108 $c->render( json => $attributes, status => 200 );
111 get '/build_query' => sub {
113 my ( $filtered_params, $reserved_params ) =
114 $c->extract_reserved_params( $c->req->params->to_hash );
117 $query = $c->build_query_params( $filtered_params, $reserved_params );
118 $c->render( json => { query => $query }, status => 200 );
122 json => { exception_msg => $_->message, exception_type => ref($_) },
128 get '/stash_embed' => sub {
146 json => $c->stash( 'koha.embed' )
152 json => { error => "$_" }
157 get '/stash_embed_no_spec' => sub {
161 $c->stash_embed({ spec => {} });
165 json => $c->stash( 'koha.embed' )
171 json => { error => "$_" }
178 $args->{three} = delete $args->{tres}
179 if exists $args->{tres};
185 use Test::More tests => 4;
188 subtest 'extract_reserved_params() tests' => sub {
192 my $t = Test::Mojo->new;
194 $t->get_ok('/query')->status_is(200)
195 ->json_is( '/filtered_params' =>
196 { firstname => 'Manuel', surname => 'Cohen Arazi' } )
197 ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
199 $t->get_ok('/query_full')->status_is(200)
201 '/filtered_params' => {
202 firstname => 'Manuel',
203 surname => 'Cohen Arazi'
206 '/reserved_params' => {
215 subtest 'dbic_merge_sorting() tests' => sub {
219 my $t = Test::Mojo->new;
221 $t->get_ok('/dbic_merge_sorting')->status_is(200)
222 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
223 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
232 $t->get_ok('/dbic_merge_sorting_result_set')->status_is(200)
233 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
234 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
237 { -desc => 'city_zipcode' },
238 { -asc => 'city_country' },
239 { -asc => 'city_state' }
243 $t->get_ok('/dbic_merge_sorting_single')->status_is(200)
244 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
245 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
246 '/order_by' => { '-desc' => 'uno' }
250 subtest '_build_query_params_from_api' => sub {
254 my $t = Test::Mojo->new;
257 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
259 ->json_is( '/query' =>
260 { author => { like => '%Orson%' }, title => { like => '%Ender%' } } );
262 # _match => starts_with
263 $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
265 ->json_is( '/query' =>
266 { author => { like => 'Orson%' }, title => { like => 'Ender%' } } );
268 # _match => ends_with
269 $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
271 ->json_is( '/query' =>
272 { author => { like => '%Orson' }, title => { like => '%Ender' } } );
275 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
277 ->json_is( '/query' => { author => 'Orson', title => 'Ender' } );
280 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
282 ->json_is( '/exception_msg' => 'Invalid value for _match param (blah)' )
283 ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );
287 subtest 'stash_embed() tests' => sub {
291 my $t = Test::Mojo->new;
293 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item' } )
295 ->json_is( { checkouts => { children => { item => {} } } } );
297 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,library' } )
299 ->json_is( { checkouts => { children => { item => {} } }, library => {} } );
301 $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
305 error => 'Embeding patron is not authorised. Check your x-koha-embed headers or remove it.'
309 $t->get_ok( '/stash_embed_no_spec' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
313 error => 'Embedding objects is not allowed on this endpoint.'