Bug 14598 [QA Followup] - Correct the behavior of GetItem
[koha.git] / t / db_dependent / Items.t
blobe3c00c843b818f176c7b6b66875c27ced3a444ff
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;
21 use MARC::Record;
22 use C4::Biblio;
23 use Koha::Database;
24 use Koha::Library;
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
29 use Test::More tests => 10;
31 use Test::Warn;
33 BEGIN {
34 use_ok('C4::Items');
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 {
43 plan tests => 16;
45 $schema->storage->txn_begin;
47 my $builder = t::lib::TestBuilder->new;
48 my $library = $builder->build({
49 source => 'Branch',
50 });
51 my $itemtype = $builder->build({
52 source => 'Itemtype',
53 });
55 # Create a biblio instance for testing
56 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
57 my ($bibnum, $bibitemnum) = get_biblio();
59 # Add an item.
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.");
64 # Get item.
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'} . '.');
76 # Delete item.
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 C4::Context->set_preference('item-level_itypes', '1');
97 $getitem = GetItem($itemnumber);
98 is( $getitem->{itype}, $itemtype->{itemtype}, "Itemtype set correctly when using item_level-itypes" );
99 C4::Context->set_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 {
108 plan tests => 9;
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({
116 source => 'Branch',
119 my $library2 = $builder->build({
120 source => 'Branch',
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();
130 # Add two items
131 my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
133 homebranch => $library1->{branchcode},
134 holdingbranch => $library1->{branchcode},
135 withdrawn => 1,
136 itype => $itemtype->{itemtype},
138 $biblionumber
140 my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
142 homebranch => $library2->{branchcode},
143 holdingbranch => $library2->{branchcode},
144 withdrawn => 0,
145 itype => $itemtype->{itemtype},
147 $biblionumber
150 my $opachiddenitems;
151 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
152 my @hidden;
153 my @items;
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");
162 # Blank spaces
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
168 $opachiddenitems = "
169 withdrawn: [1]";
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
176 $opachiddenitems = "
177 withdrawn: [1,0]";
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
184 $opachiddenitems = "
185 withdrawn: [1]
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
194 @items = ();
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 {
203 plan tests => 4;
205 $schema->storage->txn_begin;
207 my $builder = t::lib::TestBuilder->new;
208 my $library1 = $builder->build({
209 source => 'Branch',
211 my $library2 = $builder->build({
212 source => 'Branch',
214 my $itemtype = $builder->build({
215 source => 'Itemtype',
218 # Add a biblio
219 my ($biblionumber, $biblioitemnumber) = get_biblio();
220 # Add an item
221 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
223 homebranch => $library1->{branchcode},
224 holdingbranch => $library2->{branchcode},
225 itype => $itemtype->{itemtype},
227 $biblionumber
230 my $library = Koha::Libraries->find( $library1->{branchcode} );
231 $library->opac_info("homebranch OPAC info");
232 $library->store;
234 $library = Koha::Libraries->find( $library2->{branchcode} );
235 $library->opac_info("holdingbranch OPAC info");
236 $library->store;
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 {
252 plan tests => 4;
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 );
276 $item->update();
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 {
290 plan tests => 14;
292 $schema->storage->txn_begin;
293 my $dbh = C4::Context->dbh;
294 my $builder = t::lib::TestBuilder->new;
296 my $library1 = $builder->build({
297 source => 'Branch',
299 my $library2 = $builder->build({
300 source => 'Branch',
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});
313 # Add two items
314 my (undef, undef, $item1_itemnumber) = AddItem({
315 homebranch => $library1->{branchcode},
316 holdingbranch => $library1->{branchcode},
317 itype => $itemtype->{itemtype},
318 }, $biblionumber);
319 my (undef, undef, $item2_itemnumber) = AddItem({
320 homebranch => $library2->{branchcode},
321 holdingbranch => $library2->{branchcode},
322 itype => $itemtype->{itemtype},
323 }, $biblionumber);
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'
336 my $filter = {
337 field => 'homebranch',
338 query => $library1->{branchcode},
339 operator => '=',
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;
347 last;
350 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
352 # Search all items where homebranch != 'CPL'
353 $filter = {
354 field => 'homebranch',
355 query => $library1->{branchcode},
356 operator => '!=',
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;
364 last;
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 %'
370 $filter = {
371 field => 'marc:245$a',
372 query => 'Silence in the %',
373 operator => 'like',
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'
380 $filter = {
381 conjunction => 'AND',
382 filters => [
384 field => 'marc:245$a',
385 query => 'Silence in the %',
386 operator => 'like',
389 field => 'homebranch',
390 query => $library1->{branchcode},
391 operator => '=',
395 ($items, $total_results) = SearchItems($filter);
396 my $found = 0;
397 foreach my $item (@$items) {
398 if ($item->{itemnumber} == $item1_itemnumber) {
399 $found = 1;
400 last;
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);
412 # Clear cache
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(
421 new MARC::Field($itemfield, '', '', 'z' => 'foobar')
423 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
424 $biblionumber);
426 # Search item where item subfield z is "foobar"
427 $filter = {
428 field => 'marc:' . $itemfield . '$z',
429 query => 'foobar',
430 operator => 'like',
432 ($items, $total_results) = SearchItems($filter);
433 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
435 # Link $z to items.itemnotes (and make sure there is no other subfields
436 # linked to it)
437 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
438 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
440 # Clear cache
441 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
442 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
443 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
444 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
446 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
448 # Make sure the link is used
449 my $item3 = GetItem($item3_itemnumber);
450 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
452 # Do the same search again.
453 # This time it will search in items.itemnotes
454 ($items, $total_results) = SearchItems($filter);
455 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
457 my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
458 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
460 $schema->storage->txn_rollback;
463 subtest 'Koha::Item(s) tests' => sub {
465 plan tests => 5;
467 $schema->storage->txn_begin();
469 my $builder = t::lib::TestBuilder->new;
470 my $library1 = $builder->build({
471 source => 'Branch',
473 my $library2 = $builder->build({
474 source => 'Branch',
476 my $itemtype = $builder->build({
477 source => 'Itemtype',
480 # Create a biblio and item for testing
481 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
482 my ($bibnum, $bibitemnum) = get_biblio();
483 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
485 homebranch => $library1->{branchcode},
486 holdingbranch => $library2->{branchcode},
487 itype => $itemtype->{itemtype},
489 $bibnum
492 # Get item.
493 my $item = Koha::Items->find( $itemnumber );
494 is( ref($item), 'Koha::Item', "Got Koha::Item" );
496 my $homebranch = $item->home_branch();
497 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
498 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
500 my $holdingbranch = $item->holding_branch();
501 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
502 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
504 $schema->storage->txn_rollback;
507 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
508 plan tests => 7;
510 $schema->storage->txn_begin();
512 my $builder = t::lib::TestBuilder->new;
513 my $library1 = $builder->build({
514 source => 'Branch',
516 my $library2 = $builder->build({
517 source => 'Branch',
519 my $itemtype = $builder->build({
520 source => 'Itemtype',
523 my ( $biblionumber, $biblioitemnumber ) = get_biblio();
524 my $item_infos = [
525 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
526 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
527 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
528 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
529 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
530 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
531 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
532 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
534 my $number_of_items = scalar @$item_infos;
535 my $number_of_items_with_homebranch_is_CPL =
536 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
538 my @itemnumbers;
539 for my $item_info (@$item_infos) {
540 my ( undef, undef, $itemnumber ) = AddItem(
542 homebranch => $item_info->{homebranch},
543 holdingbranch => $item_info->{holdingbanch},
544 itype => $itemtype->{itemtype},
546 $biblionumber
548 push @itemnumbers, $itemnumber;
551 # Emptied the OpacHiddenItems pref
552 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
554 my ($itemfield) =
555 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
556 my $record = C4::Biblio::GetMarcBiblio($biblionumber);
557 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
558 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
559 'Should crap is no record passed.';
561 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
562 my @items = $record->field($itemfield);
563 is( scalar @items, $number_of_items, 'Should return all items' );
565 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber,
566 [ $itemnumbers[1], $itemnumbers[3] ] );
567 @items = $record->field($itemfield);
568 is( scalar @items, 2, 'Should return all items present in the list' );
570 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
571 @items = $record->field($itemfield);
572 is( scalar @items, $number_of_items, 'Should return all items for opac' );
574 my $opachiddenitems = "
575 homebranch: ['$library1->{branchcode}']";
576 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
578 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
579 @items = $record->field($itemfield);
580 is( scalar @items,
581 $number_of_items,
582 'Even with OpacHiddenItems set, all items should have been embedded' );
584 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
585 @items = $record->field($itemfield);
587 scalar @items,
588 $number_of_items - $number_of_items_with_homebranch_is_CPL,
589 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
592 $opachiddenitems = "
593 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
594 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
595 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
596 @items = $record->field($itemfield);
598 scalar @items,
600 'For OPAC, If all items are hidden, no item should have been embedded'
603 $schema->storage->txn_rollback;
607 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
608 plan tests => 4;
610 $schema->storage->txn_begin();
612 my $builder = t::lib::TestBuilder->new;
613 my $framework = $builder->build({
614 source => 'BiblioFramework',
616 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
617 $builder->build({
618 source => 'MarcSubfieldStructure',
619 value => {
620 frameworkcode => $framework->{frameworkcode},
621 kohafield => 'biblio.biblionumber',
622 tagfield => '999',
623 tagsubfield => 'c',
626 $builder->build({
627 source => 'MarcSubfieldStructure',
628 value => {
629 frameworkcode => $framework->{frameworkcode},
630 kohafield => 'biblioitems.biblioitemnumber',
631 tagfield => '999',
632 tagsubfield => 'd',
635 my $mss_itemnumber = $builder->build({
636 source => 'MarcSubfieldStructure',
637 value => {
638 frameworkcode => $framework->{frameworkcode},
639 kohafield => 'items.itemnumber',
640 tagfield => '952',
641 tagsubfield => '9',
645 my $mss_barcode = $builder->build({
646 source => 'MarcSubfieldStructure',
647 value => {
648 frameworkcode => $framework->{frameworkcode},
649 kohafield => 'items.barcode',
650 tagfield => '952',
651 tagsubfield => 'p',
655 # Create a record with a barcode
656 my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
657 my $item_record = new MARC::Record;
658 my $a_barcode = 'a barcode';
659 my $barcode_field = MARC::Field->new(
660 '952', ' ', ' ',
661 p => $a_barcode,
663 $item_record->append_fields( $barcode_field );
664 my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
666 # Make sure everything has been set up
667 my $item = GetItem($item_itemnumber);
668 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
670 # Delete the barcode field and save the record
671 $item_record->delete_fields( $barcode_field );
672 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
673 $item = GetItem($item_itemnumber);
674 is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
676 # Re-add the barcode field and save the record
677 $item_record->append_fields( $barcode_field );
678 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
679 $item = GetItem($item_itemnumber);
680 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
682 # Remove the mapping
683 my $dbh = C4::Context->dbh;
684 $dbh->do(q|
685 DELETE FROM marc_subfield_structure
686 WHERE kohafield = 'items.barcode'
687 AND frameworkcode = ?
688 |, undef, $framework->{frameworkcode} );
690 # And make sure the caches are cleared
691 my $cache = Koha::Caches->get_instance();
692 $cache->clear_from_cache("MarcStructure-0-" . $framework->{frameworkcode});
693 $cache->clear_from_cache("MarcStructure-1-" . $framework->{frameworkcode});
694 $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode});
695 $cache->clear_from_cache("MarcSubfieldStructure-" . $framework->{frameworkcode});
697 # Update the MARC field with another value
698 $item_record->delete_fields( $barcode_field );
699 my $another_barcode = 'another_barcode';
700 my $another_barcode_field = MARC::Field->new(
701 '952', ' ', ' ',
702 p => $another_barcode,
704 $item_record->append_fields( $another_barcode_field );
705 # The DB value should not have been updated
706 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
707 $item = GetItem($item_itemnumber);
708 is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
710 $schema->storage->txn_rollback;
713 # Helper method to set up a Biblio.
714 sub get_biblio {
715 my ( $frameworkcode ) = @_;
716 $frameworkcode //= '';
717 my $bib = MARC::Record->new();
718 $bib->append_fields(
719 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
720 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
722 my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
723 return ($bibnum, $bibitemnum);