Bug 22456: (QA follow-up) Consider cancellation requested as cancelled in OPAC
[koha.git] / t / db_dependent / Koha / Holds.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 8;
23 use Test::Warn;
24
25 use C4::Circulation qw( AddIssue );
26 use C4::Reserves qw( AddReserve ModReserve ModReserveCancelAll );
27 use Koha::AuthorisedValueCategory;
28 use Koha::Database;
29 use Koha::DateUtils qw( dt_from_string );
30 use Koha::Holds;
31
32 use t::lib::Mocks;
33 use t::lib::TestBuilder;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $builder = t::lib::TestBuilder->new;
39
40 subtest 'DB constraints' => sub {
41     plan tests => 1;
42
43     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
44     my $item = $builder->build_sample_item;
45     my $hold_info = {
46         branchcode     => $patron->branchcode,
47         borrowernumber => $patron->borrowernumber,
48         biblionumber   => $item->biblionumber,
49         priority       => 1,
50         title          => "title for fee",
51         itemnumber     => $item->itemnumber,
52     };
53
54     my $reserve_id = C4::Reserves::AddReserve($hold_info);
55     my $hold = Koha::Holds->find( $reserve_id );
56
57     warning_like {
58         eval { $hold->priority(undef)->store }
59     }
60     qr{.*DBD::mysql::st execute failed: Column 'priority' cannot be null.*},
61       'DBD should have raised an error about priority that cannot be null';
62 };
63
64 subtest 'cancel' => sub {
65     plan tests => 12;
66     my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
67     my $library    = $builder->build_object( { class => 'Koha::Libraries' } );
68     my $itemtype   = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
69     my $item_info  = {
70         biblionumber     => $biblioitem->biblionumber,
71         biblioitemnumber => $biblioitem->biblioitemnumber,
72         homebranch       => $library->branchcode,
73         holdingbranch    => $library->branchcode,
74         itype            => $itemtype->itemtype,
75     };
76     my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
77     my $manager = $builder->build_object({ class => "Koha::Patrons" });
78     t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
79
80     my ( @patrons, @holds );
81     for my $i ( 0 .. 2 ) {
82         my $priority = $i + 1;
83         my $patron   = $builder->build_object(
84             {
85                 class => 'Koha::Patrons',
86                 value => { branchcode => $library->branchcode, }
87             }
88         );
89         my $reserve_id = C4::Reserves::AddReserve(
90             {
91                 branchcode     => $library->branchcode,
92                 borrowernumber => $patron->borrowernumber,
93                 biblionumber   => $item->biblionumber,
94                 priority       => $priority,
95                 title          => "title for fee",
96                 itemnumber     => $item->itemnumber,
97             }
98         );
99         my $hold = Koha::Holds->find($reserve_id);
100         push @patrons, $patron;
101         push @holds,   $hold;
102     }
103
104     # There are 3 holds on this records
105     my $nb_of_holds =
106       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
107     is( $nb_of_holds, 3,
108         'There should have 3 holds placed on this biblio record' );
109     my $first_hold  = $holds[0];
110     my $second_hold = $holds[1];
111     my $third_hold  = $holds[2];
112     is( ref($second_hold), 'Koha::Hold',
113         'We should play with Koha::Hold objects' );
114     is( $second_hold->priority, 2,
115         'Second hold should have a priority set to 3' );
116
117     # Remove the second hold, only 2 should still exist in DB and priorities must have been updated
118     my $is_cancelled = $second_hold->cancel;
119     is( ref($is_cancelled), 'Koha::Hold',
120         'Koha::Hold->cancel should return the Koha::Hold (?)' )
121       ;    # This is can reconsidered
122     is( $second_hold->in_storage, 0,
123         'The hold has been cancelled and does not longer exist in DB' );
124     $nb_of_holds =
125       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
126     is( $nb_of_holds, 2,
127         'a hold has been cancelled, there should have only 2 holds placed on this biblio record'
128     );
129
130     # discard_changes to refetch
131     is( $first_hold->discard_changes->priority, 1, 'First hold should still be first' );
132     is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
133
134     subtest 'charge_cancel_fee parameter' => sub {
135         plan tests => 4;
136         my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } );
137         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } });
138         is( $patron->account->balance, 0, 'A new patron does not have any charges' );
139
140         my $hold_info = {
141             branchcode     => $library->branchcode,
142             borrowernumber => $patron->borrowernumber,
143             biblionumber   => $item->biblionumber,
144             priority       => 1,
145             title          => "title for fee",
146             itemnumber     => $item->itemnumber,
147         };
148
149         # First, test cancelling a reserve when there's no charge configured.
150         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
151         my $reserve_id = C4::Reserves::AddReserve( $hold_info );
152         Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
153         is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' );
154
155         # Then, test cancelling a reserve when there's no charge desired.
156         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
157         $reserve_id = C4::Reserves::AddReserve( $hold_info );
158         Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0
159         is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' );
160
161
162         # Finally, test cancelling a reserve when there's a charge desired and configured.
163         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
164         $reserve_id = C4::Reserves::AddReserve( $hold_info );
165         Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
166         is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' );
167     };
168
169     subtest 'waiting hold' => sub {
170         plan tests => 1;
171         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
172         my $reserve_id = C4::Reserves::AddReserve(
173             {
174                 branchcode     => $library->branchcode,
175                 borrowernumber => $patron->borrowernumber,
176                 biblionumber   => $item->biblionumber,
177                 priority       => 1,
178                 title          => "title for fee",
179                 itemnumber     => $item->itemnumber,
180                 found          => 'W',
181             }
182         );
183         Koha::Holds->find( $reserve_id )->cancel;
184         my $hold_old = Koha::Old::Holds->find( $reserve_id );
185         is( $hold_old->found, 'W', 'The found column should have been kept and a hold is cancelled' );
186     };
187
188     subtest 'HoldsLog' => sub {
189         plan tests => 2;
190         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
191         my $hold_info = {
192             branchcode     => $library->branchcode,
193             borrowernumber => $patron->borrowernumber,
194             biblionumber   => $item->biblionumber,
195             priority       => 1,
196             title          => "title for fee",
197             itemnumber     => $item->itemnumber,
198         };
199
200         t::lib::Mocks::mock_preference('HoldsLog', 0);
201         my $reserve_id = C4::Reserves::AddReserve($hold_info);
202         Koha::Holds->find( $reserve_id )->cancel;
203         my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
204         is( $number_of_logs, 0, 'Without HoldsLog, Koha::Hold->cancel should not have logged' );
205
206         t::lib::Mocks::mock_preference('HoldsLog', 1);
207         $reserve_id = C4::Reserves::AddReserve($hold_info);
208         Koha::Holds->find( $reserve_id )->cancel;
209         $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
210         is( $number_of_logs, 1, 'With HoldsLog, Koha::Hold->cancel should have logged' );
211     };
212
213     subtest 'rollback' => sub {
214         plan tests => 3;
215         my $patron_category = $builder->build_object(
216             {
217                 class => 'Koha::Patron::Categories',
218                 value => { reservefee => 0 }
219             }
220         );
221         my $patron = $builder->build_object(
222             {
223                 class => 'Koha::Patrons',
224                 value => { categorycode => $patron_category->categorycode }
225             }
226         );
227         my $hold_info = {
228             branchcode     => $library->branchcode,
229             borrowernumber => $patron->borrowernumber,
230             biblionumber   => $item->biblionumber,
231             priority       => 1,
232             title          => "title for fee",
233             itemnumber     => $item->itemnumber,
234         };
235
236         t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',42 );
237         my $reserve_id = C4::Reserves::AddReserve($hold_info);
238         my $hold       = Koha::Holds->find($reserve_id);
239
240         # Add a row with the same id to make the cancel fails
241         Koha::Old::Hold->new( $hold->unblessed )->store;
242
243         warning_like {
244             eval { $hold->cancel( { charge_cancel_fee => 1 } ) };
245         }
246         qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
247           'DBD should have raised an error about dup primary key';
248
249         $hold = Koha::Holds->find($reserve_id);
250         is( ref($hold), 'Koha::Hold', 'The hold should not have been deleted' );
251         is( $patron->account->balance, 0,
252 'If the hold has not been cancelled, the patron should not have been charged'
253         );
254     };
255
256 };
257
258 subtest 'cancel with reason' => sub {
259     plan tests => 7;
260     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
261     my $item = $builder->build_sample_item({ library => $library->branchcode });
262     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
263     t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
264
265     my $patron = $builder->build_object(
266         {
267             class => 'Koha::Patrons',
268             value => { branchcode => $library->branchcode, }
269         }
270     );
271
272     my $reserve_id = C4::Reserves::AddReserve(
273         {
274             branchcode     => $library->branchcode,
275             borrowernumber => $patron->borrowernumber,
276             biblionumber   => $item->biblionumber,
277             priority       => 1,
278             itemnumber     => $item->itemnumber,
279         }
280     );
281
282     my $hold = Koha::Holds->find($reserve_id);
283
284     ok($reserve_id, "Hold created");
285     ok($hold, "Hold found");
286
287     my $av = Koha::AuthorisedValue->new( { category => 'HOLD_CANCELLATION', authorised_value => 'TEST_REASON' } )->store;
288     Koha::Notice::Templates->search({ code => 'HOLD_CANCELLATION'})->delete();
289     my $notice = Koha::Notice::Template->new({
290         name                   => 'Hold cancellation',
291         module                 => 'reserves',
292         code                   => 'HOLD_CANCELLATION',
293         title                  => 'Hold cancelled',
294         content                => 'Your hold was cancelled.',
295         message_transport_type => 'email',
296         branchcode             => q{},
297     })->store();
298
299     $hold->cancel({cancellation_reason => 'TEST_REASON'});
300
301     $hold = Koha::Holds->find($reserve_id);
302     is( $hold, undef, 'Hold is not in the reserves table');
303     $hold = Koha::Old::Holds->find($reserve_id);
304     ok( $hold, 'Hold was found in the old reserves table');
305
306     my $message = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'HOLD_CANCELLATION'});
307     ok( $message, 'Found hold cancellation message');
308     is( $message->subject, 'Hold cancelled', 'Message has correct title' );
309     is( $message->content, 'Your hold was cancelled.', 'Message has correct content');
310
311     $notice->delete;
312     $av->delete;
313     $message->delete;
314 };
315
316 subtest 'cancel all with reason' => sub {
317     plan tests => 7;
318     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
319     my $item = $builder->build_sample_item({ library => $library->branchcode });
320     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
321     t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
322
323     my $patron = $builder->build_object(
324         {
325             class => 'Koha::Patrons',
326             value => { branchcode => $library->branchcode, }
327         }
328     );
329
330     my $reserve_id = C4::Reserves::AddReserve(
331         {
332             branchcode     => $library->branchcode,
333             borrowernumber => $patron->borrowernumber,
334             biblionumber   => $item->biblionumber,
335             priority       => 1,
336             itemnumber     => $item->itemnumber,
337         }
338     );
339
340     my $hold = Koha::Holds->find($reserve_id);
341
342     ok($reserve_id, "Hold created");
343     ok($hold, "Hold found");
344
345     my $av = Koha::AuthorisedValue->new( { category => 'HOLD_CANCELLATION', authorised_value => 'TEST_REASON' } )->store;
346     Koha::Notice::Templates->search({ code => 'HOLD_CANCELLATION'})->delete();
347     my $notice = Koha::Notice::Template->new({
348         name                   => 'Hold cancellation',
349         module                 => 'reserves',
350         code                   => 'HOLD_CANCELLATION',
351         title                  => 'Hold cancelled',
352         content                => 'Your hold was cancelled.',
353         message_transport_type => 'email',
354         branchcode             => q{},
355     })->store();
356
357     ModReserveCancelAll($item->id, $patron->id, 'TEST_REASON');
358
359     $hold = Koha::Holds->find($reserve_id);
360     is( $hold, undef, 'Hold is not in the reserves table');
361     $hold = Koha::Old::Holds->find($reserve_id);
362     ok( $hold, 'Hold was found in the old reserves table');
363
364     my $message = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'HOLD_CANCELLATION'});
365     ok( $message, 'Found hold cancellation message');
366     is( $message->subject, 'Hold cancelled', 'Message has correct title' );
367     is( $message->content, 'Your hold was cancelled.', 'Message has correct content');
368
369     $av->delete;
370     $message->delete;
371 };
372
373 subtest 'Desks' => sub {
374     plan tests => 5;
375     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
376
377     my $desk = Koha::Desk->new({
378         desk_name  => 'my_desk_name_for_test',
379         branchcode => $library->branchcode ,
380                                })->store;
381     ok($desk, "Desk created");
382     my $item = $builder->build_sample_item({ library => $library->branchcode });
383     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
384     t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
385
386     my $patron = $builder->build_object(
387         {
388             class => 'Koha::Patrons',
389             value => { branchcode => $library->branchcode, }
390         }
391         );
392
393     my $reserve_id = C4::Reserves::AddReserve(
394         {
395             branchcode     => $library->branchcode,
396             borrowernumber => $patron->borrowernumber,
397             biblionumber   => $item->biblionumber,
398             priority       => 1,
399             itemnumber     => $item->itemnumber,
400         }
401     );
402
403     my $hold = Koha::Holds->find($reserve_id);
404
405     ok($reserve_id, "Hold created");
406     ok($hold, "Hold found");
407     $hold->set_waiting($desk->desk_id);
408     is($hold->found, 'W', 'Hold is waiting with correct status set');
409     is($hold->desk_id, $desk->desk_id, 'Hold is attach to its desk');
410
411 };
412
413 subtest 'get_items_that_can_fill' => sub {
414     plan tests => 6;
415
416     my $biblio = $builder->build_sample_biblio;
417     my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' }); # For 1, 2, 3, 4
418     my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
419     my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itype => $itype_1->itemtype } );
420         # waiting
421     my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itype => $itype_1->itemtype } );
422     my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itype => $itype_1->itemtype } )
423       ;    # onloan
424     my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itype => $itype_1->itemtype } )
425       ;    # in transfer
426     my $item_5 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itype => $itype_2->itemtype } );
427     my $lost       = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 1 } );
428     my $withdrawn  = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 1 } );
429     my $notforloan = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
430
431     my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
432     my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
433     my $patron_3 = $builder->build_object( { class => 'Koha::Patrons' } );
434
435     my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
436
437     t::lib::Mocks::mock_userenv( { patron => $patron_1 } );
438
439     my $reserve_id_1 = C4::Reserves::AddReserve(
440         {
441             branchcode     => $library_1->branchcode,
442             borrowernumber => $patron_1->borrowernumber,
443             biblionumber   => $biblio->biblionumber,
444             priority       => 1,
445             itemnumber     => $item_1->itemnumber,
446         }
447     );
448
449     my $holds = Koha::Holds->search({ reserve_id => $reserve_id_1 });
450     my $items = $holds->get_items_that_can_fill;
451     is_deeply( [ map { $_->itemnumber } $items->as_list ], [ $item_1->itemnumber ], 'Item level hold can only be filled by the specific item');
452
453     my $reserve_id_2 = C4::Reserves::AddReserve(
454         {
455             branchcode     => $library_1->branchcode,
456             borrowernumber => $patron_2->borrowernumber,
457             biblionumber   => $biblio->biblionumber,
458             priority       => 2,
459             branchcode     => $item_1->homebranch,
460         }
461     );
462
463     my $waiting_reserve_id = C4::Reserves::AddReserve(
464         {
465             branchcode     => $library_1->branchcode,
466             borrowernumber => $patron_2->borrowernumber,
467             biblionumber   => $biblio->biblionumber,
468             priority       => 0,
469             found          => 'W',
470             itemnumber     => $item_1->itemnumber,
471         }
472     );
473
474     my $notforloan_reserve_id = C4::Reserves::AddReserve(
475         {
476             branchcode     => $library_1->branchcode,
477             borrowernumber => $patron_2->borrowernumber,
478             biblionumber   => $biblio->biblionumber,
479             priority       => 0,
480             itemnumber     => $notforloan->itemnumber,
481         }
482     );
483
484     # item 3 is on loan
485     AddIssue( $patron_3->unblessed, $item_3->barcode );
486
487     # item 4 is in transfer
488     my $from = $builder->build_object( { class => 'Koha::Libraries' } );
489     my $to   = $builder->build_object( { class => 'Koha::Libraries' } );
490     Koha::Item::Transfer->new(
491         {
492             itemnumber  => $item_4->itemnumber,
493             datearrived => undef,
494             frombranch  => $from->branchcode,
495             tobranch    => $to->branchcode
496         }
497     )->store;
498
499     $holds = Koha::Holds->search(
500         {
501             reserve_id => [ $reserve_id_1, $reserve_id_2, $waiting_reserve_id, $notforloan_reserve_id, ]
502         }
503     );
504
505     $items = $holds->get_items_that_can_fill;
506     is_deeply( [ map { $_->itemnumber } $items->as_list ],
507         [ $item_2->itemnumber, $item_5->itemnumber ], 'Only item 2 and 5 are available for filling the hold' );
508
509     # Marking item_5 is no hold allowed
510     Koha::CirculationRule->new(
511         {
512             rule_name  => 'holdallowed',
513             rule_value => 'not_allowed',
514             itemtype   => $item_5->itype
515         }
516     )->store;
517     $items = $holds->get_items_that_can_fill;
518     is_deeply( [ map { $_->itemnumber } $items->as_list ],
519         [ $item_2->itemnumber ], 'Only item 2 is available for filling the hold' );
520
521
522     my $noloan_itype = $builder->build_object( { class => 'Koha::ItemTypes', value => { notforloan => 1 } } );
523     t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
524     Koha::Holds->find( $waiting_reserve_id )->delete;
525     $holds = Koha::Holds->search(
526         {
527             reserve_id => [ $reserve_id_1, $reserve_id_2 ]
528         }
529     );
530     $items = $holds->get_items_that_can_fill;
531     is_deeply( [ sort map { $_->itemnumber } $items->as_list ],
532         [ $item_1->itemnumber, $item_2->itemnumber, $item_5->itemnumber ], 'Items 1, 2, and 5 are available for filling the holds' );
533
534     my $no_holds = Koha::Holds->new->empty();
535     my $no_items = $no_holds->get_items_that_can_fill();
536     is( ref $no_items, "Koha::Items", "Routine returns a Koha::Items object");
537     is( $no_items->count, 0, "Object is empty when called on no holds");
538
539 };
540
541 subtest 'set_waiting+patron_expiration_date' => sub {
542     plan tests => 2;
543     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
544
545     my $item =
546       $builder->build_sample_item( { library => $library->branchcode } );
547     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
548     t::lib::Mocks::mock_userenv(
549         { patron => $manager, branchcode => $manager->branchcode } );
550
551     my $patron = $builder->build_object(
552         {
553             class => 'Koha::Patrons',
554             value => { branchcode => $library->branchcode, }
555         }
556     );
557
558     subtest 'patron_expiration_date < expiration_date' => sub {
559         plan tests => 6;
560         t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 5 );
561         my $patron_expiration_date = dt_from_string->add( days => 3 )->ymd;
562         my $reserve_id             = C4::Reserves::AddReserve(
563             {
564                 branchcode      => $library->branchcode,
565                 borrowernumber  => $patron->borrowernumber,
566                 biblionumber    => $item->biblionumber,
567                 priority        => 1,
568                 itemnumber      => $item->itemnumber,
569                 expiration_date => $patron_expiration_date,
570             }
571         );
572
573         my $hold = Koha::Holds->find($reserve_id);
574
575         is(
576             $hold->expirationdate,
577             $patron_expiration_date,
578             'expiration date set to patron expiration date'
579         );
580         is(
581             $hold->patron_expiration_date, $patron_expiration_date,
582             'patron expiration date correctly set'
583         );
584
585         $hold->set_waiting;
586
587         $hold = $hold->get_from_storage;
588         is( $hold->expirationdate,         $patron_expiration_date );
589         is( $hold->patron_expiration_date, $patron_expiration_date );
590
591         C4::Reserves::RevertWaitingStatus(
592             { itemnumber => $item->itemnumber }
593         );
594
595         $hold = $hold->get_from_storage;
596         is( $hold->expirationdate,         $patron_expiration_date );
597         is( $hold->patron_expiration_date, $patron_expiration_date );
598     };
599
600     subtest 'patron_expiration_date > expiration_date' => sub {
601         plan tests => 6;
602         t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 5 );
603         my $new_expiration_date = dt_from_string->add( days => 5 )->ymd;
604         my $patron_expiration_date = dt_from_string->add( days => 6 )->ymd;
605         my $reserve_id             = C4::Reserves::AddReserve(
606             {
607                 branchcode      => $library->branchcode,
608                 borrowernumber  => $patron->borrowernumber,
609                 biblionumber    => $item->biblionumber,
610                 priority        => 1,
611                 itemnumber      => $item->itemnumber,
612                 expiration_date => $patron_expiration_date,
613             }
614         );
615
616         my $hold = Koha::Holds->find($reserve_id);
617
618         is(
619             $hold->expirationdate,
620             $patron_expiration_date,
621             'expiration date set to patron expiration date'
622         );
623         is(
624             $hold->patron_expiration_date, $patron_expiration_date,
625             'patron expiration date correctly set'
626         );
627
628         $hold->set_waiting;
629
630         $hold = $hold->get_from_storage;
631         is( $hold->expirationdate,         $new_expiration_date );
632         is( $hold->patron_expiration_date, $patron_expiration_date );
633
634         C4::Reserves::RevertWaitingStatus(
635             { itemnumber => $item->itemnumber }
636         );
637
638         $hold = $hold->get_from_storage;
639         is( $hold->expirationdate,         $patron_expiration_date );
640         is( $hold->patron_expiration_date, $patron_expiration_date );
641     };
642 };
643
644
645 $schema->storage->txn_rollback;
646
647 subtest 'filter_by_has_cancellation_requests() and filter_out_has_cancellation_requests() tests' => sub {
648
649     plan tests => 7;
650
651     $schema->storage->txn_begin;
652
653     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
654
655     my $item_1 = $builder->build_sample_item;
656     my $item_2 = $builder->build_sample_item;
657     my $item_3 = $builder->build_sample_item;
658
659     my $hold_1 = $builder->build_object(
660         {
661             class => 'Koha::Holds',
662             value => {
663                 found          => 'W',
664                 itemnumber     => $item_1->id,
665                 biblionumber   => $item_1->biblionumber,
666                 borrowernumber => $patron->id
667             }
668         }
669     );
670     my $hold_2 = $builder->build_object(
671         {
672             class => 'Koha::Holds',
673             value => {
674                 found          => 'W',
675                 itemnumber     => $item_2->id,
676                 biblionumber   => $item_2->biblionumber,
677                 borrowernumber => $patron->id
678             }
679         }
680     );
681     my $hold_3 = $builder->build_object(
682         {
683             class => 'Koha::Holds',
684             value => {
685                 found          => 'W',
686                 itemnumber     => $item_3->id,
687                 biblionumber   => $item_3->biblionumber,
688                 borrowernumber => $patron->id
689             }
690         }
691     );
692
693     my $rs = Koha::Holds->search(
694         { reserve_id => [ $hold_1->id, $hold_2->id, $hold_3->id ] } );
695
696     is( $rs->count, 3 );
697
698     my $filtered_rs = $rs->filter_by_has_cancellation_requests;
699
700     is( $filtered_rs->count, 0 );
701
702     my $filtered_out_rs = $rs->filter_out_has_cancellation_requests;
703
704     is( $filtered_out_rs->count, 3 );
705
706     $hold_2->add_cancellation_request;
707
708     $filtered_rs = $rs->filter_by_has_cancellation_requests;
709
710     is( $filtered_rs->count,    1 );
711     is( $filtered_rs->next->id, $hold_2->id );
712
713     $filtered_out_rs = $rs->filter_out_has_cancellation_requests;
714
715     is( $filtered_out_rs->count,    2 );
716     is( $filtered_out_rs->next->id, $hold_1->id );
717
718     $schema->storage->txn_rollback;
719 };