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>.
22 use Test::More tests => 12;
25 use C4::Biblio qw( GetMarcSubfieldStructure );
26 use C4::Circulation qw( AddIssue AddReturn );
33 use List::MoreUtils qw(all);
35 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
41 subtest 'tracked_links relationship' => sub {
44 my $biblio = $builder->build_sample_biblio();
45 my $item = $builder->build_sample_item({
46 biblionumber => $biblio->biblionumber,
48 my $tracked_links = $item->tracked_links;
49 is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
50 is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
51 my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
52 my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
54 is($item->tracked_links()->count,2,"Two tracked links found");
57 subtest 'hidden_in_opac() tests' => sub {
61 $schema->storage->txn_begin;
63 my $item = $builder->build_sample_item({ itemlost => 2 });
66 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
67 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
69 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
70 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
72 # enable hidelostitems to verify correct behaviour
73 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
74 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
76 # disable hidelostitems
77 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
78 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
80 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
82 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
86 $schema->storage->txn_rollback;
89 subtest 'has_pending_hold() tests' => sub {
93 $schema->storage->txn_begin;
95 my $dbh = C4::Context->dbh;
96 my $item = $builder->build_sample_item({ itemlost => 0 });
97 my $itemnumber = $item->itemnumber;
99 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
100 ok( $item->has_pending_hold, "Yes, we have a pending hold");
101 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
102 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
104 $schema->storage->txn_rollback;
107 subtest "as_marc_field() tests" => sub {
109 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
111 my @schema_columns = $schema->resultset('Item')->result_source->columns;
112 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
114 plan tests => 2 * (scalar @mapped_columns + 1) + 2;
116 $schema->storage->txn_begin;
118 my $item = $builder->build_sample_item;
119 # Make sure it has at least one undefined attribute
120 $item->set({ replacementprice => undef })->store->discard_changes;
122 # Tests with the mss parameter
123 my $marc_field = $item->as_marc_field({ mss => $mss });
127 $mss->{'items.itemnumber'}[0]->{tagfield},
128 'Generated field set the right tag number'
131 foreach my $column ( @mapped_columns ) {
132 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
133 is( $marc_field->subfield($tagsubfield),
134 $item->$column, "Value is mapped correctly for column $column" );
137 # Tests without the mss parameter
138 $marc_field = $item->as_marc_field();
142 $mss->{'items.itemnumber'}[0]->{tagfield},
143 'Generated field set the right tag number'
146 foreach my $column (@mapped_columns) {
147 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
148 is( $marc_field->subfield($tagsubfield),
149 $item->$column, "Value is mapped correctly for column $column" );
152 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
155 tagfield => $mss->{'items.itemnumber'}[0]->{tagfield},
160 $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
161 my @unlinked_subfields;
162 push @unlinked_subfields, X => 'Something weird';
163 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
165 $marc_field = $item->as_marc_field;
167 my @subfields = $marc_field->subfields;
168 my $result = all { defined $_->[1] } @subfields;
169 ok( $result, 'There are no undef subfields' );
171 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
173 $schema->storage->txn_rollback;
176 subtest 'pickup_locations' => sub {
179 $schema->storage->txn_begin;
181 my $dbh = C4::Context->dbh;
183 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
184 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
185 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
186 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
187 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
188 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
189 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
190 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
192 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
193 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
196 $library1->branchcode, $library2->branchcode,
197 $library3->branchcode, $library4->branchcode
200 my $item1 = $builder->build_sample_item(
202 homebranch => $library1->branchcode,
203 holdingbranch => $library2->branchcode,
209 my $item3 = $builder->build_sample_item(
211 homebranch => $library3->branchcode,
212 holdingbranch => $library4->branchcode,
214 itype => $item1->itype,
218 Koha::CirculationRules->set_rules(
220 categorycode => undef,
221 itemtype => $item1->itype,
224 reservesallowed => 25,
230 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
231 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
234 "1-1-from_home_library-any" => 3,
235 "1-1-from_home_library-holdgroup" => 2,
236 "1-1-from_home_library-patrongroup" => 2,
237 "1-1-from_home_library-homebranch" => 1,
238 "1-1-from_home_library-holdingbranch" => 1,
239 "1-1-from_any_library-any" => 3,
240 "1-1-from_any_library-holdgroup" => 2,
241 "1-1-from_any_library-patrongroup" => 2,
242 "1-1-from_any_library-homebranch" => 1,
243 "1-1-from_any_library-holdingbranch" => 1,
244 "1-1-from_local_hold_group-any" => 3,
245 "1-1-from_local_hold_group-holdgroup" => 2,
246 "1-1-from_local_hold_group-patrongroup" => 2,
247 "1-1-from_local_hold_group-homebranch" => 1,
248 "1-1-from_local_hold_group-holdingbranch" => 1,
249 "1-4-from_home_library-any" => 0,
250 "1-4-from_home_library-holdgroup" => 0,
251 "1-4-from_home_library-patrongroup" => 0,
252 "1-4-from_home_library-homebranch" => 0,
253 "1-4-from_home_library-holdingbranch" => 0,
254 "1-4-from_any_library-any" => 3,
255 "1-4-from_any_library-holdgroup" => 2,
256 "1-4-from_any_library-patrongroup" => 1,
257 "1-4-from_any_library-homebranch" => 1,
258 "1-4-from_any_library-holdingbranch" => 1,
259 "1-4-from_local_hold_group-any" => 0,
260 "1-4-from_local_hold_group-holdgroup" => 0,
261 "1-4-from_local_hold_group-patrongroup" => 0,
262 "1-4-from_local_hold_group-homebranch" => 0,
263 "1-4-from_local_hold_group-holdingbranch" => 0,
264 "3-1-from_home_library-any" => 0,
265 "3-1-from_home_library-holdgroup" => 0,
266 "3-1-from_home_library-patrongroup" => 0,
267 "3-1-from_home_library-homebranch" => 0,
268 "3-1-from_home_library-holdingbranch" => 0,
269 "3-1-from_any_library-any" => 3,
270 "3-1-from_any_library-holdgroup" => 1,
271 "3-1-from_any_library-patrongroup" => 2,
272 "3-1-from_any_library-homebranch" => 0,
273 "3-1-from_any_library-holdingbranch" => 1,
274 "3-1-from_local_hold_group-any" => 0,
275 "3-1-from_local_hold_group-holdgroup" => 0,
276 "3-1-from_local_hold_group-patrongroup" => 0,
277 "3-1-from_local_hold_group-homebranch" => 0,
278 "3-1-from_local_hold_group-holdingbranch" => 0,
279 "3-4-from_home_library-any" => 0,
280 "3-4-from_home_library-holdgroup" => 0,
281 "3-4-from_home_library-patrongroup" => 0,
282 "3-4-from_home_library-homebranch" => 0,
283 "3-4-from_home_library-holdingbranch" => 0,
284 "3-4-from_any_library-any" => 3,
285 "3-4-from_any_library-holdgroup" => 1,
286 "3-4-from_any_library-patrongroup" => 1,
287 "3-4-from_any_library-homebranch" => 0,
288 "3-4-from_any_library-holdingbranch" => 1,
289 "3-4-from_local_hold_group-any" => 3,
290 "3-4-from_local_hold_group-holdgroup" => 1,
291 "3-4-from_local_hold_group-patrongroup" => 1,
292 "3-4-from_local_hold_group-homebranch" => 0,
293 "3-4-from_local_hold_group-holdingbranch" => 1
297 my ( $item, $patron, $ha, $hfp, $results ) = @_;
299 Koha::CirculationRules->set_rules(
305 hold_fulfillment_policy => $hfp,
306 returnbranch => 'any'
311 $ha eq 'from_local_hold_group' ? 'holdgroup'
313 $ha eq 'from_any_library' ? 'any'
318 my $pickup_location = $_;
319 grep { $pickup_location->branchcode eq $_ } @branchcodes
320 } $item->pickup_locations( { patron => $patron } )->as_list;
323 scalar(@pl) eq $results->{
324 $item->copynumber . '-'
325 . $patron->firstname . '-'
335 . ', hold_fulfillment_policy: '
339 $item->copynumber . '-'
340 . $patron->firstname . '-'
351 foreach my $item ($item1, $item3) {
352 foreach my $patron ($patron1, $patron4) {
353 #holdallowed 1: homebranch, 2: any, 3: holdgroup
354 foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
355 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
356 _doTest($item, $patron, $ha, $hfp, $results);
362 # Now test that branchtransferlimits will further filter the pickup locations
364 my $item_no_ccode = $builder->build_sample_item(
366 homebranch => $library1->branchcode,
367 holdingbranch => $library2->branchcode,
368 itype => $item1->itype,
372 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
373 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
374 Koha::CirculationRules->set_rules(
377 itemtype => $item1->itype,
379 holdallowed => 'from_home_library',
380 hold_fulfillment_policy => 1,
381 returnbranch => 'any'
385 $builder->build_object(
387 class => 'Koha::Item::Transfer::Limits',
389 toBranch => $library1->branchcode,
390 fromBranch => $library2->branchcode,
391 itemtype => $item1->itype,
397 my @pickup_locations = map {
398 my $pickup_location = $_;
399 grep { $pickup_location->branchcode eq $_ } @branchcodes
400 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
402 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
404 $builder->build_object(
406 class => 'Koha::Item::Transfer::Limits',
408 toBranch => $library4->branchcode,
409 fromBranch => $library2->branchcode,
410 itemtype => $item1->itype,
416 @pickup_locations = map {
417 my $pickup_location = $_;
418 grep { $pickup_location->branchcode eq $_ } @branchcodes
419 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
421 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
423 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
424 @pickup_locations = map {
425 my $pickup_location = $_;
426 grep { $pickup_location->branchcode eq $_ } @branchcodes
427 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
428 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
430 @pickup_locations = map {
431 my $pickup_location = $_;
432 grep { $pickup_location->branchcode eq $_ } @branchcodes
433 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
434 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");
436 $builder->build_object(
438 class => 'Koha::Item::Transfer::Limits',
440 toBranch => $library2->branchcode,
441 fromBranch => $library2->branchcode,
443 ccode => $item1->ccode,
448 @pickup_locations = map {
449 my $pickup_location = $_;
450 grep { $pickup_location->branchcode eq $_ } @branchcodes
451 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
452 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
454 $builder->build_object(
456 class => 'Koha::Item::Transfer::Limits',
458 toBranch => $library4->branchcode,
459 fromBranch => $library2->branchcode,
461 ccode => $item1->ccode,
466 @pickup_locations = map {
467 my $pickup_location = $_;
468 grep { $pickup_location->branchcode eq $_ } @branchcodes
469 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
470 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
472 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
474 $schema->storage->txn_rollback;
477 subtest 'request_transfer' => sub {
479 $schema->storage->txn_begin;
481 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
482 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
483 my $item = $builder->build_sample_item(
485 homebranch => $library1->branchcode,
486 holdingbranch => $library2->branchcode,
490 # Mandatory fields tests
491 throws_ok { $item->request_transfer( { to => $library1 } ) }
492 'Koha::Exceptions::MissingParameter',
493 'Exception thrown if `reason` parameter is missing';
495 throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
496 'Koha::Exceptions::MissingParameter',
497 'Exception thrown if `to` parameter is missing';
500 my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
501 is( ref($transfer), 'Koha::Item::Transfer',
502 'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
504 my $original_transfer = $transfer->get_from_storage;
506 # Transfer already in progress
507 throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
508 'Koha::Exceptions::Item::Transfer::InQueue',
509 'Exception thrown if transfer is already in progress';
512 is( ref( $exception->transfer ),
513 'Koha::Item::Transfer',
514 'The exception contains the found Koha::Item::Transfer' );
517 my $queued_transfer = $item->request_transfer(
518 { to => $library2, reason => 'Manual', enqueue => 1 } );
519 is( ref($queued_transfer), 'Koha::Item::Transfer',
520 'Koha::Item->request_transfer allowed when enqueue is set' );
521 my $transfers = $item->get_transfers;
522 is($transfers->count, 2, "There are now 2 live transfers in the queue");
523 $transfer = $transfer->get_from_storage;
524 is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
525 $queued_transfer->datearrived(dt_from_string)->store();
528 my $replaced_transfer = $item->request_transfer(
529 { to => $library2, reason => 'Manual', replace => 1 } );
530 is( ref($replaced_transfer), 'Koha::Item::Transfer',
531 'Koha::Item->request_transfer allowed when replace is set' );
532 $original_transfer->discard_changes;
533 ok($original_transfer->datecancelled, "Original transfer cancelled");
534 $transfers = $item->get_transfers;
535 is($transfers->count, 1, "There is only 1 live transfer in the queue");
536 $replaced_transfer->datearrived(dt_from_string)->store();
538 # BranchTransferLimits
539 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
540 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
541 my $limit = Koha::Item::Transfer::Limit->new({
542 fromBranch => $library2->branchcode,
543 toBranch => $library1->branchcode,
544 itemtype => $item->effective_itemtype,
547 throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
548 'Koha::Exceptions::Item::Transfer::Limit',
549 'Exception thrown if transfer is prevented by limits';
551 my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
552 is( ref($forced_transfer), 'Koha::Item::Transfer',
553 'Koha::Item->request_transfer allowed when ignore_limits is set'
556 $schema->storage->txn_rollback;
559 subtest 'deletion' => sub {
562 $schema->storage->txn_begin;
564 my $biblio = $builder->build_sample_biblio();
566 my $item = $builder->build_sample_item(
568 biblionumber => $biblio->biblionumber,
572 is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
573 ; # FIXME This should be Koha::Deleted::Item
574 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
575 $item = $builder->build_sample_item(
577 biblionumber => $biblio->biblionumber,
581 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
584 my $library = $builder->build_object({ class => 'Koha::Libraries' });
585 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
586 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
588 my $patron = $builder->build_object({class => 'Koha::Patrons'});
589 $item = $builder->build_sample_item({ library => $library->branchcode });
592 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
595 $item->safe_to_delete,
597 'Koha::Item->safe_to_delete reports item on loan',
603 'item that is on loan cannot be deleted',
606 AddReturn( $item->barcode, $library->branchcode );
608 # book_reserved is tested in t/db_dependent/Reserves.t
611 t::lib::Mocks::mock_preference('IndependentBranches', 1);
612 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
615 $item_2->safe_to_delete,
617 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
621 $item_2->safe_delete,
623 'IndependentBranches prevents deletion at another branch',
628 { # codeblock to limit scope of $module->mock
630 my $module = Test::MockModule->new('C4::Items');
631 $module->mock( GetAnalyticsCount => sub { return 1 } );
633 $item->discard_changes;
635 $item->safe_to_delete,
637 'Koha::Item->safe_to_delete reports linked analytics',
643 'Linked analytics prevents deletion of item',
648 { # last_item_for_hold
649 C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
650 is( $item->safe_to_delete, 'last_item_for_hold', 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio' );
652 # With another item attached to the biblio, the item can be deleted
653 $builder->build_sample_item({ biblionumber => $item->biblionumber });
657 $item->safe_to_delete,
659 'Koha::Item->safe_to_delete shows item safe to delete'
664 my $test_item = Koha::Items->find( $item->itemnumber );
666 is( $test_item, undef,
667 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
670 $schema->storage->txn_rollback;
673 subtest 'renewal_branchcode' => sub {
676 $schema->storage->txn_begin;
678 my $item = $builder->build_sample_item();
679 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
680 my $checkout = $builder->build_object({
681 class => 'Koha::Checkouts',
683 itemnumber => $item->itemnumber,
688 C4::Context->interface( 'intranet' );
689 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
691 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
692 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
693 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
694 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
696 C4::Context->interface( 'opac' );
698 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
699 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
700 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
702 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
703 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
704 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
706 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
707 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
708 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");
710 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
711 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
712 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");
714 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
715 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
716 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");
718 $schema->storage->txn_rollback;
721 subtest 'Tests for itemtype' => sub {
723 $schema->storage->txn_begin;
725 my $biblio = $builder->build_sample_biblio;
726 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
727 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
729 t::lib::Mocks::mock_preference('item-level_itypes', 1);
730 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
731 t::lib::Mocks::mock_preference('item-level_itypes', 0);
732 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
734 $schema->storage->txn_rollback;
737 subtest 'get_transfers' => sub {
739 $schema->storage->txn_begin;
741 my $item = $builder->build_sample_item();
743 my $transfers = $item->get_transfers();
744 is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
745 is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
747 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
749 my $transfer_1 = $builder->build_object(
751 class => 'Koha::Item::Transfers',
753 itemnumber => $item->itemnumber,
754 frombranch => $item->holdingbranch,
755 tobranch => $library_to->branchcode,
758 datearrived => undef,
759 datecancelled => undef,
760 daterequested => \'NOW()'
765 $transfers = $item->get_transfers();
766 is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
768 my $transfer_2 = $builder->build_object(
770 class => 'Koha::Item::Transfers',
772 itemnumber => $item->itemnumber,
773 frombranch => $item->holdingbranch,
774 tobranch => $library_to->branchcode,
777 datearrived => undef,
778 datecancelled => undef,
779 daterequested => \'NOW()'
784 my $transfer_3 = $builder->build_object(
786 class => 'Koha::Item::Transfers',
788 itemnumber => $item->itemnumber,
789 frombranch => $item->holdingbranch,
790 tobranch => $library_to->branchcode,
793 datearrived => undef,
794 datecancelled => undef,
795 daterequested => \'NOW()'
800 $transfers = $item->get_transfers();
801 is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
802 my $result_1 = $transfers->next;
803 my $result_2 = $transfers->next;
804 my $result_3 = $transfers->next;
805 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
806 is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
807 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
809 $transfer_2->datesent(\'NOW()')->store;
810 $transfers = $item->get_transfers();
811 is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
812 $result_1 = $transfers->next;
813 $result_2 = $transfers->next;
814 $result_3 = $transfers->next;
815 is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
816 is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
817 is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
819 $transfer_2->datearrived(\'NOW()')->store;
820 $transfers = $item->get_transfers();
821 is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
822 $result_1 = $transfers->next;
823 $result_2 = $transfers->next;
824 is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
825 is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
827 $transfer_1->datecancelled(\'NOW()')->store;
828 $transfers = $item->get_transfers();
829 is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
830 $result_1 = $transfers->next;
831 is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
833 $schema->storage->txn_rollback;
836 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
839 $schema->storage->txn_begin;
841 my $biblio = $builder->build_sample_biblio();
842 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
844 my $orders = $item->orders;
845 is ($orders->count, 0, 'No order on this item yet');
847 my $order_note = 'Order for ' . $item->itemnumber;
849 my $aq_order1 = $builder->build_object({
850 class => 'Koha::Acquisition::Orders',
852 biblionumber => $biblio->biblionumber,
853 order_internalnote => $order_note,
856 my $aq_order2 = $builder->build_object({
857 class => 'Koha::Acquisition::Orders',
859 biblionumber => $biblio->biblionumber,
862 my $aq_order_item1 = $builder->build({
863 source => 'AqordersItem',
865 ordernumber => $aq_order1->ordernumber,
866 itemnumber => $item->itemnumber,
870 $orders = $item->orders;
871 is ($orders->count, 1, 'One order found by item with the relationship');
872 is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
875 subtest 'move_to_biblio() tests' => sub {
878 $schema->storage->txn_begin;
880 my $dbh = C4::Context->dbh;
882 my $source_biblio = $builder->build_sample_biblio();
883 my $target_biblio = $builder->build_sample_biblio();
885 my $source_biblionumber = $source_biblio->biblionumber;
886 my $target_biblionumber = $target_biblio->biblionumber;
888 my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
889 my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
890 my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
892 my $itemnumber1 = $item1->itemnumber;
893 my $itemnumber2 = $item2->itemnumber;
895 my $library = $builder->build_object({ class => 'Koha::Libraries' });
897 my $patron = $builder->build_object({
898 class => 'Koha::Patrons',
899 value => { branchcode => $library->branchcode }
901 my $borrowernumber = $patron->borrowernumber;
903 my $aq_budget = $builder->build({
904 source => 'Aqbudget',
906 budget_notes => 'test',
910 my $aq_order1 = $builder->build_object({
911 class => 'Koha::Acquisition::Orders',
913 biblionumber => $source_biblionumber,
914 budget_id => $aq_budget->{budget_id},
917 my $aq_order_item1 = $builder->build({
918 source => 'AqordersItem',
920 ordernumber => $aq_order1->ordernumber,
921 itemnumber => $itemnumber1,
924 my $aq_order2 = $builder->build_object({
925 class => 'Koha::Acquisition::Orders',
927 biblionumber => $source_biblionumber,
928 budget_id => $aq_budget->{budget_id},
931 my $aq_order_item2 = $builder->build({
932 source => 'AqordersItem',
934 ordernumber => $aq_order2->ordernumber,
935 itemnumber => $itemnumber2,
939 my $bib_level_hold = $builder->build_object({
940 class => 'Koha::Holds',
942 biblionumber => $source_biblionumber,
946 my $item_level_hold1 = $builder->build_object({
947 class => 'Koha::Holds',
949 biblionumber => $source_biblionumber,
950 itemnumber => $itemnumber1,
953 my $item_level_hold2 = $builder->build_object({
954 class => 'Koha::Holds',
956 biblionumber => $source_biblionumber,
957 itemnumber => $itemnumber2,
961 my $tmp_holdsqueue1 = $builder->build({
962 source => 'TmpHoldsqueue',
964 borrowernumber => $borrowernumber,
965 biblionumber => $source_biblionumber,
966 itemnumber => $itemnumber1,
969 my $tmp_holdsqueue2 = $builder->build({
970 source => 'TmpHoldsqueue',
972 borrowernumber => $borrowernumber,
973 biblionumber => $source_biblionumber,
974 itemnumber => $itemnumber2,
977 my $hold_fill_target1 = $builder->build({
978 source => 'HoldFillTarget',
980 borrowernumber => $borrowernumber,
981 biblionumber => $source_biblionumber,
982 itemnumber => $itemnumber1,
985 my $hold_fill_target2 = $builder->build({
986 source => 'HoldFillTarget',
988 borrowernumber => $borrowernumber,
989 biblionumber => $source_biblionumber,
990 itemnumber => $itemnumber2,
993 my $linktracker1 = $builder->build({
994 source => 'Linktracker',
996 borrowernumber => $borrowernumber,
997 biblionumber => $source_biblionumber,
998 itemnumber => $itemnumber1,
1001 my $linktracker2 = $builder->build({
1002 source => 'Linktracker',
1004 borrowernumber => $borrowernumber,
1005 biblionumber => $source_biblionumber,
1006 itemnumber => $itemnumber2,
1010 my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1011 is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1013 $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1014 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');
1016 my $get_item1 = Koha::Items->find( $item1->itemnumber );
1017 is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1018 my $get_item2 = Koha::Items->find( $item2->itemnumber );
1019 is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1020 my $get_item3 = Koha::Items->find( $item3->itemnumber );
1021 is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1023 $aq_order1->discard_changes;
1024 $aq_order2->discard_changes;
1025 is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1026 is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1028 $bib_level_hold->discard_changes;
1029 $item_level_hold1->discard_changes;
1030 $item_level_hold2->discard_changes;
1031 is($bib_level_hold->biblionumber, $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1032 is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1033 is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1035 my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1036 my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1037 is($get_tmp_holdsqueue1->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1038 is($get_tmp_holdsqueue2->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1040 my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1041 my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1042 # Why does ->biblionumber return a Biblio object???
1043 is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1044 is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1046 my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1047 my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1048 is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1049 is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1051 $schema->storage->txn_rollback;