Bug 14364: Allow automatically canceled expired waiting holds to fill the next hold
[koha.git] / t / db_dependent / Holds / ExpireReservesAutoFill.t
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 => 10;
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     @messages = $schema->resultset('MessageQueue')
128       ->search( { letter_code => 'HOLD_CHANGED' } );
129     is( @messages, 2, 'Found 2 messages in the message queue' );
130     is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' );
131 };
132
133 subtest 'Test automatically canceled expired waiting holds to fill the next hold, with a transfer' => sub {
134     plan tests => 5;
135
136     $dbh->do('DELETE FROM reserves');
137     $dbh->do('DELETE FROM message_queue');
138
139     # Add a hold on the item for each of our patrons
140     my $hold_1 = Koha::Hold->new(
141         {
142             priority       => 0,
143             borrowernumber => $patron_1->{borrowernumber},
144             branchcode     => $library_1,
145             biblionumber   => $biblionumber,
146             itemnumber     => $itemnumber,
147             found          => 'W',
148             reservedate    => '1900-01-01',
149             waitingdate    => '1900-01-01',
150             expirationdate => '1900-01-01',
151             lowestPriority => 0,
152             suspend        => 0,
153         }
154     )->store();
155     my $hold_2 = Koha::Hold->new(
156         {
157             priority       => 1,
158             borrowernumber => $patron_2->{borrowernumber},
159             branchcode     => $library_2,
160             biblionumber   => $biblionumber,
161             itemnumber     => $itemnumber,
162             reservedate    => '1900-01-01',
163             expirationdate => '9999-01-01',
164             lowestPriority => 0,
165             suspend        => 0,
166         }
167     )->store();
168
169     # Test CancelExpiredReserves
170     t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
171     t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay',       1 );
172     t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays',     1 );
173     t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill',       1 );
174     t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail',
175         'kyle@example.com' );
176
177     CancelExpiredReserves();
178
179     my @holds = Koha::Holds->search( {}, { order_by => 'priority' } );
180     $hold_2 = $holds[0];
181
182     is( @holds,            1,   'Found 1 hold' );
183     is( $hold_2->priority, 0,   'Next hold in line now has priority of 0' );
184     is( $hold_2->found,    'T', 'Next hold in line is now set to in transit' );
185     is( $hold_2->branchcode, $library_2, "Next hold in line has correct branchcode" );
186
187     my @messages = $schema->resultset('MessageQueue')
188       ->search( { letter_code => 'HOLD_CHANGED' } );
189     is( @messages, 0, 'No messages in the message queue when generating transfer' );
190 };