3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 4;
26 use Koha::AuthorisedValueCategory;
31 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'DB constraints' => sub {
41 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
42 my $item = $builder->build_sample_item;
44 branchcode => $patron->branchcode,
45 borrowernumber => $patron->borrowernumber,
46 biblionumber => $item->biblionumber,
48 title => "title for fee",
49 itemnumber => $item->itemnumber,
52 my $reserve_id = C4::Reserves::AddReserve($hold_info);
53 my $hold = Koha::Holds->find( $reserve_id );
56 eval { $hold->priority(undef)->store }
58 qr{.*DBD::mysql::st execute failed: Column 'priority' cannot be null.*},
59 'DBD should have raised an error about priority that cannot be null';
62 subtest 'cancel' => sub {
64 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
65 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
66 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
68 biblionumber => $biblioitem->biblionumber,
69 biblioitemnumber => $biblioitem->biblioitemnumber,
70 homebranch => $library->branchcode,
71 holdingbranch => $library->branchcode,
72 itype => $itemtype->itemtype,
74 my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
75 my $manager = $builder->build_object({ class => "Koha::Patrons" });
76 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
78 my ( @patrons, @holds );
79 for my $i ( 0 .. 2 ) {
80 my $priority = $i + 1;
81 my $patron = $builder->build_object(
83 class => 'Koha::Patrons',
84 value => { branchcode => $library->branchcode, }
87 my $reserve_id = C4::Reserves::AddReserve(
89 branchcode => $library->branchcode,
90 borrowernumber => $patron->borrowernumber,
91 biblionumber => $item->biblionumber,
92 priority => $priority,
93 title => "title for fee",
94 itemnumber => $item->itemnumber,
97 my $hold = Koha::Holds->find($reserve_id);
98 push @patrons, $patron;
102 # There are 3 holds on this records
104 Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
106 'There should have 3 holds placed on this biblio record' );
107 my $first_hold = $holds[0];
108 my $second_hold = $holds[1];
109 my $third_hold = $holds[2];
110 is( ref($second_hold), 'Koha::Hold',
111 'We should play with Koha::Hold objects' );
112 is( $second_hold->priority, 2,
113 'Second hold should have a priority set to 3' );
115 # Remove the second hold, only 2 should still exist in DB and priorities must have been updated
116 my $is_cancelled = $second_hold->cancel;
117 is( ref($is_cancelled), 'Koha::Hold',
118 'Koha::Hold->cancel should return the Koha::Hold (?)' )
119 ; # This is can reconsidered
120 is( $second_hold->in_storage, 0,
121 'The hold has been cancelled and does not longer exist in DB' );
123 Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
125 'a hold has been cancelled, there should have only 2 holds placed on this biblio record'
128 # discard_changes to refetch
129 is( $first_hold->discard_changes->priority, 1, 'First hold should still be first' );
130 is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
132 subtest 'charge_cancel_fee parameter' => sub {
134 my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } );
135 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } });
136 is( $patron->account->balance, 0, 'A new patron does not have any charges' );
139 branchcode => $library->branchcode,
140 borrowernumber => $patron->borrowernumber,
141 biblionumber => $item->biblionumber,
143 title => "title for fee",
144 itemnumber => $item->itemnumber,
147 # First, test cancelling a reserve when there's no charge configured.
148 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
149 my $reserve_id = C4::Reserves::AddReserve( $hold_info );
150 Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
151 is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' );
153 # Then, test cancelling a reserve when there's no charge desired.
154 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
155 $reserve_id = C4::Reserves::AddReserve( $hold_info );
156 Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0
157 is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' );
160 # Finally, test cancelling a reserve when there's a charge desired and configured.
161 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
162 $reserve_id = C4::Reserves::AddReserve( $hold_info );
163 Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
164 is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' );
167 subtest 'waiting hold' => sub {
169 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
170 my $reserve_id = C4::Reserves::AddReserve(
172 branchcode => $library->branchcode,
173 borrowernumber => $patron->borrowernumber,
174 biblionumber => $item->biblionumber,
176 title => "title for fee",
177 itemnumber => $item->itemnumber,
181 Koha::Holds->find( $reserve_id )->cancel;
182 my $hold_old = Koha::Old::Holds->find( $reserve_id );
183 is( $hold_old->found, 'W', 'The found column should have been kept and a hold is cancelled' );
186 subtest 'HoldsLog' => sub {
188 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
190 branchcode => $library->branchcode,
191 borrowernumber => $patron->borrowernumber,
192 biblionumber => $item->biblionumber,
194 title => "title for fee",
195 itemnumber => $item->itemnumber,
198 t::lib::Mocks::mock_preference('HoldsLog', 0);
199 my $reserve_id = C4::Reserves::AddReserve($hold_info);
200 Koha::Holds->find( $reserve_id )->cancel;
201 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
202 is( $number_of_logs, 0, 'Without HoldsLog, Koha::Hold->cancel should not have logged' );
204 t::lib::Mocks::mock_preference('HoldsLog', 1);
205 $reserve_id = C4::Reserves::AddReserve($hold_info);
206 Koha::Holds->find( $reserve_id )->cancel;
207 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
208 is( $number_of_logs, 1, 'With HoldsLog, Koha::Hold->cancel should have logged' );
211 subtest 'rollback' => sub {
213 my $patron_category = $builder->build_object(
215 class => 'Koha::Patron::Categories',
216 value => { reservefee => 0 }
219 my $patron = $builder->build_object(
221 class => 'Koha::Patrons',
222 value => { categorycode => $patron_category->categorycode }
226 branchcode => $library->branchcode,
227 borrowernumber => $patron->borrowernumber,
228 biblionumber => $item->biblionumber,
230 title => "title for fee",
231 itemnumber => $item->itemnumber,
234 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',42 );
235 my $reserve_id = C4::Reserves::AddReserve($hold_info);
236 my $hold = Koha::Holds->find($reserve_id);
238 # Add a row with the same id to make the cancel fails
239 Koha::Old::Hold->new( $hold->unblessed )->store;
242 eval { $hold->cancel( { charge_cancel_fee => 1 } ) };
244 qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
245 'DBD should have raised an error about dup primary key';
247 $hold = Koha::Holds->find($reserve_id);
248 is( ref($hold), 'Koha::Hold', 'The hold should not have been deleted' );
249 is( $patron->account->balance, 0,
250 'If the hold has not been cancelled, the patron should not have been charged'
256 subtest 'cancel with reason' => sub {
258 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
259 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
260 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
262 biblionumber => $biblioitem->biblionumber,
263 biblioitemnumber => $biblioitem->biblioitemnumber,
264 homebranch => $library->branchcode,
265 holdingbranch => $library->branchcode,
266 itype => $itemtype->itemtype,
268 my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
269 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
270 t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
272 my $patron = $builder->build_object(
274 class => 'Koha::Patrons',
275 value => { branchcode => $library->branchcode, }
279 my $reserve_id = C4::Reserves::AddReserve(
281 branchcode => $library->branchcode,
282 borrowernumber => $patron->borrowernumber,
283 biblionumber => $item->biblionumber,
285 itemnumber => $item->itemnumber,
289 my $hold = Koha::Holds->find($reserve_id);
291 ok($reserve_id, "Hold created");
292 ok($hold, "Hold found");
294 my $av = Koha::AuthorisedValue->new( { category => 'HOLD_CANCELLATION', authorised_value => 'TEST_REASON' } )->store;
295 Koha::Notice::Templates->search({ code => 'HOLD_CANCELLATION'})->delete();
296 my $notice = Koha::Notice::Template->new({
297 name => 'Hold cancellation',
298 module => 'reserves',
299 code => 'HOLD_CANCELLATION',
300 title => 'Hold cancelled',
301 content => 'Your hold was cancelled.',
302 message_transport_type => 'email',
306 $hold->cancel({cancellation_reason => 'TEST_REASON'});
308 $hold = Koha::Holds->find($reserve_id);
309 is( $hold, undef, 'Hold is not in the reserves table');
310 $hold = Koha::Old::Holds->find($reserve_id);
311 ok( $hold, 'Hold was found in the old reserves table');
313 my $message = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'HOLD_CANCELLATION'});
314 ok( $message, 'Found hold cancellation message');
315 is( $message->subject, 'Hold cancelled', 'Message has correct title' );
316 is( $message->content, 'Your hold was cancelled.', 'Message has correct content');
323 subtest 'cancel all with reason' => sub {
325 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
326 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
327 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
329 biblionumber => $biblioitem->biblionumber,
330 biblioitemnumber => $biblioitem->biblioitemnumber,
331 homebranch => $library->branchcode,
332 holdingbranch => $library->branchcode,
333 itype => $itemtype->itemtype,
335 my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
336 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
337 t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
339 my $patron = $builder->build_object(
341 class => 'Koha::Patrons',
342 value => { branchcode => $library->branchcode, }
346 my $reserve_id = C4::Reserves::AddReserve(
348 branchcode => $library->branchcode,
349 borrowernumber => $patron->borrowernumber,
350 biblionumber => $item->biblionumber,
352 itemnumber => $item->itemnumber,
356 my $hold = Koha::Holds->find($reserve_id);
358 ok($reserve_id, "Hold created");
359 ok($hold, "Hold found");
361 my $av = Koha::AuthorisedValue->new( { category => 'HOLD_CANCELLATION', authorised_value => 'TEST_REASON' } )->store;
362 Koha::Notice::Templates->search({ code => 'HOLD_CANCELLATION'})->delete();
363 my $notice = Koha::Notice::Template->new({
364 name => 'Hold cancellation',
365 module => 'reserves',
366 code => 'HOLD_CANCELLATION',
367 title => 'Hold cancelled',
368 content => 'Your hold was cancelled.',
369 message_transport_type => 'email',
373 ModReserveCancelAll($item->id, $patron->id, 'TEST_REASON');
375 $hold = Koha::Holds->find($reserve_id);
376 is( $hold, undef, 'Hold is not in the reserves table');
377 $hold = Koha::Old::Holds->find($reserve_id);
378 ok( $hold, 'Hold was found in the old reserves table');
380 my $message = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'HOLD_CANCELLATION'});
381 ok( $message, 'Found hold cancellation message');
382 is( $message->subject, 'Hold cancelled', 'Message has correct title' );
383 is( $message->content, 'Your hold was cancelled.', 'Message has correct content');
389 $schema->storage->txn_rollback;