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>.
20 use Test::More tests => 5;
24 use t::lib::TestBuilder;
27 use JSON qw( encode_json );
29 use Koha::Acquisition::Orders;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'list() tests' => sub {
43 $schema->storage->txn_begin;
45 my $patron = $builder->build_object({
46 class => 'Koha::Patrons',
47 value => { flags => 1 }
49 my $password = 'thePassword123';
50 $patron->set_password({ password => $password, skip_validation => 1 });
51 my $userid = $patron->userid;
53 my $basket = $builder->build_object({ class => 'Koha::Acquisition::Baskets', value => { is_standing => 1 } });
55 my $biblio = $builder->build_sample_biblio;
56 my $biblioitem = $biblio->biblioitem;
57 $biblioitem->set({ isbn => 'SOMETHING' })->store;
60 my $order = $builder->build_object(
62 class => 'Koha::Acquisition::Orders',
64 basketno => $basket->basketno,
66 biblionumber => $biblio->biblionumber
70 my $another_order = $order->unblessed; # create a copy of $order but make
71 delete $another_order->{ordernumber}; # sure ordernumber will be regenerated
72 $another_order = $builder->build_object({ class => 'Koha::Acquisition::Orders', value => $another_order });
74 ## Authorized user tests
75 my $count_of_orders = Koha::Acquisition::Orders->search->count;
76 # Make sure we are returned with the correct amount of orders
77 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?_per_page=-1" )
78 ->status_is( 200, 'SWAGGER3.2.2' )
79 ->json_has('/'.($count_of_orders-1).'/order_id')
80 ->json_hasnt('/'.($count_of_orders).'/order_id');
82 subtest 'query parameters' => sub {
85 biblio_id => 'biblionumber',
86 basket_id => 'basketno',
87 fund_id => 'budget_id',
90 my $size = keys %{$fields};
92 plan tests => $size * (2 + 2 * $size);
94 foreach my $field ( keys %{$fields} ) {
95 my $model_field = $fields->{ $field };
96 my $result = $t->get_ok("//$userid:$password@/api/v1/acquisitions/orders?$field=" . $order->$model_field)
99 foreach my $key ( keys %{$fields} ) {
100 my $key_field = $fields->{ $key };
101 # Check the result order first since it's not predefined.
102 if ($result->tx->res->json->[0]->{$key} eq $order->$key_field) {
103 $result->json_is( "/0/$key", $order->$key_field );
104 $result->json_is( "/1/$key", $another_order->$key_field );
106 $result->json_is( "/0/$key", $another_order->$key_field );
107 $result->json_is( "/1/$key", $order->$key_field );
113 my $query = { "biblio.isbn" => { "-like" => "\%SOMETHING\%" } };
114 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?q=" . encode_json($query) => {'x-koha-embed' => 'biblio'} )
115 ->status_is( 200, "Embedding biblio and searching on biblio.isbn doesn't make it explode" )
116 ->json_has( "/0/biblio", "biblio object correctly embedded" )
117 ->json_is( "/0/biblio/isbn", 'SOMETHING', 'Filtering by a biblioitems column works!' );
119 $query = { "biblio.isbn" => { "-like" => "\%SOMETHING\%" }, "biblio.title" => { -like => "\%\%" } };
120 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?q=" . encode_json($query) => {'x-koha-embed' => 'biblio'} )
121 ->status_is( 200, "Embedding biblio and searching on biblio.isbn and biblio.title doesn't make it explode" )
122 ->json_has( "/0/biblio", "biblio object correctly embedded" )
123 ->json_is( "/0/biblio/isbn", 'SOMETHING', 'Filtering by a biblioitem column works!' )
124 ->json_is( "/0/biblio/title", $biblio->title, 'Filtering by a biblioitem and biblio columns works!' );
126 my $result = $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?biblio_id=" . $biblio->biblionumber )
129 is( scalar @{ $result->tx->res->json}, 2, 'Two orders fetched' );
131 # Mark $another_order as cancelled
132 $another_order->set({ orderstatus => 'cancelled' })->store;
134 $result = $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?only_active=1&biblio_id=" . $biblio->biblionumber )
135 ->status_is( 200, "only_active parameter accepted" );
137 is( scalar @{ $result->tx->res->json}, 1, 'Only one order is active' );
139 # Warn on unsupported query parameter
140 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?order_blah=blah" )
142 ->json_is( [{ path => '/query/order_blah', message => 'Malformed query string'}] );
144 subtest 'sorting tests' => sub {
148 $schema->storage->txn_begin;
150 my $biblio_1 = $builder->build_sample_biblio();
151 my $biblio_2 = $builder->build_sample_biblio();
153 my $basket = $builder->build_object({ class => 'Koha::Acquisition::Baskets', value => { is_standing => 1 } });
154 my $order_1 = $builder->build_object(
156 class => 'Koha::Acquisition::Orders',
158 basketno => $basket->basketno,
159 orderstatus => 'new',
160 biblionumber => $biblio_1->biblionumber
164 my $order_2 = $builder->build_object(
166 class => 'Koha::Acquisition::Orders',
168 basketno => $basket->basketno,
169 orderstatus => 'new',
170 biblionumber => $biblio_2->biblionumber
177 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?_order_by=+me.order_id&basket_id=" . $basket->id )
178 ->status_is( 200, "query successful" )
181 is( scalar @$result, 2, 'Two elements returned' );
183 is( $result->[0]->{order_id}, $order_1->id, 'The first element is order_1' );
184 is( $result->[1]->{order_id}, $order_2->id, 'The second element is order_2' );
188 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?_order_by=-me.order_id&basket_id=" . $basket->id )
189 ->status_is( 200, "query successful" )
192 is( scalar @$result, 2, 'Two elements returned' );
194 is( $result->[0]->{order_id}, $order_2->id, 'The first element is order_2' );
195 is( $result->[1]->{order_id}, $order_1->id, 'The second element is order_1' );
197 $biblio_1->biblioitem->set(
201 publishercode => 'M',
205 $biblio_2->biblioitem->set(
209 publishercode => 'L',
214 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?_order_by=-biblio.isbn,+biblio.ean,-biblio.publisher&basket_id=" . $basket->id => {'x-koha-embed' => 'biblio'} )
215 ->status_is( 200, "query successful" )
218 is( $result->[0]->{order_id}, $order_2->id, 'Ordered correctly' );
219 is( $result->[1]->{order_id}, $order_1->id, 'Ordered correctly' );
221 $schema->storage->txn_rollback;
224 $schema->storage->txn_rollback;
227 subtest 'get() tests' => sub {
231 $schema->storage->txn_begin;
233 my $order = $builder->build_object(
235 class => 'Koha::Acquisition::Orders',
236 value => { orderstatus => 'new' }
239 my $patron = $builder->build_object({
240 class => 'Koha::Patrons',
241 value => { flags => 2048 }
243 my $password = 'thePassword123';
244 $patron->set_password({ password => $password, skip_validation => 1 });
245 my $userid = $patron->userid;
247 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
248 ->status_is( 200, 'SWAGGER3.2.2' )
249 ->json_is( '' => $order->to_api, 'SWAGGER3.3.2' );
251 my $non_existent_order_id = $order->ordernumber;
254 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $non_existent_order_id )
256 ->json_is( '/error' => 'Order not found' );
258 # FIXME This does not work on all the OS we support
259 ## Regression tests for bug 25513
260 ## Pick a high value that could be transformed into exponential
261 ## representation and not considered a number by buggy DBD::mysql versions
262 #$order = $builder->build_object(
264 # class => 'Koha::Acquisition::Orders',
266 # orderstatus => 'new',
267 # ecost_tax_excluded => 9963405519357589504,
268 # unitprice => 10177559957753600000
273 #$t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
274 # ->json_is( '' => $order->to_api, 'Number representation should be consistent' );
276 $schema->storage->txn_rollback;
279 subtest 'add() tests' => sub {
283 $schema->storage->txn_begin;
285 my $authorized_patron = $builder->build_object({
286 class => 'Koha::Patrons',
287 value => { flags => 1 }
289 my $password = 'thePassword123';
290 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
291 my $auth_userid = $authorized_patron->userid;
293 my $unauthorized_patron = $builder->build_object({
294 class => 'Koha::Patrons',
295 value => { flags => 4 }
297 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
298 my $unauth_userid = $unauthorized_patron->userid;
300 my $order_obj = $builder->build_object(
302 class => 'Koha::Acquisition::Orders',
304 orderstatus => 'new',
306 replacementprice => 10,
308 quantityreceived => 0,
309 datecancellationprinted => undef,
310 order_internalnote => 'This is a dummy note for testing'
314 my $order = $order_obj->to_api;
316 delete $order->{ordernumber};
317 $order->{uncertain_price} = Mojo::JSON->false;
319 # Unauthorized attempt to write
320 $t->post_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
323 # Authorized attempt to write invalid data
324 my $order_with_invalid_field = { %$order };
325 $order_with_invalid_field->{'orderinvalid'} = 'Order invalid';
327 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order_with_invalid_field )
332 message => "Properties not allowed: orderinvalid.",
338 # Authorized attempt to write
339 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
340 ->status_is( 201, 'SWAGGER3.2.1' )
341 ->json_is( '/internal_note' => $order->{internal_note}, 'SWAGGER3.3.1' )
342 ->header_like( Location => qr/\/api\/v1\/acquisitions\/orders\/\d*/, 'SWAGGER3.4.1' );
345 my $order_id = $order->{order_id};
346 # Authorized attempt to create with null id
347 $order->{order_id} = undef;
349 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
351 ->json_has('/errors');
353 # Authorized attempt to create with existing id
354 $order->{order_id} = $order_id;
357 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
359 ->json_has( '/error' => "Fails when trying to add an existing order_id")
360 ->json_like( '/conflict' => qr/(aqorders\.)?PRIMARY/ ); }
361 qr/DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(aqorders\.)?PRIMARY'/;
363 $schema->storage->txn_rollback;
366 subtest 'update() tests' => sub {
369 $schema->storage->txn_begin;
371 my $authorized_patron = $builder->build_object({
372 class => 'Koha::Patrons',
373 value => { flags => 1 }
375 my $password = 'thePassword123';
376 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
377 my $auth_userid = $authorized_patron->userid;
379 my $unauthorized_patron = $builder->build_object({
380 class => 'Koha::Patrons',
381 value => { flags => 4 }
383 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
384 my $unauth_userid = $unauthorized_patron->userid;
386 my $library = $builder->build_object({ class => 'Koha::Libraries' });
387 my $library_id = $library->branchcode;
389 # Unauthorized attempt to update
390 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
391 => json => { name => 'New unauthorized name change' } )
394 # Attempt partial update on a PUT
395 my $library_with_missing_field = {
396 address1 => "New library address",
399 my $result = $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
401 # Check the result order first since it's not predefined.
402 if ($result->tx->res->json->{errors}->[0]->{path} eq '/body/name') {
406 {message => "Missing property.", path => "/body/name"},
407 {message => "Missing property.", path => "/body/library_id"}
414 {message => "Missing property.", path => "/body/library_id"},
415 {message => "Missing property.", path => "/body/name"}
420 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
421 my $library_with_updated_field = $deleted_library->to_api;
422 $library_with_updated_field->{library_id} = $library_id;
423 $deleted_library->delete;
425 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
426 ->status_is(200, 'SWAGGER3.2.1')
427 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
429 # Authorized attempt to write invalid data
430 my $library_with_invalid_field = { %$library_with_updated_field };
431 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
433 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
438 message => "Properties not allowed: branchinvalid.",
444 my $non_existent_code = 'nope'.int(rand(10000));
445 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
448 $schema->storage->txn_rollback;
451 subtest 'delete() tests' => sub {
454 $schema->storage->txn_begin;
456 my $authorized_patron = $builder->build_object({
457 class => 'Koha::Patrons',
458 value => { flags => 1 }
460 my $password = 'thePassword123';
461 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
462 my $auth_userid = $authorized_patron->userid;
464 my $unauthorized_patron = $builder->build_object({
465 class => 'Koha::Patrons',
466 value => { flags => 4 }
468 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
469 my $unauth_userid = $unauthorized_patron->userid;
471 my $order = $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
473 # Unauthorized attempt to delete
474 $t->delete_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
477 $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
478 ->status_is(204, 'SWAGGER3.2.4')
479 ->content_is('', 'SWAGGER3.3.4');
481 $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
484 $schema->storage->txn_rollback;