Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Test::More tests => 3; |
6 |
|
7 |
use t::lib::Mocks; |
8 |
use t::lib::TestBuilder; |
9 |
|
10 |
use MARC::Record; |
11 |
|
12 |
use C4::Context; |
13 |
use C4::Biblio; |
14 |
use C4::Items; |
15 |
use Koha::Database; |
16 |
use Koha::Holds; |
17 |
|
18 |
BEGIN { |
19 |
use FindBin; |
20 |
use lib $FindBin::Bin; |
21 |
use_ok('C4::Reserves'); |
22 |
} |
23 |
|
24 |
my $schema = Koha::Database->new->schema; |
25 |
$schema->storage->txn_begin; |
26 |
|
27 |
my $builder = t::lib::TestBuilder->new(); |
28 |
my $dbh = C4::Context->dbh; |
29 |
|
30 |
# Create two random branches |
31 |
my $library_1 = $builder->build({ source => 'Branch' })->{ branchcode }; |
32 |
my $library_2 = $builder->build({ source => 'Branch' })->{ branchcode }; |
33 |
|
34 |
my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' }); |
35 |
my $biblionumber = $biblio->id; |
36 |
|
37 |
# Create item instance for testing. |
38 |
my $itemnumber = $builder->build_sample_item({ library => $library_1, biblionumber => $biblio->biblionumber })->itemnumber; |
39 |
|
40 |
my $patron_1 = $builder->build( { source => 'Borrower' } ); |
41 |
my $patron_2 = $builder->build( { source => 'Borrower' } ); |
42 |
my $patron_3 = $builder->build( { source => 'Borrower' } ); |
43 |
|
44 |
subtest 'Test automatically canceled expired waiting holds to fill the next hold, without a transfer' => sub { |
45 |
plan tests => 8; |
46 |
|
47 |
$dbh->do('DELETE FROM reserves'); |
48 |
$dbh->do('DELETE FROM message_queue'); |
49 |
|
50 |
# Add a hold on the item for each of our patrons |
51 |
my $hold_1 = Koha::Hold->new( |
52 |
{ |
53 |
priority => 0, |
54 |
borrowernumber => $patron_1->{borrowernumber}, |
55 |
branchcode => $library_1, |
56 |
biblionumber => $biblionumber, |
57 |
itemnumber => $itemnumber, |
58 |
found => 'W', |
59 |
reservedate => '1900-01-01', |
60 |
waitingdate => '1900-01-01', |
61 |
expirationdate => '1900-01-01', |
62 |
lowestPriority => 0, |
63 |
suspend => 0, |
64 |
} |
65 |
)->store(); |
66 |
my $hold_2 = Koha::Hold->new( |
67 |
{ |
68 |
priority => 1, |
69 |
borrowernumber => $patron_2->{borrowernumber}, |
70 |
branchcode => $library_1, |
71 |
biblionumber => $biblionumber, |
72 |
itemnumber => $itemnumber, |
73 |
reservedate => '1900-01-01', |
74 |
expirationdate => '9999-01-01', |
75 |
lowestPriority => 0, |
76 |
suspend => 0, |
77 |
} |
78 |
)->store(); |
79 |
my $hold_3 = Koha::Hold->new( |
80 |
{ |
81 |
priority => 2, |
82 |
borrowernumber => $patron_2->{borrowernumber}, |
83 |
branchcode => $library_1, |
84 |
biblionumber => $biblionumber, |
85 |
itemnumber => $itemnumber, |
86 |
reservedate => '1900-01-01', |
87 |
expirationdate => '9999-01-01', |
88 |
lowestPriority => 0, |
89 |
suspend => 0, |
90 |
} |
91 |
)->store(); |
92 |
|
93 |
# Test CancelExpiredReserves |
94 |
t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); |
95 |
t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 1 ); |
96 |
t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays', 1 ); |
97 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill', 1 ); |
98 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail', |
99 |
'kyle@example.com' ); |
100 |
|
101 |
CancelExpiredReserves(); |
102 |
|
103 |
my @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
104 |
$hold_2 = $holds[0]; |
105 |
$hold_3 = $holds[1]; |
106 |
|
107 |
is( @holds, 2, 'Found 2 holds' ); |
108 |
is( $hold_2->priority, 0, 'Next hold in line now has priority of 0' ); |
109 |
is( $hold_2->found, 'W', 'Next hold in line is now set to waiting' ); |
110 |
|
111 |
my @messages = $schema->resultset('MessageQueue') |
112 |
->search( { letter_code => 'HOLD_CHANGED' } ); |
113 |
is( @messages, 1, 'Found 1 message in the message queue' ); |
114 |
is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' ); |
115 |
|
116 |
$hold_2->expirationdate('1900-01-01')->store(); |
117 |
|
118 |
CancelExpiredReserves(); |
119 |
|
120 |
@holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
121 |
$hold_3 = $holds[0]; |
122 |
|
123 |
is( @holds, 1, 'Found 1 hold' ); |
124 |
is( $hold_3->priority, 0, 'Next hold in line now has priority of 0' ); |
125 |
is( $hold_3->found, 'W', 'Next hold in line is now set to waiting' ); |
126 |
}; |
127 |
|
128 |
subtest 'Test automatically canceled expired waiting holds to fill the next hold, with a transfer' => sub { |
129 |
plan tests => 6; |
130 |
|
131 |
$dbh->do('DELETE FROM reserves'); |
132 |
$dbh->do('DELETE FROM message_queue'); |
133 |
|
134 |
# Add a hold on the item for each of our patrons |
135 |
my $hold_1 = Koha::Hold->new( |
136 |
{ |
137 |
priority => 0, |
138 |
borrowernumber => $patron_1->{borrowernumber}, |
139 |
branchcode => $library_1, |
140 |
biblionumber => $biblionumber, |
141 |
itemnumber => $itemnumber, |
142 |
found => 'W', |
143 |
reservedate => '1900-01-01', |
144 |
waitingdate => '1900-01-01', |
145 |
expirationdate => '1900-01-01', |
146 |
lowestPriority => 0, |
147 |
suspend => 0, |
148 |
} |
149 |
)->store(); |
150 |
my $hold_2 = Koha::Hold->new( |
151 |
{ |
152 |
priority => 1, |
153 |
borrowernumber => $patron_2->{borrowernumber}, |
154 |
branchcode => $library_2, |
155 |
biblionumber => $biblionumber, |
156 |
itemnumber => $itemnumber, |
157 |
reservedate => '1900-01-01', |
158 |
expirationdate => '9999-01-01', |
159 |
lowestPriority => 0, |
160 |
suspend => 0, |
161 |
} |
162 |
)->store(); |
163 |
|
164 |
# Test CancelExpiredReserves |
165 |
t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); |
166 |
t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 1 ); |
167 |
t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays', 1 ); |
168 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill', 1 ); |
169 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail', |
170 |
'kyle@example.com' ); |
171 |
|
172 |
CancelExpiredReserves(); |
173 |
|
174 |
my @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
175 |
$hold_2 = $holds[0]; |
176 |
|
177 |
is( @holds, 1, 'Found 1 hold' ); |
178 |
is( $hold_2->priority, 0, 'Next hold in line now has priority of 0' ); |
179 |
is( $hold_2->found, 'T', 'Next hold in line is now set to in transit' ); |
180 |
is( $hold_2->branchcode, $library_2, "Next hold in line has correct branchcode" ); |
181 |
|
182 |
my @messages = $schema->resultset('MessageQueue') |
183 |
->search( { letter_code => 'HOLD_CHANGED' } ); |
184 |
is( @messages, 1, 'Found 1 message in the message queue' ); |
185 |
is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' ); |
186 |
}; |