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 => 12;
28 use t::lib::TestBuilder;
32 use Koha::DateUtils qw(dt_from_string);
37 my $schema = Koha::Database->new->schema;
38 my $builder = t::lib::TestBuilder->new;
40 subtest 'store() tests' => sub {
43 $schema->storage->txn_begin;
45 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
46 my $item = $builder->build_sample_item;
50 borrowernumber => $patron->borrowernumber,
51 biblionumber => $item->biblionumber,
53 itemnumber => $item->itemnumber,
57 'Koha::Exceptions::Hold::MissingPickupLocation',
58 'Exception thrown because branchcode was not passed';
60 my $hold = $builder->build_object( { class => 'Koha::Holds' } );
62 $hold->branchcode(undef)->store;
64 'Koha::Exceptions::Hold::MissingPickupLocation',
65 'Exception thrown if one tries to set branchcode to null';
67 $schema->storage->txn_rollback;
70 subtest 'biblio() tests' => sub {
74 $schema->storage->txn_begin;
76 my $hold = $builder->build_object(
78 class => 'Koha::Holds',
82 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /cannot be null/ };
83 throws_ok { $hold->biblionumber(undef)->store; }
84 'DBIx::Class::Exception',
85 'reserves.biblionumber cannot be null, exception thrown';
87 $schema->storage->txn_rollback;
90 subtest 'fill() tests' => sub {
94 $schema->storage->txn_begin;
98 my $category = $builder->build_object(
100 class => 'Koha::Patron::Categories',
101 value => { reservefee => $fee }
104 my $patron = $builder->build_object(
106 class => 'Koha::Patrons',
107 value => { categorycode => $category->id }
110 my $manager = $builder->build_object( { class => 'Koha::Patrons' } );
112 my $title = 'Do what you want';
113 my $biblio = $builder->build_sample_biblio( { title => $title } );
114 my $item = $builder->build_sample_item( { biblionumber => $biblio->id } );
115 my $hold = $builder->build_object(
117 class => 'Koha::Holds',
119 biblionumber => $biblio->id,
120 borrowernumber => $patron->id,
121 itemnumber => $item->id,
127 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'any_time_is_collected' );
128 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
129 t::lib::Mocks::mock_userenv(
130 { patron => $manager, branchcode => $manager->branchcode } );
132 my $interface = 'api';
133 C4::Context->interface($interface);
135 my $ret = $hold->fill;
137 is( ref($ret), 'Koha::Hold', '->fill returns the object type' );
138 is( $ret->id, $hold->id, '->fill returns the object' );
140 is( Koha::Holds->find($hold->id), undef, 'Hold no longer current' );
141 my $old_hold = Koha::Old::Holds->find( $hold->id );
143 is( $old_hold->id, $hold->id, 'reserve_id retained' );
144 is( $old_hold->priority, 0, 'priority set to 0' );
145 is( $old_hold->found, 'F', 'found set to F' );
147 subtest 'item_id parameter' => sub {
149 $category->reservefee(0)->store; # do not disturb later accounts
150 $hold = $builder->build_object({ class => 'Koha::Holds', value => { biblionumber => $biblio->id, borrowernumber => $patron->id, itemnumber => undef, priority => 1 } });
151 # Simulating checkout without confirming hold
152 $hold->fill({ item_id => $item->id });
153 $old_hold = Koha::Old::Holds->find($hold->id);
154 is( $old_hold->itemnumber, $item->itemnumber, 'The itemnumber has been saved in old_reserves by fill' );
156 $category->reservefee($fee)->store; # restore
159 subtest 'fee applied tests' => sub {
163 my $account = $patron->account;
164 is( $account->balance, $fee, 'Charge applied correctly' );
166 my $debits = $account->outstanding_debits;
167 is( $debits->count, 1, 'Only one fee charged' );
169 my $fee_debit = $debits->next;
170 is( $fee_debit->amount * 1, $fee, 'Fee amount stored correctly' );
171 is( $fee_debit->description, $title,
172 'Fee description stored correctly' );
173 is( $fee_debit->manager_id, $manager->id,
174 'Fee manager_id stored correctly' );
175 is( $fee_debit->branchcode, $manager->branchcode,
176 'Fee branchcode stored correctly' );
177 is( $fee_debit->interface, $interface,
178 'Fee interface stored correctly' );
179 is( $fee_debit->debit_type_code,
180 'RESERVE', 'Fee debit_type_code stored correctly' );
181 is( $fee_debit->itemnumber, $item->id,
182 'Fee itemnumber stored correctly' );
185 my $logs = Koha::ActionLogs->search(
193 is( $logs->count, 1, '1 log line added' );
195 # Set HoldFeeMode to something other than any_time_is_collected
196 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
198 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
200 $hold = $builder->build_object(
202 class => 'Koha::Holds',
204 biblionumber => $biblio->id,
205 borrowernumber => $patron->id,
206 itemnumber => $item->id,
214 my $account = $patron->account;
215 is( $account->balance, $fee, 'No new charge applied' );
217 my $debits = $account->outstanding_debits;
218 is( $debits->count, 1, 'Only one fee charged, because of HoldFeeMode' );
220 $logs = Koha::ActionLogs->search(
228 is( $logs->count, 0, 'HoldsLog disabled, no logs added' );
230 subtest 'anonymization behavior tests' => sub {
234 # reduce the tests noise
235 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
236 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
237 # unset AnonymousPatron
238 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
241 $patron->privacy(0)->store;
242 my $hold = $builder->build_object(
244 class => 'Koha::Holds',
245 value => { borrowernumber => $patron->id, found => undef }
249 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
250 $patron->borrowernumber, 'Patron link is kept' );
252 # 1 == "default", meaning it is not protected from removal
253 $patron->privacy(1)->store;
254 $hold = $builder->build_object(
256 class => 'Koha::Holds',
257 value => { borrowernumber => $patron->id, found => undef }
261 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
262 $patron->borrowernumber, 'Patron link is kept' );
264 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
265 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
266 # We need anonymous patron set to change patron privacy to never
267 # (2 == delete immediately)
268 # then we can undef for further tests
269 $patron->privacy(2)->store;
270 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
271 $hold = $builder->build_object(
273 class => 'Koha::Holds',
274 value => { borrowernumber => $patron->id, found => undef }
281 'AnonymousPatron not set, exception thrown';
283 $hold->discard_changes; # refresh from DB
285 ok( !$hold->is_found, 'Hold is not filled' );
287 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
289 $hold = $builder->build_object(
291 class => 'Koha::Holds',
292 value => { borrowernumber => $patron->id, found => undef }
297 Koha::Old::Holds->find( $hold->id )->borrowernumber,
298 $anonymous_patron->id,
299 'Patron link is set to the configured anonymous patron immediately'
303 subtest 'holds_queue update tests' => sub {
307 my $biblio = $builder->build_sample_biblio;
309 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
310 $mock->mock( 'enqueue', sub {
311 my ( $self, $args ) = @_;
315 '->fill triggers a holds queue update for the related biblio'
319 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
321 $builder->build_object(
323 class => 'Koha::Holds',
325 biblionumber => $biblio->id,
330 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
331 # this call shouldn't add a new test
332 $builder->build_object(
334 class => 'Koha::Holds',
336 biblionumber => $biblio->id,
342 $schema->storage->txn_rollback;
345 subtest 'patron() tests' => sub {
349 $schema->storage->txn_begin;
351 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
352 my $hold = $builder->build_object(
354 class => 'Koha::Holds',
356 borrowernumber => $patron->borrowernumber
361 my $hold_patron = $hold->patron;
362 is( ref($hold_patron), 'Koha::Patron', 'Right type' );
363 is( $hold_patron->id, $patron->id, 'Right object' );
365 $schema->storage->txn_rollback;
368 subtest 'set_pickup_location() tests' => sub {
372 $schema->storage->txn_begin;
374 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
375 my $mock_item = Test::MockModule->new('Koha::Item');
377 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
378 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
379 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
381 # let's control what Koha::Biblio->pickup_locations returns, for testing
382 $mock_biblio->mock( 'pickup_locations', sub {
383 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
385 # let's mock what Koha::Item->pickup_locations returns, for testing
386 $mock_item->mock( 'pickup_locations', sub {
387 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
390 my $biblio = $builder->build_sample_biblio;
391 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
393 # Test biblio-level holds
394 my $biblio_hold = $builder->build_object(
396 class => "Koha::Holds",
398 biblionumber => $biblio->biblionumber,
399 branchcode => $library_3->branchcode,
406 { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
407 'Koha::Exceptions::Hold::InvalidPickupLocation',
408 'Exception thrown on invalid pickup location';
410 $biblio_hold->discard_changes;
411 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
413 my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
414 is( ref($ret), 'Koha::Hold', 'self is returned' );
416 $biblio_hold->discard_changes;
417 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
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,
432 { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
433 'Koha::Exceptions::Hold::InvalidPickupLocation',
434 'Exception thrown on invalid pickup location';
436 $item_hold->discard_changes;
437 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
439 $item_hold->set_pickup_location({ library_id => $library_1->branchcode, force => 1 });
440 $item_hold->discard_changes;
441 is( $item_hold->branchcode, $library_1->branchcode, 'branchcode changed because of \'force\'' );
443 $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
444 is( ref($ret), 'Koha::Hold', 'self is returned' );
446 $item_hold->discard_changes;
447 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
450 { $item_hold->set_pickup_location({ library_id => undef }); }
451 'Koha::Exceptions::MissingParameter',
452 'Exception thrown if missing parameter';
454 like( "$@", qr/The library_id parameter is mandatory/, 'Exception message is clear' );
456 $schema->storage->txn_rollback;
459 subtest 'is_pickup_location_valid() tests' => sub {
463 $schema->storage->txn_begin;
465 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
466 my $mock_item = Test::MockModule->new('Koha::Item');
468 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
469 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
470 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
472 # let's control what Koha::Biblio->pickup_locations returns, for testing
473 $mock_biblio->mock( 'pickup_locations', sub {
474 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
476 # let's mock what Koha::Item->pickup_locations returns, for testing
477 $mock_item->mock( 'pickup_locations', sub {
478 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
481 my $biblio = $builder->build_sample_biblio;
482 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
484 # Test biblio-level holds
485 my $biblio_hold = $builder->build_object(
487 class => "Koha::Holds",
489 biblionumber => $biblio->biblionumber,
490 branchcode => $library_3->branchcode,
496 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
497 ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
499 # Test item-level holds
500 my $item_hold = $builder->build_object(
502 class => "Koha::Holds",
504 biblionumber => $biblio->biblionumber,
505 branchcode => $library_3->branchcode,
506 itemnumber => $item->itemnumber,
511 ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
512 ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
514 subtest 'pickup_locations() returning ->empty' => sub {
518 $schema->storage->txn_begin;
520 my $library = $builder->build_object({ class => 'Koha::Libraries' });
522 my $mock_item = Test::MockModule->new('Koha::Item');
523 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
525 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
526 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
528 my $item = $builder->build_sample_item();
529 my $biblio = $item->biblio;
531 # Test biblio-level holds
532 my $biblio_hold = $builder->build_object(
534 class => "Koha::Holds",
536 biblionumber => $biblio->biblionumber,
542 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
544 # Test item-level holds
545 my $item_hold = $builder->build_object(
547 class => "Koha::Holds",
549 biblionumber => $biblio->biblionumber,
550 itemnumber => $item->itemnumber,
555 ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
557 $schema->storage->txn_rollback;
560 $schema->storage->txn_rollback;
563 subtest 'cancel() tests' => sub {
567 $schema->storage->txn_begin;
569 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
571 # reduce the tests noise
572 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
573 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',
576 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
579 $patron->privacy(0)->store;
580 my $hold = $builder->build_object(
582 class => 'Koha::Holds',
583 value => { borrowernumber => $patron->id, found => undef }
587 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
588 $patron->borrowernumber, 'Patron link is kept' );
590 # 1 == "default", meaning it is not protected from removal
591 $patron->privacy(1)->store;
592 $hold = $builder->build_object(
594 class => 'Koha::Holds',
595 value => { borrowernumber => $patron->id, found => undef }
599 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
600 $patron->borrowernumber, 'Patron link is kept' );
602 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
603 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
604 # We need anonymous patron set to change patron privacy to never
605 # (2 == delete immediately)
606 # then we can undef for further tests
607 $patron->privacy(2)->store;
608 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
609 $hold = $builder->build_object(
611 class => 'Koha::Holds',
612 value => { borrowernumber => $patron->id, found => undef }
618 'AnonymousPatron not set, exception thrown';
620 $hold->discard_changes;
622 ok( !$hold->is_found, 'Hold is not cancelled' );
624 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
626 $hold = $builder->build_object(
628 class => 'Koha::Holds',
629 value => { borrowernumber => $patron->id, found => undef }
634 Koha::Old::Holds->find( $hold->id )->borrowernumber,
635 $anonymous_patron->id,
636 'Patron link is set to the configured anonymous patron immediately'
639 subtest 'holds_queue update tests' => sub {
643 my $biblio = $builder->build_sample_biblio;
645 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
647 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
648 $mock->mock( 'enqueue', sub {
649 my ( $self, $args ) = @_;
653 '->cancel triggers a holds queue update for the related biblio'
657 $builder->build_object(
659 class => 'Koha::Holds',
661 biblionumber => $biblio->id,
666 # If the skip_holds_queue param is not honoured, then test count will fail.
667 $builder->build_object(
669 class => 'Koha::Holds',
671 biblionumber => $biblio->id,
674 )->cancel({ skip_holds_queue => 1 });
676 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
678 $builder->build_object(
680 class => 'Koha::Holds',
682 biblionumber => $biblio->id,
685 )->cancel({ skip_holds_queue => 0 });
688 $schema->storage->txn_rollback;
691 subtest 'suspend_hold() and resume() tests' => sub {
695 $schema->storage->txn_begin;
697 my $biblio = $builder->build_sample_biblio;
700 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
702 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
703 $mock->mock( 'enqueue', sub {
704 my ( $self, $args ) = @_;
708 "->$action triggers a holds queue update for the related biblio"
712 my $hold = $builder->build_object(
714 class => 'Koha::Holds',
716 biblionumber => $biblio->id,
722 $action = 'suspend_hold';
728 $schema->storage->txn_rollback;
731 subtest 'cancellation_requests(), add_cancellation_request() and cancellation_requested() tests' => sub {
735 $schema->storage->txn_begin;
737 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
739 my $hold = $builder->build_object( { class => 'Koha::Holds', } );
741 is( $hold->cancellation_requests->count, 0 );
742 ok( !$hold->cancellation_requested );
744 # Add two cancellation requests
745 my $request_1 = $hold->add_cancellation_request;
746 isnt( $request_1->creation_date, undef, 'creation_date is set' );
748 my $requester = $builder->build_object( { class => 'Koha::Patrons' } );
749 my $creation_date = '2021-06-25 14:05:35';
751 my $request_2 = $hold->add_cancellation_request(
753 creation_date => $creation_date,
757 is( $request_2->creation_date, $creation_date, 'Passed creation_date set' );
759 is( $hold->cancellation_requests->count, 2 );
760 ok( $hold->cancellation_requested );
762 $schema->storage->txn_rollback;
765 subtest 'cancellation_requestable_from_opac() tests' => sub {
769 $schema->storage->txn_begin;
772 $builder->build_object( { class => 'Koha::Patron::Categories' } );
773 my $item_home_library =
774 $builder->build_object( { class => 'Koha::Libraries' } );
775 my $patron_home_library =
776 $builder->build_object( { class => 'Koha::Libraries' } );
779 $builder->build_sample_item( { library => $item_home_library->id } );
780 my $patron = $builder->build_object(
782 class => 'Koha::Patrons',
783 value => { branchcode => $patron_home_library->id }
787 subtest 'Exception cases' => sub {
791 my $hold = $builder->build_object(
793 class => 'Koha::Holds',
797 borrowernumber => $patron->id
802 throws_ok { $hold->cancellation_requestable_from_opac; }
803 'Koha::Exceptions::InvalidStatus',
804 'Exception thrown because hold is not waiting';
806 is( $@->invalid_status, 'hold_not_waiting' );
808 $hold = $builder->build_object(
810 class => 'Koha::Holds',
814 borrowernumber => $patron->id
819 throws_ok { $hold->cancellation_requestable_from_opac; }
820 'Koha::Exceptions::InvalidStatus',
821 'Exception thrown because waiting hold has no item linked';
823 is( $@->invalid_status, 'no_item_linked' );
826 # set default rule to enabled
827 Koha::CirculationRules->set_rule(
832 rule_name => 'waiting_hold_cancellation',
837 my $hold = $builder->build_object(
839 class => 'Koha::Holds',
841 itemnumber => $item->id,
843 borrowernumber => $patron->id
848 t::lib::Mocks::mock_preference( 'ReservesControlBranch',
851 Koha::CirculationRules->set_rule(
853 categorycode => $patron->categorycode,
854 itemtype => $item->itype,
855 branchcode => $item->homebranch,
856 rule_name => 'waiting_hold_cancellation',
861 ok( !$hold->cancellation_requestable_from_opac );
863 Koha::CirculationRules->set_rule(
865 categorycode => $patron->categorycode,
866 itemtype => $item->itype,
867 branchcode => $item->homebranch,
868 rule_name => 'waiting_hold_cancellation',
874 $hold->cancellation_requestable_from_opac,
875 'Make sure it is picking the right circulation rule'
878 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
880 Koha::CirculationRules->set_rule(
882 categorycode => $patron->categorycode,
883 itemtype => $item->itype,
884 branchcode => $patron->branchcode,
885 rule_name => 'waiting_hold_cancellation',
890 ok( !$hold->cancellation_requestable_from_opac );
892 Koha::CirculationRules->set_rule(
894 categorycode => $patron->categorycode,
895 itemtype => $item->itype,
896 branchcode => $patron->branchcode,
897 rule_name => 'waiting_hold_cancellation',
903 $hold->cancellation_requestable_from_opac,
904 'Make sure it is picking the right circulation rule'
907 $schema->storage->txn_rollback;
910 subtest 'can_update_pickup_location_opac() tests' => sub {
914 $schema->storage->txn_begin;
916 my $hold = $builder->build_object(
917 { class => 'Koha::Holds',
918 value => { found => undef, suspend => 0, suspend_until => undef, waitingdate => undef }
922 t::lib::Mocks::mock_preference( 'OPACAllowUserToChangeBranch', '' );
924 is( $hold->can_update_pickup_location_opac, 0, "Pending hold pickup can't be changed (No change allowed)" );
927 is( $hold->can_update_pickup_location_opac, 0, "In transit hold pickup can't be changed (No change allowed)" );
930 is( $hold->can_update_pickup_location_opac, 0, "Waiting hold pickup can't be changed (No change allowed)" );
933 my $dt = dt_from_string();
935 $hold->suspend_hold( $dt );
936 is( $hold->can_update_pickup_location_opac, 0, "Suspended hold pickup can't be changed (No change allowed)" );
939 t::lib::Mocks::mock_preference( 'OPACAllowUserToChangeBranch', 'pending,intransit,suspended' );
941 is( $hold->can_update_pickup_location_opac, 1, "Pending hold pickup can be changed (pending,intransit,suspended allowed)" );
944 is( $hold->can_update_pickup_location_opac, 1, "In transit hold pickup can be changed (pending,intransit,suspended allowed)" );
947 is( $hold->can_update_pickup_location_opac, 0, "Waiting hold pickup can't be changed (pending,intransit,suspended allowed)" );
950 $dt = dt_from_string();
951 $hold->suspend_hold( $dt );
952 is( $hold->can_update_pickup_location_opac, 1, "Suspended hold pickup can be changed (pending,intransit,suspended allowed)" );
954 $schema->storage->txn_rollback;
957 subtest 'Koha::Hold::item_group tests' => sub {
961 $schema->storage->txn_begin;
963 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
964 my $category = $builder->build_object(
966 class => 'Koha::Patron::Categories',
967 value => { exclude_from_local_holds_priority => 0 }
970 my $patron = $builder->build_object(
972 class => "Koha::Patrons",
974 branchcode => $library->branchcode,
975 categorycode => $category->categorycode
979 my $biblio = $builder->build_sample_biblio();
982 Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
984 my $hold = $builder->build_object(
986 class => "Koha::Holds",
988 borrowernumber => $patron->borrowernumber,
989 biblionumber => $biblio->biblionumber,
991 item_group_id => $item_group->id,
996 is( $hold->item_group->id, $item_group->id, "Got correct item group" );
998 $schema->storage->txn_rollback;
1001 subtest 'change_type tests' => sub {
1005 $schema->storage->txn_begin;
1007 my $item = $builder->build_object( { class => 'Koha::Items', } );
1008 my $hold = $builder->build_object( {
1009 class => 'Koha::Holds',
1011 itemnumber => undef,
1012 item_level_hold => 0,
1016 my $hold2 = $builder->build_object( {
1017 class => 'Koha::Holds',
1019 borrowernumber => $hold->borrowernumber,
1023 ok( $hold->change_type );
1025 $hold->discard_changes;
1027 is( $hold->itemnumber, undef, 'record hold to record hold, no changes');
1029 is( $hold->item_level_hold, 0, 'item_level_hold=0' );
1031 ok( $hold->change_type( $item->itemnumber ) );
1033 $hold->discard_changes;
1035 is( $hold->itemnumber, $item->itemnumber, 'record hold to item hold');
1037 is( $hold->item_level_hold, 1, 'item_level_hold=1' );
1039 ok( $hold->change_type( $item->itemnumber ) );
1041 $hold->discard_changes;
1043 is( $hold->itemnumber, $item->itemnumber, 'item hold to item hold, no changes');
1045 is( $hold->item_level_hold, 1, 'item_level_hold=1' );
1047 ok( $hold->change_type );
1049 $hold->discard_changes;
1051 is( $hold->itemnumber, undef, 'item hold to record hold');
1053 is( $hold->item_level_hold, 0, 'item_level_hold=0' );
1055 my $hold3 = $builder->build_object( {
1056 class => 'Koha::Holds',
1058 biblionumber => $hold->biblionumber,
1059 borrowernumber => $hold->borrowernumber,
1063 throws_ok { $hold->change_type }
1064 'Koha::Exceptions::Hold::CannotChangeHoldType',
1065 'Exception thrown because more than one hold per record';
1067 $schema->storage->txn_rollback;