Bug 30432: Unit test
[koha-ffzg.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 => 7;
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     t::lib::Mocks::mock_userenv( { patron => $patron_1 } );
436
437     my $reserve_id_1 = C4::Reserves::AddReserve(
438         {
439             borrowernumber => $patron_1->borrowernumber,
440             biblionumber   => $biblio->biblionumber,
441             priority       => 1,
442             itemnumber     => $item_1->itemnumber,
443         }
444     );
445
446     my $holds = Koha::Holds->search({ reserve_id => $reserve_id_1 });
447     my $items = $holds->get_items_that_can_fill;
448     is_deeply( [ map { $_->itemnumber } $items->as_list ], [ $item_1->itemnumber ], 'Item level hold can only be filled by the specific item');
449
450     my $reserve_id_2 = C4::Reserves::AddReserve(
451         {
452             borrowernumber => $patron_2->borrowernumber,
453             biblionumber   => $biblio->biblionumber,
454             priority       => 2,
455             branchcode     => $item_1->homebranch,
456         }
457     );
458
459     my $waiting_reserve_id = C4::Reserves::AddReserve(
460         {
461             borrowernumber => $patron_2->borrowernumber,
462             biblionumber   => $biblio->biblionumber,
463             priority       => 0,
464             found          => 'W',
465             itemnumber     => $item_1->itemnumber,
466         }
467     );
468
469     # item 3 is on loan
470     AddIssue( $patron_3->unblessed, $item_3->barcode );
471
472     # item 4 is in transfer
473     my $from = $builder->build_object( { class => 'Koha::Libraries' } );
474     my $to   = $builder->build_object( { class => 'Koha::Libraries' } );
475     Koha::Item::Transfer->new(
476         {
477             itemnumber  => $item_4->itemnumber,
478             datearrived => undef,
479             frombranch  => $from->branchcode,
480             tobranch    => $to->branchcode
481         }
482     )->store;
483
484     $holds = Koha::Holds->search(
485         {
486             reserve_id => [ $reserve_id_1, $reserve_id_2, $waiting_reserve_id, ]
487         }
488     );
489
490     $items = $holds->get_items_that_can_fill;
491     is_deeply( [ map { $_->itemnumber } $items->as_list ],
492         [ $item_2->itemnumber, $item_5->itemnumber ], 'Only item 2 and 5 are available for filling the hold' );
493
494     # Marking item_5 is no hold allowed
495     Koha::CirculationRule->new(
496         {
497             rule_name  => 'holdallowed',
498             rule_value => 'not_allowed',
499             itemtype   => $item_5->itype
500         }
501     )->store;
502     $items = $holds->get_items_that_can_fill;
503     is_deeply( [ map { $_->itemnumber } $items->as_list ],
504         [ $item_2->itemnumber ], 'Only item 2 is available for filling the hold' );
505
506
507     my $noloan_itype = $builder->build_object( { class => 'Koha::ItemTypes', value => { notforloan => 1 } } );
508     t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
509     Koha::Holds->find( $waiting_reserve_id )->delete;
510     $holds = Koha::Holds->search(
511         {
512             reserve_id => [ $reserve_id_1, $reserve_id_2 ]
513         }
514     );
515     $items = $holds->get_items_that_can_fill;
516     is_deeply( [ map { $_->itemnumber } $items->as_list ],
517         [ $item_1->itemnumber, $item_2->itemnumber, $item_5->itemnumber ], 'Items 1, 2, and 5 are available for filling the holds' );
518
519     my $no_holds = Koha::Holds->new->empty();
520     my $no_items = $no_holds->get_items_that_can_fill();
521     is( ref $no_items, "Koha::Items", "Routine returns a Koha::Items object");
522     is( $no_items->count, 0, "Object is empty when called on no holds");
523
524 };
525
526 subtest 'set_waiting+patron_expiration_date' => sub {
527     plan tests => 2;
528     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
529
530     my $item =
531       $builder->build_sample_item( { library => $library->branchcode } );
532     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
533     t::lib::Mocks::mock_userenv(
534         { patron => $manager, branchcode => $manager->branchcode } );
535
536     my $patron = $builder->build_object(
537         {
538             class => 'Koha::Patrons',
539             value => { branchcode => $library->branchcode, }
540         }
541     );
542
543     subtest 'patron_expiration_date < expiration_date' => sub {
544         plan tests => 6;
545         t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 5 );
546         my $patron_expiration_date = dt_from_string->add( days => 3 )->ymd;
547         my $reserve_id             = C4::Reserves::AddReserve(
548             {
549                 branchcode      => $library->branchcode,
550                 borrowernumber  => $patron->borrowernumber,
551                 biblionumber    => $item->biblionumber,
552                 priority        => 1,
553                 itemnumber      => $item->itemnumber,
554                 expiration_date => $patron_expiration_date,
555             }
556         );
557
558         my $hold = Koha::Holds->find($reserve_id);
559
560         is(
561             $hold->expirationdate,
562             $patron_expiration_date,
563             'expiration date set to patron expiration date'
564         );
565         is(
566             $hold->patron_expiration_date, $patron_expiration_date,
567             'patron expiration date correctly set'
568         );
569
570         $hold->set_waiting;
571
572         $hold = $hold->get_from_storage;
573         is( $hold->expirationdate,         $patron_expiration_date );
574         is( $hold->patron_expiration_date, $patron_expiration_date );
575
576         C4::Reserves::RevertWaitingStatus(
577             { itemnumber => $item->itemnumber }
578         );
579
580         $hold = $hold->get_from_storage;
581         is( $hold->expirationdate,         $patron_expiration_date );
582         is( $hold->patron_expiration_date, $patron_expiration_date );
583     };
584
585     subtest 'patron_expiration_date > expiration_date' => sub {
586         plan tests => 6;
587         t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 5 );
588         my $new_expiration_date = dt_from_string->add( days => 5 )->ymd;
589         my $patron_expiration_date = dt_from_string->add( days => 6 )->ymd;
590         my $reserve_id             = C4::Reserves::AddReserve(
591             {
592                 branchcode      => $library->branchcode,
593                 borrowernumber  => $patron->borrowernumber,
594                 biblionumber    => $item->biblionumber,
595                 priority        => 1,
596                 itemnumber      => $item->itemnumber,
597                 expiration_date => $patron_expiration_date,
598             }
599         );
600
601         my $hold = Koha::Holds->find($reserve_id);
602
603         is(
604             $hold->expirationdate,
605             $patron_expiration_date,
606             'expiration date set to patron expiration date'
607         );
608         is(
609             $hold->patron_expiration_date, $patron_expiration_date,
610             'patron expiration date correctly set'
611         );
612
613         $hold->set_waiting;
614
615         $hold = $hold->get_from_storage;
616         is( $hold->expirationdate,         $new_expiration_date );
617         is( $hold->patron_expiration_date, $patron_expiration_date );
618
619         C4::Reserves::RevertWaitingStatus(
620             { itemnumber => $item->itemnumber }
621         );
622
623         $hold = $hold->get_from_storage;
624         is( $hold->expirationdate,         $patron_expiration_date );
625         is( $hold->patron_expiration_date, $patron_expiration_date );
626     };
627 };
628
629
630 $schema->storage->txn_rollback;
631
632 1;