3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 => 15;
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_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber);
64 cmp_ok($item_bibnum, '==', $biblio->biblionumber, "New item is linked to correct biblionumber.");
65 cmp_ok($item_bibitemnum, '==', $biblio->biblioitem->biblioitemnumber, "New item is linked to correct biblioitemnumber.");
68 my $getitem = Koha::Items->find($itemnumber);
69 cmp_ok($getitem->itemnumber, '==', $itemnumber, "Retrieved item has correct itemnumber.");
70 cmp_ok($getitem->biblioitemnumber, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
71 is( $getitem->location, $location, "The location should not have been modified" );
72 is( $getitem->permanent_location, $location, "The permanent_location should have been set to the location value" );
75 # Do not modify anything, and do not explode!
76 my $dbh = C4::Context->dbh;
77 local $dbh->{RaiseError} = 1;
78 ModItem({}, $biblio->biblionumber, $itemnumber);
80 # Modify item; setting barcode.
81 ModItem({ barcode => '987654321' }, $biblio->biblionumber, $itemnumber);
82 my $moditem = Koha::Items->find($itemnumber);
83 cmp_ok($moditem->barcode, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->barcode . '.');
86 DelItem({ biblionumber => $biblio->biblionumber, itemnumber => $itemnumber });
87 my $getdeleted = Koha::Items->find($itemnumber);
88 is($getdeleted, undef, "Item deleted as expected.");
90 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $biblio->biblionumber);
91 $getitem = Koha::Items->find($itemnumber);
92 is( $getitem->location, $location, "The location should not have been modified" );
93 is( $getitem->permanent_location, 'my permanent location', "The permanent_location should not have modified" );
95 ModItem({ location => $location }, $biblio->biblionumber, $itemnumber);
96 $getitem = Koha::Items->find($itemnumber);
97 is( $getitem->location, $location, "The location should have been set to correct location" );
98 is( $getitem->permanent_location, $location, "The permanent_location should have been set to location" );
100 ModItem({ location => 'CART' }, $biblio->biblionumber, $itemnumber);
101 $getitem = Koha::Items->find($itemnumber);
102 is( $getitem->location, 'CART', "The location should have been set to CART" );
103 is( $getitem->permanent_location, $location, "The permanent_location should not have been set to CART" );
105 t::lib::Mocks::mock_preference('item-level_itypes', '1');
106 $getitem = Koha::Items->find($itemnumber);
107 is( $getitem->effective_itemtype, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
108 t::lib::Mocks::mock_preference('item-level_itypes', '0');
109 $getitem = Koha::Items->find($itemnumber);
110 is( $getitem->effective_itemtype, $biblio->biblioitem->itemtype, "Itemtype set correctly when not using item-level_itypes" );
112 $schema->storage->txn_rollback;
115 subtest 'ModItem tests' => sub {
118 $schema->storage->txn_begin;
120 my $builder = t::lib::TestBuilder->new;
121 my $item = $builder->build({
127 itemlost_on => undef,
129 withdrawn_on => undef,
133 my @fields = qw( itemlost withdrawn damaged );
134 for my $field (@fields) {
136 ModItem( $item, $item->{biblionumber}, $item->{itemnumber} );
137 my $post_mod_item = Koha::Items->find({ itemnumber => $item->{itemnumber} })->unblessed;
138 is( output_pref({ str => $post_mod_item->{$field."_on"}, dateonly => 1 }), output_pref({ dt => dt_from_string(), dateonly => 1 }), "When updating $field, $field"."_on is updated" );
141 ModItem( $item, $item->{biblionumber}, $item->{itemnumber} );
142 $post_mod_item = Koha::Items->find({ itemnumber => $item->{itemnumber} })->unblessed;
143 is( $post_mod_item->{$field."_on"}, undef, "When clearing $field, $field"."_on is cleared" );
146 $schema->storage->txn_rollback;
150 subtest 'GetHiddenItemnumbers tests' => sub {
154 # This sub is controlled by the OpacHiddenItems system preference.
156 $schema->storage->txn_begin;
158 my $builder = t::lib::TestBuilder->new;
159 my $library1 = $builder->build({
163 my $library2 = $builder->build({
166 my $itemtype = $builder->build({
167 source => 'Itemtype',
170 # Create a new biblio
171 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
172 my $biblio = $builder->build_sample_biblio();
175 my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
177 homebranch => $library1->{branchcode},
178 holdingbranch => $library1->{branchcode},
180 itype => $itemtype->{itemtype},
182 $biblio->biblionumber
184 my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
186 homebranch => $library2->{branchcode},
187 holdingbranch => $library2->{branchcode},
189 itype => $itemtype->{itemtype},
191 $biblio->biblionumber
195 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
198 push @items, Koha::Items->find( $item1_itemnumber )->unblessed;
199 push @items, Koha::Items->find( $item2_itemnumber )->unblessed;
201 # Empty OpacHiddenItems
202 t::lib::Mocks::mock_preference('OpacHiddenItems','');
203 ok( !defined( GetHiddenItemnumbers( { items => \@items } ) ),
204 "Hidden items list undef if OpacHiddenItems empty");
207 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
208 ok( scalar GetHiddenItemnumbers( { items => \@items } ) == 0,
209 "Hidden items list empty if OpacHiddenItems only contains blanks");
211 # One variable / value
214 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
215 @hidden = GetHiddenItemnumbers( { items => \@items } );
216 ok( scalar @hidden == 1, "Only one hidden item");
217 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
219 # One variable, two values
222 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
223 @hidden = GetHiddenItemnumbers( { items => \@items } );
224 ok( scalar @hidden == 2, "Two items hidden");
225 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
227 # Two variables, a value each
230 homebranch: [$library2->{branchcode}]
232 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
233 @hidden = GetHiddenItemnumbers( { items => \@items } );
234 ok( scalar @hidden == 2, "Two items hidden");
235 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
237 # Override hidden with patron category
238 t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'S' );
239 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'PT' } );
240 ok( scalar @hidden == 2, "Two items still hidden");
241 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'S' } );
242 ok( scalar @hidden == 0, "Two items not hidden");
244 # Valid OpacHiddenItems, empty list
246 @hidden = GetHiddenItemnumbers( { items => \@items } );
247 ok( scalar @hidden == 0, "Empty items list, no item hidden");
249 $schema->storage->txn_rollback;
252 subtest 'GetItemsInfo tests' => sub {
256 $schema->storage->txn_begin;
258 my $builder = t::lib::TestBuilder->new;
259 my $library1 = $builder->build({
262 my $library2 = $builder->build({
265 my $itemtype = $builder->build({
266 source => 'Itemtype',
269 Koha::AuthorisedValues->delete;
270 my $av1 = Koha::AuthorisedValue->new(
272 category => 'RESTRICTED',
273 authorised_value => '1',
274 lib => 'Restricted Access',
275 lib_opac => 'Restricted Access OPAC',
280 my $biblio = $builder->build_sample_biblio();
282 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
284 homebranch => $library1->{branchcode},
285 holdingbranch => $library2->{branchcode},
286 itype => $itemtype->{itemtype},
289 $biblio->biblionumber
292 my $library = Koha::Libraries->find( $library1->{branchcode} );
293 $library->opac_info("homebranch OPAC info");
296 $library = Koha::Libraries->find( $library2->{branchcode} );
297 $library->opac_info("holdingbranch OPAC info");
300 my @results = GetItemsInfo( $biblio->biblionumber );
301 ok( @results, 'GetItemsInfo returns results');
303 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
304 'GetItemsInfo returns the correct home branch OPAC info notice' );
305 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
306 'GetItemsInfo returns the correct holding branch OPAC info notice' );
307 is( exists( $results[0]->{ onsite_checkout } ), 1,
308 'GetItemsInfo returns a onsite_checkout key' );
309 is( $results[0]->{ restricted }, 1,
310 'GetItemsInfo returns a restricted value code' );
311 is( $results[0]->{ restrictedvalue }, "Restricted Access",
312 'GetItemsInfo returns a restricted value description (staff)' );
313 is( $results[0]->{ restrictedvalueopac }, "Restricted Access OPAC",
314 'GetItemsInfo returns a restricted value description (OPAC)' );
316 #place item into holds queue
317 my $dbh = C4::Context->dbh;
318 @results = GetItemsInfo( $biblio->biblionumber );
319 is( $results[0]->{ has_pending_hold }, "0",
320 'Hold not marked as pending/unavailable if nothing in tmp_holdsqueue for item' );
322 $dbh->do(q{INSERT INTO tmp_holdsqueue (biblionumber, itemnumber, surname, borrowernumber ) VALUES (?, ?, "Zorro", 42)}, undef, $item_bibnum, $itemnumber);
323 @results = GetItemsInfo( $biblio->biblionumber );
324 is( $results[0]->{ has_pending_hold }, "1",
325 'Hold marked as pending/unavailable if tmp_holdsqueue is not empty for item' );
327 $schema->storage->txn_rollback;
330 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
334 $schema->storage->txn_begin;
336 my $biblio = $schema->resultset('Biblio')->create({
337 title => "Test title",
338 datecreated => dt_from_string,
339 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
341 my $biblioitem = $biblio->biblioitems->first;
342 my $item = $schema->resultset('Item')->create({
343 biblioitemnumber => $biblioitem->biblioitemnumber,
344 biblionumber => $biblio->biblionumber,
345 itype => "ITEM_LEVEL",
348 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
349 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
351 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
352 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
354 # If itemtype is not defined and item-level_level item types are set
355 # fallback to biblio-level itemtype (Bug 14651) and warn
356 $item->itype( undef );
358 my $effective_itemtype;
359 warning_is { $effective_itemtype = $item->effective_itemtype() }
360 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
361 '->effective_itemtype() raises a warning when falling back to bib-level';
363 ok( defined $effective_itemtype &&
364 $effective_itemtype eq 'BIB_LEVEL',
365 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
367 $schema->storage->txn_rollback;
370 subtest 'SearchItems test' => sub {
373 $schema->storage->txn_begin;
374 my $dbh = C4::Context->dbh;
375 my $builder = t::lib::TestBuilder->new;
377 my $library1 = $builder->build({
380 my $library2 = $builder->build({
383 my $itemtype = $builder->build({
384 source => 'Itemtype',
387 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
388 my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
390 my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
391 $builder->build_sample_biblio({ title => 'Silence in the shadow' });
393 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
396 my (undef, undef, $item1_itemnumber) = AddItem({
397 homebranch => $library1->{branchcode},
398 holdingbranch => $library1->{branchcode},
399 itype => $itemtype->{itemtype},
400 }, $biblio->biblionumber);
401 my (undef, undef, $item2_itemnumber) = AddItem({
402 homebranch => $library2->{branchcode},
403 holdingbranch => $library2->{branchcode},
404 itype => $itemtype->{itemtype},
405 }, $biblio->biblionumber);
407 my ($items, $total_results);
409 ($items, $total_results) = SearchItems();
410 is($total_results, $initial_items_count + 2, "Created 2 new items");
411 is(scalar @$items, $total_results, "SearchItems() returns all items");
413 ($items, $total_results) = SearchItems(undef, {rows => 1});
414 is($total_results, $initial_items_count + 2);
415 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
417 # Search all items where homebranch = 'CPL'
419 field => 'homebranch',
420 query => $library1->{branchcode},
423 ($items, $total_results) = SearchItems($filter);
424 ok($total_results > 0, "There is at least one CPL item");
425 my $all_items_are_CPL = 1;
426 foreach my $item (@$items) {
427 if ($item->{homebranch} ne $library1->{branchcode}) {
428 $all_items_are_CPL = 0;
432 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
434 # Search all items where homebranch != 'CPL'
436 field => 'homebranch',
437 query => $library1->{branchcode},
440 ($items, $total_results) = SearchItems($filter);
441 ok($total_results > 0, "There is at least one non-CPL item");
442 my $all_items_are_not_CPL = 1;
443 foreach my $item (@$items) {
444 if ($item->{homebranch} eq $library1->{branchcode}) {
445 $all_items_are_not_CPL = 0;
449 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
451 # Search all items where biblio title (245$a) is like 'Silence in the %'
453 field => 'marc:245$a',
454 query => 'Silence in the %',
457 ($items, $total_results) = SearchItems($filter);
458 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
460 # Search all items where biblio title is 'Silence in the library'
461 # and homebranch is 'CPL'
463 conjunction => 'AND',
466 field => 'marc:245$a',
467 query => 'Silence in the %',
471 field => 'homebranch',
472 query => $library1->{branchcode},
477 ($items, $total_results) = SearchItems($filter);
479 foreach my $item (@$items) {
480 if ($item->{itemnumber} == $item1_itemnumber) {
485 ok($found, "item1 found");
487 my $frameworkcode = q||;
488 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
490 # Create item subfield 'z' without link
491 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
492 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
495 my $cache = Koha::Caches->get_instance();
496 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
497 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
498 $cache->clear_from_cache("default_value_for_mod_marc-");
499 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
501 my $item3_record = new MARC::Record;
502 $item3_record->append_fields(
506 'y' => $itemtype->{itemtype}
509 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
510 $biblio->biblionumber);
512 # Search item where item subfield z is "foobar"
514 field => 'marc:' . $itemfield . '$z',
518 ($items, $total_results) = SearchItems($filter);
519 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
521 # Link $z to items.itemnotes (and make sure there is no other subfields
523 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
524 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
527 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
528 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
529 $cache->clear_from_cache("default_value_for_mod_marc-");
530 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
532 ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
534 # Make sure the link is used
535 my $item3 = Koha::Items->find($item3_itemnumber);
536 ok($item3->itemnotes eq 'foobar', 'itemnotes eq "foobar"');
538 # Do the same search again.
539 # This time it will search in items.itemnotes
540 ($items, $total_results) = SearchItems($filter);
541 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
543 my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
544 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
546 $schema->storage->txn_rollback;
549 subtest 'Koha::Item(s) tests' => sub {
553 $schema->storage->txn_begin();
555 my $builder = t::lib::TestBuilder->new;
556 my $library1 = $builder->build({
559 my $library2 = $builder->build({
562 my $itemtype = $builder->build({
563 source => 'Itemtype',
566 # Create a biblio and item for testing
567 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
568 my $biblio = $builder->build_sample_biblio({title => 'Silence in the library'});
569 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
571 homebranch => $library1->{branchcode},
572 holdingbranch => $library2->{branchcode},
573 itype => $itemtype->{itemtype},
575 $biblio->biblionumber
579 my $item = Koha::Items->find( $itemnumber );
580 is( ref($item), 'Koha::Item', "Got Koha::Item" );
582 my $homebranch = $item->home_branch();
583 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
584 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
586 my $holdingbranch = $item->holding_branch();
587 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
588 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
590 $biblio = $item->biblio();
591 is( ref($biblio), 'Koha::Biblio', "Got Koha::Biblio from biblio method" );
592 is( $biblio->title(), 'Silence in the library', 'Title matches biblio title' );
594 $schema->storage->txn_rollback;
597 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
600 $schema->storage->txn_begin();
602 my $builder = t::lib::TestBuilder->new;
603 my $library1 = $builder->build({
606 my $library2 = $builder->build({
609 my $itemtype = $builder->build({
610 source => 'Itemtype',
613 my $biblio = $builder->build_sample_biblio();
615 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
616 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
617 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
618 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
619 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
620 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
621 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
622 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
624 my $number_of_items = scalar @$item_infos;
625 my $number_of_items_with_homebranch_is_CPL =
626 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
629 for my $item_info (@$item_infos) {
630 my ( undef, undef, $itemnumber ) = AddItem(
632 homebranch => $item_info->{homebranch},
633 holdingbranch => $item_info->{holdingbanch},
634 itype => $itemtype->{itemtype},
636 $biblio->biblionumber
638 push @itemnumbers, $itemnumber;
641 # Emptied the OpacHiddenItems pref
642 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
645 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
646 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
647 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
648 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
649 'Should carp is no record passed.';
651 C4::Biblio::EmbedItemsInMarcBiblio({
652 marc_record => $record,
653 biblionumber => $biblio->biblionumber });
654 my @items = $record->field($itemfield);
655 is( scalar @items, $number_of_items, 'Should return all items' );
657 my $marc_with_items = C4::Biblio::GetMarcBiblio({
658 biblionumber => $biblio->biblionumber,
660 is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
662 C4::Biblio::EmbedItemsInMarcBiblio({
663 marc_record => $record,
664 biblionumber => $biblio->biblionumber,
665 item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
666 @items = $record->field($itemfield);
667 is( scalar @items, 2, 'Should return all items present in the list' );
669 C4::Biblio::EmbedItemsInMarcBiblio({
670 marc_record => $record,
671 biblionumber => $biblio->biblionumber,
673 @items = $record->field($itemfield);
674 is( scalar @items, $number_of_items, 'Should return all items for opac' );
676 my $opachiddenitems = "
677 homebranch: ['$library1->{branchcode}']";
678 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
680 C4::Biblio::EmbedItemsInMarcBiblio({
681 marc_record => $record,
682 biblionumber => $biblio->biblionumber });
683 @items = $record->field($itemfield);
686 'Even with OpacHiddenItems set, all items should have been embedded' );
688 C4::Biblio::EmbedItemsInMarcBiblio({
689 marc_record => $record,
690 biblionumber => $biblio->biblionumber,
692 @items = $record->field($itemfield);
695 $number_of_items - $number_of_items_with_homebranch_is_CPL,
696 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
700 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
701 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
702 C4::Biblio::EmbedItemsInMarcBiblio({
703 marc_record => $record,
704 biblionumber => $biblio->biblionumber,
706 @items = $record->field($itemfield);
710 'For OPAC, If all items are hidden, no item should have been embedded'
713 $schema->storage->txn_rollback;
717 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
720 $schema->storage->txn_begin();
722 my $builder = t::lib::TestBuilder->new;
723 my $framework = $builder->build({ source => 'BiblioFramework' });
725 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
726 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '999', tagsubfield => [ 'c', 'd' ] })->delete;
727 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'c', kohafield => 'biblio.biblionumber' })->store;
728 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'd', kohafield => 'biblioitems.biblioitemnumber' })->store;
730 # Same for item fields: itemnumber, barcode, itype
731 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => [ '9', 'p', 'y' ] })->delete;
732 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
733 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
734 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
735 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
737 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
739 # Create a record with a barcode
740 my $biblio = $builder->build_sample_biblio({ frameworkcode => $framework->{frameworkcode} });
741 my $item_record = new MARC::Record;
742 my $a_barcode = 'a barcode';
743 my $barcode_field = MARC::Field->new(
748 my $itemtype_field = MARC::Field->new(
752 $item_record->append_fields( $barcode_field );
753 my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblio->biblionumber);
755 # Make sure everything has been set up
756 my $item = Koha::Items->find($item_itemnumber);
757 is( $item->barcode, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
759 # Delete the barcode field and save the record
760 $item_record->delete_fields( $barcode_field );
761 $item_record->append_fields( $itemtype_field ); # itemtype is mandatory
762 ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber);
763 $item = Koha::Items->find($item_itemnumber);
764 is( $item->barcode, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
766 # Re-add the barcode field and save the record
767 $item_record->append_fields( $barcode_field );
768 ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber);
769 $item = Koha::Items->find($item_itemnumber);
770 is( $item->barcode, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
772 # Remove the mapping for barcode
773 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
775 # And make sure the caches are cleared
776 my $cache = Koha::Caches->get_instance();
777 $cache->clear_from_cache("default_value_for_mod_marc-");
778 $cache->clear_from_cache("MarcSubfieldStructure-");
780 # Update the MARC field with another value
781 $item_record->delete_fields( $barcode_field );
782 my $another_barcode = 'another_barcode';
783 my $another_barcode_field = MARC::Field->new(
785 p => $another_barcode,
787 $item_record->append_fields( $another_barcode_field );
788 # The DB value should not have been updated
789 ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber);
790 $item = Koha::Items->find($item_itemnumber);
791 is ( $item->barcode, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
793 $cache->clear_from_cache("default_value_for_mod_marc-");
794 $cache->clear_from_cache( "MarcSubfieldStructure-" );
795 $schema->storage->txn_rollback;
798 subtest '_mod_item_dates' => sub {
801 is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
802 is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
806 itemcallnumber => 'V II 149 1963',
809 $orgitem = { %$item };
810 C4::Items::_mod_item_dates($item);
811 is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
813 # add two correct dates
814 t::lib::Mocks::mock_preference('dateformat', 'us');
815 $item->{dateaccessioned} = '01/31/2016';
816 $item->{onloan} = $item->{dateaccessioned};
817 $orgitem = { %$item };
818 C4::Items::_mod_item_dates($item);
819 is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
820 is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
823 # add some invalid dates
824 $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
825 $item->{anotherdate} = 'tralala'; # even worse
826 $item->{myzerodate} = '0000-00-00'; # wrong too
827 C4::Items::_mod_item_dates($item);
828 is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
829 is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
830 is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
832 # check if itemlost_on was not touched
833 $item->{itemlost_on} = '12345678';
834 $item->{withdrawn_on} = '12/31/2015 23:59:00';
835 $item->{damaged_on} = '01/20/2017 09:00:00';
836 $orgitem = { %$item };
837 C4::Items::_mod_item_dates($item);
838 is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
840 t::lib::Mocks::mock_preference('dateformat', 'metric');
841 $item->{dateaccessioned} = '01/31/2016'; #wrong
842 $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
843 C4::Items::_mod_item_dates($item);
844 is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
845 is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
846 'yetanotherdatetime is ok' );
849 subtest 'get_hostitemnumbers_of' => sub {
852 $schema->storage->txn_begin;
853 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
854 my $builder = t::lib::TestBuilder->new;
856 # Host item field without 0 or 9
857 my $bib1 = MARC::Record->new();
858 $bib1->append_fields(
859 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
860 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
861 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
863 my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
864 my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
865 is( scalar @itemnumbers1, 0, '773 without 0 or 9');
867 # Correct host item field, analytical records on
868 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
869 my $hostitem = $builder->build_sample_item();
870 my $bib2 = MARC::Record->new();
871 $bib2->append_fields(
872 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
873 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
874 MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
876 my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
877 my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
878 is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
880 # Correct host item field, analytical records off
881 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
882 @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
883 is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
885 $schema->storage->txn_rollback;
888 subtest 'Test logging for ModItem' => sub {
892 t::lib::Mocks::mock_preference('CataloguingLog', 1);
894 $schema->storage->txn_begin;
896 my $builder = t::lib::TestBuilder->new;
897 my $library = $builder->build({
900 my $itemtype = $builder->build({
901 source => 'Itemtype',
904 # Create a biblio instance for testing
905 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
906 my $biblio = $builder->build_sample_biblio();
909 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber);
911 # False means no logging
912 $schema->resultset('ActionLog')->search()->delete();
913 ModItem({ location => $location }, $biblio->biblionumber, $itemnumber, { log_action => 0 });
914 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
917 $schema->resultset('ActionLog')->search()->delete();
918 ModItem({ location => $location }, $biblio->biblionumber, $itemnumber, { log_action => 1 });
919 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
921 # Undefined defaults to true
922 $schema->resultset('ActionLog')->search()->delete();
923 ModItem({ location => $location }, $biblio->biblionumber, $itemnumber);
924 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
926 $schema->storage->txn_rollback;
929 subtest 'Check stockrotationitem relationship' => sub {
932 $schema->storage->txn_begin();
934 my $builder = t::lib::TestBuilder->new;
935 my $item = $builder->build({ source => 'Item' });
938 source => 'Stockrotationitem',
939 value => { itemnumber_id => $item->{itemnumber} }
942 my $sritem = Koha::Items->find($item->{itemnumber})->stockrotationitem;
943 isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
945 $schema->storage->txn_rollback;
948 subtest 'Check add_to_rota method' => sub {
951 $schema->storage->txn_begin();
953 my $builder = t::lib::TestBuilder->new;
954 my $item = $builder->build({ source => 'Item' });
955 my $rota = $builder->build({ source => 'Stockrotationrota' });
956 my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
959 source => 'Stockrotationstage',
960 value => { rota_id => $rota->{rota_id} },
963 my $sritem = Koha::Items->find($item->{itemnumber});
964 $sritem->add_to_rota($rota->{rota_id});
967 Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
968 $srrota->stockrotationstages->next->stage_id,
969 "Adding to a rota a new sritem item being assigned to its first stage."
972 my $newrota = $builder->build({ source => 'Stockrotationrota' });
974 my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
977 source => 'Stockrotationstage',
978 value => { rota_id => $newrota->{rota_id} },
981 $sritem->add_to_rota($newrota->{rota_id});
984 Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
985 $srnewrota->stockrotationstages->next->stage_id,
986 "Moving an item results in that sritem being assigned to the new first stage."
989 $schema->storage->txn_rollback;
992 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
994 $schema->storage->txn_begin;
996 my $builder = t::lib::TestBuilder->new;
997 my $biblio = $builder->build({ source => 'Biblio', value => { frameworkcode => q{} } });
998 my $item = $builder->build({ source => 'Item', value => { biblionumber => $biblio->{biblionumber}, ccode => 'A|B' } });
1000 Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
1001 Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
1002 my $mapping = Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '952', tagsubfield => '8', kohafield => 'items.ccode' })->store;
1005 my $marc = C4::Items::Item2Marc( $item, $biblio->{biblionumber} );
1006 my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
1007 is( @subs, 2, 'Expect two subfields' );
1008 is( $subs[0], 'A', 'First subfield matches' );
1009 is( $subs[1], 'B', 'Second subfield matches' );
1011 $schema->storage->txn_rollback;