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 GetItemsInfo SearchItems AddItemFromMarc ModItemFromMarc get_hostitemnumbers_of Item2Marc );
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 'GetItemsInfo tests' => sub {
197 $schema->storage->txn_begin;
199 my $builder = t::lib::TestBuilder->new;
200 my $library1 = $builder->build({
203 my $library2 = $builder->build({
206 my $itemtype = $builder->build({
207 source => 'Itemtype',
210 Koha::AuthorisedValues->delete;
211 my $av1 = Koha::AuthorisedValue->new(
213 category => 'RESTRICTED',
214 authorised_value => '1',
215 lib => 'Restricted Access',
216 lib_opac => 'Restricted Access OPAC',
221 my $biblio = $builder->build_sample_biblio();
223 my $itemnumber = $builder->build_sample_item(
225 biblionumber => $biblio->biblionumber,
226 homebranch => $library1->{branchcode},
227 holdingbranch => $library2->{branchcode},
228 itype => $itemtype->{itemtype},
233 my $library = Koha::Libraries->find( $library1->{branchcode} );
234 $library->opac_info("homebranch OPAC info");
237 $library = Koha::Libraries->find( $library2->{branchcode} );
238 $library->opac_info("holdingbranch OPAC info");
241 my @results = GetItemsInfo( $biblio->biblionumber );
242 ok( @results, 'GetItemsInfo returns results');
244 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
245 'GetItemsInfo returns the correct home branch OPAC info notice' );
246 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
247 'GetItemsInfo returns the correct holding branch OPAC info notice' );
248 is( exists( $results[0]->{ onsite_checkout } ), 1,
249 'GetItemsInfo returns a onsite_checkout key' );
250 is( $results[0]->{ restricted }, 1,
251 'GetItemsInfo returns a restricted value code' );
252 is( $results[0]->{ restrictedvalue }, "Restricted Access",
253 'GetItemsInfo returns a restricted value description (staff)' );
254 is( $results[0]->{ restrictedvalueopac }, "Restricted Access OPAC",
255 'GetItemsInfo returns a restricted value description (OPAC)' );
257 #place item into holds queue
258 my $dbh = C4::Context->dbh;
259 @results = GetItemsInfo( $biblio->biblionumber );
260 is( $results[0]->{ has_pending_hold }, "0",
261 'Hold not marked as pending/unavailable if nothing in tmp_holdsqueue for item' );
263 $dbh->do(q{INSERT INTO tmp_holdsqueue (biblionumber, itemnumber, surname, borrowernumber ) VALUES (?, ?, "Zorro", 42)}, undef, $biblio->biblionumber, $itemnumber);
264 @results = GetItemsInfo( $biblio->biblionumber );
265 is( $results[0]->{ has_pending_hold }, "1",
266 'Hold marked as pending/unavailable if tmp_holdsqueue is not empty for item' );
268 $schema->storage->txn_rollback;
271 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
275 $schema->storage->txn_begin;
277 my $biblio = $schema->resultset('Biblio')->create({
278 title => "Test title",
279 datecreated => dt_from_string,
280 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
282 my $biblioitem = $biblio->biblioitems->first;
283 my $item = $schema->resultset('Item')->create({
284 biblioitemnumber => $biblioitem->biblioitemnumber,
285 biblionumber => $biblio->biblionumber,
286 itype => "ITEM_LEVEL",
289 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
290 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
292 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
293 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
295 # If itemtype is not defined and item-level_level item types are set
296 # fallback to biblio-level itemtype (Bug 14651) and warn
297 $item->itype( undef );
299 my $effective_itemtype;
300 warning_is { $effective_itemtype = $item->effective_itemtype() }
301 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
302 '->effective_itemtype() raises a warning when falling back to bib-level';
304 ok( defined $effective_itemtype &&
305 $effective_itemtype eq 'BIB_LEVEL',
306 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
308 $schema->storage->txn_rollback;
311 subtest 'SearchItems test' => sub {
314 $schema->storage->txn_begin;
315 my $dbh = C4::Context->dbh;
316 my $builder = t::lib::TestBuilder->new;
318 my $library1 = $builder->build({
321 my $library2 = $builder->build({
324 my $itemtype = $builder->build({
325 source => 'Itemtype',
328 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
329 my ($cpl_items_before) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
331 my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
332 $builder->build_sample_biblio({ title => 'Silence in the shadow' });
334 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
337 my $item1 = $builder->build_sample_item(
339 biblionumber => $biblio->biblionumber,
340 library => $library1->{branchcode},
341 itype => $itemtype->{itemtype}
344 my $item1_itemnumber = $item1->itemnumber;
345 my $item2_itemnumber = $builder->build_sample_item(
347 biblionumber => $biblio->biblionumber,
348 library => $library2->{branchcode},
349 itype => $itemtype->{itemtype}
353 my ($items, $total_results);
355 ($items, $total_results) = SearchItems();
357 is($total_results, $initial_items_count + 2, "Created 2 new items");
358 is(scalar @$items, $total_results, "SearchItems() returns all items");
360 ($items, $total_results) = SearchItems(undef, {rows => 1});
361 is($total_results, $initial_items_count + 2);
362 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
364 # Search all items where homebranch = 'CPL'
366 field => 'homebranch',
367 query => $library1->{branchcode},
370 ($items, $total_results) = SearchItems($filter);
371 ok($total_results > 0, "There is at least one CPL item");
372 my $all_items_are_CPL = 1;
373 foreach my $item (@$items) {
374 if ($item->{homebranch} ne $library1->{branchcode}) {
375 $all_items_are_CPL = 0;
379 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
381 # Search all items where homebranch != 'CPL'
383 field => 'homebranch',
384 query => $library1->{branchcode},
387 ($items, $total_results) = SearchItems($filter);
388 ok($total_results > 0, "There is at least one non-CPL item");
389 my $all_items_are_not_CPL = 1;
390 foreach my $item (@$items) {
391 if ($item->{homebranch} eq $library1->{branchcode}) {
392 $all_items_are_not_CPL = 0;
396 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
398 # Search all items where biblio title (245$a) is like 'Silence in the %'
400 field => 'marc:245$a',
401 query => 'Silence in the %',
404 ($items, $total_results) = SearchItems($filter);
405 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
407 # Search all items where biblio title is 'Silence in the library'
408 # and homebranch is 'CPL'
410 conjunction => 'AND',
413 field => 'marc:245$a',
414 query => 'Silence in the %',
418 field => 'homebranch',
419 query => $library1->{branchcode},
424 ($items, $total_results) = SearchItems($filter);
426 foreach my $item (@$items) {
427 if ($item->{itemnumber} == $item1_itemnumber) {
432 ok($found, "item1 found");
434 my $frameworkcode = q||;
435 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
437 # Create item subfield 'z' without link
438 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
439 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
442 my $cache = Koha::Caches->get_instance();
443 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
444 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
445 $cache->clear_from_cache("default_value_for_mod_marc-");
446 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
448 my $item3_record = MARC::Record->new;
449 $item3_record->append_fields(
453 'y' => $itemtype->{itemtype}
456 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
457 $biblio->biblionumber);
459 # Search item where item subfield z is "foobar"
461 field => 'marc:' . $itemfield . '$z',
465 ($items, $total_results) = SearchItems($filter);
466 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
468 # Link $z to items.itemnotes (and make sure there is no other subfields
470 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
471 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
474 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
475 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
476 $cache->clear_from_cache("default_value_for_mod_marc-");
477 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
479 ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
481 # Make sure the link is used
482 my $item3 = Koha::Items->find($item3_itemnumber);
483 is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"');
485 # Do the same search again.
486 # This time it will search in items.itemnotes
487 ($items, $total_results) = SearchItems($filter);
488 is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"');
490 my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
491 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' );
495 conjunction => 'AND',
503 field => 'homebranch',
504 query => $library1->{branchcode},
509 ($items, $total_results) = SearchItems($filter);
510 is($total_results, 1, "Search items.issues issues = 0 returns result (items.issues defaults to 0)");
514 conjunction => 'AND',
517 field => 'new_status',
522 field => 'homebranch',
523 query => $library1->{branchcode},
528 ($items, $total_results) = SearchItems($filter);
529 is($total_results, 0, 'found no item with new_status=0 without ifnull');
531 $filter->{filters}[0]->{ifnull} = 0;
532 ($items, $total_results) = SearchItems($filter);
533 is($total_results, 1, 'found all items of library1 with new_status=0 with ifnull = 0');
535 t::lib::Mocks::mock_userenv({ branchcode => $item1->homebranch });
536 my $patron_borrower = $builder->build_object({ class => 'Koha::Patrons' })->unblessed;
537 AddIssue( $patron_borrower, $item1->barcode );
538 # Search item where item is checked out
540 conjunction => 'AND',
548 field => 'homebranch',
549 query => $item1->homebranch,
554 ($items, $total_results) = SearchItems($filter);
555 ok(scalar @$items == 1, 'found 1 checked out item');
557 # When sorting by descending availability, checked out item should show first
559 sortby => 'availability',
563 field => 'homebranch',
564 query => $item1->homebranch,
567 ($items, $total_results) = SearchItems($filter,$params);
568 is($items->[0]->{barcode}, $item1->barcode, 'Items sorted as expected by availability');
570 $schema->storage->txn_rollback;
573 subtest 'Koha::Item(s) tests' => sub {
577 $schema->storage->txn_begin();
579 my $builder = t::lib::TestBuilder->new;
580 my $library1 = $builder->build({
583 my $library2 = $builder->build({
586 my $itemtype = $builder->build({
587 source => 'Itemtype',
590 # Create a biblio and item for testing
591 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
592 my $biblio = $builder->build_sample_biblio();
593 my $itemnumber = $builder->build_sample_item(
595 biblionumber => $biblio->biblionumber,
596 homebranch => $library1->{branchcode},
597 holdingbranch => $library2->{branchcode},
598 itype => $itemtype->{itemtype}
603 my $item = Koha::Items->find( $itemnumber );
604 is( ref($item), 'Koha::Item', "Got Koha::Item" );
606 my $homebranch = $item->home_branch();
607 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
608 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
610 my $holdingbranch = $item->holding_branch();
611 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
612 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
614 $biblio = $item->biblio();
615 is( ref($item->biblio), 'Koha::Biblio', "Got Koha::Biblio from biblio method" );
616 is( $item->biblio->title(), $biblio->title, 'Title matches biblio title' );
618 $schema->storage->txn_rollback;
621 subtest 'get_hostitemnumbers_of' => sub {
624 $schema->storage->txn_begin;
625 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
626 my $builder = t::lib::TestBuilder->new;
628 # Host item field without 0 or 9
629 my $bib1 = MARC::Record->new();
630 $bib1->append_fields(
631 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
632 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
633 MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
635 my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
636 my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
637 is( scalar @itemnumbers1, 0, '773 without 0 or 9');
639 # Correct host item field, analytical records on
640 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
641 my $hostitem = $builder->build_sample_item();
642 my $bib2 = MARC::Record->new();
643 $bib2->append_fields(
644 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
645 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
646 MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
648 my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
649 my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
650 is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
652 # Correct host item field, analytical records off
653 t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
654 @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
655 is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
657 $schema->storage->txn_rollback;
660 subtest 'Test logging for ModItem' => sub {
664 t::lib::Mocks::mock_preference('CataloguingLog', 1);
666 $schema->storage->txn_begin;
668 my $builder = t::lib::TestBuilder->new;
669 my $library = $builder->build({
672 my $itemtype = $builder->build({
673 source => 'Itemtype',
676 # Create a biblio instance for testing
677 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
678 my $biblio = $builder->build_sample_biblio();
681 my $item = $builder->build_sample_item(
683 biblionumber => $biblio->biblionumber,
684 library => $library->{homebranch},
685 location => $location,
686 itype => $itemtype->{itemtype}
690 # False means no logging
691 $schema->resultset('ActionLog')->search()->delete();
692 $item->location($location)->store({ log_action => 0 });
693 is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
696 $schema->resultset('ActionLog')->search()->delete();
697 $item->location('new location')->store({ log_action => 1 });
698 is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
700 # Undefined defaults to true
701 $schema->resultset('ActionLog')->search()->delete();
702 $item->location($location)->store();
703 is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
705 $schema->storage->txn_rollback;
708 subtest 'Check stockrotationitem relationship' => sub {
711 $schema->storage->txn_begin();
713 my $builder = t::lib::TestBuilder->new;
714 my $item = $builder->build_sample_item;
717 source => 'Stockrotationitem',
718 value => { itemnumber_id => $item->itemnumber }
721 my $sritem = Koha::Items->find($item->itemnumber)->stockrotationitem;
722 isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
724 $schema->storage->txn_rollback;
727 subtest 'Check add_to_rota method' => sub {
730 $schema->storage->txn_begin();
732 my $builder = t::lib::TestBuilder->new;
733 my $item = $builder->build_sample_item;
734 my $rota = $builder->build({ source => 'Stockrotationrota' });
735 my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
738 source => 'Stockrotationstage',
739 value => { rota_id => $rota->{rota_id} },
742 my $sritem = Koha::Items->find($item->itemnumber);
743 $sritem->add_to_rota($rota->{rota_id});
746 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
747 $srrota->stockrotationstages->next->stage_id,
748 "Adding to a rota a new sritem item being assigned to its first stage."
751 my $newrota = $builder->build({ source => 'Stockrotationrota' });
753 my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
756 source => 'Stockrotationstage',
757 value => { rota_id => $newrota->{rota_id} },
760 $sritem->add_to_rota($newrota->{rota_id});
763 Koha::StockRotationItems->find($item->itemnumber)->stage_id,
764 $srnewrota->stockrotationstages->next->stage_id,
765 "Moving an item results in that sritem being assigned to the new first stage."
768 $schema->storage->txn_rollback;
771 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
773 $schema->storage->txn_begin;
775 my $builder = t::lib::TestBuilder->new;
776 my $item = $builder->build_sample_item({ ccode => 'A|B' });
778 Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
779 Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
780 my $mapping = Koha::MarcSubfieldStructure->new(
782 frameworkcode => q{},
785 kohafield => 'items.ccode',
789 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
792 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
793 my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
794 is( @subs, 2, 'Expect two subfields' );
795 is( $subs[0], 'A', 'First subfield matches' );
796 is( $subs[1], 'B', 'Second subfield matches' );
798 $schema->storage->txn_rollback;
801 subtest 'ModItemFromMarc' => sub {
803 $schema->storage->txn_begin;
805 my $builder = t::lib::TestBuilder->new;
806 my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
807 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
808 my $biblio = $builder->build_sample_biblio;
809 my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
810 my $item_record = MARC::Record->new;
811 $item_record->append_fields(
814 'y' => $itemtype->itemtype,
821 my (undef, undef, $itemnumber) = AddItemFromMarc($item_record,
822 $biblio->biblionumber);
824 my $item = Koha::Items->find($itemnumber);
825 is( $item->itemlost, 1, 'itemlost picked from the item marc');
827 $item->new_status("this is something")->store;
829 my $updated_item_record = MARC::Record->new;
830 $updated_item_record->append_fields(
833 'y' => $itemtype->itemtype,
837 my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
838 is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
839 is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
840 is( Koha::Items->find($itemnumber)->new_status, "this is something" );
842 subtest 'cn_sort' => sub {
845 my $item = $builder->build_sample_item;
846 $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
847 is( $item->cn_sort, 'XXX', 'init values set are expected' );
849 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
850 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
851 is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
853 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
854 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
855 is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
858 subtest 'onloan' => sub {
861 my $item = $builder->build_sample_item;
862 $item->set({ onloan => '2022-03-19' })->store;
863 is( $item->onloan, '2022-03-19', 'init values set are expected' );
865 my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
866 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
867 $marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
868 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
869 is( $item->get_from_storage->onloan, '2022-03-19', 'onloan has not been updated if not passed' );
871 $marc = C4::Items::Item2Marc( { %{$item->unblessed}, onloan => '2022-03-26' }, $item->biblionumber );
872 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
873 is( $item->get_from_storage->onloan, '2022-03-26', 'onloan has been updated when passed in' );
876 subtest 'permanent_location' => sub {
879 # Make sure items.permanent_location is not mapped
880 Koha::MarcSubfieldStructures->search(
882 frameworkcode => q{},
883 kohafield => 'items.permanent_location',
886 Koha::MarcSubfieldStructures->search(
888 frameworkcode => q{},
893 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
895 my $item = $builder->build_sample_item;
897 # By default, setting location to something new should set permanent location to the same thing
898 # with the usual exceptions
899 $item->set({ location => 'A', permanent_location => 'A' })->store;
900 is( $item->location, 'A', 'initial location set as expected' );
901 is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
903 $item->location('B');
904 my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
905 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
907 $item = $item->get_from_storage;
908 is( $item->location, 'B', 'new location set as expected' );
909 is( $item->permanent_location, 'B', 'new permanent location set as expected' );
911 # Added a marc mapping for permanent location, allows it to be edited independently
912 my $mapping = Koha::MarcSubfieldStructure->new(
914 frameworkcode => q{},
917 kohafield => 'items.permanent_location',
923 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
925 # Now if we change location, and also pass in a permanent location
926 # the permanent_location will not be overwritten by location
927 $item->location('C');
928 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
929 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
930 $item = $item->get_from_storage;
931 is( $item->location, 'C', 'next new location set as expected' );
932 is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
934 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
935 # Clear values from the DB
936 $item = $item->get_from_storage;
938 # Update the location
939 $item->location('D');
940 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
941 # Remove the permanent_location field from the form
942 $marc->field('952')->delete_subfield("C");
943 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
944 $item = $item->get_from_storage;
945 is( $item->location, 'D', 'next new location set as expected' );
946 is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
948 # Clear values from the DB
949 $item->permanent_location(undef)->more_subfields_xml(undef)->store;
950 $item = $item->get_from_storage;
952 # This time nothing is set, but we pass an empty string
953 $item->permanent_location("");
954 $item->location('E');
955 $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
956 $marc->field('952')->add_subfields( "C", "" );
957 ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
958 $item = $item->get_from_storage;
959 is( $item->location, 'E', 'next new location set as expected' );
960 is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
963 $schema->storage->txn_rollback;
964 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );