Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Test::More tests => 9; |
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 = $builder->build( { source => 'Branch' } ); |
32 |
my $branchcode = $library->{branchcode}; |
33 |
|
34 |
$dbh->do('DELETE FROM reserves'); |
35 |
$dbh->do('DELETE FROM message_queue'); |
36 |
|
37 |
# Create a biblio instance for testing |
38 |
my ($biblionumber) = create_helper_biblio('DUMMY'); |
39 |
|
40 |
# Create item instance for testing. |
41 |
my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = |
42 |
AddItem( { homebranch => $branchcode, holdingbranch => $branchcode }, |
43 |
$biblionumber ); |
44 |
|
45 |
my $patron_1 = $builder->build( { source => 'Borrower' } ); |
46 |
my $patron_2 = $builder->build( { source => 'Borrower' } ); |
47 |
my $patron_3 = $builder->build( { source => 'Borrower' } ); |
48 |
|
49 |
# Add a hold on the item for each of our patrons |
50 |
my $hold_1 = Koha::Hold->new( |
51 |
{ |
52 |
priority => 0, |
53 |
borrowernumber => $patron_1->{borrowernumber}, |
54 |
branchcode => $library->{branchcode}, |
55 |
biblionumber => $biblionumber, |
56 |
itemnumber => $itemnumber, |
57 |
found => 'W', |
58 |
reservedate => '1900-01-01', |
59 |
waitingdate => '1900-01-01', |
60 |
expirationdate => '1900-01-01', |
61 |
lowestPriority => 0, |
62 |
suspend => 0, |
63 |
} |
64 |
)->store(); |
65 |
my $hold_2 = Koha::Hold->new( |
66 |
{ |
67 |
priority => 1, |
68 |
borrowernumber => $patron_2->{borrowernumber}, |
69 |
branchcode => $library->{branchcode}, |
70 |
biblionumber => $biblionumber, |
71 |
itemnumber => $itemnumber, |
72 |
reservedate => '1900-01-01', |
73 |
expirationdate => '9999-01-01', |
74 |
lowestPriority => 0, |
75 |
suspend => 0, |
76 |
} |
77 |
)->store(); |
78 |
my $hold_3 = Koha::Hold->new( |
79 |
{ |
80 |
priority => 2, |
81 |
borrowernumber => $patron_2->{borrowernumber}, |
82 |
branchcode => $library->{branchcode}, |
83 |
biblionumber => $biblionumber, |
84 |
itemnumber => $itemnumber, |
85 |
reservedate => '1900-01-01', |
86 |
expirationdate => '9999-01-01', |
87 |
lowestPriority => 0, |
88 |
suspend => 0, |
89 |
} |
90 |
)->store(); |
91 |
|
92 |
# Test CancelExpiredReserves |
93 |
t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); |
94 |
t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 1 ); |
95 |
t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays', 1 ); |
96 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill', 1 ); |
97 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail', |
98 |
'kyle@example.com' ); |
99 |
|
100 |
CancelExpiredReserves(); |
101 |
|
102 |
my @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
103 |
$hold_2 = $holds[0]; |
104 |
$hold_3 = $holds[1]; |
105 |
|
106 |
is( @holds, 2, 'Found 2 holds' ); |
107 |
is( $hold_2->priority, 0, 'Next hold in line now has priority of 0' ); |
108 |
is( $hold_2->found, 'W', 'Next hold in line is now set to waiting' ); |
109 |
|
110 |
my @messages = $schema->resultset('MessageQueue') |
111 |
->search( { letter_code => 'HOLD_CHANGED' } ); |
112 |
is( @messages, 1, 'Found 1 message in the message queue' ); |
113 |
is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' ); |
114 |
|
115 |
$hold_2->expirationdate('1900-01-01')->store(); |
116 |
|
117 |
CancelExpiredReserves(); |
118 |
|
119 |
@holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
120 |
$hold_3 = $holds[0]; |
121 |
|
122 |
is( @holds, 1, 'Found 1 hold' ); |
123 |
is( $hold_3->priority, 0, 'Next hold in line now has priority of 0' ); |
124 |
is( $hold_3->found, 'W', 'Next hold in line is now set to waiting' ); |
125 |
|
126 |
# Helper method to set up a Biblio. |
127 |
sub create_helper_biblio { |
128 |
my $itemtype = shift; |
129 |
my $bib = MARC::Record->new(); |
130 |
my $title = 'Silence in the library'; |
131 |
$bib->append_fields( |
132 |
MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), |
133 |
MARC::Field->new( '245', ' ', ' ', a => $title ), |
134 |
MARC::Field->new( '942', ' ', ' ', c => $itemtype ), |
135 |
); |
136 |
return my ( $b, $t, $bi ) = AddBiblio( $bib, '' ); |
137 |
} |