3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use C4::Items qw( ModItemTransfer GetHiddenItemnumbers GetItemsInfo SearchItems AddItemFromMarc ModItemFromMarc get_hostitemnumbers_of Item2Marc );
23 use C4::Biblio qw( GetMarcFromKohaField EmbedItemsInMarcBiblio GetMarcBiblio AddBiblio );
24 use C4::Circulation qw( AddIssue );
27 use Koha::DateUtils qw( dt_from_string );
30 use Koha::MarcSubfieldStructures;
32 use Koha::AuthorisedValues;
35 use t::lib::TestBuilder;
37 use Test::More tests => 14;
41 my $schema = Koha::Database->new->schema;
42 my $location = 'My Location';
44 subtest 'General Add, Get and Del tests' => sub {
48 $schema->storage->txn_begin;
50 my $builder = t::lib::TestBuilder->new;
51 my $library = $builder->build({
54 my $itemtype = $builder->build({
58 # Create a biblio instance for testing
59 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
60 my $biblio = $builder->build_sample_biblio();
63 my $item = $builder->build_sample_item(
65 biblionumber => $biblio->biblionumber,
66 library => $library->{branchcode},
67 location => $location,
68 itype => $itemtype->{itemtype}
71 my $itemnumber = $item->itemnumber;
72 cmp_ok($item->biblionumber, '==', $biblio->biblionumber, "New item is linked to correct biblionumber.");
73 cmp_ok($item->biblioitemnumber, '==', $biblio->biblioitem->biblioitemnumber, "New item is linked to correct biblioitemnumber.");
76 my $getitem = Koha::Items->find($itemnumber);
77 cmp_ok($getitem->itemnumber, '==', $itemnumber, "Retrieved item has correct itemnumber.");
78 cmp_ok($getitem->biblioitemnumber, '==', $item->biblioitemnumber, "Retrieved item has correct biblioitemnumber."); # We are not testing anything useful here
79 is( $getitem->location, $location, "The location should not have been modified" );
80 is( $getitem->permanent_location, $location, "The permanent_location should have been set to the location value" );
83 # Do not modify anything, and do not explode!
84 $getitem->set({})->store;
86 # Modify item; setting barcode.
87 $getitem->barcode('987654321')->store;
88 my $moditem = Koha::Items->find($itemnumber);
89 cmp_ok($moditem->barcode, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->barcode . '.');
93 my $getdeleted = Koha::Items->find($itemnumber);
94 is($getdeleted, undef, "Item deleted as expected.");
96 $itemnumber = $builder->build_sample_item(
98 biblionumber => $biblio->biblionumber,
99 library => $library->{branchcode},
100 location => $location,
101 permanent_location => 'my permanent location',
102 itype => $itemtype->{itemtype}
105 $getitem = Koha::Items->find($itemnumber);
106 is( $getitem->location, $location, "The location should not have been modified" );
107 is( $getitem->permanent_location, 'my permanent location', "The permanent_location should not have modified" );
109 my $new_location = "New location";
110 $getitem->location($new_location)->store;
111 $getitem = Koha::Items->find($itemnumber);
112 is( $getitem->location, $new_location, "The location should have been set to correct location" );
113 is( $getitem->permanent_location, $new_location, "The permanent_location should have been set to location" );
115 $getitem->location('CART')->store;
116 $getitem = Koha::Items->find($itemnumber);
117 is( $getitem->location, 'CART', "The location should have been set to CART" );
118 is( $getitem->permanent_location, $new_location, "The permanent_location should not have been set to CART" );
120 t::lib::Mocks::mock_preference('item-level_itypes', '1');
121 $getitem = Koha::Items->find($itemnumber);
122 is( $getitem->effective_itemtype, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
123 t::lib::Mocks::mock_preference('item-level_itypes', '0');
124 $getitem = Koha::Items->find($itemnumber);
125 is( $getitem->effective_itemtype, $biblio->biblioitem->itemtype, "Itemtype set correctly when not using item-level_itypes" );
127 $schema->storage->txn_rollback;
130 subtest 'ModItemTransfer tests' => sub {
133 $schema->storage->txn_begin;
135 my $builder = t::lib::TestBuilder->new;
136 my $item = $builder->build_sample_item();
138 my $library1 = $builder->build(
143 my $library2 = $builder->build(
149 ModItemTransfer( $item->itemnumber, $library1->{branchcode},
150 $library2->{branchcode}, 'Manual' );
152 my $transfers = Koha::Item::Transfers->search(
154 itemnumber => $item->itemnumber
158 is( $transfers->count, 1, "One transfer created with ModItemTransfer" );
159 $item->discard_changes;
160 is($item->holdingbranch, $library1->{branchcode}, "Items holding branch was updated to frombranch");
162 ModItemTransfer( $item->itemnumber, $library2->{branchcode},
163 $library1->{branchcode}, 'Manual' );
164 $transfers = Koha::Item::Transfers->search(
165 { itemnumber => $item->itemnumber, },
166 { order_by => { '-asc' => 'branchtransfer_id' } }
169 is($transfers->count, 2, "Second transfer recorded on second call of ModItemTransfer");
170 my $transfer1 = $transfers->next;
171 my $transfer2 = $transfers->next;
172 isnt($transfer1->datecancelled, undef, "First transfer marked as cancelled by ModItemTransfer");
173 like($transfer1->cancellation_reason,qr/^Manual/, "First transfer contains cancellation_reason 'Manual'");
174 is($transfer2->datearrived, undef, "Second transfer is now the active transfer");
175 $item->discard_changes;
176 is($item->holdingbranch, $library2->{branchcode}, "Items holding branch was updated to frombranch");
178 # Check 'reason' is populated when passed
179 ModItemTransfer( $item->itemnumber, $library2->{branchcode},
180 $library1->{branchcode}, "Manual" );
182 $transfers = Koha::Item::Transfers->search(
183 { itemnumber => $item->itemnumber, },
184 { order_by => { '-desc' => 'branchtransfer_id' } }
187 my $transfer3 = $transfers->next;
188 is($transfer3->reason, 'Manual', "Reason set via ModItemTransfer");
190 $schema->storage->txn_rollback;
193 subtest 'GetHiddenItemnumbers tests' => sub {
197 # This sub is controlled by the OpacHiddenItems system preference.
199 $schema->storage->txn_begin;
201 my $builder = t::lib::TestBuilder->new;
202 my $library1 = $builder->build({
206 my $library2 = $builder->build({
209 my $itemtype = $builder->build({
210 source => 'Itemtype',
213 # Create a new biblio
214 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
215 my $biblio = $builder->build_sample_biblio();
218 my $item1_itemnumber = $builder->build_sample_item(
220 biblionumber => $biblio->biblionumber,
221 library => $library1->{branchcode},
223 itype => $itemtype->{itemtype}
226 my $item2_itemnumber = $builder->build_sample_item(
228 biblionumber => $biblio->biblionumber,
229 library => $library2->{branchcode},
231 itype => $itemtype->{itemtype}
235 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
238 push @items, Koha::Items->find( $item1_itemnumber )->unblessed;
239 push @items, Koha::Items->find( $item2_itemnumber )->unblessed;
241 # Empty OpacHiddenItems
242 t::lib::Mocks::mock_preference('OpacHiddenItems','');
243 ok( !defined( GetHiddenItemnumbers( { items => \@items } ) ),
244 "Hidden items list undef if OpacHiddenItems empty");
247 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
248 ok( scalar GetHiddenItemnumbers( { items => \@items } ) == 0,
249 "Hidden items list empty if OpacHiddenItems only contains blanks");
251 # One variable / value
254 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
255 @hidden = GetHiddenItemnumbers( { items => \@items } );
256 ok( scalar @hidden == 1, "Only one hidden item");
257 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
259 # One variable, two values
262 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
263 @hidden = GetHiddenItemnumbers( { items => \@items } );
264 ok( scalar @hidden == 2, "Two items hidden");
265 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
267 # Two variables, a value each
270 homebranch: [$library2->{branchcode}]
272 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
273 @hidden = GetHiddenItemnumbers( { items => \@items } );
274 ok( scalar @hidden == 2, "Two items hidden");
275 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
277 # Override hidden with patron category
278 t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'S' );
279 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'PT' } );
280 ok( scalar @hidden == 2, "Two items still hidden");
281 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'S' } );
282 ok( scalar @hidden == 0, "Two items not hidden");
284 # Valid OpacHiddenItems, empty list
286 @hidden = GetHiddenItemnumbers( { items => \@items } );
287 ok( scalar @hidden == 0, "Empty items list, no item hidden");
289 $schema->storage->txn_rollback;
292 subtest 'GetItemsInfo tests' => sub {
296 $schema->storage->txn_begin;
298 my $builder = t::lib::TestBuilder->new;
299 my $library1 = $builder->build({
302 my $library2 = $builder->build({
305 my $itemtype = $builder->build({
306 source => 'Itemtype',
309 Koha::AuthorisedValues->delete;
310 my $av1 = Koha::AuthorisedValue->new(
312 category => 'RESTRICTED',
313 authorised_value => '1',
314 lib => 'Restricted Access',
315 lib_opac => 'Restricted Access OPAC',
320 my $biblio = $builder->build_sample_biblio();
322 my $itemnumber = $builder->build_sample_item(
324 biblionumber => $biblio->biblionumber,
325 homebranch => $library1->{branchcode},
326 holdingbranch => $library2->{branchcode},
327 itype => $itemtype->{itemtype},
332 my $library = Koha::Libraries->find( $library1->{branchcode} );
333 $library->opac_info("homebranch OPAC info");
336 $library = Koha::Libraries->find( $library2->{branchcode} );
337 $library->opac_info("holdingbranch OPAC info");
340 my @results = GetItemsInfo( $biblio->biblionumber );
341 ok( @results, 'GetItemsInfo returns results');
343 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
344 'GetItemsInfo returns the correct home branch OPAC info notice' );
345 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
346 'GetItemsInfo returns the correct holding branch OPAC info notice' );
347 is( exists( $results[0]->{ onsite_checkout } ), 1,
348 'GetItemsInfo returns a onsite_checkout key' );
349 is( $results[0]->{ restricted }, 1,
350 'GetItemsInfo returns a restricted value code' );
351 is( $results[0]->{ restrictedvalue }, "Restricted Access",
352 'GetItemsInfo returns a restricted value description (staff)' );
353 is( $results[0]->{ restrictedvalueopac }, "Restricted Access OPAC",
354 'GetItemsInfo returns a restricted value description (OPAC)' );
356 #place item into holds queue
357 my $dbh = C4::Context->dbh;
358 @results = GetItemsInfo( $biblio->biblionumber );
359 is( $results[0]->{ has_pending_hold }, "0",
360 'Hold not marked as pending/unavailable if nothing in tmp_holdsqueue for item' );
362 $dbh->do(q{INSERT INTO tmp_holdsqueue (biblionumber, itemnumber, surname, borrowernumber ) VALUES (?, ?, "Zorro", 42)}, undef, $biblio->biblionumber, $itemnumber);
363 @results = GetItemsInfo( $biblio->biblionumber );
364 is( $results[0]->{ has_pending_hold }, "1",
365 'Hold marked as pending/unavailable if tmp_holdsqueue is not empty for item' );
367 $schema->storage->txn_rollback;
370 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
374 $schema->storage->txn_begin;
376 my $biblio = $schema->resultset('Biblio')->create({
377 title => "Test title",
378 datecreated => dt_from_string,
379 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
381 my $biblioitem = $biblio->biblioitems->first;
382 my $item = $schema->resultset('Item')->create({
383 biblioitemnumber => $biblioitem->biblioitemnumber,
384 biblionumber => $biblio->biblionumber,
385 itype => "ITEM_LEVEL",
388 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
389 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
391 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
392 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
394 # If itemtype is not defined and item-level_level item types are set
395 # fallback to biblio-level itemtype (Bug 14651) and warn
396 $item->itype( undef );
398 my $effective_itemtype;
399 warning_is { $effective_itemtype = $item->effective_itemtype() }
400 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
401 '->effective_itemtype() raises a warning when falling back to bib-level';
403 ok( defined $effective_itemtype &&
404 $effective_itemtype eq 'BIB_LEVEL',
405 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
407 $schema->storage->txn_rollback;
410 subtest 'SearchItems test' => sub {
413 $schema->storage->txn_begin;
414 my $dbh = C4::Context->dbh;
415 my $builder = t::lib::TestBuilder->new;
417 my $library1 = $builder->build({
420 my $library2 = $builder->build({
423 my $itemtype = $builder->build({
424 source => 'Itemtype',
427 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
428 my ($cpl_items_before) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
430 my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
431 $builder->build_sample_biblio({ title => 'Silence in the shadow' });
433 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
436 my $item1 = $builder->build_sample_item(
438 biblionumber => $biblio->biblionumber,
439 library => $library1->{branchcode},
440 itype => $itemtype->{itemtype}
443 my $item1_itemnumber = $item1->itemnumber;
444 my $item2_itemnumber = $builder->build_sample_item(
446 biblionumber => $biblio->biblionumber,
447 library => $library2->{branchcode},
448 itype => $itemtype->{itemtype}
452 my ($items, $total_results);
454 ($items, $total_results) = SearchItems();
456 is($total_results, $initial_items_count + 2, "Created 2 new items");
457 is(scalar @$items, $total_results, "SearchItems() returns all items");
459 ($items, $total_results) = SearchItems(undef, {rows => 1});
460 is($total_results, $initial_items_count + 2);
461 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
463 # Search all items where homebranch = 'CPL'
465 field => 'homebranch',
466 query => $library1->{branchcode},
469 ($items, $total_results) = SearchItems($filter);
470 ok($total_results > 0, "There is at least one CPL item");
471 my $all_items_are_CPL = 1;
472 foreach my $item (@$items) {
473 if ($item->{homebranch} ne $library1->{branchcode}) {
474 $all_items_are_CPL = 0;
478 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
480 # Search all items where homebranch != 'CPL'
482 field => 'homebranch',
483 query => $library1->{branchcode},
486 ($items, $total_results) = SearchItems($filter);
487 ok($total_results > 0, "There is at least one non-CPL item");
488 my $all_items_are_not_CPL = 1;
489 foreach my $item (@$items) {
490 if ($item->{homebranch} eq $library1->{branchcode}) {
491 $all_items_are_not_CPL = 0;
495 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
497 # Search all items where biblio title (245$a) is like 'Silence in the %'
499 field => 'marc:245$a',
500 query => 'Silence in the %',
503 ($items, $total_results) = SearchItems($filter);
504 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
506 # Search all items where biblio title is 'Silence in the library'
507 # and homebranch is 'CPL'
509 conjunction => 'AND',
512 field => 'marc:245$a',
513 query => 'Silence in the %',
517 field => 'homebranch',
518 query => $library1->{branchcode},
523 ($items, $total_results) = SearchItems($filter);
525 foreach my $item (@$items) {
526 if ($item->{itemnumber} == $item1_itemnumber) {
531 ok($found, "item1 found");
533 my $frameworkcode = q||;
534 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
536 # Create item subfield 'z' without link
537 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
538 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
541 my $cache = Koha::Caches->get_instance();
542 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
543 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
544 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
546 my $item3_record = MARC::Record->new;
547 $item3_record->append_fields(
551 'y' => $itemtype->{itemtype}
554 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
555 $biblio->biblionumber);
557 # Search item where item subfield z is "foobar"
559 field => 'marc:' . $itemfield . '$z',
563 ($items, $total_results) = SearchItems($filter);
564 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
566 # Link $z to items.itemnotes (and make sure there is no other subfields
568 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
569 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
572 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
573 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
574 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
576 ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
578 # Make sure the link is used
579 my $item3 = Koha::Items->find($item3_itemnumber);
580 is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"');
582 # Do the same search again.
583 # This time it will search in items.itemnotes
584 ($items, $total_results) = SearchItems($filter);
585 is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"');
587 my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
588 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' );
592 conjunction => 'AND',
600 field => 'homebranch',
601 query => $library1->{branchcode},
606 ($items, $total_results) = SearchItems($filter);
607 is($total_results, 1, "Search items.issues issues = 0 returns result (items.issues defaults to 0)");
611 conjunction => 'AND',
614 field => 'new_status',
619 field => 'homebranch',
620 query => $library1->{branchcode},
625 ($items, $total_results) = SearchItems($filter);
626 is($total_results, 0, 'found no item with new_status=0 without ifnull');
628 $filter->{filters}[0]->{ifnull} = 0;
629 ($items, $total_results) = SearchItems($filter);
630 is($total_results, 1, 'found all items of library1 with new_status=0 with ifnull = 0');
632 t::lib::Mocks::mock_userenv({ branchcode => $item1->homebranch });
633 my $patron_borrower = $builder->build_object({ class => 'Koha::Patrons' })->unblessed;
634 AddIssue( $patron_borrower, $item1->barcode );
635 # Search item where item is checked out
637 conjunction => 'AND',
645 field => 'homebranch',
646 query => $item1->homebranch,
651 ($items, $total_results) = SearchItems($filter);
652 ok(scalar @$items == 1, 'found 1 checked out item');
654 # When sorting by descending availability, checked out item should show first
656 sortby => 'availability',
660 field => 'homebranch',
661 query => $item1->homebranch,
664 ($items, $total_results) = SearchItems($filter,$params);
665 is($items->[0]->{barcode}, $item1->barcode, 'Items sorted as expected by availability');
667 $schema->storage->txn_rollback;
670 subtest 'Koha::Item(s) tests' => sub {
674 $schema->storage->txn_begin();
676 my $builder = t::lib::TestBuilder->new;
677 my $library1 = $builder->build({
680 my $library2 = $builder->build({
683 my $itemtype = $builder->build({
684 source => 'Itemtype',
687 # Create a biblio and item for testing
688 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
689 my $biblio = $builder->build_sample_biblio();
690 my $itemnumber = $builder->build_sample_item(
692 biblionumber => $biblio->biblionumber,
693 homebranch => $library1->{branchcode},
694 holdingbranch => $library2->{branchcode},
695 itype => $itemtype->{itemtype}
700 my $item = Koha::Items->find( $itemnumber );
701 is( ref($item), 'Koha::Item', "Got Koha::Item" );
703 my $homebranch = $item->home_branch();
704 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
705 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
707 my $holdingbranch = $item->holding_branch();
708 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
709 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
711 $biblio = $item->biblio();
712 is( ref($item->biblio), 'Koha::Biblio', "Got Koha::Biblio from biblio method" );
713 is( $item->biblio->title(), $biblio->title, 'Title matches biblio title' );
715 $schema->storage->txn_rollback;
718 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
721 $schema->storage->txn_begin();
723 my $builder = t::lib::TestBuilder->new;
724 my $library1 = $builder->build({
727 my $library2 = $builder->build({
730 my $itemtype = $builder->build({
731 source => 'Itemtype',
734 my $biblio = $builder->build_sample_biblio();
736 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
737 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
738 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
739 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
740 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
741 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
742 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
743 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
745 my $number_of_items = scalar @$item_infos;
746 my $number_of_items_with_homebranch_is_CPL =
747 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
750 for my $item_info (@$item_infos) {
751 my $itemnumber = $builder->build_sample_item(
753 biblionumber => $biblio->biblionumber,
754 homebranch => $item_info->{homebranch},
755 holdingbranch => $item_info->{holdingbranch},
756 itype => $itemtype->{itemtype}
760 push @itemnumbers, $itemnumber;
763 # Emptied the OpacHiddenItems pref
764 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
767 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
768 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
769 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
770 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
771 'Should carp is no record passed.';
773 C4::Biblio::EmbedItemsInMarcBiblio({
774 marc_record => $record,
775 biblionumber => $biblio->biblionumber });
776 my @items = $record->field($itemfield);
777 is( scalar @items, $number_of_items, 'Should return all items' );
779 my $marc_with_items = C4::Biblio::GetMarcBiblio({
780 biblionumber => $biblio->biblionumber,
782 is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
784 C4::Biblio::EmbedItemsInMarcBiblio({
785 marc_record => $record,
786 biblionumber => $biblio->biblionumber,
787 item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
788 @items = $record->field($itemfield);
789 is( scalar @items, 2, 'Should return all items present in the list' );
791 C4::Biblio::EmbedItemsInMarcBiblio({
792 marc_record => $record,
793 biblionumber => $biblio->biblionumber,
795 @items = $record->field($itemfield);
796 is( scalar @items, $number_of_items, 'Should return all items for opac' );
798 my $opachiddenitems = "
799 homebranch: ['$library1->{branchcode}']";
800 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
802 C4::Biblio::EmbedItemsInMarcBiblio({
803 marc_record => $record,
804 biblionumber => $biblio->biblionumber });
805 @items = $record->field($itemfield);
808 'Even with OpacHiddenItems set, all items should have been embedded' );
810 C4::Biblio::EmbedItemsInMarcBiblio({
811 marc_record => $record,
812 biblionumber => $biblio->biblionumber,
814 @items = $record->field($itemfield);
817 $number_of_items - $number_of_items_with_homebranch_is_CPL,
818 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
822 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
823 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
824 C4::Biblio::EmbedItemsInMarcBiblio({
825 marc_record => $record,
826 biblionumber => $biblio->biblionumber,
828 @items = $record->field($itemfield);
832 'For OPAC, If all items are hidden, no item should have been embedded'
835 $schema->storage->txn_rollback;
839 subtest 'get_hostitemnumbers_of' => sub {
842 $schema->storage->txn_begin;
843 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
844 my $builder = t::lib::TestBuilder->new;
846 # Host item field without 0 or 9
847 my $bib1 = MARC::Record->new();
848 $bib1->append_fields(
849 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
850 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
851 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
853 my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
854 my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
855 is( scalar @itemnumbers1, 0, '773 without 0 or 9');
857 # Correct host item field, analytical records on
858 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
859 my $hostitem = $builder->build_sample_item();
860 my $bib2 = MARC::Record->new();
861 $bib2->append_fields(
862 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
863 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
864 MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
866 my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
867 my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
868 is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
870 # Correct host item field, analytical records off
871 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
872 @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
873 is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
875 $schema->storage->txn_rollback;
878 subtest 'Test logging for ModItem' => sub {
882 t::lib::Mocks::mock_preference('CataloguingLog', 1);
884 $schema->storage->txn_begin;
886 my $builder = t::lib::TestBuilder->new;
887 my $library = $builder->build({
890 my $itemtype = $builder->build({
891 source => 'Itemtype',
894 # Create a biblio instance for testing
895 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
896 my $biblio = $builder->build_sample_biblio();
899 my $item = $builder->build_sample_item(
901 biblionumber => $biblio->biblionumber,
902 library => $library->{homebranch},
903 location => $location,
904 itype => $itemtype->{itemtype}
908 # False means no logging
909 $schema->resultset('ActionLog')->search()->delete();
910 $item->location($location)->store({ log_action => 0 });
911 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
914 $schema->resultset('ActionLog')->search()->delete();
915 $item->location('new location')->store({ log_action => 1 });
916 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
918 # Undefined defaults to true
919 $schema->resultset('ActionLog')->search()->delete();
920 $item->location($location)->store();
921 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
923 $schema->storage->txn_rollback;
926 subtest 'Check stockrotationitem relationship' => sub {
929 $schema->storage->txn_begin();
931 my $builder = t::lib::TestBuilder->new;
932 my $item = $builder->build_sample_item;
935 source => 'Stockrotationitem',
936 value => { itemnumber_id => $item->itemnumber }
939 my $sritem = Koha::Items->find($item->itemnumber)->stockrotationitem;
940 isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
942 $schema->storage->txn_rollback;
945 subtest 'Check add_to_rota method' => sub {
948 $schema->storage->txn_begin();
950 my $builder = t::lib::TestBuilder->new;
951 my $item = $builder->build_sample_item;
952 my $rota = $builder->build({ source => 'Stockrotationrota' });
953 my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
956 source => 'Stockrotationstage',
957 value => { rota_id => $rota->{rota_id} },
960 my $sritem = Koha::Items->find($item->itemnumber);
961 $sritem->add_to_rota($rota->{rota_id});
964 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
965 $srrota->stockrotationstages->next->stage_id,
966 "Adding to a rota a new sritem item being assigned to its first stage."
969 my $newrota = $builder->build({ source => 'Stockrotationrota' });
971 my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
974 source => 'Stockrotationstage',
975 value => { rota_id => $newrota->{rota_id} },
978 $sritem->add_to_rota($newrota->{rota_id});
981 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
982 $srnewrota->stockrotationstages->next->stage_id,
983 "Moving an item results in that sritem being assigned to the new first stage."
986 $schema->storage->txn_rollback;
989 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
991 $schema->storage->txn_begin;
993 my $builder = t::lib::TestBuilder->new;
994 my $item = $builder->build_sample_item({ ccode => 'A|B' });
996 Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
997 Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
998 my $mapping = Koha::MarcSubfieldStructure->new(
1000 frameworkcode => q{},
1003 kohafield => 'items.ccode',
1007 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1010 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1011 my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
1012 is( @subs, 2, 'Expect two subfields' );
1013 is( $subs[0], 'A', 'First subfield matches' );
1014 is( $subs[1], 'B', 'Second subfield matches' );
1016 $schema->storage->txn_rollback;
1019 subtest 'ModItemFromMarc' => sub {
1021 $schema->storage->txn_begin;
1023 my $builder = t::lib::TestBuilder->new;
1024 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
1025 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1026 my $biblio = $builder->build_sample_biblio;
1027 my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
1028 my $item_record = MARC::Record->new;
1029 $item_record->append_fields(
1032 'y' => $itemtype->itemtype,
1039 my (undef, undef, $itemnumber) = AddItemFromMarc($item_record,
1040 $biblio->biblionumber);
1042 my $item = Koha::Items->find($itemnumber);
1043 is( $item->itemlost, 1, 'itemlost picked from the item marc');
1045 $item->new_status("this is something")->store;
1047 my $updated_item_record = MARC::Record->new;
1048 $updated_item_record->append_fields(
1051 'y' => $itemtype->itemtype,
1055 my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
1056 is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
1057 is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
1058 is( Koha::Items->find($itemnumber)->new_status, "this is something" );
1060 subtest 'cn_sort' => sub {
1063 my $item = $builder->build_sample_item;
1064 $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
1065 is( $item->cn_sort, 'XXX', 'init values set are expected' );
1067 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1068 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1069 is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
1071 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
1072 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1073 is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
1076 subtest 'onloan' => sub {
1079 my $item = $builder->build_sample_item;
1080 $item->set({ onloan => '2022-03-19' })->store;
1081 is( $item->onloan, '2022-03-19', 'init values set are expected' );
1083 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1084 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
1085 $marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
1086 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1087 is( $item->get_from_storage->onloan, '2022-03-19', 'onloan has not been updated if not passed' );
1089 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, onloan => '2022-03-26' }, $item->biblionumber );
1090 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1091 is( $item->get_from_storage->onloan, '2022-03-26', 'onloan has been updated when passed in' );
1094 subtest 'dateaccessioned' => sub {
1097 my $item = $builder->build_sample_item;
1098 $item->set({ dateaccessioned => '2022-03-19' })->store->discard_changes;
1099 is( $item->dateaccessioned, '2022-03-19', 'init values set are expected' );
1101 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1102 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.dateaccessioned' );
1103 $marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
1104 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1105 is( $item->get_from_storage->dateaccessioned, '2022-03-19', 'dateaccessioned has not been updated if not passed' );
1107 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, dateaccessioned => '2022-03-26' }, $item->biblionumber );
1108 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1109 is( $item->get_from_storage->dateaccessioned, '2022-03-26', 'dateaccessioned has been updated when passed in' );
1112 subtest 'permanent_location' => sub {
1115 # Make sure items.permanent_location is not mapped
1116 Koha::MarcSubfieldStructures->search(
1118 frameworkcode => q{},
1119 kohafield => 'items.permanent_location',
1122 Koha::MarcSubfieldStructures->search(
1124 frameworkcode => q{},
1129 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1131 my $item = $builder->build_sample_item;
1133 # By default, setting location to something new should set permanent location to the same thing
1134 # with the usual exceptions
1135 $item->set({ location => 'A', permanent_location => 'A' })->store;
1136 is( $item->location, 'A', 'initial location set as expected' );
1137 is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
1139 $item->location('B');
1140 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1141 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1143 $item = $item->get_from_storage;
1144 is( $item->location, 'B', 'new location set as expected' );
1145 is( $item->permanent_location, 'B', 'new permanent location set as expected' );
1147 # Added a marc mapping for permanent location, allows it to be edited independently
1148 my $mapping = Koha::MarcSubfieldStructure->new(
1150 frameworkcode => q{},
1153 kohafield => 'items.permanent_location',
1159 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1161 # Now if we change location, and also pass in a permanent location
1162 # the permanent_location will not be overwritten by location
1163 $item->location('C');
1164 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1165 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1166 $item = $item->get_from_storage;
1167 is( $item->location, 'C', 'next new location set as expected' );
1168 is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
1170 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
1171 # Clear values from the DB
1172 $item = $item->get_from_storage;
1174 # Update the location
1175 $item->location('D');
1176 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1177 # Remove the permanent_location field from the form
1178 $marc->field('952')->delete_subfield("C");
1179 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1180 $item = $item->get_from_storage;
1181 is( $item->location, 'D', 'next new location set as expected' );
1182 is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
1184 # Clear values from the DB
1185 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
1186 $item = $item->get_from_storage;
1188 # This time nothing is set, but we pass an empty string
1189 $item->permanent_location("");
1190 $item->location('E');
1191 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1192 $marc->field('952')->add_subfields( "C", "" );
1193 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1194 $item = $item->get_from_storage;
1195 is( $item->location, 'E', 'next new location set as expected' );
1196 is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
1199 $schema->storage->txn_rollback;
1200 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );