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 => 6;
28 use t::lib::TestBuilder;
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'fill() tests' => sub {
43 $schema->storage->txn_begin;
47 my $category = $builder->build_object(
49 class => 'Koha::Patron::Categories',
50 value => { reservefee => $fee }
53 my $patron = $builder->build_object(
55 class => 'Koha::Patrons',
56 value => { categorycode => $category->id }
59 my $manager = $builder->build_object( { class => 'Koha::Patrons' } );
61 my $title = 'Do what you want';
62 my $biblio = $builder->build_sample_biblio( { title => $title } );
63 my $item = $builder->build_sample_item( { biblionumber => $biblio->id } );
64 my $hold = $builder->build_object(
66 class => 'Koha::Holds',
68 biblionumber => $biblio->id,
69 borrowernumber => $patron->id,
70 itemnumber => $item->id,
76 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'any_time_is_collected' );
77 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
78 t::lib::Mocks::mock_userenv(
79 { patron => $manager, branchcode => $manager->branchcode } );
81 my $interface = 'api';
82 C4::Context->interface($interface);
84 my $ret = $hold->fill;
86 is( ref($ret), 'Koha::Hold', '->fill returns the object type' );
87 is( $ret->id, $hold->id, '->fill returns the object' );
89 is( Koha::Holds->find($hold->id), undef, 'Hold no longer current' );
90 my $old_hold = Koha::Old::Holds->find( $hold->id );
92 is( $old_hold->id, $hold->id, 'reserve_id retained' );
93 is( $old_hold->priority, 0, 'priority set to 0' );
94 is( $old_hold->found, 'F', 'found set to F' );
96 subtest 'fee applied tests' => sub {
100 my $account = $patron->account;
101 is( $account->balance, $fee, 'Charge applied correctly' );
103 my $debits = $account->outstanding_debits;
104 is( $debits->count, 1, 'Only one fee charged' );
106 my $fee_debit = $debits->next;
107 is( $fee_debit->amount * 1, $fee, 'Fee amount stored correctly' );
108 is( $fee_debit->description, $title,
109 'Fee description stored correctly' );
110 is( $fee_debit->manager_id, $manager->id,
111 'Fee manager_id stored correctly' );
112 is( $fee_debit->branchcode, $manager->branchcode,
113 'Fee branchcode stored correctly' );
114 is( $fee_debit->interface, $interface,
115 'Fee interface stored correctly' );
116 is( $fee_debit->debit_type_code,
117 'RESERVE', 'Fee debit_type_code stored correctly' );
118 is( $fee_debit->itemnumber, $item->id,
119 'Fee itemnumber stored correctly' );
122 my $logs = Koha::ActionLogs->search(
130 is( $logs->count, 1, '1 log line added' );
132 # Set HoldFeeMode to something other than any_time_is_collected
133 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
135 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
137 $hold = $builder->build_object(
139 class => 'Koha::Holds',
141 biblionumber => $biblio->id,
142 borrowernumber => $patron->id,
143 itemnumber => $item->id,
151 my $account = $patron->account;
152 is( $account->balance, $fee, 'No new charge applied' );
154 my $debits = $account->outstanding_debits;
155 is( $debits->count, 1, 'Only one fee charged, because of HoldFeeMode' );
157 $logs = Koha::ActionLogs->search(
165 is( $logs->count, 0, 'HoldsLog disabled, no logs added' );
167 subtest 'anonymization behavior tests' => sub {
171 # reduce the tests noise
172 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
173 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
174 # unset AnonymousPatron
175 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
178 $patron->privacy(0)->store;
179 my $hold = $builder->build_object(
181 class => 'Koha::Holds',
182 value => { borrowernumber => $patron->id, found => undef }
186 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
187 $patron->borrowernumber, 'Patron link is kept' );
189 # 1 == "default", meaning it is not protected from removal
190 $patron->privacy(1)->store;
191 $hold = $builder->build_object(
193 class => 'Koha::Holds',
194 value => { borrowernumber => $patron->id, found => undef }
198 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
199 $patron->borrowernumber, 'Patron link is kept' );
201 # 2 == delete immediately
202 $patron->privacy(2)->store;
203 $hold = $builder->build_object(
205 class => 'Koha::Holds',
206 value => { borrowernumber => $patron->id, found => undef }
213 'AnonymousPatron not set, exception thrown';
215 $hold->discard_changes; # refresh from DB
217 ok( !$hold->is_found, 'Hold is not filled' );
219 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
220 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
222 $hold = $builder->build_object(
224 class => 'Koha::Holds',
225 value => { borrowernumber => $patron->id, found => undef }
230 Koha::Old::Holds->find( $hold->id )->borrowernumber,
231 $anonymous_patron->id,
232 'Patron link is set to the configured anonymous patron immediately'
236 subtest 'holds_queue update tests' => sub {
240 my $biblio = $builder->build_sample_biblio;
242 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
243 $mock->mock( 'enqueue', sub {
244 my ( $self, $args ) = @_;
248 '->fill triggers a holds queue update for the related biblio'
252 $builder->build_object(
254 class => 'Koha::Holds',
256 biblionumber => $biblio->id,
262 $schema->storage->txn_rollback;
265 subtest 'patron() tests' => sub {
269 $schema->storage->txn_begin;
271 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
272 my $hold = $builder->build_object(
274 class => 'Koha::Holds',
276 borrowernumber => $patron->borrowernumber
281 my $hold_patron = $hold->patron;
282 is( ref($hold_patron), 'Koha::Patron', 'Right type' );
283 is( $hold_patron->id, $patron->id, 'Right object' );
285 $schema->storage->txn_rollback;
288 subtest 'set_pickup_location() tests' => sub {
292 $schema->storage->txn_begin;
294 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
295 my $mock_item = Test::MockModule->new('Koha::Item');
297 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
298 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
299 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
301 # let's control what Koha::Biblio->pickup_locations returns, for testing
302 $mock_biblio->mock( 'pickup_locations', sub {
303 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
305 # let's mock what Koha::Item->pickup_locations returns, for testing
306 $mock_item->mock( 'pickup_locations', sub {
307 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
310 my $biblio = $builder->build_sample_biblio;
311 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
313 # Test biblio-level holds
314 my $biblio_hold = $builder->build_object(
316 class => "Koha::Holds",
318 biblionumber => $biblio->biblionumber,
319 branchcode => $library_3->branchcode,
326 { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
327 'Koha::Exceptions::Hold::InvalidPickupLocation',
328 'Exception thrown on invalid pickup location';
330 $biblio_hold->discard_changes;
331 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
333 my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
334 is( ref($ret), 'Koha::Hold', 'self is returned' );
336 $biblio_hold->discard_changes;
337 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
339 # Test item-level holds
340 my $item_hold = $builder->build_object(
342 class => "Koha::Holds",
344 biblionumber => $biblio->biblionumber,
345 branchcode => $library_3->branchcode,
346 itemnumber => $item->itemnumber,
352 { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
353 'Koha::Exceptions::Hold::InvalidPickupLocation',
354 'Exception thrown on invalid pickup location';
356 $item_hold->discard_changes;
357 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
359 $item_hold->set_pickup_location({ library_id => $library_1->branchcode, force => 1 });
360 $item_hold->discard_changes;
361 is( $item_hold->branchcode, $library_1->branchcode, 'branchcode changed because of \'force\'' );
363 $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
364 is( ref($ret), 'Koha::Hold', 'self is returned' );
366 $item_hold->discard_changes;
367 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
370 { $item_hold->set_pickup_location({ library_id => undef }); }
371 'Koha::Exceptions::MissingParameter',
372 'Exception thrown if missing parameter';
374 like( "$@", qr/The library_id parameter is mandatory/, 'Exception message is clear' );
376 $schema->storage->txn_rollback;
379 subtest 'is_pickup_location_valid() tests' => sub {
383 $schema->storage->txn_begin;
385 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
386 my $mock_item = Test::MockModule->new('Koha::Item');
388 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
389 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
390 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
392 # let's control what Koha::Biblio->pickup_locations returns, for testing
393 $mock_biblio->mock( 'pickup_locations', sub {
394 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
396 # let's mock what Koha::Item->pickup_locations returns, for testing
397 $mock_item->mock( 'pickup_locations', sub {
398 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
401 my $biblio = $builder->build_sample_biblio;
402 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
404 # Test biblio-level holds
405 my $biblio_hold = $builder->build_object(
407 class => "Koha::Holds",
409 biblionumber => $biblio->biblionumber,
410 branchcode => $library_3->branchcode,
416 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
417 ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
419 # Test item-level holds
420 my $item_hold = $builder->build_object(
422 class => "Koha::Holds",
424 biblionumber => $biblio->biblionumber,
425 branchcode => $library_3->branchcode,
426 itemnumber => $item->itemnumber,
431 ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
432 ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
434 subtest 'pickup_locations() returning ->empty' => sub {
438 $schema->storage->txn_begin;
440 my $library = $builder->build_object({ class => 'Koha::Libraries' });
442 my $mock_item = Test::MockModule->new('Koha::Item');
443 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
445 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
446 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
448 my $item = $builder->build_sample_item();
449 my $biblio = $item->biblio;
451 # Test biblio-level holds
452 my $biblio_hold = $builder->build_object(
454 class => "Koha::Holds",
456 biblionumber => $biblio->biblionumber,
462 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
464 # Test item-level holds
465 my $item_hold = $builder->build_object(
467 class => "Koha::Holds",
469 biblionumber => $biblio->biblionumber,
470 itemnumber => $item->itemnumber,
475 ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
477 $schema->storage->txn_rollback;
480 $schema->storage->txn_rollback;
483 subtest 'cancel() tests' => sub {
487 $schema->storage->txn_begin;
489 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
491 # reduce the tests noise
492 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
493 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',
496 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
499 $patron->privacy(0)->store;
500 my $hold = $builder->build_object(
502 class => 'Koha::Holds',
503 value => { borrowernumber => $patron->id, found => undef }
507 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
508 $patron->borrowernumber, 'Patron link is kept' );
510 # 1 == "default", meaning it is not protected from removal
511 $patron->privacy(1)->store;
512 $hold = $builder->build_object(
514 class => 'Koha::Holds',
515 value => { borrowernumber => $patron->id, found => undef }
519 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
520 $patron->borrowernumber, 'Patron link is kept' );
522 # 2 == delete immediately
523 $patron->privacy(2)->store;
524 $hold = $builder->build_object(
526 class => 'Koha::Holds',
527 value => { borrowernumber => $patron->id, found => undef }
533 'AnonymousPatron not set, exception thrown';
535 $hold->discard_changes;
537 ok( !$hold->is_found, 'Hold is not cancelled' );
539 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
540 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
542 $hold = $builder->build_object(
544 class => 'Koha::Holds',
545 value => { borrowernumber => $patron->id, found => undef }
550 Koha::Old::Holds->find( $hold->id )->borrowernumber,
551 $anonymous_patron->id,
552 'Patron link is set to the configured anonymous patron immediately'
555 subtest 'holds_queue update tests' => sub {
559 my $biblio = $builder->build_sample_biblio;
561 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
562 $mock->mock( 'enqueue', sub {
563 my ( $self, $args ) = @_;
567 '->cancel triggers a holds queue update for the related biblio'
571 $builder->build_object(
573 class => 'Koha::Holds',
575 biblionumber => $biblio->id,
580 # If the skip_holds_queue param is not honoured, then test count will fail.
581 $builder->build_object(
583 class => 'Koha::Holds',
585 biblionumber => $biblio->id,
588 )->cancel({ skip_holds_queue => 1 });
591 $schema->storage->txn_rollback;
594 subtest 'suspend_hold() and resume() tests' => sub {
598 $schema->storage->txn_begin;
600 my $biblio = $builder->build_sample_biblio;
603 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
604 $mock->mock( 'enqueue', sub {
605 my ( $self, $args ) = @_;
609 "->$action triggers a holds queue update for the related biblio"
613 my $hold = $builder->build_object(
615 class => 'Koha::Holds',
617 biblionumber => $biblio->id,
622 $action = 'suspend_hold';
628 $schema->storage->txn_rollback;