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>.
21 # Dummy app for testing the plugin
22 use Mojolicious
::Lite
;
24 app
->log->level('error');
26 plugin
'Koha::REST::Plugin::Objects';
27 plugin
'Koha::REST::Plugin::Query';
28 plugin
'Koha::REST::Plugin::Pagination';
30 get
'/cities' => sub {
32 $c->validation->output($c->req->params->to_hash);
33 my $cities = $c->objects->search(Koha
::Cities
->new);
34 $c->render( status
=> 200, json
=> $cities );
37 get
'/cities_to_model' => sub {
39 $c->validation->output($c->req->params->to_hash);
40 my $cities_set = Koha
::Cities
->new;
41 my $cities = $c->objects->search( $cities_set, \
&to_model
);
42 $c->render( status
=> 200, json
=> $cities );
45 get
'/cities_to_model_to_api' => sub {
47 $c->validation->output($c->req->params->to_hash);
48 my $cities_set = Koha
::Cities
->new;
49 my $cities = $c->objects->search( $cities_set, \
&to_model
, \
&to_api
);
50 $c->render( status
=> 200, json
=> $cities );
56 if ( exists $params->{nombre
} ) {
57 $params->{city_name
} = delete $params->{nombre
};
66 if ( exists $params->{city_name
} ) {
67 $params->{nombre
} = delete $params->{city_name
};
74 use Test
::More tests
=> 1;
77 use t
::lib
::TestBuilder
;
80 my $schema = Koha
::Database
->new()->schema();
83 my $builder = t
::lib
::TestBuilder
->new;
85 subtest
'objects.search helper' => sub {
89 my $t = Test
::Mojo
->new;
91 $schema->storage->txn_begin;
93 # Remove existing cities to have more control on the search restuls
96 # Create two sample patrons that match the query
97 $builder->build_object({
98 class => 'Koha::Cities',
100 city_name
=> 'Manuel'
103 $builder->build_object({
104 class => 'Koha::Cities',
106 city_name
=> 'Manuela'
110 $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
112 ->header_like( 'Link' => qr/<http:\/\
/.*\?.*&_page=2.*>; rel="next",/ )
115 ->json_is('/0/city_name' => 'Manuel');
117 $builder->build_object({
118 class => 'Koha::Cities',
120 city_name
=> 'Emanuel'
125 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=starts_with')
130 ->json_is('/0/city_name' => 'Manuel')
131 ->json_is('/1/city_name' => 'Manuela');
134 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=ends_with')
139 ->json_is('/0/city_name' => 'Manuel')
140 ->json_is('/1/city_name' => 'Emanuel');
143 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=exact')
147 ->json_is('/0/city_name' => 'Manuel');
150 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=contains')
156 ->json_is('/0/city_name' => 'Manuel')
157 ->json_is('/1/city_name' => 'Manuela')
158 ->json_is('/2/city_name' => 'Emanuel');
162 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
167 ->json_is('/0/city_name' => 'Manuel')
168 ->json_is('/1/city_name' => 'Manuela');
171 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
176 ->json_is('/0/city_name' => 'Manuel')
177 ->json_is('/1/city_name' => 'Emanuel');
180 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
184 ->json_is('/0/city_name' => 'Manuel');
187 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
193 ->json_is('/0/city_name' => 'Manuel')
194 ->json_is('/1/city_name' => 'Manuela')
195 ->json_is('/2/city_name' => 'Emanuel');
197 ## _to_model && _to_api tests
199 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
204 ->json_is('/0/nombre' => 'Manuel')
205 ->json_is('/1/nombre' => 'Manuela');
208 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
213 ->json_is('/0/nombre' => 'Manuel')
214 ->json_is('/1/nombre' => 'Emanuel');
217 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
221 ->json_is('/0/nombre' => 'Manuel');
224 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
230 ->json_is('/0/nombre' => 'Manuel')
231 ->json_is('/1/nombre' => 'Manuela')
232 ->json_is('/2/nombre' => 'Emanuel');
234 $schema->storage->txn_rollback;