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 => 28;
27 use C4::Biblio qw( GetMarcSubfieldStructure );
28 use C4::Circulation qw( AddIssue AddReturn );
33 use Koha::DateUtils qw( dt_from_string );
37 use List::MoreUtils qw(all);
39 use t::lib::TestBuilder;
43 my $schema = Koha::Database->new->schema;
44 my $builder = t::lib::TestBuilder->new;
46 subtest 'return_claims relationship' => sub {
49 $schema->storage->txn_begin;
51 my $biblio = $builder->build_sample_biblio();
52 my $item = $builder->build_sample_item({
53 biblionumber => $biblio->biblionumber,
55 my $return_claims = $item->return_claims;
56 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
57 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
58 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
59 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
61 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
63 $schema->storage->txn_rollback;
66 subtest 'return_claim accessor' => sub {
69 $schema->storage->txn_begin;
71 my $biblio = $builder->build_sample_biblio();
72 my $item = $builder->build_sample_item({
73 biblionumber => $biblio->biblionumber,
75 my $return_claim = $item->return_claim;
76 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
78 my $claim1 = $builder->build_object(
80 class => 'Koha::Checkouts::ReturnClaims',
81 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
84 my $claim2 = $builder->build_object(
86 class => 'Koha::Checkouts::ReturnClaims',
87 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
91 $return_claim = $item->return_claim;
92 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
93 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
95 $claim2->resolution('test')->store();
96 $return_claim = $item->return_claim;
97 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
99 $claim1->resolution('test')->store();
100 $return_claim = $item->return_claim;
101 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
103 $schema->storage->txn_rollback;
106 subtest 'tracked_links relationship' => sub {
109 my $biblio = $builder->build_sample_biblio();
110 my $item = $builder->build_sample_item({
111 biblionumber => $biblio->biblionumber,
113 my $tracked_links = $item->tracked_links;
114 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
115 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
116 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
117 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
119 is($item->tracked_links()->count,2,"Two tracked links found");
122 subtest 'is_bundle tests' => sub {
125 $schema->storage->txn_begin;
127 my $item = $builder->build_sample_item();
129 my $is_bundle = $item->is_bundle;
130 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
132 my $item2 = $builder->build_sample_item();
133 $schema->resultset('ItemBundle')
134 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
136 $is_bundle = $item->is_bundle;
137 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
139 $schema->storage->txn_rollback;
142 subtest 'in_bundle tests' => sub {
145 $schema->storage->txn_begin;
147 my $item = $builder->build_sample_item();
149 my $in_bundle = $item->in_bundle;
150 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
152 my $host_item = $builder->build_sample_item();
153 $schema->resultset('ItemBundle')
154 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
156 $in_bundle = $item->in_bundle;
157 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
159 $schema->storage->txn_rollback;
162 subtest 'bundle_items tests' => sub {
165 $schema->storage->txn_begin;
167 my $host_item = $builder->build_sample_item();
168 my $bundle_items = $host_item->bundle_items;
169 is( ref($bundle_items), 'Koha::Items',
170 'bundle_items returns a Koha::Items object set' );
171 is( $bundle_items->count, 0,
172 'bundle_items set is empty when no items are bundled' );
174 my $bundle_item1 = $builder->build_sample_item();
175 my $bundle_item2 = $builder->build_sample_item();
176 my $bundle_item3 = $builder->build_sample_item();
177 $schema->resultset('ItemBundle')
179 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
180 $schema->resultset('ItemBundle')
182 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
183 $schema->resultset('ItemBundle')
185 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
187 $bundle_items = $host_item->bundle_items;
188 is( $bundle_items->count, 3,
189 'bundle_items returns all the bundled items in the set' );
191 $schema->storage->txn_rollback;
194 subtest 'bundle_host tests' => sub {
197 $schema->storage->txn_begin;
199 my $host_item = $builder->build_sample_item();
200 my $bundle_item1 = $builder->build_sample_item();
201 my $bundle_item2 = $builder->build_sample_item();
202 $schema->resultset('ItemBundle')
204 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
206 my $bundle_host = $bundle_item1->bundle_host;
207 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
208 $bundle_host = $bundle_item2->bundle_host;
209 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
210 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
212 $schema->storage->txn_rollback;
215 subtest 'add_to_bundle tests' => sub {
218 $schema->storage->txn_begin;
220 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
222 my $host_item = $builder->build_sample_item();
223 my $bundle_item1 = $builder->build_sample_item();
224 my $bundle_item2 = $builder->build_sample_item();
226 throws_ok { $host_item->add_to_bundle($host_item) }
227 'Koha::Exceptions::Item::Bundle::IsBundle',
228 'Exception thrown if you try to add the item to itself';
230 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
231 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
233 throws_ok { $host_item->add_to_bundle($bundle_item1) }
234 'Koha::Exceptions::Object::DuplicateID',
235 'Exception thrown if you try to add the same item twice';
237 throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
238 'Koha::Exceptions::Item::Bundle::IsBundle',
239 'Exception thrown if you try to add an item to a bundled item';
241 throws_ok { $bundle_item2->add_to_bundle($host_item) }
242 'Koha::Exceptions::Item::Bundle::IsBundle',
243 'Exception thrown if you try to add a bundle host to a bundle item';
245 $schema->storage->txn_rollback;
248 subtest 'remove_from_bundle tests' => sub {
251 $schema->storage->txn_begin;
253 my $host_item = $builder->build_sample_item();
254 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
255 $schema->resultset('ItemBundle')
257 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
259 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
260 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
261 $bundle_item1 = $bundle_item1->get_from_storage;
262 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
264 $schema->storage->txn_rollback;
267 subtest 'hidden_in_opac() tests' => sub {
271 $schema->storage->txn_begin;
273 my $item = $builder->build_sample_item({ itemlost => 2 });
276 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
277 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
279 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
280 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
282 # enable hidelostitems to verify correct behaviour
283 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
284 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
286 # disable hidelostitems
287 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
288 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
290 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
292 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
296 $schema->storage->txn_rollback;
299 subtest 'has_pending_hold() tests' => sub {
303 $schema->storage->txn_begin;
305 my $dbh = C4::Context->dbh;
306 my $item = $builder->build_sample_item({ itemlost => 0 });
307 my $itemnumber = $item->itemnumber;
309 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
310 ok( $item->has_pending_hold, "Yes, we have a pending hold");
311 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
312 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
314 $schema->storage->txn_rollback;
317 subtest "as_marc_field() tests" => sub {
319 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
320 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
322 my @schema_columns = $schema->resultset('Item')->result_source->columns;
323 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
325 plan tests => 2 * (scalar @mapped_columns + 1) + 4;
327 $schema->storage->txn_begin;
329 my $item = $builder->build_sample_item;
330 # Make sure it has at least one undefined attribute
331 $item->set({ replacementprice => undef })->store->discard_changes;
333 # Tests with the mss parameter
334 my $marc_field = $item->as_marc_field({ mss => $mss });
339 'Generated field set the right tag number'
342 foreach my $column ( @mapped_columns ) {
343 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
344 is( $marc_field->subfield($tagsubfield),
345 $item->$column, "Value is mapped correctly for column $column" );
348 # Tests without the mss parameter
349 $marc_field = $item->as_marc_field();
354 'Generated field set the right tag number'
357 foreach my $column (@mapped_columns) {
358 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
359 is( $marc_field->subfield($tagsubfield),
360 $item->$column, "Value is mapped correctly for column $column" );
363 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
366 tagfield => $itemtag,
370 Koha::MarcSubfieldStructure->new(
373 tagfield => $itemtag,
379 my @unlinked_subfields;
380 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
381 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
383 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
384 Koha::MarcSubfieldStructures->search(
385 { frameworkcode => '', tagfield => $itemtag } )
386 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
388 $marc_field = $item->as_marc_field;
390 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
391 my @subfields = $marc_field->subfields;
392 my $result = all { defined $_->[1] } @subfields;
393 ok( $result, 'There are no undef subfields' );
394 my @ordered_subfields = sort {
395 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
396 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
398 is_deeply(\@subfields, \@ordered_subfields);
400 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
401 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
403 $schema->storage->txn_rollback;
404 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
407 subtest 'pickup_locations' => sub {
410 $schema->storage->txn_begin;
412 my $dbh = C4::Context->dbh;
414 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
415 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
416 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
417 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
418 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
419 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
420 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
421 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
423 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
424 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
427 $library1->branchcode, $library2->branchcode,
428 $library3->branchcode, $library4->branchcode
431 my $item1 = $builder->build_sample_item(
433 homebranch => $library1->branchcode,
434 holdingbranch => $library2->branchcode,
440 my $item3 = $builder->build_sample_item(
442 homebranch => $library3->branchcode,
443 holdingbranch => $library4->branchcode,
445 itype => $item1->itype,
449 Koha::CirculationRules->set_rules(
451 categorycode => undef,
452 itemtype => $item1->itype,
455 reservesallowed => 25,
461 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
462 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
465 "1-1-from_home_library-any" => 3,
466 "1-1-from_home_library-holdgroup" => 2,
467 "1-1-from_home_library-patrongroup" => 2,
468 "1-1-from_home_library-homebranch" => 1,
469 "1-1-from_home_library-holdingbranch" => 1,
470 "1-1-from_any_library-any" => 3,
471 "1-1-from_any_library-holdgroup" => 2,
472 "1-1-from_any_library-patrongroup" => 2,
473 "1-1-from_any_library-homebranch" => 1,
474 "1-1-from_any_library-holdingbranch" => 1,
475 "1-1-from_local_hold_group-any" => 3,
476 "1-1-from_local_hold_group-holdgroup" => 2,
477 "1-1-from_local_hold_group-patrongroup" => 2,
478 "1-1-from_local_hold_group-homebranch" => 1,
479 "1-1-from_local_hold_group-holdingbranch" => 1,
480 "1-4-from_home_library-any" => 0,
481 "1-4-from_home_library-holdgroup" => 0,
482 "1-4-from_home_library-patrongroup" => 0,
483 "1-4-from_home_library-homebranch" => 0,
484 "1-4-from_home_library-holdingbranch" => 0,
485 "1-4-from_any_library-any" => 3,
486 "1-4-from_any_library-holdgroup" => 2,
487 "1-4-from_any_library-patrongroup" => 1,
488 "1-4-from_any_library-homebranch" => 1,
489 "1-4-from_any_library-holdingbranch" => 1,
490 "1-4-from_local_hold_group-any" => 0,
491 "1-4-from_local_hold_group-holdgroup" => 0,
492 "1-4-from_local_hold_group-patrongroup" => 0,
493 "1-4-from_local_hold_group-homebranch" => 0,
494 "1-4-from_local_hold_group-holdingbranch" => 0,
495 "3-1-from_home_library-any" => 0,
496 "3-1-from_home_library-holdgroup" => 0,
497 "3-1-from_home_library-patrongroup" => 0,
498 "3-1-from_home_library-homebranch" => 0,
499 "3-1-from_home_library-holdingbranch" => 0,
500 "3-1-from_any_library-any" => 3,
501 "3-1-from_any_library-holdgroup" => 1,
502 "3-1-from_any_library-patrongroup" => 2,
503 "3-1-from_any_library-homebranch" => 0,
504 "3-1-from_any_library-holdingbranch" => 1,
505 "3-1-from_local_hold_group-any" => 0,
506 "3-1-from_local_hold_group-holdgroup" => 0,
507 "3-1-from_local_hold_group-patrongroup" => 0,
508 "3-1-from_local_hold_group-homebranch" => 0,
509 "3-1-from_local_hold_group-holdingbranch" => 0,
510 "3-4-from_home_library-any" => 0,
511 "3-4-from_home_library-holdgroup" => 0,
512 "3-4-from_home_library-patrongroup" => 0,
513 "3-4-from_home_library-homebranch" => 0,
514 "3-4-from_home_library-holdingbranch" => 0,
515 "3-4-from_any_library-any" => 3,
516 "3-4-from_any_library-holdgroup" => 1,
517 "3-4-from_any_library-patrongroup" => 1,
518 "3-4-from_any_library-homebranch" => 0,
519 "3-4-from_any_library-holdingbranch" => 1,
520 "3-4-from_local_hold_group-any" => 3,
521 "3-4-from_local_hold_group-holdgroup" => 1,
522 "3-4-from_local_hold_group-patrongroup" => 1,
523 "3-4-from_local_hold_group-homebranch" => 0,
524 "3-4-from_local_hold_group-holdingbranch" => 1
528 my ( $item, $patron, $ha, $hfp, $results ) = @_;
530 Koha::CirculationRules->set_rules(
536 hold_fulfillment_policy => $hfp,
537 returnbranch => 'any'
542 $ha eq 'from_local_hold_group' ? 'holdgroup'
544 $ha eq 'from_any_library' ? 'any'
549 my $pickup_location = $_;
550 grep { $pickup_location->branchcode eq $_ } @branchcodes
551 } $item->pickup_locations( { patron => $patron } )->as_list;
554 scalar(@pl) eq $results->{
555 $item->copynumber . '-'
556 . $patron->firstname . '-'
566 . ', hold_fulfillment_policy: '
570 $item->copynumber . '-'
571 . $patron->firstname . '-'
582 foreach my $item ($item1, $item3) {
583 foreach my $patron ($patron1, $patron4) {
584 #holdallowed 1: homebranch, 2: any, 3: holdgroup
585 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
586 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
587 _doTest($item, $patron, $ha, $hfp, $results);
593 # Now test that branchtransferlimits will further filter the pickup locations
595 my $item_no_ccode = $builder->build_sample_item(
597 homebranch => $library1->branchcode,
598 holdingbranch => $library2->branchcode,
599 itype => $item1->itype,
603 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
604 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
605 Koha::CirculationRules->set_rules(
608 itemtype => $item1->itype,
610 holdallowed => 'from_home_library',
611 hold_fulfillment_policy => 1,
612 returnbranch => 'any'
616 $builder->build_object(
618 class => 'Koha::Item::Transfer::Limits',
620 toBranch => $library1->branchcode,
621 fromBranch => $library2->branchcode,
622 itemtype => $item1->itype,
628 my @pickup_locations = map {
629 my $pickup_location = $_;
630 grep { $pickup_location->branchcode eq $_ } @branchcodes
631 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
633 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
635 $builder->build_object(
637 class => 'Koha::Item::Transfer::Limits',
639 toBranch => $library4->branchcode,
640 fromBranch => $library2->branchcode,
641 itemtype => $item1->itype,
647 @pickup_locations = map {
648 my $pickup_location = $_;
649 grep { $pickup_location->branchcode eq $_ } @branchcodes
650 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
652 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
654 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
655 @pickup_locations = map {
656 my $pickup_location = $_;
657 grep { $pickup_location->branchcode eq $_ } @branchcodes
658 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
659 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
661 @pickup_locations = map {
662 my $pickup_location = $_;
663 grep { $pickup_location->branchcode eq $_ } @branchcodes
664 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
665 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");
667 $builder->build_object(
669 class => 'Koha::Item::Transfer::Limits',
671 toBranch => $library2->branchcode,
672 fromBranch => $library2->branchcode,
674 ccode => $item1->ccode,
679 @pickup_locations = map {
680 my $pickup_location = $_;
681 grep { $pickup_location->branchcode eq $_ } @branchcodes
682 } $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,
692 ccode => $item1->ccode,
697 @pickup_locations = map {
698 my $pickup_location = $_;
699 grep { $pickup_location->branchcode eq $_ } @branchcodes
700 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
701 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
703 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
705 $schema->storage->txn_rollback;
708 subtest 'request_transfer' => sub {
710 $schema->storage->txn_begin;
712 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
713 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
714 my $item = $builder->build_sample_item(
716 homebranch => $library1->branchcode,
717 holdingbranch => $library2->branchcode,
721 # Mandatory fields tests
722 throws_ok { $item->request_transfer( { to => $library1 } ) }
723 'Koha::Exceptions::MissingParameter',
724 'Exception thrown if `reason` parameter is missing';
726 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
727 'Koha::Exceptions::MissingParameter',
728 'Exception thrown if `to` parameter is missing';
731 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
732 is( ref($transfer), 'Koha::Item::Transfer',
733 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
735 my $original_transfer = $transfer->get_from_storage;
737 # Transfer already in progress
738 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
739 'Koha::Exceptions::Item::Transfer::InQueue',
740 'Exception thrown if transfer is already in progress';
743 is( ref( $exception->transfer ),
744 'Koha::Item::Transfer',
745 'The exception contains the found Koha::Item::Transfer' );
748 my $queued_transfer = $item->request_transfer(
749 { to => $library2, reason => 'Manual', enqueue => 1 } );
750 is( ref($queued_transfer), 'Koha::Item::Transfer',
751 'Koha::Item->request_transfer allowed when enqueue is set' );
752 my $transfers = $item->get_transfers;
753 is($transfers->count, 2, "There are now 2 live transfers in the queue");
754 $transfer = $transfer->get_from_storage;
755 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
756 $queued_transfer->datearrived(dt_from_string)->store();
759 my $replaced_transfer = $item->request_transfer(
760 { to => $library2, reason => 'Manual', replace => 1 } );
761 is( ref($replaced_transfer), 'Koha::Item::Transfer',
762 'Koha::Item->request_transfer allowed when replace is set' );
763 $original_transfer->discard_changes;
764 ok($original_transfer->datecancelled, "Original transfer cancelled");
765 $transfers = $item->get_transfers;
766 is($transfers->count, 1, "There is only 1 live transfer in the queue");
767 $replaced_transfer->datearrived(dt_from_string)->store();
769 # BranchTransferLimits
770 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
771 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
772 my $limit = Koha::Item::Transfer::Limit->new({
773 fromBranch => $library2->branchcode,
774 toBranch => $library1->branchcode,
775 itemtype => $item->effective_itemtype,
778 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
779 'Koha::Exceptions::Item::Transfer::Limit',
780 'Exception thrown if transfer is prevented by limits';
782 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
783 is( ref($forced_transfer), 'Koha::Item::Transfer',
784 'Koha::Item->request_transfer allowed when ignore_limits is set'
787 $schema->storage->txn_rollback;
790 subtest 'deletion' => sub {
793 $schema->storage->txn_begin;
795 my $biblio = $builder->build_sample_biblio();
797 my $item = $builder->build_sample_item(
799 biblionumber => $biblio->biblionumber,
802 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
804 my $deleted_item = $item->move_to_deleted;
805 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
806 ; # FIXME This should be Koha::Deleted::Item
807 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
809 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
810 $item = $builder->build_sample_item(
812 biblionumber => $biblio->biblionumber,
816 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
819 my $library = $builder->build_object({ class => 'Koha::Libraries' });
820 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
821 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
823 my $patron = $builder->build_object({class => 'Koha::Patrons'});
824 $item = $builder->build_sample_item({ library => $library->branchcode });
827 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
830 @{$item->safe_to_delete->messages}[0]->message,
832 'Koha::Item->safe_to_delete reports item on loan',
836 @{$item->safe_to_delete->messages}[0]->message,
838 'item that is on loan cannot be deleted',
842 ! $item->safe_to_delete,
843 'Koha::Item->safe_to_delete shows item NOT safe to delete'
846 AddReturn( $item->barcode, $library->branchcode );
849 t::lib::Mocks::mock_preference('IndependentBranches', 1);
850 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
853 @{$item_2->safe_to_delete->messages}[0]->message,
855 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
859 @{$item_2->safe_to_delete->messages}[0]->message,
861 'IndependentBranches prevents deletion at another branch',
866 { # codeblock to limit scope of $module->mock
868 my $module = Test::MockModule->new('C4::Items');
869 $module->mock( GetAnalyticsCount => sub { return 1 } );
871 $item->discard_changes;
873 @{$item->safe_to_delete->messages}[0]->message,
875 'Koha::Item->safe_to_delete reports linked analytics',
879 @{$item->safe_to_delete->messages}[0]->message,
881 'Linked analytics prevents deletion of item',
887 $item->safe_to_delete,
888 'Koha::Item->safe_to_delete shows item safe to delete'
893 my $test_item = Koha::Items->find( $item->itemnumber );
895 is( $test_item, undef,
896 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
899 subtest 'holds tests' => sub {
904 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
906 $schema->storage->txn_begin;
908 my $item = $builder->build_sample_item;
910 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
911 my $safe_to_delete = $item->safe_to_delete;
913 ok( !$safe_to_delete, 'Cannot delete' );
915 @{ $safe_to_delete->messages }[0]->message,
917 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
922 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
923 $safe_to_delete = $item->safe_to_delete;
925 ok( !$safe_to_delete, 'Cannot delete' );
927 @{ $safe_to_delete->messages }[0]->message,
929 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
934 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
935 $safe_to_delete = $item->safe_to_delete;
937 ok( !$safe_to_delete, 'Cannot delete' );
939 @{ $safe_to_delete->messages }[0]->message,
941 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
946 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
947 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
949 $safe_to_delete = $item->safe_to_delete;
951 ok( !$safe_to_delete );
954 @{ $safe_to_delete->messages}[0]->message,
955 'last_item_for_hold',
956 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
959 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
961 ok( $item->safe_to_delete );
963 $schema->storage->txn_rollback;
966 $schema->storage->txn_rollback;
969 subtest 'renewal_branchcode' => sub {
972 $schema->storage->txn_begin;
974 my $item = $builder->build_sample_item();
975 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
976 my $checkout = $builder->build_object({
977 class => 'Koha::Checkouts',
979 itemnumber => $item->itemnumber,
984 C4::Context->interface( 'intranet' );
985 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
987 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
988 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
989 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
990 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
992 C4::Context->interface( 'opac' );
994 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
995 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
996 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
998 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
999 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1000 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1002 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1003 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1004 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");
1006 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1007 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1008 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");
1010 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1011 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1012 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");
1014 $schema->storage->txn_rollback;
1017 subtest 'Tests for itemtype' => sub {
1019 $schema->storage->txn_begin;
1021 my $biblio = $builder->build_sample_biblio;
1022 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1023 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1025 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1026 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1027 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1028 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1030 $schema->storage->txn_rollback;
1033 subtest 'get_transfers' => sub {
1035 $schema->storage->txn_begin;
1037 my $item = $builder->build_sample_item();
1039 my $transfers = $item->get_transfers();
1040 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1041 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1043 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1045 my $transfer_1 = $builder->build_object(
1047 class => 'Koha::Item::Transfers',
1049 itemnumber => $item->itemnumber,
1050 frombranch => $item->holdingbranch,
1051 tobranch => $library_to->branchcode,
1054 datearrived => undef,
1055 datecancelled => undef,
1056 daterequested => \'NOW()'
1061 $transfers = $item->get_transfers();
1062 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1064 my $transfer_2 = $builder->build_object(
1066 class => 'Koha::Item::Transfers',
1068 itemnumber => $item->itemnumber,
1069 frombranch => $item->holdingbranch,
1070 tobranch => $library_to->branchcode,
1073 datearrived => undef,
1074 datecancelled => undef,
1075 daterequested => \'NOW()'
1080 my $transfer_3 = $builder->build_object(
1082 class => 'Koha::Item::Transfers',
1084 itemnumber => $item->itemnumber,
1085 frombranch => $item->holdingbranch,
1086 tobranch => $library_to->branchcode,
1089 datearrived => undef,
1090 datecancelled => undef,
1091 daterequested => \'NOW()'
1096 $transfers = $item->get_transfers();
1097 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1098 my $result_1 = $transfers->next;
1099 my $result_2 = $transfers->next;
1100 my $result_3 = $transfers->next;
1101 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1102 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1103 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1105 $transfer_2->datesent(\'NOW()')->store;
1106 $transfers = $item->get_transfers();
1107 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1108 $result_1 = $transfers->next;
1109 $result_2 = $transfers->next;
1110 $result_3 = $transfers->next;
1111 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1112 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1113 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1115 $transfer_2->datearrived(\'NOW()')->store;
1116 $transfers = $item->get_transfers();
1117 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1118 $result_1 = $transfers->next;
1119 $result_2 = $transfers->next;
1120 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1121 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1123 $transfer_1->datecancelled(\'NOW()')->store;
1124 $transfers = $item->get_transfers();
1125 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1126 $result_1 = $transfers->next;
1127 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1129 $schema->storage->txn_rollback;
1132 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1135 $schema->storage->txn_begin;
1137 my $biblio = $builder->build_sample_biblio();
1138 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1140 my $orders = $item->orders;
1141 is ($orders->count, 0, 'No order on this item yet');
1143 my $order_note = 'Order for ' . $item->itemnumber;
1145 my $aq_order1 = $builder->build_object({
1146 class => 'Koha::Acquisition::Orders',
1148 biblionumber => $biblio->biblionumber,
1149 order_internalnote => $order_note,
1152 my $aq_order2 = $builder->build_object({
1153 class => 'Koha::Acquisition::Orders',
1155 biblionumber => $biblio->biblionumber,
1158 my $aq_order_item1 = $builder->build({
1159 source => 'AqordersItem',
1161 ordernumber => $aq_order1->ordernumber,
1162 itemnumber => $item->itemnumber,
1166 $orders = $item->orders;
1167 is ($orders->count, 1, 'One order found by item with the relationship');
1168 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1171 subtest 'move_to_biblio() tests' => sub {
1174 $schema->storage->txn_begin;
1176 my $dbh = C4::Context->dbh;
1178 my $source_biblio = $builder->build_sample_biblio();
1179 my $target_biblio = $builder->build_sample_biblio();
1181 my $source_biblionumber = $source_biblio->biblionumber;
1182 my $target_biblionumber = $target_biblio->biblionumber;
1184 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1185 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1186 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1188 my $itemnumber1 = $item1->itemnumber;
1189 my $itemnumber2 = $item2->itemnumber;
1191 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1193 my $patron = $builder->build_object({
1194 class => 'Koha::Patrons',
1195 value => { branchcode => $library->branchcode }
1197 my $borrowernumber = $patron->borrowernumber;
1199 my $aq_budget = $builder->build({
1200 source => 'Aqbudget',
1202 budget_notes => 'test',
1206 my $aq_order1 = $builder->build_object({
1207 class => 'Koha::Acquisition::Orders',
1209 biblionumber => $source_biblionumber,
1210 budget_id => $aq_budget->{budget_id},
1213 my $aq_order_item1 = $builder->build({
1214 source => 'AqordersItem',
1216 ordernumber => $aq_order1->ordernumber,
1217 itemnumber => $itemnumber1,
1220 my $aq_order2 = $builder->build_object({
1221 class => 'Koha::Acquisition::Orders',
1223 biblionumber => $source_biblionumber,
1224 budget_id => $aq_budget->{budget_id},
1227 my $aq_order_item2 = $builder->build({
1228 source => 'AqordersItem',
1230 ordernumber => $aq_order2->ordernumber,
1231 itemnumber => $itemnumber2,
1235 my $bib_level_hold = $builder->build_object({
1236 class => 'Koha::Holds',
1238 biblionumber => $source_biblionumber,
1239 itemnumber => undef,
1242 my $item_level_hold1 = $builder->build_object({
1243 class => 'Koha::Holds',
1245 biblionumber => $source_biblionumber,
1246 itemnumber => $itemnumber1,
1249 my $item_level_hold2 = $builder->build_object({
1250 class => 'Koha::Holds',
1252 biblionumber => $source_biblionumber,
1253 itemnumber => $itemnumber2,
1257 my $tmp_holdsqueue1 = $builder->build({
1258 source => 'TmpHoldsqueue',
1260 borrowernumber => $borrowernumber,
1261 biblionumber => $source_biblionumber,
1262 itemnumber => $itemnumber1,
1265 my $tmp_holdsqueue2 = $builder->build({
1266 source => 'TmpHoldsqueue',
1268 borrowernumber => $borrowernumber,
1269 biblionumber => $source_biblionumber,
1270 itemnumber => $itemnumber2,
1273 my $hold_fill_target1 = $builder->build({
1274 source => 'HoldFillTarget',
1276 borrowernumber => $borrowernumber,
1277 biblionumber => $source_biblionumber,
1278 itemnumber => $itemnumber1,
1281 my $hold_fill_target2 = $builder->build({
1282 source => 'HoldFillTarget',
1284 borrowernumber => $borrowernumber,
1285 biblionumber => $source_biblionumber,
1286 itemnumber => $itemnumber2,
1289 my $linktracker1 = $builder->build({
1290 source => 'Linktracker',
1292 borrowernumber => $borrowernumber,
1293 biblionumber => $source_biblionumber,
1294 itemnumber => $itemnumber1,
1297 my $linktracker2 = $builder->build({
1298 source => 'Linktracker',
1300 borrowernumber => $borrowernumber,
1301 biblionumber => $source_biblionumber,
1302 itemnumber => $itemnumber2,
1306 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1307 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1309 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1310 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');
1312 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1313 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1314 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1315 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1316 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1317 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1319 $aq_order1->discard_changes;
1320 $aq_order2->discard_changes;
1321 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1322 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1324 $bib_level_hold->discard_changes;
1325 $item_level_hold1->discard_changes;
1326 $item_level_hold2->discard_changes;
1327 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1328 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1329 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1331 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1332 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1333 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1334 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1336 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1337 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1338 # Why does ->biblionumber return a Biblio object???
1339 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1340 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1342 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1343 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1344 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1345 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1347 $schema->storage->txn_rollback;
1350 subtest 'columns_to_str' => sub {
1353 $schema->storage->txn_begin;
1355 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1357 my $cache = Koha::Caches->get_instance();
1358 $cache->clear_from_cache("MarcStructure-0-");
1359 $cache->clear_from_cache("MarcStructure-1-");
1360 $cache->clear_from_cache("MarcSubfieldStructure-");
1361 $cache->clear_from_cache("libraries:name");
1362 $cache->clear_from_cache("itemtype:description:en");
1363 $cache->clear_from_cache("cn_sources:description");
1364 $cache->clear_from_cache("AV_descriptions:LOST");
1366 # Creating subfields 'é', 'è' that are not linked with a kohafield
1367 Koha::MarcSubfieldStructures->search(
1369 frameworkcode => '',
1370 tagfield => $itemtag,
1371 tagsubfield => ['é', 'è'],
1373 )->delete; # In case it exist already
1375 # é is not linked with a AV
1376 # è is linked with AV branches
1377 Koha::MarcSubfieldStructure->new(
1379 frameworkcode => '',
1380 tagfield => $itemtag,
1381 tagsubfield => 'é',
1384 defaultvalue => 'ééé',
1388 Koha::MarcSubfieldStructure->new(
1390 frameworkcode => '',
1391 tagfield => $itemtag,
1392 tagsubfield => 'è',
1395 defaultvalue => 'èèè',
1397 authorised_value => 'branches',
1401 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1402 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1403 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1404 my $dateaccessioned = '2020-12-15';
1405 my $library = Koha::Libraries->search->next;
1406 my $branchcode = $library->branchcode;
1408 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1410 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1411 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1412 xmlns="http://www.loc.gov/MARC21/slim">
1415 <leader> a </leader>
1416 <datafield tag="999" ind1=" " ind2=" ">
1417 <subfield code="é">value é</subfield>
1418 <subfield code="è">$branchcode</subfield>
1426 itemlost => $lost_av->authorised_value,
1427 dateaccessioned => $dateaccessioned,
1428 more_subfields_xml => $some_marc_xml,
1432 $item = $item->get_from_storage;
1434 my $s = $item->columns_to_str;
1435 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1436 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1437 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1438 is( $s->{'è'}, $library->branchname );
1440 $cache->clear_from_cache("MarcStructure-0-");
1441 $cache->clear_from_cache("MarcStructure-1-");
1442 $cache->clear_from_cache("MarcSubfieldStructure-");
1443 $cache->clear_from_cache("libraries:name");
1444 $cache->clear_from_cache("itemtype:description:en");
1445 $cache->clear_from_cache("cn_sources:description");
1446 $cache->clear_from_cache("AV_descriptions:LOST");
1448 $schema->storage->txn_rollback;
1451 subtest 'strings_map() tests' => sub {
1455 $schema->storage->txn_begin;
1457 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1459 my $cache = Koha::Caches->get_instance();
1460 $cache->clear_from_cache("MarcStructure-0-");
1461 $cache->clear_from_cache("MarcStructure-1-");
1462 $cache->clear_from_cache("MarcSubfieldStructure-");
1463 $cache->clear_from_cache("libraries:name");
1464 $cache->clear_from_cache("itemtype:description:en");
1465 $cache->clear_from_cache("cn_sources:description");
1467 # Recreating subfields just to be sure tests will be ok
1472 Koha::MarcSubfieldStructures->search(
1474 frameworkcode => '',
1475 tagfield => $itemtag,
1476 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1478 )->delete; # In case it exist already
1480 Koha::MarcSubfieldStructure->new(
1482 authorised_value => 'LOST',
1484 frameworkcode => '',
1485 kohafield => 'items.itemlost',
1488 tagfield => $itemtag,
1492 Koha::MarcSubfieldStructure->new(
1494 authorised_value => 'cn_source',
1496 frameworkcode => '',
1497 kohafield => 'items.cn_source',
1500 tagfield => $itemtag,
1504 Koha::MarcSubfieldStructure->new(
1506 authorised_value => '',
1508 frameworkcode => '',
1509 kohafield => 'items.materials',
1512 tagfield => $itemtag,
1516 Koha::MarcSubfieldStructure->new(
1518 authorised_value => 'branches',
1520 frameworkcode => '',
1521 kohafield => 'items.homebranch',
1524 tagfield => $itemtag,
1528 Koha::MarcSubfieldStructure->new(
1530 authorised_value => 'itemtypes',
1532 frameworkcode => '',
1533 kohafield => 'items.itype',
1536 tagfield => $itemtag,
1541 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1542 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1543 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1544 my $item = $builder->build_sample_item(
1546 biblionumber => $biblio->id,
1547 library => $library->id
1551 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1552 my $lost_av = $builder->build_object(
1554 class => 'Koha::AuthorisedValues',
1556 authorised_value => 3,
1558 lib => 'internal description',
1559 lib_opac => 'public description',
1564 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1565 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1566 my $class_source = $builder->build_object(
1568 class => 'Koha::ClassSources',
1570 class_sort_rule => $class_sort_rule->class_sort_rule,
1571 class_split_rule => $class_split_rule->class_split_rule,
1578 cn_source => $class_source->id,
1579 itemlost => $lost_av->authorised_value,
1580 itype => $itype->itemtype,
1581 materials => 'Suff',
1583 )->store->discard_changes;
1585 my $strings = $item->strings_map;
1587 subtest 'unmapped field tests' => sub {
1591 ok( !exists $strings->{materials}, "Unmapped field not present" );
1594 subtest 'av handling' => sub {
1598 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1599 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1600 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1601 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1604 subtest 'cn_source handling' => sub {
1608 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1609 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1610 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1613 subtest 'branches handling' => sub {
1617 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1618 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1619 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1622 subtest 'itemtype handling' => sub {
1626 ok( exists $strings->{itype}, "'itype' entry exists" );
1627 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1628 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1631 subtest 'public flag tests' => sub {
1635 $strings = $item->strings_map( { public => 1 } );
1637 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1638 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1639 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1640 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1643 $cache->clear_from_cache("MarcStructure-0-");
1644 $cache->clear_from_cache("MarcStructure-1-");
1645 $cache->clear_from_cache("MarcSubfieldStructure-");
1646 $cache->clear_from_cache("libraries:name");
1647 $cache->clear_from_cache("itemtype:description:en");
1648 $cache->clear_from_cache("cn_sources:description");
1650 $schema->storage->txn_rollback;
1653 subtest 'store() tests' => sub {
1657 subtest 'dateaccessioned handling' => sub {
1661 $schema->storage->txn_begin;
1663 my $item = $builder->build_sample_item;
1665 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1667 # reset dateaccessioned on the DB
1668 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1669 $item->discard_changes;
1671 ok( !defined $item->dateaccessioned );
1674 $item->replacementprice(100)->store->discard_changes;
1676 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1678 $schema->storage->txn_rollback;
1681 subtest '_set_found_trigger() tests' => sub {
1685 $schema->storage->txn_begin;
1687 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1688 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1690 # Add a lost item debit
1691 my $debit = $patron->account->add_debit(
1695 item_id => $item->id,
1696 interface => 'intranet',
1700 # Add a lost item processing fee
1701 my $processing_debit = $patron->account->add_debit(
1704 type => 'PROCESSING',
1705 item_id => $item->id,
1706 interface => 'intranet',
1710 my $lostreturn_policy = {
1711 lostreturn => 'charge',
1712 processingreturn => 'refund'
1715 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1716 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1718 # simulate it was found
1719 $item->set( { itemlost => 0 } )->store;
1721 my $messages = $item->object_messages;
1723 my $message_1 = $messages->[0];
1725 is( $message_1->type, 'info', 'type is correct' );
1726 is( $message_1->message, 'lost_refunded', 'message is correct' );
1728 # Find the refund credit
1729 my $credit = $debit->credits->next;
1732 $message_1->payload,
1733 { credit_id => $credit->id },
1737 my $message_2 = $messages->[1];
1739 is( $message_2->type, 'info', 'type is correct' );
1740 is( $message_2->message, 'lost_charge', 'message is correct' );
1741 is( $message_2->payload, undef, 'no payload' );
1743 my $message_3 = $messages->[2];
1744 is( $message_3->message, 'processing_refunded', 'message is correct' );
1746 my $processing_credit = $processing_debit->credits->next;
1748 $message_3->payload,
1749 { credit_id => $processing_credit->id },
1753 # Let's build a new item
1754 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1755 $item->set( { itemlost => 0 } )->store;
1757 $messages = $item->object_messages;
1758 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1760 $schema->storage->txn_rollback;
1763 subtest 'holds_queue update tests' => sub {
1767 $schema->storage->txn_begin;
1769 my $biblio = $builder->build_sample_biblio;
1771 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1772 $mock->mock( 'enqueue', sub {
1773 my ( $self, $args ) = @_;
1775 $args->{biblio_ids},
1777 '->store triggers a holds queue update for the related biblio'
1781 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1784 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1787 $item->set({ reserves => 1 })->store;
1789 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1791 $item->set({ reserves => 0 })->store;
1793 $schema->storage->txn_rollback;
1797 subtest 'Recalls tests' => sub {
1801 $schema->storage->txn_begin;
1803 my $item1 = $builder->build_sample_item;
1804 my $biblio = $item1->biblio;
1805 my $branchcode = $item1->holdingbranch;
1806 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1807 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1808 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1809 my $item2 = $builder->build_object(
1810 { class => 'Koha::Items',
1811 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1815 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1816 t::lib::Mocks::mock_preference('UseRecalls', 1);
1818 my $recall1 = Koha::Recall->new(
1819 { patron_id => $patron1->borrowernumber,
1820 created_date => \'NOW()',
1821 biblio_id => $biblio->biblionumber,
1822 pickup_library_id => $branchcode,
1823 item_id => $item1->itemnumber,
1824 expiration_date => undef,
1828 my $recall2 = Koha::Recall->new(
1829 { patron_id => $patron2->borrowernumber,
1830 created_date => \'NOW()',
1831 biblio_id => $biblio->biblionumber,
1832 pickup_library_id => $branchcode,
1833 item_id => $item1->itemnumber,
1834 expiration_date => undef,
1839 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1841 $recall2->set_cancelled;
1843 t::lib::Mocks::mock_preference('UseRecalls', 0);
1844 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1846 t::lib::Mocks::mock_preference("UseRecalls", 1);
1848 $item1->update({ notforloan => 1 });
1849 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1850 $item1->update({ notforloan => 0, itemlost => 1 });
1851 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1852 $item1->update({ itemlost => 0, withdrawn => 1 });
1853 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1854 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1856 $item1->update({ withdrawn => 0 });
1857 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1859 Koha::CirculationRules->set_rules({
1860 branchcode => $branchcode,
1861 categorycode => $patron1->categorycode,
1862 itemtype => $item1->effective_itemtype,
1864 recalls_allowed => 0,
1865 recalls_per_record => 1,
1866 on_shelf_recalls => 'all',
1869 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1871 Koha::CirculationRules->set_rules({
1872 branchcode => $branchcode,
1873 categorycode => $patron1->categorycode,
1874 itemtype => $item1->effective_itemtype,
1876 recalls_allowed => 1,
1877 recalls_per_record => 1,
1878 on_shelf_recalls => 'all',
1881 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1882 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1883 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1885 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1886 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1887 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1889 $recall1->set_cancelled;
1890 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1892 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1894 Koha::CirculationRules->set_rules({
1895 branchcode => $branchcode,
1896 categorycode => $patron1->categorycode,
1897 itemtype => $item1->effective_itemtype,
1899 recalls_allowed => 1,
1900 recalls_per_record => 1,
1901 on_shelf_recalls => 'any',
1904 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1905 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1907 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1908 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1910 $recall1 = Koha::Recall->new(
1911 { patron_id => $patron1->borrowernumber,
1912 created_date => \'NOW()',
1913 biblio_id => $biblio->biblionumber,
1914 pickup_library_id => $branchcode,
1916 expiration_date => undef,
1921 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1923 Koha::CirculationRules->set_rules({
1924 branchcode => undef,
1925 categorycode => undef,
1926 itemtype => $item1->effective_itemtype,
1928 recalls_allowed => 0,
1929 recalls_per_record => 1,
1930 on_shelf_recalls => 'any',
1933 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1935 Koha::CirculationRules->set_rules({
1936 branchcode => undef,
1937 categorycode => undef,
1938 itemtype => $item1->effective_itemtype,
1940 recalls_allowed => 1,
1941 recalls_per_record => 1,
1942 on_shelf_recalls => 'any',
1945 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1947 # check_recalls tests
1949 $recall1 = Koha::Recall->new(
1950 { patron_id => $patron2->borrowernumber,
1951 created_date => \'NOW()',
1952 biblio_id => $biblio->biblionumber,
1953 pickup_library_id => $branchcode,
1954 item_id => $item1->itemnumber,
1955 expiration_date => undef,
1959 $recall2 = Koha::Recall->new(
1960 { patron_id => $patron1->borrowernumber,
1961 created_date => \'NOW()',
1962 biblio_id => $biblio->biblionumber,
1963 pickup_library_id => $branchcode,
1965 expiration_date => undef,
1969 $recall2->set_waiting( { item => $item1 } );
1970 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
1972 # return a waiting recall
1973 my $check_recall = $item1->check_recalls;
1974 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
1976 $recall2->revert_waiting;
1978 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
1980 # return recall based on recalldate
1981 $check_recall = $item1->check_recalls;
1982 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
1984 $recall1->set_cancelled;
1986 # return a biblio-level recall
1987 $check_recall = $item1->check_recalls;
1988 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
1990 $recall2->set_cancelled;
1992 $schema->storage->txn_rollback;
1995 subtest 'Notforloan tests' => sub {
1999 $schema->storage->txn_begin;
2001 my $item1 = $builder->build_sample_item;
2002 $item1->update({ notforloan => 0 });
2003 $item1->itemtype->notforloan(0);
2004 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2005 $item1->update({ notforloan => 1 });
2006 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2007 $item1->update({ notforloan => 0 });
2008 $item1->itemtype->update({ notforloan => 1 });
2009 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2011 $schema->storage->txn_rollback;
2014 subtest 'item_group() tests' => sub {
2018 $schema->storage->txn_begin;
2020 my $biblio = $builder->build_sample_biblio();
2021 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2022 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2024 is( $item_1->item_group, undef, 'Item 1 has no item group');
2025 is( $item_2->item_group, undef, 'Item 2 has no item group');
2027 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2028 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2030 $item_group_1->add_item({ item_id => $item_1->id });
2031 $item_group_2->add_item({ item_id => $item_2->id });
2033 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2034 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2036 $schema->storage->txn_rollback;
2039 subtest 'has_pending_recall() tests' => sub {
2043 $schema->storage->txn_begin;
2045 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2046 my $item = $builder->build_sample_item;
2047 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2049 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2050 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2052 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2054 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2056 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2058 $recall->status('waiting')->store;
2060 ok( $item->has_pending_recall, 'The item has a pending recall' );
2062 $schema->storage->txn_rollback;
2065 subtest 'is_denied_renewal' => sub {
2068 $schema->storage->txn_begin;
2070 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2072 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2073 homebranch => $library->branchcode,
2077 itemcallnumber => undef,
2082 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2083 homebranch => $library->branchcode,
2086 location => 'NOPROC'
2091 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2092 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2094 $idr_rules="withdrawn: [1]";
2095 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2096 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2097 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2099 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2100 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2101 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2103 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2104 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2105 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2106 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2108 $idr_rules="itemcallnumber: [NULL]";
2109 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2110 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when NULL in pref' );
2112 $idr_rules="itemcallnumber: ['']";
2113 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2114 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2116 $idr_rules="itemnotes: [NULL]";
2117 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2118 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when NULL in pref' );
2120 $idr_rules="itemnotes: ['']";
2121 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2122 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2124 $schema->storage->txn_rollback;