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 $library = $builder->build_object( { class => 'Koha::Libraries' } );
259 my $item = $builder->build_sample_item({ library
=> $library->branchcode });
260 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
261 t
::lib
::Mocks
::mock_userenv
( { patron
=> $manager, branchcode
=> $manager->branchcode } );
263 my $patron = $builder->build_object(
265 class => 'Koha::Patrons',
266 value
=> { branchcode
=> $library->branchcode, }
270 my $reserve_id = C4
::Reserves
::AddReserve
(
272 branchcode
=> $library->branchcode,
273 borrowernumber
=> $patron->borrowernumber,
274 biblionumber
=> $item->biblionumber,
276 itemnumber
=> $item->itemnumber,
280 my $hold = Koha
::Holds
->find($reserve_id);
282 ok
($reserve_id, "Hold created");
283 ok
($hold, "Hold found");
285 my $av = Koha
::AuthorisedValue
->new( { category
=> 'HOLD_CANCELLATION', authorised_value
=> 'TEST_REASON' } )->store;
286 Koha
::Notice
::Templates
->search({ code
=> 'HOLD_CANCELLATION'})->delete();
287 my $notice = Koha
::Notice
::Template
->new({
288 name
=> 'Hold cancellation',
289 module
=> 'reserves',
290 code
=> 'HOLD_CANCELLATION',
291 title
=> 'Hold cancelled',
292 content
=> 'Your hold was cancelled.',
293 message_transport_type
=> 'email',
297 $hold->cancel({cancellation_reason
=> 'TEST_REASON'});
299 $hold = Koha
::Holds
->find($reserve_id);
300 is
( $hold, undef, 'Hold is not in the reserves table');
301 $hold = Koha
::Old
::Holds
->find($reserve_id);
302 ok
( $hold, 'Hold was found in the old reserves table');
304 my $message = Koha
::Notice
::Messages
->find({ borrowernumber
=> $patron->id, letter_code
=> 'HOLD_CANCELLATION'});
305 ok
( $message, 'Found hold cancellation message');
306 is
( $message->subject, 'Hold cancelled', 'Message has correct title' );
307 is
( $message->content, 'Your hold was cancelled.', 'Message has correct content');
314 subtest
'cancel all with reason' => sub {
316 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
317 my $item = $builder->build_sample_item({ library
=> $library->branchcode });
318 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
319 t
::lib
::Mocks
::mock_userenv
( { patron
=> $manager, branchcode
=> $manager->branchcode } );
321 my $patron = $builder->build_object(
323 class => 'Koha::Patrons',
324 value
=> { branchcode
=> $library->branchcode, }
328 my $reserve_id = C4
::Reserves
::AddReserve
(
330 branchcode
=> $library->branchcode,
331 borrowernumber
=> $patron->borrowernumber,
332 biblionumber
=> $item->biblionumber,
334 itemnumber
=> $item->itemnumber,
338 my $hold = Koha
::Holds
->find($reserve_id);
340 ok
($reserve_id, "Hold created");
341 ok
($hold, "Hold found");
343 my $av = Koha
::AuthorisedValue
->new( { category
=> 'HOLD_CANCELLATION', authorised_value
=> 'TEST_REASON' } )->store;
344 Koha
::Notice
::Templates
->search({ code
=> 'HOLD_CANCELLATION'})->delete();
345 my $notice = Koha
::Notice
::Template
->new({
346 name
=> 'Hold cancellation',
347 module
=> 'reserves',
348 code
=> 'HOLD_CANCELLATION',
349 title
=> 'Hold cancelled',
350 content
=> 'Your hold was cancelled.',
351 message_transport_type
=> 'email',
355 ModReserveCancelAll
($item->id, $patron->id, 'TEST_REASON');
357 $hold = Koha
::Holds
->find($reserve_id);
358 is
( $hold, undef, 'Hold is not in the reserves table');
359 $hold = Koha
::Old
::Holds
->find($reserve_id);
360 ok
( $hold, 'Hold was found in the old reserves table');
362 my $message = Koha
::Notice
::Messages
->find({ borrowernumber
=> $patron->id, letter_code
=> 'HOLD_CANCELLATION'});
363 ok
( $message, 'Found hold cancellation message');
364 is
( $message->subject, 'Hold cancelled', 'Message has correct title' );
365 is
( $message->content, 'Your hold was cancelled.', 'Message has correct content');
371 $schema->storage->txn_rollback;