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;
27 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'fill() tests' => sub {
41 $schema->storage->txn_begin;
45 my $category = $builder->build_object(
47 class => 'Koha::Patron::Categories',
48 value => { reservefee => $fee }
51 my $patron = $builder->build_object(
53 class => 'Koha::Patrons',
54 value => { categorycode => $category->id }
57 my $manager = $builder->build_object( { class => 'Koha::Patrons' } );
59 my $title = 'Do what you want';
60 my $biblio = $builder->build_sample_biblio( { title => $title } );
61 my $item = $builder->build_sample_item( { biblionumber => $biblio->id } );
62 my $hold = $builder->build_object(
64 class => 'Koha::Holds',
66 biblionumber => $biblio->id,
67 borrowernumber => $patron->id,
68 itemnumber => $item->id,
74 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'any_time_is_collected' );
75 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
76 t::lib::Mocks::mock_userenv(
77 { patron => $manager, branchcode => $manager->branchcode } );
79 my $interface = 'api';
80 C4::Context->interface($interface);
82 my $ret = $hold->fill;
84 is( ref($ret), 'Koha::Hold', '->fill returns the object type' );
85 is( $ret->id, $hold->id, '->fill returns the object' );
87 is( Koha::Holds->find($hold->id), undef, 'Hold no longer current' );
88 my $old_hold = Koha::Old::Holds->find( $hold->id );
90 is( $old_hold->id, $hold->id, 'reserve_id retained' );
91 is( $old_hold->priority, 0, 'priority set to 0' );
92 is( $old_hold->found, 'F', 'found set to F' );
94 subtest 'fee applied tests' => sub {
98 my $account = $patron->account;
99 is( $account->balance, $fee, 'Charge applied correctly' );
101 my $debits = $account->outstanding_debits;
102 is( $debits->count, 1, 'Only one fee charged' );
104 my $fee_debit = $debits->next;
105 is( $fee_debit->amount * 1, $fee, 'Fee amount stored correctly' );
106 is( $fee_debit->description, $title,
107 'Fee description stored correctly' );
108 is( $fee_debit->manager_id, $manager->id,
109 'Fee manager_id stored correctly' );
110 is( $fee_debit->branchcode, $manager->branchcode,
111 'Fee branchcode stored correctly' );
112 is( $fee_debit->interface, $interface,
113 'Fee interface stored correctly' );
114 is( $fee_debit->debit_type_code,
115 'RESERVE', 'Fee debit_type_code stored correctly' );
116 is( $fee_debit->itemnumber, $item->id,
117 'Fee itemnumber stored correctly' );
120 my $logs = Koha::ActionLogs->search(
128 is( $logs->count, 1, '1 log line added' );
130 # Set HoldFeeMode to something other than any_time_is_collected
131 t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
133 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
135 $hold = $builder->build_object(
137 class => 'Koha::Holds',
139 biblionumber => $biblio->id,
140 borrowernumber => $patron->id,
141 itemnumber => $item->id,
149 my $account = $patron->account;
150 is( $account->balance, $fee, 'No new charge applied' );
152 my $debits = $account->outstanding_debits;
153 is( $debits->count, 1, 'Only one fee charged, because of HoldFeeMode' );
155 $logs = Koha::ActionLogs->search(
163 is( $logs->count, 0, 'HoldsLog disabled, no logs added' );
165 $schema->storage->txn_rollback;
168 subtest 'patron() tests' => sub {
172 $schema->storage->txn_begin;
174 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
175 my $hold = $builder->build_object(
177 class => 'Koha::Holds',
179 borrowernumber => $patron->borrowernumber
184 my $hold_patron = $hold->patron;
185 is( ref($hold_patron), 'Koha::Patron', 'Right type' );
186 is( $hold_patron->id, $patron->id, 'Right object' );
188 $schema->storage->txn_rollback;
191 subtest 'set_pickup_location() tests' => sub {
195 $schema->storage->txn_begin;
197 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
198 my $mock_item = Test::MockModule->new('Koha::Item');
200 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
201 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
202 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
204 # let's control what Koha::Biblio->pickup_locations returns, for testing
205 $mock_biblio->mock( 'pickup_locations', sub {
206 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
208 # let's mock what Koha::Item->pickup_locations returns, for testing
209 $mock_item->mock( 'pickup_locations', sub {
210 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
213 my $biblio = $builder->build_sample_biblio;
214 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
216 # Test biblio-level holds
217 my $biblio_hold = $builder->build_object(
219 class => "Koha::Holds",
221 biblionumber => $biblio->biblionumber,
222 branchcode => $library_3->branchcode,
229 { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
230 'Koha::Exceptions::Hold::InvalidPickupLocation',
231 'Exception thrown on invalid pickup location';
233 $biblio_hold->discard_changes;
234 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
236 my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
237 is( ref($ret), 'Koha::Hold', 'self is returned' );
239 $biblio_hold->discard_changes;
240 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
242 # Test item-level holds
243 my $item_hold = $builder->build_object(
245 class => "Koha::Holds",
247 biblionumber => $biblio->biblionumber,
248 branchcode => $library_3->branchcode,
249 itemnumber => $item->itemnumber,
255 { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
256 'Koha::Exceptions::Hold::InvalidPickupLocation',
257 'Exception thrown on invalid pickup location';
259 $item_hold->discard_changes;
260 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
262 $item_hold->set_pickup_location({ library_id => $library_1->branchcode, force => 1 });
263 $item_hold->discard_changes;
264 is( $item_hold->branchcode, $library_1->branchcode, 'branchcode changed because of \'force\'' );
266 $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
267 is( ref($ret), 'Koha::Hold', 'self is returned' );
269 $item_hold->discard_changes;
270 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
273 { $item_hold->set_pickup_location({ library_id => undef }); }
274 'Koha::Exceptions::MissingParameter',
275 'Exception thrown if missing parameter';
277 is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
279 $schema->storage->txn_rollback;
282 subtest 'is_pickup_location_valid() tests' => sub {
286 $schema->storage->txn_begin;
288 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
289 my $mock_item = Test::MockModule->new('Koha::Item');
291 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
292 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
293 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
295 # let's control what Koha::Biblio->pickup_locations returns, for testing
296 $mock_biblio->mock( 'pickup_locations', sub {
297 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
299 # let's mock what Koha::Item->pickup_locations returns, for testing
300 $mock_item->mock( 'pickup_locations', sub {
301 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
304 my $biblio = $builder->build_sample_biblio;
305 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
307 # Test biblio-level holds
308 my $biblio_hold = $builder->build_object(
310 class => "Koha::Holds",
312 biblionumber => $biblio->biblionumber,
313 branchcode => $library_3->branchcode,
319 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
320 ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
322 # Test item-level holds
323 my $item_hold = $builder->build_object(
325 class => "Koha::Holds",
327 biblionumber => $biblio->biblionumber,
328 branchcode => $library_3->branchcode,
329 itemnumber => $item->itemnumber,
334 ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
335 ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
337 subtest 'pickup_locations() returning ->empty' => sub {
341 $schema->storage->txn_begin;
343 my $library = $builder->build_object({ class => 'Koha::Libraries' });
345 my $mock_item = Test::MockModule->new('Koha::Item');
346 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
348 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
349 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
351 my $item = $builder->build_sample_item();
352 my $biblio = $item->biblio;
354 # Test biblio-level holds
355 my $biblio_hold = $builder->build_object(
357 class => "Koha::Holds",
359 biblionumber => $biblio->biblionumber,
365 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
367 # Test item-level holds
368 my $item_hold = $builder->build_object(
370 class => "Koha::Holds",
372 biblionumber => $biblio->biblionumber,
373 itemnumber => $item->itemnumber,
378 ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
380 $schema->storage->txn_rollback;
383 $schema->storage->txn_rollback;