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 t
::lib
::TestBuilder
;
29 use Test
::More tests
=> 12;
35 use_ok
('Koha::Items');
38 my $schema = Koha
::Database
->new->schema;
39 my $location = 'My Location';
41 subtest
'General Add, Get and Del tests' => sub {
45 $schema->storage->txn_begin;
47 my $builder = t
::lib
::TestBuilder
->new;
48 my $library = $builder->build({
51 my $itemtype = $builder->build({
55 # Create a biblio instance for testing
56 t
::lib
::Mocks
::mock_preference
('marcflavour', 'MARC21');
57 my ($bibnum, $bibitemnum) = get_biblio
();
60 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $library->{branchcode
}, holdingbranch
=> $library->{branchcode
}, location
=> $location, itype
=> $itemtype->{itemtype
} } , $bibnum);
61 cmp_ok
($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
62 cmp_ok
($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
65 my $getitem = GetItem
($itemnumber);
66 cmp_ok
($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
67 cmp_ok
($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
68 is
( $getitem->{location
}, $location, "The location should not have been modified" );
69 is
( $getitem->{permanent_location
}, $location, "The permanent_location should have been set to the location value" );
71 # Modify item; setting barcode.
72 ModItem
({ barcode
=> '987654321' }, $bibnum, $itemnumber);
73 my $moditem = GetItem
($itemnumber);
74 cmp_ok
($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
77 DelItem
({ biblionumber
=> $bibnum, itemnumber
=> $itemnumber });
78 my $getdeleted = GetItem
($itemnumber);
79 is
($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
81 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $library->{branchcode
}, holdingbranch
=> $library->{branchcode
}, location
=> $location, permanent_location
=> 'my permanent location', itype
=> $itemtype->{itemtype
} } , $bibnum);
82 $getitem = GetItem
($itemnumber);
83 is
( $getitem->{location
}, $location, "The location should not have been modified" );
84 is
( $getitem->{permanent_location
}, 'my permanent location', "The permanent_location should not have modified" );
86 ModItem
({ location
=> $location }, $bibnum, $itemnumber);
87 $getitem = GetItem
($itemnumber);
88 is
( $getitem->{location
}, $location, "The location should have been set to correct location" );
89 is
( $getitem->{permanent_location
}, $location, "The permanent_location should have been set to location" );
91 ModItem
({ location
=> 'CART' }, $bibnum, $itemnumber);
92 $getitem = GetItem
($itemnumber);
93 is
( $getitem->{location
}, 'CART', "The location should have been set to CART" );
94 is
( $getitem->{permanent_location
}, $location, "The permanent_location should not have been set to CART" );
96 t
::lib
::Mocks
::mock_preference
('item-level_itypes', '1');
97 $getitem = GetItem
($itemnumber);
98 is
( $getitem->{itype
}, $itemtype->{itemtype
}, "Itemtype set correctly when using item_level-itypes" );
99 t
::lib
::Mocks
::mock_preference
('item-level_itypes', '0');
100 $getitem = GetItem
($itemnumber);
101 is
( $getitem->{itype
}, undef, "Itemtype set correctly when not using item_level-itypes" );
103 $schema->storage->txn_rollback;
106 subtest
'GetHiddenItemnumbers tests' => sub {
110 # This sub is controlled by the OpacHiddenItems system preference.
112 $schema->storage->txn_begin;
114 my $builder = t
::lib
::TestBuilder
->new;
115 my $library1 = $builder->build({
119 my $library2 = $builder->build({
122 my $itemtype = $builder->build({
123 source
=> 'Itemtype',
126 # Create a new biblio
127 t
::lib
::Mocks
::mock_preference
('marcflavour', 'MARC21');
128 my ($biblionumber, $biblioitemnumber) = get_biblio
();
131 my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem
(
133 homebranch
=> $library1->{branchcode
},
134 holdingbranch
=> $library1->{branchcode
},
136 itype
=> $itemtype->{itemtype
},
140 my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem
(
142 homebranch
=> $library2->{branchcode
},
143 holdingbranch
=> $library2->{branchcode
},
145 itype
=> $itemtype->{itemtype
},
151 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
154 push @items, GetItem
( $item1_itemnumber );
155 push @items, GetItem
( $item2_itemnumber );
157 # Empty OpacHiddenItems
158 t
::lib
::Mocks
::mock_preference
('OpacHiddenItems','');
159 ok
( !defined( GetHiddenItemnumbers
( @items ) ),
160 "Hidden items list undef if OpacHiddenItems empty");
163 t
::lib
::Mocks
::mock_preference
('OpacHiddenItems',' ');
164 ok
( scalar GetHiddenItemnumbers
( @items ) == 0,
165 "Hidden items list empty if OpacHiddenItems only contains blanks");
167 # One variable / value
170 t
::lib
::Mocks
::mock_preference
( 'OpacHiddenItems', $opachiddenitems );
171 @hidden = GetHiddenItemnumbers
( @items );
172 ok
( scalar @hidden == 1, "Only one hidden item");
173 is
( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
175 # One variable, two values
178 t
::lib
::Mocks
::mock_preference
( 'OpacHiddenItems', $opachiddenitems );
179 @hidden = GetHiddenItemnumbers
( @items );
180 ok
( scalar @hidden == 2, "Two items hidden");
181 is_deeply
( \
@hidden, \
@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
183 # Two variables, a value each
186 homebranch: [$library2->{branchcode}]
188 t
::lib
::Mocks
::mock_preference
( 'OpacHiddenItems', $opachiddenitems );
189 @hidden = GetHiddenItemnumbers
( @items );
190 ok
( scalar @hidden == 2, "Two items hidden");
191 is_deeply
( \
@hidden, \
@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
193 # Valid OpacHiddenItems, empty list
195 @hidden = GetHiddenItemnumbers
( @items );
196 ok
( scalar @hidden == 0, "Empty items list, no item hidden");
198 $schema->storage->txn_rollback;
201 subtest
'GetItemsInfo tests' => sub {
205 $schema->storage->txn_begin;
207 my $builder = t
::lib
::TestBuilder
->new;
208 my $library1 = $builder->build({
211 my $library2 = $builder->build({
214 my $itemtype = $builder->build({
215 source
=> 'Itemtype',
219 my ($biblionumber, $biblioitemnumber) = get_biblio
();
221 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem
(
223 homebranch
=> $library1->{branchcode
},
224 holdingbranch
=> $library2->{branchcode
},
225 itype
=> $itemtype->{itemtype
},
230 my $library = Koha
::Libraries
->find( $library1->{branchcode
} );
231 $library->opac_info("homebranch OPAC info");
234 $library = Koha
::Libraries
->find( $library2->{branchcode
} );
235 $library->opac_info("holdingbranch OPAC info");
238 my @results = GetItemsInfo
( $biblionumber );
239 ok
( @results, 'GetItemsInfo returns results');
240 is
( $results[0]->{ home_branch_opac_info
}, "homebranch OPAC info",
241 'GetItemsInfo returns the correct home branch OPAC info notice' );
242 is
( $results[0]->{ holding_branch_opac_info
}, "holdingbranch OPAC info",
243 'GetItemsInfo returns the correct holding branch OPAC info notice' );
244 is
( exists( $results[0]->{ onsite_checkout
} ), 1,
245 'GetItemsInfo returns a onsite_checkout key' );
247 $schema->storage->txn_rollback;
250 subtest
q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
254 $schema->storage->txn_begin;
256 my $biblio = $schema->resultset('Biblio')->create({
257 title
=> "Test title",
258 biblioitems
=> [ { itemtype
=> 'BIB_LEVEL' } ],
260 my $biblioitem = $biblio->biblioitems->first;
261 my $item = $schema->resultset('Item')->create({
262 biblioitemnumber
=> $biblioitem->biblioitemnumber,
263 biblionumber
=> $biblio->biblionumber,
264 itype
=> "ITEM_LEVEL",
267 t
::lib
::Mocks
::mock_preference
( 'item-level_itypes', 0 );
268 is
( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
270 t
::lib
::Mocks
::mock_preference
( 'item-level_itypes', 1 );
271 is
( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
273 # If itemtype is not defined and item-level_level item types are set
274 # fallback to biblio-level itemtype (Bug 14651) and warn
275 $item->itype( undef );
277 my $effective_itemtype;
278 warning_is
{ $effective_itemtype = $item->effective_itemtype() }
279 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
280 '->effective_itemtype() raises a warning when falling back to bib-level';
282 ok
( defined $effective_itemtype &&
283 $effective_itemtype eq 'BIB_LEVEL',
284 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
286 $schema->storage->txn_rollback;
289 subtest
'SearchItems test' => sub {
292 $schema->storage->txn_begin;
293 my $dbh = C4
::Context
->dbh;
294 my $builder = t
::lib
::TestBuilder
->new;
296 my $library1 = $builder->build({
299 my $library2 = $builder->build({
302 my $itemtype = $builder->build({
303 source
=> 'Itemtype',
306 t
::lib
::Mocks
::mock_preference
('marcflavour', 'MARC21');
307 my $cpl_items_before = SearchItemsByField
( 'homebranch', $library1->{branchcode
});
309 my ($biblionumber) = get_biblio
();
311 my (undef, $initial_items_count) = SearchItems
(undef, {rows
=> 1});
314 my (undef, undef, $item1_itemnumber) = AddItem
({
315 homebranch
=> $library1->{branchcode
},
316 holdingbranch
=> $library1->{branchcode
},
317 itype
=> $itemtype->{itemtype
},
319 my (undef, undef, $item2_itemnumber) = AddItem
({
320 homebranch
=> $library2->{branchcode
},
321 holdingbranch
=> $library2->{branchcode
},
322 itype
=> $itemtype->{itemtype
},
325 my ($items, $total_results);
327 ($items, $total_results) = SearchItems
();
328 is
($total_results, $initial_items_count + 2, "Created 2 new items");
329 is
(scalar @
$items, $total_results, "SearchItems() returns all items");
331 ($items, $total_results) = SearchItems
(undef, {rows
=> 1});
332 is
($total_results, $initial_items_count + 2);
333 is
(scalar @
$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
335 # Search all items where homebranch = 'CPL'
337 field
=> 'homebranch',
338 query
=> $library1->{branchcode
},
341 ($items, $total_results) = SearchItems
($filter);
342 ok
($total_results > 0, "There is at least one CPL item");
343 my $all_items_are_CPL = 1;
344 foreach my $item (@
$items) {
345 if ($item->{homebranch
} ne $library1->{branchcode
}) {
346 $all_items_are_CPL = 0;
350 ok
($all_items_are_CPL, "All items returned by SearchItems are from CPL");
352 # Search all items where homebranch != 'CPL'
354 field
=> 'homebranch',
355 query
=> $library1->{branchcode
},
358 ($items, $total_results) = SearchItems
($filter);
359 ok
($total_results > 0, "There is at least one non-CPL item");
360 my $all_items_are_not_CPL = 1;
361 foreach my $item (@
$items) {
362 if ($item->{homebranch
} eq $library1->{branchcode
}) {
363 $all_items_are_not_CPL = 0;
367 ok
($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
369 # Search all items where biblio title (245$a) is like 'Silence in the %'
371 field
=> 'marc:245$a',
372 query
=> 'Silence in the %',
375 ($items, $total_results) = SearchItems
($filter);
376 ok
($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
378 # Search all items where biblio title is 'Silence in the library'
379 # and homebranch is 'CPL'
381 conjunction
=> 'AND',
384 field
=> 'marc:245$a',
385 query
=> 'Silence in the %',
389 field
=> 'homebranch',
390 query
=> $library1->{branchcode
},
395 ($items, $total_results) = SearchItems
($filter);
397 foreach my $item (@
$items) {
398 if ($item->{itemnumber
} == $item1_itemnumber) {
403 ok
($found, "item1 found");
405 my $frameworkcode = q
||;
406 my ($itemfield) = GetMarcFromKohaField
('items.itemnumber', $frameworkcode);
408 # Create item subfield 'z' without link
409 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
410 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
413 my $cache = Koha
::Caches
->get_instance();
414 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
415 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
416 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
417 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
419 my $item3_record = new MARC
::Record
;
420 $item3_record->append_fields(
424 'y' => $itemtype->{itemtype
}
427 my (undef, undef, $item3_itemnumber) = AddItemFromMarc
($item3_record,
430 # Search item where item subfield z is "foobar"
432 field
=> 'marc:' . $itemfield . '$z',
436 ($items, $total_results) = SearchItems
($filter);
437 ok
(scalar @
$items == 1, 'found 1 item with $z = "foobar"');
439 # Link $z to items.itemnotes (and make sure there is no other subfields
441 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
442 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
445 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
446 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
447 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
448 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
450 ModItemFromMarc
($item3_record, $biblionumber, $item3_itemnumber);
452 # Make sure the link is used
453 my $item3 = GetItem
($item3_itemnumber);
454 ok
($item3->{itemnotes
} eq 'foobar', 'itemnotes eq "foobar"');
456 # Do the same search again.
457 # This time it will search in items.itemnotes
458 ($items, $total_results) = SearchItems
($filter);
459 ok
(scalar @
$items == 1, 'found 1 item with itemnotes = "foobar"');
461 my $cpl_items_after = SearchItemsByField
( 'homebranch', $library1->{branchcode
});
462 is
( ( scalar( @
$cpl_items_after ) - scalar ( @
$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
464 $schema->storage->txn_rollback;
467 subtest
'Koha::Item(s) tests' => sub {
471 $schema->storage->txn_begin();
473 my $builder = t
::lib
::TestBuilder
->new;
474 my $library1 = $builder->build({
477 my $library2 = $builder->build({
480 my $itemtype = $builder->build({
481 source
=> 'Itemtype',
484 # Create a biblio and item for testing
485 t
::lib
::Mocks
::mock_preference
('marcflavour', 'MARC21');
486 my ($bibnum, $bibitemnum) = get_biblio
();
487 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem
(
489 homebranch
=> $library1->{branchcode
},
490 holdingbranch
=> $library2->{branchcode
},
491 itype
=> $itemtype->{itemtype
},
497 my $item = Koha
::Items
->find( $itemnumber );
498 is
( ref($item), 'Koha::Item', "Got Koha::Item" );
500 my $homebranch = $item->home_branch();
501 is
( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
502 is
( $homebranch->branchcode(), $library1->{branchcode
}, "Home branch code matches homebranch" );
504 my $holdingbranch = $item->holding_branch();
505 is
( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
506 is
( $holdingbranch->branchcode(), $library2->{branchcode
}, "Home branch code matches holdingbranch" );
508 $schema->storage->txn_rollback;
511 subtest
'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
514 $schema->storage->txn_begin();
516 my $builder = t
::lib
::TestBuilder
->new;
517 my $library1 = $builder->build({
520 my $library2 = $builder->build({
523 my $itemtype = $builder->build({
524 source
=> 'Itemtype',
527 my ( $biblionumber, $biblioitemnumber ) = get_biblio
();
529 { homebranch
=> $library1->{branchcode
}, holdingbranch
=> $library1->{branchcode
} },
530 { homebranch
=> $library1->{branchcode
}, holdingbranch
=> $library1->{branchcode
} },
531 { homebranch
=> $library1->{branchcode
}, holdingbranch
=> $library1->{branchcode
} },
532 { homebranch
=> $library2->{branchcode
}, holdingbranch
=> $library2->{branchcode
} },
533 { homebranch
=> $library2->{branchcode
}, holdingbranch
=> $library2->{branchcode
} },
534 { homebranch
=> $library1->{branchcode
}, holdingbranch
=> $library2->{branchcode
} },
535 { homebranch
=> $library1->{branchcode
}, holdingbranch
=> $library2->{branchcode
} },
536 { homebranch
=> $library1->{branchcode
}, holdingbranch
=> $library2->{branchcode
} },
538 my $number_of_items = scalar @
$item_infos;
539 my $number_of_items_with_homebranch_is_CPL =
540 grep { $_->{homebranch
} eq $library1->{branchcode
} } @
$item_infos;
543 for my $item_info (@
$item_infos) {
544 my ( undef, undef, $itemnumber ) = AddItem
(
546 homebranch
=> $item_info->{homebranch
},
547 holdingbranch
=> $item_info->{holdingbanch
},
548 itype
=> $itemtype->{itemtype
},
552 push @itemnumbers, $itemnumber;
555 # Emptied the OpacHiddenItems pref
556 t
::lib
::Mocks
::mock_preference
( 'OpacHiddenItems', '' );
559 C4
::Biblio
::GetMarcFromKohaField
( 'items.itemnumber', '' );
560 my $record = C4
::Biblio
::GetMarcBiblio
({ biblionumber
=> $biblionumber });
561 warning_is
{ C4
::Biblio
::EmbedItemsInMarcBiblio
() }
562 { carped
=> 'EmbedItemsInMarcBiblio: No MARC record passed' },
563 'Should crap is no record passed.';
565 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $biblionumber );
566 my @items = $record->field($itemfield);
567 is
( scalar @items, $number_of_items, 'Should return all items' );
569 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $biblionumber,
570 [ $itemnumbers[1], $itemnumbers[3] ] );
571 @items = $record->field($itemfield);
572 is
( scalar @items, 2, 'Should return all items present in the list' );
574 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $biblionumber, undef, 1 );
575 @items = $record->field($itemfield);
576 is
( scalar @items, $number_of_items, 'Should return all items for opac' );
578 my $opachiddenitems = "
579 homebranch: ['$library1->{branchcode}']";
580 t
::lib
::Mocks
::mock_preference
( 'OpacHiddenItems', $opachiddenitems );
582 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $biblionumber );
583 @items = $record->field($itemfield);
586 'Even with OpacHiddenItems set, all items should have been embedded' );
588 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $biblionumber, undef, 1 );
589 @items = $record->field($itemfield);
592 $number_of_items - $number_of_items_with_homebranch_is_CPL,
593 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
597 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
598 t
::lib
::Mocks
::mock_preference
( 'OpacHiddenItems', $opachiddenitems );
599 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $biblionumber, undef, 1 );
600 @items = $record->field($itemfield);
604 'For OPAC, If all items are hidden, no item should have been embedded'
607 $schema->storage->txn_rollback;
611 subtest
'C4::Items::_build_default_values_for_mod_marc' => sub {
614 $schema->storage->txn_begin();
616 my $builder = t
::lib
::TestBuilder
->new;
617 my $framework = $builder->build({
618 source
=> 'BiblioFramework',
620 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
622 source
=> 'MarcSubfieldStructure',
624 frameworkcode
=> $framework->{frameworkcode
},
625 kohafield
=> 'biblio.biblionumber',
631 source
=> 'MarcSubfieldStructure',
633 frameworkcode
=> $framework->{frameworkcode
},
634 kohafield
=> 'biblioitems.biblioitemnumber',
639 my $mss_itemnumber = $builder->build({
640 source
=> 'MarcSubfieldStructure',
642 frameworkcode
=> $framework->{frameworkcode
},
643 kohafield
=> 'items.itemnumber',
649 my $mss_barcode = $builder->build({
650 source
=> 'MarcSubfieldStructure',
652 frameworkcode
=> $framework->{frameworkcode
},
653 kohafield
=> 'items.barcode',
659 my $mss_itemtype = $builder->build({
660 source
=> 'MarcSubfieldStructure',
662 frameworkcode
=> $framework->{frameworkcode
},
663 kohafield
=> 'items.itype',
669 my $itemtype = $builder->build({ source
=> 'Itemtype' })->{itemtype
};
671 # Create a record with a barcode
672 my ($biblionumber) = get_biblio
( $framework->{frameworkcode
} );
673 my $item_record = new MARC
::Record
;
674 my $a_barcode = 'a barcode';
675 my $barcode_field = MARC
::Field
->new(
680 my $itemtype_field = MARC
::Field
->new(
684 $item_record->append_fields( $barcode_field );
685 my (undef, undef, $item_itemnumber) = AddItemFromMarc
($item_record, $biblionumber);
687 # Make sure everything has been set up
688 my $item = GetItem
($item_itemnumber);
689 is
( $item->{barcode
}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
691 # Delete the barcode field and save the record
692 $item_record->delete_fields( $barcode_field );
693 $item_record->append_fields( $itemtype_field ); # itemtype is mandatory
694 ModItemFromMarc
($item_record, $biblionumber, $item_itemnumber);
695 $item = GetItem
($item_itemnumber);
696 is
( $item->{barcode
}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
698 # Re-add the barcode field and save the record
699 $item_record->append_fields( $barcode_field );
700 ModItemFromMarc
($item_record, $biblionumber, $item_itemnumber);
701 $item = GetItem
($item_itemnumber);
702 is
( $item->{barcode
}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
705 my $dbh = C4
::Context
->dbh;
707 DELETE FROM marc_subfield_structure
708 WHERE kohafield
= 'items.barcode'
709 AND frameworkcode
= ?
710 |, undef, $framework->{frameworkcode
} );
712 # And make sure the caches are cleared
713 my $cache = Koha
::Caches
->get_instance();
714 $cache->clear_from_cache("MarcStructure-0-" . $framework->{frameworkcode
});
715 $cache->clear_from_cache("MarcStructure-1-" . $framework->{frameworkcode
});
716 $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode
});
717 $cache->clear_from_cache("MarcSubfieldStructure-" . $framework->{frameworkcode
});
719 # Update the MARC field with another value
720 $item_record->delete_fields( $barcode_field );
721 my $another_barcode = 'another_barcode';
722 my $another_barcode_field = MARC
::Field
->new(
724 p
=> $another_barcode,
726 $item_record->append_fields( $another_barcode_field );
727 # The DB value should not have been updated
728 ModItemFromMarc
($item_record, $biblionumber, $item_itemnumber);
729 $item = GetItem
($item_itemnumber);
730 is
( $item->{barcode
}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
732 $schema->storage->txn_rollback;
735 subtest
'_mod_item_dates' => sub {
738 is
( C4
::Items
::_mod_item_dates
(), undef, 'Call without parameters' );
739 is
( C4
::Items
::_mod_item_dates
(1), undef, 'Call without hashref' );
743 itemcallnumber
=> 'V II 149 1963',
746 $orgitem = { %$item };
747 C4
::Items
::_mod_item_dates
($item);
748 is_deeply
( $item, $orgitem, 'No dates passed to _mod_item_dates' );
750 # add two correct dates
751 t
::lib
::Mocks
::mock_preference
('dateformat', 'us');
752 $item->{dateaccessioned
} = '01/31/2016';
753 $item->{onloan
} = $item->{dateaccessioned
};
754 $orgitem = { %$item };
755 C4
::Items
::_mod_item_dates
($item);
756 is
( $item->{dateaccessioned
}, '2016-01-31', 'dateaccessioned is fine' );
757 is
( $item->{onloan
}, '2016-01-31', 'onloan is fine too' );
760 # add some invalid dates
761 $item->{notexistingcolumndate
} = '13/1/2015'; # wrong format
762 $item->{anotherdate
} = 'tralala'; # even worse
763 $item->{myzerodate
} = '0000-00-00'; # wrong too
764 C4
::Items
::_mod_item_dates
($item);
765 is
( $item->{notexistingcolumndate
}, undef, 'Invalid date became NULL' );
766 is
( $item->{anotherdate
}, undef, 'Second invalid date became NULL too' );
767 is
( $item->{myzerodate
}, undef, '0000-00-00 became NULL too' );
769 # check if itemlost_on was not touched
770 $item->{itemlost_on
} = '12345678';
771 $item->{withdrawn_on
} = '12/31/2015 23:59:00';
772 $orgitem = { %$item };
773 C4
::Items
::_mod_item_dates
($item);
774 is_deeply
( $item, $orgitem, 'Colums with _on are not touched' );
776 t
::lib
::Mocks
::mock_preference
('dateformat', 'metric');
777 $item->{dateaccessioned
} = '01/31/2016'; #wrong
778 $item->{yetanotherdatetime
} = '20/01/2016 13:58:00'; #okay
779 C4
::Items
::_mod_item_dates
($item);
780 is
( $item->{dateaccessioned
}, undef, 'dateaccessioned wrong format' );
781 is
( $item->{yetanotherdatetime
}, '2016-01-20 13:58:00',
782 'yetanotherdatetime is ok' );
785 subtest
'get_hostitemnumbers_of' => sub {
788 my $bib = MARC
::Record
->new();
790 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
791 MARC
::Field
->new('245', ' ', ' ', a
=> 'Silence in the library'),
792 MARC
::Field
->new('773', ' ', ' ', b
=> 'b without 0 or 9'),
794 my ($biblionumber, $bibitemnum) = AddBiblio
($bib, '');
796 my @itemnumbers = C4
::Items
::get_hostitemnumbers_of
( $biblionumber );
797 is
( @itemnumbers, 0, );
800 # Helper method to set up a Biblio.
802 my ( $frameworkcode ) = @_;
803 $frameworkcode //= '';
804 my $bib = MARC
::Record
->new();
806 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
807 MARC
::Field
->new('245', ' ', ' ', a
=> 'Silence in the library'),
809 my ($bibnum, $bibitemnum) = AddBiblio
($bib, $frameworkcode);
810 return ($bibnum, $bibitemnum);