Bug 25514: Try to fix random failure from REST/Plugin/Objects.t
[koha.git] / t / db_dependent / Koha / REST / Plugin / Objects.t
blobcbec228ce9221eda74957c6b01d312e55ebf0788
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;
20 use Koha::Acquisition::Orders;
21 use Koha::Cities;
22 use Koha::Holds;
23 use Koha::Biblios;
25 # Dummy app for testing the plugin
26 use Mojolicious::Lite;
28 app->log->level('error');
30 plugin 'Koha::REST::Plugin::Objects';
31 plugin 'Koha::REST::Plugin::Query';
32 plugin 'Koha::REST::Plugin::Pagination';
34 get '/cities' => sub {
35 my $c = shift;
36 $c->validation->output($c->req->params->to_hash);
37 my $cities = $c->objects->search(Koha::Cities->new);
38 $c->render( status => 200, json => $cities );
41 get '/orders' => sub {
42 my $c = shift;
43 $c->stash('koha.embed', ( { fund => {} } ) );
44 $c->validation->output($c->req->params->to_hash);
45 my $orders = $c->objects->search(Koha::Acquisition::Orders->new);
46 $c->render( status => 200, json => $orders );
49 get '/patrons/:patron_id/holds' => sub {
50 my $c = shift;
51 my $params = $c->req->params->to_hash;
52 $params->{patron_id} = $c->stash("patron_id");
53 $c->validation->output($params);
54 my $holds_set = Koha::Holds->new;
55 my $holds = $c->objects->search( $holds_set );
56 $c->render( status => 200, json => {count => scalar(@$holds)} );
59 get '/biblios' => sub {
60 my $c = shift;
61 my $output = $c->req->params->to_hash;
62 $output->{query} = $c->req->json if defined $c->req->json;
63 my $headers = $c->req->headers->to_hash;
64 $output->{'x-koha-query'} = $headers->{'x-koha-query'} if defined $headers->{'x-koha-query'};
65 $c->validation->output($output);
66 my $biblios_set = Koha::Biblios->new;
67 $c->stash("koha.embed", {
68 "suggestions" => {
69 children => {
70 "suggester" => {}
73 });
74 my $biblios = $c->objects->search($biblios_set);
75 $c->render( status => 200, json => {count => scalar(@$biblios), biblios => $biblios} );
79 # The tests
80 use Test::More tests => 10;
81 use Test::Mojo;
83 use t::lib::Mocks;
84 use t::lib::TestBuilder;
85 use Koha::Database;
87 my $schema = Koha::Database->new()->schema();
88 my $builder = t::lib::TestBuilder->new;
90 subtest 'objects.search helper' => sub {
92 plan tests => 50;
94 $schema->storage->txn_begin;
96 # Remove existing cities to have more control on the search results
97 Koha::Cities->delete;
99 # Create three sample cities that match the query. This makes sure we
100 # always have a "next" link regardless of Mojolicious::Plugin::OpenAPI version.
101 $builder->build_object({
102 class => 'Koha::Cities',
103 value => {
104 city_name => 'Manuel'
107 $builder->build_object({
108 class => 'Koha::Cities',
109 value => {
110 city_name => 'Manuela'
113 $builder->build_object({
114 class => 'Koha::Cities',
115 value => {
116 city_name => 'Manuelab'
120 my $t = Test::Mojo->new;
121 $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
122 ->status_is(200)
123 ->header_like( 'Link' => qr/<http:\/\/.*[\?&]_page=2.*>; rel="next",/ )
124 ->json_has('/0')
125 ->json_hasnt('/1')
126 ->json_is('/0/name' => 'Manuel');
128 $builder->build_object({
129 class => 'Koha::Cities',
130 value => {
131 city_name => 'Emanuel'
135 # _match=starts_with
136 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=starts_with')
137 ->status_is(200)
138 ->json_has('/0')
139 ->json_has('/1')
140 ->json_has('/2')
141 ->json_hasnt('/3')
142 ->json_is('/0/name' => 'Manuel')
143 ->json_is('/1/name' => 'Manuela')
144 ->json_is('/2/name' => 'Manuelab');
146 # _match=ends_with
147 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=ends_with')
148 ->status_is(200)
149 ->json_has('/0')
150 ->json_has('/1')
151 ->json_hasnt('/2')
152 ->json_is('/0/name' => 'Manuel')
153 ->json_is('/1/name' => 'Emanuel');
155 # _match=exact
156 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=exact')
157 ->status_is(200)
158 ->json_has('/0')
159 ->json_hasnt('/1')
160 ->json_is('/0/name' => 'Manuel');
162 # _match=contains
163 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=contains')
164 ->status_is(200)
165 ->json_has('/0')
166 ->json_has('/1')
167 ->json_has('/2')
168 ->json_has('/3')
169 ->json_hasnt('/4')
170 ->json_is('/0/name' => 'Manuel')
171 ->json_is('/1/name' => 'Manuela')
172 ->json_is('/2/name' => 'Manuelab')
173 ->json_is('/3/name' => 'Emanuel');
175 # Add 20 more cities
176 for ( 1..20 ) {
177 $builder->build_object({ class => 'Koha::Cities' });
180 t::lib::Mocks::mock_preference('RESTdefaultPageSize', 20 );
181 $t->get_ok('/cities')
182 ->status_is(200);
184 my $response_count = scalar @{ $t->tx->res->json };
185 is( $response_count, 20, 'RESTdefaultPageSize is honoured by default (20)' );
187 t::lib::Mocks::mock_preference('RESTdefaultPageSize', 5 );
188 $t->get_ok('/cities')
189 ->status_is(200);
191 $response_count = scalar @{ $t->tx->res->json };
192 is( $response_count, 5, 'RESTdefaultPageSize is honoured by default (5)' );
194 $t->get_ok('/cities?_page=1&_per_page=-1')
195 ->status_is(200);
197 $response_count = scalar @{ $t->tx->res->json };
198 is( $response_count, 24, '_per_page=-1 means all resources' );
200 $t->get_ok('/cities?_page=100&_per_page=-1')
201 ->status_is(200);
203 $response_count = scalar @{ $t->tx->res->json };
204 is( $response_count, 24, 'When _per_page=-1 the page param is not considered' );
206 $schema->storage->txn_rollback;
209 subtest 'objects.search helper, sorting on mapped column' => sub {
211 plan tests => 14;
213 $schema->storage->txn_begin;
215 # Have complete control over the existing cities to ease testing
216 Koha::Cities->delete;
218 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'A', city_country => 'Argentina' } });
219 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'B', city_country => 'Argentina' } });
221 my $t = Test::Mojo->new;
222 $t->get_ok('/cities?_order_by=%2Bname&_order_by=+country')
223 ->status_is(200)
224 ->json_has('/0')
225 ->json_has('/1')
226 ->json_hasnt('/2')
227 ->json_is('/0/name' => 'A')
228 ->json_is('/1/name' => 'B');
230 $t->get_ok('/cities?_order_by=-name')
231 ->status_is(200)
232 ->json_has('/0')
233 ->json_has('/1')
234 ->json_hasnt('/2')
235 ->json_is('/0/name' => 'B')
236 ->json_is('/1/name' => 'A');
238 $schema->storage->txn_rollback;
241 subtest 'objects.search helper, encoding' => sub {
243 plan tests => 5;
245 $schema->storage->txn_begin;
247 Koha::Cities->delete;
249 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'A', city_country => 'Argentina' } });
250 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'B', city_country => '❤Argentina❤' } });
252 my $t = Test::Mojo->new;
253 $t->get_ok('/cities?q={"country": "❤Argentina❤"}')
254 ->status_is(200)
255 ->json_has('/0')
256 ->json_hasnt('/1')
257 ->json_is('/0/name' => 'B');
259 $schema->storage->txn_rollback;
262 subtest 'objects.search helper, embed' => sub {
264 plan tests => 2;
266 $schema->storage->txn_begin;
268 my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' });
270 my $t = Test::Mojo->new;
271 $t->get_ok('/orders?order_id=' . $order->ordernumber)
272 ->json_is('/0',$order->to_api({ embed => ( { fund => {} } ) }));
274 $schema->storage->txn_rollback;
277 subtest 'objects.search helper, with path parameters and _match' => sub {
278 plan tests => 8;
280 $schema->storage->txn_begin;
282 Koha::Holds->search()->delete;
284 my $patron = Koha::Patrons->find(10);
285 $patron->delete if $patron;
286 $patron = $builder->build_object( { class => "Koha::Patrons" } );
287 $patron->borrowernumber(10)->store;
288 $builder->build_object(
290 class => "Koha::Holds",
291 value => { borrowernumber => $patron->borrowernumber }
295 my $t = Test::Mojo->new;
296 $t->get_ok('/patrons/1/holds?_match=exact')
297 ->json_is('/count' => 0, 'there should be no holds for borrower 1 with _match=exact');
299 $t->get_ok('/patrons/1/holds?_match=contains')
300 ->json_is('/count' => 0, 'there should be no holds for borrower 1 with _match=contains');
302 $t->get_ok('/patrons/10/holds?_match=exact')
303 ->json_is('/count' => 1, 'there should be 1 hold for borrower 10 with _match=exact');
305 $t->get_ok('/patrons/10/holds?_match=contains')
306 ->json_is('/count' => 1, 'there should be 1 hold for borrower 10 with _match=contains');
308 $schema->storage->txn_rollback;
311 subtest 'object.search helper with query parameter' => sub {
312 plan tests => 4;
314 $schema->storage->txn_begin;
316 my $patron1 = $builder->build_object( { class => "Koha::Patrons" } );
317 my $patron2 = $builder->build_object( { class => "Koha::Patrons" } );
318 my $biblio1 = $builder->build_sample_biblio;
319 my $biblio2 = $builder->build_sample_biblio;
320 my $biblio3 = $builder->build_sample_biblio;
321 my $suggestion1 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron1->borrowernumber, biblionumber => $biblio1->biblionumber} } );
322 my $suggestion2 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio2->biblionumber} } );
323 my $suggestion3 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio3->biblionumber} } );
325 my $t = Test::Mojo->new;
326 $t->get_ok('/biblios' => json => {"suggestions.suggester.patron_id" => $patron1->borrowernumber })
327 ->json_is('/count' => 1, 'there should be 1 biblio with suggestions of patron 1');
329 $t->get_ok('/biblios' => json => {"suggestions.suggester.patron_id" => $patron2->borrowernumber })
330 ->json_is('/count' => 2, 'there should be 2 biblios with suggestions of patron 2');
332 $schema->storage->txn_rollback;
335 subtest 'object.search helper with q parameter' => sub {
336 plan tests => 4;
338 $schema->storage->txn_begin;
340 my $patron1 = $builder->build_object( { class => "Koha::Patrons" } );
341 my $patron2 = $builder->build_object( { class => "Koha::Patrons" } );
342 my $biblio1 = $builder->build_sample_biblio;
343 my $biblio2 = $builder->build_sample_biblio;
344 my $biblio3 = $builder->build_sample_biblio;
345 my $suggestion1 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron1->borrowernumber, biblionumber => $biblio1->biblionumber} } );
346 my $suggestion2 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio2->biblionumber} } );
347 my $suggestion3 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio3->biblionumber} } );
349 my $t = Test::Mojo->new;
350 $t->get_ok('/biblios?q={"suggestions.suggester.patron_id": "'.$patron1->borrowernumber.'"}')
351 ->json_is('/count' => 1, 'there should be 1 biblio with suggestions of patron 1');
353 $t->get_ok('/biblios?q={"suggestions.suggester.patron_id": "'.$patron2->borrowernumber.'"}')
354 ->json_is('/count' => 2, 'there should be 2 biblios with suggestions of patron 2');
356 $schema->storage->txn_rollback;
359 subtest 'object.search helper with x-koha-query header' => sub {
360 plan tests => 4;
362 $schema->storage->txn_begin;
364 my $patron1 = $builder->build_object( { class => "Koha::Patrons" } );
365 my $patron2 = $builder->build_object( { class => "Koha::Patrons" } );
366 my $biblio1 = $builder->build_sample_biblio;
367 my $biblio2 = $builder->build_sample_biblio;
368 my $biblio3 = $builder->build_sample_biblio;
369 my $suggestion1 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron1->borrowernumber, biblionumber => $biblio1->biblionumber} } );
370 my $suggestion2 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio2->biblionumber} } );
371 my $suggestion3 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio3->biblionumber} } );
373 my $t = Test::Mojo->new;
374 $t->get_ok('/biblios' => {'x-koha-query' => '{"suggestions.suggester.patron_id": "'.$patron1->borrowernumber.'"}'})
375 ->json_is('/count' => 1, 'there should be 1 biblio with suggestions of patron 1');
377 $t->get_ok('/biblios' => {'x-koha-query' => '{"suggestions.suggester.patron_id": "'.$patron2->borrowernumber.'"}'})
378 ->json_is('/count' => 2, 'there should be 2 biblios with suggestions of patron 2');
380 $schema->storage->txn_rollback;
383 subtest 'object.search helper with all query methods' => sub {
384 plan tests => 6;
386 $schema->storage->txn_begin;
388 my $patron1 = $builder->build_object( { class => "Koha::Patrons" , value => {firstname=>'patron1'} } );
389 my $patron2 = $builder->build_object( { class => "Koha::Patrons" , value => {firstname=>'patron2'} } );
390 my $biblio1 = $builder->build_sample_biblio;
391 my $biblio2 = $builder->build_sample_biblio;
392 my $biblio3 = $builder->build_sample_biblio;
393 my $suggestion1 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron1->borrowernumber, biblionumber => $biblio1->biblionumber} } );
394 my $suggestion2 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio2->biblionumber} } );
395 my $suggestion3 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio3->biblionumber} } );
397 my $t = Test::Mojo->new;
398 $t->get_ok('/biblios?q={"suggestions.suggester.firstname": "'.$patron1->firstname.'"}' => {'x-koha-query' => '{"suggestions.suggester.patron_id": "'.$patron1->borrowernumber.'"}'} => json => {"suggestions.suggester.cardnumber" => $patron1->cardnumber})
399 ->json_is('/count' => 1, 'there should be 1 biblio with suggestions of patron 1');
401 $t->get_ok('/biblios?q={"suggestions.suggester.firstname": "'.$patron2->firstname.'"}' => {'x-koha-query' => '{"suggestions.suggester.patron_id": "'.$patron2->borrowernumber.'"}'} => json => {"suggestions.suggester.cardnumber" => $patron2->cardnumber})
402 ->json_is('/count' => 2, 'there should be 2 biblios with suggestions of patron 2');
404 $t->get_ok('/biblios?q={"suggestions.suggester.firstname": "'.$patron1->firstname.'"}' => {'x-koha-query' => '{"suggestions.suggester.patron_id": "'.$patron2->borrowernumber.'"}'} => json => {"suggestions.suggester.cardnumber" => $patron2->cardnumber})
405 ->json_is('/count' => 0, 'there shouldn\'t be biblios where suggester has patron1 fistname and patron2 id');
407 $schema->storage->txn_rollback;
410 subtest 'object.search helper order by embedded columns' => sub {
411 plan tests => 3;
413 my $patron1 = $builder->build_object( { class => "Koha::Patrons" , value => {firstname=>'patron1'} } );
414 my $patron2 = $builder->build_object( { class => "Koha::Patrons" , value => {firstname=>'patron2'} } );
415 my $biblio1 = $builder->build_sample_biblio;
416 my $biblio2 = $builder->build_sample_biblio;
417 my $suggestion1 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron1->borrowernumber, biblionumber => $biblio1->biblionumber} } );
418 my $suggestion2 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio2->biblionumber} } );
420 my $t = Test::Mojo->new;
421 $t->get_ok('/biblios?_order_by=-suggestions.suggester.firstname' => json => [{"me.biblio_id" => $biblio1->biblionumber}, {"me.biblio_id" => $biblio2->biblionumber}])
422 ->json_is('/biblios/0/biblio_id' => $biblio2->biblionumber, 'Biblio 2 should be first')
423 ->json_is('/biblios/1/biblio_id' => $biblio1->biblionumber, 'Biblio 1 should be second');
425 $schema->storage->txn_begin;