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 );
36 use Koha::AuthorisedValues;
38 use List::MoreUtils qw(all);
40 use t::lib::TestBuilder;
44 my $schema = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new;
47 subtest 'return_claims relationship' => sub {
50 $schema->storage->txn_begin;
52 my $biblio = $builder->build_sample_biblio();
53 my $item = $builder->build_sample_item({
54 biblionumber => $biblio->biblionumber,
56 my $return_claims = $item->return_claims;
57 is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
58 is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
59 my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
60 my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
62 is($item->return_claims()->count,2,"Two ReturnClaims found for item");
64 $schema->storage->txn_rollback;
67 subtest 'return_claim accessor' => sub {
70 $schema->storage->txn_begin;
72 my $biblio = $builder->build_sample_biblio();
73 my $item = $builder->build_sample_item({
74 biblionumber => $biblio->biblionumber,
76 my $return_claim = $item->return_claim;
77 is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
79 my $claim1 = $builder->build_object(
81 class => 'Koha::Checkouts::ReturnClaims',
82 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
85 my $claim2 = $builder->build_object(
87 class => 'Koha::Checkouts::ReturnClaims',
88 value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
92 $return_claim = $item->return_claim;
93 is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
94 is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
96 $claim2->resolution('test')->store();
97 $return_claim = $item->return_claim;
98 is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
100 $claim1->resolution('test')->store();
101 $return_claim = $item->return_claim;
102 is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
104 $schema->storage->txn_rollback;
107 subtest 'tracked_links relationship' => sub {
110 my $biblio = $builder->build_sample_biblio();
111 my $item = $builder->build_sample_item({
112 biblionumber => $biblio->biblionumber,
114 my $tracked_links = $item->tracked_links;
115 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
116 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
117 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
118 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
120 is($item->tracked_links()->count,2,"Two tracked links found");
123 subtest 'is_bundle tests' => sub {
126 $schema->storage->txn_begin;
128 my $item = $builder->build_sample_item();
130 my $is_bundle = $item->is_bundle;
131 is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
133 my $item2 = $builder->build_sample_item();
134 $schema->resultset('ItemBundle')
135 ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
137 $is_bundle = $item->is_bundle;
138 is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
140 $schema->storage->txn_rollback;
143 subtest 'in_bundle tests' => sub {
146 $schema->storage->txn_begin;
148 my $item = $builder->build_sample_item();
150 my $in_bundle = $item->in_bundle;
151 is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
153 my $host_item = $builder->build_sample_item();
154 $schema->resultset('ItemBundle')
155 ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
157 $in_bundle = $item->in_bundle;
158 is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
160 $schema->storage->txn_rollback;
163 subtest 'bundle_items tests' => sub {
166 $schema->storage->txn_begin;
168 my $host_item = $builder->build_sample_item();
169 my $bundle_items = $host_item->bundle_items;
170 is( ref($bundle_items), 'Koha::Items',
171 'bundle_items returns a Koha::Items object set' );
172 is( $bundle_items->count, 0,
173 'bundle_items set is empty when no items are bundled' );
175 my $bundle_item1 = $builder->build_sample_item();
176 my $bundle_item2 = $builder->build_sample_item();
177 my $bundle_item3 = $builder->build_sample_item();
178 $schema->resultset('ItemBundle')
180 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
181 $schema->resultset('ItemBundle')
183 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
184 $schema->resultset('ItemBundle')
186 { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
188 $bundle_items = $host_item->bundle_items;
189 is( $bundle_items->count, 3,
190 'bundle_items returns all the bundled items in the set' );
192 $schema->storage->txn_rollback;
195 subtest 'bundle_host tests' => sub {
198 $schema->storage->txn_begin;
200 my $host_item = $builder->build_sample_item();
201 my $bundle_item1 = $builder->build_sample_item();
202 my $bundle_item2 = $builder->build_sample_item();
203 $schema->resultset('ItemBundle')
205 { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
207 my $bundle_host = $bundle_item1->bundle_host;
208 is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
209 $bundle_host = $bundle_item2->bundle_host;
210 is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
211 is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
213 $schema->storage->txn_rollback;
216 subtest 'add_to_bundle tests' => sub {
219 $schema->storage->txn_begin;
221 t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
223 my $library = $builder->build_object({ class => 'Koha::Libraries' });
224 t::lib::Mocks::mock_userenv({
225 branchcode => $library->branchcode
228 my $host_item = $builder->build_sample_item();
229 my $bundle_item1 = $builder->build_sample_item();
230 my $bundle_item2 = $builder->build_sample_item();
231 my $bundle_item3 = $builder->build_sample_item();
233 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
235 throws_ok { $host_item->add_to_bundle($host_item) }
236 'Koha::Exceptions::Item::Bundle::IsBundle',
237 'Exception thrown if you try to add the item to itself';
239 my $reserve_id = C4::Reserves::AddReserve(
241 branchcode => $library->branchcode,
242 borrowernumber => $patron->borrowernumber,
243 biblionumber => $bundle_item3->biblionumber,
244 itemnumber => $bundle_item3->itemnumber,
247 throws_ok { $host_item->add_to_bundle($bundle_item3) }
248 'Koha::Exceptions::Item::Bundle::ItemHasHolds',
249 'Exception thrown if you try to add an item with holds to a bundle';
251 ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
252 is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
254 throws_ok { $host_item->add_to_bundle($bundle_item1) }
255 'Koha::Exceptions::Object::DuplicateID',
256 'Exception thrown if you try to add the same item twice';
258 throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
259 'Koha::Exceptions::Item::Bundle::IsBundle',
260 'Exception thrown if you try to add an item to a bundled item';
262 throws_ok { $bundle_item2->add_to_bundle($host_item) }
263 'Koha::Exceptions::Item::Bundle::IsBundle',
264 'Exception thrown if you try to add a bundle host to a bundle item';
266 C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
267 throws_ok { $host_item->add_to_bundle($bundle_item2) }
268 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
269 'Exception thrown if you try to add a checked out item';
271 $bundle_item2->withdrawn(1)->store;
272 t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
273 throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
274 'Koha::Exceptions::Checkin::FailedCheckin',
275 'Exception thrown if you try to add a checked out item using
276 "force_checkin" and the return is not possible';
278 $bundle_item2->withdrawn(0)->store;
279 lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
280 'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
282 $bundle_item2->discard_changes;
283 ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
285 $schema->storage->txn_rollback;
288 subtest 'remove_from_bundle tests' => sub {
291 $schema->storage->txn_begin;
293 my $host_item = $builder->build_sample_item();
294 my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
295 $schema->resultset('ItemBundle')
297 { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
299 is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
300 is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
301 $bundle_item1 = $bundle_item1->get_from_storage;
302 is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
304 $schema->storage->txn_rollback;
307 subtest 'hidden_in_opac() tests' => sub {
311 $schema->storage->txn_begin;
313 my $item = $builder->build_sample_item({ itemlost => 2 });
316 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
317 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
319 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
320 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
322 # enable hidelostitems to verify correct behaviour
323 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
324 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
326 # disable hidelostitems
327 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
328 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
330 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
332 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
336 $schema->storage->txn_rollback;
339 subtest 'has_pending_hold() tests' => sub {
343 $schema->storage->txn_begin;
345 my $dbh = C4::Context->dbh;
346 my $item = $builder->build_sample_item({ itemlost => 0 });
347 my $itemnumber = $item->itemnumber;
349 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
350 ok( $item->has_pending_hold, "Yes, we have a pending hold");
351 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
352 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
354 $schema->storage->txn_rollback;
357 subtest "as_marc_field() tests" => sub {
359 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
360 my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
362 my @schema_columns = $schema->resultset('Item')->result_source->columns;
363 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
365 plan tests => 2 * (scalar @mapped_columns + 1) + 4;
367 $schema->storage->txn_begin;
369 my $item = $builder->build_sample_item;
370 # Make sure it has at least one undefined attribute
371 $item->set({ replacementprice => undef })->store->discard_changes;
373 # Tests with the mss parameter
374 my $marc_field = $item->as_marc_field({ mss => $mss });
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 # Tests without the mss parameter
389 $marc_field = $item->as_marc_field();
394 'Generated field set the right tag number'
397 foreach my $column (@mapped_columns) {
398 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
399 is( $marc_field->subfield($tagsubfield),
400 $item->$column, "Value is mapped correctly for column $column" );
403 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
406 tagfield => $itemtag,
410 Koha::MarcSubfieldStructure->new(
413 tagfield => $itemtag,
419 my @unlinked_subfields;
420 push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
421 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
423 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
424 Koha::MarcSubfieldStructures->search(
425 { frameworkcode => '', tagfield => $itemtag } )
426 ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
428 $marc_field = $item->as_marc_field;
430 my $tagslib = C4::Biblio::GetMarcStructure(1, '');
431 my @subfields = $marc_field->subfields;
432 my $result = all { defined $_->[1] } @subfields;
433 ok( $result, 'There are no undef subfields' );
434 my @ordered_subfields = sort {
435 $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
436 <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
438 is_deeply(\@subfields, \@ordered_subfields);
440 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
441 is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
443 $schema->storage->txn_rollback;
444 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
447 subtest 'pickup_locations() tests' => sub {
451 $schema->storage->txn_begin;
453 my $dbh = C4::Context->dbh;
455 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
456 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
457 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
458 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
459 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
460 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
461 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
462 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
464 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
465 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
468 $library1->branchcode, $library2->branchcode,
469 $library3->branchcode, $library4->branchcode
472 my $item1 = $builder->build_sample_item(
474 homebranch => $library1->branchcode,
475 holdingbranch => $library2->branchcode,
481 my $item3 = $builder->build_sample_item(
483 homebranch => $library3->branchcode,
484 holdingbranch => $library4->branchcode,
486 itype => $item1->itype,
490 Koha::CirculationRules->set_rules(
492 categorycode => undef,
493 itemtype => $item1->itype,
496 reservesallowed => 25,
502 { $item1->pickup_locations }
503 'Koha::Exceptions::MissingParameter',
504 'Exception thrown on missing parameter';
506 is( $@->parameter, 'patron', 'Exception param correctly set' );
508 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
509 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
512 "1-1-from_home_library-any" => 3,
513 "1-1-from_home_library-holdgroup" => 2,
514 "1-1-from_home_library-patrongroup" => 2,
515 "1-1-from_home_library-homebranch" => 1,
516 "1-1-from_home_library-holdingbranch" => 1,
517 "1-1-from_any_library-any" => 3,
518 "1-1-from_any_library-holdgroup" => 2,
519 "1-1-from_any_library-patrongroup" => 2,
520 "1-1-from_any_library-homebranch" => 1,
521 "1-1-from_any_library-holdingbranch" => 1,
522 "1-1-from_local_hold_group-any" => 3,
523 "1-1-from_local_hold_group-holdgroup" => 2,
524 "1-1-from_local_hold_group-patrongroup" => 2,
525 "1-1-from_local_hold_group-homebranch" => 1,
526 "1-1-from_local_hold_group-holdingbranch" => 1,
527 "1-4-from_home_library-any" => 0,
528 "1-4-from_home_library-holdgroup" => 0,
529 "1-4-from_home_library-patrongroup" => 0,
530 "1-4-from_home_library-homebranch" => 0,
531 "1-4-from_home_library-holdingbranch" => 0,
532 "1-4-from_any_library-any" => 3,
533 "1-4-from_any_library-holdgroup" => 2,
534 "1-4-from_any_library-patrongroup" => 1,
535 "1-4-from_any_library-homebranch" => 1,
536 "1-4-from_any_library-holdingbranch" => 1,
537 "1-4-from_local_hold_group-any" => 0,
538 "1-4-from_local_hold_group-holdgroup" => 0,
539 "1-4-from_local_hold_group-patrongroup" => 0,
540 "1-4-from_local_hold_group-homebranch" => 0,
541 "1-4-from_local_hold_group-holdingbranch" => 0,
542 "3-1-from_home_library-any" => 0,
543 "3-1-from_home_library-holdgroup" => 0,
544 "3-1-from_home_library-patrongroup" => 0,
545 "3-1-from_home_library-homebranch" => 0,
546 "3-1-from_home_library-holdingbranch" => 0,
547 "3-1-from_any_library-any" => 3,
548 "3-1-from_any_library-holdgroup" => 1,
549 "3-1-from_any_library-patrongroup" => 2,
550 "3-1-from_any_library-homebranch" => 0,
551 "3-1-from_any_library-holdingbranch" => 1,
552 "3-1-from_local_hold_group-any" => 0,
553 "3-1-from_local_hold_group-holdgroup" => 0,
554 "3-1-from_local_hold_group-patrongroup" => 0,
555 "3-1-from_local_hold_group-homebranch" => 0,
556 "3-1-from_local_hold_group-holdingbranch" => 0,
557 "3-4-from_home_library-any" => 0,
558 "3-4-from_home_library-holdgroup" => 0,
559 "3-4-from_home_library-patrongroup" => 0,
560 "3-4-from_home_library-homebranch" => 0,
561 "3-4-from_home_library-holdingbranch" => 0,
562 "3-4-from_any_library-any" => 3,
563 "3-4-from_any_library-holdgroup" => 1,
564 "3-4-from_any_library-patrongroup" => 1,
565 "3-4-from_any_library-homebranch" => 0,
566 "3-4-from_any_library-holdingbranch" => 1,
567 "3-4-from_local_hold_group-any" => 3,
568 "3-4-from_local_hold_group-holdgroup" => 1,
569 "3-4-from_local_hold_group-patrongroup" => 1,
570 "3-4-from_local_hold_group-homebranch" => 0,
571 "3-4-from_local_hold_group-holdingbranch" => 1
575 my ( $item, $patron, $ha, $hfp, $results ) = @_;
577 Koha::CirculationRules->set_rules(
583 hold_fulfillment_policy => $hfp,
584 returnbranch => 'any'
589 $ha eq 'from_local_hold_group' ? 'holdgroup'
591 $ha eq 'from_any_library' ? 'any'
596 my $pickup_location = $_;
597 grep { $pickup_location->branchcode eq $_ } @branchcodes
598 } $item->pickup_locations( { patron => $patron } )->as_list;
601 scalar(@pl) eq $results->{
602 $item->copynumber . '-'
603 . $patron->firstname . '-'
613 . ', hold_fulfillment_policy: '
617 $item->copynumber . '-'
618 . $patron->firstname . '-'
629 foreach my $item ($item1, $item3) {
630 foreach my $patron ($patron1, $patron4) {
631 #holdallowed 1: homebranch, 2: any, 3: holdgroup
632 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
633 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
634 _doTest($item, $patron, $ha, $hfp, $results);
640 # Now test that branchtransferlimits will further filter the pickup locations
642 my $item_no_ccode = $builder->build_sample_item(
644 homebranch => $library1->branchcode,
645 holdingbranch => $library2->branchcode,
646 itype => $item1->itype,
650 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
651 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
652 Koha::CirculationRules->set_rules(
655 itemtype => $item1->itype,
657 holdallowed => 'from_home_library',
658 hold_fulfillment_policy => 1,
659 returnbranch => 'any'
663 $builder->build_object(
665 class => 'Koha::Item::Transfer::Limits',
667 toBranch => $library1->branchcode,
668 fromBranch => $library2->branchcode,
669 itemtype => $item1->itype,
675 my @pickup_locations = map {
676 my $pickup_location = $_;
677 grep { $pickup_location->branchcode eq $_ } @branchcodes
678 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
680 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
682 $builder->build_object(
684 class => 'Koha::Item::Transfer::Limits',
686 toBranch => $library4->branchcode,
687 fromBranch => $library2->branchcode,
688 itemtype => $item1->itype,
694 @pickup_locations = map {
695 my $pickup_location = $_;
696 grep { $pickup_location->branchcode eq $_ } @branchcodes
697 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
699 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
701 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
702 @pickup_locations = map {
703 my $pickup_location = $_;
704 grep { $pickup_location->branchcode eq $_ } @branchcodes
705 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
706 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
708 @pickup_locations = map {
709 my $pickup_location = $_;
710 grep { $pickup_location->branchcode eq $_ } @branchcodes
711 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
712 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");
714 $builder->build_object(
716 class => 'Koha::Item::Transfer::Limits',
718 toBranch => $library2->branchcode,
719 fromBranch => $library2->branchcode,
721 ccode => $item1->ccode,
726 @pickup_locations = map {
727 my $pickup_location = $_;
728 grep { $pickup_location->branchcode eq $_ } @branchcodes
729 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
730 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
732 $builder->build_object(
734 class => 'Koha::Item::Transfer::Limits',
736 toBranch => $library4->branchcode,
737 fromBranch => $library2->branchcode,
739 ccode => $item1->ccode,
744 @pickup_locations = map {
745 my $pickup_location = $_;
746 grep { $pickup_location->branchcode eq $_ } @branchcodes
747 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
748 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
750 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
752 $schema->storage->txn_rollback;
755 subtest 'request_transfer' => sub {
757 $schema->storage->txn_begin;
759 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
760 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
761 my $item = $builder->build_sample_item(
763 homebranch => $library1->branchcode,
764 holdingbranch => $library2->branchcode,
768 # Mandatory fields tests
769 throws_ok { $item->request_transfer( { to => $library1 } ) }
770 'Koha::Exceptions::MissingParameter',
771 'Exception thrown if `reason` parameter is missing';
773 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
774 'Koha::Exceptions::MissingParameter',
775 'Exception thrown if `to` parameter is missing';
778 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
779 is( ref($transfer), 'Koha::Item::Transfer',
780 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
782 my $original_transfer = $transfer->get_from_storage;
784 # Transfer already in progress
785 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
786 'Koha::Exceptions::Item::Transfer::InQueue',
787 'Exception thrown if transfer is already in progress';
790 is( ref( $exception->transfer ),
791 'Koha::Item::Transfer',
792 'The exception contains the found Koha::Item::Transfer' );
795 my $queued_transfer = $item->request_transfer(
796 { to => $library2, reason => 'Manual', enqueue => 1 } );
797 is( ref($queued_transfer), 'Koha::Item::Transfer',
798 'Koha::Item->request_transfer allowed when enqueue is set' );
799 my $transfers = $item->get_transfers;
800 is($transfers->count, 2, "There are now 2 live transfers in the queue");
801 $transfer = $transfer->get_from_storage;
802 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
803 $queued_transfer->datearrived(dt_from_string)->store();
806 my $replaced_transfer = $item->request_transfer(
807 { to => $library2, reason => 'Manual', replace => 1 } );
808 is( ref($replaced_transfer), 'Koha::Item::Transfer',
809 'Koha::Item->request_transfer allowed when replace is set' );
810 $original_transfer->discard_changes;
811 ok($original_transfer->datecancelled, "Original transfer cancelled");
812 $transfers = $item->get_transfers;
813 is($transfers->count, 1, "There is only 1 live transfer in the queue");
814 $replaced_transfer->datearrived(dt_from_string)->store();
816 # BranchTransferLimits
817 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
818 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
819 my $limit = Koha::Item::Transfer::Limit->new({
820 fromBranch => $library2->branchcode,
821 toBranch => $library1->branchcode,
822 itemtype => $item->effective_itemtype,
825 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
826 'Koha::Exceptions::Item::Transfer::Limit',
827 'Exception thrown if transfer is prevented by limits';
829 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
830 is( ref($forced_transfer), 'Koha::Item::Transfer',
831 'Koha::Item->request_transfer allowed when ignore_limits is set'
834 $schema->storage->txn_rollback;
837 subtest 'deletion' => sub {
840 $schema->storage->txn_begin;
842 my $biblio = $builder->build_sample_biblio();
844 my $item = $builder->build_sample_item(
846 biblionumber => $biblio->biblionumber,
849 is( $item->deleted_on, undef, 'deleted_on not set for new item' );
851 my $deleted_item = $item->move_to_deleted;
852 is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
853 ; # FIXME This should be Koha::Deleted::Item
854 is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
856 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
857 $item = $builder->build_sample_item(
859 biblionumber => $biblio->biblionumber,
863 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
866 my $library = $builder->build_object({ class => 'Koha::Libraries' });
867 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
868 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
870 my $patron = $builder->build_object({class => 'Koha::Patrons'});
871 $item = $builder->build_sample_item({ library => $library->branchcode });
874 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
877 @{$item->safe_to_delete->messages}[0]->message,
879 'Koha::Item->safe_to_delete reports item on loan',
883 @{$item->safe_to_delete->messages}[0]->message,
885 'item that is on loan cannot be deleted',
889 ! $item->safe_to_delete,
890 'Koha::Item->safe_to_delete shows item NOT safe to delete'
893 AddReturn( $item->barcode, $library->branchcode );
896 t::lib::Mocks::mock_preference('IndependentBranches', 1);
897 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
900 @{$item_2->safe_to_delete->messages}[0]->message,
902 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
906 @{$item_2->safe_to_delete->messages}[0]->message,
908 'IndependentBranches prevents deletion at another branch',
913 { # codeblock to limit scope of $module->mock
915 my $module = Test::MockModule->new('C4::Items');
916 $module->mock( GetAnalyticsCount => sub { return 1 } );
918 $item->discard_changes;
920 @{$item->safe_to_delete->messages}[0]->message,
922 'Koha::Item->safe_to_delete reports linked analytics',
926 @{$item->safe_to_delete->messages}[0]->message,
928 'Linked analytics prevents deletion of item',
934 $item->safe_to_delete,
935 'Koha::Item->safe_to_delete shows item safe to delete'
940 my $test_item = Koha::Items->find( $item->itemnumber );
942 is( $test_item, undef,
943 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
946 subtest 'holds tests' => sub {
951 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
953 $schema->storage->txn_begin;
955 my $item = $builder->build_sample_item;
957 my $processing = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'P' } } );
958 my $safe_to_delete = $item->safe_to_delete;
960 ok( !$safe_to_delete, 'Cannot delete' );
962 @{ $safe_to_delete->messages }[0]->message,
964 'Koha::Item->safe_to_delete reports a in processing hold blocks deletion'
969 my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'T' } } );
970 $safe_to_delete = $item->safe_to_delete;
972 ok( !$safe_to_delete, 'Cannot delete' );
974 @{ $safe_to_delete->messages }[0]->message,
976 'Koha::Item->safe_to_delete reports a in transit hold blocks deletion'
981 my $waiting = $builder->build_object( { class => 'Koha::Holds', value => { itemnumber => $item->id, itemnumber => $item->id, found => 'W' } } );
982 $safe_to_delete = $item->safe_to_delete;
984 ok( !$safe_to_delete, 'Cannot delete' );
986 @{ $safe_to_delete->messages }[0]->message,
988 'Koha::Item->safe_to_delete reports a waiting hold blocks deletion'
993 # Add am unfilled biblio-level hold to catch the 'last_item_for_hold' use case
994 $builder->build_object( { class => 'Koha::Holds', value => { biblionumber => $item->biblionumber, itemnumber => undef, found => undef } } );
996 $safe_to_delete = $item->safe_to_delete;
998 ok( !$safe_to_delete );
1001 @{ $safe_to_delete->messages}[0]->message,
1002 'last_item_for_hold',
1003 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
1006 my $extra_item = $builder->build_sample_item({ biblionumber => $item->biblionumber });
1008 ok( $item->safe_to_delete );
1010 $schema->storage->txn_rollback;
1013 $schema->storage->txn_rollback;
1016 subtest 'renewal_branchcode' => sub {
1019 $schema->storage->txn_begin;
1021 my $item = $builder->build_sample_item();
1022 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1023 my $checkout = $builder->build_object({
1024 class => 'Koha::Checkouts',
1026 itemnumber => $item->itemnumber,
1031 C4::Context->interface( 'intranet' );
1032 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1034 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1035 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1036 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1037 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1039 C4::Context->interface( 'opac' );
1041 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1042 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1043 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1045 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1046 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1047 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1049 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1050 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1051 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");
1053 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1054 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1055 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");
1057 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1058 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1059 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");
1061 $schema->storage->txn_rollback;
1064 subtest 'Tests for itemtype' => sub {
1066 $schema->storage->txn_begin;
1068 my $biblio = $builder->build_sample_biblio;
1069 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1070 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
1072 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1073 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
1074 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1075 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
1077 $schema->storage->txn_rollback;
1080 subtest 'get_transfers' => sub {
1082 $schema->storage->txn_begin;
1084 my $item = $builder->build_sample_item();
1086 my $transfers = $item->get_transfers();
1087 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1088 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
1090 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
1092 my $transfer_1 = $builder->build_object(
1094 class => 'Koha::Item::Transfers',
1096 itemnumber => $item->itemnumber,
1097 frombranch => $item->holdingbranch,
1098 tobranch => $library_to->branchcode,
1101 datearrived => undef,
1102 datecancelled => undef,
1103 daterequested => \'NOW()'
1108 $transfers = $item->get_transfers();
1109 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1111 my $transfer_2 = $builder->build_object(
1113 class => 'Koha::Item::Transfers',
1115 itemnumber => $item->itemnumber,
1116 frombranch => $item->holdingbranch,
1117 tobranch => $library_to->branchcode,
1120 datearrived => undef,
1121 datecancelled => undef,
1122 daterequested => \'NOW()'
1127 my $transfer_3 = $builder->build_object(
1129 class => 'Koha::Item::Transfers',
1131 itemnumber => $item->itemnumber,
1132 frombranch => $item->holdingbranch,
1133 tobranch => $library_to->branchcode,
1136 datearrived => undef,
1137 datecancelled => undef,
1138 daterequested => \'NOW()'
1143 $transfers = $item->get_transfers();
1144 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1145 my $result_1 = $transfers->next;
1146 my $result_2 = $transfers->next;
1147 my $result_3 = $transfers->next;
1148 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1149 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1150 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1152 $transfer_2->datesent(\'NOW()')->store;
1153 $transfers = $item->get_transfers();
1154 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1155 $result_1 = $transfers->next;
1156 $result_2 = $transfers->next;
1157 $result_3 = $transfers->next;
1158 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1159 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1160 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1162 $transfer_2->datearrived(\'NOW()')->store;
1163 $transfers = $item->get_transfers();
1164 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1165 $result_1 = $transfers->next;
1166 $result_2 = $transfers->next;
1167 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1168 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1170 $transfer_1->datecancelled(\'NOW()')->store;
1171 $transfers = $item->get_transfers();
1172 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1173 $result_1 = $transfers->next;
1174 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1176 $schema->storage->txn_rollback;
1179 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1182 $schema->storage->txn_begin;
1184 my $biblio = $builder->build_sample_biblio();
1185 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1187 my $orders = $item->orders;
1188 is ($orders->count, 0, 'No order on this item yet');
1190 my $order_note = 'Order for ' . $item->itemnumber;
1192 my $aq_order1 = $builder->build_object({
1193 class => 'Koha::Acquisition::Orders',
1195 biblionumber => $biblio->biblionumber,
1196 order_internalnote => $order_note,
1199 my $aq_order2 = $builder->build_object({
1200 class => 'Koha::Acquisition::Orders',
1202 biblionumber => $biblio->biblionumber,
1205 my $aq_order_item1 = $builder->build({
1206 source => 'AqordersItem',
1208 ordernumber => $aq_order1->ordernumber,
1209 itemnumber => $item->itemnumber,
1213 $orders = $item->orders;
1214 is ($orders->count, 1, 'One order found by item with the relationship');
1215 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1218 subtest 'move_to_biblio() tests' => sub {
1221 $schema->storage->txn_begin;
1223 my $dbh = C4::Context->dbh;
1225 my $source_biblio = $builder->build_sample_biblio();
1226 my $target_biblio = $builder->build_sample_biblio();
1228 my $source_biblionumber = $source_biblio->biblionumber;
1229 my $target_biblionumber = $target_biblio->biblionumber;
1231 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1232 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1233 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1235 my $itemnumber1 = $item1->itemnumber;
1236 my $itemnumber2 = $item2->itemnumber;
1238 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1240 my $patron = $builder->build_object({
1241 class => 'Koha::Patrons',
1242 value => { branchcode => $library->branchcode }
1244 my $borrowernumber = $patron->borrowernumber;
1246 my $aq_budget = $builder->build({
1247 source => 'Aqbudget',
1249 budget_notes => 'test',
1253 my $aq_order1 = $builder->build_object({
1254 class => 'Koha::Acquisition::Orders',
1256 biblionumber => $source_biblionumber,
1257 budget_id => $aq_budget->{budget_id},
1260 my $aq_order_item1 = $builder->build({
1261 source => 'AqordersItem',
1263 ordernumber => $aq_order1->ordernumber,
1264 itemnumber => $itemnumber1,
1267 my $aq_order2 = $builder->build_object({
1268 class => 'Koha::Acquisition::Orders',
1270 biblionumber => $source_biblionumber,
1271 budget_id => $aq_budget->{budget_id},
1274 my $aq_order_item2 = $builder->build({
1275 source => 'AqordersItem',
1277 ordernumber => $aq_order2->ordernumber,
1278 itemnumber => $itemnumber2,
1282 my $bib_level_hold = $builder->build_object({
1283 class => 'Koha::Holds',
1285 biblionumber => $source_biblionumber,
1286 itemnumber => undef,
1289 my $item_level_hold1 = $builder->build_object({
1290 class => 'Koha::Holds',
1292 biblionumber => $source_biblionumber,
1293 itemnumber => $itemnumber1,
1296 my $item_level_hold2 = $builder->build_object({
1297 class => 'Koha::Holds',
1299 biblionumber => $source_biblionumber,
1300 itemnumber => $itemnumber2,
1304 my $tmp_holdsqueue1 = $builder->build({
1305 source => 'TmpHoldsqueue',
1307 borrowernumber => $borrowernumber,
1308 biblionumber => $source_biblionumber,
1309 itemnumber => $itemnumber1,
1312 my $tmp_holdsqueue2 = $builder->build({
1313 source => 'TmpHoldsqueue',
1315 borrowernumber => $borrowernumber,
1316 biblionumber => $source_biblionumber,
1317 itemnumber => $itemnumber2,
1320 my $hold_fill_target1 = $builder->build({
1321 source => 'HoldFillTarget',
1323 borrowernumber => $borrowernumber,
1324 biblionumber => $source_biblionumber,
1325 itemnumber => $itemnumber1,
1328 my $hold_fill_target2 = $builder->build({
1329 source => 'HoldFillTarget',
1331 borrowernumber => $borrowernumber,
1332 biblionumber => $source_biblionumber,
1333 itemnumber => $itemnumber2,
1336 my $linktracker1 = $builder->build({
1337 source => 'Linktracker',
1339 borrowernumber => $borrowernumber,
1340 biblionumber => $source_biblionumber,
1341 itemnumber => $itemnumber1,
1344 my $linktracker2 = $builder->build({
1345 source => 'Linktracker',
1347 borrowernumber => $borrowernumber,
1348 biblionumber => $source_biblionumber,
1349 itemnumber => $itemnumber2,
1353 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1354 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1356 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1357 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');
1359 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1360 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1361 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1362 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1363 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1364 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1366 $aq_order1->discard_changes;
1367 $aq_order2->discard_changes;
1368 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1369 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1371 $bib_level_hold->discard_changes;
1372 $item_level_hold1->discard_changes;
1373 $item_level_hold2->discard_changes;
1374 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1375 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1376 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1378 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1379 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1380 is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1381 is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1383 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1384 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1385 # Why does ->biblionumber return a Biblio object???
1386 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1387 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1389 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1390 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1391 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1392 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1394 $schema->storage->txn_rollback;
1397 subtest 'columns_to_str' => sub {
1400 $schema->storage->txn_begin;
1402 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1404 my $cache = Koha::Caches->get_instance();
1405 $cache->clear_from_cache("MarcStructure-0-");
1406 $cache->clear_from_cache("MarcStructure-1-");
1407 $cache->clear_from_cache("MarcSubfieldStructure-");
1408 $cache->clear_from_cache("libraries:name");
1409 $cache->clear_from_cache("itemtype:description:en");
1410 $cache->clear_from_cache("cn_sources:description");
1411 $cache->clear_from_cache("AV_descriptions:LOST");
1413 # Creating subfields 'é', 'è' that are not linked with a kohafield
1414 Koha::MarcSubfieldStructures->search(
1416 frameworkcode => '',
1417 tagfield => $itemtag,
1418 tagsubfield => ['é', 'è'],
1420 )->delete; # In case it exist already
1422 # é is not linked with a AV
1423 # è is linked with AV branches
1424 Koha::MarcSubfieldStructure->new(
1426 frameworkcode => '',
1427 tagfield => $itemtag,
1428 tagsubfield => 'é',
1431 defaultvalue => 'ééé',
1435 Koha::MarcSubfieldStructure->new(
1437 frameworkcode => '',
1438 tagfield => $itemtag,
1439 tagsubfield => 'è',
1442 defaultvalue => 'èèè',
1444 authorised_value => 'branches',
1448 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1449 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1450 my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1451 my $dateaccessioned = '2020-12-15';
1452 my $library = Koha::Libraries->search->next;
1453 my $branchcode = $library->branchcode;
1455 my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1457 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1458 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1459 xmlns="http://www.loc.gov/MARC21/slim">
1462 <leader> a </leader>
1463 <datafield tag="999" ind1=" " ind2=" ">
1464 <subfield code="é">value é</subfield>
1465 <subfield code="è">$branchcode</subfield>
1473 itemlost => $lost_av->authorised_value,
1474 dateaccessioned => $dateaccessioned,
1475 more_subfields_xml => $some_marc_xml,
1479 Koha::Caches->get_instance->flush_all;
1481 $item = $item->get_from_storage;
1483 my $s = $item->columns_to_str;
1484 is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1485 is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1486 is( $s->{'é'}, 'value é', 'subfield ok with more than a-Z');
1487 is( $s->{'è'}, $library->branchname );
1489 $cache->clear_from_cache("MarcStructure-0-");
1490 $cache->clear_from_cache("MarcStructure-1-");
1491 $cache->clear_from_cache("MarcSubfieldStructure-");
1492 $cache->clear_from_cache("libraries:name");
1493 $cache->clear_from_cache("itemtype:description:en");
1494 $cache->clear_from_cache("cn_sources:description");
1495 $cache->clear_from_cache("AV_descriptions:LOST");
1497 $schema->storage->txn_rollback;
1500 subtest 'strings_map() tests' => sub {
1504 $schema->storage->txn_begin;
1506 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
1508 my $cache = Koha::Caches->get_instance();
1509 $cache->clear_from_cache("MarcStructure-0-");
1510 $cache->clear_from_cache("MarcStructure-1-");
1511 $cache->clear_from_cache("MarcSubfieldStructure-");
1512 $cache->clear_from_cache("libraries:name");
1513 $cache->clear_from_cache("itemtype:description:en");
1514 $cache->clear_from_cache("cn_sources:description");
1515 $cache->clear_from_cache("AV_descriptions:LOST");
1517 # Recreating subfields just to be sure tests will be ok
1522 Koha::MarcSubfieldStructures->search(
1524 frameworkcode => '',
1525 tagfield => $itemtag,
1526 tagsubfield => [ '1', '2', '3', 'a', 'y' ],
1528 )->delete; # In case it exist already
1530 Koha::MarcSubfieldStructure->new(
1532 authorised_value => 'LOST',
1534 frameworkcode => '',
1535 kohafield => 'items.itemlost',
1538 tagfield => $itemtag,
1542 Koha::MarcSubfieldStructure->new(
1544 authorised_value => 'cn_source',
1546 frameworkcode => '',
1547 kohafield => 'items.cn_source',
1550 tagfield => $itemtag,
1554 Koha::MarcSubfieldStructure->new(
1556 authorised_value => '',
1558 frameworkcode => '',
1559 kohafield => 'items.materials',
1562 tagfield => $itemtag,
1566 Koha::MarcSubfieldStructure->new(
1568 authorised_value => 'branches',
1570 frameworkcode => '',
1571 kohafield => 'items.homebranch',
1574 tagfield => $itemtag,
1578 Koha::MarcSubfieldStructure->new(
1580 authorised_value => 'itemtypes',
1582 frameworkcode => '',
1583 kohafield => 'items.itype',
1586 tagfield => $itemtag,
1591 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1592 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1593 my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
1594 my $item = $builder->build_sample_item(
1596 biblionumber => $biblio->id,
1597 library => $library->id
1601 Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
1602 my $lost_av = $builder->build_object(
1604 class => 'Koha::AuthorisedValues',
1606 authorised_value => 3,
1608 lib => 'internal description',
1609 lib_opac => 'public description',
1614 my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
1615 my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
1616 my $class_source = $builder->build_object(
1618 class => 'Koha::ClassSources',
1620 class_sort_rule => $class_sort_rule->class_sort_rule,
1621 class_split_rule => $class_split_rule->class_split_rule,
1626 Koha::Caches->get_instance->flush_all;
1630 cn_source => $class_source->id,
1631 itemlost => $lost_av->authorised_value,
1632 itype => $itype->itemtype,
1633 materials => 'Suff',
1635 )->store->discard_changes;
1637 my $strings = $item->strings_map;
1639 subtest 'unmapped field tests' => sub {
1643 ok( !exists $strings->{materials}, "Unmapped field not present" );
1646 subtest 'av handling' => sub {
1650 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1651 is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
1652 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1653 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1656 subtest 'cn_source handling' => sub {
1660 ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
1661 is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
1662 is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
1665 subtest 'branches handling' => sub {
1669 ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
1670 is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
1671 is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
1674 subtest 'itemtype handling' => sub {
1678 ok( exists $strings->{itype}, "'itype' entry exists" );
1679 is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
1680 is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
1683 subtest 'public flag tests' => sub {
1687 $strings = $item->strings_map( { public => 1 } );
1689 ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
1690 is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
1691 is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
1692 is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
1695 $cache->clear_from_cache("MarcStructure-0-");
1696 $cache->clear_from_cache("MarcStructure-1-");
1697 $cache->clear_from_cache("MarcSubfieldStructure-");
1698 $cache->clear_from_cache("libraries:name");
1699 $cache->clear_from_cache("itemtype:description:en");
1700 $cache->clear_from_cache("cn_sources:description");
1702 $schema->storage->txn_rollback;
1705 subtest 'store() tests' => sub {
1709 subtest 'dateaccessioned handling' => sub {
1713 $schema->storage->txn_begin;
1715 my $item = $builder->build_sample_item;
1717 ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1719 # reset dateaccessioned on the DB
1720 $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1721 $item->discard_changes;
1723 ok( !defined $item->dateaccessioned );
1726 $item->replacementprice(100)->store->discard_changes;
1728 ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1730 $schema->storage->txn_rollback;
1733 subtest '_set_found_trigger() tests' => sub {
1737 $schema->storage->txn_begin;
1739 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1740 my $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1742 # Add a lost item debit
1743 my $debit = $patron->account->add_debit(
1747 item_id => $item->id,
1748 interface => 'intranet',
1752 # Add a lost item processing fee
1753 my $processing_debit = $patron->account->add_debit(
1756 type => 'PROCESSING',
1757 item_id => $item->id,
1758 interface => 'intranet',
1762 my $lostreturn_policy = {
1763 lostreturn => 'charge',
1764 processingreturn => 'refund'
1767 my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1768 $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1770 # simulate it was found
1771 $item->set( { itemlost => 0 } )->store;
1773 my $messages = $item->object_messages;
1775 my $message_1 = $messages->[0];
1777 is( $message_1->type, 'info', 'type is correct' );
1778 is( $message_1->message, 'lost_refunded', 'message is correct' );
1780 # Find the refund credit
1781 my $credit = $debit->credits->next;
1784 $message_1->payload,
1785 { credit_id => $credit->id },
1789 my $message_2 = $messages->[1];
1791 is( $message_2->type, 'info', 'type is correct' );
1792 is( $message_2->message, 'lost_charge', 'message is correct' );
1793 is( $message_2->payload, undef, 'no payload' );
1795 my $message_3 = $messages->[2];
1796 is( $message_3->message, 'processing_refunded', 'message is correct' );
1798 my $processing_credit = $processing_debit->credits->next;
1800 $message_3->payload,
1801 { credit_id => $processing_credit->id },
1805 # Let's build a new item
1806 $item = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1807 $item->set( { itemlost => 0 } )->store;
1809 $messages = $item->object_messages;
1810 is( scalar @{$messages}, 0, 'This item has no history, no associated lost fines, presumed not lost by patron, no messages returned');
1812 $schema->storage->txn_rollback;
1815 subtest 'holds_queue update tests' => sub {
1819 $schema->storage->txn_begin;
1821 my $biblio = $builder->build_sample_biblio;
1823 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1824 $mock->mock( 'enqueue', sub {
1825 my ( $self, $args ) = @_;
1827 $args->{biblio_ids},
1829 '->store triggers a holds queue update for the related biblio'
1833 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1836 my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1839 $item->set({ reserves => 1 })->store;
1841 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1843 $item->set({ reserves => 0 })->store;
1845 $schema->storage->txn_rollback;
1849 subtest 'Recalls tests' => sub {
1853 $schema->storage->txn_begin;
1855 my $item1 = $builder->build_sample_item;
1856 my $biblio = $item1->biblio;
1857 my $branchcode = $item1->holdingbranch;
1858 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1859 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1860 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1861 my $item2 = $builder->build_object(
1862 { class => 'Koha::Items',
1863 value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1867 t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1868 t::lib::Mocks::mock_preference('UseRecalls', 1);
1870 my $recall1 = Koha::Recall->new(
1871 { patron_id => $patron1->borrowernumber,
1872 created_date => \'NOW()',
1873 biblio_id => $biblio->biblionumber,
1874 pickup_library_id => $branchcode,
1875 item_id => $item1->itemnumber,
1876 expiration_date => undef,
1880 my $recall2 = Koha::Recall->new(
1881 { patron_id => $patron2->borrowernumber,
1882 created_date => \'NOW()',
1883 biblio_id => $biblio->biblionumber,
1884 pickup_library_id => $branchcode,
1885 item_id => $item1->itemnumber,
1886 expiration_date => undef,
1891 is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1893 $recall2->set_cancelled;
1895 t::lib::Mocks::mock_preference('UseRecalls', 0);
1896 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1898 t::lib::Mocks::mock_preference("UseRecalls", 1);
1900 $item1->update({ notforloan => 1 });
1901 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1902 $item1->update({ notforloan => 0, itemlost => 1 });
1903 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1904 $item1->update({ itemlost => 0, withdrawn => 1 });
1905 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1906 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1908 $item1->update({ withdrawn => 0 });
1909 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1911 Koha::CirculationRules->set_rules({
1912 branchcode => $branchcode,
1913 categorycode => $patron1->categorycode,
1914 itemtype => $item1->effective_itemtype,
1916 recalls_allowed => 0,
1917 recalls_per_record => 1,
1918 on_shelf_recalls => 'all',
1921 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1923 Koha::CirculationRules->set_rules({
1924 branchcode => $branchcode,
1925 categorycode => $patron1->categorycode,
1926 itemtype => $item1->effective_itemtype,
1928 recalls_allowed => 1,
1929 recalls_per_record => 1,
1930 on_shelf_recalls => 'all',
1933 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1934 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1935 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1937 my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1938 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1939 C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1941 $recall1->set_cancelled;
1942 is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1944 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1946 Koha::CirculationRules->set_rules({
1947 branchcode => $branchcode,
1948 categorycode => $patron1->categorycode,
1949 itemtype => $item1->effective_itemtype,
1951 recalls_allowed => 1,
1952 recalls_per_record => 1,
1953 on_shelf_recalls => 'any',
1956 C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1957 is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1959 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1960 is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1962 $recall1 = Koha::Recall->new(
1963 { patron_id => $patron1->borrowernumber,
1964 created_date => \'NOW()',
1965 biblio_id => $biblio->biblionumber,
1966 pickup_library_id => $branchcode,
1968 expiration_date => undef,
1973 # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1975 Koha::CirculationRules->set_rules({
1976 branchcode => $branchcode,
1977 categorycode => $patron1->categorycode,
1978 itemtype => $item1->effective_itemtype,
1980 recalls_allowed => 0,
1981 recalls_per_record => 1,
1982 on_shelf_recalls => 'any',
1985 is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1987 Koha::CirculationRules->set_rules({
1988 branchcode => $branchcode,
1989 categorycode => $patron1->categorycode,
1990 itemtype => $item1->effective_itemtype,
1992 recalls_allowed => 1,
1993 recalls_per_record => 1,
1994 on_shelf_recalls => 'any',
1997 is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1999 # check_recalls tests
2001 $recall1 = Koha::Recall->new(
2002 { patron_id => $patron2->borrowernumber,
2003 created_date => \'NOW()',
2004 biblio_id => $biblio->biblionumber,
2005 pickup_library_id => $branchcode,
2006 item_id => $item1->itemnumber,
2007 expiration_date => undef,
2011 $recall2 = Koha::Recall->new(
2012 { patron_id => $patron1->borrowernumber,
2013 created_date => \'NOW()',
2014 biblio_id => $biblio->biblionumber,
2015 pickup_library_id => $branchcode,
2017 expiration_date => undef,
2021 $recall2->set_waiting( { item => $item1 } );
2022 is( $item1->has_pending_recall, 1, 'Item has pending recall' );
2024 # return a waiting recall
2025 my $check_recall = $item1->check_recalls;
2026 is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
2028 $recall2->revert_waiting;
2030 is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
2032 # return recall based on recalldate
2033 $check_recall = $item1->check_recalls;
2034 is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
2036 $recall1->set_cancelled;
2038 # return a biblio-level recall
2039 $check_recall = $item1->check_recalls;
2040 is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
2042 $recall2->set_cancelled;
2044 $schema->storage->txn_rollback;
2047 subtest 'Notforloan tests' => sub {
2051 $schema->storage->txn_begin;
2053 my $item1 = $builder->build_sample_item;
2054 $item1->update({ notforloan => 0 });
2055 $item1->itemtype->notforloan(0);
2056 is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
2057 $item1->update({ notforloan => 1 });
2058 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
2059 $item1->update({ notforloan => 0 });
2060 $item1->itemtype->update({ notforloan => 1 });
2061 is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
2063 $schema->storage->txn_rollback;
2066 subtest 'item_group() tests' => sub {
2070 $schema->storage->txn_begin;
2072 my $biblio = $builder->build_sample_biblio();
2073 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2074 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
2076 is( $item_1->item_group, undef, 'Item 1 has no item group');
2077 is( $item_2->item_group, undef, 'Item 2 has no item group');
2079 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2080 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
2082 $item_group_1->add_item({ item_id => $item_1->id });
2083 $item_group_2->add_item({ item_id => $item_2->id });
2085 is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
2086 is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
2088 $schema->storage->txn_rollback;
2091 subtest 'has_pending_recall() tests' => sub {
2095 $schema->storage->txn_begin;
2097 my $library = $builder->build_object({ class => 'Koha::Libraries' });
2098 my $item = $builder->build_sample_item;
2099 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
2101 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
2102 t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
2104 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
2106 my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
2108 ok( !$item->has_pending_recall, 'The item has no pending recalls' );
2110 $recall->status('waiting')->store;
2112 ok( $item->has_pending_recall, 'The item has a pending recall' );
2114 $schema->storage->txn_rollback;
2117 subtest 'is_denied_renewal' => sub {
2120 $schema->storage->txn_begin;
2122 my $library = $builder->build_object({ class => 'Koha::Libraries'});
2124 my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2125 homebranch => $library->branchcode,
2129 itemcallnumber => undef,
2134 my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2135 homebranch => $library->branchcode,
2138 location => 'NOPROC'
2143 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2144 is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
2146 $idr_rules="withdrawn: [1]";
2147 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2148 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
2149 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2151 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2152 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2153 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2155 $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2156 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2157 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2158 is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2160 $idr_rules="itemcallnumber: [NULL]";
2161 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2162 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when NULL in pref' );
2164 $idr_rules="itemcallnumber: ['']";
2165 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2166 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
2168 $idr_rules="itemnotes: [NULL]";
2169 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2170 is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when NULL in pref' );
2172 $idr_rules="itemnotes: ['']";
2173 C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
2174 is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
2176 $schema->storage->txn_rollback;