Bug 24161: Fix failing test (when running slow)
[koha.git] / t / db_dependent / Koha / Acquisition / Order.t
blob7d1add94a4493fc1d5baef9ca69218f6b8ab224d
1 #!/usr/bin/perl
3 # Copyright 2017 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 10;
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
27 use Koha::Database;
28 use Koha::DateUtils qw(dt_from_string);
30 my $schema = Koha::Database->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest 'basket() tests' => sub {
35 plan tests => 2;
37 $schema->storage->txn_begin;
39 my $basket = $builder->build_object(
41 class => 'Koha::Acquisition::Baskets'
44 my $order = $builder->build_object(
46 class => 'Koha::Acquisition::Orders',
47 value => { basketno => $basket->basketno }
51 my $retrieved_basket = $order->basket;
52 is( ref($retrieved_basket), 'Koha::Acquisition::Basket',
53 'Type is correct for ->basket' );
54 is_deeply( $retrieved_basket->unblessed,
55 $basket->unblessed, "Correct basket found and updated" );
57 $schema->storage->txn_rollback;
60 subtest 'biblio() tests' => sub {
62 plan tests => 5;
64 $schema->storage->txn_begin;
66 my $order = $builder->build_object(
68 class => 'Koha::Acquisition::Orders',
69 value => { biblionumber => undef }
73 is( $order->biblio, undef, 'If no linked biblio, undef is returned' );
75 # Add and link a biblio to the order
76 my $biblio = $builder->build_sample_biblio();
77 $order->set({ biblionumber => $biblio->biblionumber })->store->discard_changes;
79 my $THE_biblio = $order->biblio;
80 is( ref($THE_biblio), 'Koha::Biblio', 'Returns a Koha::Biblio object' );
81 is( $THE_biblio->biblionumber, $biblio->biblionumber, 'It is not cheating about the object' );
83 $order->biblio->delete;
84 $order = Koha::Acquisition::Orders->find($order->ordernumber);
85 ok( $order, 'The order is not deleted if the biblio is deleted' );
86 is( $order->biblio, undef, 'order.biblio is correctly set to NULL when the biblio is deleted' );
88 $schema->storage->txn_rollback;
91 subtest 'store' => sub {
92 plan tests => 1;
94 $schema->storage->txn_begin;
95 my $o = $builder->build_object(
97 class => 'Koha::Acquisition::Orders'
101 subtest 'entrydate' => sub {
102 plan tests => 2;
104 my $order;
106 t::lib::Mocks::mock_preference( 'TimeFormat', '12hr' );
107 $order = Koha::Acquisition::Order->new(
109 basketno => $o->basketno,
110 biblionumber => $o->biblionumber,
111 budget_id => $o->budget_id,
112 quantity => 1,
114 )->store;
115 $order->discard_changes;
116 like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
118 t::lib::Mocks::mock_preference( 'TimeFormat', '24hr' );
119 $order = Koha::Acquisition::Order->new(
121 basketno => $o->basketno,
122 biblionumber => $o->biblionumber,
123 budget_id => $o->budget_id,
124 quantity => 1,
126 )->store;
127 $order->discard_changes;
128 like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
130 $schema->storage->txn_rollback;
133 subtest 'fund' => sub {
134 plan tests => 1;
136 $schema->storage->txn_begin;
137 my $o = $builder->build_object(
139 class => 'Koha::Acquisition::Orders',
143 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
144 is( ref( $order->fund ),
145 'Koha::Acquisition::Fund',
146 '->fund should return a Koha::Acquisition::Fund object' );
147 $schema->storage->txn_rollback;
150 subtest 'invoice' => sub {
151 plan tests => 2;
153 $schema->storage->txn_begin;
154 my $o = $builder->build_object(
156 class => 'Koha::Acquisition::Orders',
157 value => { cancellationreason => 'XXXXXXXX', invoiceid => undef }, # not received yet
161 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
162 is( $order->invoice, undef,
163 '->invoice should return undef if no invoice defined yet');
165 my $invoice = $builder->build_object(
167 class => 'Koha::Acquisition::Invoices',
171 $o->invoiceid( $invoice->invoiceid )->store;
172 $order = Koha::Acquisition::Orders->find( $o->ordernumber );
173 is( ref( $order->invoice ), 'Koha::Acquisition::Invoice',
174 '->invoice should return a Koha::Acquisition::Invoice object if an invoice is defined');
176 $schema->storage->txn_rollback;
179 subtest 'subscription' => sub {
180 plan tests => 2;
182 $schema->storage->txn_begin;
183 my $o = $builder->build_object(
185 class => 'Koha::Acquisition::Orders',
186 value => { subscriptionid => undef }, # not linked to a subscription
190 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
191 is( $order->subscription, undef,
192 '->subscription should return undef if not created from a subscription');
194 $o = $builder->build_object(
196 class => 'Koha::Acquisition::Orders',
197 # Will be linked to a subscription by TestBuilder
201 $order = Koha::Acquisition::Orders->find( $o->ordernumber );
202 is( ref( $order->subscription ), 'Koha::Subscription',
203 '->subscription should return a Koha::Subscription object if created from a subscription');
205 $schema->storage->txn_rollback;
208 subtest 'duplicate_to | add_item' => sub {
209 plan tests => 3;
211 $schema->storage->txn_begin;
213 my $item = $builder->build_sample_item;
214 my $order_no_sub = $builder->build_object(
216 class => 'Koha::Acquisition::Orders',
217 value =>
219 biblionumber => $item->biblionumber,
220 subscriptionid => undef, # not linked to a subscription
224 $order_no_sub->basket->create_items(undef)->store; # use syspref
225 $order_no_sub->add_item( $item->itemnumber );
227 $item = $builder->build_sample_item;
228 my $order_from_sub = $builder->build_object(
230 class => 'Koha::Acquisition::Orders',
231 value =>
233 biblionumber => $item->biblionumber,
234 # Will be linked to a subscription by TestBuilder
238 $order_from_sub->basket->create_items(undef)->store; # use syspref
239 $order_from_sub->add_item( $item->itemnumber );
241 my $basket_to = $builder->build_object(
242 { class => 'Koha::Acquisition::Baskets' });
244 subtest 'Create item on receiving' => sub {
245 plan tests => 2;
247 t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
249 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
250 is( $duplicated_order->items->count, 0,
251 'Items should not be copied if the original order did not create items on ordering'
254 $duplicated_order = $order_from_sub->duplicate_to($basket_to);
255 is( $duplicated_order->items->count, 0,
256 'Items should not be copied if the original order is created from a subscription'
260 subtest 'Create item on ordering' => sub {
261 plan tests => 2;
263 t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering');
265 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
266 is( $duplicated_order->items->count, 1,
267 'Items should be copied if items are created on ordering'
270 $duplicated_order = $order_from_sub->duplicate_to($basket_to);
271 is( $duplicated_order->items->count, 0,
272 'Items should never be copied if the original order is created from a subscription'
276 subtest 'Regression tests' => sub {
277 plan tests => 1;
279 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
280 is($duplicated_order->invoiceid, undef, "invoiceid should be set to null for a new duplicated order");
283 $schema->storage->txn_rollback;
286 subtest 'current_item_level_holds() tests' => sub {
288 plan tests => 5;
290 $schema->storage->txn_begin;
292 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
293 my $biblio = $builder->build_sample_biblio();
294 my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
295 my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
296 my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
298 C4::Reserves::AddReserve(
300 branchcode => $patron->branchcode,
301 borrowernumber => $patron->borrowernumber,
302 biblionumber => $biblio->biblionumber,
303 reservation_date => dt_from_string->add( days => -2 ),
304 itemnumber => $item_1->itemnumber,
307 C4::Reserves::AddReserve(
309 branchcode => $patron->branchcode,
310 borrowernumber => $patron->borrowernumber,
311 biblionumber => $biblio->biblionumber,
312 reservation_date => dt_from_string->add( days => -2 ),
313 itemnumber => $item_2->itemnumber,
316 # Add a hold in the future
317 C4::Reserves::AddReserve(
319 branchcode => $patron->branchcode,
320 borrowernumber => $patron->borrowernumber,
321 biblionumber => $biblio->biblionumber,
322 reservation_date => dt_from_string->add( days => 2 ),
323 itemnumber => $item_3->itemnumber,
327 # Add an order with no biblionumber
328 my $order = $builder->build_object(
330 class => 'Koha::Acquisition::Orders',
331 value => {
332 biblionumber => undef
337 my $holds = $order->current_item_level_holds;
339 is( ref($holds), 'Koha::Holds', 'Koha::Holds iterator returned if no linked biblio' );
340 is( $holds->count, 0, 'Count is 0 if no linked biblio' );
342 $order->set({ biblionumber => $biblio->biblionumber })->store->discard_changes;
344 $holds = $order->current_item_level_holds;
346 is( ref($holds), 'Koha::Holds', 'Koha::Holds iterator returned if no linked items' );
347 is( $holds->count, 0, 'Count is 0 if no linked items' );
349 $order->add_item( $item_2->itemnumber );
350 $order->add_item( $item_3->itemnumber );
352 $holds = $order->current_item_level_holds;
353 is( $holds->count, 1, 'Only current (not future) holds are returned');
355 $schema->storage->txn_rollback;
358 subtest 'claim*' => sub {
359 plan tests => 6;
361 $schema->storage->txn_begin;
362 my $order = $builder->build_object(
364 class => 'Koha::Acquisition::Orders',
368 my $now = dt_from_string;
369 is( $order->claims->count, 0, 'No claim yet, ->claims should return an empty set');
370 is( $order->claims_count, 0, 'No claim yet, ->claims_count should return 0');
371 is( $order->claimed_date, undef, 'No claim yet, ->claimed_date should return undef');
373 my $claim_1 = $order->claim;
374 my $claim_2 = $order->claim;
376 $claim_2->claimed_on($now->clone->subtract(days => 1))->store;
378 is( $order->claims->count, 2, '->claims should return the correct number of claims');
379 is( $order->claims_count, 2, '->claims_count should return the correct number of claims');
380 is( dt_from_string($order->claimed_date), $now, '->claimed_date should return the date of the last claim');
382 $schema->storage->txn_rollback;
385 subtest 'filter_by_late' => sub {
386 plan tests => 16;
388 $schema->storage->txn_begin;
389 my $now = dt_from_string;
390 my $bookseller = $builder->build_object(
392 class => 'Koha::Acquisition::Booksellers',
393 value => { deliverytime => 2 }
396 my $basket_1 = $builder->build_object(
398 class => 'Koha::Acquisition::Baskets',
399 value => {
400 booksellerid => $bookseller->id,
401 closedate => undef,
405 my $order_1 = $builder->build_object(
407 class => 'Koha::Acquisition::Orders',
408 value => {
409 basketno => $basket_1->basketno,
410 datereceived => undef,
411 datecancellationprinted => undef,
415 my $basket_2 = $builder->build_object( # expected tomorrow
417 class => 'Koha::Acquisition::Baskets',
418 value => {
419 booksellerid => $bookseller->id,
420 closedate => $now->clone->subtract( days => 1 ),
424 my $order_2 = $builder->build_object(
426 class => 'Koha::Acquisition::Orders',
427 value => {
428 basketno => $basket_2->basketno,
429 datereceived => undef,
430 datecancellationprinted => undef,
434 my $basket_3 = $builder->build_object( # expected yesterday (1 day)
436 class => 'Koha::Acquisition::Baskets',
437 value => {
438 booksellerid => $bookseller->id,
439 closedate => $now->clone->subtract( days => 3 ),
443 my $order_3 = $builder->build_object(
445 class => 'Koha::Acquisition::Orders',
446 value => {
447 basketno => $basket_3->basketno,
448 datereceived => undef,
449 datecancellationprinted => undef,
453 my $basket_4 = $builder->build_object( # expected 3 days ago
455 class => 'Koha::Acquisition::Baskets',
456 value => {
457 booksellerid => $bookseller->id,
458 closedate => $now->clone->subtract( days => 5 ),
462 my $order_4 = $builder->build_object(
464 class => 'Koha::Acquisition::Orders',
465 value => {
466 basketno => $basket_4->basketno,
467 datereceived => undef,
468 datecancellationprinted => undef,
473 my $orders = Koha::Acquisition::Orders->search(
475 ordernumber => {
476 -in => [
477 $order_1->ordernumber, $order_2->ordernumber,
478 $order_3->ordernumber, $order_4->ordernumber,
484 my $late_orders = $orders->filter_by_lates;
485 is( $late_orders->count, 3 );
487 $late_orders = $orders->filter_by_lates( { delay => 0 } );
488 is( $late_orders->count, 3 );
490 $late_orders = $orders->filter_by_lates( { delay => 1 } );
491 is( $late_orders->count, 3 );
493 $late_orders = $orders->filter_by_lates( { delay => 3 } );
494 is( $late_orders->count, 2 );
496 $late_orders = $orders->filter_by_lates( { delay => 4 } );
497 is( $late_orders->count, 1 );
499 $late_orders = $orders->filter_by_lates( { delay => 5 } );
500 is( $late_orders->count, 1 );
502 $late_orders = $orders->filter_by_lates( { delay => 6 } );
503 is( $late_orders->count, 0 );
505 $late_orders = $orders->filter_by_lates(
506 { estimated_from => $now->clone->subtract( days => 6 ) } );
507 is( $late_orders->count, 2 );
508 is( $late_orders->next->ordernumber, $order_3->ordernumber );
510 $late_orders = $orders->filter_by_lates(
511 { estimated_from => $now->clone->subtract( days => 5 ) } );
512 is( $late_orders->count, 2 );
513 is( $late_orders->next->ordernumber, $order_3->ordernumber );
515 $late_orders = $orders->filter_by_lates(
516 { estimated_from => $now->clone->subtract( days => 4 ) } );
517 is( $late_orders->count, 2 );
518 is( $late_orders->next->ordernumber, $order_3->ordernumber );
520 $late_orders = $orders->filter_by_lates(
521 { estimated_from => $now->clone->subtract( days => 3 ) } );
522 is( $late_orders->count, 2 );
524 $late_orders = $orders->filter_by_lates(
525 { estimated_from => $now->clone->subtract( days => 1 ) } );
526 is( $late_orders->count, 1 );
528 $late_orders = $orders->filter_by_lates(
530 estimated_from => $now->clone->subtract( days => 4 ),
531 estimated_to => $now->clone->subtract( days => 3 )
534 is( $late_orders->count, 1 );
536 $schema->storage->txn_rollback;