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 => 25;
27 use C4::Biblio qw( GetMarcSubfieldStructure );
28 use C4::Circulation qw( AddIssue AddReturn );
33 use Koha::DateUtils qw( dt_from_string );
36 use List::MoreUtils qw(all);
38 use t::lib::TestBuilder;
41 my $schema = Koha::Database->new->schema;
42 my $builder = t::lib::TestBuilder->new;
44 subtest 'return_claims relationship' => sub {
47 $schema->storage->txn_begin;
49 my $biblio = $builder->build_sample_biblio();
50 my $item = $builder->build_sample_item({
51 biblionumber => $biblio->biblionumber,
53 my $return_claims = $item->return_claims;
54 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
55 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
56 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
57 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
59 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
61 $schema->storage->txn_rollback;
64 subtest 'return_claim accessor' => sub {
67 $schema->storage->txn_begin;
69 my $biblio = $builder->build_sample_biblio();
70 my $item = $builder->build_sample_item({
71 biblionumber => $biblio->biblionumber,
73 my $return_claim = $item->return_claim;
74 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
76 my $claim1 = $builder->build_object(
78 class => 'Koha::Checkouts::ReturnClaims',
79 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
82 my $claim2 = $builder->build_object(
84 class => 'Koha::Checkouts::ReturnClaims',
85 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
89 $return_claim = $item->return_claim;
90 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
91 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
93 $claim2->resolution('test')->store();
94 $return_claim = $item->return_claim;
95 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
97 $claim1->resolution('test')->store();
98 $return_claim = $item->return_claim;
99 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
101 $schema->storage->txn_rollback;
104 subtest 'tracked_links relationship' => sub {
107 my $biblio = $builder->build_sample_biblio();
108 my $item = $builder->build_sample_item({
109 biblionumber => $biblio->biblionumber,
111 my $tracked_links = $item->tracked_links;
112 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
113 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
114 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
115 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
117 is($item->tracked_links()->count,2,"Two tracked links found");
120 subtest 'is_bundle tests' => sub {
123 $schema->storage->txn_begin;
125 my $item = $builder->build_sample_item();
127 my $is_bundle = $item->is_bundle;
128 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
130 my $item2 = $builder->build_sample_item();
131 $schema->resultset('ItemBundle')
132 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
134 $is_bundle = $item->is_bundle;
135 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
137 $schema->storage->txn_rollback;
140 subtest 'in_bundle tests' => sub {
143 $schema->storage->txn_begin;
145 my $item = $builder->build_sample_item();
147 my $in_bundle = $item->in_bundle;
148 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
150 my $host_item = $builder->build_sample_item();
151 $schema->resultset('ItemBundle')
152 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
154 $in_bundle = $item->in_bundle;
155 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
157 $schema->storage->txn_rollback;
160 subtest 'bundle_items tests' => sub {
163 $schema->storage->txn_begin;
165 my $host_item = $builder->build_sample_item();
166 my $bundle_items = $host_item->bundle_items;
167 is( ref($bundle_items), 'Koha::Items',
168 'bundle_items returns a Koha::Items object set' );
169 is( $bundle_items->count, 0,
170 'bundle_items set is empty when no items are bundled' );
172 my $bundle_item1 = $builder->build_sample_item();
173 my $bundle_item2 = $builder->build_sample_item();
174 my $bundle_item3 = $builder->build_sample_item();
175 $schema->resultset('ItemBundle')
177 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
178 $schema->resultset('ItemBundle')
180 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
181 $schema->resultset('ItemBundle')
183 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
185 $bundle_items = $host_item->bundle_items;
186 is( $bundle_items->count, 3,
187 'bundle_items returns all the bundled items in the set' );
189 $schema->storage->txn_rollback;
192 subtest 'bundle_host tests' => sub {
195 $schema->storage->txn_begin;
197 my $host_item = $builder->build_sample_item();
198 my $bundle_item1 = $builder->build_sample_item();
199 my $bundle_item2 = $builder->build_sample_item();
200 $schema->resultset('ItemBundle')
202 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
204 my $bundle_host = $bundle_item1->bundle_host;
205 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
206 $bundle_host = $bundle_item2->bundle_host;
207 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
208 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
210 $schema->storage->txn_rollback;
213 subtest 'add_to_bundle tests' => sub {
216 $schema->storage->txn_begin;
218 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
220 my $host_item = $builder->build_sample_item();
221 my $bundle_item1 = $builder->build_sample_item();
222 my $bundle_item2 = $builder->build_sample_item();
224 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
225 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
227 throws_ok { $host_item->add_to_bundle($bundle_item1) }
228 'Koha::Exceptions::Object::DuplicateID',
229 'Exception thrown if you try to add the same item twice';
231 $schema->storage->txn_rollback;
234 subtest 'remove_from_bundle tests' => sub {
237 $schema->storage->txn_begin;
239 my $host_item = $builder->build_sample_item();
240 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
241 $schema->resultset('ItemBundle')
243 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
245 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
246 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
247 $bundle_item1 = $bundle_item1->get_from_storage;
248 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
250 $schema->storage->txn_rollback;
253 subtest 'hidden_in_opac() tests' => sub {
257 $schema->storage->txn_begin;
259 my $item = $builder->build_sample_item({ itemlost => 2 });
262 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
263 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
265 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
266 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
268 # enable hidelostitems to verify correct behaviour
269 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
270 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
272 # disable hidelostitems
273 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
274 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
276 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
278 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
282 $schema->storage->txn_rollback;
285 subtest 'has_pending_hold() tests' => sub {
289 $schema->storage->txn_begin;
291 my $dbh = C4::Context->dbh;
292 my $item = $builder->build_sample_item({ itemlost => 0 });
293 my $itemnumber = $item->itemnumber;
295 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
296 ok( $item->has_pending_hold, "Yes, we have a pending hold");
297 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
298 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
300 $schema->storage->txn_rollback;
303 subtest "as_marc_field() tests" => sub {
305 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
306 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
308 my @schema_columns = $schema->resultset('Item')->result_source->columns;
309 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
311 plan tests => 2 * (scalar @mapped_columns + 1) + 3;
313 $schema->storage->txn_begin;
315 my $item = $builder->build_sample_item;
316 # Make sure it has at least one undefined attribute
317 $item->set({ replacementprice => undef })->store->discard_changes;
319 # Tests with the mss parameter
320 my $marc_field = $item->as_marc_field({ mss => $mss });
325 'Generated field set the right tag number'
328 foreach my $column ( @mapped_columns ) {
329 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
330 is( $marc_field->subfield($tagsubfield),
331 $item->$column, "Value is mapped correctly for column $column" );
334 # Tests without the mss parameter
335 $marc_field = $item->as_marc_field();
340 'Generated field set the right tag number'
343 foreach my $column (@mapped_columns) {
344 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
345 is( $marc_field->subfield($tagsubfield),
346 $item->$column, "Value is mapped correctly for column $column" );
349 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
352 tagfield => $itemtag,
357 my @unlinked_subfields;
358 push @unlinked_subfields, X => 'Something weird';
359 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
361 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
362 Koha::MarcSubfieldStructures->search(
363 { frameworkcode => '', tagfield => $itemtag } )
364 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
366 $marc_field = $item->as_marc_field;
368 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
369 my @subfields = $marc_field->subfields;
370 my $result = all { defined $_->[1] } @subfields;
371 ok( $result, 'There are no undef subfields' );
372 my @ordered_subfields = sort {
373 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
374 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
376 is_deeply(\@subfields, \@ordered_subfields);
378 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
380 $schema->storage->txn_rollback;
381 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
384 subtest 'pickup_locations' => sub {
387 $schema->storage->txn_begin;
389 my $dbh = C4::Context->dbh;
391 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
392 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
393 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
394 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
395 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
396 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
397 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
398 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
400 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
401 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
404 $library1->branchcode, $library2->branchcode,
405 $library3->branchcode, $library4->branchcode
408 my $item1 = $builder->build_sample_item(
410 homebranch => $library1->branchcode,
411 holdingbranch => $library2->branchcode,
417 my $item3 = $builder->build_sample_item(
419 homebranch => $library3->branchcode,
420 holdingbranch => $library4->branchcode,
422 itype => $item1->itype,
426 Koha::CirculationRules->set_rules(
428 categorycode => undef,
429 itemtype => $item1->itype,
432 reservesallowed => 25,
438 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
439 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
442 "1-1-from_home_library-any" => 3,
443 "1-1-from_home_library-holdgroup" => 2,
444 "1-1-from_home_library-patrongroup" => 2,
445 "1-1-from_home_library-homebranch" => 1,
446 "1-1-from_home_library-holdingbranch" => 1,
447 "1-1-from_any_library-any" => 3,
448 "1-1-from_any_library-holdgroup" => 2,
449 "1-1-from_any_library-patrongroup" => 2,
450 "1-1-from_any_library-homebranch" => 1,
451 "1-1-from_any_library-holdingbranch" => 1,
452 "1-1-from_local_hold_group-any" => 3,
453 "1-1-from_local_hold_group-holdgroup" => 2,
454 "1-1-from_local_hold_group-patrongroup" => 2,
455 "1-1-from_local_hold_group-homebranch" => 1,
456 "1-1-from_local_hold_group-holdingbranch" => 1,
457 "1-4-from_home_library-any" => 0,
458 "1-4-from_home_library-holdgroup" => 0,
459 "1-4-from_home_library-patrongroup" => 0,
460 "1-4-from_home_library-homebranch" => 0,
461 "1-4-from_home_library-holdingbranch" => 0,
462 "1-4-from_any_library-any" => 3,
463 "1-4-from_any_library-holdgroup" => 2,
464 "1-4-from_any_library-patrongroup" => 1,
465 "1-4-from_any_library-homebranch" => 1,
466 "1-4-from_any_library-holdingbranch" => 1,
467 "1-4-from_local_hold_group-any" => 0,
468 "1-4-from_local_hold_group-holdgroup" => 0,
469 "1-4-from_local_hold_group-patrongroup" => 0,
470 "1-4-from_local_hold_group-homebranch" => 0,
471 "1-4-from_local_hold_group-holdingbranch" => 0,
472 "3-1-from_home_library-any" => 0,
473 "3-1-from_home_library-holdgroup" => 0,
474 "3-1-from_home_library-patrongroup" => 0,
475 "3-1-from_home_library-homebranch" => 0,
476 "3-1-from_home_library-holdingbranch" => 0,
477 "3-1-from_any_library-any" => 3,
478 "3-1-from_any_library-holdgroup" => 1,
479 "3-1-from_any_library-patrongroup" => 2,
480 "3-1-from_any_library-homebranch" => 0,
481 "3-1-from_any_library-holdingbranch" => 1,
482 "3-1-from_local_hold_group-any" => 0,
483 "3-1-from_local_hold_group-holdgroup" => 0,
484 "3-1-from_local_hold_group-patrongroup" => 0,
485 "3-1-from_local_hold_group-homebranch" => 0,
486 "3-1-from_local_hold_group-holdingbranch" => 0,
487 "3-4-from_home_library-any" => 0,
488 "3-4-from_home_library-holdgroup" => 0,
489 "3-4-from_home_library-patrongroup" => 0,
490 "3-4-from_home_library-homebranch" => 0,
491 "3-4-from_home_library-holdingbranch" => 0,
492 "3-4-from_any_library-any" => 3,
493 "3-4-from_any_library-holdgroup" => 1,
494 "3-4-from_any_library-patrongroup" => 1,
495 "3-4-from_any_library-homebranch" => 0,
496 "3-4-from_any_library-holdingbranch" => 1,
497 "3-4-from_local_hold_group-any" => 3,
498 "3-4-from_local_hold_group-holdgroup" => 1,
499 "3-4-from_local_hold_group-patrongroup" => 1,
500 "3-4-from_local_hold_group-homebranch" => 0,
501 "3-4-from_local_hold_group-holdingbranch" => 1
505 my ( $item, $patron, $ha, $hfp, $results ) = @_;
507 Koha::CirculationRules->set_rules(
513 hold_fulfillment_policy => $hfp,
514 returnbranch => 'any'
519 $ha eq 'from_local_hold_group' ? 'holdgroup'
521 $ha eq 'from_any_library' ? 'any'
526 my $pickup_location = $_;
527 grep { $pickup_location->branchcode eq $_ } @branchcodes
528 } $item->pickup_locations( { patron => $patron } )->as_list;
531 scalar(@pl) eq $results->{
532 $item->copynumber . '-'
533 . $patron->firstname . '-'
543 . ', hold_fulfillment_policy: '
547 $item->copynumber . '-'
548 . $patron->firstname . '-'
559 foreach my $item ($item1, $item3) {
560 foreach my $patron ($patron1, $patron4) {
561 #holdallowed 1: homebranch, 2: any, 3: holdgroup
562 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
563 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
564 _doTest($item, $patron, $ha, $hfp, $results);
570 # Now test that branchtransferlimits will further filter the pickup locations
572 my $item_no_ccode = $builder->build_sample_item(
574 homebranch => $library1->branchcode,
575 holdingbranch => $library2->branchcode,
576 itype => $item1->itype,
580 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
581 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
582 Koha::CirculationRules->set_rules(
585 itemtype => $item1->itype,
587 holdallowed => 'from_home_library',
588 hold_fulfillment_policy => 1,
589 returnbranch => 'any'
593 $builder->build_object(
595 class => 'Koha::Item::Transfer::Limits',
597 toBranch => $library1->branchcode,
598 fromBranch => $library2->branchcode,
599 itemtype => $item1->itype,
605 my @pickup_locations = map {
606 my $pickup_location = $_;
607 grep { $pickup_location->branchcode eq $_ } @branchcodes
608 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
610 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
612 $builder->build_object(
614 class => 'Koha::Item::Transfer::Limits',
616 toBranch => $library4->branchcode,
617 fromBranch => $library2->branchcode,
618 itemtype => $item1->itype,
624 @pickup_locations = map {
625 my $pickup_location = $_;
626 grep { $pickup_location->branchcode eq $_ } @branchcodes
627 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
629 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
631 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
632 @pickup_locations = map {
633 my $pickup_location = $_;
634 grep { $pickup_location->branchcode eq $_ } @branchcodes
635 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
636 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
638 @pickup_locations = map {
639 my $pickup_location = $_;
640 grep { $pickup_location->branchcode eq $_ } @branchcodes
641 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
642 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");
644 $builder->build_object(
646 class => 'Koha::Item::Transfer::Limits',
648 toBranch => $library2->branchcode,
649 fromBranch => $library2->branchcode,
651 ccode => $item1->ccode,
656 @pickup_locations = map {
657 my $pickup_location = $_;
658 grep { $pickup_location->branchcode eq $_ } @branchcodes
659 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
660 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
662 $builder->build_object(
664 class => 'Koha::Item::Transfer::Limits',
666 toBranch => $library4->branchcode,
667 fromBranch => $library2->branchcode,
669 ccode => $item1->ccode,
674 @pickup_locations = map {
675 my $pickup_location = $_;
676 grep { $pickup_location->branchcode eq $_ } @branchcodes
677 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
678 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
680 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
682 $schema->storage->txn_rollback;
685 subtest 'request_transfer' => sub {
687 $schema->storage->txn_begin;
689 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
690 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
691 my $item = $builder->build_sample_item(
693 homebranch => $library1->branchcode,
694 holdingbranch => $library2->branchcode,
698 # Mandatory fields tests
699 throws_ok { $item->request_transfer( { to => $library1 } ) }
700 'Koha::Exceptions::MissingParameter',
701 'Exception thrown if `reason` parameter is missing';
703 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
704 'Koha::Exceptions::MissingParameter',
705 'Exception thrown if `to` parameter is missing';
708 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
709 is( ref($transfer), 'Koha::Item::Transfer',
710 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
712 my $original_transfer = $transfer->get_from_storage;
714 # Transfer already in progress
715 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
716 'Koha::Exceptions::Item::Transfer::InQueue',
717 'Exception thrown if transfer is already in progress';
720 is( ref( $exception->transfer ),
721 'Koha::Item::Transfer',
722 'The exception contains the found Koha::Item::Transfer' );
725 my $queued_transfer = $item->request_transfer(
726 { to => $library2, reason => 'Manual', enqueue => 1 } );
727 is( ref($queued_transfer), 'Koha::Item::Transfer',
728 'Koha::Item->request_transfer allowed when enqueue is set' );
729 my $transfers = $item->get_transfers;
730 is($transfers->count, 2, "There are now 2 live transfers in the queue");
731 $transfer = $transfer->get_from_storage;
732 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
733 $queued_transfer->datearrived(dt_from_string)->store();
736 my $replaced_transfer = $item->request_transfer(
737 { to => $library2, reason => 'Manual', replace => 1 } );
738 is( ref($replaced_transfer), 'Koha::Item::Transfer',
739 'Koha::Item->request_transfer allowed when replace is set' );
740 $original_transfer->discard_changes;
741 ok($original_transfer->datecancelled, "Original transfer cancelled");
742 $transfers = $item->get_transfers;
743 is($transfers->count, 1, "There is only 1 live transfer in the queue");
744 $replaced_transfer->datearrived(dt_from_string)->store();
746 # BranchTransferLimits
747 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
748 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
749 my $limit = Koha::Item::Transfer::Limit->new({
750 fromBranch => $library2->branchcode,
751 toBranch => $library1->branchcode,
752 itemtype => $item->effective_itemtype,
755 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
756 'Koha::Exceptions::Item::Transfer::Limit',
757 'Exception thrown if transfer is prevented by limits';
759 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
760 is( ref($forced_transfer), 'Koha::Item::Transfer',
761 'Koha::Item->request_transfer allowed when ignore_limits is set'
764 $schema->storage->txn_rollback;
767 subtest 'deletion' => sub {
770 $schema->storage->txn_begin;
772 my $biblio = $builder->build_sample_biblio();
774 my $item = $builder->build_sample_item(
776 biblionumber => $biblio->biblionumber,
780 is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
781 ; # FIXME This should be Koha::Deleted::Item
782 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
783 $item = $builder->build_sample_item(
785 biblionumber => $biblio->biblionumber,
789 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
792 my $library = $builder->build_object({ class => 'Koha::Libraries' });
793 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
794 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
796 my $patron = $builder->build_object({class => 'Koha::Patrons'});
797 $item = $builder->build_sample_item({ library => $library->branchcode });
800 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
803 @{$item->safe_to_delete->messages}[0]->message,
805 'Koha::Item->safe_to_delete reports item on loan',
809 @{$item->safe_to_delete->messages}[0]->message,
811 'item that is on loan cannot be deleted',
815 ! $item->safe_to_delete,
816 'Koha::Item->safe_to_delete shows item NOT safe to delete'
819 AddReturn( $item->barcode, $library->branchcode );
821 # book_reserved is tested in t/db_dependent/Reserves.t
824 t::lib::Mocks::mock_preference('IndependentBranches', 1);
825 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
828 @{$item_2->safe_to_delete->messages}[0]->message,
830 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
834 @{$item_2->safe_to_delete->messages}[0]->message,
836 'IndependentBranches prevents deletion at another branch',
841 { # codeblock to limit scope of $module->mock
843 my $module = Test::MockModule->new('C4::Items');
844 $module->mock( GetAnalyticsCount => sub { return 1 } );
846 $item->discard_changes;
848 @{$item->safe_to_delete->messages}[0]->message,
850 'Koha::Item->safe_to_delete reports linked analytics',
854 @{$item->safe_to_delete->messages}[0]->message,
856 'Linked analytics prevents deletion of item',
861 { # last_item_for_hold
862 C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
864 @{$item->safe_to_delete->messages}[0]->message,
865 'last_item_for_hold',
866 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
868 # With another item attached to the biblio, the item can be deleted
869 $builder->build_sample_item({ biblionumber => $item->biblionumber });
873 $item->safe_to_delete,
874 'Koha::Item->safe_to_delete shows item safe to delete'
879 my $test_item = Koha::Items->find( $item->itemnumber );
881 is( $test_item, undef,
882 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
885 $schema->storage->txn_rollback;
888 subtest 'renewal_branchcode' => sub {
891 $schema->storage->txn_begin;
893 my $item = $builder->build_sample_item();
894 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
895 my $checkout = $builder->build_object({
896 class => 'Koha::Checkouts',
898 itemnumber => $item->itemnumber,
903 C4::Context->interface( 'intranet' );
904 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
906 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
907 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
908 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
909 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
911 C4::Context->interface( 'opac' );
913 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
914 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
915 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
917 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
918 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
919 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
921 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
922 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
923 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");
925 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
926 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
927 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");
929 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
930 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
931 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");
933 $schema->storage->txn_rollback;
936 subtest 'Tests for itemtype' => sub {
938 $schema->storage->txn_begin;
940 my $biblio = $builder->build_sample_biblio;
941 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
942 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
944 t::lib::Mocks::mock_preference('item-level_itypes', 1);
945 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
946 t::lib::Mocks::mock_preference('item-level_itypes', 0);
947 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
949 $schema->storage->txn_rollback;
952 subtest 'get_transfers' => sub {
954 $schema->storage->txn_begin;
956 my $item = $builder->build_sample_item();
958 my $transfers = $item->get_transfers();
959 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
960 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
962 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
964 my $transfer_1 = $builder->build_object(
966 class => 'Koha::Item::Transfers',
968 itemnumber => $item->itemnumber,
969 frombranch => $item->holdingbranch,
970 tobranch => $library_to->branchcode,
973 datearrived => undef,
974 datecancelled => undef,
975 daterequested => \'NOW()'
980 $transfers = $item->get_transfers();
981 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
983 my $transfer_2 = $builder->build_object(
985 class => 'Koha::Item::Transfers',
987 itemnumber => $item->itemnumber,
988 frombranch => $item->holdingbranch,
989 tobranch => $library_to->branchcode,
992 datearrived => undef,
993 datecancelled => undef,
994 daterequested => \'NOW()'
999 my $transfer_3 = $builder->build_object(
1001 class => 'Koha::Item::Transfers',
1003 itemnumber => $item->itemnumber,
1004 frombranch => $item->holdingbranch,
1005 tobranch => $library_to->branchcode,
1008 datearrived => undef,
1009 datecancelled => undef,
1010 daterequested => \'NOW()'
1015 $transfers = $item->get_transfers();
1016 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1017 my $result_1 = $transfers->next;
1018 my $result_2 = $transfers->next;
1019 my $result_3 = $transfers->next;
1020 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1021 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1022 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1024 $transfer_2->datesent(\'NOW()')->store;
1025 $transfers = $item->get_transfers();
1026 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1027 $result_1 = $transfers->next;
1028 $result_2 = $transfers->next;
1029 $result_3 = $transfers->next;
1030 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1031 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1032 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1034 $transfer_2->datearrived(\'NOW()')->store;
1035 $transfers = $item->get_transfers();
1036 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1037 $result_1 = $transfers->next;
1038 $result_2 = $transfers->next;
1039 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1040 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1042 $transfer_1->datecancelled(\'NOW()')->store;
1043 $transfers = $item->get_transfers();
1044 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1045 $result_1 = $transfers->next;
1046 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1048 $schema->storage->txn_rollback;
1051 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1054 $schema->storage->txn_begin;
1056 my $biblio = $builder->build_sample_biblio();
1057 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1059 my $orders = $item->orders;
1060 is ($orders->count, 0, 'No order on this item yet');
1062 my $order_note = 'Order for ' . $item->itemnumber;
1064 my $aq_order1 = $builder->build_object({
1065 class => 'Koha::Acquisition::Orders',
1067 biblionumber => $biblio->biblionumber,
1068 order_internalnote => $order_note,
1071 my $aq_order2 = $builder->build_object({
1072 class => 'Koha::Acquisition::Orders',
1074 biblionumber => $biblio->biblionumber,
1077 my $aq_order_item1 = $builder->build({
1078 source => 'AqordersItem',
1080 ordernumber => $aq_order1->ordernumber,
1081 itemnumber => $item->itemnumber,
1085 $orders = $item->orders;
1086 is ($orders->count, 1, 'One order found by item with the relationship');
1087 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1090 subtest 'move_to_biblio() tests' => sub {
1093 $schema->storage->txn_begin;
1095 my $dbh = C4::Context->dbh;
1097 my $source_biblio = $builder->build_sample_biblio();
1098 my $target_biblio = $builder->build_sample_biblio();
1100 my $source_biblionumber = $source_biblio->biblionumber;
1101 my $target_biblionumber = $target_biblio->biblionumber;
1103 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1104 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1105 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1107 my $itemnumber1 = $item1->itemnumber;
1108 my $itemnumber2 = $item2->itemnumber;
1110 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1112 my $patron = $builder->build_object({
1113 class => 'Koha::Patrons',
1114 value => { branchcode => $library->branchcode }
1116 my $borrowernumber = $patron->borrowernumber;
1118 my $aq_budget = $builder->build({
1119 source => 'Aqbudget',
1121 budget_notes => 'test',
1125 my $aq_order1 = $builder->build_object({
1126 class => 'Koha::Acquisition::Orders',
1128 biblionumber => $source_biblionumber,
1129 budget_id => $aq_budget->{budget_id},
1132 my $aq_order_item1 = $builder->build({
1133 source => 'AqordersItem',
1135 ordernumber => $aq_order1->ordernumber,
1136 itemnumber => $itemnumber1,
1139 my $aq_order2 = $builder->build_object({
1140 class => 'Koha::Acquisition::Orders',
1142 biblionumber => $source_biblionumber,
1143 budget_id => $aq_budget->{budget_id},
1146 my $aq_order_item2 = $builder->build({
1147 source => 'AqordersItem',
1149 ordernumber => $aq_order2->ordernumber,
1150 itemnumber => $itemnumber2,
1154 my $bib_level_hold = $builder->build_object({
1155 class => 'Koha::Holds',
1157 biblionumber => $source_biblionumber,
1158 itemnumber => undef,
1161 my $item_level_hold1 = $builder->build_object({
1162 class => 'Koha::Holds',
1164 biblionumber => $source_biblionumber,
1165 itemnumber => $itemnumber1,
1168 my $item_level_hold2 = $builder->build_object({
1169 class => 'Koha::Holds',
1171 biblionumber => $source_biblionumber,
1172 itemnumber => $itemnumber2,
1176 my $tmp_holdsqueue1 = $builder->build({
1177 source => 'TmpHoldsqueue',
1179 borrowernumber => $borrowernumber,
1180 biblionumber => $source_biblionumber,
1181 itemnumber => $itemnumber1,
1184 my $tmp_holdsqueue2 = $builder->build({
1185 source => 'TmpHoldsqueue',
1187 borrowernumber => $borrowernumber,
1188 biblionumber => $source_biblionumber,
1189 itemnumber => $itemnumber2,
1192 my $hold_fill_target1 = $builder->build({
1193 source => 'HoldFillTarget',
1195 borrowernumber => $borrowernumber,
1196 biblionumber => $source_biblionumber,
1197 itemnumber => $itemnumber1,
1200 my $hold_fill_target2 = $builder->build({
1201 source => 'HoldFillTarget',
1203 borrowernumber => $borrowernumber,
1204 biblionumber => $source_biblionumber,
1205 itemnumber => $itemnumber2,
1208 my $linktracker1 = $builder->build({
1209 source => 'Linktracker',
1211 borrowernumber => $borrowernumber,
1212 biblionumber => $source_biblionumber,
1213 itemnumber => $itemnumber1,
1216 my $linktracker2 = $builder->build({
1217 source => 'Linktracker',
1219 borrowernumber => $borrowernumber,
1220 biblionumber => $source_biblionumber,
1221 itemnumber => $itemnumber2,
1225 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1226 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1228 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1229 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');
1231 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1232 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1233 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1234 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1235 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1236 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1238 $aq_order1->discard_changes;
1239 $aq_order2->discard_changes;
1240 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1241 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1243 $bib_level_hold->discard_changes;
1244 $item_level_hold1->discard_changes;
1245 $item_level_hold2->discard_changes;
1246 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1247 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1248 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1250 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1251 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1252 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1253 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1255 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1256 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1257 # Why does ->biblionumber return a Biblio object???
1258 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1259 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1261 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1262 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1263 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1264 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1266 $schema->storage->txn_rollback;
1269 subtest 'columns_to_str' => sub {
1272 $schema->storage->txn_begin;
1274 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1276 my $cache = Koha::Caches->get_instance();
1277 $cache->clear_from_cache("MarcStructure-0-");
1278 $cache->clear_from_cache("MarcStructure-1-");
1279 $cache->clear_from_cache("default_value_for_mod_marc-");
1280 $cache->clear_from_cache("MarcSubfieldStructure-");
1282 # Creating subfields 'é', 'è' that are not linked with a kohafield
1283 Koha::MarcSubfieldStructures->search(
1285 frameworkcode => '',
1286 tagfield => $itemtag,
1287 tagsubfield => ['é', 'è'],
1289 )->delete; # In case it exist already
1291 # é is not linked with a AV
1292 # è is linked with AV branches
1293 Koha::MarcSubfieldStructure->new(
1295 frameworkcode => '',
1296 tagfield => $itemtag,
1297 tagsubfield => 'é',
1300 defaultvalue => 'ééé',
1304 Koha::MarcSubfieldStructure->new(
1306 frameworkcode => '',
1307 tagfield => $itemtag,
1308 tagsubfield => 'è',
1311 defaultvalue => 'èèè',
1313 authorised_value => 'branches',
1317 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1318 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1319 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1320 my $dateaccessioned = '2020-12-15';
1321 my $library = Koha::Libraries->search->next;
1322 my $branchcode = $library->branchcode;
1324 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1326 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1327 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1328 xmlns="http://www.loc.gov/MARC21/slim">
1331 <leader> a </leader>
1332 <datafield tag="999" ind1=" " ind2=" ">
1333 <subfield code="é">value é</subfield>
1334 <subfield code="è">$branchcode</subfield>
1342 itemlost => $lost_av->authorised_value,
1343 dateaccessioned => $dateaccessioned,
1344 more_subfields_xml => $some_marc_xml,
1348 $item = $item->get_from_storage;
1350 my $s = $item->columns_to_str;
1351 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1352 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1353 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1354 is( $s->{'è'}, $library->branchname );
1356 $cache->clear_from_cache("MarcStructure-0-");
1357 $cache->clear_from_cache("MarcStructure-1-");
1358 $cache->clear_from_cache("default_value_for_mod_marc-");
1359 $cache->clear_from_cache("MarcSubfieldStructure-");
1361 $schema->storage->txn_rollback;
1365 subtest 'store() tests' => sub {
1369 subtest 'dateaccessioned handling' => sub {
1373 $schema->storage->txn_begin;
1375 my $item = $builder->build_sample_item;
1377 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1379 # reset dateaccessioned on the DB
1380 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1381 $item->discard_changes;
1383 ok( !defined $item->dateaccessioned );
1386 $item->replacementprice(100)->store->discard_changes;
1388 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1390 $schema->storage->txn_rollback;
1393 subtest '_set_found_trigger() tests' => sub {
1397 $schema->storage->txn_begin;
1399 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1400 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1402 # Add a lost item debit
1403 my $debit = $patron->account->add_debit(
1407 item_id => $item->id,
1408 interface => 'intranet',
1412 my $lostreturn_policy = 'charge';
1414 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1415 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1417 # simulate it was found
1418 $item->set( { itemlost => 0 } )->store;
1420 my $messages = $item->object_messages;
1422 my $message_1 = $messages->[0];
1424 is( $message_1->type, 'info', 'type is correct' );
1425 is( $message_1->message, 'lost_refunded', 'message is correct' );
1427 # Find the refund credit
1428 my $credit = $debit->credits->next;
1431 $message_1->payload,
1432 { credit_id => $credit->id },
1436 my $message_2 = $messages->[1];
1438 is( $message_2->type, 'info', 'type is correct' );
1439 is( $message_2->message, 'lost_charge', 'message is correct' );
1440 is( $message_2->payload, undef, 'no payload' );
1442 $schema->storage->txn_rollback;
1445 subtest 'holds_queue update tests' => sub {
1449 $schema->storage->txn_begin;
1451 my $biblio = $builder->build_sample_biblio;
1453 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1454 $mock->mock( 'enqueue', sub {
1455 my ( $self, $args ) = @_;
1457 $args->{biblio_ids},
1459 '->store triggers a holds queue update for the related biblio'
1463 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1466 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1469 $item->set({ reserves => 1 })->store;
1471 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1473 $item->set({ reserves => 0 })->store;
1475 $schema->storage->txn_rollback;
1479 subtest 'Recalls tests' => sub {
1483 $schema->storage->txn_begin;
1485 my $item1 = $builder->build_sample_item;
1486 my $biblio = $item1->biblio;
1487 my $branchcode = $item1->holdingbranch;
1488 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1489 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1490 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1491 my $item2 = $builder->build_object(
1492 { class => 'Koha::Items',
1493 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1497 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1498 t::lib::Mocks::mock_preference('UseRecalls', 1);
1500 my $recall1 = Koha::Recall->new(
1501 { patron_id => $patron1->borrowernumber,
1502 created_date => \'NOW()',
1503 biblio_id => $biblio->biblionumber,
1504 pickup_library_id => $branchcode,
1505 item_id => $item1->itemnumber,
1506 expiration_date => undef,
1510 my $recall2 = Koha::Recall->new(
1511 { patron_id => $patron2->borrowernumber,
1512 created_date => \'NOW()',
1513 biblio_id => $biblio->biblionumber,
1514 pickup_library_id => $branchcode,
1515 item_id => $item1->itemnumber,
1516 expiration_date => undef,
1521 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1523 $recall2->set_cancelled;
1525 t::lib::Mocks::mock_preference('UseRecalls', 0);
1526 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1528 t::lib::Mocks::mock_preference("UseRecalls", 1);
1530 $item1->update({ notforloan => 1 });
1531 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1532 $item1->update({ notforloan => 0, itemlost => 1 });
1533 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1534 $item1->update({ itemlost => 0, withdrawn => 1 });
1535 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1536 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1538 $item1->update({ withdrawn => 0 });
1539 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1541 Koha::CirculationRules->set_rules({
1542 branchcode => $branchcode,
1543 categorycode => $patron1->categorycode,
1544 itemtype => $item1->effective_itemtype,
1546 recalls_allowed => 0,
1547 recalls_per_record => 1,
1548 on_shelf_recalls => 'all',
1551 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1553 Koha::CirculationRules->set_rules({
1554 branchcode => $branchcode,
1555 categorycode => $patron1->categorycode,
1556 itemtype => $item1->effective_itemtype,
1558 recalls_allowed => 1,
1559 recalls_per_record => 1,
1560 on_shelf_recalls => 'all',
1563 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1564 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1565 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1567 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1568 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1569 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1571 $recall1->set_cancelled;
1572 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1574 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1576 Koha::CirculationRules->set_rules({
1577 branchcode => $branchcode,
1578 categorycode => $patron1->categorycode,
1579 itemtype => $item1->effective_itemtype,
1581 recalls_allowed => 1,
1582 recalls_per_record => 1,
1583 on_shelf_recalls => 'any',
1586 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1587 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1589 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1590 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1592 $recall1 = Koha::Recall->new(
1593 { patron_id => $patron1->borrowernumber,
1594 created_date => \'NOW()',
1595 biblio_id => $biblio->biblionumber,
1596 pickup_library_id => $branchcode,
1598 expiration_date => undef,
1603 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1605 Koha::CirculationRules->set_rules({
1606 branchcode => undef,
1607 categorycode => undef,
1608 itemtype => $item1->effective_itemtype,
1610 recalls_allowed => 0,
1611 recalls_per_record => 1,
1612 on_shelf_recalls => 'any',
1615 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1617 Koha::CirculationRules->set_rules({
1618 branchcode => undef,
1619 categorycode => undef,
1620 itemtype => $item1->effective_itemtype,
1622 recalls_allowed => 1,
1623 recalls_per_record => 1,
1624 on_shelf_recalls => 'any',
1627 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1629 # check_recalls tests
1631 $recall1 = Koha::Recall->new(
1632 { patron_id => $patron2->borrowernumber,
1633 created_date => \'NOW()',
1634 biblio_id => $biblio->biblionumber,
1635 pickup_library_id => $branchcode,
1636 item_id => $item1->itemnumber,
1637 expiration_date => undef,
1641 $recall2 = Koha::Recall->new(
1642 { patron_id => $patron1->borrowernumber,
1643 created_date => \'NOW()',
1644 biblio_id => $biblio->biblionumber,
1645 pickup_library_id => $branchcode,
1647 expiration_date => undef,
1651 $recall2->set_waiting( { item => $item1 } );
1653 # return a waiting recall
1654 my $check_recall = $item1->check_recalls;
1655 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
1657 $recall2->revert_waiting;
1659 # return recall based on recalldate
1660 $check_recall = $item1->check_recalls;
1661 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
1663 $recall1->set_cancelled;
1665 # return a biblio-level recall
1666 $check_recall = $item1->check_recalls;
1667 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
1669 $recall2->set_cancelled;
1671 $schema->storage->txn_rollback;
1674 subtest 'Notforloan tests' => sub {
1678 $schema->storage->txn_begin;
1680 my $item1 = $builder->build_sample_item;
1681 $item1->update({ notforloan => 0 });
1682 $item1->itemtype->notforloan(0);
1683 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
1684 $item1->update({ notforloan => 1 });
1685 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
1686 $item1->update({ notforloan => 0 });
1687 $item1->itemtype->update({ notforloan => 1 });
1688 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
1690 $schema->storage->txn_rollback;
1693 subtest 'item_group() tests' => sub {
1697 $schema->storage->txn_begin;
1699 my $biblio = $builder->build_sample_biblio();
1700 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1701 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1703 is( $item_1->item_group, undef, 'Item 1 has no item group');
1704 is( $item_2->item_group, undef, 'Item 2 has no item group');
1706 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1707 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1709 $item_group_1->add_item({ item_id => $item_1->id });
1710 $item_group_2->add_item({ item_id => $item_2->id });
1712 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
1713 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
1715 $schema->storage->txn_rollback;