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 );
26 use Koha::DateUtils qw( dt_from_string );
29 use Koha::MarcSubfieldStructures;
31 use Koha::AuthorisedValues;
34 use t::lib::TestBuilder;
36 use Test::More tests => 14;
40 my $schema = Koha::Database->new->schema;
41 my $location = 'My Location';
43 subtest 'General Add, Get and Del tests' => sub {
47 $schema->storage->txn_begin;
49 my $builder = t::lib::TestBuilder->new;
50 my $library = $builder->build({
53 my $itemtype = $builder->build({
57 # Create a biblio instance for testing
58 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
59 my $biblio = $builder->build_sample_biblio();
62 my $item = $builder->build_sample_item(
64 biblionumber => $biblio->biblionumber,
65 library => $library->{branchcode},
66 location => $location,
67 itype => $itemtype->{itemtype}
70 my $itemnumber = $item->itemnumber;
71 cmp_ok($item->biblionumber, '==', $biblio->biblionumber, "New item is linked to correct biblionumber.");
72 cmp_ok($item->biblioitemnumber, '==', $biblio->biblioitem->biblioitemnumber, "New item is linked to correct biblioitemnumber.");
75 my $getitem = Koha::Items->find($itemnumber);
76 cmp_ok($getitem->itemnumber, '==', $itemnumber, "Retrieved item has correct itemnumber.");
77 cmp_ok($getitem->biblioitemnumber, '==', $item->biblioitemnumber, "Retrieved item has correct biblioitemnumber."); # We are not testing anything useful here
78 is( $getitem->location, $location, "The location should not have been modified" );
79 is( $getitem->permanent_location, $location, "The permanent_location should have been set to the location value" );
82 # Do not modify anything, and do not explode!
83 $getitem->set({})->store;
85 # Modify item; setting barcode.
86 $getitem->barcode('987654321')->store;
87 my $moditem = Koha::Items->find($itemnumber);
88 cmp_ok($moditem->barcode, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->barcode . '.');
92 my $getdeleted = Koha::Items->find($itemnumber);
93 is($getdeleted, undef, "Item deleted as expected.");
95 $itemnumber = $builder->build_sample_item(
97 biblionumber => $biblio->biblionumber,
98 library => $library->{branchcode},
99 location => $location,
100 permanent_location => 'my permanent location',
101 itype => $itemtype->{itemtype}
104 $getitem = Koha::Items->find($itemnumber);
105 is( $getitem->location, $location, "The location should not have been modified" );
106 is( $getitem->permanent_location, 'my permanent location', "The permanent_location should not have modified" );
108 my $new_location = "New location";
109 $getitem->location($new_location)->store;
110 $getitem = Koha::Items->find($itemnumber);
111 is( $getitem->location, $new_location, "The location should have been set to correct location" );
112 is( $getitem->permanent_location, $new_location, "The permanent_location should have been set to location" );
114 $getitem->location('CART')->store;
115 $getitem = Koha::Items->find($itemnumber);
116 is( $getitem->location, 'CART', "The location should have been set to CART" );
117 is( $getitem->permanent_location, $new_location, "The permanent_location should not have been set to CART" );
119 t::lib::Mocks::mock_preference('item-level_itypes', '1');
120 $getitem = Koha::Items->find($itemnumber);
121 is( $getitem->effective_itemtype, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
122 t::lib::Mocks::mock_preference('item-level_itypes', '0');
123 $getitem = Koha::Items->find($itemnumber);
124 is( $getitem->effective_itemtype, $biblio->biblioitem->itemtype, "Itemtype set correctly when not using item-level_itypes" );
126 $schema->storage->txn_rollback;
129 subtest 'ModItemTransfer tests' => sub {
132 $schema->storage->txn_begin;
134 my $builder = t::lib::TestBuilder->new;
135 my $item = $builder->build_sample_item();
137 my $library1 = $builder->build(
142 my $library2 = $builder->build(
148 ModItemTransfer( $item->itemnumber, $library1->{branchcode},
149 $library2->{branchcode}, 'Manual' );
151 my $transfers = Koha::Item::Transfers->search(
153 itemnumber => $item->itemnumber
157 is( $transfers->count, 1, "One transfer created with ModItemTransfer" );
158 $item->discard_changes;
159 is($item->holdingbranch, $library1->{branchcode}, "Items holding branch was updated to frombranch");
161 ModItemTransfer( $item->itemnumber, $library2->{branchcode},
162 $library1->{branchcode}, 'Manual' );
163 $transfers = Koha::Item::Transfers->search(
164 { itemnumber => $item->itemnumber, },
165 { order_by => { '-asc' => 'branchtransfer_id' } }
168 is($transfers->count, 2, "Second transfer recorded on second call of ModItemTransfer");
169 my $transfer1 = $transfers->next;
170 my $transfer2 = $transfers->next;
171 isnt($transfer1->datecancelled, undef, "First transfer marked as cancelled by ModItemTransfer");
172 like($transfer1->cancellation_reason,qr/^Manual/, "First transfer contains cancellation_reason 'Manual'");
173 is($transfer2->datearrived, undef, "Second transfer is now the active transfer");
174 $item->discard_changes;
175 is($item->holdingbranch, $library2->{branchcode}, "Items holding branch was updated to frombranch");
177 # Check 'reason' is populated when passed
178 ModItemTransfer( $item->itemnumber, $library2->{branchcode},
179 $library1->{branchcode}, "Manual" );
181 $transfers = Koha::Item::Transfers->search(
182 { itemnumber => $item->itemnumber, },
183 { order_by => { '-desc' => 'branchtransfer_id' } }
186 my $transfer3 = $transfers->next;
187 is($transfer3->reason, 'Manual', "Reason set via ModItemTransfer");
189 $schema->storage->txn_rollback;
192 subtest 'GetHiddenItemnumbers tests' => sub {
196 # This sub is controlled by the OpacHiddenItems system preference.
198 $schema->storage->txn_begin;
200 my $builder = t::lib::TestBuilder->new;
201 my $library1 = $builder->build({
205 my $library2 = $builder->build({
208 my $itemtype = $builder->build({
209 source => 'Itemtype',
212 # Create a new biblio
213 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
214 my $biblio = $builder->build_sample_biblio();
217 my $item1_itemnumber = $builder->build_sample_item(
219 biblionumber => $biblio->biblionumber,
220 library => $library1->{branchcode},
222 itype => $itemtype->{itemtype}
225 my $item2_itemnumber = $builder->build_sample_item(
227 biblionumber => $biblio->biblionumber,
228 library => $library2->{branchcode},
230 itype => $itemtype->{itemtype}
234 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
237 push @items, Koha::Items->find( $item1_itemnumber )->unblessed;
238 push @items, Koha::Items->find( $item2_itemnumber )->unblessed;
240 # Empty OpacHiddenItems
241 t::lib::Mocks::mock_preference('OpacHiddenItems','');
242 ok( !defined( GetHiddenItemnumbers( { items => \@items } ) ),
243 "Hidden items list undef if OpacHiddenItems empty");
246 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
247 ok( scalar GetHiddenItemnumbers( { items => \@items } ) == 0,
248 "Hidden items list empty if OpacHiddenItems only contains blanks");
250 # One variable / value
253 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
254 @hidden = GetHiddenItemnumbers( { items => \@items } );
255 ok( scalar @hidden == 1, "Only one hidden item");
256 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
258 # One variable, two values
261 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
262 @hidden = GetHiddenItemnumbers( { items => \@items } );
263 ok( scalar @hidden == 2, "Two items hidden");
264 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
266 # Two variables, a value each
269 homebranch: [$library2->{branchcode}]
271 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
272 @hidden = GetHiddenItemnumbers( { items => \@items } );
273 ok( scalar @hidden == 2, "Two items hidden");
274 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
276 # Override hidden with patron category
277 t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'S' );
278 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'PT' } );
279 ok( scalar @hidden == 2, "Two items still hidden");
280 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'S' } );
281 ok( scalar @hidden == 0, "Two items not hidden");
283 # Valid OpacHiddenItems, empty list
285 @hidden = GetHiddenItemnumbers( { items => \@items } );
286 ok( scalar @hidden == 0, "Empty items list, no item hidden");
288 $schema->storage->txn_rollback;
291 subtest 'GetItemsInfo tests' => sub {
295 $schema->storage->txn_begin;
297 my $builder = t::lib::TestBuilder->new;
298 my $library1 = $builder->build({
301 my $library2 = $builder->build({
304 my $itemtype = $builder->build({
305 source => 'Itemtype',
308 Koha::AuthorisedValues->delete;
309 my $av1 = Koha::AuthorisedValue->new(
311 category => 'RESTRICTED',
312 authorised_value => '1',
313 lib => 'Restricted Access',
314 lib_opac => 'Restricted Access OPAC',
319 my $biblio = $builder->build_sample_biblio();
321 my $itemnumber = $builder->build_sample_item(
323 biblionumber => $biblio->biblionumber,
324 homebranch => $library1->{branchcode},
325 holdingbranch => $library2->{branchcode},
326 itype => $itemtype->{itemtype},
331 my $library = Koha::Libraries->find( $library1->{branchcode} );
332 $library->opac_info("homebranch OPAC info");
335 $library = Koha::Libraries->find( $library2->{branchcode} );
336 $library->opac_info("holdingbranch OPAC info");
339 my @results = GetItemsInfo( $biblio->biblionumber );
340 ok( @results, 'GetItemsInfo returns results');
342 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
343 'GetItemsInfo returns the correct home branch OPAC info notice' );
344 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
345 'GetItemsInfo returns the correct holding branch OPAC info notice' );
346 is( exists( $results[0]->{ onsite_checkout } ), 1,
347 'GetItemsInfo returns a onsite_checkout key' );
348 is( $results[0]->{ restricted }, 1,
349 'GetItemsInfo returns a restricted value code' );
350 is( $results[0]->{ restrictedvalue }, "Restricted Access",
351 'GetItemsInfo returns a restricted value description (staff)' );
352 is( $results[0]->{ restrictedvalueopac }, "Restricted Access OPAC",
353 'GetItemsInfo returns a restricted value description (OPAC)' );
355 #place item into holds queue
356 my $dbh = C4::Context->dbh;
357 @results = GetItemsInfo( $biblio->biblionumber );
358 is( $results[0]->{ has_pending_hold }, "0",
359 'Hold not marked as pending/unavailable if nothing in tmp_holdsqueue for item' );
361 $dbh->do(q{INSERT INTO tmp_holdsqueue (biblionumber, itemnumber, surname, borrowernumber ) VALUES (?, ?, "Zorro", 42)}, undef, $biblio->biblionumber, $itemnumber);
362 @results = GetItemsInfo( $biblio->biblionumber );
363 is( $results[0]->{ has_pending_hold }, "1",
364 'Hold marked as pending/unavailable if tmp_holdsqueue is not empty for item' );
366 $schema->storage->txn_rollback;
369 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
373 $schema->storage->txn_begin;
375 my $biblio = $schema->resultset('Biblio')->create({
376 title => "Test title",
377 datecreated => dt_from_string,
378 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
380 my $biblioitem = $biblio->biblioitems->first;
381 my $item = $schema->resultset('Item')->create({
382 biblioitemnumber => $biblioitem->biblioitemnumber,
383 biblionumber => $biblio->biblionumber,
384 itype => "ITEM_LEVEL",
387 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
388 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
390 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
391 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
393 # If itemtype is not defined and item-level_level item types are set
394 # fallback to biblio-level itemtype (Bug 14651) and warn
395 $item->itype( undef );
397 my $effective_itemtype;
398 warning_is { $effective_itemtype = $item->effective_itemtype() }
399 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
400 '->effective_itemtype() raises a warning when falling back to bib-level';
402 ok( defined $effective_itemtype &&
403 $effective_itemtype eq 'BIB_LEVEL',
404 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
406 $schema->storage->txn_rollback;
409 subtest 'SearchItems test' => sub {
412 $schema->storage->txn_begin;
413 my $dbh = C4::Context->dbh;
414 my $builder = t::lib::TestBuilder->new;
416 my $library1 = $builder->build({
419 my $library2 = $builder->build({
422 my $itemtype = $builder->build({
423 source => 'Itemtype',
426 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
427 my ($cpl_items_before) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
429 my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
430 $builder->build_sample_biblio({ title => 'Silence in the shadow' });
432 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
435 my $item1_itemnumber = $builder->build_sample_item(
437 biblionumber => $biblio->biblionumber,
438 library => $library1->{branchcode},
439 itype => $itemtype->{itemtype}
442 my $item2_itemnumber = $builder->build_sample_item(
444 biblionumber => $biblio->biblionumber,
445 library => $library2->{branchcode},
446 itype => $itemtype->{itemtype}
450 my ($items, $total_results);
452 ($items, $total_results) = SearchItems();
453 is($total_results, $initial_items_count + 2, "Created 2 new items");
454 is(scalar @$items, $total_results, "SearchItems() returns all items");
456 ($items, $total_results) = SearchItems(undef, {rows => 1});
457 is($total_results, $initial_items_count + 2);
458 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
460 # Search all items where homebranch = 'CPL'
462 field => 'homebranch',
463 query => $library1->{branchcode},
466 ($items, $total_results) = SearchItems($filter);
467 ok($total_results > 0, "There is at least one CPL item");
468 my $all_items_are_CPL = 1;
469 foreach my $item (@$items) {
470 if ($item->{homebranch} ne $library1->{branchcode}) {
471 $all_items_are_CPL = 0;
475 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
477 # Search all items where homebranch != 'CPL'
479 field => 'homebranch',
480 query => $library1->{branchcode},
483 ($items, $total_results) = SearchItems($filter);
484 ok($total_results > 0, "There is at least one non-CPL item");
485 my $all_items_are_not_CPL = 1;
486 foreach my $item (@$items) {
487 if ($item->{homebranch} eq $library1->{branchcode}) {
488 $all_items_are_not_CPL = 0;
492 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
494 # Search all items where biblio title (245$a) is like 'Silence in the %'
496 field => 'marc:245$a',
497 query => 'Silence in the %',
500 ($items, $total_results) = SearchItems($filter);
501 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
503 # Search all items where biblio title is 'Silence in the library'
504 # and homebranch is 'CPL'
506 conjunction => 'AND',
509 field => 'marc:245$a',
510 query => 'Silence in the %',
514 field => 'homebranch',
515 query => $library1->{branchcode},
520 ($items, $total_results) = SearchItems($filter);
522 foreach my $item (@$items) {
523 if ($item->{itemnumber} == $item1_itemnumber) {
528 ok($found, "item1 found");
530 my $frameworkcode = q||;
531 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
533 # Create item subfield 'z' without link
534 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
535 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
538 my $cache = Koha::Caches->get_instance();
539 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
540 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
541 $cache->clear_from_cache("default_value_for_mod_marc-");
542 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
544 my $item3_record = MARC::Record->new;
545 $item3_record->append_fields(
549 'y' => $itemtype->{itemtype}
552 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
553 $biblio->biblionumber);
555 # Search item where item subfield z is "foobar"
557 field => 'marc:' . $itemfield . '$z',
561 ($items, $total_results) = SearchItems($filter);
562 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
564 # Link $z to items.itemnotes (and make sure there is no other subfields
566 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
567 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
570 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
571 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
572 $cache->clear_from_cache("default_value_for_mod_marc-");
573 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
575 ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
577 # Make sure the link is used
578 my $item3 = Koha::Items->find($item3_itemnumber);
579 is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"');
581 # Do the same search again.
582 # This time it will search in items.itemnotes
583 ($items, $total_results) = SearchItems($filter);
584 is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"');
586 my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
587 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' );
591 conjunction => 'AND',
599 field => 'homebranch',
600 query => $library1->{branchcode},
605 ($items, $total_results) = SearchItems($filter);
606 is($total_results, 1, "Search items.issues issues = 0 returns result (items.issues defaults to 0)");
610 conjunction => 'AND',
613 field => 'new_status',
618 field => 'homebranch',
619 query => $library1->{branchcode},
624 ($items, $total_results) = SearchItems($filter);
625 is($total_results, 0, 'found no item with new_status=0 without ifnull');
627 $filter->{filters}[0]->{ifnull} = 0;
628 ($items, $total_results) = SearchItems($filter);
629 is($total_results, 1, 'found all items of library1 with new_status=0 with ifnull = 0');
631 $schema->storage->txn_rollback;
634 subtest 'Koha::Item(s) tests' => sub {
638 $schema->storage->txn_begin();
640 my $builder = t::lib::TestBuilder->new;
641 my $library1 = $builder->build({
644 my $library2 = $builder->build({
647 my $itemtype = $builder->build({
648 source => 'Itemtype',
651 # Create a biblio and item for testing
652 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
653 my $biblio = $builder->build_sample_biblio();
654 my $itemnumber = $builder->build_sample_item(
656 biblionumber => $biblio->biblionumber,
657 homebranch => $library1->{branchcode},
658 holdingbranch => $library2->{branchcode},
659 itype => $itemtype->{itemtype}
664 my $item = Koha::Items->find( $itemnumber );
665 is( ref($item), 'Koha::Item', "Got Koha::Item" );
667 my $homebranch = $item->home_branch();
668 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
669 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
671 my $holdingbranch = $item->holding_branch();
672 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
673 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
675 $biblio = $item->biblio();
676 is( ref($item->biblio), 'Koha::Biblio', "Got Koha::Biblio from biblio method" );
677 is( $item->biblio->title(), $biblio->title, 'Title matches biblio title' );
679 $schema->storage->txn_rollback;
682 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
685 $schema->storage->txn_begin();
687 my $builder = t::lib::TestBuilder->new;
688 my $library1 = $builder->build({
691 my $library2 = $builder->build({
694 my $itemtype = $builder->build({
695 source => 'Itemtype',
698 my $biblio = $builder->build_sample_biblio();
700 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
701 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
702 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
703 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
704 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
705 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
706 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
707 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
709 my $number_of_items = scalar @$item_infos;
710 my $number_of_items_with_homebranch_is_CPL =
711 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
714 for my $item_info (@$item_infos) {
715 my $itemnumber = $builder->build_sample_item(
717 biblionumber => $biblio->biblionumber,
718 homebranch => $item_info->{homebranch},
719 holdingbranch => $item_info->{holdingbranch},
720 itype => $itemtype->{itemtype}
724 push @itemnumbers, $itemnumber;
727 # Emptied the OpacHiddenItems pref
728 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
731 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
732 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
733 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
734 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
735 'Should carp is no record passed.';
737 C4::Biblio::EmbedItemsInMarcBiblio({
738 marc_record => $record,
739 biblionumber => $biblio->biblionumber });
740 my @items = $record->field($itemfield);
741 is( scalar @items, $number_of_items, 'Should return all items' );
743 my $marc_with_items = C4::Biblio::GetMarcBiblio({
744 biblionumber => $biblio->biblionumber,
746 is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
748 C4::Biblio::EmbedItemsInMarcBiblio({
749 marc_record => $record,
750 biblionumber => $biblio->biblionumber,
751 item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
752 @items = $record->field($itemfield);
753 is( scalar @items, 2, 'Should return all items present in the list' );
755 C4::Biblio::EmbedItemsInMarcBiblio({
756 marc_record => $record,
757 biblionumber => $biblio->biblionumber,
759 @items = $record->field($itemfield);
760 is( scalar @items, $number_of_items, 'Should return all items for opac' );
762 my $opachiddenitems = "
763 homebranch: ['$library1->{branchcode}']";
764 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
766 C4::Biblio::EmbedItemsInMarcBiblio({
767 marc_record => $record,
768 biblionumber => $biblio->biblionumber });
769 @items = $record->field($itemfield);
772 'Even with OpacHiddenItems set, all items should have been embedded' );
774 C4::Biblio::EmbedItemsInMarcBiblio({
775 marc_record => $record,
776 biblionumber => $biblio->biblionumber,
778 @items = $record->field($itemfield);
781 $number_of_items - $number_of_items_with_homebranch_is_CPL,
782 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
786 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
787 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
788 C4::Biblio::EmbedItemsInMarcBiblio({
789 marc_record => $record,
790 biblionumber => $biblio->biblionumber,
792 @items = $record->field($itemfield);
796 'For OPAC, If all items are hidden, no item should have been embedded'
799 $schema->storage->txn_rollback;
803 subtest 'get_hostitemnumbers_of' => sub {
806 $schema->storage->txn_begin;
807 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
808 my $builder = t::lib::TestBuilder->new;
810 # Host item field without 0 or 9
811 my $bib1 = MARC::Record->new();
812 $bib1->append_fields(
813 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
814 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
815 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
817 my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
818 my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
819 is( scalar @itemnumbers1, 0, '773 without 0 or 9');
821 # Correct host item field, analytical records on
822 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
823 my $hostitem = $builder->build_sample_item();
824 my $bib2 = MARC::Record->new();
825 $bib2->append_fields(
826 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
827 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
828 MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
830 my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
831 my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
832 is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
834 # Correct host item field, analytical records off
835 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
836 @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
837 is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
839 $schema->storage->txn_rollback;
842 subtest 'Test logging for ModItem' => sub {
846 t::lib::Mocks::mock_preference('CataloguingLog', 1);
848 $schema->storage->txn_begin;
850 my $builder = t::lib::TestBuilder->new;
851 my $library = $builder->build({
854 my $itemtype = $builder->build({
855 source => 'Itemtype',
858 # Create a biblio instance for testing
859 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
860 my $biblio = $builder->build_sample_biblio();
863 my $item = $builder->build_sample_item(
865 biblionumber => $biblio->biblionumber,
866 library => $library->{homebranch},
867 location => $location,
868 itype => $itemtype->{itemtype}
872 # False means no logging
873 $schema->resultset('ActionLog')->search()->delete();
874 $item->location($location)->store({ log_action => 0 });
875 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
878 $schema->resultset('ActionLog')->search()->delete();
879 $item->location('new location')->store({ log_action => 1 });
880 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
882 # Undefined defaults to true
883 $schema->resultset('ActionLog')->search()->delete();
884 $item->location($location)->store();
885 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
887 $schema->storage->txn_rollback;
890 subtest 'Check stockrotationitem relationship' => sub {
893 $schema->storage->txn_begin();
895 my $builder = t::lib::TestBuilder->new;
896 my $item = $builder->build_sample_item;
899 source => 'Stockrotationitem',
900 value => { itemnumber_id => $item->itemnumber }
903 my $sritem = Koha::Items->find($item->itemnumber)->stockrotationitem;
904 isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
906 $schema->storage->txn_rollback;
909 subtest 'Check add_to_rota method' => sub {
912 $schema->storage->txn_begin();
914 my $builder = t::lib::TestBuilder->new;
915 my $item = $builder->build_sample_item;
916 my $rota = $builder->build({ source => 'Stockrotationrota' });
917 my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
920 source => 'Stockrotationstage',
921 value => { rota_id => $rota->{rota_id} },
924 my $sritem = Koha::Items->find($item->itemnumber);
925 $sritem->add_to_rota($rota->{rota_id});
928 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
929 $srrota->stockrotationstages->next->stage_id,
930 "Adding to a rota a new sritem item being assigned to its first stage."
933 my $newrota = $builder->build({ source => 'Stockrotationrota' });
935 my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
938 source => 'Stockrotationstage',
939 value => { rota_id => $newrota->{rota_id} },
942 $sritem->add_to_rota($newrota->{rota_id});
945 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
946 $srnewrota->stockrotationstages->next->stage_id,
947 "Moving an item results in that sritem being assigned to the new first stage."
950 $schema->storage->txn_rollback;
953 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
955 $schema->storage->txn_begin;
957 my $builder = t::lib::TestBuilder->new;
958 my $item = $builder->build_sample_item({ ccode => 'A|B' });
960 Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
961 Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
962 my $mapping = Koha::MarcSubfieldStructure->new(
964 frameworkcode => q{},
967 kohafield => 'items.ccode',
971 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
974 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
975 my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
976 is( @subs, 2, 'Expect two subfields' );
977 is( $subs[0], 'A', 'First subfield matches' );
978 is( $subs[1], 'B', 'Second subfield matches' );
980 $schema->storage->txn_rollback;
983 subtest 'ModItemFromMarc' => sub {
985 $schema->storage->txn_begin;
987 my $builder = t::lib::TestBuilder->new;
988 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
989 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
990 my $biblio = $builder->build_sample_biblio;
991 my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
992 my $item_record = MARC::Record->new;
993 $item_record->append_fields(
996 'y' => $itemtype->itemtype,
1003 my (undef, undef, $itemnumber) = AddItemFromMarc($item_record,
1004 $biblio->biblionumber);
1006 my $item = Koha::Items->find($itemnumber);
1007 is( $item->itemlost, 1, 'itemlost picked from the item marc');
1009 $item->new_status("this is something")->store;
1011 my $updated_item_record = MARC::Record->new;
1012 $updated_item_record->append_fields(
1015 'y' => $itemtype->itemtype,
1019 my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
1020 is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
1021 is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
1022 is( Koha::Items->find($itemnumber)->new_status, "this is something" );
1024 subtest 'cn_sort' => sub {
1027 my $item = $builder->build_sample_item;
1028 $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
1029 is( $item->cn_sort, 'XXX', 'init values set are expected' );
1031 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1032 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1033 is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
1035 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
1036 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1037 is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
1040 subtest 'permanent_location' => sub {
1043 # Make sure items.permanent_location is not mapped
1044 Koha::MarcSubfieldStructures->search(
1046 frameworkcode => q{},
1047 kohafield => 'items.permanent_location',
1050 Koha::MarcSubfieldStructures->search(
1052 frameworkcode => q{},
1057 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1059 my $item = $builder->build_sample_item;
1061 # By default, setting location to something new should set permanent location to the same thing
1062 # with the usual exceptions
1063 $item->set({ location => 'A', permanent_location => 'A' })->store;
1064 is( $item->location, 'A', 'initial location set as expected' );
1065 is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
1067 $item->location('B');
1068 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1069 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1071 $item = $item->get_from_storage;
1072 is( $item->location, 'B', 'new location set as expected' );
1073 is( $item->permanent_location, 'B', 'new permanent location set as expected' );
1075 # Added a marc mapping for permanent location, allows it to be edited independently
1076 my $mapping = Koha::MarcSubfieldStructure->new(
1078 frameworkcode => q{},
1081 kohafield => 'items.permanent_location',
1087 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1089 # Now if we change location, and also pass in a permanent location
1090 # the permanent_location will not be overwritten by location
1091 $item->location('C');
1092 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1093 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1094 $item = $item->get_from_storage;
1095 is( $item->location, 'C', 'next new location set as expected' );
1096 is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
1098 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
1099 # Clear values from the DB
1100 $item = $item->get_from_storage;
1102 # Update the location
1103 $item->location('D');
1104 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1105 # Remove the permanent_location field from the form
1106 $marc->field('952')->delete_subfield("C");
1107 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1108 $item = $item->get_from_storage;
1109 is( $item->location, 'D', 'next new location set as expected' );
1110 is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
1112 # Clear values from the DB
1113 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
1114 $item = $item->get_from_storage;
1116 # This time nothing is set, but we pass an empty string
1117 $item->permanent_location("");
1118 $item->location('E');
1119 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1120 $marc->field('952')->add_subfields( "C", "" );
1121 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1122 $item = $item->get_from_storage;
1123 is( $item->location, 'E', 'next new location set as expected' );
1124 is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
1127 $schema->storage->txn_rollback;
1128 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );