Bug 24191: Regression tests
[koha.git] / t / db_dependent / Koha / REST / Plugin / Objects.t
bloba65d949fc654f0422b9924d93cd8afc6ad522580
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
19 use Koha::Cities;
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 {
31 my $c = shift;
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 {
38 my $c = shift;
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 {
46 my $c = shift;
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 {
54 my $c = shift;
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 );
61 sub to_model {
62 my $params = shift;
64 if ( exists $params->{nombre} ) {
65 $params->{city_name} = delete $params->{nombre};
68 return $params;
71 sub to_api {
72 my $params = shift;
74 if ( exists $params->{city_name} ) {
75 $params->{nombre} = delete $params->{city_name};
78 return $params;
81 # The tests
82 use Test::More tests => 2;
83 use Test::Mojo;
85 use t::lib::TestBuilder;
86 use Koha::Database;
88 my $schema = Koha::Database->new()->schema();
91 my $builder = t::lib::TestBuilder->new;
93 subtest 'objects.search helper' => sub {
95 plan tests => 90;
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',
107 value => {
108 city_name => 'Manuel'
111 $builder->build_object({
112 class => 'Koha::Cities',
113 value => {
114 city_name => 'Manuela'
118 $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
119 ->status_is(200)
120 ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
121 ->json_has('/0')
122 ->json_hasnt('/1')
123 ->json_is('/0/city_name' => 'Manuel');
125 $builder->build_object({
126 class => 'Koha::Cities',
127 value => {
128 city_name => 'Emanuel'
132 # _match=starts_with
133 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=starts_with')
134 ->status_is(200)
135 ->json_has('/0')
136 ->json_has('/1')
137 ->json_hasnt('/2')
138 ->json_is('/0/city_name' => 'Manuel')
139 ->json_is('/1/city_name' => 'Manuela');
141 # _match=ends_with
142 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=ends_with')
143 ->status_is(200)
144 ->json_has('/0')
145 ->json_has('/1')
146 ->json_hasnt('/2')
147 ->json_is('/0/city_name' => 'Manuel')
148 ->json_is('/1/city_name' => 'Emanuel');
150 # _match=exact
151 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=exact')
152 ->status_is(200)
153 ->json_has('/0')
154 ->json_hasnt('/1')
155 ->json_is('/0/city_name' => 'Manuel');
157 # _match=contains
158 $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=contains')
159 ->status_is(200)
160 ->json_has('/0')
161 ->json_has('/1')
162 ->json_has('/2')
163 ->json_hasnt('/3')
164 ->json_is('/0/city_name' => 'Manuel')
165 ->json_is('/1/city_name' => 'Manuela')
166 ->json_is('/2/city_name' => 'Emanuel');
168 ## _to_model tests
169 # _match=starts_with
170 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
171 ->status_is(200)
172 ->json_has('/0')
173 ->json_has('/1')
174 ->json_hasnt('/2')
175 ->json_is('/0/city_name' => 'Manuel')
176 ->json_is('/1/city_name' => 'Manuela');
178 # _match=ends_with
179 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
180 ->status_is(200)
181 ->json_has('/0')
182 ->json_has('/1')
183 ->json_hasnt('/2')
184 ->json_is('/0/city_name' => 'Manuel')
185 ->json_is('/1/city_name' => 'Emanuel');
187 # _match=exact
188 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
189 ->status_is(200)
190 ->json_has('/0')
191 ->json_hasnt('/1')
192 ->json_is('/0/city_name' => 'Manuel');
194 # _match=contains
195 $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
196 ->status_is(200)
197 ->json_has('/0')
198 ->json_has('/1')
199 ->json_has('/2')
200 ->json_hasnt('/3')
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
206 # _match=starts_with
207 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
208 ->status_is(200)
209 ->json_has('/0')
210 ->json_has('/1')
211 ->json_hasnt('/2')
212 ->json_is('/0/nombre' => 'Manuel')
213 ->json_is('/1/nombre' => 'Manuela');
215 # _match=ends_with
216 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
217 ->status_is(200)
218 ->json_has('/0')
219 ->json_has('/1')
220 ->json_hasnt('/2')
221 ->json_is('/0/nombre' => 'Manuel')
222 ->json_is('/1/nombre' => 'Emanuel');
224 # _match=exact
225 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
226 ->status_is(200)
227 ->json_has('/0')
228 ->json_hasnt('/1')
229 ->json_is('/0/nombre' => 'Manuel');
231 # _match=contains
232 $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
233 ->status_is(200)
234 ->json_has('/0')
235 ->json_has('/1')
236 ->json_has('/2')
237 ->json_hasnt('/3')
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 {
247 plan tests => 14;
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')
260 ->status_is(200)
261 ->json_has('/0')
262 ->json_has('/1')
263 ->json_hasnt('/2')
264 ->json_is('/0/nombre' => 'A')
265 ->json_is('/1/nombre' => 'B');
267 $t->get_ok('/cities_sorted?_order_by=-nombre')
268 ->status_is(200)
269 ->json_has('/0')
270 ->json_has('/1')
271 ->json_hasnt('/2')
272 ->json_is('/0/nombre' => 'B')
273 ->json_is('/1/nombre' => 'A');
275 $schema->storage->txn_rollback;