4 use Test::More tests => 4;
7 use t::lib::TestBuilder;
10 use C4::Reserves qw( CancelExpiredReserves );
12 use Koha::DateUtils qw( dt_from_string );
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
18 subtest 'CancelExpiredReserves tests incl. holidays' => sub {
21 my $builder = t::lib::TestBuilder->new();
23 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
24 # Waiting holds could be cancelled only if ExpireReservesMaxPickUpDelay is set to "allow", see bug 19260
25 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
28 my $today = dt_from_string();
29 my $reserve_reservedate = $today->clone;
30 $reserve_reservedate->subtract(days => 30);
32 my $reserve1_expirationdate = $today->clone;
33 $reserve1_expirationdate->add(days => 1);
36 my $reserve1 = $builder->build({
39 reservedate => $reserve_reservedate,
40 patron_expiration_date => $reserve1_expirationdate,
41 cancellationdate => undef,
47 CancelExpiredReserves();
48 my $r1 = Koha::Holds->find($reserve1->{reserve_id});
49 ok($r1, 'Reserve 1 should not be canceled.');
51 my $reserve2_expirationdate = $today->clone;
52 $reserve2_expirationdate->subtract(days => 1);
55 my $reserve2 = $builder->build({
58 reservedate => $reserve_reservedate,
59 patron_expiration_date => $reserve2_expirationdate,
60 cancellationdate => undef,
66 CancelExpiredReserves();
67 my $r2 = Koha::Holds->find($reserve2->{reserve_id});
68 is($r2, undef,'reserve 2 should be canceled.');
70 # Reserve expired on holiday
71 my $reserve3 = $builder->build({
74 reservedate => $reserve_reservedate,
75 patron_expiration_date => $reserve2_expirationdate,
77 cancellationdate => undef,
83 Koha::Caches->get_instance()->flush_all();
84 my $holiday = $builder->build({
85 source => 'SpecialHoliday',
89 month => $today->month,
91 title => 'My holiday',
96 CancelExpiredReserves();
97 my $r3 = Koha::Holds->find($reserve3->{reserve_id});
98 ok($r3,'Reserve 3 should not be canceled.');
100 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
101 CancelExpiredReserves();
102 $r3 = Koha::Holds->find($reserve3->{reserve_id});
103 is($r3, undef,'Reserve 3 should be canceled.');
106 subtest 'Test handling of waiting reserves by CancelExpiredReserves' => sub {
111 my $builder = t::lib::TestBuilder->new();
112 my $category = $builder->build({ source => 'Category' });
113 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
114 my $item = $builder->build_sample_item;
115 my $itemnumber = $item->itemnumber;
116 my $borrowernumber = $builder->build({ source => 'Borrower', value => { categorycode => $category->{categorycode}, branchcode => $branchcode }})->{borrowernumber};
118 my $resdate = dt_from_string->add( days => -20 );
119 my $expdate = dt_from_string->add( days => -2 );
120 my $notexpdate = dt_from_string->add( days => 2 );
122 my $hold1 = Koha::Hold->new({
123 branchcode => $branchcode,
124 borrowernumber => $borrowernumber,
125 biblionumber => $item->biblionumber,
127 reservedate => $resdate,
128 expirationdate => $notexpdate,
132 my $hold2 = Koha::Hold->new({
133 branchcode => $branchcode,
134 borrowernumber => $borrowernumber,
135 biblionumber => $item->biblionumber,
137 reservedate => $resdate,
138 expirationdate => $expdate,
142 my $hold3 = Koha::Hold->new({
143 branchcode => $branchcode,
144 borrowernumber => $borrowernumber,
145 biblionumber => $item->biblionumber,
146 itemnumber => $itemnumber,
148 reservedate => $resdate,
149 expirationdate => $expdate,
153 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 );
154 CancelExpiredReserves();
155 my $count1 = Koha::Holds->search->count;
156 is( $count1, 2, 'Only the non-waiting expired holds should be cancelled');
158 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
159 CancelExpiredReserves();
160 my $count2 = Koha::Holds->search->count;
161 is( $count2, 1, 'Also the waiting expired hold should be cancelled now');
165 subtest 'Test handling of in transit reserves by CancelExpiredReserves' => sub {
168 my $builder = t::lib::TestBuilder->new();
170 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
171 my $expdate = dt_from_string->add( days => -2 );
172 my $reserve = $builder->build({
175 patron_expiration_date => '2018-01-01',
177 cancellationdate => undef,
179 suspend_until => undef
182 my $count = Koha::Holds->search->count;
183 CancelExpiredReserves();
184 is(Koha::Holds->search->count, $count-1, "Transit hold is cancelled if ExpireReservesMaxPickUpDelay set");
186 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 );
187 my $reserve2 = $builder->build({
190 patron_expiration_date => '2018-01-01',
192 cancellationdate => undef,
194 suspend_until => undef
197 CancelExpiredReserves();
198 is(Koha::Holds->search->count, $count-1, "Transit hold is cancelled if ExpireReservesMaxPickUpDelay unset");
202 subtest 'Test handling of cancellation reason if passed' => sub {
205 my $builder = t::lib::TestBuilder->new();
207 my $expdate = dt_from_string->add( days => -2 );
208 my $reserve = $builder->build({
211 patron_expiration_date => '2018-01-01',
213 cancellationdate => undef,
215 suspend_until => undef
218 my $reserve_id = $reserve->{reserve_id};
219 my $count = Koha::Holds->search->count;
220 CancelExpiredReserves("EXPIRED");
221 is(Koha::Holds->search->count, $count-1, "Hold is cancelled when reason is passed");
222 my $old_reserve = Koha::Old::Holds->find($reserve_id);
223 is($old_reserve->cancellation_reason, 'EXPIRED', "Hold cancellation_reason was set correctly");
226 $schema->storage->txn_rollback;