5 use Test::More tests => 3;
8 use t::lib::TestBuilder;
20 use lib $FindBin::Bin;
21 use_ok('C4::Reserves');
24 my $schema = Koha::Database->new->schema;
25 $schema->storage->txn_begin;
27 my $builder = t::lib::TestBuilder->new();
28 my $dbh = C4::Context->dbh;
30 # Create two random branches
31 my $library_1 = $builder->build({ source => 'Branch' })->{ branchcode };
32 my $library_2 = $builder->build({ source => 'Branch' })->{ branchcode };
34 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
35 my $biblionumber = $biblio->id;
37 # Create item instance for testing.
38 my $itemnumber = $builder->build_sample_item({ library => $library_1, biblionumber => $biblio->biblionumber })->itemnumber;
40 my $patron_1 = $builder->build( { source => 'Borrower' } );
41 my $patron_2 = $builder->build( { source => 'Borrower' } );
42 my $patron_3 = $builder->build( { source => 'Borrower' } );
44 subtest 'Test automatically canceled expired waiting holds to fill the next hold, without a transfer' => sub {
47 $dbh->do('DELETE FROM reserves');
48 $dbh->do('DELETE FROM message_queue');
50 # Add a hold on the item for each of our patrons
51 my $hold_1 = Koha::Hold->new(
54 borrowernumber => $patron_1->{borrowernumber},
55 branchcode => $library_1,
56 biblionumber => $biblionumber,
57 itemnumber => $itemnumber,
59 reservedate => '1900-01-01',
60 waitingdate => '1900-01-01',
61 expirationdate => '1900-01-01',
66 my $hold_2 = Koha::Hold->new(
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',
79 my $hold_3 = Koha::Hold->new(
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',
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',
101 CancelExpiredReserves();
103 my @holds = Koha::Holds->search( {}, { order_by => 'priority' } );
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' );
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' );
116 $hold_2->expirationdate('1900-01-01')->store();
118 CancelExpiredReserves();
120 @holds = Koha::Holds->search( {}, { order_by => 'priority' } );
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' );
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' );
133 subtest 'Test automatically canceled expired waiting holds to fill the next hold, with a transfer' => sub {
136 $dbh->do('DELETE FROM reserves');
137 $dbh->do('DELETE FROM message_queue');
139 # Add a hold on the item for each of our patrons
140 my $hold_1 = Koha::Hold->new(
143 borrowernumber => $patron_1->{borrowernumber},
144 branchcode => $library_1,
145 biblionumber => $biblionumber,
146 itemnumber => $itemnumber,
148 reservedate => '1900-01-01',
149 waitingdate => '1900-01-01',
150 expirationdate => '1900-01-01',
155 my $hold_2 = Koha::Hold->new(
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',
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' );
177 CancelExpiredReserves();
179 my @holds = Koha::Holds->search( {}, { order_by => 'priority' } );
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" );
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' );