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 => 30;
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, $host_item->barcode );
267 throws_ok { $host_item->add_to_bundle($bundle_item2) }
268 'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut',
269 'Exception thrown if you try to add an item to a checked out bundle';
270 C4::Circulation::AddReturn( $host_item->barcode, $host_item->homebranch );
271 $host_item->discard_changes;
273 C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
274 throws_ok { $host_item->add_to_bundle($bundle_item2) }
275 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
276 'Exception thrown if you try to add a checked out item';
278 $bundle_item2->withdrawn(1)->store;
279 t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
280 throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
281 'Koha::Exceptions::Checkin::FailedCheckin',
282 'Exception thrown if you try to add a checked out item using
283 "force_checkin" and the return is not possible';
285 $bundle_item2->withdrawn(0)->store;
286 lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
287 'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
289 $bundle_item2->discard_changes;
290 ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
292 $schema->storage->txn_rollback;
295 subtest 'remove_from_bundle tests' => sub {
298 $schema->storage->txn_begin;
300 my $host_item = $builder->build_sample_item();
301 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
302 $host_item->add_to_bundle($bundle_item1);
304 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
305 t::lib::Mocks::mock_userenv( { branchcode => $patron->branchcode } );
307 C4::Circulation::AddIssue( $patron->unblessed, $host_item->barcode );
308 throws_ok { $bundle_item1->remove_from_bundle }
309 'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut',
310 'Exception thrown if you try to add an item to a checked out bundle';
311 my ( $doreturn, $messages, $issue ) = C4::Circulation::AddReturn( $host_item->barcode, $host_item->homebranch );
312 $bundle_item1->discard_changes;
314 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
315 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
316 $bundle_item1->discard_changes;
317 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
319 $schema->storage->txn_rollback;
322 subtest 'hidden_in_opac() tests' => sub {
326 $schema->storage->txn_begin;
328 my $item = $builder->build_sample_item({ itemlost => 2 });
331 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
332 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
334 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
335 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
337 # enable hidelostitems to verify correct behaviour
338 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
339 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
341 # disable hidelostitems
342 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
343 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
345 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
347 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
351 $schema->storage->txn_rollback;
354 subtest 'has_pending_hold() tests' => sub {
358 $schema->storage->txn_begin;
360 my $dbh = C4::Context->dbh;
361 my $item = $builder->build_sample_item({ itemlost => 0 });
362 my $itemnumber = $item->itemnumber;
364 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
365 ok( $item->has_pending_hold, "Yes, we have a pending hold");
366 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
367 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
369 $schema->storage->txn_rollback;
372 subtest "as_marc_field() tests" => sub {
374 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
375 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
377 my @schema_columns = $schema->resultset('Item')->result_source->columns;
378 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
380 plan tests => scalar @mapped_columns + 5;
382 $schema->storage->txn_begin;
384 my $item = $builder->build_sample_item;
385 # Make sure it has at least one undefined attribute
386 $item->set({ replacementprice => undef })->store->discard_changes;
388 my $marc_field = $item->as_marc_field;
393 'Generated field set the right tag number'
396 foreach my $column (@mapped_columns) {
397 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
398 is( $marc_field->subfield($tagsubfield),
399 $item->$column, "Value is mapped correctly for column $column" );
402 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
405 tagfield => $itemtag,
409 Koha::MarcSubfieldStructure->new(
412 tagfield => $itemtag,
418 my @unlinked_subfields;
419 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
420 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
422 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
423 Koha::MarcSubfieldStructures->search(
424 { frameworkcode => '', tagfield => $itemtag } )
425 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
427 $marc_field = $item->as_marc_field;
429 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
430 my @subfields = $marc_field->subfields;
431 my $result = all { defined $_->[1] } @subfields;
432 ok( $result, 'There are no undef subfields' );
433 my @ordered_subfields = sort {
434 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
435 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
437 is_deeply(\@subfields, \@ordered_subfields);
439 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
440 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
442 $schema->storage->txn_rollback;
443 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
446 subtest 'pickup_locations() tests' => sub {
450 $schema->storage->txn_begin;
452 my $dbh = C4::Context->dbh;
454 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
455 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
456 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
457 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
458 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
459 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
460 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
461 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
463 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
464 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
467 $library1->branchcode, $library2->branchcode,
468 $library3->branchcode, $library4->branchcode
471 my $item1 = $builder->build_sample_item(
473 homebranch => $library1->branchcode,
474 holdingbranch => $library2->branchcode,
480 my $item3 = $builder->build_sample_item(
482 homebranch => $library3->branchcode,
483 holdingbranch => $library4->branchcode,
485 itype => $item1->itype,
489 Koha::CirculationRules->set_rules(
491 categorycode => undef,
492 itemtype => $item1->itype,
495 reservesallowed => 25,
501 { $item1->pickup_locations }
502 'Koha::Exceptions::MissingParameter',
503 'Exception thrown on missing parameter';
505 is( $@->parameter, 'patron', 'Exception param correctly set' );
507 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
508 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
511 "1-1-from_home_library-any" => 3,
512 "1-1-from_home_library-holdgroup" => 2,
513 "1-1-from_home_library-patrongroup" => 2,
514 "1-1-from_home_library-homebranch" => 1,
515 "1-1-from_home_library-holdingbranch" => 1,
516 "1-1-from_any_library-any" => 3,
517 "1-1-from_any_library-holdgroup" => 2,
518 "1-1-from_any_library-patrongroup" => 2,
519 "1-1-from_any_library-homebranch" => 1,
520 "1-1-from_any_library-holdingbranch" => 1,
521 "1-1-from_local_hold_group-any" => 3,
522 "1-1-from_local_hold_group-holdgroup" => 2,
523 "1-1-from_local_hold_group-patrongroup" => 2,
524 "1-1-from_local_hold_group-homebranch" => 1,
525 "1-1-from_local_hold_group-holdingbranch" => 1,
526 "1-4-from_home_library-any" => 0,
527 "1-4-from_home_library-holdgroup" => 0,
528 "1-4-from_home_library-patrongroup" => 0,
529 "1-4-from_home_library-homebranch" => 0,
530 "1-4-from_home_library-holdingbranch" => 0,
531 "1-4-from_any_library-any" => 3,
532 "1-4-from_any_library-holdgroup" => 2,
533 "1-4-from_any_library-patrongroup" => 1,
534 "1-4-from_any_library-homebranch" => 1,
535 "1-4-from_any_library-holdingbranch" => 1,
536 "1-4-from_local_hold_group-any" => 0,
537 "1-4-from_local_hold_group-holdgroup" => 0,
538 "1-4-from_local_hold_group-patrongroup" => 0,
539 "1-4-from_local_hold_group-homebranch" => 0,
540 "1-4-from_local_hold_group-holdingbranch" => 0,
541 "3-1-from_home_library-any" => 0,
542 "3-1-from_home_library-holdgroup" => 0,
543 "3-1-from_home_library-patrongroup" => 0,
544 "3-1-from_home_library-homebranch" => 0,
545 "3-1-from_home_library-holdingbranch" => 0,
546 "3-1-from_any_library-any" => 3,
547 "3-1-from_any_library-holdgroup" => 1,
548 "3-1-from_any_library-patrongroup" => 2,
549 "3-1-from_any_library-homebranch" => 0,
550 "3-1-from_any_library-holdingbranch" => 1,
551 "3-1-from_local_hold_group-any" => 0,
552 "3-1-from_local_hold_group-holdgroup" => 0,
553 "3-1-from_local_hold_group-patrongroup" => 0,
554 "3-1-from_local_hold_group-homebranch" => 0,
555 "3-1-from_local_hold_group-holdingbranch" => 0,
556 "3-4-from_home_library-any" => 0,
557 "3-4-from_home_library-holdgroup" => 0,
558 "3-4-from_home_library-patrongroup" => 0,
559 "3-4-from_home_library-homebranch" => 0,
560 "3-4-from_home_library-holdingbranch" => 0,
561 "3-4-from_any_library-any" => 3,
562 "3-4-from_any_library-holdgroup" => 1,
563 "3-4-from_any_library-patrongroup" => 1,
564 "3-4-from_any_library-homebranch" => 0,
565 "3-4-from_any_library-holdingbranch" => 1,
566 "3-4-from_local_hold_group-any" => 3,
567 "3-4-from_local_hold_group-holdgroup" => 1,
568 "3-4-from_local_hold_group-patrongroup" => 1,
569 "3-4-from_local_hold_group-homebranch" => 0,
570 "3-4-from_local_hold_group-holdingbranch" => 1
574 my ( $item, $patron, $ha, $hfp, $results ) = @_;
576 Koha::CirculationRules->set_rules(
582 hold_fulfillment_policy => $hfp,
583 returnbranch => 'any'
588 $ha eq 'from_local_hold_group' ? 'holdgroup'
590 $ha eq 'from_any_library' ? 'any'
595 my $pickup_location = $_;
596 grep { $pickup_location->branchcode eq $_ } @branchcodes
597 } $item->pickup_locations( { patron => $patron } )->as_list;
600 scalar(@pl) eq $results->{
601 $item->copynumber . '-'
602 . $patron->firstname . '-'
612 . ', hold_fulfillment_policy: '
616 $item->copynumber . '-'
617 . $patron->firstname . '-'
628 foreach my $item ($item1, $item3) {
629 foreach my $patron ($patron1, $patron4) {
630 #holdallowed 1: homebranch, 2: any, 3: holdgroup
631 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
632 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
633 _doTest($item, $patron, $ha, $hfp, $results);
639 # Now test that branchtransferlimits will further filter the pickup locations
641 my $item_no_ccode = $builder->build_sample_item(
643 homebranch => $library1->branchcode,
644 holdingbranch => $library2->branchcode,
645 itype => $item1->itype,
649 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
650 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
651 Koha::CirculationRules->set_rules(
654 itemtype => $item1->itype,
656 holdallowed => 'from_home_library',
657 hold_fulfillment_policy => 1,
658 returnbranch => 'any'
662 $builder->build_object(
664 class => 'Koha::Item::Transfer::Limits',
666 toBranch => $library1->branchcode,
667 fromBranch => $library2->branchcode,
668 itemtype => $item1->itype,
674 my @pickup_locations = map {
675 my $pickup_location = $_;
676 grep { $pickup_location->branchcode eq $_ } @branchcodes
677 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
679 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
681 $builder->build_object(
683 class => 'Koha::Item::Transfer::Limits',
685 toBranch => $library4->branchcode,
686 fromBranch => $library2->branchcode,
687 itemtype => $item1->itype,
693 @pickup_locations = map {
694 my $pickup_location = $_;
695 grep { $pickup_location->branchcode eq $_ } @branchcodes
696 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
698 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
700 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
701 @pickup_locations = map {
702 my $pickup_location = $_;
703 grep { $pickup_location->branchcode eq $_ } @branchcodes
704 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
705 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
707 @pickup_locations = map {
708 my $pickup_location = $_;
709 grep { $pickup_location->branchcode eq $_ } @branchcodes
710 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
711 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");
713 $builder->build_object(
715 class => 'Koha::Item::Transfer::Limits',
717 toBranch => $library2->branchcode,
718 fromBranch => $library2->branchcode,
720 ccode => $item1->ccode,
725 @pickup_locations = map {
726 my $pickup_location = $_;
727 grep { $pickup_location->branchcode eq $_ } @branchcodes
728 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
729 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
731 $builder->build_object(
733 class => 'Koha::Item::Transfer::Limits',
735 toBranch => $library4->branchcode,
736 fromBranch => $library2->branchcode,
738 ccode => $item1->ccode,
743 @pickup_locations = map {
744 my $pickup_location = $_;
745 grep { $pickup_location->branchcode eq $_ } @branchcodes
746 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
747 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
749 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
751 $schema->storage->txn_rollback;
754 subtest 'request_transfer' => sub {
756 $schema->storage->txn_begin;
758 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
759 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
760 my $item = $builder->build_sample_item(
762 homebranch => $library1->branchcode,
763 holdingbranch => $library2->branchcode,
767 # Mandatory fields tests
768 throws_ok { $item->request_transfer( { to => $library1 } ) }
769 'Koha::Exceptions::MissingParameter',
770 'Exception thrown if `reason` parameter is missing';
772 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
773 'Koha::Exceptions::MissingParameter',
774 'Exception thrown if `to` parameter is missing';
777 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
778 is( ref($transfer), 'Koha::Item::Transfer',
779 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
781 my $original_transfer = $transfer->get_from_storage;
783 # Transfer already in progress
784 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
785 'Koha::Exceptions::Item::Transfer::InQueue',
786 'Exception thrown if transfer is already in progress';
789 is( ref( $exception->transfer ),
790 'Koha::Item::Transfer',
791 'The exception contains the found Koha::Item::Transfer' );
794 my $queued_transfer = $item->request_transfer(
795 { to => $library2, reason => 'Manual', enqueue => 1 } );
796 is( ref($queued_transfer), 'Koha::Item::Transfer',
797 'Koha::Item->request_transfer allowed when enqueue is set' );
798 my $transfers = $item->get_transfers;
799 is($transfers->count, 2, "There are now 2 live transfers in the queue");
800 $transfer = $transfer->get_from_storage;
801 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
802 $queued_transfer->datearrived(dt_from_string)->store();
805 my $replaced_transfer = $item->request_transfer(
806 { to => $library2, reason => 'Manual', replace => 1 } );
807 is( ref($replaced_transfer), 'Koha::Item::Transfer',
808 'Koha::Item->request_transfer allowed when replace is set' );
809 $original_transfer->discard_changes;
810 ok($original_transfer->datecancelled, "Original transfer cancelled");
811 $transfers = $item->get_transfers;
812 is($transfers->count, 1, "There is only 1 live transfer in the queue");
813 $replaced_transfer->datearrived(dt_from_string)->store();
815 # BranchTransferLimits
816 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
817 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
818 my $limit = Koha::Item::Transfer::Limit->new({
819 fromBranch => $library2->branchcode,
820 toBranch => $library1->branchcode,
821 itemtype => $item->effective_itemtype,
824 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
825 'Koha::Exceptions::Item::Transfer::Limit',
826 'Exception thrown if transfer is prevented by limits';
828 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
829 is( ref($forced_transfer), 'Koha::Item::Transfer',
830 'Koha::Item->request_transfer allowed when ignore_limits is set'
833 $schema->storage->txn_rollback;
836 subtest 'deletion' => sub {
839 $schema->storage->txn_begin;
841 my $biblio = $builder->build_sample_biblio();
843 my $item = $builder->build_sample_item(
845 biblionumber => $biblio->biblionumber,
848 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
850 my $deleted_item = $item->move_to_deleted;
851 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
852 ; # FIXME This should be Koha::Deleted::Item
853 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
855 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
856 $item = $builder->build_sample_item(
858 biblionumber => $biblio->biblionumber,
862 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
865 my $library = $builder->build_object({ class => 'Koha::Libraries' });
866 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
867 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
869 my $patron = $builder->build_object({class => 'Koha::Patrons'});
870 $item = $builder->build_sample_item({ library => $library->branchcode });
873 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
876 @{$item->safe_to_delete->messages}[0]->message,
878 'Koha::Item->safe_to_delete reports item on loan',
882 @{$item->safe_to_delete->messages}[0]->message,
884 'item that is on loan cannot be deleted',
888 ! $item->safe_to_delete,
889 'Koha::Item->safe_to_delete shows item NOT safe to delete'
892 AddReturn( $item->barcode, $library->branchcode );
895 t::lib::Mocks::mock_preference('IndependentBranches', 1);
896 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
899 @{$item_2->safe_to_delete->messages}[0]->message,
901 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
905 @{$item_2->safe_to_delete->messages}[0]->message,
907 'IndependentBranches prevents deletion at another branch',
912 { # codeblock to limit scope of $module->mock
914 my $module = Test::MockModule->new('C4::Items');
915 $module->mock( GetAnalyticsCount => sub { return 1 } );
917 $item->discard_changes;
919 @{$item->safe_to_delete->messages}[0]->message,
921 'Koha::Item->safe_to_delete reports linked analytics',
925 @{$item->safe_to_delete->messages}[0]->message,
927 'Linked analytics prevents deletion of item',
933 $item->safe_to_delete,
934 'Koha::Item->safe_to_delete shows item safe to delete'
939 my $test_item = Koha::Items->find( $item->itemnumber );
941 is( $test_item, undef,
942 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
945 subtest 'holds tests' => sub {
950 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
952 $schema->storage->txn_begin;
954 my $item = $builder->build_sample_item;
956 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
957 my $safe_to_delete = $item->safe_to_delete;
959 ok( !$safe_to_delete, 'Cannot delete' );
961 @{ $safe_to_delete->messages }[0]->message,
963 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
968 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
969 $safe_to_delete = $item->safe_to_delete;
971 ok( !$safe_to_delete, 'Cannot delete' );
973 @{ $safe_to_delete->messages }[0]->message,
975 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
980 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
981 $safe_to_delete = $item->safe_to_delete;
983 ok( !$safe_to_delete, 'Cannot delete' );
985 @{ $safe_to_delete->messages }[0]->message,
987 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
992 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
993 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
995 $safe_to_delete = $item->safe_to_delete;
997 ok( !$safe_to_delete );
1000 @{ $safe_to_delete->messages}[0]->message,
1001 'last_item_for_hold',
1002 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
1005 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
1007 ok( $item->safe_to_delete );
1009 $schema->storage->txn_rollback;
1012 $schema->storage->txn_rollback;
1015 subtest 'renewal_branchcode' => sub {
1018 $schema->storage->txn_begin;
1020 my $item = $builder->build_sample_item();
1021 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1022 my $checkout = $builder->build_object({
1023 class => 'Koha::Checkouts',
1025 itemnumber => $item->itemnumber,
1030 C4::Context->interface( 'intranet' );
1031 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1033 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1034 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1035 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1036 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1038 C4::Context->interface( 'opac' );
1040 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1041 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1042 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1044 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1045 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1046 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1048 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1049 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1050 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");
1052 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1053 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1054 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");
1056 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1057 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1058 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");
1060 $schema->storage->txn_rollback;
1063 subtest 'Tests for itemtype' => sub {
1065 $schema->storage->txn_begin;
1067 my $biblio = $builder->build_sample_biblio;
1068 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1069 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1071 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1072 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1073 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1074 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1076 $schema->storage->txn_rollback;
1079 subtest 'get_transfers' => sub {
1081 $schema->storage->txn_begin;
1083 my $item = $builder->build_sample_item();
1085 my $transfers = $item->get_transfers();
1086 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1087 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1089 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1091 my $transfer_1 = $builder->build_object(
1093 class => 'Koha::Item::Transfers',
1095 itemnumber => $item->itemnumber,
1096 frombranch => $item->holdingbranch,
1097 tobranch => $library_to->branchcode,
1100 datearrived => undef,
1101 datecancelled => undef,
1102 daterequested => \'NOW()'
1107 $transfers = $item->get_transfers();
1108 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1110 my $transfer_2 = $builder->build_object(
1112 class => 'Koha::Item::Transfers',
1114 itemnumber => $item->itemnumber,
1115 frombranch => $item->holdingbranch,
1116 tobranch => $library_to->branchcode,
1119 datearrived => undef,
1120 datecancelled => undef,
1121 daterequested => \'NOW()'
1126 my $transfer_3 = $builder->build_object(
1128 class => 'Koha::Item::Transfers',
1130 itemnumber => $item->itemnumber,
1131 frombranch => $item->holdingbranch,
1132 tobranch => $library_to->branchcode,
1135 datearrived => undef,
1136 datecancelled => undef,
1137 daterequested => \'NOW()'
1142 $transfers = $item->get_transfers();
1143 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1144 my $result_1 = $transfers->next;
1145 my $result_2 = $transfers->next;
1146 my $result_3 = $transfers->next;
1147 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1148 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1149 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1151 $transfer_2->datesent(\'NOW()')->store;
1152 $transfers = $item->get_transfers();
1153 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1154 $result_1 = $transfers->next;
1155 $result_2 = $transfers->next;
1156 $result_3 = $transfers->next;
1157 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1158 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1159 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1161 $transfer_2->datearrived(\'NOW()')->store;
1162 $transfers = $item->get_transfers();
1163 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1164 $result_1 = $transfers->next;
1165 $result_2 = $transfers->next;
1166 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1167 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1169 $transfer_1->datecancelled(\'NOW()')->store;
1170 $transfers = $item->get_transfers();
1171 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1172 $result_1 = $transfers->next;
1173 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1175 $schema->storage->txn_rollback;
1178 subtest 'Test for relationship between item and current_branchtransfers' => sub {
1181 $schema->storage->txn_begin;
1183 my $item = $builder->build_sample_item();
1184 my $transfer = $builder->build_object(
1186 class => 'Koha::Item::Transfers',
1188 itemnumber => $item->itemnumber,
1189 datesent => dt_from_string,
1190 datearrived => dt_from_string,
1191 datecancelled => undef,
1196 my $transfer_item = $transfer->item;
1197 my $biblio = Koha::Biblios->find( $transfer_item->biblionumber );
1199 my $current_branchtransfers = Koha::Items->search(
1200 { 'me.itemnumber' => $transfer_item->itemnumber },
1201 { prefetch => ['current_branchtransfers'] }
1204 my $item_with_branchtransfers = $current_branchtransfers->next;
1207 $transfer_item->itemnumber,
1208 $item_with_branchtransfers->itemnumber,
1209 'following two items are the same'
1212 # following two tests should produce the same result
1214 $transfer_item->get_transfer,
1216 'Koha::Item->get_transfer returns undef with no active transfers'
1219 $item_with_branchtransfers->get_transfer, undef,
1220 'prefetched result->get_transfer returns undef with no active transfers'
1225 datearrived => undef,
1229 $current_branchtransfers = Koha::Items->search(
1230 { 'me.itemnumber' => $transfer_item->itemnumber },
1231 { prefetch => ['current_branchtransfers'] }
1234 $item_with_branchtransfers = $current_branchtransfers->next;
1237 $transfer_item->get_transfer->branchtransfer_id,
1238 $item_with_branchtransfers->get_transfer->branchtransfer_id,
1239 'an active transfer produces same branchtransfer_id for both methods'
1242 $schema->storage->txn_rollback;
1245 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1248 $schema->storage->txn_begin;
1250 my $biblio = $builder->build_sample_biblio();
1251 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1253 my $orders = $item->orders;
1254 is ($orders->count, 0, 'No order on this item yet');
1256 my $order_note = 'Order for ' . $item->itemnumber;
1258 my $aq_order1 = $builder->build_object({
1259 class => 'Koha::Acquisition::Orders',
1261 biblionumber => $biblio->biblionumber,
1262 order_internalnote => $order_note,
1265 my $aq_order2 = $builder->build_object({
1266 class => 'Koha::Acquisition::Orders',
1268 biblionumber => $biblio->biblionumber,
1271 my $aq_order_item1 = $builder->build({
1272 source => 'AqordersItem',
1274 ordernumber => $aq_order1->ordernumber,
1275 itemnumber => $item->itemnumber,
1279 $orders = $item->orders;
1280 is ($orders->count, 1, 'One order found by item with the relationship');
1281 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1284 subtest 'move_to_biblio() tests' => sub {
1287 $schema->storage->txn_begin;
1289 my $dbh = C4::Context->dbh;
1291 my $source_biblio = $builder->build_sample_biblio();
1292 my $target_biblio = $builder->build_sample_biblio();
1294 my $source_biblionumber = $source_biblio->biblionumber;
1295 my $target_biblionumber = $target_biblio->biblionumber;
1297 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1298 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1299 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1301 my $itemnumber1 = $item1->itemnumber;
1302 my $itemnumber2 = $item2->itemnumber;
1304 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1306 my $patron = $builder->build_object({
1307 class => 'Koha::Patrons',
1308 value => { branchcode => $library->branchcode }
1310 my $borrowernumber = $patron->borrowernumber;
1312 my $aq_budget = $builder->build({
1313 source => 'Aqbudget',
1315 budget_notes => 'test',
1319 my $aq_order1 = $builder->build_object({
1320 class => 'Koha::Acquisition::Orders',
1322 biblionumber => $source_biblionumber,
1323 budget_id => $aq_budget->{budget_id},
1326 my $aq_order_item1 = $builder->build({
1327 source => 'AqordersItem',
1329 ordernumber => $aq_order1->ordernumber,
1330 itemnumber => $itemnumber1,
1333 my $aq_order2 = $builder->build_object({
1334 class => 'Koha::Acquisition::Orders',
1336 biblionumber => $source_biblionumber,
1337 budget_id => $aq_budget->{budget_id},
1340 my $aq_order_item2 = $builder->build({
1341 source => 'AqordersItem',
1343 ordernumber => $aq_order2->ordernumber,
1344 itemnumber => $itemnumber2,
1348 my $bib_level_hold = $builder->build_object({
1349 class => 'Koha::Holds',
1351 biblionumber => $source_biblionumber,
1352 itemnumber => undef,
1355 my $item_level_hold1 = $builder->build_object({
1356 class => 'Koha::Holds',
1358 biblionumber => $source_biblionumber,
1359 itemnumber => $itemnumber1,
1362 my $item_level_hold2 = $builder->build_object({
1363 class => 'Koha::Holds',
1365 biblionumber => $source_biblionumber,
1366 itemnumber => $itemnumber2,
1370 my $tmp_holdsqueue1 = $builder->build({
1371 source => 'TmpHoldsqueue',
1373 borrowernumber => $borrowernumber,
1374 biblionumber => $source_biblionumber,
1375 itemnumber => $itemnumber1,
1378 my $tmp_holdsqueue2 = $builder->build({
1379 source => 'TmpHoldsqueue',
1381 borrowernumber => $borrowernumber,
1382 biblionumber => $source_biblionumber,
1383 itemnumber => $itemnumber2,
1386 my $hold_fill_target1 = $builder->build({
1387 source => 'HoldFillTarget',
1389 borrowernumber => $borrowernumber,
1390 biblionumber => $source_biblionumber,
1391 itemnumber => $itemnumber1,
1394 my $hold_fill_target2 = $builder->build({
1395 source => 'HoldFillTarget',
1397 borrowernumber => $borrowernumber,
1398 biblionumber => $source_biblionumber,
1399 itemnumber => $itemnumber2,
1402 my $linktracker1 = $builder->build({
1403 source => 'Linktracker',
1405 borrowernumber => $borrowernumber,
1406 biblionumber => $source_biblionumber,
1407 itemnumber => $itemnumber1,
1410 my $linktracker2 = $builder->build({
1411 source => 'Linktracker',
1413 borrowernumber => $borrowernumber,
1414 biblionumber => $source_biblionumber,
1415 itemnumber => $itemnumber2,
1419 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1420 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1422 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1423 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');
1425 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1426 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1427 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1428 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1429 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1430 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1432 $aq_order1->discard_changes;
1433 $aq_order2->discard_changes;
1434 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1435 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1437 $bib_level_hold->discard_changes;
1438 $item_level_hold1->discard_changes;
1439 $item_level_hold2->discard_changes;
1440 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1441 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1442 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1444 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1445 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1446 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1447 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1449 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1450 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1451 # Why does ->biblionumber return a Biblio object???
1452 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1453 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1455 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1456 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1457 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1458 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1460 $schema->storage->txn_rollback;
1463 subtest 'columns_to_str' => sub {
1466 $schema->storage->txn_begin;
1468 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1470 my $cache = Koha::Caches->get_instance();
1471 $cache->clear_from_cache("MarcStructure-0-");
1472 $cache->clear_from_cache("MarcStructure-1-");
1473 $cache->clear_from_cache("MarcSubfieldStructure-");
1474 $cache->clear_from_cache("libraries:name");
1475 $cache->clear_from_cache("itemtype:description:en");
1476 $cache->clear_from_cache("cn_sources:description");
1477 $cache->clear_from_cache("AV_descriptions:LOST");
1479 # Creating subfields 'é', 'è' that are not linked with a kohafield
1480 Koha::MarcSubfieldStructures->search(
1482 frameworkcode => '',
1483 tagfield => $itemtag,
1484 tagsubfield => ['é', 'è'],
1486 )->delete; # In case it exist already
1488 # é is not linked with a AV
1489 # è is linked with AV branches
1490 Koha::MarcSubfieldStructure->new(
1492 frameworkcode => '',
1493 tagfield => $itemtag,
1494 tagsubfield => 'é',
1497 defaultvalue => 'ééé',
1501 Koha::MarcSubfieldStructure->new(
1503 frameworkcode => '',
1504 tagfield => $itemtag,
1505 tagsubfield => 'è',
1508 defaultvalue => 'èèè',
1510 authorised_value => 'branches',
1514 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1515 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1516 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1517 my $dateaccessioned = '2020-12-15';
1518 my $library = Koha::Libraries->search->next;
1519 my $branchcode = $library->branchcode;
1521 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1523 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1524 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1525 xmlns="http://www.loc.gov/MARC21/slim">
1528 <leader> a </leader>
1529 <datafield tag="999" ind1=" " ind2=" ">
1530 <subfield code="é">value é</subfield>
1531 <subfield code="è">$branchcode</subfield>
1539 itemlost => $lost_av->authorised_value,
1540 dateaccessioned => $dateaccessioned,
1541 more_subfields_xml => $some_marc_xml,
1545 Koha::Caches->get_instance->flush_all;
1547 $item = $item->get_from_storage;
1549 my $s = $item->columns_to_str;
1550 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1551 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1552 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1553 is( $s->{'è'}, $library->branchname );
1555 $cache->clear_from_cache("MarcStructure-0-");
1556 $cache->clear_from_cache("MarcStructure-1-");
1557 $cache->clear_from_cache("MarcSubfieldStructure-");
1558 $cache->clear_from_cache("libraries:name");
1559 $cache->clear_from_cache("itemtype:description:en");
1560 $cache->clear_from_cache("cn_sources:description");
1561 $cache->clear_from_cache("AV_descriptions:LOST");
1563 $schema->storage->txn_rollback;
1566 subtest 'strings_map() tests' => sub {
1570 $schema->storage->txn_begin;
1572 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1574 my $cache = Koha::Caches->get_instance();
1575 $cache->clear_from_cache("MarcStructure-0-");
1576 $cache->clear_from_cache("MarcStructure-1-");
1577 $cache->clear_from_cache("MarcSubfieldStructure-");
1578 $cache->clear_from_cache("libraries:name");
1579 $cache->clear_from_cache("itemtype:description:en");
1580 $cache->clear_from_cache("cn_sources:description");
1581 $cache->clear_from_cache("AV_descriptions:LOST");
1583 # Recreating subfields just to be sure tests will be ok
1588 Koha::MarcSubfieldStructures->search(
1590 frameworkcode => '',
1591 tagfield => $itemtag,
1592 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1594 )->delete; # In case it exist already
1596 Koha::MarcSubfieldStructure->new(
1598 authorised_value => 'LOST',
1600 frameworkcode => '',
1601 kohafield => 'items.itemlost',
1604 tagfield => $itemtag,
1608 Koha::MarcSubfieldStructure->new(
1610 authorised_value => 'cn_source',
1612 frameworkcode => '',
1613 kohafield => 'items.cn_source',
1616 tagfield => $itemtag,
1620 Koha::MarcSubfieldStructure->new(
1622 authorised_value => '',
1624 frameworkcode => '',
1625 kohafield => 'items.materials',
1628 tagfield => $itemtag,
1632 Koha::MarcSubfieldStructure->new(
1634 authorised_value => 'branches',
1636 frameworkcode => '',
1637 kohafield => 'items.homebranch',
1640 tagfield => $itemtag,
1644 Koha::MarcSubfieldStructure->new(
1646 authorised_value => 'itemtypes',
1648 frameworkcode => '',
1649 kohafield => 'items.itype',
1652 tagfield => $itemtag,
1657 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1658 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1659 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1660 my $item = $builder->build_sample_item(
1662 biblionumber => $biblio->id,
1663 library => $library->id
1667 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1668 my $lost_av = $builder->build_object(
1670 class => 'Koha::AuthorisedValues',
1672 authorised_value => 3,
1674 lib => 'internal description',
1675 lib_opac => 'public description',
1680 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1681 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1682 my $class_source = $builder->build_object(
1684 class => 'Koha::ClassSources',
1686 class_sort_rule => $class_sort_rule->class_sort_rule,
1687 class_split_rule => $class_split_rule->class_split_rule,
1692 Koha::Caches->get_instance->flush_all;
1696 cn_source => $class_source->id,
1697 itemlost => $lost_av->authorised_value,
1698 itype => $itype->itemtype,
1699 materials => 'Suff',
1701 )->store->discard_changes;
1703 my $strings = $item->strings_map;
1705 subtest 'unmapped field tests' => sub {
1709 ok( !exists $strings->{materials}, "Unmapped field not present" );
1712 subtest 'av handling' => sub {
1716 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1717 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1718 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1719 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1722 subtest 'cn_source handling' => sub {
1726 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1727 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1728 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1731 subtest 'branches handling' => sub {
1735 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1736 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1737 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1740 subtest 'itemtype handling' => sub {
1744 ok( exists $strings->{itype}, "'itype' entry exists" );
1745 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1746 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1749 subtest 'public flag tests' => sub {
1753 $strings = $item->strings_map( { public => 1 } );
1755 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1756 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1757 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1758 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1761 $cache->clear_from_cache("MarcStructure-0-");
1762 $cache->clear_from_cache("MarcStructure-1-");
1763 $cache->clear_from_cache("MarcSubfieldStructure-");
1764 $cache->clear_from_cache("libraries:name");
1765 $cache->clear_from_cache("itemtype:description:en");
1766 $cache->clear_from_cache("cn_sources:description");
1768 $schema->storage->txn_rollback;
1771 subtest 'store() tests' => sub {
1775 subtest 'dateaccessioned handling' => sub {
1779 $schema->storage->txn_begin;
1781 my $item = $builder->build_sample_item;
1783 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1785 # reset dateaccessioned on the DB
1786 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1787 $item->discard_changes;
1789 ok( !defined $item->dateaccessioned );
1792 $item->replacementprice(100)->store->discard_changes;
1794 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1796 $schema->storage->txn_rollback;
1799 subtest '_set_found_trigger() tests' => sub {
1803 $schema->storage->txn_begin;
1805 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1806 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1808 # Add a lost item debit
1809 my $debit = $patron->account->add_debit(
1813 item_id => $item->id,
1814 interface => 'intranet',
1818 # Add a lost item processing fee
1819 my $processing_debit = $patron->account->add_debit(
1822 type => 'PROCESSING',
1823 item_id => $item->id,
1824 interface => 'intranet',
1828 my $lostreturn_policy = {
1829 lostreturn => 'charge',
1830 processingreturn => 'refund'
1833 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1834 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1836 # simulate it was found
1837 $item->set( { itemlost => 0 } )->store;
1839 my $messages = $item->object_messages;
1841 my $message_1 = $messages->[0];
1843 is( $message_1->type, 'info', 'type is correct' );
1844 is( $message_1->message, 'lost_refunded', 'message is correct' );
1846 # Find the refund credit
1847 my $credit = $debit->credits->next;
1850 $message_1->payload,
1851 { credit_id => $credit->id },
1855 my $message_2 = $messages->[1];
1857 is( $message_2->type, 'info', 'type is correct' );
1858 is( $message_2->message, 'lost_charge', 'message is correct' );
1859 is( $message_2->payload, undef, 'no payload' );
1861 my $message_3 = $messages->[2];
1862 is( $message_3->message, 'processing_refunded', 'message is correct' );
1864 my $processing_credit = $processing_debit->credits->next;
1866 $message_3->payload,
1867 { credit_id => $processing_credit->id },
1871 # Let's build a new item
1872 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1873 $item->set( { itemlost => 0 } )->store;
1875 $messages = $item->object_messages;
1876 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1878 $schema->storage->txn_rollback;
1881 subtest 'holds_queue update tests' => sub {
1885 $schema->storage->txn_begin;
1887 my $biblio = $builder->build_sample_biblio;
1889 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1890 $mock->mock( 'enqueue', sub {
1891 my ( $self, $args ) = @_;
1893 $args->{biblio_ids},
1895 '->store triggers a holds queue update for the related biblio'
1899 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1902 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1905 $item->set({ reserves => 1 })->store;
1907 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1909 $item->set({ reserves => 0 })->store;
1911 $schema->storage->txn_rollback;
1915 subtest 'Recalls tests' => sub {
1919 $schema->storage->txn_begin;
1921 my $item1 = $builder->build_sample_item;
1922 my $biblio = $item1->biblio;
1923 my $branchcode = $item1->holdingbranch;
1924 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1925 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1926 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1927 my $item2 = $builder->build_object(
1928 { class => 'Koha::Items',
1929 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1933 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1934 t::lib::Mocks::mock_preference('UseRecalls', 1);
1936 my $recall1 = Koha::Recall->new(
1937 { patron_id => $patron1->borrowernumber,
1938 created_date => \'NOW()',
1939 biblio_id => $biblio->biblionumber,
1940 pickup_library_id => $branchcode,
1941 item_id => $item1->itemnumber,
1942 expiration_date => undef,
1946 my $recall2 = Koha::Recall->new(
1947 { patron_id => $patron2->borrowernumber,
1948 created_date => \'NOW()',
1949 biblio_id => $biblio->biblionumber,
1950 pickup_library_id => $branchcode,
1951 item_id => $item1->itemnumber,
1952 expiration_date => undef,
1957 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1959 $recall2->set_cancelled;
1961 t::lib::Mocks::mock_preference('UseRecalls', 0);
1962 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1964 t::lib::Mocks::mock_preference("UseRecalls", 1);
1966 $item1->update({ notforloan => 1 });
1967 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1968 $item1->update({ notforloan => 0, itemlost => 1 });
1969 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1970 $item1->update({ itemlost => 0, withdrawn => 1 });
1971 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1972 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1974 $item1->update({ withdrawn => 0 });
1975 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1977 Koha::CirculationRules->set_rules({
1978 branchcode => $branchcode,
1979 categorycode => $patron1->categorycode,
1980 itemtype => $item1->effective_itemtype,
1982 recalls_allowed => 0,
1983 recalls_per_record => 1,
1984 on_shelf_recalls => 'all',
1987 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1989 Koha::CirculationRules->set_rules({
1990 branchcode => $branchcode,
1991 categorycode => $patron1->categorycode,
1992 itemtype => $item1->effective_itemtype,
1994 recalls_allowed => 1,
1995 recalls_per_record => 1,
1996 on_shelf_recalls => 'all',
1999 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
2000 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
2001 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
2003 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
2004 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
2005 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
2007 $recall1->set_cancelled;
2008 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
2010 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
2012 Koha::CirculationRules->set_rules({
2013 branchcode => $branchcode,
2014 categorycode => $patron1->categorycode,
2015 itemtype => $item1->effective_itemtype,
2017 recalls_allowed => 1,
2018 recalls_per_record => 1,
2019 on_shelf_recalls => 'any',
2022 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
2023 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
2025 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
2026 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
2028 $recall1 = Koha::Recall->new(
2029 { patron_id => $patron1->borrowernumber,
2030 created_date => \'NOW()',
2031 biblio_id => $biblio->biblionumber,
2032 pickup_library_id => $branchcode,
2034 expiration_date => undef,
2039 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
2041 Koha::CirculationRules->set_rules({
2042 branchcode => $branchcode,
2043 categorycode => $patron1->categorycode,
2044 itemtype => $item1->effective_itemtype,
2046 recalls_allowed => 0,
2047 recalls_per_record => 1,
2048 on_shelf_recalls => 'any',
2051 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
2053 Koha::CirculationRules->set_rules({
2054 branchcode => $branchcode,
2055 categorycode => $patron1->categorycode,
2056 itemtype => $item1->effective_itemtype,
2058 recalls_allowed => 1,
2059 recalls_per_record => 1,
2060 on_shelf_recalls => 'any',
2063 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
2065 # check_recalls tests
2067 $recall1 = Koha::Recall->new(
2068 { patron_id => $patron2->borrowernumber,
2069 created_date => \'NOW()',
2070 biblio_id => $biblio->biblionumber,
2071 pickup_library_id => $branchcode,
2072 item_id => $item1->itemnumber,
2073 expiration_date => undef,
2077 $recall2 = Koha::Recall->new(
2078 { patron_id => $patron1->borrowernumber,
2079 created_date => \'NOW()',
2080 biblio_id => $biblio->biblionumber,
2081 pickup_library_id => $branchcode,
2083 expiration_date => undef,
2087 $recall2->set_waiting( { item => $item1 } );
2088 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
2090 # return a waiting recall
2091 my $check_recall = $item1->check_recalls;
2092 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
2094 $recall2->revert_waiting;
2096 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
2098 # return recall based on recalldate
2099 $check_recall = $item1->check_recalls;
2100 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
2102 $recall1->set_cancelled;
2104 # return a biblio-level recall
2105 $check_recall = $item1->check_recalls;
2106 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
2108 $recall2->set_cancelled;
2110 $schema->storage->txn_rollback;
2113 subtest 'Notforloan tests' => sub {
2117 $schema->storage->txn_begin;
2119 my $item1 = $builder->build_sample_item;
2120 $item1->update({ notforloan => 0 });
2121 $item1->itemtype->notforloan(0);
2122 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2123 $item1->update({ notforloan => 1 });
2124 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2125 $item1->update({ notforloan => 0 });
2126 $item1->itemtype->update({ notforloan => 1 });
2127 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2129 $schema->storage->txn_rollback;
2132 subtest 'item_group() tests' => sub {
2136 $schema->storage->txn_begin;
2138 my $biblio = $builder->build_sample_biblio();
2139 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2140 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2142 is( $item_1->item_group, undef, 'Item 1 has no item group');
2143 is( $item_2->item_group, undef, 'Item 2 has no item group');
2145 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2146 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2148 $item_group_1->add_item({ item_id => $item_1->id });
2149 $item_group_2->add_item({ item_id => $item_2->id });
2151 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2152 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2154 $schema->storage->txn_rollback;
2157 subtest 'has_pending_recall() tests' => sub {
2161 $schema->storage->txn_begin;
2163 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2164 my $item = $builder->build_sample_item;
2165 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2167 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2168 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2170 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2172 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2174 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2176 $recall->status('waiting')->store;
2178 ok( $item->has_pending_recall, 'The item has a pending recall' );
2180 $schema->storage->txn_rollback;
2183 subtest 'is_denied_renewal' => sub {
2186 $schema->storage->txn_begin;
2188 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2190 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2191 homebranch => $library->branchcode,
2195 itemcallnumber => undef,
2200 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2201 homebranch => $library->branchcode,
2204 location => 'NOPROC'
2209 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2210 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2212 # The wrong column delete should be silently ignored and not trigger item delete
2213 $idr_rules="delete: [yes]\nwithdrawn: [1]";
2214 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2215 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2216 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2218 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2219 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2220 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2222 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2223 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2224 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2225 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2227 $idr_rules="itemcallnumber: [null]";
2228 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2229 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when null in pref' );
2231 $idr_rules="itemcallnumber: ['']";
2232 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2233 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2235 $idr_rules="itemnotes: [null]";
2236 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2237 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when null in pref' );
2239 $idr_rules="itemnotes: ['']";
2240 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2241 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2243 $schema->storage->txn_rollback;
2246 subtest 'current_branchtransfers relationship' => sub {
2249 $schema->storage->txn_begin;
2251 my $biblio = $builder->build_sample_biblio();
2252 my $item = $builder->build_sample_item(
2254 biblionumber => $biblio->biblionumber,
2257 my $transfers = $item->_result->current_branchtransfers;
2258 is( ref($transfers), 'DBIx::Class::ResultSet',
2259 'current_branchtransfers returns a DBIx::Class::ResultSet' );
2260 is( $transfers->count, 0,
2261 "Empty Koha::Item::Transfers set returned if no return_claims" );
2262 my $transfer1 = $builder->build(
2264 source => 'Branchtransfer',
2266 itemnumber => $item->itemnumber,
2267 datearrived => dt_from_string,
2271 my $transfer2 = $builder->build(
2273 source => 'Branchtransfer',
2275 itemnumber => $item->itemnumber,
2276 datearrived => undef,
2277 datecancelled => dt_from_string,
2281 my $transfer3 = $builder->build(
2283 source => 'Branchtransfer',
2285 itemnumber => $item->itemnumber,
2286 datearrived => undef,
2287 datecancelled => undef,
2292 is( $item->_result->current_branchtransfers()->count,
2293 1, "One transfer found for item" );
2295 $schema->storage->txn_rollback;