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 => 5;
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' );
176 $patron->privacy(0)->store;
177 my $hold = $builder->build_object(
179 class => 'Koha::Holds',
180 value => { borrowernumber => $patron->id, status => undef }
184 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
185 $patron->borrowernumber, 'Patron link is kept' );
187 # 1 == "default", meaning it is not protected from removal
188 $patron->privacy(1)->store;
189 $hold = $builder->build_object(
191 class => 'Koha::Holds',
192 value => { borrowernumber => $patron->id, status => undef }
196 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
197 $patron->borrowernumber, 'Patron link is kept' );
199 # 2 == delete immediately
200 $patron->privacy(2)->store;
201 $hold = $builder->build_object(
203 class => 'Koha::Holds',
204 value => { borrowernumber => $patron->id, status => undef }
208 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
209 undef, 'Patron link is deleted immediately' );
211 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
212 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
214 $hold = $builder->build_object(
216 class => 'Koha::Holds',
217 value => { borrowernumber => $patron->id, status => undef }
222 Koha::Old::Holds->find( $hold->id )->borrowernumber,
223 $anonymous_patron->id,
224 'Patron link is set to the configured anonymous patron immediately'
228 $schema->storage->txn_rollback;
231 subtest 'patron() tests' => sub {
235 $schema->storage->txn_begin;
237 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
238 my $hold = $builder->build_object(
240 class => 'Koha::Holds',
242 borrowernumber => $patron->borrowernumber
247 my $hold_patron = $hold->patron;
248 is( ref($hold_patron), 'Koha::Patron', 'Right type' );
249 is( $hold_patron->id, $patron->id, 'Right object' );
251 $schema->storage->txn_rollback;
254 subtest 'set_pickup_location() tests' => sub {
258 $schema->storage->txn_begin;
260 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
261 my $mock_item = Test::MockModule->new('Koha::Item');
263 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
264 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
265 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
267 # let's control what Koha::Biblio->pickup_locations returns, for testing
268 $mock_biblio->mock( 'pickup_locations', sub {
269 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
271 # let's mock what Koha::Item->pickup_locations returns, for testing
272 $mock_item->mock( 'pickup_locations', sub {
273 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
276 my $biblio = $builder->build_sample_biblio;
277 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
279 # Test biblio-level holds
280 my $biblio_hold = $builder->build_object(
282 class => "Koha::Holds",
284 biblionumber => $biblio->biblionumber,
285 branchcode => $library_3->branchcode,
292 { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
293 'Koha::Exceptions::Hold::InvalidPickupLocation',
294 'Exception thrown on invalid pickup location';
296 $biblio_hold->discard_changes;
297 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
299 my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
300 is( ref($ret), 'Koha::Hold', 'self is returned' );
302 $biblio_hold->discard_changes;
303 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
305 # Test item-level holds
306 my $item_hold = $builder->build_object(
308 class => "Koha::Holds",
310 biblionumber => $biblio->biblionumber,
311 branchcode => $library_3->branchcode,
312 itemnumber => $item->itemnumber,
318 { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
319 'Koha::Exceptions::Hold::InvalidPickupLocation',
320 'Exception thrown on invalid pickup location';
322 $item_hold->discard_changes;
323 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
325 $item_hold->set_pickup_location({ library_id => $library_1->branchcode, force => 1 });
326 $item_hold->discard_changes;
327 is( $item_hold->branchcode, $library_1->branchcode, 'branchcode changed because of \'force\'' );
329 $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
330 is( ref($ret), 'Koha::Hold', 'self is returned' );
332 $item_hold->discard_changes;
333 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
336 { $item_hold->set_pickup_location({ library_id => undef }); }
337 'Koha::Exceptions::MissingParameter',
338 'Exception thrown if missing parameter';
340 is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
342 $schema->storage->txn_rollback;
345 subtest 'is_pickup_location_valid() tests' => sub {
349 $schema->storage->txn_begin;
351 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
352 my $mock_item = Test::MockModule->new('Koha::Item');
354 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
355 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
356 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
358 # let's control what Koha::Biblio->pickup_locations returns, for testing
359 $mock_biblio->mock( 'pickup_locations', sub {
360 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
362 # let's mock what Koha::Item->pickup_locations returns, for testing
363 $mock_item->mock( 'pickup_locations', sub {
364 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
367 my $biblio = $builder->build_sample_biblio;
368 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
370 # Test biblio-level holds
371 my $biblio_hold = $builder->build_object(
373 class => "Koha::Holds",
375 biblionumber => $biblio->biblionumber,
376 branchcode => $library_3->branchcode,
382 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
383 ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
385 # Test item-level holds
386 my $item_hold = $builder->build_object(
388 class => "Koha::Holds",
390 biblionumber => $biblio->biblionumber,
391 branchcode => $library_3->branchcode,
392 itemnumber => $item->itemnumber,
397 ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
398 ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
400 subtest 'pickup_locations() returning ->empty' => sub {
404 $schema->storage->txn_begin;
406 my $library = $builder->build_object({ class => 'Koha::Libraries' });
408 my $mock_item = Test::MockModule->new('Koha::Item');
409 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
411 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
412 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
414 my $item = $builder->build_sample_item();
415 my $biblio = $item->biblio;
417 # Test biblio-level holds
418 my $biblio_hold = $builder->build_object(
420 class => "Koha::Holds",
422 biblionumber => $biblio->biblionumber,
428 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
430 # Test item-level holds
431 my $item_hold = $builder->build_object(
433 class => "Koha::Holds",
435 biblionumber => $biblio->biblionumber,
436 itemnumber => $item->itemnumber,
441 ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
443 $schema->storage->txn_rollback;
446 $schema->storage->txn_rollback;
449 subtest 'cancel() tests' => sub {
453 $schema->storage->txn_begin;
455 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
457 # reduce the tests noise
458 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
459 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',
462 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
465 $patron->privacy(0)->store;
466 my $hold = $builder->build_object(
468 class => 'Koha::Holds',
469 value => { borrowernumber => $patron->id, status => undef }
473 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
474 $patron->borrowernumber, 'Patron link is kept' );
476 # 1 == "default", meaning it is not protected from removal
477 $patron->privacy(1)->store;
478 $hold = $builder->build_object(
480 class => 'Koha::Holds',
481 value => { borrowernumber => $patron->id, status => undef }
485 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
486 $patron->borrowernumber, 'Patron link is kept' );
488 # 2 == delete immediately
489 $patron->privacy(2)->store;
490 $hold = $builder->build_object(
492 class => 'Koha::Holds',
493 value => { borrowernumber => $patron->id, status => undef }
497 is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
498 undef, 'Patron link is deleted immediately' );
500 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
501 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
503 $hold = $builder->build_object(
505 class => 'Koha::Holds',
506 value => { borrowernumber => $patron->id, status => undef }
511 Koha::Old::Holds->find( $hold->id )->borrowernumber,
512 $anonymous_patron->id,
513 'Patron link is set to the configured anonymous patron immediately'
516 $schema->storage->txn_rollback;