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 );
53 get '/cities_sorted' => sub {
55 $c->validation->output($c->req->params->to_hash);
56 my $cities_set = Koha::Cities->new;
57 my $cities = $c->objects->search( $cities_set, \&to_model, \&to_api );
58 $c->render( status => 200, json => $cities );
64 if ( exists $params->{nombre} ) {
65 $params->{city_name} = delete $params->{nombre};
74 if ( exists $params->{city_name} ) {
75 $params->{nombre} = delete $params->{city_name};
82 use Test::More tests => 2;
85 use t::lib::TestBuilder;
88 my $schema = Koha::Database->new()->schema();
91 my $builder = t::lib::TestBuilder->new;
93 subtest 'objects.search helper' => sub {
97 my $t = Test::Mojo->new;
99 $schema->storage->txn_begin;
101 # Remove existing cities to have more control on the search restuls
102 Koha::Cities->delete;
104 # Create two sample patrons that match the query
105 $builder->build_object({
106 class => 'Koha::Cities',
108 city_name => 'Manuel'
111 $builder->build_object({
112 class => 'Koha::Cities',
114 city_name => 'Manuela'
118 $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
120 ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
123 ->json_is('/0/city_name' => 'Manuel');
125 $builder->build_object({
126 class => 'Koha::Cities',
128 city_name => 'Emanuel'
133 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=starts_with')
138 ->json_is('/0/city_name' => 'Manuel')
139 ->json_is('/1/city_name' => 'Manuela');
142 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=ends_with')
147 ->json_is('/0/city_name' => 'Manuel')
148 ->json_is('/1/city_name' => 'Emanuel');
151 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=exact')
155 ->json_is('/0/city_name' => 'Manuel');
158 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=contains')
164 ->json_is('/0/city_name' => 'Manuel')
165 ->json_is('/1/city_name' => 'Manuela')
166 ->json_is('/2/city_name' => 'Emanuel');
170 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
175 ->json_is('/0/city_name' => 'Manuel')
176 ->json_is('/1/city_name' => 'Manuela');
179 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
184 ->json_is('/0/city_name' => 'Manuel')
185 ->json_is('/1/city_name' => 'Emanuel');
188 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
192 ->json_is('/0/city_name' => 'Manuel');
195 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
201 ->json_is('/0/city_name' => 'Manuel')
202 ->json_is('/1/city_name' => 'Manuela')
203 ->json_is('/2/city_name' => 'Emanuel');
205 ## _to_model && _to_api tests
207 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
212 ->json_is('/0/nombre' => 'Manuel')
213 ->json_is('/1/nombre' => 'Manuela');
216 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
221 ->json_is('/0/nombre' => 'Manuel')
222 ->json_is('/1/nombre' => 'Emanuel');
225 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
229 ->json_is('/0/nombre' => 'Manuel');
232 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
238 ->json_is('/0/nombre' => 'Manuel')
239 ->json_is('/1/nombre' => 'Manuela')
240 ->json_is('/2/nombre' => 'Emanuel');
242 $schema->storage->txn_rollback;
245 subtest 'objects.search helper, sorting on mapped column' => sub {
249 my $t = Test::Mojo->new;
251 $schema->storage->txn_begin;
253 # Have complete control over the existing cities to ease testing
254 Koha::Cities->delete;
256 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'A', city_country => 'Argentina' } });
257 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'B', city_country => 'Argentina' } });
259 $t->get_ok('/cities_sorted?_order_by=%2Bnombre&_order_by=+city_country')
264 ->json_is('/0/nombre' => 'A')
265 ->json_is('/1/nombre' => 'B');
267 $t->get_ok('/cities_sorted?_order_by=-nombre')
272 ->json_is('/0/nombre' => 'B')
273 ->json_is('/1/nombre' => 'A');
275 $schema->storage->txn_rollback;