Bug 21774: Cloned item subfields disappear when editing an item
[koha.git] / t / db_dependent / Items.t
blobbc7aaa9feb215af2a7abf56a2fe0d3a6d8366024
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 => 15;
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" );
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'} . '.');
84 # Delete item.
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 {
115 plan tests => 6;
117 $schema->storage->txn_begin;
119 my $builder = t::lib::TestBuilder->new;
120 my $item = $builder->build({
121 source => 'Item',
122 value => {
123 itemlost => 0,
124 damaged => 0,
125 withdrawn => 0,
126 itemlost_on => undef,
127 damaged_on => undef,
128 withdrawn_on => undef,
132 my @fields = qw( itemlost withdrawn damaged );
133 for my $field (@fields) {
134 $item->{$field} = 1;
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" );
139 $item->{$field} = 0;
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 {
151 plan tests => 11;
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({
159 source => 'Branch',
162 my $library2 = $builder->build({
163 source => 'Branch',
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();
173 # Add two items
174 my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
176 homebranch => $library1->{branchcode},
177 holdingbranch => $library1->{branchcode},
178 withdrawn => 1,
179 itype => $itemtype->{itemtype},
181 $biblionumber
183 my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
185 homebranch => $library2->{branchcode},
186 holdingbranch => $library2->{branchcode},
187 withdrawn => 0,
188 itype => $itemtype->{itemtype},
190 $biblionumber
193 my $opachiddenitems;
194 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
195 my @hidden;
196 my @items;
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 => \@items } ) ),
203 "Hidden items list undef if OpacHiddenItems empty");
205 # Blank spaces
206 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
207 ok( scalar GetHiddenItemnumbers( { items => \@items } ) == 0,
208 "Hidden items list empty if OpacHiddenItems only contains blanks");
210 # One variable / value
211 $opachiddenitems = "
212 withdrawn: [1]";
213 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
214 @hidden = GetHiddenItemnumbers( { items => \@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
219 $opachiddenitems = "
220 withdrawn: [1,0]";
221 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
222 @hidden = GetHiddenItemnumbers( { items => \@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
227 $opachiddenitems = "
228 withdrawn: [1]
229 homebranch: [$library2->{branchcode}]
231 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
232 @hidden = GetHiddenItemnumbers( { items => \@items } );
233 ok( scalar @hidden == 2, "Two items hidden");
234 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
236 # Override hidden with patron category
237 t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'S' );
238 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'PT' } );
239 ok( scalar @hidden == 2, "Two items still hidden");
240 @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'S' } );
241 ok( scalar @hidden == 0, "Two items not hidden");
243 # Valid OpacHiddenItems, empty list
244 @items = ();
245 @hidden = GetHiddenItemnumbers( { items => \@items } );
246 ok( scalar @hidden == 0, "Empty items list, no item hidden");
248 $schema->storage->txn_rollback;
251 subtest 'GetItemsInfo tests' => sub {
253 plan tests => 4;
255 $schema->storage->txn_begin;
257 my $builder = t::lib::TestBuilder->new;
258 my $library1 = $builder->build({
259 source => 'Branch',
261 my $library2 = $builder->build({
262 source => 'Branch',
264 my $itemtype = $builder->build({
265 source => 'Itemtype',
268 # Add a biblio
269 my ($biblionumber, $biblioitemnumber) = get_biblio();
270 # Add an item
271 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
273 homebranch => $library1->{branchcode},
274 holdingbranch => $library2->{branchcode},
275 itype => $itemtype->{itemtype},
277 $biblionumber
280 my $library = Koha::Libraries->find( $library1->{branchcode} );
281 $library->opac_info("homebranch OPAC info");
282 $library->store;
284 $library = Koha::Libraries->find( $library2->{branchcode} );
285 $library->opac_info("holdingbranch OPAC info");
286 $library->store;
288 my @results = GetItemsInfo( $biblionumber );
289 ok( @results, 'GetItemsInfo returns results');
290 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
291 'GetItemsInfo returns the correct home branch OPAC info notice' );
292 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
293 'GetItemsInfo returns the correct holding branch OPAC info notice' );
294 is( exists( $results[0]->{ onsite_checkout } ), 1,
295 'GetItemsInfo returns a onsite_checkout key' );
297 $schema->storage->txn_rollback;
300 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
302 plan tests => 4;
304 $schema->storage->txn_begin;
306 my $biblio = $schema->resultset('Biblio')->create({
307 title => "Test title",
308 datecreated => dt_from_string,
309 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
311 my $biblioitem = $biblio->biblioitems->first;
312 my $item = $schema->resultset('Item')->create({
313 biblioitemnumber => $biblioitem->biblioitemnumber,
314 biblionumber => $biblio->biblionumber,
315 itype => "ITEM_LEVEL",
318 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
319 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
321 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
322 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
324 # If itemtype is not defined and item-level_level item types are set
325 # fallback to biblio-level itemtype (Bug 14651) and warn
326 $item->itype( undef );
327 $item->update();
328 my $effective_itemtype;
329 warning_is { $effective_itemtype = $item->effective_itemtype() }
330 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
331 '->effective_itemtype() raises a warning when falling back to bib-level';
333 ok( defined $effective_itemtype &&
334 $effective_itemtype eq 'BIB_LEVEL',
335 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
337 $schema->storage->txn_rollback;
340 subtest 'SearchItems test' => sub {
341 plan tests => 14;
343 $schema->storage->txn_begin;
344 my $dbh = C4::Context->dbh;
345 my $builder = t::lib::TestBuilder->new;
347 my $library1 = $builder->build({
348 source => 'Branch',
350 my $library2 = $builder->build({
351 source => 'Branch',
353 my $itemtype = $builder->build({
354 source => 'Itemtype',
357 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
358 my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
360 my ($biblionumber) = get_biblio();
362 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
364 # Add two items
365 my (undef, undef, $item1_itemnumber) = AddItem({
366 homebranch => $library1->{branchcode},
367 holdingbranch => $library1->{branchcode},
368 itype => $itemtype->{itemtype},
369 }, $biblionumber);
370 my (undef, undef, $item2_itemnumber) = AddItem({
371 homebranch => $library2->{branchcode},
372 holdingbranch => $library2->{branchcode},
373 itype => $itemtype->{itemtype},
374 }, $biblionumber);
376 my ($items, $total_results);
378 ($items, $total_results) = SearchItems();
379 is($total_results, $initial_items_count + 2, "Created 2 new items");
380 is(scalar @$items, $total_results, "SearchItems() returns all items");
382 ($items, $total_results) = SearchItems(undef, {rows => 1});
383 is($total_results, $initial_items_count + 2);
384 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
386 # Search all items where homebranch = 'CPL'
387 my $filter = {
388 field => 'homebranch',
389 query => $library1->{branchcode},
390 operator => '=',
392 ($items, $total_results) = SearchItems($filter);
393 ok($total_results > 0, "There is at least one CPL item");
394 my $all_items_are_CPL = 1;
395 foreach my $item (@$items) {
396 if ($item->{homebranch} ne $library1->{branchcode}) {
397 $all_items_are_CPL = 0;
398 last;
401 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
403 # Search all items where homebranch != 'CPL'
404 $filter = {
405 field => 'homebranch',
406 query => $library1->{branchcode},
407 operator => '!=',
409 ($items, $total_results) = SearchItems($filter);
410 ok($total_results > 0, "There is at least one non-CPL item");
411 my $all_items_are_not_CPL = 1;
412 foreach my $item (@$items) {
413 if ($item->{homebranch} eq $library1->{branchcode}) {
414 $all_items_are_not_CPL = 0;
415 last;
418 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
420 # Search all items where biblio title (245$a) is like 'Silence in the %'
421 $filter = {
422 field => 'marc:245$a',
423 query => 'Silence in the %',
424 operator => 'like',
426 ($items, $total_results) = SearchItems($filter);
427 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
429 # Search all items where biblio title is 'Silence in the library'
430 # and homebranch is 'CPL'
431 $filter = {
432 conjunction => 'AND',
433 filters => [
435 field => 'marc:245$a',
436 query => 'Silence in the %',
437 operator => 'like',
440 field => 'homebranch',
441 query => $library1->{branchcode},
442 operator => '=',
446 ($items, $total_results) = SearchItems($filter);
447 my $found = 0;
448 foreach my $item (@$items) {
449 if ($item->{itemnumber} == $item1_itemnumber) {
450 $found = 1;
451 last;
454 ok($found, "item1 found");
456 my $frameworkcode = q||;
457 my ($itemfield) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
459 # Create item subfield 'z' without link
460 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
461 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
463 # Clear cache
464 my $cache = Koha::Caches->get_instance();
465 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
466 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
467 $cache->clear_from_cache("default_value_for_mod_marc-");
468 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
470 my $item3_record = new MARC::Record;
471 $item3_record->append_fields(
472 new MARC::Field(
473 $itemfield, '', '',
474 'z' => 'foobar',
475 'y' => $itemtype->{itemtype}
478 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
479 $biblionumber);
481 # Search item where item subfield z is "foobar"
482 $filter = {
483 field => 'marc:' . $itemfield . '$z',
484 query => 'foobar',
485 operator => 'like',
487 ($items, $total_results) = SearchItems($filter);
488 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
490 # Link $z to items.itemnotes (and make sure there is no other subfields
491 # linked to it)
492 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
493 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
495 # Clear cache
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 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
503 # Make sure the link is used
504 my $item3 = GetItem($item3_itemnumber);
505 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
507 # Do the same search again.
508 # This time it will search in items.itemnotes
509 ($items, $total_results) = SearchItems($filter);
510 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
512 my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
513 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
515 $schema->storage->txn_rollback;
518 subtest 'Koha::Item(s) tests' => sub {
520 plan tests => 5;
522 $schema->storage->txn_begin();
524 my $builder = t::lib::TestBuilder->new;
525 my $library1 = $builder->build({
526 source => 'Branch',
528 my $library2 = $builder->build({
529 source => 'Branch',
531 my $itemtype = $builder->build({
532 source => 'Itemtype',
535 # Create a biblio and item for testing
536 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
537 my ($bibnum, $bibitemnum) = get_biblio();
538 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
540 homebranch => $library1->{branchcode},
541 holdingbranch => $library2->{branchcode},
542 itype => $itemtype->{itemtype},
544 $bibnum
547 # Get item.
548 my $item = Koha::Items->find( $itemnumber );
549 is( ref($item), 'Koha::Item', "Got Koha::Item" );
551 my $homebranch = $item->home_branch();
552 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
553 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
555 my $holdingbranch = $item->holding_branch();
556 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
557 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
559 $schema->storage->txn_rollback;
562 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
563 plan tests => 8;
565 $schema->storage->txn_begin();
567 my $builder = t::lib::TestBuilder->new;
568 my $library1 = $builder->build({
569 source => 'Branch',
571 my $library2 = $builder->build({
572 source => 'Branch',
574 my $itemtype = $builder->build({
575 source => 'Itemtype',
578 my ( $biblionumber, $biblioitemnumber ) = get_biblio();
579 my $item_infos = [
580 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
581 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
582 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
583 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
584 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
585 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
586 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
587 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
589 my $number_of_items = scalar @$item_infos;
590 my $number_of_items_with_homebranch_is_CPL =
591 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
593 my @itemnumbers;
594 for my $item_info (@$item_infos) {
595 my ( undef, undef, $itemnumber ) = AddItem(
597 homebranch => $item_info->{homebranch},
598 holdingbranch => $item_info->{holdingbanch},
599 itype => $itemtype->{itemtype},
601 $biblionumber
603 push @itemnumbers, $itemnumber;
606 # Emptied the OpacHiddenItems pref
607 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
609 my ($itemfield) =
610 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
611 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
612 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
613 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
614 'Should carp is no record passed.';
616 C4::Biblio::EmbedItemsInMarcBiblio({
617 marc_record => $record,
618 biblionumber => $biblionumber });
619 my @items = $record->field($itemfield);
620 is( scalar @items, $number_of_items, 'Should return all items' );
622 my $marc_with_items = C4::Biblio::GetMarcBiblio({
623 biblionumber => $biblionumber,
624 embed_items => 1 });
625 is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
627 C4::Biblio::EmbedItemsInMarcBiblio({
628 marc_record => $record,
629 biblionumber => $biblionumber,
630 item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
631 @items = $record->field($itemfield);
632 is( scalar @items, 2, 'Should return all items present in the list' );
634 C4::Biblio::EmbedItemsInMarcBiblio({
635 marc_record => $record,
636 biblionumber => $biblionumber,
637 opac => 1 });
638 @items = $record->field($itemfield);
639 is( scalar @items, $number_of_items, 'Should return all items for opac' );
641 my $opachiddenitems = "
642 homebranch: ['$library1->{branchcode}']";
643 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
645 C4::Biblio::EmbedItemsInMarcBiblio({
646 marc_record => $record,
647 biblionumber => $biblionumber });
648 @items = $record->field($itemfield);
649 is( scalar @items,
650 $number_of_items,
651 'Even with OpacHiddenItems set, all items should have been embedded' );
653 C4::Biblio::EmbedItemsInMarcBiblio({
654 marc_record => $record,
655 biblionumber => $biblionumber,
656 opac => 1 });
657 @items = $record->field($itemfield);
659 scalar @items,
660 $number_of_items - $number_of_items_with_homebranch_is_CPL,
661 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
664 $opachiddenitems = "
665 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
666 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
667 C4::Biblio::EmbedItemsInMarcBiblio({
668 marc_record => $record,
669 biblionumber => $biblionumber,
670 opac => 1 });
671 @items = $record->field($itemfield);
673 scalar @items,
675 'For OPAC, If all items are hidden, no item should have been embedded'
678 $schema->storage->txn_rollback;
682 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
683 plan tests => 4;
685 $schema->storage->txn_begin();
687 my $builder = t::lib::TestBuilder->new;
688 my $framework = $builder->build({ source => 'BiblioFramework' });
690 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
691 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '999', tagsubfield => [ 'c', 'd' ] })->delete;
692 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'c', kohafield => 'biblio.biblionumber' })->store;
693 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '999', tagsubfield => 'd', kohafield => 'biblioitems.biblioitemnumber' })->store;
695 # Same for item fields: itemnumber, barcode, itype
696 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => [ '9', 'p', 'y' ] })->delete;
697 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
698 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
699 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
700 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
702 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
704 # Create a record with a barcode
705 my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
706 my $item_record = new MARC::Record;
707 my $a_barcode = 'a barcode';
708 my $barcode_field = MARC::Field->new(
709 '952', ' ', ' ',
710 p => $a_barcode,
711 y => $itemtype
713 my $itemtype_field = MARC::Field->new(
714 '952', ' ', ' ',
715 y => $itemtype
717 $item_record->append_fields( $barcode_field );
718 my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
720 # Make sure everything has been set up
721 my $item = GetItem($item_itemnumber);
722 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
724 # Delete the barcode field and save the record
725 $item_record->delete_fields( $barcode_field );
726 $item_record->append_fields( $itemtype_field ); # itemtype is mandatory
727 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
728 $item = GetItem($item_itemnumber);
729 is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
731 # Re-add the barcode field and save the record
732 $item_record->append_fields( $barcode_field );
733 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
734 $item = GetItem($item_itemnumber);
735 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
737 # Remove the mapping for barcode
738 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
740 # And make sure the caches are cleared
741 my $cache = Koha::Caches->get_instance();
742 $cache->clear_from_cache("default_value_for_mod_marc-");
743 $cache->clear_from_cache("MarcSubfieldStructure-");
745 # Update the MARC field with another value
746 $item_record->delete_fields( $barcode_field );
747 my $another_barcode = 'another_barcode';
748 my $another_barcode_field = MARC::Field->new(
749 '952', ' ', ' ',
750 p => $another_barcode,
752 $item_record->append_fields( $another_barcode_field );
753 # The DB value should not have been updated
754 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
755 $item = GetItem($item_itemnumber);
756 is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
758 $cache->clear_from_cache("default_value_for_mod_marc-");
759 $cache->clear_from_cache( "MarcSubfieldStructure-" );
760 $schema->storage->txn_rollback;
763 subtest '_mod_item_dates' => sub {
764 plan tests => 11;
766 is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
767 is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
769 my $orgitem;
770 my $item = {
771 itemcallnumber => 'V II 149 1963',
772 barcode => '109304',
774 $orgitem = { %$item };
775 C4::Items::_mod_item_dates($item);
776 is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
778 # add two correct dates
779 t::lib::Mocks::mock_preference('dateformat', 'us');
780 $item->{dateaccessioned} = '01/31/2016';
781 $item->{onloan} = $item->{dateaccessioned};
782 $orgitem = { %$item };
783 C4::Items::_mod_item_dates($item);
784 is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
785 is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
788 # add some invalid dates
789 $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
790 $item->{anotherdate} = 'tralala'; # even worse
791 $item->{myzerodate} = '0000-00-00'; # wrong too
792 C4::Items::_mod_item_dates($item);
793 is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
794 is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
795 is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
797 # check if itemlost_on was not touched
798 $item->{itemlost_on} = '12345678';
799 $item->{withdrawn_on} = '12/31/2015 23:59:00';
800 $item->{damaged_on} = '01/20/2017 09:00:00';
801 $orgitem = { %$item };
802 C4::Items::_mod_item_dates($item);
803 is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
805 t::lib::Mocks::mock_preference('dateformat', 'metric');
806 $item->{dateaccessioned} = '01/31/2016'; #wrong
807 $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
808 C4::Items::_mod_item_dates($item);
809 is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
810 is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
811 'yetanotherdatetime is ok' );
814 subtest 'get_hostitemnumbers_of' => sub {
815 plan tests => 1;
817 $schema->storage->txn_begin;
819 my $bib = MARC::Record->new();
820 $bib->append_fields(
821 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
822 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
823 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
825 my ($biblionumber, $bibitemnum) = AddBiblio($bib, '');
827 my @itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
828 is( @itemnumbers, 0, );
830 $schema->storage->txn_rollback;
833 subtest 'Test logging for ModItem' => sub {
835 plan tests => 3;
837 t::lib::Mocks::mock_preference('CataloguingLog', 1);
839 $schema->storage->txn_begin;
841 my $builder = t::lib::TestBuilder->new;
842 my $library = $builder->build({
843 source => 'Branch',
845 my $itemtype = $builder->build({
846 source => 'Itemtype',
849 # Create a biblio instance for testing
850 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
851 my ($bibnum, $bibitemnum) = get_biblio();
853 # Add an item.
854 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
856 # False means no logging
857 $schema->resultset('ActionLog')->search()->delete();
858 ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 0 });
859 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
861 # True means logging
862 $schema->resultset('ActionLog')->search()->delete();
863 ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 1 });
864 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
866 # Undefined defaults to true
867 $schema->resultset('ActionLog')->search()->delete();
868 ModItem({ location => $location }, $bibnum, $itemnumber);
869 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
871 $schema->storage->txn_rollback;
874 subtest 'Check stockrotationitem relationship' => sub {
875 plan tests => 1;
877 $schema->storage->txn_begin();
879 my $builder = t::lib::TestBuilder->new;
880 my $item = $builder->build({ source => 'Item' });
882 $builder->build({
883 source => 'Stockrotationitem',
884 value => { itemnumber_id => $item->{itemnumber} }
887 my $sritem = Koha::Items->find($item->{itemnumber})->stockrotationitem;
888 isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
890 $schema->storage->txn_rollback;
893 subtest 'Check add_to_rota method' => sub {
894 plan tests => 2;
896 $schema->storage->txn_begin();
898 my $builder = t::lib::TestBuilder->new;
899 my $item = $builder->build({ source => 'Item' });
900 my $rota = $builder->build({ source => 'Stockrotationrota' });
901 my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
903 $builder->build({
904 source => 'Stockrotationstage',
905 value => { rota_id => $rota->{rota_id} },
908 my $sritem = Koha::Items->find($item->{itemnumber});
909 $sritem->add_to_rota($rota->{rota_id});
912 Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
913 $srrota->stockrotationstages->next->stage_id,
914 "Adding to a rota a new sritem item being assigned to its first stage."
917 my $newrota = $builder->build({ source => 'Stockrotationrota' });
919 my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
921 $builder->build({
922 source => 'Stockrotationstage',
923 value => { rota_id => $newrota->{rota_id} },
926 $sritem->add_to_rota($newrota->{rota_id});
929 Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
930 $srnewrota->stockrotationstages->next->stage_id,
931 "Moving an item results in that sritem being assigned to the new first stage."
934 $schema->storage->txn_rollback;
937 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
938 plan tests => 3;
939 $schema->storage->txn_begin;
941 my $builder = t::lib::TestBuilder->new;
942 my $biblio = $builder->build({ source => 'Biblio', value => { frameworkcode => q{} } });
943 my $item = $builder->build({ source => 'Item', value => { biblionumber => $biblio->{biblionumber}, ccode => 'A|B' } });
945 Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
946 Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
947 my $mapping = Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '952', tagsubfield => '8', kohafield => 'items.ccode' })->store;
949 # Start testing
950 my $marc = C4::Items::Item2Marc( $item, $biblio->{biblionumber} );
951 my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
952 is( @subs, 2, 'Expect two subfields' );
953 is( $subs[0], 'A', 'First subfield matches' );
954 is( $subs[1], 'B', 'Second subfield matches' );
956 $schema->storage->txn_rollback;
959 # Helper method to set up a Biblio.
960 sub get_biblio {
961 my ( $frameworkcode ) = @_;
962 $frameworkcode //= '';
963 my $bib = MARC::Record->new();
964 $bib->append_fields(
965 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
966 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
968 my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
969 return ($bibnum, $bibitemnum);