3 # Copyright 2019 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>.
23 use Test::More tests => 30;
27 use C4::Biblio qw( GetMarcSubfieldStructure );
28 use C4::Circulation qw( AddIssue AddReturn );
33 use Koha::DateUtils qw( dt_from_string );
36 use Koha::AuthorisedValues;
38 use List::MoreUtils qw(all);
40 use t::lib::TestBuilder;
44 my $schema = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new;
47 subtest 'return_claims relationship' => sub {
50 $schema->storage->txn_begin;
52 my $biblio = $builder->build_sample_biblio();
53 my $item = $builder->build_sample_item({
54 biblionumber => $biblio->biblionumber,
56 my $return_claims = $item->return_claims;
57 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
58 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
59 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
60 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
62 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
64 $schema->storage->txn_rollback;
67 subtest 'return_claim accessor' => sub {
70 $schema->storage->txn_begin;
72 my $biblio = $builder->build_sample_biblio();
73 my $item = $builder->build_sample_item({
74 biblionumber => $biblio->biblionumber,
76 my $return_claim = $item->return_claim;
77 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
79 my $claim1 = $builder->build_object(
81 class => 'Koha::Checkouts::ReturnClaims',
82 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
85 my $claim2 = $builder->build_object(
87 class => 'Koha::Checkouts::ReturnClaims',
88 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
92 $return_claim = $item->return_claim;
93 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
94 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
96 $claim2->resolution('test')->store();
97 $return_claim = $item->return_claim;
98 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
100 $claim1->resolution('test')->store();
101 $return_claim = $item->return_claim;
102 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
104 $schema->storage->txn_rollback;
107 subtest 'tracked_links relationship' => sub {
110 my $biblio = $builder->build_sample_biblio();
111 my $item = $builder->build_sample_item({
112 biblionumber => $biblio->biblionumber,
114 my $tracked_links = $item->tracked_links;
115 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
116 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
117 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
118 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
120 is($item->tracked_links()->count,2,"Two tracked links found");
123 subtest 'is_bundle tests' => sub {
126 $schema->storage->txn_begin;
128 my $item = $builder->build_sample_item();
130 my $is_bundle = $item->is_bundle;
131 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
133 my $item2 = $builder->build_sample_item();
134 $schema->resultset('ItemBundle')
135 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
137 $is_bundle = $item->is_bundle;
138 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
140 $schema->storage->txn_rollback;
143 subtest 'in_bundle tests' => sub {
146 $schema->storage->txn_begin;
148 my $item = $builder->build_sample_item();
150 my $in_bundle = $item->in_bundle;
151 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
153 my $host_item = $builder->build_sample_item();
154 $schema->resultset('ItemBundle')
155 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
157 $in_bundle = $item->in_bundle;
158 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
160 $schema->storage->txn_rollback;
163 subtest 'bundle_items tests' => sub {
166 $schema->storage->txn_begin;
168 my $host_item = $builder->build_sample_item();
169 my $bundle_items = $host_item->bundle_items;
170 is( ref($bundle_items), 'Koha::Items',
171 'bundle_items returns a Koha::Items object set' );
172 is( $bundle_items->count, 0,
173 'bundle_items set is empty when no items are bundled' );
175 my $bundle_item1 = $builder->build_sample_item();
176 my $bundle_item2 = $builder->build_sample_item();
177 my $bundle_item3 = $builder->build_sample_item();
178 $schema->resultset('ItemBundle')
180 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
181 $schema->resultset('ItemBundle')
183 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
184 $schema->resultset('ItemBundle')
186 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
188 $bundle_items = $host_item->bundle_items;
189 is( $bundle_items->count, 3,
190 'bundle_items returns all the bundled items in the set' );
192 $schema->storage->txn_rollback;
195 subtest 'bundle_host tests' => sub {
198 $schema->storage->txn_begin;
200 my $host_item = $builder->build_sample_item();
201 my $bundle_item1 = $builder->build_sample_item();
202 my $bundle_item2 = $builder->build_sample_item();
203 $schema->resultset('ItemBundle')
205 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
207 my $bundle_host = $bundle_item1->bundle_host;
208 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
209 $bundle_host = $bundle_item2->bundle_host;
210 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
211 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
213 $schema->storage->txn_rollback;
216 subtest 'add_to_bundle tests' => sub {
219 $schema->storage->txn_begin;
221 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
223 my $library = $builder->build_object({ class => 'Koha::Libraries' });
224 t::lib::Mocks::mock_userenv({
225 branchcode => $library->branchcode
228 my $host_item = $builder->build_sample_item();
229 my $bundle_item1 = $builder->build_sample_item();
230 my $bundle_item2 = $builder->build_sample_item();
231 my $bundle_item3 = $builder->build_sample_item();
233 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
235 throws_ok { $host_item->add_to_bundle($host_item) }
236 'Koha::Exceptions::Item::Bundle::IsBundle',
237 'Exception thrown if you try to add the item to itself';
239 my $reserve_id = C4::Reserves::AddReserve(
241 branchcode => $library->branchcode,
242 borrowernumber => $patron->borrowernumber,
243 biblionumber => $bundle_item3->biblionumber,
244 itemnumber => $bundle_item3->itemnumber,
247 throws_ok { $host_item->add_to_bundle($bundle_item3) }
248 'Koha::Exceptions::Item::Bundle::ItemHasHolds',
249 'Exception thrown if you try to add an item with holds to a bundle';
251 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
252 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
254 throws_ok { $host_item->add_to_bundle($bundle_item1) }
255 'Koha::Exceptions::Object::DuplicateID',
256 'Exception thrown if you try to add the same item twice';
258 throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
259 'Koha::Exceptions::Item::Bundle::IsBundle',
260 'Exception thrown if you try to add an item to a bundled item';
262 throws_ok { $bundle_item2->add_to_bundle($host_item) }
263 'Koha::Exceptions::Item::Bundle::IsBundle',
264 'Exception thrown if you try to add a bundle host to a bundle item';
266 C4::Circulation::AddIssue( $patron->unblessed, $host_item->barcode );
267 throws_ok { $host_item->add_to_bundle($bundle_item2) }
268 'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut',
269 'Exception thrown if you try to add an item to a checked out bundle';
270 C4::Circulation::AddReturn( $host_item->barcode, $host_item->homebranch );
271 $host_item->discard_changes;
273 C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
274 throws_ok { $host_item->add_to_bundle($bundle_item2) }
275 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
276 'Exception thrown if you try to add a checked out item';
278 $bundle_item2->withdrawn(1)->store;
279 t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
280 throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
281 'Koha::Exceptions::Checkin::FailedCheckin',
282 'Exception thrown if you try to add a checked out item using
283 "force_checkin" and the return is not possible';
285 $bundle_item2->withdrawn(0)->store;
286 lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
287 'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
289 $bundle_item2->discard_changes;
290 ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
292 $schema->storage->txn_rollback;
295 subtest 'remove_from_bundle tests' => sub {
298 $schema->storage->txn_begin;
300 my $host_item = $builder->build_sample_item();
301 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
302 $host_item->add_to_bundle($bundle_item1);
304 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
305 t::lib::Mocks::mock_userenv( { branchcode => $patron->branchcode } );
307 C4::Circulation::AddIssue( $patron->unblessed, $host_item->barcode );
308 throws_ok { $bundle_item1->remove_from_bundle }
309 'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut',
310 'Exception thrown if you try to add an item to a checked out bundle';
311 my ( $doreturn, $messages, $issue ) = C4::Circulation::AddReturn( $host_item->barcode, $host_item->homebranch );
312 $bundle_item1->discard_changes;
314 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
315 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
316 $bundle_item1->discard_changes;
317 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
319 $schema->storage->txn_rollback;
322 subtest 'hidden_in_opac() tests' => sub {
326 $schema->storage->txn_begin;
328 my $item = $builder->build_sample_item({ itemlost => 2 });
331 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
332 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
334 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
335 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
337 # enable hidelostitems to verify correct behaviour
338 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
339 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
341 # disable hidelostitems
342 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
343 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
345 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
347 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
351 $schema->storage->txn_rollback;
354 subtest 'has_pending_hold() tests' => sub {
358 $schema->storage->txn_begin;
360 my $dbh = C4::Context->dbh;
361 my $item = $builder->build_sample_item({ itemlost => 0 });
362 my $itemnumber = $item->itemnumber;
364 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
365 my $borrowernumber = $patron->id;
368 "INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',$borrowernumber,$itemnumber)");
369 ok( $item->has_pending_hold, "Yes, we have a pending hold");
370 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
371 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
373 $schema->storage->txn_rollback;
376 subtest "as_marc_field() tests" => sub {
378 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
379 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
381 my @schema_columns = $schema->resultset('Item')->result_source->columns;
382 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
384 plan tests => scalar @mapped_columns + 5;
386 $schema->storage->txn_begin;
388 my $item = $builder->build_sample_item;
389 # Make sure it has at least one undefined attribute
390 $item->set({ replacementprice => undef })->store->discard_changes;
392 my $marc_field = $item->as_marc_field;
397 'Generated field set the right tag number'
400 foreach my $column (@mapped_columns) {
401 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
402 is( $marc_field->subfield($tagsubfield),
403 $item->$column, "Value is mapped correctly for column $column" );
406 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
409 tagfield => $itemtag,
413 Koha::MarcSubfieldStructure->new(
416 tagfield => $itemtag,
422 my @unlinked_subfields;
423 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
424 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
426 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
427 Koha::MarcSubfieldStructures->search(
428 { frameworkcode => '', tagfield => $itemtag } )
429 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
431 $marc_field = $item->as_marc_field;
433 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
434 my @subfields = $marc_field->subfields;
435 my $result = all { defined $_->[1] } @subfields;
436 ok( $result, 'There are no undef subfields' );
437 my @ordered_subfields = sort {
438 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
439 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
441 is_deeply(\@subfields, \@ordered_subfields);
443 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
444 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
446 $schema->storage->txn_rollback;
447 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
450 subtest 'pickup_locations() tests' => sub {
454 $schema->storage->txn_begin;
456 my $dbh = C4::Context->dbh;
458 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
459 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
460 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
461 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
462 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
463 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
464 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
465 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
467 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
468 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
471 $library1->branchcode, $library2->branchcode,
472 $library3->branchcode, $library4->branchcode
475 my $item1 = $builder->build_sample_item(
477 homebranch => $library1->branchcode,
478 holdingbranch => $library2->branchcode,
484 my $item3 = $builder->build_sample_item(
486 homebranch => $library3->branchcode,
487 holdingbranch => $library4->branchcode,
489 itype => $item1->itype,
493 Koha::CirculationRules->set_rules(
495 categorycode => undef,
496 itemtype => $item1->itype,
499 reservesallowed => 25,
505 { $item1->pickup_locations }
506 'Koha::Exceptions::MissingParameter',
507 'Exception thrown on missing parameter';
509 is( $@->parameter, 'patron', 'Exception param correctly set' );
511 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
512 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
515 "1-1-from_home_library-any" => 3,
516 "1-1-from_home_library-holdgroup" => 2,
517 "1-1-from_home_library-patrongroup" => 2,
518 "1-1-from_home_library-homebranch" => 1,
519 "1-1-from_home_library-holdingbranch" => 1,
520 "1-1-from_any_library-any" => 3,
521 "1-1-from_any_library-holdgroup" => 2,
522 "1-1-from_any_library-patrongroup" => 2,
523 "1-1-from_any_library-homebranch" => 1,
524 "1-1-from_any_library-holdingbranch" => 1,
525 "1-1-from_local_hold_group-any" => 3,
526 "1-1-from_local_hold_group-holdgroup" => 2,
527 "1-1-from_local_hold_group-patrongroup" => 2,
528 "1-1-from_local_hold_group-homebranch" => 1,
529 "1-1-from_local_hold_group-holdingbranch" => 1,
530 "1-4-from_home_library-any" => 0,
531 "1-4-from_home_library-holdgroup" => 0,
532 "1-4-from_home_library-patrongroup" => 0,
533 "1-4-from_home_library-homebranch" => 0,
534 "1-4-from_home_library-holdingbranch" => 0,
535 "1-4-from_any_library-any" => 3,
536 "1-4-from_any_library-holdgroup" => 2,
537 "1-4-from_any_library-patrongroup" => 1,
538 "1-4-from_any_library-homebranch" => 1,
539 "1-4-from_any_library-holdingbranch" => 1,
540 "1-4-from_local_hold_group-any" => 0,
541 "1-4-from_local_hold_group-holdgroup" => 0,
542 "1-4-from_local_hold_group-patrongroup" => 0,
543 "1-4-from_local_hold_group-homebranch" => 0,
544 "1-4-from_local_hold_group-holdingbranch" => 0,
545 "3-1-from_home_library-any" => 0,
546 "3-1-from_home_library-holdgroup" => 0,
547 "3-1-from_home_library-patrongroup" => 0,
548 "3-1-from_home_library-homebranch" => 0,
549 "3-1-from_home_library-holdingbranch" => 0,
550 "3-1-from_any_library-any" => 3,
551 "3-1-from_any_library-holdgroup" => 1,
552 "3-1-from_any_library-patrongroup" => 2,
553 "3-1-from_any_library-homebranch" => 0,
554 "3-1-from_any_library-holdingbranch" => 1,
555 "3-1-from_local_hold_group-any" => 0,
556 "3-1-from_local_hold_group-holdgroup" => 0,
557 "3-1-from_local_hold_group-patrongroup" => 0,
558 "3-1-from_local_hold_group-homebranch" => 0,
559 "3-1-from_local_hold_group-holdingbranch" => 0,
560 "3-4-from_home_library-any" => 0,
561 "3-4-from_home_library-holdgroup" => 0,
562 "3-4-from_home_library-patrongroup" => 0,
563 "3-4-from_home_library-homebranch" => 0,
564 "3-4-from_home_library-holdingbranch" => 0,
565 "3-4-from_any_library-any" => 3,
566 "3-4-from_any_library-holdgroup" => 1,
567 "3-4-from_any_library-patrongroup" => 1,
568 "3-4-from_any_library-homebranch" => 0,
569 "3-4-from_any_library-holdingbranch" => 1,
570 "3-4-from_local_hold_group-any" => 3,
571 "3-4-from_local_hold_group-holdgroup" => 1,
572 "3-4-from_local_hold_group-patrongroup" => 1,
573 "3-4-from_local_hold_group-homebranch" => 0,
574 "3-4-from_local_hold_group-holdingbranch" => 1
578 my ( $item, $patron, $ha, $hfp, $results ) = @_;
580 Koha::CirculationRules->set_rules(
586 hold_fulfillment_policy => $hfp,
587 returnbranch => 'any'
592 $ha eq 'from_local_hold_group' ? 'holdgroup'
594 $ha eq 'from_any_library' ? 'any'
599 my $pickup_location = $_;
600 grep { $pickup_location->branchcode eq $_ } @branchcodes
601 } $item->pickup_locations( { patron => $patron } )->as_list;
604 scalar(@pl) eq $results->{
605 $item->copynumber . '-'
606 . $patron->firstname . '-'
616 . ', hold_fulfillment_policy: '
620 $item->copynumber . '-'
621 . $patron->firstname . '-'
632 foreach my $item ($item1, $item3) {
633 foreach my $patron ($patron1, $patron4) {
634 #holdallowed 1: homebranch, 2: any, 3: holdgroup
635 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
636 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
637 _doTest($item, $patron, $ha, $hfp, $results);
643 # Now test that branchtransferlimits will further filter the pickup locations
645 my $item_no_ccode = $builder->build_sample_item(
647 homebranch => $library1->branchcode,
648 holdingbranch => $library2->branchcode,
649 itype => $item1->itype,
653 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
654 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
655 Koha::CirculationRules->set_rules(
658 itemtype => $item1->itype,
660 holdallowed => 'from_home_library',
661 hold_fulfillment_policy => 1,
662 returnbranch => 'any'
666 $builder->build_object(
668 class => 'Koha::Item::Transfer::Limits',
670 toBranch => $library1->branchcode,
671 fromBranch => $library2->branchcode,
672 itemtype => $item1->itype,
678 my @pickup_locations = map {
679 my $pickup_location = $_;
680 grep { $pickup_location->branchcode eq $_ } @branchcodes
681 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
683 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
685 $builder->build_object(
687 class => 'Koha::Item::Transfer::Limits',
689 toBranch => $library4->branchcode,
690 fromBranch => $library2->branchcode,
691 itemtype => $item1->itype,
697 @pickup_locations = map {
698 my $pickup_location = $_;
699 grep { $pickup_location->branchcode eq $_ } @branchcodes
700 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
702 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
704 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
705 @pickup_locations = map {
706 my $pickup_location = $_;
707 grep { $pickup_location->branchcode eq $_ } @branchcodes
708 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
709 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
711 @pickup_locations = map {
712 my $pickup_location = $_;
713 grep { $pickup_location->branchcode eq $_ } @branchcodes
714 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
715 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode and an item with no ccode we get back the libraries that are pickup locations");
717 $builder->build_object(
719 class => 'Koha::Item::Transfer::Limits',
721 toBranch => $library2->branchcode,
722 fromBranch => $library2->branchcode,
724 ccode => $item1->ccode,
729 @pickup_locations = map {
730 my $pickup_location = $_;
731 grep { $pickup_location->branchcode eq $_ } @branchcodes
732 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
733 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
735 $builder->build_object(
737 class => 'Koha::Item::Transfer::Limits',
739 toBranch => $library4->branchcode,
740 fromBranch => $library2->branchcode,
742 ccode => $item1->ccode,
747 @pickup_locations = map {
748 my $pickup_location = $_;
749 grep { $pickup_location->branchcode eq $_ } @branchcodes
750 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
751 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
753 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
755 $schema->storage->txn_rollback;
758 subtest 'request_transfer' => sub {
760 $schema->storage->txn_begin;
762 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
763 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
764 my $item = $builder->build_sample_item(
766 homebranch => $library1->branchcode,
767 holdingbranch => $library2->branchcode,
771 # Mandatory fields tests
772 throws_ok { $item->request_transfer( { to => $library1 } ) }
773 'Koha::Exceptions::MissingParameter',
774 'Exception thrown if `reason` parameter is missing';
776 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
777 'Koha::Exceptions::MissingParameter',
778 'Exception thrown if `to` parameter is missing';
781 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
782 is( ref($transfer), 'Koha::Item::Transfer',
783 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
785 my $original_transfer = $transfer->get_from_storage;
787 # Transfer already in progress
788 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
789 'Koha::Exceptions::Item::Transfer::InQueue',
790 'Exception thrown if transfer is already in progress';
793 is( ref( $exception->transfer ),
794 'Koha::Item::Transfer',
795 'The exception contains the found Koha::Item::Transfer' );
798 my $queued_transfer = $item->request_transfer(
799 { to => $library2, reason => 'Manual', enqueue => 1 } );
800 is( ref($queued_transfer), 'Koha::Item::Transfer',
801 'Koha::Item->request_transfer allowed when enqueue is set' );
802 my $transfers = $item->get_transfers;
803 is($transfers->count, 2, "There are now 2 live transfers in the queue");
804 $transfer = $transfer->get_from_storage;
805 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
806 $queued_transfer->datearrived(dt_from_string)->store();
809 my $replaced_transfer = $item->request_transfer(
810 { to => $library2, reason => 'Manual', replace => 1 } );
811 is( ref($replaced_transfer), 'Koha::Item::Transfer',
812 'Koha::Item->request_transfer allowed when replace is set' );
813 $original_transfer->discard_changes;
814 ok($original_transfer->datecancelled, "Original transfer cancelled");
815 $transfers = $item->get_transfers;
816 is($transfers->count, 1, "There is only 1 live transfer in the queue");
817 $replaced_transfer->datearrived(dt_from_string)->store();
819 # BranchTransferLimits
820 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
821 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
822 my $limit = Koha::Item::Transfer::Limit->new({
823 fromBranch => $library2->branchcode,
824 toBranch => $library1->branchcode,
825 itemtype => $item->effective_itemtype,
828 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
829 'Koha::Exceptions::Item::Transfer::Limit',
830 'Exception thrown if transfer is prevented by limits';
832 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
833 is( ref($forced_transfer), 'Koha::Item::Transfer',
834 'Koha::Item->request_transfer allowed when ignore_limits is set'
837 $schema->storage->txn_rollback;
840 subtest 'deletion' => sub {
843 $schema->storage->txn_begin;
845 my $biblio = $builder->build_sample_biblio();
847 my $item = $builder->build_sample_item(
849 biblionumber => $biblio->biblionumber,
852 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
854 my $deleted_item = $item->move_to_deleted;
855 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
856 ; # FIXME This should be Koha::Deleted::Item
857 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
859 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
860 $item = $builder->build_sample_item(
862 biblionumber => $biblio->biblionumber,
866 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
869 my $library = $builder->build_object({ class => 'Koha::Libraries' });
870 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
871 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
873 my $patron = $builder->build_object({class => 'Koha::Patrons'});
874 $item = $builder->build_sample_item({ library => $library->branchcode });
877 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
880 @{$item->safe_to_delete->messages}[0]->message,
882 'Koha::Item->safe_to_delete reports item on loan',
886 @{$item->safe_to_delete->messages}[0]->message,
888 'item that is on loan cannot be deleted',
892 ! $item->safe_to_delete,
893 'Koha::Item->safe_to_delete shows item NOT safe to delete'
896 AddReturn( $item->barcode, $library->branchcode );
899 t::lib::Mocks::mock_preference('IndependentBranches', 1);
900 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
903 @{$item_2->safe_to_delete->messages}[0]->message,
905 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
909 @{$item_2->safe_to_delete->messages}[0]->message,
911 'IndependentBranches prevents deletion at another branch',
916 { # codeblock to limit scope of $module->mock
918 my $module = Test::MockModule->new('C4::Items');
919 $module->mock( GetAnalyticsCount => sub { return 1 } );
921 $item->discard_changes;
923 @{$item->safe_to_delete->messages}[0]->message,
925 'Koha::Item->safe_to_delete reports linked analytics',
929 @{$item->safe_to_delete->messages}[0]->message,
931 'Linked analytics prevents deletion of item',
937 $item->safe_to_delete,
938 'Koha::Item->safe_to_delete shows item safe to delete'
943 my $test_item = Koha::Items->find( $item->itemnumber );
945 is( $test_item, undef,
946 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
949 subtest 'holds tests' => sub {
954 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
956 $schema->storage->txn_begin;
958 my $item = $builder->build_sample_item;
960 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
961 my $safe_to_delete = $item->safe_to_delete;
963 ok( !$safe_to_delete, 'Cannot delete' );
965 @{ $safe_to_delete->messages }[0]->message,
967 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
972 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
973 $safe_to_delete = $item->safe_to_delete;
975 ok( !$safe_to_delete, 'Cannot delete' );
977 @{ $safe_to_delete->messages }[0]->message,
979 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
984 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
985 $safe_to_delete = $item->safe_to_delete;
987 ok( !$safe_to_delete, 'Cannot delete' );
989 @{ $safe_to_delete->messages }[0]->message,
991 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
996 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
997 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
999 $safe_to_delete = $item->safe_to_delete;
1001 ok( !$safe_to_delete );
1004 @{ $safe_to_delete->messages}[0]->message,
1005 'last_item_for_hold',
1006 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
1009 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
1011 ok( $item->safe_to_delete );
1013 $schema->storage->txn_rollback;
1016 $schema->storage->txn_rollback;
1019 subtest 'renewal_branchcode' => sub {
1022 $schema->storage->txn_begin;
1024 my $item = $builder->build_sample_item();
1025 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1026 my $checkout = $builder->build_object({
1027 class => 'Koha::Checkouts',
1029 itemnumber => $item->itemnumber,
1034 C4::Context->interface( 'intranet' );
1035 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1037 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1038 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1039 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1040 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1042 C4::Context->interface( 'opac' );
1044 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1045 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1046 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1048 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1049 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1050 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1052 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1053 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1054 is( $item->renewal_branchcode({branch=>'MONKEY'}), $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed");
1056 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1057 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1058 is( $item->renewal_branchcode({branch=>'TURKEY'}), $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron even if branch passed");
1060 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1061 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1062 is( $item->renewal_branchcode({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed");
1064 $schema->storage->txn_rollback;
1067 subtest 'Tests for itemtype' => sub {
1069 $schema->storage->txn_begin;
1071 my $biblio = $builder->build_sample_biblio;
1072 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1073 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1075 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1076 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1077 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1078 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1080 $schema->storage->txn_rollback;
1083 subtest 'get_transfers' => sub {
1085 $schema->storage->txn_begin;
1087 my $item = $builder->build_sample_item();
1089 my $transfers = $item->get_transfers();
1090 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1091 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1093 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1095 my $transfer_1 = $builder->build_object(
1097 class => 'Koha::Item::Transfers',
1099 itemnumber => $item->itemnumber,
1100 frombranch => $item->holdingbranch,
1101 tobranch => $library_to->branchcode,
1104 datearrived => undef,
1105 datecancelled => undef,
1106 daterequested => \'NOW()'
1111 $transfers = $item->get_transfers();
1112 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1114 my $transfer_2 = $builder->build_object(
1116 class => 'Koha::Item::Transfers',
1118 itemnumber => $item->itemnumber,
1119 frombranch => $item->holdingbranch,
1120 tobranch => $library_to->branchcode,
1123 datearrived => undef,
1124 datecancelled => undef,
1125 daterequested => \'NOW()'
1130 my $transfer_3 = $builder->build_object(
1132 class => 'Koha::Item::Transfers',
1134 itemnumber => $item->itemnumber,
1135 frombranch => $item->holdingbranch,
1136 tobranch => $library_to->branchcode,
1139 datearrived => undef,
1140 datecancelled => undef,
1141 daterequested => \'NOW()'
1146 $transfers = $item->get_transfers();
1147 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1148 my $result_1 = $transfers->next;
1149 my $result_2 = $transfers->next;
1150 my $result_3 = $transfers->next;
1151 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1152 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1153 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1155 $transfer_2->datesent(\'NOW()')->store;
1156 $transfers = $item->get_transfers();
1157 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1158 $result_1 = $transfers->next;
1159 $result_2 = $transfers->next;
1160 $result_3 = $transfers->next;
1161 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1162 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1163 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1165 $transfer_2->datearrived(\'NOW()')->store;
1166 $transfers = $item->get_transfers();
1167 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1168 $result_1 = $transfers->next;
1169 $result_2 = $transfers->next;
1170 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1171 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1173 $transfer_1->datecancelled(\'NOW()')->store;
1174 $transfers = $item->get_transfers();
1175 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1176 $result_1 = $transfers->next;
1177 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1179 $schema->storage->txn_rollback;
1182 subtest 'Test for relationship between item and current_branchtransfers' => sub {
1185 $schema->storage->txn_begin;
1187 my $item = $builder->build_sample_item();
1188 my $transfer = $builder->build_object(
1190 class => 'Koha::Item::Transfers',
1192 itemnumber => $item->itemnumber,
1193 datesent => dt_from_string,
1194 datearrived => dt_from_string,
1195 datecancelled => undef,
1200 my $transfer_item = $transfer->item;
1201 my $biblio = Koha::Biblios->find( $transfer_item->biblionumber );
1203 my $current_branchtransfers = Koha::Items->search(
1204 { 'me.itemnumber' => $transfer_item->itemnumber },
1205 { prefetch => ['current_branchtransfers'] }
1208 my $item_with_branchtransfers = $current_branchtransfers->next;
1211 $transfer_item->itemnumber,
1212 $item_with_branchtransfers->itemnumber,
1213 'following two items are the same'
1216 # following two tests should produce the same result
1218 $transfer_item->get_transfer,
1220 'Koha::Item->get_transfer returns undef with no active transfers'
1223 $item_with_branchtransfers->get_transfer, undef,
1224 'prefetched result->get_transfer returns undef with no active transfers'
1229 datearrived => undef,
1233 $current_branchtransfers = Koha::Items->search(
1234 { 'me.itemnumber' => $transfer_item->itemnumber },
1235 { prefetch => ['current_branchtransfers'] }
1238 $item_with_branchtransfers = $current_branchtransfers->next;
1241 $transfer_item->get_transfer->branchtransfer_id,
1242 $item_with_branchtransfers->get_transfer->branchtransfer_id,
1243 'an active transfer produces same branchtransfer_id for both methods'
1246 $schema->storage->txn_rollback;
1249 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1252 $schema->storage->txn_begin;
1254 my $biblio = $builder->build_sample_biblio();
1255 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1257 my $orders = $item->orders;
1258 is ($orders->count, 0, 'No order on this item yet');
1260 my $order_note = 'Order for ' . $item->itemnumber;
1262 my $aq_order1 = $builder->build_object({
1263 class => 'Koha::Acquisition::Orders',
1265 biblionumber => $biblio->biblionumber,
1266 order_internalnote => $order_note,
1269 my $aq_order2 = $builder->build_object({
1270 class => 'Koha::Acquisition::Orders',
1272 biblionumber => $biblio->biblionumber,
1275 my $aq_order_item1 = $builder->build({
1276 source => 'AqordersItem',
1278 ordernumber => $aq_order1->ordernumber,
1279 itemnumber => $item->itemnumber,
1283 $orders = $item->orders;
1284 is ($orders->count, 1, 'One order found by item with the relationship');
1285 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1288 subtest 'move_to_biblio() tests' => sub {
1291 $schema->storage->txn_begin;
1293 my $dbh = C4::Context->dbh;
1295 my $source_biblio = $builder->build_sample_biblio();
1296 my $target_biblio = $builder->build_sample_biblio();
1298 my $source_biblionumber = $source_biblio->biblionumber;
1299 my $target_biblionumber = $target_biblio->biblionumber;
1301 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1302 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1303 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1305 my $itemnumber1 = $item1->itemnumber;
1306 my $itemnumber2 = $item2->itemnumber;
1308 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1310 my $patron = $builder->build_object({
1311 class => 'Koha::Patrons',
1312 value => { branchcode => $library->branchcode }
1314 my $borrowernumber = $patron->borrowernumber;
1316 my $aq_budget = $builder->build({
1317 source => 'Aqbudget',
1319 budget_notes => 'test',
1323 my $aq_order1 = $builder->build_object({
1324 class => 'Koha::Acquisition::Orders',
1326 biblionumber => $source_biblionumber,
1327 budget_id => $aq_budget->{budget_id},
1330 my $aq_order_item1 = $builder->build({
1331 source => 'AqordersItem',
1333 ordernumber => $aq_order1->ordernumber,
1334 itemnumber => $itemnumber1,
1337 my $aq_order2 = $builder->build_object({
1338 class => 'Koha::Acquisition::Orders',
1340 biblionumber => $source_biblionumber,
1341 budget_id => $aq_budget->{budget_id},
1344 my $aq_order_item2 = $builder->build({
1345 source => 'AqordersItem',
1347 ordernumber => $aq_order2->ordernumber,
1348 itemnumber => $itemnumber2,
1352 my $bib_level_hold = $builder->build_object({
1353 class => 'Koha::Holds',
1355 biblionumber => $source_biblionumber,
1356 itemnumber => undef,
1359 my $item_level_hold1 = $builder->build_object({
1360 class => 'Koha::Holds',
1362 biblionumber => $source_biblionumber,
1363 itemnumber => $itemnumber1,
1366 my $item_level_hold2 = $builder->build_object({
1367 class => 'Koha::Holds',
1369 biblionumber => $source_biblionumber,
1370 itemnumber => $itemnumber2,
1374 my $tmp_holdsqueue1 = $builder->build({
1375 source => 'TmpHoldsqueue',
1377 borrowernumber => $borrowernumber,
1378 biblionumber => $source_biblionumber,
1379 itemnumber => $itemnumber1,
1382 my $tmp_holdsqueue2 = $builder->build({
1383 source => 'TmpHoldsqueue',
1385 borrowernumber => $borrowernumber,
1386 biblionumber => $source_biblionumber,
1387 itemnumber => $itemnumber2,
1390 my $hold_fill_target1 = $builder->build({
1391 source => 'HoldFillTarget',
1393 borrowernumber => $borrowernumber,
1394 biblionumber => $source_biblionumber,
1395 itemnumber => $itemnumber1,
1398 my $hold_fill_target2 = $builder->build({
1399 source => 'HoldFillTarget',
1401 borrowernumber => $borrowernumber,
1402 biblionumber => $source_biblionumber,
1403 itemnumber => $itemnumber2,
1406 my $linktracker1 = $builder->build({
1407 source => 'Linktracker',
1409 borrowernumber => $borrowernumber,
1410 biblionumber => $source_biblionumber,
1411 itemnumber => $itemnumber1,
1414 my $linktracker2 = $builder->build({
1415 source => 'Linktracker',
1417 borrowernumber => $borrowernumber,
1418 biblionumber => $source_biblionumber,
1419 itemnumber => $itemnumber2,
1423 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1424 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1426 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1427 is($to_biblionumber_after_move, undef, 'move_to_biblio returns undef if the move has failed. If called twice, the item is not attached to the first biblio anymore');
1429 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1430 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1431 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1432 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1433 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1434 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1436 $aq_order1->discard_changes;
1437 $aq_order2->discard_changes;
1438 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1439 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1441 $bib_level_hold->discard_changes;
1442 $item_level_hold1->discard_changes;
1443 $item_level_hold2->discard_changes;
1444 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1445 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1446 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1448 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1449 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1450 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1451 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1453 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1454 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1455 # Why does ->biblionumber return a Biblio object???
1456 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1457 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1459 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1460 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1461 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1462 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1464 $schema->storage->txn_rollback;
1467 subtest 'columns_to_str' => sub {
1470 $schema->storage->txn_begin;
1472 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1474 my $cache = Koha::Caches->get_instance();
1475 $cache->clear_from_cache("MarcStructure-0-");
1476 $cache->clear_from_cache("MarcStructure-1-");
1477 $cache->clear_from_cache("MarcSubfieldStructure-");
1478 $cache->clear_from_cache("libraries:name");
1479 $cache->clear_from_cache("itemtype:description:en");
1480 $cache->clear_from_cache("cn_sources:description");
1481 $cache->clear_from_cache("AV_descriptions:LOST");
1483 # Creating subfields 'é', 'è' that are not linked with a kohafield
1484 Koha::MarcSubfieldStructures->search(
1486 frameworkcode => '',
1487 tagfield => $itemtag,
1488 tagsubfield => ['é', 'è'],
1490 )->delete; # In case it exist already
1492 # é is not linked with a AV
1493 # è is linked with AV branches
1494 Koha::MarcSubfieldStructure->new(
1496 frameworkcode => '',
1497 tagfield => $itemtag,
1498 tagsubfield => 'é',
1501 defaultvalue => 'ééé',
1505 Koha::MarcSubfieldStructure->new(
1507 frameworkcode => '',
1508 tagfield => $itemtag,
1509 tagsubfield => 'è',
1512 defaultvalue => 'èèè',
1514 authorised_value => 'branches',
1518 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1519 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1520 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1521 my $dateaccessioned = '2020-12-15';
1522 my $library = Koha::Libraries->search->next;
1523 my $branchcode = $library->branchcode;
1525 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1527 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1528 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1529 xmlns="http://www.loc.gov/MARC21/slim">
1532 <leader> a </leader>
1533 <datafield tag="999" ind1=" " ind2=" ">
1534 <subfield code="é">value é</subfield>
1535 <subfield code="è">$branchcode</subfield>
1543 itemlost => $lost_av->authorised_value,
1544 dateaccessioned => $dateaccessioned,
1545 more_subfields_xml => $some_marc_xml,
1549 Koha::Caches->get_instance->flush_all;
1551 $item = $item->get_from_storage;
1553 my $s = $item->columns_to_str;
1554 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1555 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1556 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1557 is( $s->{'è'}, $library->branchname );
1559 $cache->clear_from_cache("MarcStructure-0-");
1560 $cache->clear_from_cache("MarcStructure-1-");
1561 $cache->clear_from_cache("MarcSubfieldStructure-");
1562 $cache->clear_from_cache("libraries:name");
1563 $cache->clear_from_cache("itemtype:description:en");
1564 $cache->clear_from_cache("cn_sources:description");
1565 $cache->clear_from_cache("AV_descriptions:LOST");
1567 $schema->storage->txn_rollback;
1570 subtest 'strings_map() tests' => sub {
1574 $schema->storage->txn_begin;
1576 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1578 my $cache = Koha::Caches->get_instance();
1579 $cache->clear_from_cache("MarcStructure-0-");
1580 $cache->clear_from_cache("MarcStructure-1-");
1581 $cache->clear_from_cache("MarcSubfieldStructure-");
1582 $cache->clear_from_cache("libraries:name");
1583 $cache->clear_from_cache("itemtype:description:en");
1584 $cache->clear_from_cache("cn_sources:description");
1585 $cache->clear_from_cache("AV_descriptions:LOST");
1587 # Recreating subfields just to be sure tests will be ok
1592 Koha::MarcSubfieldStructures->search(
1594 frameworkcode => '',
1595 tagfield => $itemtag,
1596 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1598 )->delete; # In case it exist already
1600 Koha::MarcSubfieldStructure->new(
1602 authorised_value => 'LOST',
1604 frameworkcode => '',
1605 kohafield => 'items.itemlost',
1608 tagfield => $itemtag,
1612 Koha::MarcSubfieldStructure->new(
1614 authorised_value => 'cn_source',
1616 frameworkcode => '',
1617 kohafield => 'items.cn_source',
1620 tagfield => $itemtag,
1624 Koha::MarcSubfieldStructure->new(
1626 authorised_value => '',
1628 frameworkcode => '',
1629 kohafield => 'items.materials',
1632 tagfield => $itemtag,
1636 Koha::MarcSubfieldStructure->new(
1638 authorised_value => 'branches',
1640 frameworkcode => '',
1641 kohafield => 'items.homebranch',
1644 tagfield => $itemtag,
1648 Koha::MarcSubfieldStructure->new(
1650 authorised_value => 'itemtypes',
1652 frameworkcode => '',
1653 kohafield => 'items.itype',
1656 tagfield => $itemtag,
1661 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1662 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1663 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1664 my $item = $builder->build_sample_item(
1666 biblionumber => $biblio->id,
1667 library => $library->id
1671 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1672 my $lost_av = $builder->build_object(
1674 class => 'Koha::AuthorisedValues',
1676 authorised_value => 3,
1678 lib => 'internal description',
1679 lib_opac => 'public description',
1684 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1685 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1686 my $class_source = $builder->build_object(
1688 class => 'Koha::ClassSources',
1690 class_sort_rule => $class_sort_rule->class_sort_rule,
1691 class_split_rule => $class_split_rule->class_split_rule,
1696 Koha::Caches->get_instance->flush_all;
1700 cn_source => $class_source->id,
1701 itemlost => $lost_av->authorised_value,
1702 itype => $itype->itemtype,
1703 materials => 'Suff',
1705 )->store->discard_changes;
1707 my $strings = $item->strings_map;
1709 subtest 'unmapped field tests' => sub {
1713 ok( !exists $strings->{materials}, "Unmapped field not present" );
1716 subtest 'av handling' => sub {
1720 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1721 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1722 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1723 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1726 subtest 'cn_source handling' => sub {
1730 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1731 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1732 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1735 subtest 'branches handling' => sub {
1739 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1740 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1741 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1744 subtest 'itemtype handling' => sub {
1748 ok( exists $strings->{itype}, "'itype' entry exists" );
1749 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1750 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1753 subtest 'public flag tests' => sub {
1757 $strings = $item->strings_map( { public => 1 } );
1759 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1760 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1761 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1762 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1765 $cache->clear_from_cache("MarcStructure-0-");
1766 $cache->clear_from_cache("MarcStructure-1-");
1767 $cache->clear_from_cache("MarcSubfieldStructure-");
1768 $cache->clear_from_cache("libraries:name");
1769 $cache->clear_from_cache("itemtype:description:en");
1770 $cache->clear_from_cache("cn_sources:description");
1772 $schema->storage->txn_rollback;
1775 subtest 'store() tests' => sub {
1779 subtest 'dateaccessioned handling' => sub {
1783 $schema->storage->txn_begin;
1785 my $item = $builder->build_sample_item;
1787 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1789 # reset dateaccessioned on the DB
1790 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1791 $item->discard_changes;
1793 ok( !defined $item->dateaccessioned );
1796 $item->replacementprice(100)->store->discard_changes;
1798 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1800 $schema->storage->txn_rollback;
1803 subtest '_set_found_trigger() tests' => sub {
1807 $schema->storage->txn_begin;
1809 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1810 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1812 # Add a lost item debit
1813 my $debit = $patron->account->add_debit(
1817 item_id => $item->id,
1818 interface => 'intranet',
1822 # Add a lost item processing fee
1823 my $processing_debit = $patron->account->add_debit(
1826 type => 'PROCESSING',
1827 item_id => $item->id,
1828 interface => 'intranet',
1832 my $lostreturn_policy = {
1833 lostreturn => 'charge',
1834 processingreturn => 'refund'
1837 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1838 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1840 # simulate it was found
1841 $item->set( { itemlost => 0 } )->store;
1843 my $messages = $item->object_messages;
1845 my $message_1 = $messages->[0];
1847 is( $message_1->type, 'info', 'type is correct' );
1848 is( $message_1->message, 'lost_refunded', 'message is correct' );
1850 # Find the refund credit
1851 my $credit = $debit->credits->next;
1854 $message_1->payload,
1855 { credit_id => $credit->id },
1859 my $message_2 = $messages->[1];
1861 is( $message_2->type, 'info', 'type is correct' );
1862 is( $message_2->message, 'lost_charge', 'message is correct' );
1863 is( $message_2->payload, undef, 'no payload' );
1865 my $message_3 = $messages->[2];
1866 is( $message_3->message, 'processing_refunded', 'message is correct' );
1868 my $processing_credit = $processing_debit->credits->next;
1870 $message_3->payload,
1871 { credit_id => $processing_credit->id },
1875 # Let's build a new item
1876 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1877 $item->set( { itemlost => 0 } )->store;
1879 $messages = $item->object_messages;
1880 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1882 $schema->storage->txn_rollback;
1885 subtest 'holds_queue update tests' => sub {
1889 $schema->storage->txn_begin;
1891 my $biblio = $builder->build_sample_biblio;
1893 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1894 $mock->mock( 'enqueue', sub {
1895 my ( $self, $args ) = @_;
1897 $args->{biblio_ids},
1899 '->store triggers a holds queue update for the related biblio'
1903 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1906 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1909 $item->set({ reserves => 1 })->store;
1911 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1913 $item->set({ reserves => 0 })->store;
1915 $schema->storage->txn_rollback;
1919 subtest 'Recalls tests' => sub {
1923 $schema->storage->txn_begin;
1925 my $item1 = $builder->build_sample_item;
1926 my $biblio = $item1->biblio;
1927 my $branchcode = $item1->holdingbranch;
1928 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1929 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1930 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1931 my $item2 = $builder->build_object(
1932 { class => 'Koha::Items',
1933 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1937 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1938 t::lib::Mocks::mock_preference('UseRecalls', 1);
1940 my $recall1 = Koha::Recall->new(
1941 { patron_id => $patron1->borrowernumber,
1942 created_date => \'NOW()',
1943 biblio_id => $biblio->biblionumber,
1944 pickup_library_id => $branchcode,
1945 item_id => $item1->itemnumber,
1946 expiration_date => undef,
1950 my $recall2 = Koha::Recall->new(
1951 { patron_id => $patron2->borrowernumber,
1952 created_date => \'NOW()',
1953 biblio_id => $biblio->biblionumber,
1954 pickup_library_id => $branchcode,
1955 item_id => $item1->itemnumber,
1956 expiration_date => undef,
1961 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1963 $recall2->set_cancelled;
1965 t::lib::Mocks::mock_preference('UseRecalls', 0);
1966 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1968 t::lib::Mocks::mock_preference("UseRecalls", 1);
1970 $item1->update({ notforloan => 1 });
1971 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1972 $item1->update({ notforloan => 0, itemlost => 1 });
1973 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1974 $item1->update({ itemlost => 0, withdrawn => 1 });
1975 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1976 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1978 $item1->update({ withdrawn => 0 });
1979 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1981 Koha::CirculationRules->set_rules({
1982 branchcode => $branchcode,
1983 categorycode => $patron1->categorycode,
1984 itemtype => $item1->effective_itemtype,
1986 recalls_allowed => 0,
1987 recalls_per_record => 1,
1988 on_shelf_recalls => 'all',
1991 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1993 Koha::CirculationRules->set_rules({
1994 branchcode => $branchcode,
1995 categorycode => $patron1->categorycode,
1996 itemtype => $item1->effective_itemtype,
1998 recalls_allowed => 1,
1999 recalls_per_record => 1,
2000 on_shelf_recalls => 'all',
2003 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
2004 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
2005 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
2007 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
2008 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
2009 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
2011 $recall1->set_cancelled;
2012 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
2014 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
2016 Koha::CirculationRules->set_rules({
2017 branchcode => $branchcode,
2018 categorycode => $patron1->categorycode,
2019 itemtype => $item1->effective_itemtype,
2021 recalls_allowed => 1,
2022 recalls_per_record => 1,
2023 on_shelf_recalls => 'any',
2026 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
2027 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
2029 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
2030 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
2032 $recall1 = Koha::Recall->new(
2033 { patron_id => $patron1->borrowernumber,
2034 created_date => \'NOW()',
2035 biblio_id => $biblio->biblionumber,
2036 pickup_library_id => $branchcode,
2038 expiration_date => undef,
2043 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
2045 Koha::CirculationRules->set_rules({
2046 branchcode => $branchcode,
2047 categorycode => $patron1->categorycode,
2048 itemtype => $item1->effective_itemtype,
2050 recalls_allowed => 0,
2051 recalls_per_record => 1,
2052 on_shelf_recalls => 'any',
2055 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
2057 Koha::CirculationRules->set_rules({
2058 branchcode => $branchcode,
2059 categorycode => $patron1->categorycode,
2060 itemtype => $item1->effective_itemtype,
2062 recalls_allowed => 1,
2063 recalls_per_record => 1,
2064 on_shelf_recalls => 'any',
2067 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
2069 # check_recalls tests
2071 $recall1 = Koha::Recall->new(
2072 { patron_id => $patron2->borrowernumber,
2073 created_date => \'NOW()',
2074 biblio_id => $biblio->biblionumber,
2075 pickup_library_id => $branchcode,
2076 item_id => $item1->itemnumber,
2077 expiration_date => undef,
2081 $recall2 = Koha::Recall->new(
2082 { patron_id => $patron1->borrowernumber,
2083 created_date => \'NOW()',
2084 biblio_id => $biblio->biblionumber,
2085 pickup_library_id => $branchcode,
2087 expiration_date => undef,
2091 $recall2->set_waiting( { item => $item1 } );
2092 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
2094 # return a waiting recall
2095 my $check_recall = $item1->check_recalls;
2096 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
2098 $recall2->revert_waiting;
2100 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
2102 # return recall based on recalldate
2103 $check_recall = $item1->check_recalls;
2104 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
2106 $recall1->set_cancelled;
2108 # return a biblio-level recall
2109 $check_recall = $item1->check_recalls;
2110 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
2112 $recall2->set_cancelled;
2114 $schema->storage->txn_rollback;
2117 subtest 'Notforloan tests' => sub {
2121 $schema->storage->txn_begin;
2123 my $item1 = $builder->build_sample_item;
2124 $item1->update({ notforloan => 0 });
2125 $item1->itemtype->notforloan(0);
2126 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2127 $item1->update({ notforloan => 1 });
2128 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2129 $item1->update({ notforloan => 0 });
2130 $item1->itemtype->update({ notforloan => 1 });
2131 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2133 $schema->storage->txn_rollback;
2136 subtest 'item_group() tests' => sub {
2140 $schema->storage->txn_begin;
2142 my $biblio = $builder->build_sample_biblio();
2143 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2144 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2146 is( $item_1->item_group, undef, 'Item 1 has no item group');
2147 is( $item_2->item_group, undef, 'Item 2 has no item group');
2149 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2150 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2152 $item_group_1->add_item({ item_id => $item_1->id });
2153 $item_group_2->add_item({ item_id => $item_2->id });
2155 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2156 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2158 $schema->storage->txn_rollback;
2161 subtest 'has_pending_recall() tests' => sub {
2165 $schema->storage->txn_begin;
2167 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2168 my $item = $builder->build_sample_item;
2169 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2171 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2172 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2174 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2176 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2178 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2180 $recall->status('waiting')->store;
2182 ok( $item->has_pending_recall, 'The item has a pending recall' );
2184 $schema->storage->txn_rollback;
2187 subtest 'is_denied_renewal' => sub {
2190 $schema->storage->txn_begin;
2192 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2194 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2195 homebranch => $library->branchcode,
2199 itemcallnumber => undef,
2204 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2205 homebranch => $library->branchcode,
2208 location => 'NOPROC'
2213 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2214 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2216 # The wrong column delete should be silently ignored and not trigger item delete
2217 $idr_rules="delete: [yes]\nwithdrawn: [1]";
2218 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2219 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2220 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2222 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2223 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2224 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2226 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2227 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2228 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2229 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2231 $idr_rules="itemcallnumber: [null]";
2232 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2233 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when null in pref' );
2235 $idr_rules="itemcallnumber: ['']";
2236 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2237 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2239 $idr_rules="itemnotes: [null]";
2240 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2241 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when null in pref' );
2243 $idr_rules="itemnotes: ['']";
2244 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2245 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2247 $schema->storage->txn_rollback;
2250 subtest 'current_branchtransfers relationship' => sub {
2253 $schema->storage->txn_begin;
2255 my $biblio = $builder->build_sample_biblio();
2256 my $item = $builder->build_sample_item(
2258 biblionumber => $biblio->biblionumber,
2261 my $transfers = $item->_result->current_branchtransfers;
2262 is( ref($transfers), 'DBIx::Class::ResultSet',
2263 'current_branchtransfers returns a DBIx::Class::ResultSet' );
2264 is( $transfers->count, 0,
2265 "Empty Koha::Item::Transfers set returned if no return_claims" );
2266 my $transfer1 = $builder->build(
2268 source => 'Branchtransfer',
2270 itemnumber => $item->itemnumber,
2271 datearrived => dt_from_string,
2275 my $transfer2 = $builder->build(
2277 source => 'Branchtransfer',
2279 itemnumber => $item->itemnumber,
2280 datearrived => undef,
2281 datecancelled => dt_from_string,
2285 my $transfer3 = $builder->build(
2287 source => 'Branchtransfer',
2289 itemnumber => $item->itemnumber,
2290 datearrived => undef,
2291 datecancelled => undef,
2296 is( $item->_result->current_branchtransfers()->count,
2297 1, "One transfer found for item" );
2299 $schema->storage->txn_rollback;