Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
use Test::More tests => 2; |
5 |
|
6 |
use t::lib::Mocks; |
7 |
use t::lib::TestBuilder; |
8 |
|
9 |
use C4::Members; |
10 |
use C4::Reserves; |
11 |
use Koha::Database; |
12 |
use Koha::DateUtils; |
13 |
use Koha::Holds; |
14 |
|
15 |
my $schema = Koha::Database->new->schema; |
16 |
$schema->storage->txn_begin; |
17 |
|
18 |
subtest 'CancelExpiredReserves tests incl. holidays' => sub { |
19 |
plan tests => 4; |
20 |
|
21 |
my $builder = t::lib::TestBuilder->new(); |
22 |
|
23 |
t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0); |
24 |
|
25 |
my $today = dt_from_string(); |
26 |
my $reserve_reservedate = $today->clone; |
27 |
$reserve_reservedate->subtract(days => 30); |
28 |
|
29 |
my $reserve1_expirationdate = $today->clone; |
30 |
$reserve1_expirationdate->add(days => 1); |
31 |
|
32 |
# Reserve not expired |
33 |
my $reserve1 = $builder->build({ |
34 |
source => 'Reserve', |
35 |
value => { |
36 |
reservedate => $reserve_reservedate, |
37 |
expirationdate => $reserve1_expirationdate, |
38 |
cancellationdate => undef, |
39 |
priority => 0, |
40 |
found => 'W', |
41 |
}, |
42 |
}); |
43 |
|
44 |
CancelExpiredReserves(); |
45 |
my $r1 = Koha::Holds->find($reserve1->{reserve_id}); |
46 |
ok($r1, 'Reserve 1 should not be canceled.'); |
47 |
|
48 |
my $reserve2_expirationdate = $today->clone; |
49 |
$reserve2_expirationdate->subtract(days => 1); |
50 |
|
51 |
# Reserve expired |
52 |
my $reserve2 = $builder->build({ |
53 |
source => 'Reserve', |
54 |
value => { |
55 |
reservedate => $reserve_reservedate, |
56 |
expirationdate => $reserve2_expirationdate, |
57 |
cancellationdate => undef, |
58 |
priority => 0, |
59 |
found => 'W', |
60 |
}, |
61 |
}); |
62 |
|
63 |
CancelExpiredReserves(); |
64 |
my $r2 = Koha::Holds->find($reserve2->{reserve_id}); |
65 |
is($r2, undef,'reserve 2 should be canceled.'); |
66 |
|
67 |
# Reserve expired on holiday |
68 |
my $reserve3 = $builder->build({ |
69 |
source => 'Reserve', |
70 |
value => { |
71 |
reservedate => $reserve_reservedate, |
72 |
expirationdate => $reserve2_expirationdate, |
73 |
branchcode => 'LIB1', |
74 |
cancellationdate => undef, |
75 |
priority => 0, |
76 |
found => 'W', |
77 |
}, |
78 |
}); |
79 |
|
80 |
Koha::Caches->get_instance()->flush_all(); |
81 |
my $holiday = $builder->build({ |
82 |
source => 'SpecialHoliday', |
83 |
value => { |
84 |
branchcode => 'LIB1', |
85 |
day => $today->day, |
86 |
month => $today->month, |
87 |
year => $today->year, |
88 |
title => 'My holiday', |
89 |
isexception => 0 |
90 |
}, |
91 |
}); |
92 |
|
93 |
CancelExpiredReserves(); |
94 |
my $r3 = Koha::Holds->find($reserve3->{reserve_id}); |
95 |
ok($r3,'Reserve 3 should not be canceled.'); |
96 |
|
97 |
t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1); |
98 |
CancelExpiredReserves(); |
99 |
$r3 = Koha::Holds->find($reserve3->{reserve_id}); |
100 |
is($r3, undef,'Reserve 3 should be canceled.'); |
101 |
}; |
102 |
|
103 |
subtest 'Test handling of waiting reserves by CancelExpiredReserves' => sub { |
104 |
plan tests => 2; |
105 |
|
106 |
Koha::Holds->delete; |
107 |
|
108 |
my $builder = t::lib::TestBuilder->new(); |
109 |
my $category = $builder->build({ source => 'Category' }); |
110 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
111 |
my $biblio = $builder->build({ source => 'Biblio' }); |
112 |
my $bibnum = $biblio->{biblionumber}; |
113 |
my $item = $builder->build({ source => 'Item', value => { biblionumber => $bibnum }}); |
114 |
my $itemnumber = $item->{itemnumber}; |
115 |
my $borrowernumber = $builder->build({ source => 'Borrower', value => { categorycode => $category->{categorycode}, branchcode => $branchcode }})->{borrowernumber}; |
116 |
|
117 |
my $resdate = dt_from_string->add( days => -20 ); |
118 |
my $expdate = dt_from_string->add( days => -2 ); |
119 |
my $notexpdate = dt_from_string->add( days => 2 ); |
120 |
|
121 |
my $hold1 = Koha::Hold->new({ |
122 |
branchcode => $branchcode, |
123 |
borrowernumber => $borrowernumber, |
124 |
biblionumber => $bibnum, |
125 |
priority => 1, |
126 |
reservedate => $resdate, |
127 |
expirationdate => $notexpdate, |
128 |
found => undef, |
129 |
})->store; |
130 |
|
131 |
my $hold2 = Koha::Hold->new({ |
132 |
branchcode => $branchcode, |
133 |
borrowernumber => $borrowernumber, |
134 |
biblionumber => $bibnum, |
135 |
priority => 2, |
136 |
reservedate => $resdate, |
137 |
expirationdate => $expdate, |
138 |
found => undef, |
139 |
})->store; |
140 |
|
141 |
my $hold3 = Koha::Hold->new({ |
142 |
branchcode => $branchcode, |
143 |
borrowernumber => $borrowernumber, |
144 |
biblionumber => $bibnum, |
145 |
itemnumber => $itemnumber, |
146 |
priority => 0, |
147 |
reservedate => $resdate, |
148 |
expirationdate => $expdate, |
149 |
found => 'W', |
150 |
})->store; |
151 |
|
152 |
t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 ); |
153 |
CancelExpiredReserves(); |
154 |
my $count1 = Koha::Holds->search->count; |
155 |
is( $count1, 2, 'Only the non-waiting expired holds should be cancelled'); |
156 |
|
157 |
t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); |
158 |
CancelExpiredReserves(); |
159 |
my $count2 = Koha::Holds->search->count; |
160 |
is( $count2, 1, 'Also the waiting expired hold should be cancelled now'); |
161 |
}; |
162 |
|
163 |
$schema->storage->txn_rollback; |