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