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 => 29;
27 use C4::Biblio qw( GetMarcSubfieldStructure );
28 use C4::Circulation qw( AddIssue AddReturn );
33 use Koha::DateUtils qw( dt_from_string );
36 use Koha::AuthorisedValues;
38 use List::MoreUtils qw(all);
40 use t::lib::TestBuilder;
44 my $schema = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new;
47 subtest 'return_claims relationship' => sub {
50 $schema->storage->txn_begin;
52 my $biblio = $builder->build_sample_biblio();
53 my $item = $builder->build_sample_item({
54 biblionumber => $biblio->biblionumber,
56 my $return_claims = $item->return_claims;
57 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
58 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
59 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
60 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
62 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
64 $schema->storage->txn_rollback;
67 subtest 'return_claim accessor' => sub {
70 $schema->storage->txn_begin;
72 my $biblio = $builder->build_sample_biblio();
73 my $item = $builder->build_sample_item({
74 biblionumber => $biblio->biblionumber,
76 my $return_claim = $item->return_claim;
77 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
79 my $claim1 = $builder->build_object(
81 class => 'Koha::Checkouts::ReturnClaims',
82 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
85 my $claim2 = $builder->build_object(
87 class => 'Koha::Checkouts::ReturnClaims',
88 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
92 $return_claim = $item->return_claim;
93 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
94 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
96 $claim2->resolution('test')->store();
97 $return_claim = $item->return_claim;
98 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
100 $claim1->resolution('test')->store();
101 $return_claim = $item->return_claim;
102 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
104 $schema->storage->txn_rollback;
107 subtest 'tracked_links relationship' => sub {
110 my $biblio = $builder->build_sample_biblio();
111 my $item = $builder->build_sample_item({
112 biblionumber => $biblio->biblionumber,
114 my $tracked_links = $item->tracked_links;
115 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
116 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
117 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
118 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
120 is($item->tracked_links()->count,2,"Two tracked links found");
123 subtest 'is_bundle tests' => sub {
126 $schema->storage->txn_begin;
128 my $item = $builder->build_sample_item();
130 my $is_bundle = $item->is_bundle;
131 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
133 my $item2 = $builder->build_sample_item();
134 $schema->resultset('ItemBundle')
135 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
137 $is_bundle = $item->is_bundle;
138 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
140 $schema->storage->txn_rollback;
143 subtest 'in_bundle tests' => sub {
146 $schema->storage->txn_begin;
148 my $item = $builder->build_sample_item();
150 my $in_bundle = $item->in_bundle;
151 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
153 my $host_item = $builder->build_sample_item();
154 $schema->resultset('ItemBundle')
155 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
157 $in_bundle = $item->in_bundle;
158 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
160 $schema->storage->txn_rollback;
163 subtest 'bundle_items tests' => sub {
166 $schema->storage->txn_begin;
168 my $host_item = $builder->build_sample_item();
169 my $bundle_items = $host_item->bundle_items;
170 is( ref($bundle_items), 'Koha::Items',
171 'bundle_items returns a Koha::Items object set' );
172 is( $bundle_items->count, 0,
173 'bundle_items set is empty when no items are bundled' );
175 my $bundle_item1 = $builder->build_sample_item();
176 my $bundle_item2 = $builder->build_sample_item();
177 my $bundle_item3 = $builder->build_sample_item();
178 $schema->resultset('ItemBundle')
180 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
181 $schema->resultset('ItemBundle')
183 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
184 $schema->resultset('ItemBundle')
186 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
188 $bundle_items = $host_item->bundle_items;
189 is( $bundle_items->count, 3,
190 'bundle_items returns all the bundled items in the set' );
192 $schema->storage->txn_rollback;
195 subtest 'bundle_host tests' => sub {
198 $schema->storage->txn_begin;
200 my $host_item = $builder->build_sample_item();
201 my $bundle_item1 = $builder->build_sample_item();
202 my $bundle_item2 = $builder->build_sample_item();
203 $schema->resultset('ItemBundle')
205 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
207 my $bundle_host = $bundle_item1->bundle_host;
208 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
209 $bundle_host = $bundle_item2->bundle_host;
210 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
211 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
213 $schema->storage->txn_rollback;
216 subtest 'add_to_bundle tests' => sub {
219 $schema->storage->txn_begin;
221 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
223 my $library = $builder->build_object({ class => 'Koha::Libraries' });
224 t::lib::Mocks::mock_userenv({
225 branchcode => $library->branchcode
228 my $host_item = $builder->build_sample_item();
229 my $bundle_item1 = $builder->build_sample_item();
230 my $bundle_item2 = $builder->build_sample_item();
231 my $bundle_item3 = $builder->build_sample_item();
233 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
235 throws_ok { $host_item->add_to_bundle($host_item) }
236 'Koha::Exceptions::Item::Bundle::IsBundle',
237 'Exception thrown if you try to add the item to itself';
239 my $reserve_id = C4::Reserves::AddReserve(
241 branchcode => $library->branchcode,
242 borrowernumber => $patron->borrowernumber,
243 biblionumber => $bundle_item3->biblionumber,
244 itemnumber => $bundle_item3->itemnumber,
247 throws_ok { $host_item->add_to_bundle($bundle_item3) }
248 'Koha::Exceptions::Item::Bundle::ItemHasHolds',
249 'Exception thrown if you try to add an item with holds to a bundle';
251 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
252 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
254 throws_ok { $host_item->add_to_bundle($bundle_item1) }
255 'Koha::Exceptions::Object::DuplicateID',
256 'Exception thrown if you try to add the same item twice';
258 throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
259 'Koha::Exceptions::Item::Bundle::IsBundle',
260 'Exception thrown if you try to add an item to a bundled item';
262 throws_ok { $bundle_item2->add_to_bundle($host_item) }
263 'Koha::Exceptions::Item::Bundle::IsBundle',
264 'Exception thrown if you try to add a bundle host to a bundle item';
266 C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
267 throws_ok { $host_item->add_to_bundle($bundle_item2) }
268 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
269 'Exception thrown if you try to add a checked out item';
271 $bundle_item2->withdrawn(1)->store;
272 t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
273 throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
274 'Koha::Exceptions::Checkin::FailedCheckin',
275 'Exception thrown if you try to add a checked out item using
276 "force_checkin" and the return is not possible';
278 $bundle_item2->withdrawn(0)->store;
279 lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
280 'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
282 $bundle_item2->discard_changes;
283 ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
285 $schema->storage->txn_rollback;
288 subtest 'remove_from_bundle tests' => sub {
291 $schema->storage->txn_begin;
293 my $host_item = $builder->build_sample_item();
294 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
295 $schema->resultset('ItemBundle')
297 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
299 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
300 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
301 $bundle_item1 = $bundle_item1->get_from_storage;
302 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
304 $schema->storage->txn_rollback;
307 subtest 'hidden_in_opac() tests' => sub {
311 $schema->storage->txn_begin;
313 my $item = $builder->build_sample_item({ itemlost => 2 });
316 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
317 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
319 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
320 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
322 # enable hidelostitems to verify correct behaviour
323 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
324 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
326 # disable hidelostitems
327 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
328 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
330 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
332 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
336 $schema->storage->txn_rollback;
339 subtest 'has_pending_hold() tests' => sub {
343 $schema->storage->txn_begin;
345 my $dbh = C4::Context->dbh;
346 my $item = $builder->build_sample_item({ itemlost => 0 });
347 my $itemnumber = $item->itemnumber;
349 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
350 ok( $item->has_pending_hold, "Yes, we have a pending hold");
351 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
352 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
354 $schema->storage->txn_rollback;
357 subtest "as_marc_field() tests" => sub {
359 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
360 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
362 my @schema_columns = $schema->resultset('Item')->result_source->columns;
363 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
365 plan tests => scalar @mapped_columns + 5;
367 $schema->storage->txn_begin;
369 my $item = $builder->build_sample_item;
370 # Make sure it has at least one undefined attribute
371 $item->set({ replacementprice => undef })->store->discard_changes;
373 my $marc_field = $item->as_marc_field;
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 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
390 tagfield => $itemtag,
394 Koha::MarcSubfieldStructure->new(
397 tagfield => $itemtag,
403 my @unlinked_subfields;
404 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
405 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
407 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
408 Koha::MarcSubfieldStructures->search(
409 { frameworkcode => '', tagfield => $itemtag } )
410 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
412 $marc_field = $item->as_marc_field;
414 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
415 my @subfields = $marc_field->subfields;
416 my $result = all { defined $_->[1] } @subfields;
417 ok( $result, 'There are no undef subfields' );
418 my @ordered_subfields = sort {
419 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
420 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
422 is_deeply(\@subfields, \@ordered_subfields);
424 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
425 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
427 $schema->storage->txn_rollback;
428 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
431 subtest 'pickup_locations() tests' => sub {
435 $schema->storage->txn_begin;
437 my $dbh = C4::Context->dbh;
439 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
440 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
441 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
442 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
443 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
444 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
445 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
446 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
448 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
449 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
452 $library1->branchcode, $library2->branchcode,
453 $library3->branchcode, $library4->branchcode
456 my $item1 = $builder->build_sample_item(
458 homebranch => $library1->branchcode,
459 holdingbranch => $library2->branchcode,
465 my $item3 = $builder->build_sample_item(
467 homebranch => $library3->branchcode,
468 holdingbranch => $library4->branchcode,
470 itype => $item1->itype,
474 Koha::CirculationRules->set_rules(
476 categorycode => undef,
477 itemtype => $item1->itype,
480 reservesallowed => 25,
486 { $item1->pickup_locations }
487 'Koha::Exceptions::MissingParameter',
488 'Exception thrown on missing parameter';
490 is( $@->parameter, 'patron', 'Exception param correctly set' );
492 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
493 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
496 "1-1-from_home_library-any" => 3,
497 "1-1-from_home_library-holdgroup" => 2,
498 "1-1-from_home_library-patrongroup" => 2,
499 "1-1-from_home_library-homebranch" => 1,
500 "1-1-from_home_library-holdingbranch" => 1,
501 "1-1-from_any_library-any" => 3,
502 "1-1-from_any_library-holdgroup" => 2,
503 "1-1-from_any_library-patrongroup" => 2,
504 "1-1-from_any_library-homebranch" => 1,
505 "1-1-from_any_library-holdingbranch" => 1,
506 "1-1-from_local_hold_group-any" => 3,
507 "1-1-from_local_hold_group-holdgroup" => 2,
508 "1-1-from_local_hold_group-patrongroup" => 2,
509 "1-1-from_local_hold_group-homebranch" => 1,
510 "1-1-from_local_hold_group-holdingbranch" => 1,
511 "1-4-from_home_library-any" => 0,
512 "1-4-from_home_library-holdgroup" => 0,
513 "1-4-from_home_library-patrongroup" => 0,
514 "1-4-from_home_library-homebranch" => 0,
515 "1-4-from_home_library-holdingbranch" => 0,
516 "1-4-from_any_library-any" => 3,
517 "1-4-from_any_library-holdgroup" => 2,
518 "1-4-from_any_library-patrongroup" => 1,
519 "1-4-from_any_library-homebranch" => 1,
520 "1-4-from_any_library-holdingbranch" => 1,
521 "1-4-from_local_hold_group-any" => 0,
522 "1-4-from_local_hold_group-holdgroup" => 0,
523 "1-4-from_local_hold_group-patrongroup" => 0,
524 "1-4-from_local_hold_group-homebranch" => 0,
525 "1-4-from_local_hold_group-holdingbranch" => 0,
526 "3-1-from_home_library-any" => 0,
527 "3-1-from_home_library-holdgroup" => 0,
528 "3-1-from_home_library-patrongroup" => 0,
529 "3-1-from_home_library-homebranch" => 0,
530 "3-1-from_home_library-holdingbranch" => 0,
531 "3-1-from_any_library-any" => 3,
532 "3-1-from_any_library-holdgroup" => 1,
533 "3-1-from_any_library-patrongroup" => 2,
534 "3-1-from_any_library-homebranch" => 0,
535 "3-1-from_any_library-holdingbranch" => 1,
536 "3-1-from_local_hold_group-any" => 0,
537 "3-1-from_local_hold_group-holdgroup" => 0,
538 "3-1-from_local_hold_group-patrongroup" => 0,
539 "3-1-from_local_hold_group-homebranch" => 0,
540 "3-1-from_local_hold_group-holdingbranch" => 0,
541 "3-4-from_home_library-any" => 0,
542 "3-4-from_home_library-holdgroup" => 0,
543 "3-4-from_home_library-patrongroup" => 0,
544 "3-4-from_home_library-homebranch" => 0,
545 "3-4-from_home_library-holdingbranch" => 0,
546 "3-4-from_any_library-any" => 3,
547 "3-4-from_any_library-holdgroup" => 1,
548 "3-4-from_any_library-patrongroup" => 1,
549 "3-4-from_any_library-homebranch" => 0,
550 "3-4-from_any_library-holdingbranch" => 1,
551 "3-4-from_local_hold_group-any" => 3,
552 "3-4-from_local_hold_group-holdgroup" => 1,
553 "3-4-from_local_hold_group-patrongroup" => 1,
554 "3-4-from_local_hold_group-homebranch" => 0,
555 "3-4-from_local_hold_group-holdingbranch" => 1
559 my ( $item, $patron, $ha, $hfp, $results ) = @_;
561 Koha::CirculationRules->set_rules(
567 hold_fulfillment_policy => $hfp,
568 returnbranch => 'any'
573 $ha eq 'from_local_hold_group' ? 'holdgroup'
575 $ha eq 'from_any_library' ? 'any'
580 my $pickup_location = $_;
581 grep { $pickup_location->branchcode eq $_ } @branchcodes
582 } $item->pickup_locations( { patron => $patron } )->as_list;
585 scalar(@pl) eq $results->{
586 $item->copynumber . '-'
587 . $patron->firstname . '-'
597 . ', hold_fulfillment_policy: '
601 $item->copynumber . '-'
602 . $patron->firstname . '-'
613 foreach my $item ($item1, $item3) {
614 foreach my $patron ($patron1, $patron4) {
615 #holdallowed 1: homebranch, 2: any, 3: holdgroup
616 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
617 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
618 _doTest($item, $patron, $ha, $hfp, $results);
624 # Now test that branchtransferlimits will further filter the pickup locations
626 my $item_no_ccode = $builder->build_sample_item(
628 homebranch => $library1->branchcode,
629 holdingbranch => $library2->branchcode,
630 itype => $item1->itype,
634 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
635 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
636 Koha::CirculationRules->set_rules(
639 itemtype => $item1->itype,
641 holdallowed => 'from_home_library',
642 hold_fulfillment_policy => 1,
643 returnbranch => 'any'
647 $builder->build_object(
649 class => 'Koha::Item::Transfer::Limits',
651 toBranch => $library1->branchcode,
652 fromBranch => $library2->branchcode,
653 itemtype => $item1->itype,
659 my @pickup_locations = map {
660 my $pickup_location = $_;
661 grep { $pickup_location->branchcode eq $_ } @branchcodes
662 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
664 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
666 $builder->build_object(
668 class => 'Koha::Item::Transfer::Limits',
670 toBranch => $library4->branchcode,
671 fromBranch => $library2->branchcode,
672 itemtype => $item1->itype,
678 @pickup_locations = map {
679 my $pickup_location = $_;
680 grep { $pickup_location->branchcode eq $_ } @branchcodes
681 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
683 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
685 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
686 @pickup_locations = map {
687 my $pickup_location = $_;
688 grep { $pickup_location->branchcode eq $_ } @branchcodes
689 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
690 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
692 @pickup_locations = map {
693 my $pickup_location = $_;
694 grep { $pickup_location->branchcode eq $_ } @branchcodes
695 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
696 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");
698 $builder->build_object(
700 class => 'Koha::Item::Transfer::Limits',
702 toBranch => $library2->branchcode,
703 fromBranch => $library2->branchcode,
705 ccode => $item1->ccode,
710 @pickup_locations = map {
711 my $pickup_location = $_;
712 grep { $pickup_location->branchcode eq $_ } @branchcodes
713 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
714 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
716 $builder->build_object(
718 class => 'Koha::Item::Transfer::Limits',
720 toBranch => $library4->branchcode,
721 fromBranch => $library2->branchcode,
723 ccode => $item1->ccode,
728 @pickup_locations = map {
729 my $pickup_location = $_;
730 grep { $pickup_location->branchcode eq $_ } @branchcodes
731 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
732 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
734 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
736 $schema->storage->txn_rollback;
739 subtest 'request_transfer' => sub {
741 $schema->storage->txn_begin;
743 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
744 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
745 my $item = $builder->build_sample_item(
747 homebranch => $library1->branchcode,
748 holdingbranch => $library2->branchcode,
752 # Mandatory fields tests
753 throws_ok { $item->request_transfer( { to => $library1 } ) }
754 'Koha::Exceptions::MissingParameter',
755 'Exception thrown if `reason` parameter is missing';
757 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
758 'Koha::Exceptions::MissingParameter',
759 'Exception thrown if `to` parameter is missing';
762 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
763 is( ref($transfer), 'Koha::Item::Transfer',
764 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
766 my $original_transfer = $transfer->get_from_storage;
768 # Transfer already in progress
769 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
770 'Koha::Exceptions::Item::Transfer::InQueue',
771 'Exception thrown if transfer is already in progress';
774 is( ref( $exception->transfer ),
775 'Koha::Item::Transfer',
776 'The exception contains the found Koha::Item::Transfer' );
779 my $queued_transfer = $item->request_transfer(
780 { to => $library2, reason => 'Manual', enqueue => 1 } );
781 is( ref($queued_transfer), 'Koha::Item::Transfer',
782 'Koha::Item->request_transfer allowed when enqueue is set' );
783 my $transfers = $item->get_transfers;
784 is($transfers->count, 2, "There are now 2 live transfers in the queue");
785 $transfer = $transfer->get_from_storage;
786 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
787 $queued_transfer->datearrived(dt_from_string)->store();
790 my $replaced_transfer = $item->request_transfer(
791 { to => $library2, reason => 'Manual', replace => 1 } );
792 is( ref($replaced_transfer), 'Koha::Item::Transfer',
793 'Koha::Item->request_transfer allowed when replace is set' );
794 $original_transfer->discard_changes;
795 ok($original_transfer->datecancelled, "Original transfer cancelled");
796 $transfers = $item->get_transfers;
797 is($transfers->count, 1, "There is only 1 live transfer in the queue");
798 $replaced_transfer->datearrived(dt_from_string)->store();
800 # BranchTransferLimits
801 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
802 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
803 my $limit = Koha::Item::Transfer::Limit->new({
804 fromBranch => $library2->branchcode,
805 toBranch => $library1->branchcode,
806 itemtype => $item->effective_itemtype,
809 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
810 'Koha::Exceptions::Item::Transfer::Limit',
811 'Exception thrown if transfer is prevented by limits';
813 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
814 is( ref($forced_transfer), 'Koha::Item::Transfer',
815 'Koha::Item->request_transfer allowed when ignore_limits is set'
818 $schema->storage->txn_rollback;
821 subtest 'deletion' => sub {
824 $schema->storage->txn_begin;
826 my $biblio = $builder->build_sample_biblio();
828 my $item = $builder->build_sample_item(
830 biblionumber => $biblio->biblionumber,
833 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
835 my $deleted_item = $item->move_to_deleted;
836 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
837 ; # FIXME This should be Koha::Deleted::Item
838 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
840 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
841 $item = $builder->build_sample_item(
843 biblionumber => $biblio->biblionumber,
847 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
850 my $library = $builder->build_object({ class => 'Koha::Libraries' });
851 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
852 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
854 my $patron = $builder->build_object({class => 'Koha::Patrons'});
855 $item = $builder->build_sample_item({ library => $library->branchcode });
858 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
861 @{$item->safe_to_delete->messages}[0]->message,
863 'Koha::Item->safe_to_delete reports item on loan',
867 @{$item->safe_to_delete->messages}[0]->message,
869 'item that is on loan cannot be deleted',
873 ! $item->safe_to_delete,
874 'Koha::Item->safe_to_delete shows item NOT safe to delete'
877 AddReturn( $item->barcode, $library->branchcode );
880 t::lib::Mocks::mock_preference('IndependentBranches', 1);
881 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
884 @{$item_2->safe_to_delete->messages}[0]->message,
886 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
890 @{$item_2->safe_to_delete->messages}[0]->message,
892 'IndependentBranches prevents deletion at another branch',
897 { # codeblock to limit scope of $module->mock
899 my $module = Test::MockModule->new('C4::Items');
900 $module->mock( GetAnalyticsCount => sub { return 1 } );
902 $item->discard_changes;
904 @{$item->safe_to_delete->messages}[0]->message,
906 'Koha::Item->safe_to_delete reports linked analytics',
910 @{$item->safe_to_delete->messages}[0]->message,
912 'Linked analytics prevents deletion of item',
918 $item->safe_to_delete,
919 'Koha::Item->safe_to_delete shows item safe to delete'
924 my $test_item = Koha::Items->find( $item->itemnumber );
926 is( $test_item, undef,
927 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
930 subtest 'holds tests' => sub {
935 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
937 $schema->storage->txn_begin;
939 my $item = $builder->build_sample_item;
941 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
942 my $safe_to_delete = $item->safe_to_delete;
944 ok( !$safe_to_delete, 'Cannot delete' );
946 @{ $safe_to_delete->messages }[0]->message,
948 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
953 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
954 $safe_to_delete = $item->safe_to_delete;
956 ok( !$safe_to_delete, 'Cannot delete' );
958 @{ $safe_to_delete->messages }[0]->message,
960 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
965 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
966 $safe_to_delete = $item->safe_to_delete;
968 ok( !$safe_to_delete, 'Cannot delete' );
970 @{ $safe_to_delete->messages }[0]->message,
972 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
977 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
978 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
980 $safe_to_delete = $item->safe_to_delete;
982 ok( !$safe_to_delete );
985 @{ $safe_to_delete->messages}[0]->message,
986 'last_item_for_hold',
987 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
990 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
992 ok( $item->safe_to_delete );
994 $schema->storage->txn_rollback;
997 $schema->storage->txn_rollback;
1000 subtest 'renewal_branchcode' => sub {
1003 $schema->storage->txn_begin;
1005 my $item = $builder->build_sample_item();
1006 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1007 my $checkout = $builder->build_object({
1008 class => 'Koha::Checkouts',
1010 itemnumber => $item->itemnumber,
1015 C4::Context->interface( 'intranet' );
1016 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1018 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1019 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1020 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1021 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1023 C4::Context->interface( 'opac' );
1025 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1026 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1027 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1029 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1030 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1031 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1033 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1034 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1035 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");
1037 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1038 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1039 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");
1041 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1042 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1043 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");
1045 $schema->storage->txn_rollback;
1048 subtest 'Tests for itemtype' => sub {
1050 $schema->storage->txn_begin;
1052 my $biblio = $builder->build_sample_biblio;
1053 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1054 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1056 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1057 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1058 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1059 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1061 $schema->storage->txn_rollback;
1064 subtest 'get_transfers' => sub {
1066 $schema->storage->txn_begin;
1068 my $item = $builder->build_sample_item();
1070 my $transfers = $item->get_transfers();
1071 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1072 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1074 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1076 my $transfer_1 = $builder->build_object(
1078 class => 'Koha::Item::Transfers',
1080 itemnumber => $item->itemnumber,
1081 frombranch => $item->holdingbranch,
1082 tobranch => $library_to->branchcode,
1085 datearrived => undef,
1086 datecancelled => undef,
1087 daterequested => \'NOW()'
1092 $transfers = $item->get_transfers();
1093 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1095 my $transfer_2 = $builder->build_object(
1097 class => 'Koha::Item::Transfers',
1099 itemnumber => $item->itemnumber,
1100 frombranch => $item->holdingbranch,
1101 tobranch => $library_to->branchcode,
1104 datearrived => undef,
1105 datecancelled => undef,
1106 daterequested => \'NOW()'
1111 my $transfer_3 = $builder->build_object(
1113 class => 'Koha::Item::Transfers',
1115 itemnumber => $item->itemnumber,
1116 frombranch => $item->holdingbranch,
1117 tobranch => $library_to->branchcode,
1120 datearrived => undef,
1121 datecancelled => undef,
1122 daterequested => \'NOW()'
1127 $transfers = $item->get_transfers();
1128 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1129 my $result_1 = $transfers->next;
1130 my $result_2 = $transfers->next;
1131 my $result_3 = $transfers->next;
1132 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1133 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1134 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1136 $transfer_2->datesent(\'NOW()')->store;
1137 $transfers = $item->get_transfers();
1138 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1139 $result_1 = $transfers->next;
1140 $result_2 = $transfers->next;
1141 $result_3 = $transfers->next;
1142 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1143 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1144 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1146 $transfer_2->datearrived(\'NOW()')->store;
1147 $transfers = $item->get_transfers();
1148 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1149 $result_1 = $transfers->next;
1150 $result_2 = $transfers->next;
1151 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1152 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1154 $transfer_1->datecancelled(\'NOW()')->store;
1155 $transfers = $item->get_transfers();
1156 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1157 $result_1 = $transfers->next;
1158 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1160 $schema->storage->txn_rollback;
1163 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1166 $schema->storage->txn_begin;
1168 my $biblio = $builder->build_sample_biblio();
1169 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1171 my $orders = $item->orders;
1172 is ($orders->count, 0, 'No order on this item yet');
1174 my $order_note = 'Order for ' . $item->itemnumber;
1176 my $aq_order1 = $builder->build_object({
1177 class => 'Koha::Acquisition::Orders',
1179 biblionumber => $biblio->biblionumber,
1180 order_internalnote => $order_note,
1183 my $aq_order2 = $builder->build_object({
1184 class => 'Koha::Acquisition::Orders',
1186 biblionumber => $biblio->biblionumber,
1189 my $aq_order_item1 = $builder->build({
1190 source => 'AqordersItem',
1192 ordernumber => $aq_order1->ordernumber,
1193 itemnumber => $item->itemnumber,
1197 $orders = $item->orders;
1198 is ($orders->count, 1, 'One order found by item with the relationship');
1199 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1202 subtest 'move_to_biblio() tests' => sub {
1205 $schema->storage->txn_begin;
1207 my $dbh = C4::Context->dbh;
1209 my $source_biblio = $builder->build_sample_biblio();
1210 my $target_biblio = $builder->build_sample_biblio();
1212 my $source_biblionumber = $source_biblio->biblionumber;
1213 my $target_biblionumber = $target_biblio->biblionumber;
1215 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1216 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1217 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1219 my $itemnumber1 = $item1->itemnumber;
1220 my $itemnumber2 = $item2->itemnumber;
1222 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1224 my $patron = $builder->build_object({
1225 class => 'Koha::Patrons',
1226 value => { branchcode => $library->branchcode }
1228 my $borrowernumber = $patron->borrowernumber;
1230 my $aq_budget = $builder->build({
1231 source => 'Aqbudget',
1233 budget_notes => 'test',
1237 my $aq_order1 = $builder->build_object({
1238 class => 'Koha::Acquisition::Orders',
1240 biblionumber => $source_biblionumber,
1241 budget_id => $aq_budget->{budget_id},
1244 my $aq_order_item1 = $builder->build({
1245 source => 'AqordersItem',
1247 ordernumber => $aq_order1->ordernumber,
1248 itemnumber => $itemnumber1,
1251 my $aq_order2 = $builder->build_object({
1252 class => 'Koha::Acquisition::Orders',
1254 biblionumber => $source_biblionumber,
1255 budget_id => $aq_budget->{budget_id},
1258 my $aq_order_item2 = $builder->build({
1259 source => 'AqordersItem',
1261 ordernumber => $aq_order2->ordernumber,
1262 itemnumber => $itemnumber2,
1266 my $bib_level_hold = $builder->build_object({
1267 class => 'Koha::Holds',
1269 biblionumber => $source_biblionumber,
1270 itemnumber => undef,
1273 my $item_level_hold1 = $builder->build_object({
1274 class => 'Koha::Holds',
1276 biblionumber => $source_biblionumber,
1277 itemnumber => $itemnumber1,
1280 my $item_level_hold2 = $builder->build_object({
1281 class => 'Koha::Holds',
1283 biblionumber => $source_biblionumber,
1284 itemnumber => $itemnumber2,
1288 my $tmp_holdsqueue1 = $builder->build({
1289 source => 'TmpHoldsqueue',
1291 borrowernumber => $borrowernumber,
1292 biblionumber => $source_biblionumber,
1293 itemnumber => $itemnumber1,
1296 my $tmp_holdsqueue2 = $builder->build({
1297 source => 'TmpHoldsqueue',
1299 borrowernumber => $borrowernumber,
1300 biblionumber => $source_biblionumber,
1301 itemnumber => $itemnumber2,
1304 my $hold_fill_target1 = $builder->build({
1305 source => 'HoldFillTarget',
1307 borrowernumber => $borrowernumber,
1308 biblionumber => $source_biblionumber,
1309 itemnumber => $itemnumber1,
1312 my $hold_fill_target2 = $builder->build({
1313 source => 'HoldFillTarget',
1315 borrowernumber => $borrowernumber,
1316 biblionumber => $source_biblionumber,
1317 itemnumber => $itemnumber2,
1320 my $linktracker1 = $builder->build({
1321 source => 'Linktracker',
1323 borrowernumber => $borrowernumber,
1324 biblionumber => $source_biblionumber,
1325 itemnumber => $itemnumber1,
1328 my $linktracker2 = $builder->build({
1329 source => 'Linktracker',
1331 borrowernumber => $borrowernumber,
1332 biblionumber => $source_biblionumber,
1333 itemnumber => $itemnumber2,
1337 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1338 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1340 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1341 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');
1343 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1344 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1345 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1346 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1347 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1348 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1350 $aq_order1->discard_changes;
1351 $aq_order2->discard_changes;
1352 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1353 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1355 $bib_level_hold->discard_changes;
1356 $item_level_hold1->discard_changes;
1357 $item_level_hold2->discard_changes;
1358 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1359 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1360 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1362 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1363 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1364 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1365 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1367 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1368 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1369 # Why does ->biblionumber return a Biblio object???
1370 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1371 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1373 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1374 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1375 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1376 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1378 $schema->storage->txn_rollback;
1381 subtest 'columns_to_str' => sub {
1384 $schema->storage->txn_begin;
1386 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1388 my $cache = Koha::Caches->get_instance();
1389 $cache->clear_from_cache("MarcStructure-0-");
1390 $cache->clear_from_cache("MarcStructure-1-");
1391 $cache->clear_from_cache("MarcSubfieldStructure-");
1392 $cache->clear_from_cache("libraries:name");
1393 $cache->clear_from_cache("itemtype:description:en");
1394 $cache->clear_from_cache("cn_sources:description");
1395 $cache->clear_from_cache("AV_descriptions:LOST");
1397 # Creating subfields 'é', 'è' that are not linked with a kohafield
1398 Koha::MarcSubfieldStructures->search(
1400 frameworkcode => '',
1401 tagfield => $itemtag,
1402 tagsubfield => ['é', 'è'],
1404 )->delete; # In case it exist already
1406 # é is not linked with a AV
1407 # è is linked with AV branches
1408 Koha::MarcSubfieldStructure->new(
1410 frameworkcode => '',
1411 tagfield => $itemtag,
1412 tagsubfield => 'é',
1415 defaultvalue => 'ééé',
1419 Koha::MarcSubfieldStructure->new(
1421 frameworkcode => '',
1422 tagfield => $itemtag,
1423 tagsubfield => 'è',
1426 defaultvalue => 'èèè',
1428 authorised_value => 'branches',
1432 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1433 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1434 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1435 my $dateaccessioned = '2020-12-15';
1436 my $library = Koha::Libraries->search->next;
1437 my $branchcode = $library->branchcode;
1439 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1441 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1442 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1443 xmlns="http://www.loc.gov/MARC21/slim">
1446 <leader> a </leader>
1447 <datafield tag="999" ind1=" " ind2=" ">
1448 <subfield code="é">value é</subfield>
1449 <subfield code="è">$branchcode</subfield>
1457 itemlost => $lost_av->authorised_value,
1458 dateaccessioned => $dateaccessioned,
1459 more_subfields_xml => $some_marc_xml,
1463 Koha::Caches->get_instance->flush_all;
1465 $item = $item->get_from_storage;
1467 my $s = $item->columns_to_str;
1468 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1469 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1470 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1471 is( $s->{'è'}, $library->branchname );
1473 $cache->clear_from_cache("MarcStructure-0-");
1474 $cache->clear_from_cache("MarcStructure-1-");
1475 $cache->clear_from_cache("MarcSubfieldStructure-");
1476 $cache->clear_from_cache("libraries:name");
1477 $cache->clear_from_cache("itemtype:description:en");
1478 $cache->clear_from_cache("cn_sources:description");
1479 $cache->clear_from_cache("AV_descriptions:LOST");
1481 $schema->storage->txn_rollback;
1484 subtest 'strings_map() tests' => sub {
1488 $schema->storage->txn_begin;
1490 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1492 my $cache = Koha::Caches->get_instance();
1493 $cache->clear_from_cache("MarcStructure-0-");
1494 $cache->clear_from_cache("MarcStructure-1-");
1495 $cache->clear_from_cache("MarcSubfieldStructure-");
1496 $cache->clear_from_cache("libraries:name");
1497 $cache->clear_from_cache("itemtype:description:en");
1498 $cache->clear_from_cache("cn_sources:description");
1499 $cache->clear_from_cache("AV_descriptions:LOST");
1501 # Recreating subfields just to be sure tests will be ok
1506 Koha::MarcSubfieldStructures->search(
1508 frameworkcode => '',
1509 tagfield => $itemtag,
1510 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1512 )->delete; # In case it exist already
1514 Koha::MarcSubfieldStructure->new(
1516 authorised_value => 'LOST',
1518 frameworkcode => '',
1519 kohafield => 'items.itemlost',
1522 tagfield => $itemtag,
1526 Koha::MarcSubfieldStructure->new(
1528 authorised_value => 'cn_source',
1530 frameworkcode => '',
1531 kohafield => 'items.cn_source',
1534 tagfield => $itemtag,
1538 Koha::MarcSubfieldStructure->new(
1540 authorised_value => '',
1542 frameworkcode => '',
1543 kohafield => 'items.materials',
1546 tagfield => $itemtag,
1550 Koha::MarcSubfieldStructure->new(
1552 authorised_value => 'branches',
1554 frameworkcode => '',
1555 kohafield => 'items.homebranch',
1558 tagfield => $itemtag,
1562 Koha::MarcSubfieldStructure->new(
1564 authorised_value => 'itemtypes',
1566 frameworkcode => '',
1567 kohafield => 'items.itype',
1570 tagfield => $itemtag,
1575 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1576 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1577 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1578 my $item = $builder->build_sample_item(
1580 biblionumber => $biblio->id,
1581 library => $library->id
1585 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1586 my $lost_av = $builder->build_object(
1588 class => 'Koha::AuthorisedValues',
1590 authorised_value => 3,
1592 lib => 'internal description',
1593 lib_opac => 'public description',
1598 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1599 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1600 my $class_source = $builder->build_object(
1602 class => 'Koha::ClassSources',
1604 class_sort_rule => $class_sort_rule->class_sort_rule,
1605 class_split_rule => $class_split_rule->class_split_rule,
1610 Koha::Caches->get_instance->flush_all;
1614 cn_source => $class_source->id,
1615 itemlost => $lost_av->authorised_value,
1616 itype => $itype->itemtype,
1617 materials => 'Suff',
1619 )->store->discard_changes;
1621 my $strings = $item->strings_map;
1623 subtest 'unmapped field tests' => sub {
1627 ok( !exists $strings->{materials}, "Unmapped field not present" );
1630 subtest 'av handling' => sub {
1634 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1635 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1636 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1637 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1640 subtest 'cn_source handling' => sub {
1644 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1645 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1646 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1649 subtest 'branches handling' => sub {
1653 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1654 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1655 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1658 subtest 'itemtype handling' => sub {
1662 ok( exists $strings->{itype}, "'itype' entry exists" );
1663 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1664 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1667 subtest 'public flag tests' => sub {
1671 $strings = $item->strings_map( { public => 1 } );
1673 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1674 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1675 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1676 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1679 $cache->clear_from_cache("MarcStructure-0-");
1680 $cache->clear_from_cache("MarcStructure-1-");
1681 $cache->clear_from_cache("MarcSubfieldStructure-");
1682 $cache->clear_from_cache("libraries:name");
1683 $cache->clear_from_cache("itemtype:description:en");
1684 $cache->clear_from_cache("cn_sources:description");
1686 $schema->storage->txn_rollback;
1689 subtest 'store() tests' => sub {
1693 subtest 'dateaccessioned handling' => sub {
1697 $schema->storage->txn_begin;
1699 my $item = $builder->build_sample_item;
1701 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1703 # reset dateaccessioned on the DB
1704 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1705 $item->discard_changes;
1707 ok( !defined $item->dateaccessioned );
1710 $item->replacementprice(100)->store->discard_changes;
1712 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1714 $schema->storage->txn_rollback;
1717 subtest '_set_found_trigger() tests' => sub {
1721 $schema->storage->txn_begin;
1723 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1724 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1726 # Add a lost item debit
1727 my $debit = $patron->account->add_debit(
1731 item_id => $item->id,
1732 interface => 'intranet',
1736 # Add a lost item processing fee
1737 my $processing_debit = $patron->account->add_debit(
1740 type => 'PROCESSING',
1741 item_id => $item->id,
1742 interface => 'intranet',
1746 my $lostreturn_policy = {
1747 lostreturn => 'charge',
1748 processingreturn => 'refund'
1751 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1752 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1754 # simulate it was found
1755 $item->set( { itemlost => 0 } )->store;
1757 my $messages = $item->object_messages;
1759 my $message_1 = $messages->[0];
1761 is( $message_1->type, 'info', 'type is correct' );
1762 is( $message_1->message, 'lost_refunded', 'message is correct' );
1764 # Find the refund credit
1765 my $credit = $debit->credits->next;
1768 $message_1->payload,
1769 { credit_id => $credit->id },
1773 my $message_2 = $messages->[1];
1775 is( $message_2->type, 'info', 'type is correct' );
1776 is( $message_2->message, 'lost_charge', 'message is correct' );
1777 is( $message_2->payload, undef, 'no payload' );
1779 my $message_3 = $messages->[2];
1780 is( $message_3->message, 'processing_refunded', 'message is correct' );
1782 my $processing_credit = $processing_debit->credits->next;
1784 $message_3->payload,
1785 { credit_id => $processing_credit->id },
1789 # Let's build a new item
1790 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1791 $item->set( { itemlost => 0 } )->store;
1793 $messages = $item->object_messages;
1794 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1796 $schema->storage->txn_rollback;
1799 subtest 'holds_queue update tests' => sub {
1803 $schema->storage->txn_begin;
1805 my $biblio = $builder->build_sample_biblio;
1807 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1808 $mock->mock( 'enqueue', sub {
1809 my ( $self, $args ) = @_;
1811 $args->{biblio_ids},
1813 '->store triggers a holds queue update for the related biblio'
1817 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1820 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1823 $item->set({ reserves => 1 })->store;
1825 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1827 $item->set({ reserves => 0 })->store;
1829 $schema->storage->txn_rollback;
1833 subtest 'Recalls tests' => sub {
1837 $schema->storage->txn_begin;
1839 my $item1 = $builder->build_sample_item;
1840 my $biblio = $item1->biblio;
1841 my $branchcode = $item1->holdingbranch;
1842 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1843 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1844 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1845 my $item2 = $builder->build_object(
1846 { class => 'Koha::Items',
1847 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1851 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1852 t::lib::Mocks::mock_preference('UseRecalls', 1);
1854 my $recall1 = Koha::Recall->new(
1855 { patron_id => $patron1->borrowernumber,
1856 created_date => \'NOW()',
1857 biblio_id => $biblio->biblionumber,
1858 pickup_library_id => $branchcode,
1859 item_id => $item1->itemnumber,
1860 expiration_date => undef,
1864 my $recall2 = Koha::Recall->new(
1865 { patron_id => $patron2->borrowernumber,
1866 created_date => \'NOW()',
1867 biblio_id => $biblio->biblionumber,
1868 pickup_library_id => $branchcode,
1869 item_id => $item1->itemnumber,
1870 expiration_date => undef,
1875 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1877 $recall2->set_cancelled;
1879 t::lib::Mocks::mock_preference('UseRecalls', 0);
1880 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1882 t::lib::Mocks::mock_preference("UseRecalls", 1);
1884 $item1->update({ notforloan => 1 });
1885 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1886 $item1->update({ notforloan => 0, itemlost => 1 });
1887 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1888 $item1->update({ itemlost => 0, withdrawn => 1 });
1889 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1890 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1892 $item1->update({ withdrawn => 0 });
1893 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1895 Koha::CirculationRules->set_rules({
1896 branchcode => $branchcode,
1897 categorycode => $patron1->categorycode,
1898 itemtype => $item1->effective_itemtype,
1900 recalls_allowed => 0,
1901 recalls_per_record => 1,
1902 on_shelf_recalls => 'all',
1905 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1907 Koha::CirculationRules->set_rules({
1908 branchcode => $branchcode,
1909 categorycode => $patron1->categorycode,
1910 itemtype => $item1->effective_itemtype,
1912 recalls_allowed => 1,
1913 recalls_per_record => 1,
1914 on_shelf_recalls => 'all',
1917 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1918 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1919 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1921 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1922 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1923 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1925 $recall1->set_cancelled;
1926 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1928 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1930 Koha::CirculationRules->set_rules({
1931 branchcode => $branchcode,
1932 categorycode => $patron1->categorycode,
1933 itemtype => $item1->effective_itemtype,
1935 recalls_allowed => 1,
1936 recalls_per_record => 1,
1937 on_shelf_recalls => 'any',
1940 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1941 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1943 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1944 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1946 $recall1 = Koha::Recall->new(
1947 { patron_id => $patron1->borrowernumber,
1948 created_date => \'NOW()',
1949 biblio_id => $biblio->biblionumber,
1950 pickup_library_id => $branchcode,
1952 expiration_date => undef,
1957 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1959 Koha::CirculationRules->set_rules({
1960 branchcode => $branchcode,
1961 categorycode => $patron1->categorycode,
1962 itemtype => $item1->effective_itemtype,
1964 recalls_allowed => 0,
1965 recalls_per_record => 1,
1966 on_shelf_recalls => 'any',
1969 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1971 Koha::CirculationRules->set_rules({
1972 branchcode => $branchcode,
1973 categorycode => $patron1->categorycode,
1974 itemtype => $item1->effective_itemtype,
1976 recalls_allowed => 1,
1977 recalls_per_record => 1,
1978 on_shelf_recalls => 'any',
1981 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1983 # check_recalls tests
1985 $recall1 = Koha::Recall->new(
1986 { patron_id => $patron2->borrowernumber,
1987 created_date => \'NOW()',
1988 biblio_id => $biblio->biblionumber,
1989 pickup_library_id => $branchcode,
1990 item_id => $item1->itemnumber,
1991 expiration_date => undef,
1995 $recall2 = Koha::Recall->new(
1996 { patron_id => $patron1->borrowernumber,
1997 created_date => \'NOW()',
1998 biblio_id => $biblio->biblionumber,
1999 pickup_library_id => $branchcode,
2001 expiration_date => undef,
2005 $recall2->set_waiting( { item => $item1 } );
2006 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
2008 # return a waiting recall
2009 my $check_recall = $item1->check_recalls;
2010 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
2012 $recall2->revert_waiting;
2014 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
2016 # return recall based on recalldate
2017 $check_recall = $item1->check_recalls;
2018 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
2020 $recall1->set_cancelled;
2022 # return a biblio-level recall
2023 $check_recall = $item1->check_recalls;
2024 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
2026 $recall2->set_cancelled;
2028 $schema->storage->txn_rollback;
2031 subtest 'Notforloan tests' => sub {
2035 $schema->storage->txn_begin;
2037 my $item1 = $builder->build_sample_item;
2038 $item1->update({ notforloan => 0 });
2039 $item1->itemtype->notforloan(0);
2040 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2041 $item1->update({ notforloan => 1 });
2042 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2043 $item1->update({ notforloan => 0 });
2044 $item1->itemtype->update({ notforloan => 1 });
2045 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2047 $schema->storage->txn_rollback;
2050 subtest 'item_group() tests' => sub {
2054 $schema->storage->txn_begin;
2056 my $biblio = $builder->build_sample_biblio();
2057 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2058 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2060 is( $item_1->item_group, undef, 'Item 1 has no item group');
2061 is( $item_2->item_group, undef, 'Item 2 has no item group');
2063 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2064 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2066 $item_group_1->add_item({ item_id => $item_1->id });
2067 $item_group_2->add_item({ item_id => $item_2->id });
2069 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2070 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2072 $schema->storage->txn_rollback;
2075 subtest 'has_pending_recall() tests' => sub {
2079 $schema->storage->txn_begin;
2081 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2082 my $item = $builder->build_sample_item;
2083 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2085 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2086 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2088 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2090 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2092 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2094 $recall->status('waiting')->store;
2096 ok( $item->has_pending_recall, 'The item has a pending recall' );
2098 $schema->storage->txn_rollback;
2101 subtest 'is_denied_renewal' => sub {
2104 $schema->storage->txn_begin;
2106 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2108 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2109 homebranch => $library->branchcode,
2113 itemcallnumber => undef,
2118 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2119 homebranch => $library->branchcode,
2122 location => 'NOPROC'
2127 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2128 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2130 # The wrong column delete should be silently ignored and not trigger item delete
2131 $idr_rules="delete: [yes]\nwithdrawn: [1]";
2132 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2133 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2134 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2136 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2137 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2138 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2140 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2141 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2142 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2143 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2145 $idr_rules="itemcallnumber: [null]";
2146 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2147 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when null in pref' );
2149 $idr_rules="itemcallnumber: ['']";
2150 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2151 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2153 $idr_rules="itemnotes: [null]";
2154 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2155 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when null in pref' );
2157 $idr_rules="itemnotes: ['']";
2158 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2159 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2161 $schema->storage->txn_rollback;
2164 subtest 'current_branchtransfers relationship' => sub {
2167 $schema->storage->txn_begin;
2169 my $biblio = $builder->build_sample_biblio();
2170 my $item = $builder->build_sample_item(
2172 biblionumber => $biblio->biblionumber,
2175 my $transfers = $item->_result->current_branchtransfers;
2176 is( ref($transfers), 'DBIx::Class::ResultSet',
2177 'current_branchtransfers returns a DBIx::Class::ResultSet' );
2178 is( $transfers->count, 0,
2179 "Empty Koha::Item::Transfers set returned if no return_claims" );
2180 my $transfer1 = $builder->build(
2182 source => 'Branchtransfer',
2184 itemnumber => $item->itemnumber,
2185 datearrived => dt_from_string,
2189 my $transfer2 = $builder->build(
2191 source => 'Branchtransfer',
2193 itemnumber => $item->itemnumber,
2194 datearrived => undef,
2195 datecancelled => dt_from_string,
2199 my $transfer3 = $builder->build(
2201 source => 'Branchtransfer',
2203 itemnumber => $item->itemnumber,
2204 datearrived => undef,
2205 datecancelled => undef,
2210 is( $item->_result->current_branchtransfers()->count,
2211 1, "One transfer found for item" );
2213 $schema->storage->txn_rollback;