Bug 32456: Unit tests
[koha.git] / t / db_dependent / Items.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use Data::Dumper;
20
21 use MARC::Record;
22 use C4::Items qw( ModItemTransfer GetHiddenItemnumbers GetItemsInfo SearchItems AddItemFromMarc ModItemFromMarc get_hostitemnumbers_of Item2Marc );
23 use C4::Biblio qw( GetMarcFromKohaField EmbedItemsInMarcBiblio GetMarcBiblio AddBiblio );
24 use C4::Circulation qw( AddIssue );
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;
32 use Koha::AuthorisedValues;
33
34 use t::lib::Mocks;
35 use t::lib::TestBuilder;
36
37 use Test::More tests => 14;
38
39 use Test::Warn;
40
41 my $schema = Koha::Database->new->schema;
42 my $location = 'My Location';
43
44 subtest 'General Add, Get and Del tests' => sub {
45
46     plan tests => 16;
47
48     $schema->storage->txn_begin;
49
50     my $builder = t::lib::TestBuilder->new;
51     my $library = $builder->build({
52         source => 'Branch',
53     });
54     my $itemtype = $builder->build({
55         source => 'Itemtype',
56     });
57
58     # Create a biblio instance for testing
59     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
60     my $biblio = $builder->build_sample_biblio();
61
62     # Add an item.
63     my $item = $builder->build_sample_item(
64         {
65             biblionumber => $biblio->biblionumber,
66             library      => $library->{branchcode},
67             location     => $location,
68             itype        => $itemtype->{itemtype}
69         }
70     );
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.");
74
75     # Get item.
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" );
81
82
83     # Do not modify anything, and do not explode!
84     $getitem->set({})->store;
85
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 . '.');
90
91     # Delete item.
92     $moditem->delete;
93     my $getdeleted = Koha::Items->find($itemnumber);
94     is($getdeleted, undef, "Item deleted as expected.");
95
96     $itemnumber = $builder->build_sample_item(
97         {
98             biblionumber => $biblio->biblionumber,
99             library      => $library->{branchcode},
100             location     => $location,
101             permanent_location => 'my permanent location',
102             itype        => $itemtype->{itemtype}
103         }
104     )->itemnumber;
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" );
108
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" );
114
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" );
119
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" );
126
127     $schema->storage->txn_rollback;
128 };
129
130 subtest 'ModItemTransfer tests' => sub {
131     plan tests => 8;
132
133     $schema->storage->txn_begin;
134
135     my $builder = t::lib::TestBuilder->new;
136     my $item    = $builder->build_sample_item();
137
138     my $library1 = $builder->build(
139         {
140             source => 'Branch',
141         }
142     );
143     my $library2 = $builder->build(
144         {
145             source => 'Branch',
146         }
147     );
148
149     ModItemTransfer( $item->itemnumber, $library1->{branchcode},
150         $library2->{branchcode}, 'Manual' );
151
152     my $transfers = Koha::Item::Transfers->search(
153         {
154             itemnumber => $item->itemnumber
155         }
156     );
157
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");
161
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' } }
167     );
168
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");
177
178     # Check 'reason' is populated when passed
179     ModItemTransfer( $item->itemnumber, $library2->{branchcode},
180         $library1->{branchcode}, "Manual" );
181
182     $transfers = Koha::Item::Transfers->search(
183         { itemnumber => $item->itemnumber, },
184         { order_by   => { '-desc' => 'branchtransfer_id' } }
185     );
186
187     my $transfer3 = $transfers->next;
188     is($transfer3->reason, 'Manual', "Reason set via ModItemTransfer");
189
190     $schema->storage->txn_rollback;
191 };
192
193 subtest 'GetHiddenItemnumbers tests' => sub {
194
195     plan tests => 11;
196
197     # This sub is controlled by the OpacHiddenItems system preference.
198
199     $schema->storage->txn_begin;
200
201     my $builder = t::lib::TestBuilder->new;
202     my $library1 = $builder->build({
203         source => 'Branch',
204     });
205
206     my $library2 = $builder->build({
207         source => 'Branch',
208     });
209     my $itemtype = $builder->build({
210         source => 'Itemtype',
211     });
212
213     # Create a new biblio
214     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
215     my $biblio = $builder->build_sample_biblio();
216
217     # Add two items
218     my $item1_itemnumber = $builder->build_sample_item(
219         {
220             biblionumber => $biblio->biblionumber,
221             library      => $library1->{branchcode},
222             withdrawn    => 1,
223             itype        => $itemtype->{itemtype}
224         }
225     )->itemnumber;
226     my $item2_itemnumber = $builder->build_sample_item(
227         {
228             biblionumber => $biblio->biblionumber,
229             library      => $library2->{branchcode},
230             withdrawn    => 0,
231             itype        => $itemtype->{itemtype}
232         }
233     )->itemnumber;
234     my $opachiddenitems;
235     my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
236     my @hidden;
237     my @items;
238     push @items, Koha::Items->find( $item1_itemnumber )->unblessed;
239     push @items, Koha::Items->find( $item2_itemnumber )->unblessed;
240
241     # Empty OpacHiddenItems
242     t::lib::Mocks::mock_preference('OpacHiddenItems','');
243     ok( !defined( GetHiddenItemnumbers( { items => \@items } ) ),
244         "Hidden items list undef if OpacHiddenItems empty");
245
246     # Blank spaces
247     t::lib::Mocks::mock_preference('OpacHiddenItems','  ');
248     ok( scalar GetHiddenItemnumbers( { items => \@items } ) == 0,
249         "Hidden items list empty if OpacHiddenItems only contains blanks");
250
251     # One variable / value
252     $opachiddenitems = "
253         withdrawn: [1]";
254     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
255     @hidden = GetHiddenItemnumbers( { items => \@items } );
256     ok( scalar @hidden == 1, "Only one hidden item");
257     is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
258
259     # One variable, two values
260     $opachiddenitems = "
261         withdrawn: [1,0]";
262     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
263     @hidden = GetHiddenItemnumbers( { items => \@items } );
264     ok( scalar @hidden == 2, "Two items hidden");
265     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
266
267     # Two variables, a value each
268     $opachiddenitems = "
269         withdrawn: [1]
270         homebranch: [$library2->{branchcode}]
271     ";
272     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
273     @hidden = GetHiddenItemnumbers( { items => \@items } );
274     ok( scalar @hidden == 2, "Two items hidden");
275     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
276
277     # Override hidden with patron category
278     t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'S' );
279     @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'PT' } );
280     ok( scalar @hidden == 2, "Two items still hidden");
281     @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'S' } );
282     ok( scalar @hidden == 0, "Two items not hidden");
283
284     # Valid OpacHiddenItems, empty list
285     @items = ();
286     @hidden = GetHiddenItemnumbers( { items => \@items } );
287     ok( scalar @hidden == 0, "Empty items list, no item hidden");
288
289     $schema->storage->txn_rollback;
290 };
291
292 subtest 'GetItemsInfo tests' => sub {
293
294     plan tests => 9;
295
296     $schema->storage->txn_begin;
297
298     my $builder = t::lib::TestBuilder->new;
299     my $library1 = $builder->build({
300         source => 'Branch',
301     });
302     my $library2 = $builder->build({
303         source => 'Branch',
304     });
305     my $itemtype = $builder->build({
306         source => 'Itemtype',
307     });
308
309     Koha::AuthorisedValues->delete;
310     my $av1 = Koha::AuthorisedValue->new(
311         {
312             category         => 'RESTRICTED',
313             authorised_value => '1',
314             lib              => 'Restricted Access',
315             lib_opac         => 'Restricted Access OPAC',
316         }
317     )->store();
318
319     # Add a biblio
320     my $biblio = $builder->build_sample_biblio();
321     # Add an item
322     my $itemnumber = $builder->build_sample_item(
323         {
324             biblionumber  => $biblio->biblionumber,
325             homebranch    => $library1->{branchcode},
326             holdingbranch => $library2->{branchcode},
327             itype         => $itemtype->{itemtype},
328             restricted    => 1,
329         }
330     )->itemnumber;
331
332     my $library = Koha::Libraries->find( $library1->{branchcode} );
333     $library->opac_info("homebranch OPAC info");
334     $library->store;
335
336     $library = Koha::Libraries->find( $library2->{branchcode} );
337     $library->opac_info("holdingbranch OPAC info");
338     $library->store;
339
340     my @results = GetItemsInfo( $biblio->biblionumber );
341     ok( @results, 'GetItemsInfo returns results');
342
343     is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
344         'GetItemsInfo returns the correct home branch OPAC info notice' );
345     is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
346         'GetItemsInfo returns the correct holding branch OPAC info notice' );
347     is( exists( $results[0]->{ onsite_checkout } ), 1,
348         'GetItemsInfo returns a onsite_checkout key' );
349     is( $results[0]->{ restricted }, 1,
350         'GetItemsInfo returns a restricted value code' );
351     is( $results[0]->{ restrictedvalue }, "Restricted Access",
352         'GetItemsInfo returns a restricted value description (staff)' );
353     is( $results[0]->{ restrictedvalueopac }, "Restricted Access OPAC",
354         'GetItemsInfo returns a restricted value description (OPAC)' );
355
356     #place item into holds queue
357     my $dbh = C4::Context->dbh;
358     @results = GetItemsInfo( $biblio->biblionumber );
359     is( $results[0]->{ has_pending_hold }, "0",
360         'Hold not marked as pending/unavailable if nothing in tmp_holdsqueue for item' );
361
362     $dbh->do(q{INSERT INTO tmp_holdsqueue (biblionumber, itemnumber, surname, borrowernumber ) VALUES (?, ?, "Zorro", 42)}, undef, $biblio->biblionumber, $itemnumber);
363     @results = GetItemsInfo( $biblio->biblionumber );
364     is( $results[0]->{ has_pending_hold }, "1",
365         'Hold marked as pending/unavailable if tmp_holdsqueue is not empty for item' );
366
367     $schema->storage->txn_rollback;
368 };
369
370 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
371
372     plan tests => 4;
373
374     $schema->storage->txn_begin;
375
376     my $biblio = $schema->resultset('Biblio')->create({
377         title       => "Test title",
378         datecreated => dt_from_string,
379         biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
380     });
381     my $biblioitem = $biblio->biblioitems->first;
382     my $item = $schema->resultset('Item')->create({
383         biblioitemnumber => $biblioitem->biblioitemnumber,
384         biblionumber     => $biblio->biblionumber,
385         itype            => "ITEM_LEVEL",
386     });
387
388     t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
389     is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
390
391     t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
392     is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
393
394     # If itemtype is not defined and item-level_level item types are set
395     # fallback to biblio-level itemtype (Bug 14651) and warn
396     $item->itype( undef );
397     $item->update();
398     my $effective_itemtype;
399     warning_is { $effective_itemtype = $item->effective_itemtype() }
400                 "item-level_itypes set but no itemtype set for item (".$item->itemnumber.")",
401                 '->effective_itemtype() raises a warning when falling back to bib-level';
402
403     ok( defined $effective_itemtype &&
404                 $effective_itemtype eq 'BIB_LEVEL',
405         '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
406
407     $schema->storage->txn_rollback;
408 };
409
410 subtest 'SearchItems test' => sub {
411     plan tests => 19;
412
413     $schema->storage->txn_begin;
414     my $dbh = C4::Context->dbh;
415     my $builder = t::lib::TestBuilder->new;
416
417     my $library1 = $builder->build({
418         source => 'Branch',
419     });
420     my $library2 = $builder->build({
421         source => 'Branch',
422     });
423     my $itemtype = $builder->build({
424         source => 'Itemtype',
425     });
426
427     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
428     my ($cpl_items_before) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
429
430     my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
431     $builder->build_sample_biblio({ title => 'Silence in the shadow' });
432
433     my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
434
435     # Add two items
436     my $item1 = $builder->build_sample_item(
437         {
438             biblionumber => $biblio->biblionumber,
439             library      => $library1->{branchcode},
440             itype        => $itemtype->{itemtype}
441         }
442     );
443     my $item1_itemnumber = $item1->itemnumber;
444     my $item2_itemnumber = $builder->build_sample_item(
445         {
446             biblionumber => $biblio->biblionumber,
447             library      => $library2->{branchcode},
448             itype        => $itemtype->{itemtype}
449         }
450     )->itemnumber;
451
452     my ($items, $total_results);
453
454     ($items, $total_results) = SearchItems();
455
456     is($total_results, $initial_items_count + 2, "Created 2 new items");
457     is(scalar @$items, $total_results, "SearchItems() returns all items");
458
459     ($items, $total_results) = SearchItems(undef, {rows => 1});
460     is($total_results, $initial_items_count + 2);
461     is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
462
463     # Search all items where homebranch = 'CPL'
464     my $filter = {
465         field => 'homebranch',
466         query => $library1->{branchcode},
467         operator => '=',
468     };
469     ($items, $total_results) = SearchItems($filter);
470     ok($total_results > 0, "There is at least one CPL item");
471     my $all_items_are_CPL = 1;
472     foreach my $item (@$items) {
473         if ($item->{homebranch} ne $library1->{branchcode}) {
474             $all_items_are_CPL = 0;
475             last;
476         }
477     }
478     ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
479
480     # Search all items where homebranch != 'CPL'
481     $filter = {
482         field => 'homebranch',
483         query => $library1->{branchcode},
484         operator => '!=',
485     };
486     ($items, $total_results) = SearchItems($filter);
487     ok($total_results > 0, "There is at least one non-CPL item");
488     my $all_items_are_not_CPL = 1;
489     foreach my $item (@$items) {
490         if ($item->{homebranch} eq $library1->{branchcode}) {
491             $all_items_are_not_CPL = 0;
492             last;
493         }
494     }
495     ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
496
497     # Search all items where biblio title (245$a) is like 'Silence in the %'
498     $filter = {
499         field => 'marc:245$a',
500         query => 'Silence in the %',
501         operator => 'like',
502     };
503     ($items, $total_results) = SearchItems($filter);
504     ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
505
506     # Search all items where biblio title is 'Silence in the library'
507     # and homebranch is 'CPL'
508     $filter = {
509         conjunction => 'AND',
510         filters => [
511             {
512                 field => 'marc:245$a',
513                 query => 'Silence in the %',
514                 operator => 'like',
515             },
516             {
517                 field => 'homebranch',
518                 query => $library1->{branchcode},
519                 operator => '=',
520             },
521         ],
522     };
523     ($items, $total_results) = SearchItems($filter);
524     my $found = 0;
525     foreach my $item (@$items) {
526         if ($item->{itemnumber} == $item1_itemnumber) {
527             $found = 1;
528             last;
529         }
530     }
531     ok($found, "item1 found");
532
533     my $frameworkcode = q||;
534     my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
535
536     # Create item subfield 'z' without link
537     $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
538     $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
539
540     # Clear cache
541     my $cache = Koha::Caches->get_instance();
542     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
543     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
544     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
545
546     my $item3_record = MARC::Record->new;
547     $item3_record->append_fields(
548         MARC::Field->new(
549             $itemfield, '', '',
550             'z' => 'foobar',
551             'y' => $itemtype->{itemtype}
552         )
553     );
554     my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
555         $biblio->biblionumber);
556
557     # Search item where item subfield z is "foobar"
558     $filter = {
559         field => 'marc:' . $itemfield . '$z',
560         query => 'foobar',
561         operator => 'like',
562     };
563     ($items, $total_results) = SearchItems($filter);
564     ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
565
566     # Link $z to items.itemnotes (and make sure there is no other subfields
567     # linked to it)
568     $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
569     $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
570
571     # Clear cache
572     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
573     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
574     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
575
576     ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
577
578     # Make sure the link is used
579     my $item3 = Koha::Items->find($item3_itemnumber);
580     is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"');
581
582     # Do the same search again.
583     # This time it will search in items.itemnotes
584     ($items, $total_results) = SearchItems($filter);
585     is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"');
586
587     my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
588     is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' );
589
590     # Issues count = 0
591     $filter = {
592         conjunction => 'AND',
593         filters => [
594             {
595                 field => 'issues',
596                 query => 0,
597                 operator => '=',
598             },
599             {
600                 field => 'homebranch',
601                 query => $library1->{branchcode},
602                 operator => '=',
603             },
604         ],
605     };
606     ($items, $total_results) = SearchItems($filter);
607     is($total_results, 1, "Search items.issues issues = 0 returns result (items.issues defaults to 0)");
608
609     # Is null
610     $filter = {
611         conjunction => 'AND',
612         filters     => [
613             {
614                 field    => 'new_status',
615                 query    => 0,
616                 operator => '='
617             },
618             {
619                 field    => 'homebranch',
620                 query    => $library1->{branchcode},
621                 operator => '=',
622             },
623         ],
624     };
625     ($items, $total_results) = SearchItems($filter);
626     is($total_results, 0, 'found no item with new_status=0 without ifnull');
627
628     $filter->{filters}[0]->{ifnull} = 0;
629     ($items, $total_results) = SearchItems($filter);
630     is($total_results, 1, 'found all items of library1 with new_status=0 with ifnull = 0');
631
632     t::lib::Mocks::mock_userenv({ branchcode => $item1->homebranch });
633     my $patron_borrower = $builder->build_object({ class => 'Koha::Patrons' })->unblessed;
634     AddIssue( $patron_borrower, $item1->barcode );
635     # Search item where item is checked out
636     $filter = {
637         conjunction => 'AND',
638         filters => [
639             {
640                 field => 'onloan',
641                 query => 'not null',
642                 operator => 'is',
643             },
644             {
645                 field => 'homebranch',
646                 query => $item1->homebranch,
647                 operator => '=',
648             },
649         ],
650     };
651     ($items, $total_results) = SearchItems($filter);
652     ok(scalar @$items == 1, 'found 1 checked out item');
653
654     # When sorting by descending availability, checked out item should show first
655     my $params = {
656         sortby => 'availability',
657         sortorder => 'DESC',
658     };
659     $filter = {
660         field => 'homebranch',
661         query => $item1->homebranch,
662         operator => '=',
663     };
664     ($items, $total_results) = SearchItems($filter,$params);
665     is($items->[0]->{barcode}, $item1->barcode, 'Items sorted as expected by availability');
666
667     $schema->storage->txn_rollback;
668 };
669
670 subtest 'Koha::Item(s) tests' => sub {
671
672     plan tests => 7;
673
674     $schema->storage->txn_begin();
675
676     my $builder = t::lib::TestBuilder->new;
677     my $library1 = $builder->build({
678         source => 'Branch',
679     });
680     my $library2 = $builder->build({
681         source => 'Branch',
682     });
683     my $itemtype = $builder->build({
684         source => 'Itemtype',
685     });
686
687     # Create a biblio and item for testing
688     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
689     my $biblio = $builder->build_sample_biblio();
690     my $itemnumber = $builder->build_sample_item(
691         {
692             biblionumber  => $biblio->biblionumber,
693             homebranch    => $library1->{branchcode},
694             holdingbranch => $library2->{branchcode},
695             itype         => $itemtype->{itemtype}
696         }
697     )->itemnumber;
698
699     # Get item.
700     my $item = Koha::Items->find( $itemnumber );
701     is( ref($item), 'Koha::Item', "Got Koha::Item" );
702
703     my $homebranch = $item->home_branch();
704     is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
705     is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
706
707     my $holdingbranch = $item->holding_branch();
708     is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
709     is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
710
711     $biblio = $item->biblio();
712     is( ref($item->biblio), 'Koha::Biblio', "Got Koha::Biblio from biblio method" );
713     is( $item->biblio->title(), $biblio->title, 'Title matches biblio title' );
714
715     $schema->storage->txn_rollback;
716 };
717
718 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
719     plan tests => 8;
720
721     $schema->storage->txn_begin();
722
723     my $builder = t::lib::TestBuilder->new;
724     my $library1 = $builder->build({
725         source => 'Branch',
726     });
727     my $library2 = $builder->build({
728         source => 'Branch',
729     });
730     my $itemtype = $builder->build({
731         source => 'Itemtype',
732     });
733
734     my $biblio = $builder->build_sample_biblio();
735     my $item_infos = [
736         { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
737         { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
738         { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
739         { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
740         { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
741         { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
742         { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
743         { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
744     ];
745     my $number_of_items = scalar @$item_infos;
746     my $number_of_items_with_homebranch_is_CPL =
747       grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
748
749     my @itemnumbers;
750     for my $item_info (@$item_infos) {
751         my $itemnumber = $builder->build_sample_item(
752             {
753                 biblionumber  => $biblio->biblionumber,
754                 homebranch    => $item_info->{homebranch},
755                 holdingbranch => $item_info->{holdingbranch},
756                 itype         => $itemtype->{itemtype}
757             }
758         )->itemnumber;
759
760         push @itemnumbers, $itemnumber;
761     }
762
763     # Emptied the OpacHiddenItems pref
764     t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
765
766     my ($itemfield) =
767       C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
768     my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
769     warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
770     { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
771       'Should carp is no record passed.';
772
773     C4::Biblio::EmbedItemsInMarcBiblio({
774         marc_record  => $record,
775         biblionumber => $biblio->biblionumber });
776     my @items = $record->field($itemfield);
777     is( scalar @items, $number_of_items, 'Should return all items' );
778
779     my $marc_with_items = C4::Biblio::GetMarcBiblio({
780         biblionumber => $biblio->biblionumber,
781         embed_items  => 1 });
782     is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
783
784     C4::Biblio::EmbedItemsInMarcBiblio({
785         marc_record  => $record,
786         biblionumber => $biblio->biblionumber,
787         item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
788     @items = $record->field($itemfield);
789     is( scalar @items, 2, 'Should return all items present in the list' );
790
791     C4::Biblio::EmbedItemsInMarcBiblio({
792         marc_record  => $record,
793         biblionumber => $biblio->biblionumber,
794         opac         => 1 });
795     @items = $record->field($itemfield);
796     is( scalar @items, $number_of_items, 'Should return all items for opac' );
797
798     my $opachiddenitems = "
799         homebranch: ['$library1->{branchcode}']";
800     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
801
802     C4::Biblio::EmbedItemsInMarcBiblio({
803         marc_record  => $record,
804         biblionumber => $biblio->biblionumber });
805     @items = $record->field($itemfield);
806     is( scalar @items,
807         $number_of_items,
808         'Even with OpacHiddenItems set, all items should have been embedded' );
809
810     C4::Biblio::EmbedItemsInMarcBiblio({
811         marc_record  => $record,
812         biblionumber => $biblio->biblionumber,
813         opac         => 1 });
814     @items = $record->field($itemfield);
815     is(
816         scalar @items,
817         $number_of_items - $number_of_items_with_homebranch_is_CPL,
818 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
819     );
820
821     $opachiddenitems = "
822         homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
823     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
824     C4::Biblio::EmbedItemsInMarcBiblio({
825         marc_record  => $record,
826         biblionumber => $biblio->biblionumber,
827         opac         => 1 });
828     @items = $record->field($itemfield);
829     is(
830         scalar @items,
831         0,
832 'For OPAC, If all items are hidden, no item should have been embedded'
833     );
834
835     $schema->storage->txn_rollback;
836 };
837
838
839 subtest 'get_hostitemnumbers_of' => sub {
840     plan tests => 3;
841
842     $schema->storage->txn_begin;
843     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
844     my $builder = t::lib::TestBuilder->new;
845
846     # Host item field without 0 or 9
847     my $bib1 = MARC::Record->new();
848     $bib1->append_fields(
849         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
850         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
851         MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
852     );
853     my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
854     my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
855     is( scalar @itemnumbers1, 0, '773 without 0 or 9');
856
857     # Correct host item field, analytical records on
858     t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
859     my $hostitem = $builder->build_sample_item();
860     my $bib2 = MARC::Record->new();
861     $bib2->append_fields(
862         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
863         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
864         MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
865     );
866     my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
867     my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
868     is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
869
870     # Correct host item field, analytical records off
871     t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
872     @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
873     is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
874
875     $schema->storage->txn_rollback;
876 };
877
878 subtest 'Test logging for ModItem' => sub {
879
880     plan tests => 3;
881
882     t::lib::Mocks::mock_preference('CataloguingLog', 1);
883
884     $schema->storage->txn_begin;
885
886     my $builder = t::lib::TestBuilder->new;
887     my $library = $builder->build({
888         source => 'Branch',
889     });
890     my $itemtype = $builder->build({
891         source => 'Itemtype',
892     });
893
894     # Create a biblio instance for testing
895     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
896     my $biblio = $builder->build_sample_biblio();
897
898     # Add an item.
899     my $item = $builder->build_sample_item(
900         {
901             biblionumber => $biblio->biblionumber,
902             library      => $library->{homebranch},
903             location     => $location,
904             itype        => $itemtype->{itemtype}
905         }
906     );
907
908     # False means no logging
909     $schema->resultset('ActionLog')->search()->delete();
910     $item->location($location)->store({ log_action => 0 });
911     is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
912
913     # True means logging
914     $schema->resultset('ActionLog')->search()->delete();
915     $item->location('new location')->store({ log_action => 1 });
916     is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
917
918     # Undefined defaults to true
919     $schema->resultset('ActionLog')->search()->delete();
920     $item->location($location)->store();
921     is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
922
923     $schema->storage->txn_rollback;
924 };
925
926 subtest 'Check stockrotationitem relationship' => sub {
927     plan tests => 1;
928
929     $schema->storage->txn_begin();
930
931     my $builder = t::lib::TestBuilder->new;
932     my $item = $builder->build_sample_item;
933
934     $builder->build({
935         source => 'Stockrotationitem',
936         value  => { itemnumber_id => $item->itemnumber }
937     });
938
939     my $sritem = Koha::Items->find($item->itemnumber)->stockrotationitem;
940     isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
941
942     $schema->storage->txn_rollback;
943 };
944
945 subtest 'Check add_to_rota method' => sub {
946     plan tests => 2;
947
948     $schema->storage->txn_begin();
949
950     my $builder = t::lib::TestBuilder->new;
951     my $item = $builder->build_sample_item;
952     my $rota = $builder->build({ source => 'Stockrotationrota' });
953     my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
954
955     $builder->build({
956         source => 'Stockrotationstage',
957         value  => { rota_id => $rota->{rota_id} },
958     });
959
960     my $sritem = Koha::Items->find($item->itemnumber);
961     $sritem->add_to_rota($rota->{rota_id});
962
963     is(
964         Koha::StockRotationItems->find($item->itemnumber)->stage_id,
965         $srrota->stockrotationstages->next->stage_id,
966         "Adding to a rota a new sritem item being assigned to its first stage."
967     );
968
969     my $newrota = $builder->build({ source => 'Stockrotationrota' });
970
971     my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
972
973     $builder->build({
974         source => 'Stockrotationstage',
975         value  => { rota_id => $newrota->{rota_id} },
976     });
977
978     $sritem->add_to_rota($newrota->{rota_id});
979
980     is(
981         Koha::StockRotationItems->find($item->itemnumber)->stage_id,
982         $srnewrota->stockrotationstages->next->stage_id,
983         "Moving an item results in that sritem being assigned to the new first stage."
984     );
985
986     $schema->storage->txn_rollback;
987 };
988
989 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
990     plan tests => 3;
991     $schema->storage->txn_begin;
992
993     my $builder = t::lib::TestBuilder->new;
994     my $item = $builder->build_sample_item({ ccode => 'A|B' });
995
996     Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
997     Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
998     my $mapping = Koha::MarcSubfieldStructure->new(
999         {
1000             frameworkcode => q{},
1001             tagfield      => '952',
1002             tagsubfield   => '8',
1003             kohafield     => 'items.ccode',
1004             repeatable    => 1
1005         }
1006     )->store;
1007     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1008
1009     # Start testing
1010     my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1011     my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
1012     is( @subs, 2, 'Expect two subfields' );
1013     is( $subs[0], 'A', 'First subfield matches' );
1014     is( $subs[1], 'B', 'Second subfield matches' );
1015
1016     $schema->storage->txn_rollback;
1017 };
1018
1019 subtest 'ModItemFromMarc' => sub {
1020     plan tests => 8;
1021     $schema->storage->txn_begin;
1022
1023     my $builder = t::lib::TestBuilder->new;
1024     my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
1025     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1026     my $biblio = $builder->build_sample_biblio;
1027     my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
1028     my $item_record = MARC::Record->new;
1029     $item_record->append_fields(
1030         MARC::Field->new(
1031             $itemfield, '', '',
1032             'y' => $itemtype->itemtype,
1033         ),
1034         MARC::Field->new(
1035             $itemfield, '', '',
1036             $lost_sf => '1',
1037         ),
1038     );
1039     my (undef, undef, $itemnumber) = AddItemFromMarc($item_record,
1040         $biblio->biblionumber);
1041
1042     my $item = Koha::Items->find($itemnumber);
1043     is( $item->itemlost, 1, 'itemlost picked from the item marc');
1044
1045     $item->new_status("this is something")->store;
1046
1047     my $updated_item_record = MARC::Record->new;
1048     $updated_item_record->append_fields(
1049         MARC::Field->new(
1050             $itemfield, '', '',
1051             'y' => $itemtype->itemtype,
1052         )
1053     );
1054
1055     my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
1056     is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
1057     is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
1058     is( Koha::Items->find($itemnumber)->new_status, "this is something" );
1059
1060     subtest 'cn_sort' => sub {
1061         plan tests => 3;
1062
1063         my $item = $builder->build_sample_item;
1064         $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
1065         is( $item->cn_sort, 'XXX', 'init values set are expected' );
1066
1067         my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1068         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1069         is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
1070
1071         $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
1072         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1073         is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
1074     };
1075
1076     subtest 'onloan' => sub {
1077         plan tests => 3;
1078
1079         my $item = $builder->build_sample_item;
1080         $item->set({ onloan => '2022-03-19' })->store;
1081         is( $item->onloan, '2022-03-19', 'init values set are expected' );
1082
1083         my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1084         my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
1085         $marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
1086         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1087         is( $item->get_from_storage->onloan, '2022-03-19', 'onloan has not been updated if not passed' );
1088
1089         $marc = C4::Items::Item2Marc( { %{$item->unblessed}, onloan => '2022-03-26' }, $item->biblionumber );
1090         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1091         is( $item->get_from_storage->onloan, '2022-03-26', 'onloan has been updated when passed in' );
1092     };
1093
1094     subtest 'dateaccessioned' => sub {
1095         plan tests => 3;
1096
1097         my $item = $builder->build_sample_item;
1098         $item->set({ dateaccessioned => '2022-03-19' })->store->discard_changes;
1099         is( $item->dateaccessioned, '2022-03-19', 'init values set are expected' );
1100
1101         my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
1102         my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.dateaccessioned' );
1103         $marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
1104         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1105         is( $item->get_from_storage->dateaccessioned, '2022-03-19', 'dateaccessioned has not been updated if not passed' );
1106
1107         $marc = C4::Items::Item2Marc( { %{$item->unblessed}, dateaccessioned => '2022-03-26' }, $item->biblionumber );
1108         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1109         is( $item->get_from_storage->dateaccessioned, '2022-03-26', 'dateaccessioned has been updated when passed in' );
1110     };
1111
1112     subtest 'permanent_location' => sub {
1113         plan tests => 10;
1114
1115         # Make sure items.permanent_location is not mapped
1116         Koha::MarcSubfieldStructures->search(
1117             {
1118                 frameworkcode => q{},
1119                 kohafield     => 'items.permanent_location',
1120             }
1121         )->delete;
1122         Koha::MarcSubfieldStructures->search(
1123             {
1124                 frameworkcode => q{},
1125                 tagfield     => '952',
1126                 tagsubfield     => 'C',
1127             }
1128         )->delete;
1129         Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1130
1131         my $item = $builder->build_sample_item;
1132
1133         # By default, setting location to something new should set permanent location to the same thing
1134         # with the usual exceptions
1135         $item->set({ location => 'A', permanent_location => 'A' })->store;
1136         is( $item->location, 'A', 'initial location set as expected' );
1137         is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
1138
1139         $item->location('B');
1140         my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1141         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1142
1143         $item = $item->get_from_storage;
1144         is( $item->location, 'B', 'new location set as expected' );
1145         is( $item->permanent_location, 'B', 'new permanent location set as expected' );
1146
1147         # Added a marc mapping for permanent location, allows it to be edited independently
1148         my $mapping = Koha::MarcSubfieldStructure->new(
1149             {
1150                 frameworkcode => q{},
1151                 tagfield      => '952',
1152                 tagsubfield   => 'C',
1153                 kohafield     => 'items.permanent_location',
1154                 repeatable    => 0,
1155                 tab           => 10,
1156                 hidden        => 0,
1157             }
1158         )->store;
1159         Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1160
1161         # Now if we change location, and also pass in a permanent location
1162         # the permanent_location will not be overwritten by location
1163         $item->location('C');
1164         $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1165         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1166         $item = $item->get_from_storage;
1167         is( $item->location, 'C', 'next new location set as expected' );
1168         is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
1169
1170         $item->permanent_location(undef)->more_subfields_xml(undef)->store;
1171         # Clear values from the DB
1172         $item = $item->get_from_storage;
1173
1174         # Update the location
1175         $item->location('D');
1176         $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1177         # Remove the permanent_location field from the form
1178         $marc->field('952')->delete_subfield("C");
1179         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1180         $item = $item->get_from_storage;
1181         is( $item->location, 'D', 'next new location set as expected' );
1182         is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
1183
1184         # Clear values from the DB
1185         $item->permanent_location(undef)->more_subfields_xml(undef)->store;
1186         $item = $item->get_from_storage;
1187
1188         # This time nothing is set, but we pass an empty string
1189         $item->permanent_location("");
1190         $item->location('E');
1191         $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
1192         $marc->field('952')->add_subfields( "C", "" );
1193         ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
1194         $item = $item->get_from_storage;
1195         is( $item->location, 'E', 'next new location set as expected' );
1196         is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
1197     };
1198
1199     $schema->storage->txn_rollback;
1200     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
1201 }