Bug 24191: Regression tests
[koha.git] / t / db_dependent / Koha / REST / Plugin / Objects.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use Koha::Cities;
20
21 # Dummy app for testing the plugin
22 use Mojolicious::Lite;
23
24 app->log->level('error');
25
26 plugin 'Koha::REST::Plugin::Objects';
27 plugin 'Koha::REST::Plugin::Query';
28 plugin 'Koha::REST::Plugin::Pagination';
29
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 );
35 };
36
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 );
43 };
44
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 );
51 };
52
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 );
59 };
60
61 sub to_model {
62     my $params = shift;
63
64     if ( exists $params->{nombre} ) {
65         $params->{city_name} = delete $params->{nombre};
66     }
67
68     return $params;
69 }
70
71 sub to_api {
72     my $params = shift;
73
74     if ( exists $params->{city_name} ) {
75         $params->{nombre} = delete $params->{city_name};
76     }
77
78     return $params;
79 }
80
81 # The tests
82 use Test::More tests => 2;
83 use Test::Mojo;
84
85 use t::lib::TestBuilder;
86 use Koha::Database;
87
88 my $schema = Koha::Database->new()->schema();
89
90
91 my $builder = t::lib::TestBuilder->new;
92
93 subtest 'objects.search helper' => sub {
94
95     plan tests => 90;
96
97     my $t = Test::Mojo->new;
98
99     $schema->storage->txn_begin;
100
101     # Remove existing cities to have more control on the search restuls
102     Koha::Cities->delete;
103
104     # Create two sample patrons that match the query
105     $builder->build_object({
106         class => 'Koha::Cities',
107         value => {
108             city_name => 'Manuel'
109         }
110     });
111     $builder->build_object({
112         class => 'Koha::Cities',
113         value => {
114             city_name => 'Manuela'
115         }
116     });
117
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');
124
125     $builder->build_object({
126         class => 'Koha::Cities',
127         value => {
128             city_name => 'Emanuel'
129         }
130     });
131
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');
140
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');
149
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');
156
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');
167
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');
177
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');
186
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');
193
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');
204
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');
214
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');
223
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');
230
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');
241
242     $schema->storage->txn_rollback;
243 };
244
245 subtest 'objects.search helper, sorting on mapped column' => sub {
246
247     plan tests => 14;
248
249     my $t = Test::Mojo->new;
250
251     $schema->storage->txn_begin;
252
253     # Have complete control over the existing cities to ease testing
254     Koha::Cities->delete;
255
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' } });
258
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');
266
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');
274
275     $schema->storage->txn_rollback;
276 }