3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use C4::Items qw( ModItemTransfer SearchItems AddItemFromMarc ModItemFromMarc get_hostitemnumbers_of Item2Marc ModDateLastSeen );
23 use C4::Biblio qw( GetMarcFromKohaField AddBiblio );
24 use C4::Circulation qw( AddIssue );
27 use Koha::DateUtils qw( dt_from_string );
30 use Koha::MarcSubfieldStructures;
32 use Koha::AuthorisedValues;
35 use t::lib::TestBuilder;
37 use Test::More tests => 12;
41 my $schema = Koha::Database->new->schema;
42 my $location = 'My Location';
44 subtest 'General Add, Get and Del tests' => sub {
48 $schema->storage->txn_begin;
50 my $builder = t::lib::TestBuilder->new;
51 my $library = $builder->build({
54 my $itemtype = $builder->build({
58 # Create a biblio instance for testing
59 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
60 my $biblio = $builder->build_sample_biblio();
63 my $item = $builder->build_sample_item(
65 biblionumber => $biblio->biblionumber,
66 library => $library->{branchcode},
67 location => $location,
68 itype => $itemtype->{itemtype}
71 my $itemnumber = $item->itemnumber;
72 cmp_ok($item->biblionumber, '==', $biblio->biblionumber, "New item is linked to correct biblionumber.");
73 cmp_ok($item->biblioitemnumber, '==', $biblio->biblioitem->biblioitemnumber, "New item is linked to correct biblioitemnumber.");
76 my $getitem = Koha::Items->find($itemnumber);
77 cmp_ok($getitem->itemnumber, '==', $itemnumber, "Retrieved item has correct itemnumber.");
78 cmp_ok($getitem->biblioitemnumber, '==', $item->biblioitemnumber, "Retrieved item has correct biblioitemnumber."); # We are not testing anything useful here
79 is( $getitem->location, $location, "The location should not have been modified" );
80 is( $getitem->permanent_location, $location, "The permanent_location should have been set to the location value" );
83 # Do not modify anything, and do not explode!
84 $getitem->set({})->store;
86 # Modify item; setting barcode.
87 $getitem->barcode('987654321')->store;
88 my $moditem = Koha::Items->find($itemnumber);
89 cmp_ok($moditem->barcode, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->barcode . '.');
93 my $getdeleted = Koha::Items->find($itemnumber);
94 is($getdeleted, undef, "Item deleted as expected.");
96 $itemnumber = $builder->build_sample_item(
98 biblionumber => $biblio->biblionumber,
99 library => $library->{branchcode},
100 location => $location,
101 permanent_location => 'my permanent location',
102 itype => $itemtype->{itemtype}
105 $getitem = Koha::Items->find($itemnumber);
106 is( $getitem->location, $location, "The location should not have been modified" );
107 is( $getitem->permanent_location, 'my permanent location', "The permanent_location should not have modified" );
109 my $new_location = "New location";
110 $getitem->location($new_location)->store;
111 $getitem = Koha::Items->find($itemnumber);
112 is( $getitem->location, $new_location, "The location should have been set to correct location" );
113 is( $getitem->permanent_location, $new_location, "The permanent_location should have been set to location" );
115 $getitem->location('CART')->store;
116 $getitem = Koha::Items->find($itemnumber);
117 is( $getitem->location, 'CART', "The location should have been set to CART" );
118 is( $getitem->permanent_location, $new_location, "The permanent_location should not have been set to CART" );
120 t::lib::Mocks::mock_preference('item-level_itypes', '1');
121 $getitem = Koha::Items->find($itemnumber);
122 is( $getitem->effective_itemtype, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
123 t::lib::Mocks::mock_preference('item-level_itypes', '0');
124 $getitem = Koha::Items->find($itemnumber);
125 is( $getitem->effective_itemtype, $biblio->biblioitem->itemtype, "Itemtype set correctly when not using item-level_itypes" );
127 $schema->storage->txn_rollback;
130 subtest 'ModItemTransfer tests' => sub {
133 $schema->storage->txn_begin;
135 my $builder = t::lib::TestBuilder->new;
136 my $item = $builder->build_sample_item();
138 my $library1 = $builder->build(
143 my $library2 = $builder->build(
149 ModItemTransfer( $item->itemnumber, $library1->{branchcode},
150 $library2->{branchcode}, 'Manual' );
152 my $transfers = Koha::Item::Transfers->search(
154 itemnumber => $item->itemnumber
158 is( $transfers->count, 1, "One transfer created with ModItemTransfer" );
159 $item->discard_changes;
160 is($item->holdingbranch, $library1->{branchcode}, "Items holding branch was updated to frombranch");
162 ModItemTransfer( $item->itemnumber, $library2->{branchcode},
163 $library1->{branchcode}, 'Manual' );
164 $transfers = Koha::Item::Transfers->search(
165 { itemnumber => $item->itemnumber, },
166 { order_by => { '-asc' => 'branchtransfer_id' } }
169 is($transfers->count, 2, "Second transfer recorded on second call of ModItemTransfer");
170 my $transfer1 = $transfers->next;
171 my $transfer2 = $transfers->next;
172 isnt($transfer1->datecancelled, undef, "First transfer marked as cancelled by ModItemTransfer");
173 like($transfer1->cancellation_reason,qr/^Manual/, "First transfer contains cancellation_reason 'Manual'");
174 is($transfer2->datearrived, undef, "Second transfer is now the active transfer");
175 $item->discard_changes;
176 is($item->holdingbranch, $library2->{branchcode}, "Items holding branch was updated to frombranch");
178 # Check 'reason' is populated when passed
179 ModItemTransfer( $item->itemnumber, $library2->{branchcode},
180 $library1->{branchcode}, "Manual" );
182 $transfers = Koha::Item::Transfers->search(
183 { itemnumber => $item->itemnumber, },
184 { order_by => { '-desc' => 'branchtransfer_id' } }
187 my $transfer3 = $transfers->next;
188 is($transfer3->reason, 'Manual', "Reason set via ModItemTransfer");
190 $schema->storage->txn_rollback;
193 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
197 $schema->storage->txn_begin;
199 my $biblio = $schema->resultset('Biblio')->create({
200 title => "Test title",
201 datecreated => dt_from_string,
202 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
204 my $biblioitem = $biblio->biblioitems->first;
205 my $item = $schema->resultset('Item')->create({
206 biblioitemnumber => $biblioitem->biblioitemnumber,
207 biblionumber => $biblio->biblionumber,
208 itype => "ITEM_LEVEL",
211 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
212 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
214 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
215 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
217 # If itemtype is not defined and item-level_level item types are set
218 # fallback to biblio-level itemtype (Bug 14651) and warn
219 $item->itype( undef );
221 my $effective_itemtype;
222 warning_is { $effective_itemtype = $item->effective_itemtype() }
223 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
224 '->effective_itemtype() raises a warning when falling back to bib-level';
226 ok( defined $effective_itemtype &&
227 $effective_itemtype eq 'BIB_LEVEL',
228 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
230 $schema->storage->txn_rollback;
233 subtest 'SearchItems test' => sub {
236 $schema->storage->txn_begin;
237 my $dbh = C4::Context->dbh;
238 my $builder = t::lib::TestBuilder->new;
240 my $library1 = $builder->build({
243 my $library2 = $builder->build({
246 my $itemtype = $builder->build({
247 source => 'Itemtype',
250 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
251 my ($cpl_items_before) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
253 my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
254 $builder->build_sample_biblio({ title => 'Silence in the shadow' });
256 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
259 my $item1 = $builder->build_sample_item(
261 biblionumber => $biblio->biblionumber,
262 library => $library1->{branchcode},
263 itype => $itemtype->{itemtype}
266 my $item1_itemnumber = $item1->itemnumber;
267 my $item2_itemnumber = $builder->build_sample_item(
269 biblionumber => $biblio->biblionumber,
270 library => $library2->{branchcode},
271 itype => $itemtype->{itemtype}
275 my ($items, $total_results);
277 ($items, $total_results) = SearchItems();
279 is($total_results, $initial_items_count + 2, "Created 2 new items");
280 is(scalar @$items, $total_results, "SearchItems() returns all items");
282 ($items, $total_results) = SearchItems(undef, {rows => 1});
283 is($total_results, $initial_items_count + 2);
284 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
286 # Search all items where homebranch = 'CPL'
288 field => 'homebranch',
289 query => $library1->{branchcode},
292 ($items, $total_results) = SearchItems($filter);
293 ok($total_results > 0, "There is at least one CPL item");
294 my $all_items_are_CPL = 1;
295 foreach my $item (@$items) {
296 if ($item->{homebranch} ne $library1->{branchcode}) {
297 $all_items_are_CPL = 0;
301 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
303 # Search all items where homebranch != 'CPL'
305 field => 'homebranch',
306 query => $library1->{branchcode},
309 ($items, $total_results) = SearchItems($filter);
310 ok($total_results > 0, "There is at least one non-CPL item");
311 my $all_items_are_not_CPL = 1;
312 foreach my $item (@$items) {
313 if ($item->{homebranch} eq $library1->{branchcode}) {
314 $all_items_are_not_CPL = 0;
318 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
320 # Search all items where biblio title (245$a) is like 'Silence in the %'
322 field => 'marc:245$a',
323 query => 'Silence in the %',
326 ($items, $total_results) = SearchItems($filter);
327 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
329 # Search all items where biblio title is 'Silence in the library'
330 # and homebranch is 'CPL'
332 conjunction => 'AND',
335 field => 'marc:245$a',
336 query => 'Silence in the %',
340 field => 'homebranch',
341 query => $library1->{branchcode},
346 ($items, $total_results) = SearchItems($filter);
348 foreach my $item (@$items) {
349 if ($item->{itemnumber} == $item1_itemnumber) {
354 ok($found, "item1 found");
356 my $frameworkcode = q||;
357 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
359 # Create item subfield 'z' without link
360 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
361 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
364 my $cache = Koha::Caches->get_instance();
365 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
366 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
367 $cache->clear_from_cache("default_value_for_mod_marc-");
368 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
370 my $item3_record = MARC::Record->new;
371 $item3_record->append_fields(
375 'y' => $itemtype->{itemtype}
378 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
379 $biblio->biblionumber);
381 # Search item where item subfield z is "foobar"
383 field => 'marc:' . $itemfield . '$z',
387 ($items, $total_results) = SearchItems($filter);
388 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
390 # Link $z to items.itemnotes (and make sure there is no other subfields
392 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
393 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
396 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
397 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
398 $cache->clear_from_cache("default_value_for_mod_marc-");
399 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
401 ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
403 # Make sure the link is used
404 my $item3 = Koha::Items->find($item3_itemnumber);
405 is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"');
407 # Do the same search again.
408 # This time it will search in items.itemnotes
409 ($items, $total_results) = SearchItems($filter);
410 is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"');
412 my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
413 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' );
417 conjunction => 'AND',
425 field => 'homebranch',
426 query => $library1->{branchcode},
431 ($items, $total_results) = SearchItems($filter);
432 is($total_results, 1, "Search items.issues issues = 0 returns result (items.issues defaults to 0)");
436 conjunction => 'AND',
439 field => 'new_status',
444 field => 'homebranch',
445 query => $library1->{branchcode},
450 ($items, $total_results) = SearchItems($filter);
451 is($total_results, 0, 'found no item with new_status=0 without ifnull');
453 $filter->{filters}[0]->{ifnull} = 0;
454 ($items, $total_results) = SearchItems($filter);
455 is($total_results, 1, 'found all items of library1 with new_status=0 with ifnull = 0');
457 t::lib::Mocks::mock_userenv({ branchcode => $item1->homebranch });
458 my $patron_borrower = $builder->build_object({ class => 'Koha::Patrons' })->unblessed;
459 AddIssue( $patron_borrower, $item1->barcode );
460 # Search item where item is checked out
462 conjunction => 'AND',
470 field => 'homebranch',
471 query => $item1->homebranch,
476 ($items, $total_results) = SearchItems($filter);
477 ok(scalar @$items == 1, 'found 1 checked out item');
479 # When sorting by descending availability, checked out item should show first
481 sortby => 'availability',
485 field => 'homebranch',
486 query => $item1->homebranch,
489 ($items, $total_results) = SearchItems($filter,$params);
490 is($items->[0]->{barcode}, $item1->barcode, 'Items sorted as expected by availability');
492 subtest 'Search items by ISSN and ISBN with variations' => sub {
495 my $marc_record = MARC::Record->new;
496 # Prepare ISBN for biblio:
497 $marc_record->append_fields( MARC::Field->new( '020', '', '', 'a' => '9780136019701' ) );
498 # Prepare ISSN for biblio:
499 $marc_record->append_fields( MARC::Field->new( '022', '', '', 'a' => '2434561X' ) );
500 my ( $isbnissn_biblionumber ) = AddBiblio( $marc_record, '' );
501 my $isbnissn_biblio = Koha::Biblios->find($isbnissn_biblionumber);
503 my $item = $builder->build_sample_item(
505 biblionumber => $isbnissn_biblio->biblionumber,
508 my $item_itemnumber = $item->itemnumber;
512 query => '978013-6019701',
516 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
517 ($items, $total_results) = SearchItems($filter_isbn);
518 is($total_results, 0, "Search items finds ISBN, no variations");
520 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
521 ($items, $total_results) = SearchItems($filter_isbn);
522 is($total_results, 1, "Search items finds ISBN with variations");
526 query => '2434-561X',
530 t::lib::Mocks::mock_preference('SearchWithISSNVariations', 0);
531 ($items, $total_results) = SearchItems($filter_issn);
532 is($total_results, 0, "Search items finds ISSN, no variations");
534 t::lib::Mocks::mock_preference('SearchWithISSNVariations', 1);
535 ($items, $total_results) = SearchItems($filter_issn);
536 is($total_results, 1, "Search items finds ISSN with variations");
539 $schema->storage->txn_rollback;
542 subtest 'Koha::Item(s) tests' => sub {
546 $schema->storage->txn_begin();
548 my $builder = t::lib::TestBuilder->new;
549 my $library1 = $builder->build({
552 my $library2 = $builder->build({
555 my $itemtype = $builder->build({
556 source => 'Itemtype',
559 # Create a biblio and item for testing
560 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
561 my $biblio = $builder->build_sample_biblio();
562 my $itemnumber = $builder->build_sample_item(
564 biblionumber => $biblio->biblionumber,
565 homebranch => $library1->{branchcode},
566 holdingbranch => $library2->{branchcode},
567 itype => $itemtype->{itemtype}
572 my $item = Koha::Items->find( $itemnumber );
573 is( ref($item), 'Koha::Item', "Got Koha::Item" );
575 my $homebranch = $item->home_branch();
576 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
577 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
579 my $holdingbranch = $item->holding_branch();
580 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
581 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
583 $biblio = $item->biblio();
584 is( ref($item->biblio), 'Koha::Biblio', "Got Koha::Biblio from biblio method" );
585 is( $item->biblio->title(), $biblio->title, 'Title matches biblio title' );
587 $schema->storage->txn_rollback;
590 subtest 'get_hostitemnumbers_of' => sub {
593 $schema->storage->txn_begin;
594 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
595 my $builder = t::lib::TestBuilder->new;
597 # Host item field without 0 or 9
598 my $bib1 = MARC::Record->new();
599 $bib1->append_fields(
600 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
601 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
602 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
604 my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
605 my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
606 is( scalar @itemnumbers1, 0, '773 without 0 or 9');
608 # Correct host item field, analytical records on
609 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
610 my $hostitem = $builder->build_sample_item();
611 my $bib2 = MARC::Record->new();
612 $bib2->append_fields(
613 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
614 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
615 MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
617 my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
618 my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
619 is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
621 # Correct host item field, analytical records off
622 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
623 @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
624 is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
626 $schema->storage->txn_rollback;
629 subtest 'Test logging for ModItem' => sub {
633 t::lib::Mocks::mock_preference('CataloguingLog', 1);
635 $schema->storage->txn_begin;
637 my $builder = t::lib::TestBuilder->new;
638 my $library = $builder->build({
641 my $itemtype = $builder->build({
642 source => 'Itemtype',
645 # Create a biblio instance for testing
646 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
647 my $biblio = $builder->build_sample_biblio();
650 my $item = $builder->build_sample_item(
652 biblionumber => $biblio->biblionumber,
653 library => $library->{homebranch},
654 location => $location,
655 itype => $itemtype->{itemtype}
659 # False means no logging
660 $schema->resultset('ActionLog')->search()->delete();
661 $item->location($location)->store({ log_action => 0 });
662 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
665 $schema->resultset('ActionLog')->search()->delete();
666 $item->location('new location')->store({ log_action => 1 });
667 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
669 # Undefined defaults to true
670 $schema->resultset('ActionLog')->search()->delete();
671 $item->location($location)->store();
672 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
674 $schema->storage->txn_rollback;
677 subtest 'Check stockrotationitem relationship' => sub {
680 $schema->storage->txn_begin();
682 my $builder = t::lib::TestBuilder->new;
683 my $item = $builder->build_sample_item;
686 source => 'Stockrotationitem',
687 value => { itemnumber_id => $item->itemnumber }
690 my $sritem = Koha::Items->find($item->itemnumber)->stockrotationitem;
691 isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
693 $schema->storage->txn_rollback;
696 subtest 'Check add_to_rota method' => sub {
699 $schema->storage->txn_begin();
701 my $builder = t::lib::TestBuilder->new;
702 my $item = $builder->build_sample_item;
703 my $rota = $builder->build({ source => 'Stockrotationrota' });
704 my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
707 source => 'Stockrotationstage',
708 value => { rota_id => $rota->{rota_id} },
711 my $sritem = Koha::Items->find($item->itemnumber);
712 $sritem->add_to_rota($rota->{rota_id});
715 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
716 $srrota->stockrotationstages->next->stage_id,
717 "Adding to a rota a new sritem item being assigned to its first stage."
720 my $newrota = $builder->build({ source => 'Stockrotationrota' });
722 my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
725 source => 'Stockrotationstage',
726 value => { rota_id => $newrota->{rota_id} },
729 $sritem->add_to_rota($newrota->{rota_id});
732 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
733 $srnewrota->stockrotationstages->next->stage_id,
734 "Moving an item results in that sritem being assigned to the new first stage."
737 $schema->storage->txn_rollback;
740 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
742 $schema->storage->txn_begin;
744 my $builder = t::lib::TestBuilder->new;
745 my $item = $builder->build_sample_item({ ccode => 'A|B' });
747 Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
748 Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
749 my $mapping = Koha::MarcSubfieldStructure->new(
751 frameworkcode => q{},
754 kohafield => 'items.ccode',
758 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
761 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
762 my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
763 is( @subs, 2, 'Expect two subfields' );
764 is( $subs[0], 'A', 'First subfield matches' );
765 is( $subs[1], 'B', 'Second subfield matches' );
767 $schema->storage->txn_rollback;
770 subtest 'ModItemFromMarc' => sub {
772 $schema->storage->txn_begin;
774 my $builder = t::lib::TestBuilder->new;
775 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
776 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
777 my $biblio = $builder->build_sample_biblio;
778 my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
779 my $item_record = MARC::Record->new;
780 $item_record->append_fields(
783 'y' => $itemtype->itemtype,
790 my (undef, undef, $itemnumber) = AddItemFromMarc($item_record,
791 $biblio->biblionumber);
793 my $item = Koha::Items->find($itemnumber);
794 is( $item->itemlost, 1, 'itemlost picked from the item marc');
796 $item->new_status("this is something")->store;
798 my $updated_item_record = MARC::Record->new;
799 $updated_item_record->append_fields(
802 'y' => $itemtype->itemtype,
806 my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
807 is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
808 is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
809 is( Koha::Items->find($itemnumber)->new_status, "this is something" );
811 subtest 'cn_sort' => sub {
814 my $item = $builder->build_sample_item;
815 $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
816 is( $item->cn_sort, 'XXX', 'init values set are expected' );
818 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
819 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
820 is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
822 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
823 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
824 is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
827 subtest 'onloan' => sub {
830 my $item = $builder->build_sample_item;
831 $item->set({ onloan => '2022-03-19' })->store;
832 is( $item->onloan, '2022-03-19', 'init values set are expected' );
834 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
835 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
836 $marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
837 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
838 is( $item->get_from_storage->onloan, '2022-03-19', 'onloan has not been updated if not passed' );
840 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, onloan => '2022-03-26' }, $item->biblionumber );
841 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
842 is( $item->get_from_storage->onloan, '2022-03-26', 'onloan has been updated when passed in' );
845 subtest 'permanent_location' => sub {
848 # Make sure items.permanent_location is not mapped
849 Koha::MarcSubfieldStructures->search(
851 frameworkcode => q{},
852 kohafield => 'items.permanent_location',
855 Koha::MarcSubfieldStructures->search(
857 frameworkcode => q{},
862 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
864 my $item = $builder->build_sample_item;
866 # By default, setting location to something new should set permanent location to the same thing
867 # with the usual exceptions
868 $item->set({ location => 'A', permanent_location => 'A' })->store;
869 is( $item->location, 'A', 'initial location set as expected' );
870 is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
872 $item->location('B');
873 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
874 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
876 $item = $item->get_from_storage;
877 is( $item->location, 'B', 'new location set as expected' );
878 is( $item->permanent_location, 'B', 'new permanent location set as expected' );
880 # Added a marc mapping for permanent location, allows it to be edited independently
881 my $mapping = Koha::MarcSubfieldStructure->new(
883 frameworkcode => q{},
886 kohafield => 'items.permanent_location',
892 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
894 # Now if we change location, and also pass in a permanent location
895 # the permanent_location will not be overwritten by location
896 $item->location('C');
897 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
898 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
899 $item = $item->get_from_storage;
900 is( $item->location, 'C', 'next new location set as expected' );
901 is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
903 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
904 # Clear values from the DB
905 $item = $item->get_from_storage;
907 # Update the location
908 $item->location('D');
909 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
910 # Remove the permanent_location field from the form
911 $marc->field('952')->delete_subfield("C");
912 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
913 $item = $item->get_from_storage;
914 is( $item->location, 'D', 'next new location set as expected' );
915 is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
917 # Clear values from the DB
918 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
919 $item = $item->get_from_storage;
921 # This time nothing is set, but we pass an empty string
922 $item->permanent_location("");
923 $item->location('E');
924 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
925 $marc->field('952')->add_subfields( "C", "" );
926 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
927 $item = $item->get_from_storage;
928 is( $item->location, 'E', 'next new location set as expected' );
929 is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
932 $schema->storage->txn_rollback;
933 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
936 subtest 'ModDateLastSeen' => sub {
938 $schema->storage->txn_begin;
940 my $builder = t::lib::TestBuilder->new;
941 my $item = $builder->build_sample_item;
942 t::lib::Mocks::mock_preference('CataloguingLog', '1');
943 my $logs_before = Koha::ActionLogs->search({ module => 'CATALOGUING', action => 'MODIFY', object => $item->itemnumber })->count;
944 ModDateLastSeen($item->itemnumber);
945 my $logs_after = Koha::ActionLogs->search({ module => 'CATALOGUING', action => 'MODIFY', object => $item->itemnumber })->count;
946 is( $logs_after, $logs_before, "ModDateLastSeen doesn't log if item not lost");
947 $item->itemlost(1)->store({ log_action => 0 });
948 ModDateLastSeen($item->itemnumber, 1);
949 $item->discard_changes;
950 $logs_after = Koha::ActionLogs->search({ module => 'CATALOGUING', action => 'MODIFY', object => $item->itemnumber })->count;
951 is( $item->itemlost, 1, "Item left lost when parameter is passed");
952 is( $logs_after, $logs_before, "ModDateLastSeen doesn't log if item not lost");
953 ModDateLastSeen($item->itemnumber);
954 $item->discard_changes;
955 $logs_after = Koha::ActionLogs->search({ module => 'CATALOGUING', action => 'MODIFY', object => $item->itemnumber })->count;
956 is( $item->itemlost, 0, "Item no longer lost when no parameter is passed");
957 is( $logs_after, $logs_before + 1, "ModDateLastSeen logs if item was lost and now found");