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
;
24 app
->log->level('error');
26 plugin
'Koha::REST::Plugin::Query';
30 $c->render( json
=> undef, status
=> 200 );
38 firstname
=> 'Manuel',
39 surname
=> 'Cohen Arazi'
41 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
44 filtered_params
=> $filtered_params,
45 reserved_params
=> $reserved_params
51 get
'/query_full' => sub {
58 firstname
=> 'Manuel',
59 surname
=> 'Cohen Arazi'
61 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
64 filtered_params
=> $filtered_params,
65 reserved_params
=> $reserved_params
71 get
'/dbic_merge_sorting' => sub {
73 my $attributes = { a
=> 'a', b
=> 'b' };
74 $attributes = $c->dbic_merge_sorting(
76 attributes
=> $attributes,
77 params
=> { _match
=> 'exact', _order_by
=> [ 'uno', '-dos', '+tres', ' cuatro' ] }
80 $c->render( json
=> $attributes, status
=> 200 );
83 get
'/build_query' => sub {
85 my ( $filtered_params, $reserved_params ) =
86 $c->extract_reserved_params( $c->req->params->to_hash );
89 $query = $c->build_query_params( $filtered_params, $reserved_params );
90 $c->render( json
=> { query
=> $query }, status
=> 200 );
94 json
=> { exception_msg
=> $_->message, exception_type
=> ref($_) },
102 use Test
::More tests
=> 3;
105 subtest
'extract_reserved_params() tests' => sub {
109 my $t = Test
::Mojo
->new;
111 $t->get_ok('/query')->status_is(200)
112 ->json_is( '/filtered_params' =>
113 { firstname
=> 'Manuel', surname
=> 'Cohen Arazi' } )
114 ->json_is( '/reserved_params' => { _page
=> 2, _per_page
=> 3 } );
116 $t->get_ok('/query_full')->status_is(200)
118 '/filtered_params' => {
119 firstname
=> 'Manuel',
120 surname
=> 'Cohen Arazi'
123 '/reserved_params' => {
132 subtest
'dbic_merge_sorting() tests' => sub {
136 my $t = Test
::Mojo
->new;
138 $t->get_ok('/dbic_merge_sorting')->status_is(200)
139 ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
140 ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
150 subtest
'_build_query_params_from_api' => sub {
154 my $t = Test
::Mojo
->new;
157 $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
159 ->json_is( '/query' =>
160 { author
=> { like
=> '%Orson%' }, title
=> { like
=> '%Ender%' } } );
162 # _match => starts_with
163 $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
165 ->json_is( '/query' =>
166 { author
=> { like
=> 'Orson%' }, title
=> { like
=> 'Ender%' } } );
168 # _match => ends_with
169 $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
171 ->json_is( '/query' =>
172 { author
=> { like
=> '%Orson' }, title
=> { like
=> '%Ender' } } );
175 $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
177 ->json_is( '/query' => { author
=> 'Orson', title
=> 'Ender' } );
180 $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
182 ->json_is( '/exception_msg' => 'Invalid value for _match param (blah)' )
183 ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );