3 # Copyright 2019 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Test::More tests => 28;
27 use C4::Biblio qw( GetMarcSubfieldStructure );
28 use C4::Circulation qw( AddIssue AddReturn );
33 use Koha::DateUtils qw( dt_from_string );
37 use List::MoreUtils qw(all);
39 use t::lib::TestBuilder;
43 my $schema = Koha::Database->new->schema;
44 my $builder = t::lib::TestBuilder->new;
46 subtest 'return_claims relationship' => sub {
49 $schema->storage->txn_begin;
51 my $biblio = $builder->build_sample_biblio();
52 my $item = $builder->build_sample_item({
53 biblionumber => $biblio->biblionumber,
55 my $return_claims = $item->return_claims;
56 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
57 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
58 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
59 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
61 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
63 $schema->storage->txn_rollback;
66 subtest 'return_claim accessor' => sub {
69 $schema->storage->txn_begin;
71 my $biblio = $builder->build_sample_biblio();
72 my $item = $builder->build_sample_item({
73 biblionumber => $biblio->biblionumber,
75 my $return_claim = $item->return_claim;
76 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
78 my $claim1 = $builder->build_object(
80 class => 'Koha::Checkouts::ReturnClaims',
81 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
84 my $claim2 = $builder->build_object(
86 class => 'Koha::Checkouts::ReturnClaims',
87 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
91 $return_claim = $item->return_claim;
92 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
93 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
95 $claim2->resolution('test')->store();
96 $return_claim = $item->return_claim;
97 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
99 $claim1->resolution('test')->store();
100 $return_claim = $item->return_claim;
101 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
103 $schema->storage->txn_rollback;
106 subtest 'tracked_links relationship' => sub {
109 my $biblio = $builder->build_sample_biblio();
110 my $item = $builder->build_sample_item({
111 biblionumber => $biblio->biblionumber,
113 my $tracked_links = $item->tracked_links;
114 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
115 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
116 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
117 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
119 is($item->tracked_links()->count,2,"Two tracked links found");
122 subtest 'is_bundle tests' => sub {
125 $schema->storage->txn_begin;
127 my $item = $builder->build_sample_item();
129 my $is_bundle = $item->is_bundle;
130 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
132 my $item2 = $builder->build_sample_item();
133 $schema->resultset('ItemBundle')
134 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
136 $is_bundle = $item->is_bundle;
137 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
139 $schema->storage->txn_rollback;
142 subtest 'in_bundle tests' => sub {
145 $schema->storage->txn_begin;
147 my $item = $builder->build_sample_item();
149 my $in_bundle = $item->in_bundle;
150 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
152 my $host_item = $builder->build_sample_item();
153 $schema->resultset('ItemBundle')
154 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
156 $in_bundle = $item->in_bundle;
157 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
159 $schema->storage->txn_rollback;
162 subtest 'bundle_items tests' => sub {
165 $schema->storage->txn_begin;
167 my $host_item = $builder->build_sample_item();
168 my $bundle_items = $host_item->bundle_items;
169 is( ref($bundle_items), 'Koha::Items',
170 'bundle_items returns a Koha::Items object set' );
171 is( $bundle_items->count, 0,
172 'bundle_items set is empty when no items are bundled' );
174 my $bundle_item1 = $builder->build_sample_item();
175 my $bundle_item2 = $builder->build_sample_item();
176 my $bundle_item3 = $builder->build_sample_item();
177 $schema->resultset('ItemBundle')
179 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
180 $schema->resultset('ItemBundle')
182 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
183 $schema->resultset('ItemBundle')
185 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
187 $bundle_items = $host_item->bundle_items;
188 is( $bundle_items->count, 3,
189 'bundle_items returns all the bundled items in the set' );
191 $schema->storage->txn_rollback;
194 subtest 'bundle_host tests' => sub {
197 $schema->storage->txn_begin;
199 my $host_item = $builder->build_sample_item();
200 my $bundle_item1 = $builder->build_sample_item();
201 my $bundle_item2 = $builder->build_sample_item();
202 $schema->resultset('ItemBundle')
204 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
206 my $bundle_host = $bundle_item1->bundle_host;
207 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
208 $bundle_host = $bundle_item2->bundle_host;
209 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
210 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
212 $schema->storage->txn_rollback;
215 subtest 'add_to_bundle tests' => sub {
218 $schema->storage->txn_begin;
220 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
222 my $library = $builder->build_object({ class => 'Koha::Libraries' });
223 t::lib::Mocks::mock_userenv({
224 branchcode => $library->branchcode
227 my $host_item = $builder->build_sample_item();
228 my $bundle_item1 = $builder->build_sample_item();
229 my $bundle_item2 = $builder->build_sample_item();
230 my $bundle_item3 = $builder->build_sample_item();
232 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
234 throws_ok { $host_item->add_to_bundle($host_item) }
235 'Koha::Exceptions::Item::Bundle::IsBundle',
236 'Exception thrown if you try to add the item to itself';
238 my $reserve_id = C4::Reserves::AddReserve(
240 branchcode => $library->branchcode,
241 borrowernumber => $patron->borrowernumber,
242 biblionumber => $bundle_item3->biblionumber,
243 itemnumber => $bundle_item3->itemnumber,
246 throws_ok { $host_item->add_to_bundle($bundle_item3) }
247 'Koha::Exceptions::Item::Bundle::ItemHasHolds',
248 'Exception thrown if you try to add an item with holds to a bundle';
250 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
251 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
253 throws_ok { $host_item->add_to_bundle($bundle_item1) }
254 'Koha::Exceptions::Object::DuplicateID',
255 'Exception thrown if you try to add the same item twice';
257 throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
258 'Koha::Exceptions::Item::Bundle::IsBundle',
259 'Exception thrown if you try to add an item to a bundled item';
261 throws_ok { $bundle_item2->add_to_bundle($host_item) }
262 'Koha::Exceptions::Item::Bundle::IsBundle',
263 'Exception thrown if you try to add a bundle host to a bundle item';
265 C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
266 throws_ok { $host_item->add_to_bundle($bundle_item2) }
267 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
268 'Exception thrown if you try to add a checked out item';
270 $bundle_item2->withdrawn(1)->store;
271 t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
272 throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
273 'Koha::Exceptions::Checkin::FailedCheckin',
274 'Exception thrown if you try to add a checked out item using
275 "force_checkin" and the return is not possible';
277 $bundle_item2->withdrawn(0)->store;
278 lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
279 'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
281 $bundle_item2->discard_changes;
282 ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
284 $schema->storage->txn_rollback;
287 subtest 'remove_from_bundle tests' => sub {
290 $schema->storage->txn_begin;
292 my $host_item = $builder->build_sample_item();
293 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
294 $schema->resultset('ItemBundle')
296 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
298 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
299 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
300 $bundle_item1 = $bundle_item1->get_from_storage;
301 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
303 $schema->storage->txn_rollback;
306 subtest 'hidden_in_opac() tests' => sub {
310 $schema->storage->txn_begin;
312 my $item = $builder->build_sample_item({ itemlost => 2 });
315 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
316 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
318 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
319 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
321 # enable hidelostitems to verify correct behaviour
322 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
323 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
325 # disable hidelostitems
326 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
327 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
329 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
331 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
335 $schema->storage->txn_rollback;
338 subtest 'has_pending_hold() tests' => sub {
342 $schema->storage->txn_begin;
344 my $dbh = C4::Context->dbh;
345 my $item = $builder->build_sample_item({ itemlost => 0 });
346 my $itemnumber = $item->itemnumber;
348 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
349 ok( $item->has_pending_hold, "Yes, we have a pending hold");
350 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
351 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
353 $schema->storage->txn_rollback;
356 subtest "as_marc_field() tests" => sub {
358 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
359 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
361 my @schema_columns = $schema->resultset('Item')->result_source->columns;
362 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
364 plan tests => 2 * (scalar @mapped_columns + 1) + 4;
366 $schema->storage->txn_begin;
368 my $item = $builder->build_sample_item;
369 # Make sure it has at least one undefined attribute
370 $item->set({ replacementprice => undef })->store->discard_changes;
372 # Tests with the mss parameter
373 my $marc_field = $item->as_marc_field({ mss => $mss });
378 'Generated field set the right tag number'
381 foreach my $column ( @mapped_columns ) {
382 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
383 is( $marc_field->subfield($tagsubfield),
384 $item->$column, "Value is mapped correctly for column $column" );
387 # Tests without the mss parameter
388 $marc_field = $item->as_marc_field();
393 'Generated field set the right tag number'
396 foreach my $column (@mapped_columns) {
397 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
398 is( $marc_field->subfield($tagsubfield),
399 $item->$column, "Value is mapped correctly for column $column" );
402 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
405 tagfield => $itemtag,
409 Koha::MarcSubfieldStructure->new(
412 tagfield => $itemtag,
418 my @unlinked_subfields;
419 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
420 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
422 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
423 Koha::MarcSubfieldStructures->search(
424 { frameworkcode => '', tagfield => $itemtag } )
425 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
427 $marc_field = $item->as_marc_field;
429 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
430 my @subfields = $marc_field->subfields;
431 my $result = all { defined $_->[1] } @subfields;
432 ok( $result, 'There are no undef subfields' );
433 my @ordered_subfields = sort {
434 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
435 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
437 is_deeply(\@subfields, \@ordered_subfields);
439 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
440 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
442 $schema->storage->txn_rollback;
443 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
446 subtest 'pickup_locations' => sub {
449 $schema->storage->txn_begin;
451 my $dbh = C4::Context->dbh;
453 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
454 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
455 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
456 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
457 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
458 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
459 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
460 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
462 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
463 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
466 $library1->branchcode, $library2->branchcode,
467 $library3->branchcode, $library4->branchcode
470 my $item1 = $builder->build_sample_item(
472 homebranch => $library1->branchcode,
473 holdingbranch => $library2->branchcode,
479 my $item3 = $builder->build_sample_item(
481 homebranch => $library3->branchcode,
482 holdingbranch => $library4->branchcode,
484 itype => $item1->itype,
488 Koha::CirculationRules->set_rules(
490 categorycode => undef,
491 itemtype => $item1->itype,
494 reservesallowed => 25,
500 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
501 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
504 "1-1-from_home_library-any" => 3,
505 "1-1-from_home_library-holdgroup" => 2,
506 "1-1-from_home_library-patrongroup" => 2,
507 "1-1-from_home_library-homebranch" => 1,
508 "1-1-from_home_library-holdingbranch" => 1,
509 "1-1-from_any_library-any" => 3,
510 "1-1-from_any_library-holdgroup" => 2,
511 "1-1-from_any_library-patrongroup" => 2,
512 "1-1-from_any_library-homebranch" => 1,
513 "1-1-from_any_library-holdingbranch" => 1,
514 "1-1-from_local_hold_group-any" => 3,
515 "1-1-from_local_hold_group-holdgroup" => 2,
516 "1-1-from_local_hold_group-patrongroup" => 2,
517 "1-1-from_local_hold_group-homebranch" => 1,
518 "1-1-from_local_hold_group-holdingbranch" => 1,
519 "1-4-from_home_library-any" => 0,
520 "1-4-from_home_library-holdgroup" => 0,
521 "1-4-from_home_library-patrongroup" => 0,
522 "1-4-from_home_library-homebranch" => 0,
523 "1-4-from_home_library-holdingbranch" => 0,
524 "1-4-from_any_library-any" => 3,
525 "1-4-from_any_library-holdgroup" => 2,
526 "1-4-from_any_library-patrongroup" => 1,
527 "1-4-from_any_library-homebranch" => 1,
528 "1-4-from_any_library-holdingbranch" => 1,
529 "1-4-from_local_hold_group-any" => 0,
530 "1-4-from_local_hold_group-holdgroup" => 0,
531 "1-4-from_local_hold_group-patrongroup" => 0,
532 "1-4-from_local_hold_group-homebranch" => 0,
533 "1-4-from_local_hold_group-holdingbranch" => 0,
534 "3-1-from_home_library-any" => 0,
535 "3-1-from_home_library-holdgroup" => 0,
536 "3-1-from_home_library-patrongroup" => 0,
537 "3-1-from_home_library-homebranch" => 0,
538 "3-1-from_home_library-holdingbranch" => 0,
539 "3-1-from_any_library-any" => 3,
540 "3-1-from_any_library-holdgroup" => 1,
541 "3-1-from_any_library-patrongroup" => 2,
542 "3-1-from_any_library-homebranch" => 0,
543 "3-1-from_any_library-holdingbranch" => 1,
544 "3-1-from_local_hold_group-any" => 0,
545 "3-1-from_local_hold_group-holdgroup" => 0,
546 "3-1-from_local_hold_group-patrongroup" => 0,
547 "3-1-from_local_hold_group-homebranch" => 0,
548 "3-1-from_local_hold_group-holdingbranch" => 0,
549 "3-4-from_home_library-any" => 0,
550 "3-4-from_home_library-holdgroup" => 0,
551 "3-4-from_home_library-patrongroup" => 0,
552 "3-4-from_home_library-homebranch" => 0,
553 "3-4-from_home_library-holdingbranch" => 0,
554 "3-4-from_any_library-any" => 3,
555 "3-4-from_any_library-holdgroup" => 1,
556 "3-4-from_any_library-patrongroup" => 1,
557 "3-4-from_any_library-homebranch" => 0,
558 "3-4-from_any_library-holdingbranch" => 1,
559 "3-4-from_local_hold_group-any" => 3,
560 "3-4-from_local_hold_group-holdgroup" => 1,
561 "3-4-from_local_hold_group-patrongroup" => 1,
562 "3-4-from_local_hold_group-homebranch" => 0,
563 "3-4-from_local_hold_group-holdingbranch" => 1
567 my ( $item, $patron, $ha, $hfp, $results ) = @_;
569 Koha::CirculationRules->set_rules(
575 hold_fulfillment_policy => $hfp,
576 returnbranch => 'any'
581 $ha eq 'from_local_hold_group' ? 'holdgroup'
583 $ha eq 'from_any_library' ? 'any'
588 my $pickup_location = $_;
589 grep { $pickup_location->branchcode eq $_ } @branchcodes
590 } $item->pickup_locations( { patron => $patron } )->as_list;
593 scalar(@pl) eq $results->{
594 $item->copynumber . '-'
595 . $patron->firstname . '-'
605 . ', hold_fulfillment_policy: '
609 $item->copynumber . '-'
610 . $patron->firstname . '-'
621 foreach my $item ($item1, $item3) {
622 foreach my $patron ($patron1, $patron4) {
623 #holdallowed 1: homebranch, 2: any, 3: holdgroup
624 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
625 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
626 _doTest($item, $patron, $ha, $hfp, $results);
632 # Now test that branchtransferlimits will further filter the pickup locations
634 my $item_no_ccode = $builder->build_sample_item(
636 homebranch => $library1->branchcode,
637 holdingbranch => $library2->branchcode,
638 itype => $item1->itype,
642 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
643 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
644 Koha::CirculationRules->set_rules(
647 itemtype => $item1->itype,
649 holdallowed => 'from_home_library',
650 hold_fulfillment_policy => 1,
651 returnbranch => 'any'
655 $builder->build_object(
657 class => 'Koha::Item::Transfer::Limits',
659 toBranch => $library1->branchcode,
660 fromBranch => $library2->branchcode,
661 itemtype => $item1->itype,
667 my @pickup_locations = map {
668 my $pickup_location = $_;
669 grep { $pickup_location->branchcode eq $_ } @branchcodes
670 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
672 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
674 $builder->build_object(
676 class => 'Koha::Item::Transfer::Limits',
678 toBranch => $library4->branchcode,
679 fromBranch => $library2->branchcode,
680 itemtype => $item1->itype,
686 @pickup_locations = map {
687 my $pickup_location = $_;
688 grep { $pickup_location->branchcode eq $_ } @branchcodes
689 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
691 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
693 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
694 @pickup_locations = map {
695 my $pickup_location = $_;
696 grep { $pickup_location->branchcode eq $_ } @branchcodes
697 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
698 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
700 @pickup_locations = map {
701 my $pickup_location = $_;
702 grep { $pickup_location->branchcode eq $_ } @branchcodes
703 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
704 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");
706 $builder->build_object(
708 class => 'Koha::Item::Transfer::Limits',
710 toBranch => $library2->branchcode,
711 fromBranch => $library2->branchcode,
713 ccode => $item1->ccode,
718 @pickup_locations = map {
719 my $pickup_location = $_;
720 grep { $pickup_location->branchcode eq $_ } @branchcodes
721 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
722 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
724 $builder->build_object(
726 class => 'Koha::Item::Transfer::Limits',
728 toBranch => $library4->branchcode,
729 fromBranch => $library2->branchcode,
731 ccode => $item1->ccode,
736 @pickup_locations = map {
737 my $pickup_location = $_;
738 grep { $pickup_location->branchcode eq $_ } @branchcodes
739 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
740 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
742 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
744 $schema->storage->txn_rollback;
747 subtest 'request_transfer' => sub {
749 $schema->storage->txn_begin;
751 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
752 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
753 my $item = $builder->build_sample_item(
755 homebranch => $library1->branchcode,
756 holdingbranch => $library2->branchcode,
760 # Mandatory fields tests
761 throws_ok { $item->request_transfer( { to => $library1 } ) }
762 'Koha::Exceptions::MissingParameter',
763 'Exception thrown if `reason` parameter is missing';
765 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
766 'Koha::Exceptions::MissingParameter',
767 'Exception thrown if `to` parameter is missing';
770 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
771 is( ref($transfer), 'Koha::Item::Transfer',
772 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
774 my $original_transfer = $transfer->get_from_storage;
776 # Transfer already in progress
777 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
778 'Koha::Exceptions::Item::Transfer::InQueue',
779 'Exception thrown if transfer is already in progress';
782 is( ref( $exception->transfer ),
783 'Koha::Item::Transfer',
784 'The exception contains the found Koha::Item::Transfer' );
787 my $queued_transfer = $item->request_transfer(
788 { to => $library2, reason => 'Manual', enqueue => 1 } );
789 is( ref($queued_transfer), 'Koha::Item::Transfer',
790 'Koha::Item->request_transfer allowed when enqueue is set' );
791 my $transfers = $item->get_transfers;
792 is($transfers->count, 2, "There are now 2 live transfers in the queue");
793 $transfer = $transfer->get_from_storage;
794 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
795 $queued_transfer->datearrived(dt_from_string)->store();
798 my $replaced_transfer = $item->request_transfer(
799 { to => $library2, reason => 'Manual', replace => 1 } );
800 is( ref($replaced_transfer), 'Koha::Item::Transfer',
801 'Koha::Item->request_transfer allowed when replace is set' );
802 $original_transfer->discard_changes;
803 ok($original_transfer->datecancelled, "Original transfer cancelled");
804 $transfers = $item->get_transfers;
805 is($transfers->count, 1, "There is only 1 live transfer in the queue");
806 $replaced_transfer->datearrived(dt_from_string)->store();
808 # BranchTransferLimits
809 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
810 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
811 my $limit = Koha::Item::Transfer::Limit->new({
812 fromBranch => $library2->branchcode,
813 toBranch => $library1->branchcode,
814 itemtype => $item->effective_itemtype,
817 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
818 'Koha::Exceptions::Item::Transfer::Limit',
819 'Exception thrown if transfer is prevented by limits';
821 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
822 is( ref($forced_transfer), 'Koha::Item::Transfer',
823 'Koha::Item->request_transfer allowed when ignore_limits is set'
826 $schema->storage->txn_rollback;
829 subtest 'deletion' => sub {
832 $schema->storage->txn_begin;
834 my $biblio = $builder->build_sample_biblio();
836 my $item = $builder->build_sample_item(
838 biblionumber => $biblio->biblionumber,
841 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
843 my $deleted_item = $item->move_to_deleted;
844 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
845 ; # FIXME This should be Koha::Deleted::Item
846 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
848 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
849 $item = $builder->build_sample_item(
851 biblionumber => $biblio->biblionumber,
855 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
858 my $library = $builder->build_object({ class => 'Koha::Libraries' });
859 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
860 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
862 my $patron = $builder->build_object({class => 'Koha::Patrons'});
863 $item = $builder->build_sample_item({ library => $library->branchcode });
866 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
869 @{$item->safe_to_delete->messages}[0]->message,
871 'Koha::Item->safe_to_delete reports item on loan',
875 @{$item->safe_to_delete->messages}[0]->message,
877 'item that is on loan cannot be deleted',
881 ! $item->safe_to_delete,
882 'Koha::Item->safe_to_delete shows item NOT safe to delete'
885 AddReturn( $item->barcode, $library->branchcode );
888 t::lib::Mocks::mock_preference('IndependentBranches', 1);
889 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
892 @{$item_2->safe_to_delete->messages}[0]->message,
894 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
898 @{$item_2->safe_to_delete->messages}[0]->message,
900 'IndependentBranches prevents deletion at another branch',
905 { # codeblock to limit scope of $module->mock
907 my $module = Test::MockModule->new('C4::Items');
908 $module->mock( GetAnalyticsCount => sub { return 1 } );
910 $item->discard_changes;
912 @{$item->safe_to_delete->messages}[0]->message,
914 'Koha::Item->safe_to_delete reports linked analytics',
918 @{$item->safe_to_delete->messages}[0]->message,
920 'Linked analytics prevents deletion of item',
926 $item->safe_to_delete,
927 'Koha::Item->safe_to_delete shows item safe to delete'
932 my $test_item = Koha::Items->find( $item->itemnumber );
934 is( $test_item, undef,
935 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
938 subtest 'holds tests' => sub {
943 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
945 $schema->storage->txn_begin;
947 my $item = $builder->build_sample_item;
949 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
950 my $safe_to_delete = $item->safe_to_delete;
952 ok( !$safe_to_delete, 'Cannot delete' );
954 @{ $safe_to_delete->messages }[0]->message,
956 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
961 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
962 $safe_to_delete = $item->safe_to_delete;
964 ok( !$safe_to_delete, 'Cannot delete' );
966 @{ $safe_to_delete->messages }[0]->message,
968 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
973 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
974 $safe_to_delete = $item->safe_to_delete;
976 ok( !$safe_to_delete, 'Cannot delete' );
978 @{ $safe_to_delete->messages }[0]->message,
980 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
985 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
986 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
988 $safe_to_delete = $item->safe_to_delete;
990 ok( !$safe_to_delete );
993 @{ $safe_to_delete->messages}[0]->message,
994 'last_item_for_hold',
995 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
998 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
1000 ok( $item->safe_to_delete );
1002 $schema->storage->txn_rollback;
1005 $schema->storage->txn_rollback;
1008 subtest 'renewal_branchcode' => sub {
1011 $schema->storage->txn_begin;
1013 my $item = $builder->build_sample_item();
1014 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1015 my $checkout = $builder->build_object({
1016 class => 'Koha::Checkouts',
1018 itemnumber => $item->itemnumber,
1023 C4::Context->interface( 'intranet' );
1024 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1026 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1027 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1028 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1029 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1031 C4::Context->interface( 'opac' );
1033 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1034 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1035 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1037 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1038 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1039 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1041 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1042 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1043 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");
1045 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1046 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1047 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");
1049 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1050 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1051 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");
1053 $schema->storage->txn_rollback;
1056 subtest 'Tests for itemtype' => sub {
1058 $schema->storage->txn_begin;
1060 my $biblio = $builder->build_sample_biblio;
1061 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1062 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1064 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1065 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1066 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1067 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1069 $schema->storage->txn_rollback;
1072 subtest 'get_transfers' => sub {
1074 $schema->storage->txn_begin;
1076 my $item = $builder->build_sample_item();
1078 my $transfers = $item->get_transfers();
1079 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1080 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1082 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1084 my $transfer_1 = $builder->build_object(
1086 class => 'Koha::Item::Transfers',
1088 itemnumber => $item->itemnumber,
1089 frombranch => $item->holdingbranch,
1090 tobranch => $library_to->branchcode,
1093 datearrived => undef,
1094 datecancelled => undef,
1095 daterequested => \'NOW()'
1100 $transfers = $item->get_transfers();
1101 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1103 my $transfer_2 = $builder->build_object(
1105 class => 'Koha::Item::Transfers',
1107 itemnumber => $item->itemnumber,
1108 frombranch => $item->holdingbranch,
1109 tobranch => $library_to->branchcode,
1112 datearrived => undef,
1113 datecancelled => undef,
1114 daterequested => \'NOW()'
1119 my $transfer_3 = $builder->build_object(
1121 class => 'Koha::Item::Transfers',
1123 itemnumber => $item->itemnumber,
1124 frombranch => $item->holdingbranch,
1125 tobranch => $library_to->branchcode,
1128 datearrived => undef,
1129 datecancelled => undef,
1130 daterequested => \'NOW()'
1135 $transfers = $item->get_transfers();
1136 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1137 my $result_1 = $transfers->next;
1138 my $result_2 = $transfers->next;
1139 my $result_3 = $transfers->next;
1140 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1141 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1142 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1144 $transfer_2->datesent(\'NOW()')->store;
1145 $transfers = $item->get_transfers();
1146 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1147 $result_1 = $transfers->next;
1148 $result_2 = $transfers->next;
1149 $result_3 = $transfers->next;
1150 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1151 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1152 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1154 $transfer_2->datearrived(\'NOW()')->store;
1155 $transfers = $item->get_transfers();
1156 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1157 $result_1 = $transfers->next;
1158 $result_2 = $transfers->next;
1159 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1160 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1162 $transfer_1->datecancelled(\'NOW()')->store;
1163 $transfers = $item->get_transfers();
1164 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1165 $result_1 = $transfers->next;
1166 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1168 $schema->storage->txn_rollback;
1171 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1174 $schema->storage->txn_begin;
1176 my $biblio = $builder->build_sample_biblio();
1177 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1179 my $orders = $item->orders;
1180 is ($orders->count, 0, 'No order on this item yet');
1182 my $order_note = 'Order for ' . $item->itemnumber;
1184 my $aq_order1 = $builder->build_object({
1185 class => 'Koha::Acquisition::Orders',
1187 biblionumber => $biblio->biblionumber,
1188 order_internalnote => $order_note,
1191 my $aq_order2 = $builder->build_object({
1192 class => 'Koha::Acquisition::Orders',
1194 biblionumber => $biblio->biblionumber,
1197 my $aq_order_item1 = $builder->build({
1198 source => 'AqordersItem',
1200 ordernumber => $aq_order1->ordernumber,
1201 itemnumber => $item->itemnumber,
1205 $orders = $item->orders;
1206 is ($orders->count, 1, 'One order found by item with the relationship');
1207 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1210 subtest 'move_to_biblio() tests' => sub {
1213 $schema->storage->txn_begin;
1215 my $dbh = C4::Context->dbh;
1217 my $source_biblio = $builder->build_sample_biblio();
1218 my $target_biblio = $builder->build_sample_biblio();
1220 my $source_biblionumber = $source_biblio->biblionumber;
1221 my $target_biblionumber = $target_biblio->biblionumber;
1223 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1224 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1225 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1227 my $itemnumber1 = $item1->itemnumber;
1228 my $itemnumber2 = $item2->itemnumber;
1230 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1232 my $patron = $builder->build_object({
1233 class => 'Koha::Patrons',
1234 value => { branchcode => $library->branchcode }
1236 my $borrowernumber = $patron->borrowernumber;
1238 my $aq_budget = $builder->build({
1239 source => 'Aqbudget',
1241 budget_notes => 'test',
1245 my $aq_order1 = $builder->build_object({
1246 class => 'Koha::Acquisition::Orders',
1248 biblionumber => $source_biblionumber,
1249 budget_id => $aq_budget->{budget_id},
1252 my $aq_order_item1 = $builder->build({
1253 source => 'AqordersItem',
1255 ordernumber => $aq_order1->ordernumber,
1256 itemnumber => $itemnumber1,
1259 my $aq_order2 = $builder->build_object({
1260 class => 'Koha::Acquisition::Orders',
1262 biblionumber => $source_biblionumber,
1263 budget_id => $aq_budget->{budget_id},
1266 my $aq_order_item2 = $builder->build({
1267 source => 'AqordersItem',
1269 ordernumber => $aq_order2->ordernumber,
1270 itemnumber => $itemnumber2,
1274 my $bib_level_hold = $builder->build_object({
1275 class => 'Koha::Holds',
1277 biblionumber => $source_biblionumber,
1278 itemnumber => undef,
1281 my $item_level_hold1 = $builder->build_object({
1282 class => 'Koha::Holds',
1284 biblionumber => $source_biblionumber,
1285 itemnumber => $itemnumber1,
1288 my $item_level_hold2 = $builder->build_object({
1289 class => 'Koha::Holds',
1291 biblionumber => $source_biblionumber,
1292 itemnumber => $itemnumber2,
1296 my $tmp_holdsqueue1 = $builder->build({
1297 source => 'TmpHoldsqueue',
1299 borrowernumber => $borrowernumber,
1300 biblionumber => $source_biblionumber,
1301 itemnumber => $itemnumber1,
1304 my $tmp_holdsqueue2 = $builder->build({
1305 source => 'TmpHoldsqueue',
1307 borrowernumber => $borrowernumber,
1308 biblionumber => $source_biblionumber,
1309 itemnumber => $itemnumber2,
1312 my $hold_fill_target1 = $builder->build({
1313 source => 'HoldFillTarget',
1315 borrowernumber => $borrowernumber,
1316 biblionumber => $source_biblionumber,
1317 itemnumber => $itemnumber1,
1320 my $hold_fill_target2 = $builder->build({
1321 source => 'HoldFillTarget',
1323 borrowernumber => $borrowernumber,
1324 biblionumber => $source_biblionumber,
1325 itemnumber => $itemnumber2,
1328 my $linktracker1 = $builder->build({
1329 source => 'Linktracker',
1331 borrowernumber => $borrowernumber,
1332 biblionumber => $source_biblionumber,
1333 itemnumber => $itemnumber1,
1336 my $linktracker2 = $builder->build({
1337 source => 'Linktracker',
1339 borrowernumber => $borrowernumber,
1340 biblionumber => $source_biblionumber,
1341 itemnumber => $itemnumber2,
1345 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1346 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1348 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1349 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');
1351 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1352 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1353 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1354 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1355 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1356 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1358 $aq_order1->discard_changes;
1359 $aq_order2->discard_changes;
1360 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1361 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1363 $bib_level_hold->discard_changes;
1364 $item_level_hold1->discard_changes;
1365 $item_level_hold2->discard_changes;
1366 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1367 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1368 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1370 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1371 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1372 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1373 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1375 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1376 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1377 # Why does ->biblionumber return a Biblio object???
1378 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1379 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1381 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1382 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1383 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1384 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1386 $schema->storage->txn_rollback;
1389 subtest 'columns_to_str' => sub {
1392 $schema->storage->txn_begin;
1394 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1396 my $cache = Koha::Caches->get_instance();
1397 $cache->clear_from_cache("MarcStructure-0-");
1398 $cache->clear_from_cache("MarcStructure-1-");
1399 $cache->clear_from_cache("MarcSubfieldStructure-");
1400 $cache->clear_from_cache("libraries:name");
1401 $cache->clear_from_cache("itemtype:description:en");
1402 $cache->clear_from_cache("cn_sources:description");
1403 $cache->clear_from_cache("AV_descriptions:LOST");
1405 # Creating subfields 'é', 'è' that are not linked with a kohafield
1406 Koha::MarcSubfieldStructures->search(
1408 frameworkcode => '',
1409 tagfield => $itemtag,
1410 tagsubfield => ['é', 'è'],
1412 )->delete; # In case it exist already
1414 # é is not linked with a AV
1415 # è is linked with AV branches
1416 Koha::MarcSubfieldStructure->new(
1418 frameworkcode => '',
1419 tagfield => $itemtag,
1420 tagsubfield => 'é',
1423 defaultvalue => 'ééé',
1427 Koha::MarcSubfieldStructure->new(
1429 frameworkcode => '',
1430 tagfield => $itemtag,
1431 tagsubfield => 'è',
1434 defaultvalue => 'èèè',
1436 authorised_value => 'branches',
1440 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1441 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1442 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1443 my $dateaccessioned = '2020-12-15';
1444 my $library = Koha::Libraries->search->next;
1445 my $branchcode = $library->branchcode;
1447 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1449 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1450 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1451 xmlns="http://www.loc.gov/MARC21/slim">
1454 <leader> a </leader>
1455 <datafield tag="999" ind1=" " ind2=" ">
1456 <subfield code="é">value é</subfield>
1457 <subfield code="è">$branchcode</subfield>
1465 itemlost => $lost_av->authorised_value,
1466 dateaccessioned => $dateaccessioned,
1467 more_subfields_xml => $some_marc_xml,
1471 $item = $item->get_from_storage;
1473 my $s = $item->columns_to_str;
1474 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1475 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1476 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1477 is( $s->{'è'}, $library->branchname );
1479 $cache->clear_from_cache("MarcStructure-0-");
1480 $cache->clear_from_cache("MarcStructure-1-");
1481 $cache->clear_from_cache("MarcSubfieldStructure-");
1482 $cache->clear_from_cache("libraries:name");
1483 $cache->clear_from_cache("itemtype:description:en");
1484 $cache->clear_from_cache("cn_sources:description");
1485 $cache->clear_from_cache("AV_descriptions:LOST");
1487 $schema->storage->txn_rollback;
1490 subtest 'strings_map() tests' => sub {
1494 $schema->storage->txn_begin;
1496 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1498 my $cache = Koha::Caches->get_instance();
1499 $cache->clear_from_cache("MarcStructure-0-");
1500 $cache->clear_from_cache("MarcStructure-1-");
1501 $cache->clear_from_cache("MarcSubfieldStructure-");
1502 $cache->clear_from_cache("libraries:name");
1503 $cache->clear_from_cache("itemtype:description:en");
1504 $cache->clear_from_cache("cn_sources:description");
1505 $cache->clear_from_cache("AV_descriptions:LOST");
1507 # Recreating subfields just to be sure tests will be ok
1512 Koha::MarcSubfieldStructures->search(
1514 frameworkcode => '',
1515 tagfield => $itemtag,
1516 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1518 )->delete; # In case it exist already
1520 Koha::MarcSubfieldStructure->new(
1522 authorised_value => 'LOST',
1524 frameworkcode => '',
1525 kohafield => 'items.itemlost',
1528 tagfield => $itemtag,
1532 Koha::MarcSubfieldStructure->new(
1534 authorised_value => 'cn_source',
1536 frameworkcode => '',
1537 kohafield => 'items.cn_source',
1540 tagfield => $itemtag,
1544 Koha::MarcSubfieldStructure->new(
1546 authorised_value => '',
1548 frameworkcode => '',
1549 kohafield => 'items.materials',
1552 tagfield => $itemtag,
1556 Koha::MarcSubfieldStructure->new(
1558 authorised_value => 'branches',
1560 frameworkcode => '',
1561 kohafield => 'items.homebranch',
1564 tagfield => $itemtag,
1568 Koha::MarcSubfieldStructure->new(
1570 authorised_value => 'itemtypes',
1572 frameworkcode => '',
1573 kohafield => 'items.itype',
1576 tagfield => $itemtag,
1581 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1582 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1583 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1584 my $item = $builder->build_sample_item(
1586 biblionumber => $biblio->id,
1587 library => $library->id
1591 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1592 my $lost_av = $builder->build_object(
1594 class => 'Koha::AuthorisedValues',
1596 authorised_value => 3,
1598 lib => 'internal description',
1599 lib_opac => 'public description',
1604 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1605 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1606 my $class_source = $builder->build_object(
1608 class => 'Koha::ClassSources',
1610 class_sort_rule => $class_sort_rule->class_sort_rule,
1611 class_split_rule => $class_split_rule->class_split_rule,
1618 cn_source => $class_source->id,
1619 itemlost => $lost_av->authorised_value,
1620 itype => $itype->itemtype,
1621 materials => 'Suff',
1623 )->store->discard_changes;
1625 my $strings = $item->strings_map;
1627 subtest 'unmapped field tests' => sub {
1631 ok( !exists $strings->{materials}, "Unmapped field not present" );
1634 subtest 'av handling' => sub {
1638 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1639 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1640 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1641 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1644 subtest 'cn_source handling' => sub {
1648 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1649 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1650 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1653 subtest 'branches handling' => sub {
1657 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1658 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1659 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1662 subtest 'itemtype handling' => sub {
1666 ok( exists $strings->{itype}, "'itype' entry exists" );
1667 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1668 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1671 subtest 'public flag tests' => sub {
1675 $strings = $item->strings_map( { public => 1 } );
1677 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1678 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1679 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1680 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1683 $cache->clear_from_cache("MarcStructure-0-");
1684 $cache->clear_from_cache("MarcStructure-1-");
1685 $cache->clear_from_cache("MarcSubfieldStructure-");
1686 $cache->clear_from_cache("libraries:name");
1687 $cache->clear_from_cache("itemtype:description:en");
1688 $cache->clear_from_cache("cn_sources:description");
1690 $schema->storage->txn_rollback;
1693 subtest 'store() tests' => sub {
1697 subtest 'dateaccessioned handling' => sub {
1701 $schema->storage->txn_begin;
1703 my $item = $builder->build_sample_item;
1705 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1707 # reset dateaccessioned on the DB
1708 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1709 $item->discard_changes;
1711 ok( !defined $item->dateaccessioned );
1714 $item->replacementprice(100)->store->discard_changes;
1716 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1718 $schema->storage->txn_rollback;
1721 subtest '_set_found_trigger() tests' => sub {
1725 $schema->storage->txn_begin;
1727 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1728 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1730 # Add a lost item debit
1731 my $debit = $patron->account->add_debit(
1735 item_id => $item->id,
1736 interface => 'intranet',
1740 # Add a lost item processing fee
1741 my $processing_debit = $patron->account->add_debit(
1744 type => 'PROCESSING',
1745 item_id => $item->id,
1746 interface => 'intranet',
1750 my $lostreturn_policy = {
1751 lostreturn => 'charge',
1752 processingreturn => 'refund'
1755 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1756 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1758 # simulate it was found
1759 $item->set( { itemlost => 0 } )->store;
1761 my $messages = $item->object_messages;
1763 my $message_1 = $messages->[0];
1765 is( $message_1->type, 'info', 'type is correct' );
1766 is( $message_1->message, 'lost_refunded', 'message is correct' );
1768 # Find the refund credit
1769 my $credit = $debit->credits->next;
1772 $message_1->payload,
1773 { credit_id => $credit->id },
1777 my $message_2 = $messages->[1];
1779 is( $message_2->type, 'info', 'type is correct' );
1780 is( $message_2->message, 'lost_charge', 'message is correct' );
1781 is( $message_2->payload, undef, 'no payload' );
1783 my $message_3 = $messages->[2];
1784 is( $message_3->message, 'processing_refunded', 'message is correct' );
1786 my $processing_credit = $processing_debit->credits->next;
1788 $message_3->payload,
1789 { credit_id => $processing_credit->id },
1793 # Let's build a new item
1794 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1795 $item->set( { itemlost => 0 } )->store;
1797 $messages = $item->object_messages;
1798 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1800 $schema->storage->txn_rollback;
1803 subtest 'holds_queue update tests' => sub {
1807 $schema->storage->txn_begin;
1809 my $biblio = $builder->build_sample_biblio;
1811 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1812 $mock->mock( 'enqueue', sub {
1813 my ( $self, $args ) = @_;
1815 $args->{biblio_ids},
1817 '->store triggers a holds queue update for the related biblio'
1821 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1824 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1827 $item->set({ reserves => 1 })->store;
1829 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1831 $item->set({ reserves => 0 })->store;
1833 $schema->storage->txn_rollback;
1837 subtest 'Recalls tests' => sub {
1841 $schema->storage->txn_begin;
1843 my $item1 = $builder->build_sample_item;
1844 my $biblio = $item1->biblio;
1845 my $branchcode = $item1->holdingbranch;
1846 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1847 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1848 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1849 my $item2 = $builder->build_object(
1850 { class => 'Koha::Items',
1851 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1855 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1856 t::lib::Mocks::mock_preference('UseRecalls', 1);
1858 my $recall1 = Koha::Recall->new(
1859 { patron_id => $patron1->borrowernumber,
1860 created_date => \'NOW()',
1861 biblio_id => $biblio->biblionumber,
1862 pickup_library_id => $branchcode,
1863 item_id => $item1->itemnumber,
1864 expiration_date => undef,
1868 my $recall2 = Koha::Recall->new(
1869 { patron_id => $patron2->borrowernumber,
1870 created_date => \'NOW()',
1871 biblio_id => $biblio->biblionumber,
1872 pickup_library_id => $branchcode,
1873 item_id => $item1->itemnumber,
1874 expiration_date => undef,
1879 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1881 $recall2->set_cancelled;
1883 t::lib::Mocks::mock_preference('UseRecalls', 0);
1884 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1886 t::lib::Mocks::mock_preference("UseRecalls", 1);
1888 $item1->update({ notforloan => 1 });
1889 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1890 $item1->update({ notforloan => 0, itemlost => 1 });
1891 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1892 $item1->update({ itemlost => 0, withdrawn => 1 });
1893 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1894 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1896 $item1->update({ withdrawn => 0 });
1897 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1899 Koha::CirculationRules->set_rules({
1900 branchcode => $branchcode,
1901 categorycode => $patron1->categorycode,
1902 itemtype => $item1->effective_itemtype,
1904 recalls_allowed => 0,
1905 recalls_per_record => 1,
1906 on_shelf_recalls => 'all',
1909 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1911 Koha::CirculationRules->set_rules({
1912 branchcode => $branchcode,
1913 categorycode => $patron1->categorycode,
1914 itemtype => $item1->effective_itemtype,
1916 recalls_allowed => 1,
1917 recalls_per_record => 1,
1918 on_shelf_recalls => 'all',
1921 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1922 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1923 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1925 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1926 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1927 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1929 $recall1->set_cancelled;
1930 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1932 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1934 Koha::CirculationRules->set_rules({
1935 branchcode => $branchcode,
1936 categorycode => $patron1->categorycode,
1937 itemtype => $item1->effective_itemtype,
1939 recalls_allowed => 1,
1940 recalls_per_record => 1,
1941 on_shelf_recalls => 'any',
1944 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1945 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1947 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1948 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1950 $recall1 = Koha::Recall->new(
1951 { patron_id => $patron1->borrowernumber,
1952 created_date => \'NOW()',
1953 biblio_id => $biblio->biblionumber,
1954 pickup_library_id => $branchcode,
1956 expiration_date => undef,
1961 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1963 Koha::CirculationRules->set_rules({
1964 branchcode => undef,
1965 categorycode => undef,
1966 itemtype => $item1->effective_itemtype,
1968 recalls_allowed => 0,
1969 recalls_per_record => 1,
1970 on_shelf_recalls => 'any',
1973 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1975 Koha::CirculationRules->set_rules({
1976 branchcode => undef,
1977 categorycode => undef,
1978 itemtype => $item1->effective_itemtype,
1980 recalls_allowed => 1,
1981 recalls_per_record => 1,
1982 on_shelf_recalls => 'any',
1985 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1987 # check_recalls tests
1989 $recall1 = Koha::Recall->new(
1990 { patron_id => $patron2->borrowernumber,
1991 created_date => \'NOW()',
1992 biblio_id => $biblio->biblionumber,
1993 pickup_library_id => $branchcode,
1994 item_id => $item1->itemnumber,
1995 expiration_date => undef,
1999 $recall2 = Koha::Recall->new(
2000 { patron_id => $patron1->borrowernumber,
2001 created_date => \'NOW()',
2002 biblio_id => $biblio->biblionumber,
2003 pickup_library_id => $branchcode,
2005 expiration_date => undef,
2009 $recall2->set_waiting( { item => $item1 } );
2010 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
2012 # return a waiting recall
2013 my $check_recall = $item1->check_recalls;
2014 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
2016 $recall2->revert_waiting;
2018 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
2020 # return recall based on recalldate
2021 $check_recall = $item1->check_recalls;
2022 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
2024 $recall1->set_cancelled;
2026 # return a biblio-level recall
2027 $check_recall = $item1->check_recalls;
2028 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
2030 $recall2->set_cancelled;
2032 $schema->storage->txn_rollback;
2035 subtest 'Notforloan tests' => sub {
2039 $schema->storage->txn_begin;
2041 my $item1 = $builder->build_sample_item;
2042 $item1->update({ notforloan => 0 });
2043 $item1->itemtype->notforloan(0);
2044 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2045 $item1->update({ notforloan => 1 });
2046 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2047 $item1->update({ notforloan => 0 });
2048 $item1->itemtype->update({ notforloan => 1 });
2049 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2051 $schema->storage->txn_rollback;
2054 subtest 'item_group() tests' => sub {
2058 $schema->storage->txn_begin;
2060 my $biblio = $builder->build_sample_biblio();
2061 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2062 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2064 is( $item_1->item_group, undef, 'Item 1 has no item group');
2065 is( $item_2->item_group, undef, 'Item 2 has no item group');
2067 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2068 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2070 $item_group_1->add_item({ item_id => $item_1->id });
2071 $item_group_2->add_item({ item_id => $item_2->id });
2073 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2074 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2076 $schema->storage->txn_rollback;
2079 subtest 'has_pending_recall() tests' => sub {
2083 $schema->storage->txn_begin;
2085 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2086 my $item = $builder->build_sample_item;
2087 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2089 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2090 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2092 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2094 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2096 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2098 $recall->status('waiting')->store;
2100 ok( $item->has_pending_recall, 'The item has a pending recall' );
2102 $schema->storage->txn_rollback;
2105 subtest 'is_denied_renewal' => sub {
2108 $schema->storage->txn_begin;
2110 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2112 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2113 homebranch => $library->branchcode,
2117 itemcallnumber => undef,
2122 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2123 homebranch => $library->branchcode,
2126 location => 'NOPROC'
2131 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2132 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2134 $idr_rules="withdrawn: [1]";
2135 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2136 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2137 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2139 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2140 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2141 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2143 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2144 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2145 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2146 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2148 $idr_rules="itemcallnumber: [NULL]";
2149 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2150 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when NULL in pref' );
2152 $idr_rules="itemcallnumber: ['']";
2153 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2154 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2156 $idr_rules="itemnotes: [NULL]";
2157 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2158 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when NULL in pref' );
2160 $idr_rules="itemnotes: ['']";
2161 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2162 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2164 $schema->storage->txn_rollback;