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;
34 use t::lib::TestBuilder;
36 use Test::More tests => 12;
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 ($bibnum, $bibitemnum) = get_biblio();
62 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
63 cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
64 cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
67 my $getitem = GetItem($itemnumber);
68 cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
69 cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
70 is( $getitem->{location}, $location, "The location should not have been modified" );
71 is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to the location value" );
74 # Do not modify anything, and do not explode!
75 my $dbh = C4::Context->dbh;
76 local $dbh->{RaiseError} = 1;
77 ModItem({}, $bibnum, $itemnumber);
79 # Modify item; setting barcode.
80 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
81 my $moditem = GetItem($itemnumber);
82 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
85 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
86 my $getdeleted = GetItem($itemnumber);
87 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
89 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $bibnum);
90 $getitem = GetItem($itemnumber);
91 is( $getitem->{location}, $location, "The location should not have been modified" );
92 is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" );
94 ModItem({ location => $location }, $bibnum, $itemnumber);
95 $getitem = GetItem($itemnumber);
96 is( $getitem->{location}, $location, "The location should have been set to correct location" );
97 is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to location" );
99 ModItem({ location => 'CART' }, $bibnum, $itemnumber);
100 $getitem = GetItem($itemnumber);
101 is( $getitem->{location}, 'CART', "The location should have been set to CART" );
102 is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" );
104 t::lib::Mocks::mock_preference('item-level_itypes', '1');
105 $getitem = GetItem($itemnumber);
106 is( $getitem->{itype}, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
107 t::lib::Mocks::mock_preference('item-level_itypes', '0');
108 $getitem = GetItem($itemnumber);
109 is( $getitem->{itype}, undef, "Itemtype set correctly when not using item-level_itypes" );
111 $schema->storage->txn_rollback;
114 subtest 'ModItem tests' => sub {
117 $schema->storage->txn_begin;
119 my $builder = t::lib::TestBuilder->new;
120 my $item = $builder->build({
126 itemlost_on => undef,
128 withdrawn_on => undef,
132 my @fields = qw( itemlost withdrawn damaged );
133 for my $field (@fields) {
135 ModItem( $item, $item->{biblionumber}, $item->{itemnumber} );
136 my $post_mod_item = Koha::Items->find({ itemnumber => $item->{itemnumber} })->unblessed;
137 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" );
140 ModItem( $item, $item->{biblionumber}, $item->{itemnumber} );
141 $post_mod_item = Koha::Items->find({ itemnumber => $item->{itemnumber} })->unblessed;
142 is( $post_mod_item->{$field."_on"}, undef, "When clearing $field, $field"."_on is cleared" );
145 $schema->storage->txn_rollback;
149 subtest 'GetHiddenItemnumbers tests' => sub {
153 # This sub is controlled by the OpacHiddenItems system preference.
155 $schema->storage->txn_begin;
157 my $builder = t::lib::TestBuilder->new;
158 my $library1 = $builder->build({
162 my $library2 = $builder->build({
165 my $itemtype = $builder->build({
166 source => 'Itemtype',
169 # Create a new biblio
170 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
171 my ($biblionumber, $biblioitemnumber) = get_biblio();
174 my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
176 homebranch => $library1->{branchcode},
177 holdingbranch => $library1->{branchcode},
179 itype => $itemtype->{itemtype},
183 my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
185 homebranch => $library2->{branchcode},
186 holdingbranch => $library2->{branchcode},
188 itype => $itemtype->{itemtype},
194 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
197 push @items, GetItem( $item1_itemnumber );
198 push @items, GetItem( $item2_itemnumber );
200 # Empty OpacHiddenItems
201 t::lib::Mocks::mock_preference('OpacHiddenItems','');
202 ok( !defined( GetHiddenItemnumbers( @items ) ),
203 "Hidden items list undef if OpacHiddenItems empty");
206 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
207 ok( scalar GetHiddenItemnumbers( @items ) == 0,
208 "Hidden items list empty if OpacHiddenItems only contains blanks");
210 # One variable / value
213 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
214 @hidden = GetHiddenItemnumbers( @items );
215 ok( scalar @hidden == 1, "Only one hidden item");
216 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
218 # One variable, two values
221 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
222 @hidden = GetHiddenItemnumbers( @items );
223 ok( scalar @hidden == 2, "Two items hidden");
224 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
226 # Two variables, a value each
229 homebranch: [$library2->{branchcode}]
231 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
232 @hidden = GetHiddenItemnumbers( @items );
233 ok( scalar @hidden == 2, "Two items hidden");
234 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
236 # Valid OpacHiddenItems, empty list
238 @hidden = GetHiddenItemnumbers( @items );
239 ok( scalar @hidden == 0, "Empty items list, no item hidden");
241 $schema->storage->txn_rollback;
244 subtest 'GetItemsInfo tests' => sub {
248 $schema->storage->txn_begin;
250 my $builder = t::lib::TestBuilder->new;
251 my $library1 = $builder->build({
254 my $library2 = $builder->build({
257 my $itemtype = $builder->build({
258 source => 'Itemtype',
262 my ($biblionumber, $biblioitemnumber) = get_biblio();
264 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
266 homebranch => $library1->{branchcode},
267 holdingbranch => $library2->{branchcode},
268 itype => $itemtype->{itemtype},
273 my $library = Koha::Libraries->find( $library1->{branchcode} );
274 $library->opac_info("homebranch OPAC info");
277 $library = Koha::Libraries->find( $library2->{branchcode} );
278 $library->opac_info("holdingbranch OPAC info");
281 my @results = GetItemsInfo( $biblionumber );
282 ok( @results, 'GetItemsInfo returns results');
283 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
284 'GetItemsInfo returns the correct home branch OPAC info notice' );
285 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
286 'GetItemsInfo returns the correct holding branch OPAC info notice' );
287 is( exists( $results[0]->{ onsite_checkout } ), 1,
288 'GetItemsInfo returns a onsite_checkout key' );
290 $schema->storage->txn_rollback;
293 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
297 $schema->storage->txn_begin;
299 my $biblio = $schema->resultset('Biblio')->create({
300 title => "Test title",
301 datecreated => dt_from_string,
302 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
304 my $biblioitem = $biblio->biblioitems->first;
305 my $item = $schema->resultset('Item')->create({
306 biblioitemnumber => $biblioitem->biblioitemnumber,
307 biblionumber => $biblio->biblionumber,
308 itype => "ITEM_LEVEL",
311 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
312 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
314 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
315 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
317 # If itemtype is not defined and item-level_level item types are set
318 # fallback to biblio-level itemtype (Bug 14651) and warn
319 $item->itype( undef );
321 my $effective_itemtype;
322 warning_is { $effective_itemtype = $item->effective_itemtype() }
323 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
324 '->effective_itemtype() raises a warning when falling back to bib-level';
326 ok( defined $effective_itemtype &&
327 $effective_itemtype eq 'BIB_LEVEL',
328 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
330 $schema->storage->txn_rollback;
333 subtest 'SearchItems test' => sub {
336 $schema->storage->txn_begin;
337 my $dbh = C4::Context->dbh;
338 my $builder = t::lib::TestBuilder->new;
340 my $library1 = $builder->build({
343 my $library2 = $builder->build({
346 my $itemtype = $builder->build({
347 source => 'Itemtype',
350 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
351 my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
353 my ($biblionumber) = get_biblio();
355 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
358 my (undef, undef, $item1_itemnumber) = AddItem({
359 homebranch => $library1->{branchcode},
360 holdingbranch => $library1->{branchcode},
361 itype => $itemtype->{itemtype},
363 my (undef, undef, $item2_itemnumber) = AddItem({
364 homebranch => $library2->{branchcode},
365 holdingbranch => $library2->{branchcode},
366 itype => $itemtype->{itemtype},
369 my ($items, $total_results);
371 ($items, $total_results) = SearchItems();
372 is($total_results, $initial_items_count + 2, "Created 2 new items");
373 is(scalar @$items, $total_results, "SearchItems() returns all items");
375 ($items, $total_results) = SearchItems(undef, {rows => 1});
376 is($total_results, $initial_items_count + 2);
377 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
379 # Search all items where homebranch = 'CPL'
381 field => 'homebranch',
382 query => $library1->{branchcode},
385 ($items, $total_results) = SearchItems($filter);
386 ok($total_results > 0, "There is at least one CPL item");
387 my $all_items_are_CPL = 1;
388 foreach my $item (@$items) {
389 if ($item->{homebranch} ne $library1->{branchcode}) {
390 $all_items_are_CPL = 0;
394 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
396 # Search all items where homebranch != 'CPL'
398 field => 'homebranch',
399 query => $library1->{branchcode},
402 ($items, $total_results) = SearchItems($filter);
403 ok($total_results > 0, "There is at least one non-CPL item");
404 my $all_items_are_not_CPL = 1;
405 foreach my $item (@$items) {
406 if ($item->{homebranch} eq $library1->{branchcode}) {
407 $all_items_are_not_CPL = 0;
411 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
413 # Search all items where biblio title (245$a) is like 'Silence in the %'
415 field => 'marc:245$a',
416 query => 'Silence in the %',
419 ($items, $total_results) = SearchItems($filter);
420 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
422 # Search all items where biblio title is 'Silence in the library'
423 # and homebranch is 'CPL'
425 conjunction => 'AND',
428 field => 'marc:245$a',
429 query => 'Silence in the %',
433 field => 'homebranch',
434 query => $library1->{branchcode},
439 ($items, $total_results) = SearchItems($filter);
441 foreach my $item (@$items) {
442 if ($item->{itemnumber} == $item1_itemnumber) {
447 ok($found, "item1 found");
449 my $frameworkcode = q||;
450 my ($itemfield) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
452 # Create item subfield 'z' without link
453 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
454 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
457 my $cache = Koha::Caches->get_instance();
458 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
459 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
460 $cache->clear_from_cache("default_value_for_mod_marc-");
461 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
463 my $item3_record = new MARC::Record;
464 $item3_record->append_fields(
468 'y' => $itemtype->{itemtype}
471 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
474 # Search item where item subfield z is "foobar"
476 field => 'marc:' . $itemfield . '$z',
480 ($items, $total_results) = SearchItems($filter);
481 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
483 # Link $z to items.itemnotes (and make sure there is no other subfields
485 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
486 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
489 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
490 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
491 $cache->clear_from_cache("default_value_for_mod_marc-");
492 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
494 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
496 # Make sure the link is used
497 my $item3 = GetItem($item3_itemnumber);
498 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
500 # Do the same search again.
501 # This time it will search in items.itemnotes
502 ($items, $total_results) = SearchItems($filter);
503 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
505 my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
506 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
508 $schema->storage->txn_rollback;
511 subtest 'Koha::Item(s) tests' => sub {
515 $schema->storage->txn_begin();
517 my $builder = t::lib::TestBuilder->new;
518 my $library1 = $builder->build({
521 my $library2 = $builder->build({
524 my $itemtype = $builder->build({
525 source => 'Itemtype',
528 # Create a biblio and item for testing
529 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
530 my ($bibnum, $bibitemnum) = get_biblio();
531 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
533 homebranch => $library1->{branchcode},
534 holdingbranch => $library2->{branchcode},
535 itype => $itemtype->{itemtype},
541 my $item = Koha::Items->find( $itemnumber );
542 is( ref($item), 'Koha::Item', "Got Koha::Item" );
544 my $homebranch = $item->home_branch();
545 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
546 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
548 my $holdingbranch = $item->holding_branch();
549 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
550 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
552 $schema->storage->txn_rollback;
555 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
558 $schema->storage->txn_begin();
560 my $builder = t::lib::TestBuilder->new;
561 my $library1 = $builder->build({
564 my $library2 = $builder->build({
567 my $itemtype = $builder->build({
568 source => 'Itemtype',
571 my ( $biblionumber, $biblioitemnumber ) = get_biblio();
573 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
574 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
575 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
576 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
577 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
578 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
579 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
580 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
582 my $number_of_items = scalar @$item_infos;
583 my $number_of_items_with_homebranch_is_CPL =
584 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
587 for my $item_info (@$item_infos) {
588 my ( undef, undef, $itemnumber ) = AddItem(
590 homebranch => $item_info->{homebranch},
591 holdingbranch => $item_info->{holdingbanch},
592 itype => $itemtype->{itemtype},
596 push @itemnumbers, $itemnumber;
599 # Emptied the OpacHiddenItems pref
600 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
603 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
604 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
605 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
606 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
607 'Should crap is no record passed.';
609 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
610 my @items = $record->field($itemfield);
611 is( scalar @items, $number_of_items, 'Should return all items' );
613 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber,
614 [ $itemnumbers[1], $itemnumbers[3] ] );
615 @items = $record->field($itemfield);
616 is( scalar @items, 2, 'Should return all items present in the list' );
618 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
619 @items = $record->field($itemfield);
620 is( scalar @items, $number_of_items, 'Should return all items for opac' );
622 my $opachiddenitems = "
623 homebranch: ['$library1->{branchcode}']";
624 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
626 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
627 @items = $record->field($itemfield);
630 'Even with OpacHiddenItems set, all items should have been embedded' );
632 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
633 @items = $record->field($itemfield);
636 $number_of_items - $number_of_items_with_homebranch_is_CPL,
637 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
641 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
642 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
643 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
644 @items = $record->field($itemfield);
648 'For OPAC, If all items are hidden, no item should have been embedded'
651 $schema->storage->txn_rollback;
655 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
658 $schema->storage->txn_begin();
660 my $builder = t::lib::TestBuilder->new;
661 my $framework = $builder->build({ source => 'BiblioFramework' });
663 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
664 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '999', tagsubfield => [ 'c', 'd' ] })->delete;
665 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'c', kohafield => 'biblio.biblionumber' })->store;
666 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'd', kohafield => 'biblioitems.biblioitemnumber' })->store;
668 # Same for item fields: itemnumber, barcode, itype
669 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => [ '9', 'p', 'y' ] })->delete;
670 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
671 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
672 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
673 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
675 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
677 # Create a record with a barcode
678 my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
679 my $item_record = new MARC::Record;
680 my $a_barcode = 'a barcode';
681 my $barcode_field = MARC::Field->new(
686 my $itemtype_field = MARC::Field->new(
690 $item_record->append_fields( $barcode_field );
691 my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
693 # Make sure everything has been set up
694 my $item = GetItem($item_itemnumber);
695 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
697 # Delete the barcode field and save the record
698 $item_record->delete_fields( $barcode_field );
699 $item_record->append_fields( $itemtype_field ); # itemtype is mandatory
700 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
701 $item = GetItem($item_itemnumber);
702 is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
704 # Re-add the barcode field and save the record
705 $item_record->append_fields( $barcode_field );
706 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
707 $item = GetItem($item_itemnumber);
708 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
710 # Remove the mapping for barcode
711 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
713 # And make sure the caches are cleared
714 my $cache = Koha::Caches->get_instance();
715 $cache->clear_from_cache("default_value_for_mod_marc-");
716 $cache->clear_from_cache("MarcSubfieldStructure-");
718 # Update the MARC field with another value
719 $item_record->delete_fields( $barcode_field );
720 my $another_barcode = 'another_barcode';
721 my $another_barcode_field = MARC::Field->new(
723 p => $another_barcode,
725 $item_record->append_fields( $another_barcode_field );
726 # The DB value should not have been updated
727 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
728 $item = GetItem($item_itemnumber);
729 is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
731 $cache->clear_from_cache("default_value_for_mod_marc-");
732 $cache->clear_from_cache( "MarcSubfieldStructure-" );
733 $schema->storage->txn_rollback;
736 subtest '_mod_item_dates' => sub {
739 is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
740 is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
744 itemcallnumber => 'V II 149 1963',
747 $orgitem = { %$item };
748 C4::Items::_mod_item_dates($item);
749 is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
751 # add two correct dates
752 t::lib::Mocks::mock_preference('dateformat', 'us');
753 $item->{dateaccessioned} = '01/31/2016';
754 $item->{onloan} = $item->{dateaccessioned};
755 $orgitem = { %$item };
756 C4::Items::_mod_item_dates($item);
757 is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
758 is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
761 # add some invalid dates
762 $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
763 $item->{anotherdate} = 'tralala'; # even worse
764 $item->{myzerodate} = '0000-00-00'; # wrong too
765 C4::Items::_mod_item_dates($item);
766 is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
767 is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
768 is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
770 # check if itemlost_on was not touched
771 $item->{itemlost_on} = '12345678';
772 $item->{withdrawn_on} = '12/31/2015 23:59:00';
773 $item->{damaged_on} = '01/20/2017 09:00:00';
774 $orgitem = { %$item };
775 C4::Items::_mod_item_dates($item);
776 is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
778 t::lib::Mocks::mock_preference('dateformat', 'metric');
779 $item->{dateaccessioned} = '01/31/2016'; #wrong
780 $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
781 C4::Items::_mod_item_dates($item);
782 is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
783 is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
784 'yetanotherdatetime is ok' );
787 subtest 'get_hostitemnumbers_of' => sub {
790 my $bib = MARC::Record->new();
792 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
793 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
794 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
796 my ($biblionumber, $bibitemnum) = AddBiblio($bib, '');
798 my @itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
799 is( @itemnumbers, 0, );
802 subtest 'Test logging for ModItem' => sub {
806 t::lib::Mocks::mock_preference('CataloguingLog', 1);
808 $schema->storage->txn_begin;
810 my $builder = t::lib::TestBuilder->new;
811 my $library = $builder->build({
814 my $itemtype = $builder->build({
815 source => 'Itemtype',
818 # Create a biblio instance for testing
819 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
820 my ($bibnum, $bibitemnum) = get_biblio();
823 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
825 # False means no logging
826 $schema->resultset('ActionLog')->search()->delete();
827 ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 0 });
828 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
831 $schema->resultset('ActionLog')->search()->delete();
832 ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 1 });
833 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
835 # Undefined defaults to true
836 $schema->resultset('ActionLog')->search()->delete();
837 ModItem({ location => $location }, $bibnum, $itemnumber);
838 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
840 $schema->storage->txn_rollback;
843 # Helper method to set up a Biblio.
845 my ( $frameworkcode ) = @_;
846 $frameworkcode //= '';
847 my $bib = MARC::Record->new();
849 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
850 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
852 my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
853 return ($bibnum, $bibitemnum);