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;
42 my $schema = Koha::Database->new->schema;
43 my $builder = t::lib::TestBuilder->new;
45 subtest 'return_claims relationship' => sub {
48 $schema->storage->txn_begin;
50 my $biblio = $builder->build_sample_biblio();
51 my $item = $builder->build_sample_item({
52 biblionumber => $biblio->biblionumber,
54 my $return_claims = $item->return_claims;
55 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
56 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
57 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
58 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
60 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
62 $schema->storage->txn_rollback;
65 subtest 'return_claim accessor' => sub {
68 $schema->storage->txn_begin;
70 my $biblio = $builder->build_sample_biblio();
71 my $item = $builder->build_sample_item({
72 biblionumber => $biblio->biblionumber,
74 my $return_claim = $item->return_claim;
75 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
77 my $claim1 = $builder->build_object(
79 class => 'Koha::Checkouts::ReturnClaims',
80 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
83 my $claim2 = $builder->build_object(
85 class => 'Koha::Checkouts::ReturnClaims',
86 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
90 $return_claim = $item->return_claim;
91 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
92 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
94 $claim2->resolution('test')->store();
95 $return_claim = $item->return_claim;
96 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
98 $claim1->resolution('test')->store();
99 $return_claim = $item->return_claim;
100 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
102 $schema->storage->txn_rollback;
105 subtest 'tracked_links relationship' => sub {
108 my $biblio = $builder->build_sample_biblio();
109 my $item = $builder->build_sample_item({
110 biblionumber => $biblio->biblionumber,
112 my $tracked_links = $item->tracked_links;
113 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
114 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
115 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
116 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
118 is($item->tracked_links()->count,2,"Two tracked links found");
121 subtest 'is_bundle tests' => sub {
124 $schema->storage->txn_begin;
126 my $item = $builder->build_sample_item();
128 my $is_bundle = $item->is_bundle;
129 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
131 my $item2 = $builder->build_sample_item();
132 $schema->resultset('ItemBundle')
133 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
135 $is_bundle = $item->is_bundle;
136 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
138 $schema->storage->txn_rollback;
141 subtest 'in_bundle tests' => sub {
144 $schema->storage->txn_begin;
146 my $item = $builder->build_sample_item();
148 my $in_bundle = $item->in_bundle;
149 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
151 my $host_item = $builder->build_sample_item();
152 $schema->resultset('ItemBundle')
153 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
155 $in_bundle = $item->in_bundle;
156 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
158 $schema->storage->txn_rollback;
161 subtest 'bundle_items tests' => sub {
164 $schema->storage->txn_begin;
166 my $host_item = $builder->build_sample_item();
167 my $bundle_items = $host_item->bundle_items;
168 is( ref($bundle_items), 'Koha::Items',
169 'bundle_items returns a Koha::Items object set' );
170 is( $bundle_items->count, 0,
171 'bundle_items set is empty when no items are bundled' );
173 my $bundle_item1 = $builder->build_sample_item();
174 my $bundle_item2 = $builder->build_sample_item();
175 my $bundle_item3 = $builder->build_sample_item();
176 $schema->resultset('ItemBundle')
178 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
179 $schema->resultset('ItemBundle')
181 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
182 $schema->resultset('ItemBundle')
184 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
186 $bundle_items = $host_item->bundle_items;
187 is( $bundle_items->count, 3,
188 'bundle_items returns all the bundled items in the set' );
190 $schema->storage->txn_rollback;
193 subtest 'bundle_host tests' => sub {
196 $schema->storage->txn_begin;
198 my $host_item = $builder->build_sample_item();
199 my $bundle_item1 = $builder->build_sample_item();
200 my $bundle_item2 = $builder->build_sample_item();
201 $schema->resultset('ItemBundle')
203 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
205 my $bundle_host = $bundle_item1->bundle_host;
206 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
207 $bundle_host = $bundle_item2->bundle_host;
208 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
209 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
211 $schema->storage->txn_rollback;
214 subtest 'add_to_bundle tests' => sub {
217 $schema->storage->txn_begin;
219 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
221 my $host_item = $builder->build_sample_item();
222 my $bundle_item1 = $builder->build_sample_item();
223 my $bundle_item2 = $builder->build_sample_item();
225 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
226 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
228 throws_ok { $host_item->add_to_bundle($bundle_item1) }
229 'Koha::Exceptions::Object::DuplicateID',
230 'Exception thrown if you try to add the same item twice';
232 $schema->storage->txn_rollback;
235 subtest 'remove_from_bundle tests' => sub {
238 $schema->storage->txn_begin;
240 my $host_item = $builder->build_sample_item();
241 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
242 $schema->resultset('ItemBundle')
244 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
246 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
247 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
248 $bundle_item1 = $bundle_item1->get_from_storage;
249 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
251 $schema->storage->txn_rollback;
254 subtest 'hidden_in_opac() tests' => sub {
258 $schema->storage->txn_begin;
260 my $item = $builder->build_sample_item({ itemlost => 2 });
263 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
264 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
266 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
267 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
269 # enable hidelostitems to verify correct behaviour
270 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
271 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
273 # disable hidelostitems
274 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
275 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
277 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
279 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
283 $schema->storage->txn_rollback;
286 subtest 'has_pending_hold() tests' => sub {
290 $schema->storage->txn_begin;
292 my $dbh = C4::Context->dbh;
293 my $item = $builder->build_sample_item({ itemlost => 0 });
294 my $itemnumber = $item->itemnumber;
296 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
297 ok( $item->has_pending_hold, "Yes, we have a pending hold");
298 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
299 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
301 $schema->storage->txn_rollback;
304 subtest "as_marc_field() tests" => sub {
306 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
307 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
309 my @schema_columns = $schema->resultset('Item')->result_source->columns;
310 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
312 plan tests => 2 * (scalar @mapped_columns + 1) + 3;
314 $schema->storage->txn_begin;
316 my $item = $builder->build_sample_item;
317 # Make sure it has at least one undefined attribute
318 $item->set({ replacementprice => undef })->store->discard_changes;
320 # Tests with the mss parameter
321 my $marc_field = $item->as_marc_field({ mss => $mss });
326 'Generated field set the right tag number'
329 foreach my $column ( @mapped_columns ) {
330 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
331 is( $marc_field->subfield($tagsubfield),
332 $item->$column, "Value is mapped correctly for column $column" );
335 # Tests without the mss parameter
336 $marc_field = $item->as_marc_field();
341 'Generated field set the right tag number'
344 foreach my $column (@mapped_columns) {
345 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
346 is( $marc_field->subfield($tagsubfield),
347 $item->$column, "Value is mapped correctly for column $column" );
350 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
353 tagfield => $itemtag,
358 my @unlinked_subfields;
359 push @unlinked_subfields, X => 'Something weird';
360 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
362 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
363 Koha::MarcSubfieldStructures->search(
364 { frameworkcode => '', tagfield => $itemtag } )
365 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
367 $marc_field = $item->as_marc_field;
369 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
370 my @subfields = $marc_field->subfields;
371 my $result = all { defined $_->[1] } @subfields;
372 ok( $result, 'There are no undef subfields' );
373 my @ordered_subfields = sort {
374 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
375 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
377 is_deeply(\@subfields, \@ordered_subfields);
379 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
381 $schema->storage->txn_rollback;
382 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
385 subtest 'pickup_locations' => sub {
388 $schema->storage->txn_begin;
390 my $dbh = C4::Context->dbh;
392 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
393 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
394 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
395 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
396 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
397 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
398 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
399 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
401 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
402 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
405 $library1->branchcode, $library2->branchcode,
406 $library3->branchcode, $library4->branchcode
409 my $item1 = $builder->build_sample_item(
411 homebranch => $library1->branchcode,
412 holdingbranch => $library2->branchcode,
418 my $item3 = $builder->build_sample_item(
420 homebranch => $library3->branchcode,
421 holdingbranch => $library4->branchcode,
423 itype => $item1->itype,
427 Koha::CirculationRules->set_rules(
429 categorycode => undef,
430 itemtype => $item1->itype,
433 reservesallowed => 25,
439 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
440 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
443 "1-1-from_home_library-any" => 3,
444 "1-1-from_home_library-holdgroup" => 2,
445 "1-1-from_home_library-patrongroup" => 2,
446 "1-1-from_home_library-homebranch" => 1,
447 "1-1-from_home_library-holdingbranch" => 1,
448 "1-1-from_any_library-any" => 3,
449 "1-1-from_any_library-holdgroup" => 2,
450 "1-1-from_any_library-patrongroup" => 2,
451 "1-1-from_any_library-homebranch" => 1,
452 "1-1-from_any_library-holdingbranch" => 1,
453 "1-1-from_local_hold_group-any" => 3,
454 "1-1-from_local_hold_group-holdgroup" => 2,
455 "1-1-from_local_hold_group-patrongroup" => 2,
456 "1-1-from_local_hold_group-homebranch" => 1,
457 "1-1-from_local_hold_group-holdingbranch" => 1,
458 "1-4-from_home_library-any" => 0,
459 "1-4-from_home_library-holdgroup" => 0,
460 "1-4-from_home_library-patrongroup" => 0,
461 "1-4-from_home_library-homebranch" => 0,
462 "1-4-from_home_library-holdingbranch" => 0,
463 "1-4-from_any_library-any" => 3,
464 "1-4-from_any_library-holdgroup" => 2,
465 "1-4-from_any_library-patrongroup" => 1,
466 "1-4-from_any_library-homebranch" => 1,
467 "1-4-from_any_library-holdingbranch" => 1,
468 "1-4-from_local_hold_group-any" => 0,
469 "1-4-from_local_hold_group-holdgroup" => 0,
470 "1-4-from_local_hold_group-patrongroup" => 0,
471 "1-4-from_local_hold_group-homebranch" => 0,
472 "1-4-from_local_hold_group-holdingbranch" => 0,
473 "3-1-from_home_library-any" => 0,
474 "3-1-from_home_library-holdgroup" => 0,
475 "3-1-from_home_library-patrongroup" => 0,
476 "3-1-from_home_library-homebranch" => 0,
477 "3-1-from_home_library-holdingbranch" => 0,
478 "3-1-from_any_library-any" => 3,
479 "3-1-from_any_library-holdgroup" => 1,
480 "3-1-from_any_library-patrongroup" => 2,
481 "3-1-from_any_library-homebranch" => 0,
482 "3-1-from_any_library-holdingbranch" => 1,
483 "3-1-from_local_hold_group-any" => 0,
484 "3-1-from_local_hold_group-holdgroup" => 0,
485 "3-1-from_local_hold_group-patrongroup" => 0,
486 "3-1-from_local_hold_group-homebranch" => 0,
487 "3-1-from_local_hold_group-holdingbranch" => 0,
488 "3-4-from_home_library-any" => 0,
489 "3-4-from_home_library-holdgroup" => 0,
490 "3-4-from_home_library-patrongroup" => 0,
491 "3-4-from_home_library-homebranch" => 0,
492 "3-4-from_home_library-holdingbranch" => 0,
493 "3-4-from_any_library-any" => 3,
494 "3-4-from_any_library-holdgroup" => 1,
495 "3-4-from_any_library-patrongroup" => 1,
496 "3-4-from_any_library-homebranch" => 0,
497 "3-4-from_any_library-holdingbranch" => 1,
498 "3-4-from_local_hold_group-any" => 3,
499 "3-4-from_local_hold_group-holdgroup" => 1,
500 "3-4-from_local_hold_group-patrongroup" => 1,
501 "3-4-from_local_hold_group-homebranch" => 0,
502 "3-4-from_local_hold_group-holdingbranch" => 1
506 my ( $item, $patron, $ha, $hfp, $results ) = @_;
508 Koha::CirculationRules->set_rules(
514 hold_fulfillment_policy => $hfp,
515 returnbranch => 'any'
520 $ha eq 'from_local_hold_group' ? 'holdgroup'
522 $ha eq 'from_any_library' ? 'any'
527 my $pickup_location = $_;
528 grep { $pickup_location->branchcode eq $_ } @branchcodes
529 } $item->pickup_locations( { patron => $patron } )->as_list;
532 scalar(@pl) eq $results->{
533 $item->copynumber . '-'
534 . $patron->firstname . '-'
544 . ', hold_fulfillment_policy: '
548 $item->copynumber . '-'
549 . $patron->firstname . '-'
560 foreach my $item ($item1, $item3) {
561 foreach my $patron ($patron1, $patron4) {
562 #holdallowed 1: homebranch, 2: any, 3: holdgroup
563 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
564 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
565 _doTest($item, $patron, $ha, $hfp, $results);
571 # Now test that branchtransferlimits will further filter the pickup locations
573 my $item_no_ccode = $builder->build_sample_item(
575 homebranch => $library1->branchcode,
576 holdingbranch => $library2->branchcode,
577 itype => $item1->itype,
581 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
582 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
583 Koha::CirculationRules->set_rules(
586 itemtype => $item1->itype,
588 holdallowed => 'from_home_library',
589 hold_fulfillment_policy => 1,
590 returnbranch => 'any'
594 $builder->build_object(
596 class => 'Koha::Item::Transfer::Limits',
598 toBranch => $library1->branchcode,
599 fromBranch => $library2->branchcode,
600 itemtype => $item1->itype,
606 my @pickup_locations = map {
607 my $pickup_location = $_;
608 grep { $pickup_location->branchcode eq $_ } @branchcodes
609 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
611 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
613 $builder->build_object(
615 class => 'Koha::Item::Transfer::Limits',
617 toBranch => $library4->branchcode,
618 fromBranch => $library2->branchcode,
619 itemtype => $item1->itype,
625 @pickup_locations = map {
626 my $pickup_location = $_;
627 grep { $pickup_location->branchcode eq $_ } @branchcodes
628 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
630 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
632 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
633 @pickup_locations = map {
634 my $pickup_location = $_;
635 grep { $pickup_location->branchcode eq $_ } @branchcodes
636 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
637 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
639 @pickup_locations = map {
640 my $pickup_location = $_;
641 grep { $pickup_location->branchcode eq $_ } @branchcodes
642 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
643 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");
645 $builder->build_object(
647 class => 'Koha::Item::Transfer::Limits',
649 toBranch => $library2->branchcode,
650 fromBranch => $library2->branchcode,
652 ccode => $item1->ccode,
657 @pickup_locations = map {
658 my $pickup_location = $_;
659 grep { $pickup_location->branchcode eq $_ } @branchcodes
660 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
661 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
663 $builder->build_object(
665 class => 'Koha::Item::Transfer::Limits',
667 toBranch => $library4->branchcode,
668 fromBranch => $library2->branchcode,
670 ccode => $item1->ccode,
675 @pickup_locations = map {
676 my $pickup_location = $_;
677 grep { $pickup_location->branchcode eq $_ } @branchcodes
678 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
679 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
681 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
683 $schema->storage->txn_rollback;
686 subtest 'request_transfer' => sub {
688 $schema->storage->txn_begin;
690 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
691 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
692 my $item = $builder->build_sample_item(
694 homebranch => $library1->branchcode,
695 holdingbranch => $library2->branchcode,
699 # Mandatory fields tests
700 throws_ok { $item->request_transfer( { to => $library1 } ) }
701 'Koha::Exceptions::MissingParameter',
702 'Exception thrown if `reason` parameter is missing';
704 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
705 'Koha::Exceptions::MissingParameter',
706 'Exception thrown if `to` parameter is missing';
709 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
710 is( ref($transfer), 'Koha::Item::Transfer',
711 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
713 my $original_transfer = $transfer->get_from_storage;
715 # Transfer already in progress
716 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
717 'Koha::Exceptions::Item::Transfer::InQueue',
718 'Exception thrown if transfer is already in progress';
721 is( ref( $exception->transfer ),
722 'Koha::Item::Transfer',
723 'The exception contains the found Koha::Item::Transfer' );
726 my $queued_transfer = $item->request_transfer(
727 { to => $library2, reason => 'Manual', enqueue => 1 } );
728 is( ref($queued_transfer), 'Koha::Item::Transfer',
729 'Koha::Item->request_transfer allowed when enqueue is set' );
730 my $transfers = $item->get_transfers;
731 is($transfers->count, 2, "There are now 2 live transfers in the queue");
732 $transfer = $transfer->get_from_storage;
733 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
734 $queued_transfer->datearrived(dt_from_string)->store();
737 my $replaced_transfer = $item->request_transfer(
738 { to => $library2, reason => 'Manual', replace => 1 } );
739 is( ref($replaced_transfer), 'Koha::Item::Transfer',
740 'Koha::Item->request_transfer allowed when replace is set' );
741 $original_transfer->discard_changes;
742 ok($original_transfer->datecancelled, "Original transfer cancelled");
743 $transfers = $item->get_transfers;
744 is($transfers->count, 1, "There is only 1 live transfer in the queue");
745 $replaced_transfer->datearrived(dt_from_string)->store();
747 # BranchTransferLimits
748 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
749 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
750 my $limit = Koha::Item::Transfer::Limit->new({
751 fromBranch => $library2->branchcode,
752 toBranch => $library1->branchcode,
753 itemtype => $item->effective_itemtype,
756 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
757 'Koha::Exceptions::Item::Transfer::Limit',
758 'Exception thrown if transfer is prevented by limits';
760 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
761 is( ref($forced_transfer), 'Koha::Item::Transfer',
762 'Koha::Item->request_transfer allowed when ignore_limits is set'
765 $schema->storage->txn_rollback;
768 subtest 'deletion' => sub {
771 $schema->storage->txn_begin;
773 my $biblio = $builder->build_sample_biblio();
775 my $item = $builder->build_sample_item(
777 biblionumber => $biblio->biblionumber,
780 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
782 my $deleted_item = $item->move_to_deleted;
783 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
784 ; # FIXME This should be Koha::Deleted::Item
785 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
787 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
788 $item = $builder->build_sample_item(
790 biblionumber => $biblio->biblionumber,
794 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
797 my $library = $builder->build_object({ class => 'Koha::Libraries' });
798 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
799 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
801 my $patron = $builder->build_object({class => 'Koha::Patrons'});
802 $item = $builder->build_sample_item({ library => $library->branchcode });
805 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
808 @{$item->safe_to_delete->messages}[0]->message,
810 'Koha::Item->safe_to_delete reports item on loan',
814 @{$item->safe_to_delete->messages}[0]->message,
816 'item that is on loan cannot be deleted',
820 ! $item->safe_to_delete,
821 'Koha::Item->safe_to_delete shows item NOT safe to delete'
824 AddReturn( $item->barcode, $library->branchcode );
826 # book_reserved is tested in t/db_dependent/Reserves.t
829 t::lib::Mocks::mock_preference('IndependentBranches', 1);
830 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
833 @{$item_2->safe_to_delete->messages}[0]->message,
835 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
839 @{$item_2->safe_to_delete->messages}[0]->message,
841 'IndependentBranches prevents deletion at another branch',
846 { # codeblock to limit scope of $module->mock
848 my $module = Test::MockModule->new('C4::Items');
849 $module->mock( GetAnalyticsCount => sub { return 1 } );
851 $item->discard_changes;
853 @{$item->safe_to_delete->messages}[0]->message,
855 'Koha::Item->safe_to_delete reports linked analytics',
859 @{$item->safe_to_delete->messages}[0]->message,
861 'Linked analytics prevents deletion of item',
866 { # last_item_for_hold
867 C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
869 @{$item->safe_to_delete->messages}[0]->message,
870 'last_item_for_hold',
871 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
873 # With another item attached to the biblio, the item can be deleted
874 $builder->build_sample_item({ biblionumber => $item->biblionumber });
878 $item->safe_to_delete,
879 'Koha::Item->safe_to_delete shows item safe to delete'
884 my $test_item = Koha::Items->find( $item->itemnumber );
886 is( $test_item, undef,
887 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
890 $schema->storage->txn_rollback;
893 subtest 'renewal_branchcode' => sub {
896 $schema->storage->txn_begin;
898 my $item = $builder->build_sample_item();
899 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
900 my $checkout = $builder->build_object({
901 class => 'Koha::Checkouts',
903 itemnumber => $item->itemnumber,
908 C4::Context->interface( 'intranet' );
909 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
911 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
912 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
913 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
914 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
916 C4::Context->interface( 'opac' );
918 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
919 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
920 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
922 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
923 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
924 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
926 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
927 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
928 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");
930 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
931 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
932 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");
934 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
935 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
936 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");
938 $schema->storage->txn_rollback;
941 subtest 'Tests for itemtype' => sub {
943 $schema->storage->txn_begin;
945 my $biblio = $builder->build_sample_biblio;
946 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
947 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
949 t::lib::Mocks::mock_preference('item-level_itypes', 1);
950 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
951 t::lib::Mocks::mock_preference('item-level_itypes', 0);
952 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
954 $schema->storage->txn_rollback;
957 subtest 'get_transfers' => sub {
959 $schema->storage->txn_begin;
961 my $item = $builder->build_sample_item();
963 my $transfers = $item->get_transfers();
964 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
965 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
967 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
969 my $transfer_1 = $builder->build_object(
971 class => 'Koha::Item::Transfers',
973 itemnumber => $item->itemnumber,
974 frombranch => $item->holdingbranch,
975 tobranch => $library_to->branchcode,
978 datearrived => undef,
979 datecancelled => undef,
980 daterequested => \'NOW()'
985 $transfers = $item->get_transfers();
986 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
988 my $transfer_2 = $builder->build_object(
990 class => 'Koha::Item::Transfers',
992 itemnumber => $item->itemnumber,
993 frombranch => $item->holdingbranch,
994 tobranch => $library_to->branchcode,
997 datearrived => undef,
998 datecancelled => undef,
999 daterequested => \'NOW()'
1004 my $transfer_3 = $builder->build_object(
1006 class => 'Koha::Item::Transfers',
1008 itemnumber => $item->itemnumber,
1009 frombranch => $item->holdingbranch,
1010 tobranch => $library_to->branchcode,
1013 datearrived => undef,
1014 datecancelled => undef,
1015 daterequested => \'NOW()'
1020 $transfers = $item->get_transfers();
1021 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1022 my $result_1 = $transfers->next;
1023 my $result_2 = $transfers->next;
1024 my $result_3 = $transfers->next;
1025 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1026 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1027 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1029 $transfer_2->datesent(\'NOW()')->store;
1030 $transfers = $item->get_transfers();
1031 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1032 $result_1 = $transfers->next;
1033 $result_2 = $transfers->next;
1034 $result_3 = $transfers->next;
1035 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1036 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1037 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1039 $transfer_2->datearrived(\'NOW()')->store;
1040 $transfers = $item->get_transfers();
1041 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1042 $result_1 = $transfers->next;
1043 $result_2 = $transfers->next;
1044 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1045 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1047 $transfer_1->datecancelled(\'NOW()')->store;
1048 $transfers = $item->get_transfers();
1049 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1050 $result_1 = $transfers->next;
1051 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1053 $schema->storage->txn_rollback;
1056 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1059 $schema->storage->txn_begin;
1061 my $biblio = $builder->build_sample_biblio();
1062 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1064 my $orders = $item->orders;
1065 is ($orders->count, 0, 'No order on this item yet');
1067 my $order_note = 'Order for ' . $item->itemnumber;
1069 my $aq_order1 = $builder->build_object({
1070 class => 'Koha::Acquisition::Orders',
1072 biblionumber => $biblio->biblionumber,
1073 order_internalnote => $order_note,
1076 my $aq_order2 = $builder->build_object({
1077 class => 'Koha::Acquisition::Orders',
1079 biblionumber => $biblio->biblionumber,
1082 my $aq_order_item1 = $builder->build({
1083 source => 'AqordersItem',
1085 ordernumber => $aq_order1->ordernumber,
1086 itemnumber => $item->itemnumber,
1090 $orders = $item->orders;
1091 is ($orders->count, 1, 'One order found by item with the relationship');
1092 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1095 subtest 'move_to_biblio() tests' => sub {
1098 $schema->storage->txn_begin;
1100 my $dbh = C4::Context->dbh;
1102 my $source_biblio = $builder->build_sample_biblio();
1103 my $target_biblio = $builder->build_sample_biblio();
1105 my $source_biblionumber = $source_biblio->biblionumber;
1106 my $target_biblionumber = $target_biblio->biblionumber;
1108 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1109 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1110 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1112 my $itemnumber1 = $item1->itemnumber;
1113 my $itemnumber2 = $item2->itemnumber;
1115 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1117 my $patron = $builder->build_object({
1118 class => 'Koha::Patrons',
1119 value => { branchcode => $library->branchcode }
1121 my $borrowernumber = $patron->borrowernumber;
1123 my $aq_budget = $builder->build({
1124 source => 'Aqbudget',
1126 budget_notes => 'test',
1130 my $aq_order1 = $builder->build_object({
1131 class => 'Koha::Acquisition::Orders',
1133 biblionumber => $source_biblionumber,
1134 budget_id => $aq_budget->{budget_id},
1137 my $aq_order_item1 = $builder->build({
1138 source => 'AqordersItem',
1140 ordernumber => $aq_order1->ordernumber,
1141 itemnumber => $itemnumber1,
1144 my $aq_order2 = $builder->build_object({
1145 class => 'Koha::Acquisition::Orders',
1147 biblionumber => $source_biblionumber,
1148 budget_id => $aq_budget->{budget_id},
1151 my $aq_order_item2 = $builder->build({
1152 source => 'AqordersItem',
1154 ordernumber => $aq_order2->ordernumber,
1155 itemnumber => $itemnumber2,
1159 my $bib_level_hold = $builder->build_object({
1160 class => 'Koha::Holds',
1162 biblionumber => $source_biblionumber,
1163 itemnumber => undef,
1166 my $item_level_hold1 = $builder->build_object({
1167 class => 'Koha::Holds',
1169 biblionumber => $source_biblionumber,
1170 itemnumber => $itemnumber1,
1173 my $item_level_hold2 = $builder->build_object({
1174 class => 'Koha::Holds',
1176 biblionumber => $source_biblionumber,
1177 itemnumber => $itemnumber2,
1181 my $tmp_holdsqueue1 = $builder->build({
1182 source => 'TmpHoldsqueue',
1184 borrowernumber => $borrowernumber,
1185 biblionumber => $source_biblionumber,
1186 itemnumber => $itemnumber1,
1189 my $tmp_holdsqueue2 = $builder->build({
1190 source => 'TmpHoldsqueue',
1192 borrowernumber => $borrowernumber,
1193 biblionumber => $source_biblionumber,
1194 itemnumber => $itemnumber2,
1197 my $hold_fill_target1 = $builder->build({
1198 source => 'HoldFillTarget',
1200 borrowernumber => $borrowernumber,
1201 biblionumber => $source_biblionumber,
1202 itemnumber => $itemnumber1,
1205 my $hold_fill_target2 = $builder->build({
1206 source => 'HoldFillTarget',
1208 borrowernumber => $borrowernumber,
1209 biblionumber => $source_biblionumber,
1210 itemnumber => $itemnumber2,
1213 my $linktracker1 = $builder->build({
1214 source => 'Linktracker',
1216 borrowernumber => $borrowernumber,
1217 biblionumber => $source_biblionumber,
1218 itemnumber => $itemnumber1,
1221 my $linktracker2 = $builder->build({
1222 source => 'Linktracker',
1224 borrowernumber => $borrowernumber,
1225 biblionumber => $source_biblionumber,
1226 itemnumber => $itemnumber2,
1230 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1231 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1233 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1234 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');
1236 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1237 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1238 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1239 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1240 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1241 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1243 $aq_order1->discard_changes;
1244 $aq_order2->discard_changes;
1245 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1246 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1248 $bib_level_hold->discard_changes;
1249 $item_level_hold1->discard_changes;
1250 $item_level_hold2->discard_changes;
1251 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1252 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1253 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1255 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1256 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1257 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1258 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1260 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1261 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1262 # Why does ->biblionumber return a Biblio object???
1263 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1264 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1266 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1267 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1268 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1269 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1271 $schema->storage->txn_rollback;
1274 subtest 'columns_to_str' => sub {
1277 $schema->storage->txn_begin;
1279 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1281 my $cache = Koha::Caches->get_instance();
1282 $cache->clear_from_cache("MarcStructure-0-");
1283 $cache->clear_from_cache("MarcStructure-1-");
1284 $cache->clear_from_cache("default_value_for_mod_marc-");
1285 $cache->clear_from_cache("MarcSubfieldStructure-");
1287 # Creating subfields 'é', 'è' that are not linked with a kohafield
1288 Koha::MarcSubfieldStructures->search(
1290 frameworkcode => '',
1291 tagfield => $itemtag,
1292 tagsubfield => ['é', 'è'],
1294 )->delete; # In case it exist already
1296 # é is not linked with a AV
1297 # è is linked with AV branches
1298 Koha::MarcSubfieldStructure->new(
1300 frameworkcode => '',
1301 tagfield => $itemtag,
1302 tagsubfield => 'é',
1305 defaultvalue => 'ééé',
1309 Koha::MarcSubfieldStructure->new(
1311 frameworkcode => '',
1312 tagfield => $itemtag,
1313 tagsubfield => 'è',
1316 defaultvalue => 'èèè',
1318 authorised_value => 'branches',
1322 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1323 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1324 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1325 my $dateaccessioned = '2020-12-15';
1326 my $library = Koha::Libraries->search->next;
1327 my $branchcode = $library->branchcode;
1329 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1331 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1332 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1333 xmlns="http://www.loc.gov/MARC21/slim">
1336 <leader> a </leader>
1337 <datafield tag="999" ind1=" " ind2=" ">
1338 <subfield code="é">value é</subfield>
1339 <subfield code="è">$branchcode</subfield>
1347 itemlost => $lost_av->authorised_value,
1348 dateaccessioned => $dateaccessioned,
1349 more_subfields_xml => $some_marc_xml,
1353 $item = $item->get_from_storage;
1355 my $s = $item->columns_to_str;
1356 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1357 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1358 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1359 is( $s->{'è'}, $library->branchname );
1361 $cache->clear_from_cache("MarcStructure-0-");
1362 $cache->clear_from_cache("MarcStructure-1-");
1363 $cache->clear_from_cache("default_value_for_mod_marc-");
1364 $cache->clear_from_cache("MarcSubfieldStructure-");
1366 $schema->storage->txn_rollback;
1370 subtest 'store() tests' => sub {
1374 subtest 'dateaccessioned handling' => sub {
1378 $schema->storage->txn_begin;
1380 my $item = $builder->build_sample_item;
1382 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1384 # reset dateaccessioned on the DB
1385 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1386 $item->discard_changes;
1388 ok( !defined $item->dateaccessioned );
1391 $item->replacementprice(100)->store->discard_changes;
1393 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1395 $schema->storage->txn_rollback;
1398 subtest '_set_found_trigger() tests' => sub {
1402 $schema->storage->txn_begin;
1404 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1405 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1407 # Add a lost item debit
1408 my $debit = $patron->account->add_debit(
1412 item_id => $item->id,
1413 interface => 'intranet',
1417 my $lostreturn_policy = 'charge';
1419 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1420 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1422 # simulate it was found
1423 $item->set( { itemlost => 0 } )->store;
1425 my $messages = $item->object_messages;
1427 my $message_1 = $messages->[0];
1429 is( $message_1->type, 'info', 'type is correct' );
1430 is( $message_1->message, 'lost_refunded', 'message is correct' );
1432 # Find the refund credit
1433 my $credit = $debit->credits->next;
1436 $message_1->payload,
1437 { credit_id => $credit->id },
1441 my $message_2 = $messages->[1];
1443 is( $message_2->type, 'info', 'type is correct' );
1444 is( $message_2->message, 'lost_charge', 'message is correct' );
1445 is( $message_2->payload, undef, 'no payload' );
1447 $schema->storage->txn_rollback;
1450 subtest 'holds_queue update tests' => sub {
1454 $schema->storage->txn_begin;
1456 my $biblio = $builder->build_sample_biblio;
1458 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1459 $mock->mock( 'enqueue', sub {
1460 my ( $self, $args ) = @_;
1462 $args->{biblio_ids},
1464 '->store triggers a holds queue update for the related biblio'
1468 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1471 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1474 $item->set({ reserves => 1 })->store;
1476 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1478 $item->set({ reserves => 0 })->store;
1480 $schema->storage->txn_rollback;
1484 subtest 'Recalls tests' => sub {
1488 $schema->storage->txn_begin;
1490 my $item1 = $builder->build_sample_item;
1491 my $biblio = $item1->biblio;
1492 my $branchcode = $item1->holdingbranch;
1493 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1494 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1495 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1496 my $item2 = $builder->build_object(
1497 { class => 'Koha::Items',
1498 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1502 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1503 t::lib::Mocks::mock_preference('UseRecalls', 1);
1505 my $recall1 = Koha::Recall->new(
1506 { patron_id => $patron1->borrowernumber,
1507 created_date => \'NOW()',
1508 biblio_id => $biblio->biblionumber,
1509 pickup_library_id => $branchcode,
1510 item_id => $item1->itemnumber,
1511 expiration_date => undef,
1515 my $recall2 = Koha::Recall->new(
1516 { patron_id => $patron2->borrowernumber,
1517 created_date => \'NOW()',
1518 biblio_id => $biblio->biblionumber,
1519 pickup_library_id => $branchcode,
1520 item_id => $item1->itemnumber,
1521 expiration_date => undef,
1526 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1528 $recall2->set_cancelled;
1530 t::lib::Mocks::mock_preference('UseRecalls', 0);
1531 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1533 t::lib::Mocks::mock_preference("UseRecalls", 1);
1535 $item1->update({ notforloan => 1 });
1536 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1537 $item1->update({ notforloan => 0, itemlost => 1 });
1538 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1539 $item1->update({ itemlost => 0, withdrawn => 1 });
1540 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1541 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1543 $item1->update({ withdrawn => 0 });
1544 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1546 Koha::CirculationRules->set_rules({
1547 branchcode => $branchcode,
1548 categorycode => $patron1->categorycode,
1549 itemtype => $item1->effective_itemtype,
1551 recalls_allowed => 0,
1552 recalls_per_record => 1,
1553 on_shelf_recalls => 'all',
1556 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1558 Koha::CirculationRules->set_rules({
1559 branchcode => $branchcode,
1560 categorycode => $patron1->categorycode,
1561 itemtype => $item1->effective_itemtype,
1563 recalls_allowed => 1,
1564 recalls_per_record => 1,
1565 on_shelf_recalls => 'all',
1568 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1569 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1570 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1572 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1573 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1574 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1576 $recall1->set_cancelled;
1577 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1579 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1581 Koha::CirculationRules->set_rules({
1582 branchcode => $branchcode,
1583 categorycode => $patron1->categorycode,
1584 itemtype => $item1->effective_itemtype,
1586 recalls_allowed => 1,
1587 recalls_per_record => 1,
1588 on_shelf_recalls => 'any',
1591 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1592 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1594 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1595 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1597 $recall1 = Koha::Recall->new(
1598 { patron_id => $patron1->borrowernumber,
1599 created_date => \'NOW()',
1600 biblio_id => $biblio->biblionumber,
1601 pickup_library_id => $branchcode,
1603 expiration_date => undef,
1608 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1610 Koha::CirculationRules->set_rules({
1611 branchcode => undef,
1612 categorycode => undef,
1613 itemtype => $item1->effective_itemtype,
1615 recalls_allowed => 0,
1616 recalls_per_record => 1,
1617 on_shelf_recalls => 'any',
1620 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1622 Koha::CirculationRules->set_rules({
1623 branchcode => undef,
1624 categorycode => undef,
1625 itemtype => $item1->effective_itemtype,
1627 recalls_allowed => 1,
1628 recalls_per_record => 1,
1629 on_shelf_recalls => 'any',
1632 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1634 # check_recalls tests
1636 $recall1 = Koha::Recall->new(
1637 { patron_id => $patron2->borrowernumber,
1638 created_date => \'NOW()',
1639 biblio_id => $biblio->biblionumber,
1640 pickup_library_id => $branchcode,
1641 item_id => $item1->itemnumber,
1642 expiration_date => undef,
1646 $recall2 = Koha::Recall->new(
1647 { patron_id => $patron1->borrowernumber,
1648 created_date => \'NOW()',
1649 biblio_id => $biblio->biblionumber,
1650 pickup_library_id => $branchcode,
1652 expiration_date => undef,
1656 $recall2->set_waiting( { item => $item1 } );
1657 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
1659 # return a waiting recall
1660 my $check_recall = $item1->check_recalls;
1661 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
1663 $recall2->revert_waiting;
1665 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
1667 # return recall based on recalldate
1668 $check_recall = $item1->check_recalls;
1669 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
1671 $recall1->set_cancelled;
1673 # return a biblio-level recall
1674 $check_recall = $item1->check_recalls;
1675 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
1677 $recall2->set_cancelled;
1679 $schema->storage->txn_rollback;
1682 subtest 'Notforloan tests' => sub {
1686 $schema->storage->txn_begin;
1688 my $item1 = $builder->build_sample_item;
1689 $item1->update({ notforloan => 0 });
1690 $item1->itemtype->notforloan(0);
1691 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
1692 $item1->update({ notforloan => 1 });
1693 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
1694 $item1->update({ notforloan => 0 });
1695 $item1->itemtype->update({ notforloan => 1 });
1696 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
1698 $schema->storage->txn_rollback;
1701 subtest 'item_group() tests' => sub {
1705 $schema->storage->txn_begin;
1707 my $biblio = $builder->build_sample_biblio();
1708 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1709 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1711 is( $item_1->item_group, undef, 'Item 1 has no item group');
1712 is( $item_2->item_group, undef, 'Item 2 has no item group');
1714 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1715 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1717 $item_group_1->add_item({ item_id => $item_1->id });
1718 $item_group_2->add_item({ item_id => $item_2->id });
1720 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
1721 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
1723 $schema->storage->txn_rollback;