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();
231 throws_ok { $host_item->add_to_bundle($host_item) }
232 'Koha::Exceptions::Item::Bundle::IsBundle',
233 'Exception thrown if you try to add the item to itself';
235 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
236 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
238 throws_ok { $host_item->add_to_bundle($bundle_item1) }
239 'Koha::Exceptions::Object::DuplicateID',
240 'Exception thrown if you try to add the same item twice';
242 throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
243 'Koha::Exceptions::Item::Bundle::IsBundle',
244 'Exception thrown if you try to add an item to a bundled item';
246 throws_ok { $bundle_item2->add_to_bundle($host_item) }
247 'Koha::Exceptions::Item::Bundle::IsBundle',
248 'Exception thrown if you try to add a bundle host to a bundle item';
250 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
251 C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
252 throws_ok { $host_item->add_to_bundle($bundle_item2) }
253 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
254 'Exception thrown if you try to add a checked out item';
256 $bundle_item2->withdrawn(1)->store;
257 t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
258 throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
259 'Koha::Exceptions::Checkin::FailedCheckin',
260 'Exception thrown if you try to add a checked out item using
261 "force_checkin" and the return is not possible';
263 $bundle_item2->withdrawn(0)->store;
264 lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
265 'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
267 $bundle_item2->discard_changes;
268 ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
270 $schema->storage->txn_rollback;
273 subtest 'remove_from_bundle tests' => sub {
276 $schema->storage->txn_begin;
278 my $host_item = $builder->build_sample_item();
279 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
280 $schema->resultset('ItemBundle')
282 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
284 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
285 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
286 $bundle_item1 = $bundle_item1->get_from_storage;
287 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
289 $schema->storage->txn_rollback;
292 subtest 'hidden_in_opac() tests' => sub {
296 $schema->storage->txn_begin;
298 my $item = $builder->build_sample_item({ itemlost => 2 });
301 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
302 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
304 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
305 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
307 # enable hidelostitems to verify correct behaviour
308 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
309 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
311 # disable hidelostitems
312 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
313 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
315 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
317 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
321 $schema->storage->txn_rollback;
324 subtest 'has_pending_hold() tests' => sub {
328 $schema->storage->txn_begin;
330 my $dbh = C4::Context->dbh;
331 my $item = $builder->build_sample_item({ itemlost => 0 });
332 my $itemnumber = $item->itemnumber;
334 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
335 ok( $item->has_pending_hold, "Yes, we have a pending hold");
336 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
337 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
339 $schema->storage->txn_rollback;
342 subtest "as_marc_field() tests" => sub {
344 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
345 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
347 my @schema_columns = $schema->resultset('Item')->result_source->columns;
348 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
350 plan tests => 2 * (scalar @mapped_columns + 1) + 4;
352 $schema->storage->txn_begin;
354 my $item = $builder->build_sample_item;
355 # Make sure it has at least one undefined attribute
356 $item->set({ replacementprice => undef })->store->discard_changes;
358 # Tests with the mss parameter
359 my $marc_field = $item->as_marc_field({ mss => $mss });
364 'Generated field set the right tag number'
367 foreach my $column ( @mapped_columns ) {
368 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
369 is( $marc_field->subfield($tagsubfield),
370 $item->$column, "Value is mapped correctly for column $column" );
373 # Tests without the mss parameter
374 $marc_field = $item->as_marc_field();
379 'Generated field set the right tag number'
382 foreach my $column (@mapped_columns) {
383 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
384 is( $marc_field->subfield($tagsubfield),
385 $item->$column, "Value is mapped correctly for column $column" );
388 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
391 tagfield => $itemtag,
395 Koha::MarcSubfieldStructure->new(
398 tagfield => $itemtag,
404 my @unlinked_subfields;
405 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
406 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
408 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
409 Koha::MarcSubfieldStructures->search(
410 { frameworkcode => '', tagfield => $itemtag } )
411 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
413 $marc_field = $item->as_marc_field;
415 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
416 my @subfields = $marc_field->subfields;
417 my $result = all { defined $_->[1] } @subfields;
418 ok( $result, 'There are no undef subfields' );
419 my @ordered_subfields = sort {
420 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
421 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
423 is_deeply(\@subfields, \@ordered_subfields);
425 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
426 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
428 $schema->storage->txn_rollback;
429 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
432 subtest 'pickup_locations' => 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 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
487 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
490 "1-1-from_home_library-any" => 3,
491 "1-1-from_home_library-holdgroup" => 2,
492 "1-1-from_home_library-patrongroup" => 2,
493 "1-1-from_home_library-homebranch" => 1,
494 "1-1-from_home_library-holdingbranch" => 1,
495 "1-1-from_any_library-any" => 3,
496 "1-1-from_any_library-holdgroup" => 2,
497 "1-1-from_any_library-patrongroup" => 2,
498 "1-1-from_any_library-homebranch" => 1,
499 "1-1-from_any_library-holdingbranch" => 1,
500 "1-1-from_local_hold_group-any" => 3,
501 "1-1-from_local_hold_group-holdgroup" => 2,
502 "1-1-from_local_hold_group-patrongroup" => 2,
503 "1-1-from_local_hold_group-homebranch" => 1,
504 "1-1-from_local_hold_group-holdingbranch" => 1,
505 "1-4-from_home_library-any" => 0,
506 "1-4-from_home_library-holdgroup" => 0,
507 "1-4-from_home_library-patrongroup" => 0,
508 "1-4-from_home_library-homebranch" => 0,
509 "1-4-from_home_library-holdingbranch" => 0,
510 "1-4-from_any_library-any" => 3,
511 "1-4-from_any_library-holdgroup" => 2,
512 "1-4-from_any_library-patrongroup" => 1,
513 "1-4-from_any_library-homebranch" => 1,
514 "1-4-from_any_library-holdingbranch" => 1,
515 "1-4-from_local_hold_group-any" => 0,
516 "1-4-from_local_hold_group-holdgroup" => 0,
517 "1-4-from_local_hold_group-patrongroup" => 0,
518 "1-4-from_local_hold_group-homebranch" => 0,
519 "1-4-from_local_hold_group-holdingbranch" => 0,
520 "3-1-from_home_library-any" => 0,
521 "3-1-from_home_library-holdgroup" => 0,
522 "3-1-from_home_library-patrongroup" => 0,
523 "3-1-from_home_library-homebranch" => 0,
524 "3-1-from_home_library-holdingbranch" => 0,
525 "3-1-from_any_library-any" => 3,
526 "3-1-from_any_library-holdgroup" => 1,
527 "3-1-from_any_library-patrongroup" => 2,
528 "3-1-from_any_library-homebranch" => 0,
529 "3-1-from_any_library-holdingbranch" => 1,
530 "3-1-from_local_hold_group-any" => 0,
531 "3-1-from_local_hold_group-holdgroup" => 0,
532 "3-1-from_local_hold_group-patrongroup" => 0,
533 "3-1-from_local_hold_group-homebranch" => 0,
534 "3-1-from_local_hold_group-holdingbranch" => 0,
535 "3-4-from_home_library-any" => 0,
536 "3-4-from_home_library-holdgroup" => 0,
537 "3-4-from_home_library-patrongroup" => 0,
538 "3-4-from_home_library-homebranch" => 0,
539 "3-4-from_home_library-holdingbranch" => 0,
540 "3-4-from_any_library-any" => 3,
541 "3-4-from_any_library-holdgroup" => 1,
542 "3-4-from_any_library-patrongroup" => 1,
543 "3-4-from_any_library-homebranch" => 0,
544 "3-4-from_any_library-holdingbranch" => 1,
545 "3-4-from_local_hold_group-any" => 3,
546 "3-4-from_local_hold_group-holdgroup" => 1,
547 "3-4-from_local_hold_group-patrongroup" => 1,
548 "3-4-from_local_hold_group-homebranch" => 0,
549 "3-4-from_local_hold_group-holdingbranch" => 1
553 my ( $item, $patron, $ha, $hfp, $results ) = @_;
555 Koha::CirculationRules->set_rules(
561 hold_fulfillment_policy => $hfp,
562 returnbranch => 'any'
567 $ha eq 'from_local_hold_group' ? 'holdgroup'
569 $ha eq 'from_any_library' ? 'any'
574 my $pickup_location = $_;
575 grep { $pickup_location->branchcode eq $_ } @branchcodes
576 } $item->pickup_locations( { patron => $patron } )->as_list;
579 scalar(@pl) eq $results->{
580 $item->copynumber . '-'
581 . $patron->firstname . '-'
591 . ', hold_fulfillment_policy: '
595 $item->copynumber . '-'
596 . $patron->firstname . '-'
607 foreach my $item ($item1, $item3) {
608 foreach my $patron ($patron1, $patron4) {
609 #holdallowed 1: homebranch, 2: any, 3: holdgroup
610 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
611 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
612 _doTest($item, $patron, $ha, $hfp, $results);
618 # Now test that branchtransferlimits will further filter the pickup locations
620 my $item_no_ccode = $builder->build_sample_item(
622 homebranch => $library1->branchcode,
623 holdingbranch => $library2->branchcode,
624 itype => $item1->itype,
628 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
629 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
630 Koha::CirculationRules->set_rules(
633 itemtype => $item1->itype,
635 holdallowed => 'from_home_library',
636 hold_fulfillment_policy => 1,
637 returnbranch => 'any'
641 $builder->build_object(
643 class => 'Koha::Item::Transfer::Limits',
645 toBranch => $library1->branchcode,
646 fromBranch => $library2->branchcode,
647 itemtype => $item1->itype,
653 my @pickup_locations = map {
654 my $pickup_location = $_;
655 grep { $pickup_location->branchcode eq $_ } @branchcodes
656 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
658 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
660 $builder->build_object(
662 class => 'Koha::Item::Transfer::Limits',
664 toBranch => $library4->branchcode,
665 fromBranch => $library2->branchcode,
666 itemtype => $item1->itype,
672 @pickup_locations = map {
673 my $pickup_location = $_;
674 grep { $pickup_location->branchcode eq $_ } @branchcodes
675 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
677 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
679 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
680 @pickup_locations = map {
681 my $pickup_location = $_;
682 grep { $pickup_location->branchcode eq $_ } @branchcodes
683 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
684 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
686 @pickup_locations = map {
687 my $pickup_location = $_;
688 grep { $pickup_location->branchcode eq $_ } @branchcodes
689 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
690 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");
692 $builder->build_object(
694 class => 'Koha::Item::Transfer::Limits',
696 toBranch => $library2->branchcode,
697 fromBranch => $library2->branchcode,
699 ccode => $item1->ccode,
704 @pickup_locations = map {
705 my $pickup_location = $_;
706 grep { $pickup_location->branchcode eq $_ } @branchcodes
707 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
708 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
710 $builder->build_object(
712 class => 'Koha::Item::Transfer::Limits',
714 toBranch => $library4->branchcode,
715 fromBranch => $library2->branchcode,
717 ccode => $item1->ccode,
722 @pickup_locations = map {
723 my $pickup_location = $_;
724 grep { $pickup_location->branchcode eq $_ } @branchcodes
725 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
726 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
728 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
730 $schema->storage->txn_rollback;
733 subtest 'request_transfer' => sub {
735 $schema->storage->txn_begin;
737 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
738 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
739 my $item = $builder->build_sample_item(
741 homebranch => $library1->branchcode,
742 holdingbranch => $library2->branchcode,
746 # Mandatory fields tests
747 throws_ok { $item->request_transfer( { to => $library1 } ) }
748 'Koha::Exceptions::MissingParameter',
749 'Exception thrown if `reason` parameter is missing';
751 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
752 'Koha::Exceptions::MissingParameter',
753 'Exception thrown if `to` parameter is missing';
756 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
757 is( ref($transfer), 'Koha::Item::Transfer',
758 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
760 my $original_transfer = $transfer->get_from_storage;
762 # Transfer already in progress
763 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
764 'Koha::Exceptions::Item::Transfer::InQueue',
765 'Exception thrown if transfer is already in progress';
768 is( ref( $exception->transfer ),
769 'Koha::Item::Transfer',
770 'The exception contains the found Koha::Item::Transfer' );
773 my $queued_transfer = $item->request_transfer(
774 { to => $library2, reason => 'Manual', enqueue => 1 } );
775 is( ref($queued_transfer), 'Koha::Item::Transfer',
776 'Koha::Item->request_transfer allowed when enqueue is set' );
777 my $transfers = $item->get_transfers;
778 is($transfers->count, 2, "There are now 2 live transfers in the queue");
779 $transfer = $transfer->get_from_storage;
780 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
781 $queued_transfer->datearrived(dt_from_string)->store();
784 my $replaced_transfer = $item->request_transfer(
785 { to => $library2, reason => 'Manual', replace => 1 } );
786 is( ref($replaced_transfer), 'Koha::Item::Transfer',
787 'Koha::Item->request_transfer allowed when replace is set' );
788 $original_transfer->discard_changes;
789 ok($original_transfer->datecancelled, "Original transfer cancelled");
790 $transfers = $item->get_transfers;
791 is($transfers->count, 1, "There is only 1 live transfer in the queue");
792 $replaced_transfer->datearrived(dt_from_string)->store();
794 # BranchTransferLimits
795 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
796 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
797 my $limit = Koha::Item::Transfer::Limit->new({
798 fromBranch => $library2->branchcode,
799 toBranch => $library1->branchcode,
800 itemtype => $item->effective_itemtype,
803 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
804 'Koha::Exceptions::Item::Transfer::Limit',
805 'Exception thrown if transfer is prevented by limits';
807 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
808 is( ref($forced_transfer), 'Koha::Item::Transfer',
809 'Koha::Item->request_transfer allowed when ignore_limits is set'
812 $schema->storage->txn_rollback;
815 subtest 'deletion' => sub {
818 $schema->storage->txn_begin;
820 my $biblio = $builder->build_sample_biblio();
822 my $item = $builder->build_sample_item(
824 biblionumber => $biblio->biblionumber,
827 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
829 my $deleted_item = $item->move_to_deleted;
830 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
831 ; # FIXME This should be Koha::Deleted::Item
832 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
834 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
835 $item = $builder->build_sample_item(
837 biblionumber => $biblio->biblionumber,
841 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
844 my $library = $builder->build_object({ class => 'Koha::Libraries' });
845 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
846 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
848 my $patron = $builder->build_object({class => 'Koha::Patrons'});
849 $item = $builder->build_sample_item({ library => $library->branchcode });
852 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
855 @{$item->safe_to_delete->messages}[0]->message,
857 'Koha::Item->safe_to_delete reports item on loan',
861 @{$item->safe_to_delete->messages}[0]->message,
863 'item that is on loan cannot be deleted',
867 ! $item->safe_to_delete,
868 'Koha::Item->safe_to_delete shows item NOT safe to delete'
871 AddReturn( $item->barcode, $library->branchcode );
874 t::lib::Mocks::mock_preference('IndependentBranches', 1);
875 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
878 @{$item_2->safe_to_delete->messages}[0]->message,
880 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
884 @{$item_2->safe_to_delete->messages}[0]->message,
886 'IndependentBranches prevents deletion at another branch',
891 { # codeblock to limit scope of $module->mock
893 my $module = Test::MockModule->new('C4::Items');
894 $module->mock( GetAnalyticsCount => sub { return 1 } );
896 $item->discard_changes;
898 @{$item->safe_to_delete->messages}[0]->message,
900 'Koha::Item->safe_to_delete reports linked analytics',
904 @{$item->safe_to_delete->messages}[0]->message,
906 'Linked analytics prevents deletion of item',
912 $item->safe_to_delete,
913 'Koha::Item->safe_to_delete shows item safe to delete'
918 my $test_item = Koha::Items->find( $item->itemnumber );
920 is( $test_item, undef,
921 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
924 subtest 'holds tests' => sub {
929 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
931 $schema->storage->txn_begin;
933 my $item = $builder->build_sample_item;
935 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
936 my $safe_to_delete = $item->safe_to_delete;
938 ok( !$safe_to_delete, 'Cannot delete' );
940 @{ $safe_to_delete->messages }[0]->message,
942 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
947 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
948 $safe_to_delete = $item->safe_to_delete;
950 ok( !$safe_to_delete, 'Cannot delete' );
952 @{ $safe_to_delete->messages }[0]->message,
954 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
959 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
960 $safe_to_delete = $item->safe_to_delete;
962 ok( !$safe_to_delete, 'Cannot delete' );
964 @{ $safe_to_delete->messages }[0]->message,
966 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
971 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
972 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
974 $safe_to_delete = $item->safe_to_delete;
976 ok( !$safe_to_delete );
979 @{ $safe_to_delete->messages}[0]->message,
980 'last_item_for_hold',
981 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
984 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
986 ok( $item->safe_to_delete );
988 $schema->storage->txn_rollback;
991 $schema->storage->txn_rollback;
994 subtest 'renewal_branchcode' => sub {
997 $schema->storage->txn_begin;
999 my $item = $builder->build_sample_item();
1000 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1001 my $checkout = $builder->build_object({
1002 class => 'Koha::Checkouts',
1004 itemnumber => $item->itemnumber,
1009 C4::Context->interface( 'intranet' );
1010 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1012 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1013 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1014 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1015 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1017 C4::Context->interface( 'opac' );
1019 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1020 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1021 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1023 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1024 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1025 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1027 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1028 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1029 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");
1031 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1032 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1033 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");
1035 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1036 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1037 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");
1039 $schema->storage->txn_rollback;
1042 subtest 'Tests for itemtype' => sub {
1044 $schema->storage->txn_begin;
1046 my $biblio = $builder->build_sample_biblio;
1047 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1048 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1050 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1051 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1052 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1053 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1055 $schema->storage->txn_rollback;
1058 subtest 'get_transfers' => sub {
1060 $schema->storage->txn_begin;
1062 my $item = $builder->build_sample_item();
1064 my $transfers = $item->get_transfers();
1065 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1066 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1068 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1070 my $transfer_1 = $builder->build_object(
1072 class => 'Koha::Item::Transfers',
1074 itemnumber => $item->itemnumber,
1075 frombranch => $item->holdingbranch,
1076 tobranch => $library_to->branchcode,
1079 datearrived => undef,
1080 datecancelled => undef,
1081 daterequested => \'NOW()'
1086 $transfers = $item->get_transfers();
1087 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1089 my $transfer_2 = $builder->build_object(
1091 class => 'Koha::Item::Transfers',
1093 itemnumber => $item->itemnumber,
1094 frombranch => $item->holdingbranch,
1095 tobranch => $library_to->branchcode,
1098 datearrived => undef,
1099 datecancelled => undef,
1100 daterequested => \'NOW()'
1105 my $transfer_3 = $builder->build_object(
1107 class => 'Koha::Item::Transfers',
1109 itemnumber => $item->itemnumber,
1110 frombranch => $item->holdingbranch,
1111 tobranch => $library_to->branchcode,
1114 datearrived => undef,
1115 datecancelled => undef,
1116 daterequested => \'NOW()'
1121 $transfers = $item->get_transfers();
1122 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1123 my $result_1 = $transfers->next;
1124 my $result_2 = $transfers->next;
1125 my $result_3 = $transfers->next;
1126 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1127 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1128 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1130 $transfer_2->datesent(\'NOW()')->store;
1131 $transfers = $item->get_transfers();
1132 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1133 $result_1 = $transfers->next;
1134 $result_2 = $transfers->next;
1135 $result_3 = $transfers->next;
1136 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1137 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1138 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1140 $transfer_2->datearrived(\'NOW()')->store;
1141 $transfers = $item->get_transfers();
1142 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1143 $result_1 = $transfers->next;
1144 $result_2 = $transfers->next;
1145 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1146 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1148 $transfer_1->datecancelled(\'NOW()')->store;
1149 $transfers = $item->get_transfers();
1150 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1151 $result_1 = $transfers->next;
1152 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1154 $schema->storage->txn_rollback;
1157 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1160 $schema->storage->txn_begin;
1162 my $biblio = $builder->build_sample_biblio();
1163 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1165 my $orders = $item->orders;
1166 is ($orders->count, 0, 'No order on this item yet');
1168 my $order_note = 'Order for ' . $item->itemnumber;
1170 my $aq_order1 = $builder->build_object({
1171 class => 'Koha::Acquisition::Orders',
1173 biblionumber => $biblio->biblionumber,
1174 order_internalnote => $order_note,
1177 my $aq_order2 = $builder->build_object({
1178 class => 'Koha::Acquisition::Orders',
1180 biblionumber => $biblio->biblionumber,
1183 my $aq_order_item1 = $builder->build({
1184 source => 'AqordersItem',
1186 ordernumber => $aq_order1->ordernumber,
1187 itemnumber => $item->itemnumber,
1191 $orders = $item->orders;
1192 is ($orders->count, 1, 'One order found by item with the relationship');
1193 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1196 subtest 'move_to_biblio() tests' => sub {
1199 $schema->storage->txn_begin;
1201 my $dbh = C4::Context->dbh;
1203 my $source_biblio = $builder->build_sample_biblio();
1204 my $target_biblio = $builder->build_sample_biblio();
1206 my $source_biblionumber = $source_biblio->biblionumber;
1207 my $target_biblionumber = $target_biblio->biblionumber;
1209 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1210 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1211 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1213 my $itemnumber1 = $item1->itemnumber;
1214 my $itemnumber2 = $item2->itemnumber;
1216 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1218 my $patron = $builder->build_object({
1219 class => 'Koha::Patrons',
1220 value => { branchcode => $library->branchcode }
1222 my $borrowernumber = $patron->borrowernumber;
1224 my $aq_budget = $builder->build({
1225 source => 'Aqbudget',
1227 budget_notes => 'test',
1231 my $aq_order1 = $builder->build_object({
1232 class => 'Koha::Acquisition::Orders',
1234 biblionumber => $source_biblionumber,
1235 budget_id => $aq_budget->{budget_id},
1238 my $aq_order_item1 = $builder->build({
1239 source => 'AqordersItem',
1241 ordernumber => $aq_order1->ordernumber,
1242 itemnumber => $itemnumber1,
1245 my $aq_order2 = $builder->build_object({
1246 class => 'Koha::Acquisition::Orders',
1248 biblionumber => $source_biblionumber,
1249 budget_id => $aq_budget->{budget_id},
1252 my $aq_order_item2 = $builder->build({
1253 source => 'AqordersItem',
1255 ordernumber => $aq_order2->ordernumber,
1256 itemnumber => $itemnumber2,
1260 my $bib_level_hold = $builder->build_object({
1261 class => 'Koha::Holds',
1263 biblionumber => $source_biblionumber,
1264 itemnumber => undef,
1267 my $item_level_hold1 = $builder->build_object({
1268 class => 'Koha::Holds',
1270 biblionumber => $source_biblionumber,
1271 itemnumber => $itemnumber1,
1274 my $item_level_hold2 = $builder->build_object({
1275 class => 'Koha::Holds',
1277 biblionumber => $source_biblionumber,
1278 itemnumber => $itemnumber2,
1282 my $tmp_holdsqueue1 = $builder->build({
1283 source => 'TmpHoldsqueue',
1285 borrowernumber => $borrowernumber,
1286 biblionumber => $source_biblionumber,
1287 itemnumber => $itemnumber1,
1290 my $tmp_holdsqueue2 = $builder->build({
1291 source => 'TmpHoldsqueue',
1293 borrowernumber => $borrowernumber,
1294 biblionumber => $source_biblionumber,
1295 itemnumber => $itemnumber2,
1298 my $hold_fill_target1 = $builder->build({
1299 source => 'HoldFillTarget',
1301 borrowernumber => $borrowernumber,
1302 biblionumber => $source_biblionumber,
1303 itemnumber => $itemnumber1,
1306 my $hold_fill_target2 = $builder->build({
1307 source => 'HoldFillTarget',
1309 borrowernumber => $borrowernumber,
1310 biblionumber => $source_biblionumber,
1311 itemnumber => $itemnumber2,
1314 my $linktracker1 = $builder->build({
1315 source => 'Linktracker',
1317 borrowernumber => $borrowernumber,
1318 biblionumber => $source_biblionumber,
1319 itemnumber => $itemnumber1,
1322 my $linktracker2 = $builder->build({
1323 source => 'Linktracker',
1325 borrowernumber => $borrowernumber,
1326 biblionumber => $source_biblionumber,
1327 itemnumber => $itemnumber2,
1331 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1332 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1334 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1335 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');
1337 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1338 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1339 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1340 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1341 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1342 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1344 $aq_order1->discard_changes;
1345 $aq_order2->discard_changes;
1346 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1347 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1349 $bib_level_hold->discard_changes;
1350 $item_level_hold1->discard_changes;
1351 $item_level_hold2->discard_changes;
1352 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1353 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1354 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1356 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1357 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1358 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1359 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1361 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1362 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1363 # Why does ->biblionumber return a Biblio object???
1364 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1365 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1367 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1368 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1369 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1370 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1372 $schema->storage->txn_rollback;
1375 subtest 'columns_to_str' => sub {
1378 $schema->storage->txn_begin;
1380 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1382 my $cache = Koha::Caches->get_instance();
1383 $cache->clear_from_cache("MarcStructure-0-");
1384 $cache->clear_from_cache("MarcStructure-1-");
1385 $cache->clear_from_cache("MarcSubfieldStructure-");
1386 $cache->clear_from_cache("libraries:name");
1387 $cache->clear_from_cache("itemtype:description:en");
1388 $cache->clear_from_cache("cn_sources:description");
1389 $cache->clear_from_cache("AV_descriptions:LOST");
1391 # Creating subfields 'é', 'è' that are not linked with a kohafield
1392 Koha::MarcSubfieldStructures->search(
1394 frameworkcode => '',
1395 tagfield => $itemtag,
1396 tagsubfield => ['é', 'è'],
1398 )->delete; # In case it exist already
1400 # é is not linked with a AV
1401 # è is linked with AV branches
1402 Koha::MarcSubfieldStructure->new(
1404 frameworkcode => '',
1405 tagfield => $itemtag,
1406 tagsubfield => 'é',
1409 defaultvalue => 'ééé',
1413 Koha::MarcSubfieldStructure->new(
1415 frameworkcode => '',
1416 tagfield => $itemtag,
1417 tagsubfield => 'è',
1420 defaultvalue => 'èèè',
1422 authorised_value => 'branches',
1426 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1427 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1428 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1429 my $dateaccessioned = '2020-12-15';
1430 my $library = Koha::Libraries->search->next;
1431 my $branchcode = $library->branchcode;
1433 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1435 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1436 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1437 xmlns="http://www.loc.gov/MARC21/slim">
1440 <leader> a </leader>
1441 <datafield tag="999" ind1=" " ind2=" ">
1442 <subfield code="é">value é</subfield>
1443 <subfield code="è">$branchcode</subfield>
1451 itemlost => $lost_av->authorised_value,
1452 dateaccessioned => $dateaccessioned,
1453 more_subfields_xml => $some_marc_xml,
1457 $item = $item->get_from_storage;
1459 my $s = $item->columns_to_str;
1460 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1461 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1462 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1463 is( $s->{'è'}, $library->branchname );
1465 $cache->clear_from_cache("MarcStructure-0-");
1466 $cache->clear_from_cache("MarcStructure-1-");
1467 $cache->clear_from_cache("MarcSubfieldStructure-");
1468 $cache->clear_from_cache("libraries:name");
1469 $cache->clear_from_cache("itemtype:description:en");
1470 $cache->clear_from_cache("cn_sources:description");
1471 $cache->clear_from_cache("AV_descriptions:LOST");
1473 $schema->storage->txn_rollback;
1476 subtest 'strings_map() tests' => sub {
1480 $schema->storage->txn_begin;
1482 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1484 my $cache = Koha::Caches->get_instance();
1485 $cache->clear_from_cache("MarcStructure-0-");
1486 $cache->clear_from_cache("MarcStructure-1-");
1487 $cache->clear_from_cache("MarcSubfieldStructure-");
1488 $cache->clear_from_cache("libraries:name");
1489 $cache->clear_from_cache("itemtype:description:en");
1490 $cache->clear_from_cache("cn_sources:description");
1491 $cache->clear_from_cache("AV_descriptions:LOST");
1493 # Recreating subfields just to be sure tests will be ok
1498 Koha::MarcSubfieldStructures->search(
1500 frameworkcode => '',
1501 tagfield => $itemtag,
1502 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1504 )->delete; # In case it exist already
1506 Koha::MarcSubfieldStructure->new(
1508 authorised_value => 'LOST',
1510 frameworkcode => '',
1511 kohafield => 'items.itemlost',
1514 tagfield => $itemtag,
1518 Koha::MarcSubfieldStructure->new(
1520 authorised_value => 'cn_source',
1522 frameworkcode => '',
1523 kohafield => 'items.cn_source',
1526 tagfield => $itemtag,
1530 Koha::MarcSubfieldStructure->new(
1532 authorised_value => '',
1534 frameworkcode => '',
1535 kohafield => 'items.materials',
1538 tagfield => $itemtag,
1542 Koha::MarcSubfieldStructure->new(
1544 authorised_value => 'branches',
1546 frameworkcode => '',
1547 kohafield => 'items.homebranch',
1550 tagfield => $itemtag,
1554 Koha::MarcSubfieldStructure->new(
1556 authorised_value => 'itemtypes',
1558 frameworkcode => '',
1559 kohafield => 'items.itype',
1562 tagfield => $itemtag,
1567 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1568 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1569 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1570 my $item = $builder->build_sample_item(
1572 biblionumber => $biblio->id,
1573 library => $library->id
1577 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1578 my $lost_av = $builder->build_object(
1580 class => 'Koha::AuthorisedValues',
1582 authorised_value => 3,
1584 lib => 'internal description',
1585 lib_opac => 'public description',
1590 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1591 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1592 my $class_source = $builder->build_object(
1594 class => 'Koha::ClassSources',
1596 class_sort_rule => $class_sort_rule->class_sort_rule,
1597 class_split_rule => $class_split_rule->class_split_rule,
1604 cn_source => $class_source->id,
1605 itemlost => $lost_av->authorised_value,
1606 itype => $itype->itemtype,
1607 materials => 'Suff',
1609 )->store->discard_changes;
1611 my $strings = $item->strings_map;
1613 subtest 'unmapped field tests' => sub {
1617 ok( !exists $strings->{materials}, "Unmapped field not present" );
1620 subtest 'av handling' => sub {
1624 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1625 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1626 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1627 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1630 subtest 'cn_source handling' => sub {
1634 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1635 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1636 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1639 subtest 'branches handling' => sub {
1643 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1644 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1645 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1648 subtest 'itemtype handling' => sub {
1652 ok( exists $strings->{itype}, "'itype' entry exists" );
1653 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1654 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1657 subtest 'public flag tests' => sub {
1661 $strings = $item->strings_map( { public => 1 } );
1663 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1664 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1665 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1666 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1669 $cache->clear_from_cache("MarcStructure-0-");
1670 $cache->clear_from_cache("MarcStructure-1-");
1671 $cache->clear_from_cache("MarcSubfieldStructure-");
1672 $cache->clear_from_cache("libraries:name");
1673 $cache->clear_from_cache("itemtype:description:en");
1674 $cache->clear_from_cache("cn_sources:description");
1676 $schema->storage->txn_rollback;
1679 subtest 'store() tests' => sub {
1683 subtest 'dateaccessioned handling' => sub {
1687 $schema->storage->txn_begin;
1689 my $item = $builder->build_sample_item;
1691 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1693 # reset dateaccessioned on the DB
1694 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1695 $item->discard_changes;
1697 ok( !defined $item->dateaccessioned );
1700 $item->replacementprice(100)->store->discard_changes;
1702 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1704 $schema->storage->txn_rollback;
1707 subtest '_set_found_trigger() tests' => sub {
1711 $schema->storage->txn_begin;
1713 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1714 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1716 # Add a lost item debit
1717 my $debit = $patron->account->add_debit(
1721 item_id => $item->id,
1722 interface => 'intranet',
1726 # Add a lost item processing fee
1727 my $processing_debit = $patron->account->add_debit(
1730 type => 'PROCESSING',
1731 item_id => $item->id,
1732 interface => 'intranet',
1736 my $lostreturn_policy = {
1737 lostreturn => 'charge',
1738 processingreturn => 'refund'
1741 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1742 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1744 # simulate it was found
1745 $item->set( { itemlost => 0 } )->store;
1747 my $messages = $item->object_messages;
1749 my $message_1 = $messages->[0];
1751 is( $message_1->type, 'info', 'type is correct' );
1752 is( $message_1->message, 'lost_refunded', 'message is correct' );
1754 # Find the refund credit
1755 my $credit = $debit->credits->next;
1758 $message_1->payload,
1759 { credit_id => $credit->id },
1763 my $message_2 = $messages->[1];
1765 is( $message_2->type, 'info', 'type is correct' );
1766 is( $message_2->message, 'lost_charge', 'message is correct' );
1767 is( $message_2->payload, undef, 'no payload' );
1769 my $message_3 = $messages->[2];
1770 is( $message_3->message, 'processing_refunded', 'message is correct' );
1772 my $processing_credit = $processing_debit->credits->next;
1774 $message_3->payload,
1775 { credit_id => $processing_credit->id },
1779 # Let's build a new item
1780 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1781 $item->set( { itemlost => 0 } )->store;
1783 $messages = $item->object_messages;
1784 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1786 $schema->storage->txn_rollback;
1789 subtest 'holds_queue update tests' => sub {
1793 $schema->storage->txn_begin;
1795 my $biblio = $builder->build_sample_biblio;
1797 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1798 $mock->mock( 'enqueue', sub {
1799 my ( $self, $args ) = @_;
1801 $args->{biblio_ids},
1803 '->store triggers a holds queue update for the related biblio'
1807 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1810 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1813 $item->set({ reserves => 1 })->store;
1815 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1817 $item->set({ reserves => 0 })->store;
1819 $schema->storage->txn_rollback;
1823 subtest 'Recalls tests' => sub {
1827 $schema->storage->txn_begin;
1829 my $item1 = $builder->build_sample_item;
1830 my $biblio = $item1->biblio;
1831 my $branchcode = $item1->holdingbranch;
1832 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1833 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1834 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1835 my $item2 = $builder->build_object(
1836 { class => 'Koha::Items',
1837 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1841 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1842 t::lib::Mocks::mock_preference('UseRecalls', 1);
1844 my $recall1 = Koha::Recall->new(
1845 { patron_id => $patron1->borrowernumber,
1846 created_date => \'NOW()',
1847 biblio_id => $biblio->biblionumber,
1848 pickup_library_id => $branchcode,
1849 item_id => $item1->itemnumber,
1850 expiration_date => undef,
1854 my $recall2 = Koha::Recall->new(
1855 { patron_id => $patron2->borrowernumber,
1856 created_date => \'NOW()',
1857 biblio_id => $biblio->biblionumber,
1858 pickup_library_id => $branchcode,
1859 item_id => $item1->itemnumber,
1860 expiration_date => undef,
1865 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1867 $recall2->set_cancelled;
1869 t::lib::Mocks::mock_preference('UseRecalls', 0);
1870 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1872 t::lib::Mocks::mock_preference("UseRecalls", 1);
1874 $item1->update({ notforloan => 1 });
1875 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1876 $item1->update({ notforloan => 0, itemlost => 1 });
1877 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1878 $item1->update({ itemlost => 0, withdrawn => 1 });
1879 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1880 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1882 $item1->update({ withdrawn => 0 });
1883 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1885 Koha::CirculationRules->set_rules({
1886 branchcode => $branchcode,
1887 categorycode => $patron1->categorycode,
1888 itemtype => $item1->effective_itemtype,
1890 recalls_allowed => 0,
1891 recalls_per_record => 1,
1892 on_shelf_recalls => 'all',
1895 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1897 Koha::CirculationRules->set_rules({
1898 branchcode => $branchcode,
1899 categorycode => $patron1->categorycode,
1900 itemtype => $item1->effective_itemtype,
1902 recalls_allowed => 1,
1903 recalls_per_record => 1,
1904 on_shelf_recalls => 'all',
1907 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1908 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1909 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1911 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1912 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1913 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1915 $recall1->set_cancelled;
1916 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1918 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1920 Koha::CirculationRules->set_rules({
1921 branchcode => $branchcode,
1922 categorycode => $patron1->categorycode,
1923 itemtype => $item1->effective_itemtype,
1925 recalls_allowed => 1,
1926 recalls_per_record => 1,
1927 on_shelf_recalls => 'any',
1930 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1931 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1933 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1934 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1936 $recall1 = Koha::Recall->new(
1937 { patron_id => $patron1->borrowernumber,
1938 created_date => \'NOW()',
1939 biblio_id => $biblio->biblionumber,
1940 pickup_library_id => $branchcode,
1942 expiration_date => undef,
1947 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1949 Koha::CirculationRules->set_rules({
1950 branchcode => undef,
1951 categorycode => undef,
1952 itemtype => $item1->effective_itemtype,
1954 recalls_allowed => 0,
1955 recalls_per_record => 1,
1956 on_shelf_recalls => 'any',
1959 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1961 Koha::CirculationRules->set_rules({
1962 branchcode => undef,
1963 categorycode => undef,
1964 itemtype => $item1->effective_itemtype,
1966 recalls_allowed => 1,
1967 recalls_per_record => 1,
1968 on_shelf_recalls => 'any',
1971 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1973 # check_recalls tests
1975 $recall1 = Koha::Recall->new(
1976 { patron_id => $patron2->borrowernumber,
1977 created_date => \'NOW()',
1978 biblio_id => $biblio->biblionumber,
1979 pickup_library_id => $branchcode,
1980 item_id => $item1->itemnumber,
1981 expiration_date => undef,
1985 $recall2 = Koha::Recall->new(
1986 { patron_id => $patron1->borrowernumber,
1987 created_date => \'NOW()',
1988 biblio_id => $biblio->biblionumber,
1989 pickup_library_id => $branchcode,
1991 expiration_date => undef,
1995 $recall2->set_waiting( { item => $item1 } );
1996 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
1998 # return a waiting recall
1999 my $check_recall = $item1->check_recalls;
2000 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
2002 $recall2->revert_waiting;
2004 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
2006 # return recall based on recalldate
2007 $check_recall = $item1->check_recalls;
2008 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
2010 $recall1->set_cancelled;
2012 # return a biblio-level recall
2013 $check_recall = $item1->check_recalls;
2014 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
2016 $recall2->set_cancelled;
2018 $schema->storage->txn_rollback;
2021 subtest 'Notforloan tests' => sub {
2025 $schema->storage->txn_begin;
2027 my $item1 = $builder->build_sample_item;
2028 $item1->update({ notforloan => 0 });
2029 $item1->itemtype->notforloan(0);
2030 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2031 $item1->update({ notforloan => 1 });
2032 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2033 $item1->update({ notforloan => 0 });
2034 $item1->itemtype->update({ notforloan => 1 });
2035 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2037 $schema->storage->txn_rollback;
2040 subtest 'item_group() tests' => sub {
2044 $schema->storage->txn_begin;
2046 my $biblio = $builder->build_sample_biblio();
2047 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2048 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2050 is( $item_1->item_group, undef, 'Item 1 has no item group');
2051 is( $item_2->item_group, undef, 'Item 2 has no item group');
2053 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2054 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2056 $item_group_1->add_item({ item_id => $item_1->id });
2057 $item_group_2->add_item({ item_id => $item_2->id });
2059 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2060 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2062 $schema->storage->txn_rollback;
2065 subtest 'has_pending_recall() tests' => sub {
2069 $schema->storage->txn_begin;
2071 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2072 my $item = $builder->build_sample_item;
2073 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2075 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2076 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2078 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2080 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2082 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2084 $recall->status('waiting')->store;
2086 ok( $item->has_pending_recall, 'The item has a pending recall' );
2088 $schema->storage->txn_rollback;
2091 subtest 'is_denied_renewal' => sub {
2094 $schema->storage->txn_begin;
2096 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2098 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2099 homebranch => $library->branchcode,
2103 itemcallnumber => undef,
2108 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2109 homebranch => $library->branchcode,
2112 location => 'NOPROC'
2117 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2118 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2120 $idr_rules="withdrawn: [1]";
2121 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2122 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2123 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2125 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2126 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2127 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2129 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2130 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2131 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2132 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2134 $idr_rules="itemcallnumber: [NULL]";
2135 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2136 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when NULL in pref' );
2138 $idr_rules="itemcallnumber: ['']";
2139 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2140 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2142 $idr_rules="itemnotes: [NULL]";
2143 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2144 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when NULL in pref' );
2146 $idr_rules="itemnotes: ['']";
2147 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2148 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2150 $schema->storage->txn_rollback;