Bug 17776: (QA follow-up) Consistent regex for Plack detection
[koha.git] / t / db_dependent / Items.t
blob92151d662d4f512244f42dafcce1ae5ef4ff7300
1 #!/usr/bin/perl
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
8 # version.
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
19 use Modern::Perl;
20 use Data::Dumper;
22 use MARC::Record;
23 use C4::Items;
24 use C4::Biblio;
25 use Koha::Items;
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Library;
29 use Koha::DateUtils;
30 use Koha::MarcSubfieldStructures;
31 use Koha::Caches;
33 use t::lib::Mocks;
34 use t::lib::TestBuilder;
36 use Test::More tests => 12;
38 use Test::Warn;
40 my $schema = Koha::Database->new->schema;
41 my $location = 'My Location';
43 subtest 'General Add, Get and Del tests' => sub {
45 plan tests => 16;
47 $schema->storage->txn_begin;
49 my $builder = t::lib::TestBuilder->new;
50 my $library = $builder->build({
51 source => 'Branch',
52 });
53 my $itemtype = $builder->build({
54 source => 'Itemtype',
55 });
57 # Create a biblio instance for testing
58 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
59 my ($bibnum, $bibitemnum) = get_biblio();
61 # Add an item.
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.");
66 # Get item.
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" );
73 # Modify item; setting barcode.
74 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
75 my $moditem = GetItem($itemnumber);
76 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
78 # Delete item.
79 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
80 my $getdeleted = GetItem($itemnumber);
81 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
83 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $bibnum);
84 $getitem = GetItem($itemnumber);
85 is( $getitem->{location}, $location, "The location should not have been modified" );
86 is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" );
88 ModItem({ location => $location }, $bibnum, $itemnumber);
89 $getitem = GetItem($itemnumber);
90 is( $getitem->{location}, $location, "The location should have been set to correct location" );
91 is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to location" );
93 ModItem({ location => 'CART' }, $bibnum, $itemnumber);
94 $getitem = GetItem($itemnumber);
95 is( $getitem->{location}, 'CART', "The location should have been set to CART" );
96 is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" );
98 t::lib::Mocks::mock_preference('item-level_itypes', '1');
99 $getitem = GetItem($itemnumber);
100 is( $getitem->{itype}, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
101 t::lib::Mocks::mock_preference('item-level_itypes', '0');
102 $getitem = GetItem($itemnumber);
103 is( $getitem->{itype}, undef, "Itemtype set correctly when not using item-level_itypes" );
105 $schema->storage->txn_rollback;
108 subtest 'ModItem tests' => sub {
109 plan tests => 6;
111 $schema->storage->txn_begin;
113 my $builder = t::lib::TestBuilder->new;
114 my $item = $builder->build({
115 source => 'Item',
116 value => {
117 itemlost => 0,
118 damaged => 0,
119 withdrawn => 0,
120 itemlost_on => undef,
121 damaged_on => undef,
122 withdrawn_on => undef,
126 my @fields = qw( itemlost withdrawn damaged );
127 for my $field (@fields) {
128 $item->{$field} = 1;
129 ModItem( $item, $item->{biblionumber}, $item->{itemnumber} );
130 my $post_mod_item = Koha::Items->find({ itemnumber => $item->{itemnumber} })->unblessed;
131 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" );
133 $item->{$field} = 0;
134 ModItem( $item, $item->{biblionumber}, $item->{itemnumber} );
135 $post_mod_item = Koha::Items->find({ itemnumber => $item->{itemnumber} })->unblessed;
136 is( $post_mod_item->{$field."_on"}, undef, "When clearing $field, $field"."_on is cleared" );
139 $schema->storage->txn_rollback;
143 subtest 'GetHiddenItemnumbers tests' => sub {
145 plan tests => 9;
147 # This sub is controlled by the OpacHiddenItems system preference.
149 $schema->storage->txn_begin;
151 my $builder = t::lib::TestBuilder->new;
152 my $library1 = $builder->build({
153 source => 'Branch',
156 my $library2 = $builder->build({
157 source => 'Branch',
159 my $itemtype = $builder->build({
160 source => 'Itemtype',
163 # Create a new biblio
164 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
165 my ($biblionumber, $biblioitemnumber) = get_biblio();
167 # Add two items
168 my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
170 homebranch => $library1->{branchcode},
171 holdingbranch => $library1->{branchcode},
172 withdrawn => 1,
173 itype => $itemtype->{itemtype},
175 $biblionumber
177 my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
179 homebranch => $library2->{branchcode},
180 holdingbranch => $library2->{branchcode},
181 withdrawn => 0,
182 itype => $itemtype->{itemtype},
184 $biblionumber
187 my $opachiddenitems;
188 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
189 my @hidden;
190 my @items;
191 push @items, GetItem( $item1_itemnumber );
192 push @items, GetItem( $item2_itemnumber );
194 # Empty OpacHiddenItems
195 t::lib::Mocks::mock_preference('OpacHiddenItems','');
196 ok( !defined( GetHiddenItemnumbers( @items ) ),
197 "Hidden items list undef if OpacHiddenItems empty");
199 # Blank spaces
200 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
201 ok( scalar GetHiddenItemnumbers( @items ) == 0,
202 "Hidden items list empty if OpacHiddenItems only contains blanks");
204 # One variable / value
205 $opachiddenitems = "
206 withdrawn: [1]";
207 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
208 @hidden = GetHiddenItemnumbers( @items );
209 ok( scalar @hidden == 1, "Only one hidden item");
210 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
212 # One variable, two values
213 $opachiddenitems = "
214 withdrawn: [1,0]";
215 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
216 @hidden = GetHiddenItemnumbers( @items );
217 ok( scalar @hidden == 2, "Two items hidden");
218 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
220 # Two variables, a value each
221 $opachiddenitems = "
222 withdrawn: [1]
223 homebranch: [$library2->{branchcode}]
225 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
226 @hidden = GetHiddenItemnumbers( @items );
227 ok( scalar @hidden == 2, "Two items hidden");
228 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
230 # Valid OpacHiddenItems, empty list
231 @items = ();
232 @hidden = GetHiddenItemnumbers( @items );
233 ok( scalar @hidden == 0, "Empty items list, no item hidden");
235 $schema->storage->txn_rollback;
238 subtest 'GetItemsInfo tests' => sub {
240 plan tests => 4;
242 $schema->storage->txn_begin;
244 my $builder = t::lib::TestBuilder->new;
245 my $library1 = $builder->build({
246 source => 'Branch',
248 my $library2 = $builder->build({
249 source => 'Branch',
251 my $itemtype = $builder->build({
252 source => 'Itemtype',
255 # Add a biblio
256 my ($biblionumber, $biblioitemnumber) = get_biblio();
257 # Add an item
258 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
260 homebranch => $library1->{branchcode},
261 holdingbranch => $library2->{branchcode},
262 itype => $itemtype->{itemtype},
264 $biblionumber
267 my $library = Koha::Libraries->find( $library1->{branchcode} );
268 $library->opac_info("homebranch OPAC info");
269 $library->store;
271 $library = Koha::Libraries->find( $library2->{branchcode} );
272 $library->opac_info("holdingbranch OPAC info");
273 $library->store;
275 my @results = GetItemsInfo( $biblionumber );
276 ok( @results, 'GetItemsInfo returns results');
277 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
278 'GetItemsInfo returns the correct home branch OPAC info notice' );
279 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
280 'GetItemsInfo returns the correct holding branch OPAC info notice' );
281 is( exists( $results[0]->{ onsite_checkout } ), 1,
282 'GetItemsInfo returns a onsite_checkout key' );
284 $schema->storage->txn_rollback;
287 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
289 plan tests => 4;
291 $schema->storage->txn_begin;
293 my $biblio = $schema->resultset('Biblio')->create({
294 title => "Test title",
295 datecreated => dt_from_string,
296 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
298 my $biblioitem = $biblio->biblioitems->first;
299 my $item = $schema->resultset('Item')->create({
300 biblioitemnumber => $biblioitem->biblioitemnumber,
301 biblionumber => $biblio->biblionumber,
302 itype => "ITEM_LEVEL",
305 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
306 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
308 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
309 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
311 # If itemtype is not defined and item-level_level item types are set
312 # fallback to biblio-level itemtype (Bug 14651) and warn
313 $item->itype( undef );
314 $item->update();
315 my $effective_itemtype;
316 warning_is { $effective_itemtype = $item->effective_itemtype() }
317 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
318 '->effective_itemtype() raises a warning when falling back to bib-level';
320 ok( defined $effective_itemtype &&
321 $effective_itemtype eq 'BIB_LEVEL',
322 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
324 $schema->storage->txn_rollback;
327 subtest 'SearchItems test' => sub {
328 plan tests => 14;
330 $schema->storage->txn_begin;
331 my $dbh = C4::Context->dbh;
332 my $builder = t::lib::TestBuilder->new;
334 my $library1 = $builder->build({
335 source => 'Branch',
337 my $library2 = $builder->build({
338 source => 'Branch',
340 my $itemtype = $builder->build({
341 source => 'Itemtype',
344 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
345 my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
347 my ($biblionumber) = get_biblio();
349 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
351 # Add two items
352 my (undef, undef, $item1_itemnumber) = AddItem({
353 homebranch => $library1->{branchcode},
354 holdingbranch => $library1->{branchcode},
355 itype => $itemtype->{itemtype},
356 }, $biblionumber);
357 my (undef, undef, $item2_itemnumber) = AddItem({
358 homebranch => $library2->{branchcode},
359 holdingbranch => $library2->{branchcode},
360 itype => $itemtype->{itemtype},
361 }, $biblionumber);
363 my ($items, $total_results);
365 ($items, $total_results) = SearchItems();
366 is($total_results, $initial_items_count + 2, "Created 2 new items");
367 is(scalar @$items, $total_results, "SearchItems() returns all items");
369 ($items, $total_results) = SearchItems(undef, {rows => 1});
370 is($total_results, $initial_items_count + 2);
371 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
373 # Search all items where homebranch = 'CPL'
374 my $filter = {
375 field => 'homebranch',
376 query => $library1->{branchcode},
377 operator => '=',
379 ($items, $total_results) = SearchItems($filter);
380 ok($total_results > 0, "There is at least one CPL item");
381 my $all_items_are_CPL = 1;
382 foreach my $item (@$items) {
383 if ($item->{homebranch} ne $library1->{branchcode}) {
384 $all_items_are_CPL = 0;
385 last;
388 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
390 # Search all items where homebranch != 'CPL'
391 $filter = {
392 field => 'homebranch',
393 query => $library1->{branchcode},
394 operator => '!=',
396 ($items, $total_results) = SearchItems($filter);
397 ok($total_results > 0, "There is at least one non-CPL item");
398 my $all_items_are_not_CPL = 1;
399 foreach my $item (@$items) {
400 if ($item->{homebranch} eq $library1->{branchcode}) {
401 $all_items_are_not_CPL = 0;
402 last;
405 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
407 # Search all items where biblio title (245$a) is like 'Silence in the %'
408 $filter = {
409 field => 'marc:245$a',
410 query => 'Silence in the %',
411 operator => 'like',
413 ($items, $total_results) = SearchItems($filter);
414 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
416 # Search all items where biblio title is 'Silence in the library'
417 # and homebranch is 'CPL'
418 $filter = {
419 conjunction => 'AND',
420 filters => [
422 field => 'marc:245$a',
423 query => 'Silence in the %',
424 operator => 'like',
427 field => 'homebranch',
428 query => $library1->{branchcode},
429 operator => '=',
433 ($items, $total_results) = SearchItems($filter);
434 my $found = 0;
435 foreach my $item (@$items) {
436 if ($item->{itemnumber} == $item1_itemnumber) {
437 $found = 1;
438 last;
441 ok($found, "item1 found");
443 my $frameworkcode = q||;
444 my ($itemfield) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
446 # Create item subfield 'z' without link
447 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
448 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
450 # Clear cache
451 my $cache = Koha::Caches->get_instance();
452 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
453 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
454 $cache->clear_from_cache("default_value_for_mod_marc-");
455 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
457 my $item3_record = new MARC::Record;
458 $item3_record->append_fields(
459 new MARC::Field(
460 $itemfield, '', '',
461 'z' => 'foobar',
462 'y' => $itemtype->{itemtype}
465 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
466 $biblionumber);
468 # Search item where item subfield z is "foobar"
469 $filter = {
470 field => 'marc:' . $itemfield . '$z',
471 query => 'foobar',
472 operator => 'like',
474 ($items, $total_results) = SearchItems($filter);
475 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
477 # Link $z to items.itemnotes (and make sure there is no other subfields
478 # linked to it)
479 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
480 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
482 # Clear cache
483 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
484 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
485 $cache->clear_from_cache("default_value_for_mod_marc-");
486 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
488 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
490 # Make sure the link is used
491 my $item3 = GetItem($item3_itemnumber);
492 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
494 # Do the same search again.
495 # This time it will search in items.itemnotes
496 ($items, $total_results) = SearchItems($filter);
497 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
499 my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
500 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
502 $schema->storage->txn_rollback;
505 subtest 'Koha::Item(s) tests' => sub {
507 plan tests => 5;
509 $schema->storage->txn_begin();
511 my $builder = t::lib::TestBuilder->new;
512 my $library1 = $builder->build({
513 source => 'Branch',
515 my $library2 = $builder->build({
516 source => 'Branch',
518 my $itemtype = $builder->build({
519 source => 'Itemtype',
522 # Create a biblio and item for testing
523 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
524 my ($bibnum, $bibitemnum) = get_biblio();
525 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
527 homebranch => $library1->{branchcode},
528 holdingbranch => $library2->{branchcode},
529 itype => $itemtype->{itemtype},
531 $bibnum
534 # Get item.
535 my $item = Koha::Items->find( $itemnumber );
536 is( ref($item), 'Koha::Item', "Got Koha::Item" );
538 my $homebranch = $item->home_branch();
539 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
540 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
542 my $holdingbranch = $item->holding_branch();
543 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
544 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
546 $schema->storage->txn_rollback;
549 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
550 plan tests => 7;
552 $schema->storage->txn_begin();
554 my $builder = t::lib::TestBuilder->new;
555 my $library1 = $builder->build({
556 source => 'Branch',
558 my $library2 = $builder->build({
559 source => 'Branch',
561 my $itemtype = $builder->build({
562 source => 'Itemtype',
565 my ( $biblionumber, $biblioitemnumber ) = get_biblio();
566 my $item_infos = [
567 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
568 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
569 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
570 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
571 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
572 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
573 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
574 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
576 my $number_of_items = scalar @$item_infos;
577 my $number_of_items_with_homebranch_is_CPL =
578 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
580 my @itemnumbers;
581 for my $item_info (@$item_infos) {
582 my ( undef, undef, $itemnumber ) = AddItem(
584 homebranch => $item_info->{homebranch},
585 holdingbranch => $item_info->{holdingbanch},
586 itype => $itemtype->{itemtype},
588 $biblionumber
590 push @itemnumbers, $itemnumber;
593 # Emptied the OpacHiddenItems pref
594 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
596 my ($itemfield) =
597 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
598 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
599 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
600 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
601 'Should crap is no record passed.';
603 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
604 my @items = $record->field($itemfield);
605 is( scalar @items, $number_of_items, 'Should return all items' );
607 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber,
608 [ $itemnumbers[1], $itemnumbers[3] ] );
609 @items = $record->field($itemfield);
610 is( scalar @items, 2, 'Should return all items present in the list' );
612 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
613 @items = $record->field($itemfield);
614 is( scalar @items, $number_of_items, 'Should return all items for opac' );
616 my $opachiddenitems = "
617 homebranch: ['$library1->{branchcode}']";
618 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
620 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
621 @items = $record->field($itemfield);
622 is( scalar @items,
623 $number_of_items,
624 'Even with OpacHiddenItems set, all items should have been embedded' );
626 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
627 @items = $record->field($itemfield);
629 scalar @items,
630 $number_of_items - $number_of_items_with_homebranch_is_CPL,
631 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
634 $opachiddenitems = "
635 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
636 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
637 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
638 @items = $record->field($itemfield);
640 scalar @items,
642 'For OPAC, If all items are hidden, no item should have been embedded'
645 $schema->storage->txn_rollback;
649 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
650 plan tests => 4;
652 $schema->storage->txn_begin();
654 my $builder = t::lib::TestBuilder->new;
655 my $framework = $builder->build({ source => 'BiblioFramework' });
657 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
658 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '999', tagsubfield => [ 'c', 'd' ] })->delete;
659 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'c', kohafield => 'biblio.biblionumber' })->store;
660 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'd', kohafield => 'biblioitems.biblioitemnumber' })->store;
662 # Same for item fields: itemnumber, barcode, itype
663 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => [ '9', 'p', 'y' ] })->delete;
664 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
665 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
666 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
667 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
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(
676 '952', ' ', ' ',
677 p => $a_barcode,
678 y => $itemtype
680 my $itemtype_field = MARC::Field->new(
681 '952', ' ', ' ',
682 y => $itemtype
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' );
704 # Remove the mapping for barcode
705 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
707 # And make sure the caches are cleared
708 my $cache = Koha::Caches->get_instance();
709 $cache->clear_from_cache("default_value_for_mod_marc-");
710 $cache->clear_from_cache("MarcSubfieldStructure-");
712 # Update the MARC field with another value
713 $item_record->delete_fields( $barcode_field );
714 my $another_barcode = 'another_barcode';
715 my $another_barcode_field = MARC::Field->new(
716 '952', ' ', ' ',
717 p => $another_barcode,
719 $item_record->append_fields( $another_barcode_field );
720 # The DB value should not have been updated
721 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
722 $item = GetItem($item_itemnumber);
723 is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
725 $cache->clear_from_cache("default_value_for_mod_marc-");
726 $cache->clear_from_cache( "MarcSubfieldStructure-" );
727 $schema->storage->txn_rollback;
730 subtest '_mod_item_dates' => sub {
731 plan tests => 11;
733 is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
734 is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
736 my $orgitem;
737 my $item = {
738 itemcallnumber => 'V II 149 1963',
739 barcode => '109304',
741 $orgitem = { %$item };
742 C4::Items::_mod_item_dates($item);
743 is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
745 # add two correct dates
746 t::lib::Mocks::mock_preference('dateformat', 'us');
747 $item->{dateaccessioned} = '01/31/2016';
748 $item->{onloan} = $item->{dateaccessioned};
749 $orgitem = { %$item };
750 C4::Items::_mod_item_dates($item);
751 is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
752 is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
755 # add some invalid dates
756 $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
757 $item->{anotherdate} = 'tralala'; # even worse
758 $item->{myzerodate} = '0000-00-00'; # wrong too
759 C4::Items::_mod_item_dates($item);
760 is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
761 is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
762 is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
764 # check if itemlost_on was not touched
765 $item->{itemlost_on} = '12345678';
766 $item->{withdrawn_on} = '12/31/2015 23:59:00';
767 $item->{damaged_on} = '01/20/2017 09:00:00';
768 $orgitem = { %$item };
769 C4::Items::_mod_item_dates($item);
770 is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
772 t::lib::Mocks::mock_preference('dateformat', 'metric');
773 $item->{dateaccessioned} = '01/31/2016'; #wrong
774 $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
775 C4::Items::_mod_item_dates($item);
776 is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
777 is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
778 'yetanotherdatetime is ok' );
781 subtest 'get_hostitemnumbers_of' => sub {
782 plan tests => 1;
784 my $bib = MARC::Record->new();
785 $bib->append_fields(
786 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
787 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
788 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
790 my ($biblionumber, $bibitemnum) = AddBiblio($bib, '');
792 my @itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
793 is( @itemnumbers, 0, );
796 subtest 'Test logging for ModItem' => sub {
798 plan tests => 3;
800 t::lib::Mocks::mock_preference('CataloguingLog', 1);
802 $schema->storage->txn_begin;
804 my $builder = t::lib::TestBuilder->new;
805 my $library = $builder->build({
806 source => 'Branch',
808 my $itemtype = $builder->build({
809 source => 'Itemtype',
812 # Create a biblio instance for testing
813 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
814 my ($bibnum, $bibitemnum) = get_biblio();
816 # Add an item.
817 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
819 # False means no logging
820 $schema->resultset('ActionLog')->search()->delete();
821 ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 0 });
822 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
824 # True means logging
825 $schema->resultset('ActionLog')->search()->delete();
826 ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 1 });
827 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
829 # Undefined defaults to true
830 $schema->resultset('ActionLog')->search()->delete();
831 ModItem({ location => $location }, $bibnum, $itemnumber);
832 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
834 $schema->storage->txn_rollback;
837 # Helper method to set up a Biblio.
838 sub get_biblio {
839 my ( $frameworkcode ) = @_;
840 $frameworkcode //= '';
841 my $bib = MARC::Record->new();
842 $bib->append_fields(
843 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
844 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
846 my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
847 return ($bibnum, $bibitemnum);