Bug 18736: Calculate tax depending on rounding
[srvgit] / t / db_dependent / Items.t
index 92151d6..4744cff 100755 (executable)
@@ -29,11 +29,12 @@ use Koha::Library;
 use Koha::DateUtils;
 use Koha::MarcSubfieldStructures;
 use Koha::Caches;
+use Koha::AuthorisedValues;
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
-use Test::More tests => 12;
+use Test::More tests => 15;
 
 use Test::Warn;
 
@@ -56,51 +57,57 @@ subtest 'General Add, Get and Del tests' => sub {
 
     # Create a biblio instance for testing
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
-    my ($bibnum, $bibitemnum) = get_biblio();
+    my $biblio = $builder->build_sample_biblio();
 
     # Add an item.
-    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
-    cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
-    cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
+    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber);
+    cmp_ok($item_bibnum, '==', $biblio->biblionumber, "New item is linked to correct biblionumber.");
+    cmp_ok($item_bibitemnum, '==', $biblio->biblioitem->biblioitemnumber, "New item is linked to correct biblioitemnumber.");
 
     # Get item.
-    my $getitem = GetItem($itemnumber);
-    cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
-    cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
-    is( $getitem->{location}, $location, "The location should not have been modified" );
-    is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to the location value" );
+    my $getitem = Koha::Items->find($itemnumber);
+    cmp_ok($getitem->itemnumber, '==', $itemnumber, "Retrieved item has correct itemnumber.");
+    cmp_ok($getitem->biblioitemnumber, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
+    is( $getitem->location, $location, "The location should not have been modified" );
+    is( $getitem->permanent_location, $location, "The permanent_location should have been set to the location value" );
+
+
+    # Do not modify anything, and do not explode!
+    my $dbh = C4::Context->dbh;
+    local $dbh->{RaiseError} = 1;
+    ModItem({}, $biblio->biblionumber, $itemnumber);
 
     # Modify item; setting barcode.
-    ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
-    my $moditem = GetItem($itemnumber);
-    cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
+    ModItem({ barcode => '987654321' }, $biblio->biblionumber, $itemnumber);
+    my $moditem = Koha::Items->find($itemnumber);
+    cmp_ok($moditem->barcode, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->barcode . '.');
 
     # Delete item.
-    DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
-    my $getdeleted = GetItem($itemnumber);
-    is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
+    DelItem({ biblionumber => $biblio->biblionumber, itemnumber => $itemnumber });
+    my $getdeleted = Koha::Items->find($itemnumber);
+    is($getdeleted, undef, "Item deleted as expected.");
 
-    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $bibnum);
-    $getitem = GetItem($itemnumber);
-    is( $getitem->{location}, $location, "The location should not have been modified" );
-    is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" );
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $biblio->biblionumber);
+    $getitem = Koha::Items->find($itemnumber);
+    is( $getitem->location, $location, "The location should not have been modified" );
+    is( $getitem->permanent_location, 'my permanent location', "The permanent_location should not have modified" );
 
-    ModItem({ location => $location }, $bibnum, $itemnumber);
-    $getitem = GetItem($itemnumber);
-    is( $getitem->{location}, $location, "The location should have been set to correct location" );
-    is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to location" );
+    ModItem({ location => $location }, $biblio->biblionumber, $itemnumber);
+    $getitem = Koha::Items->find($itemnumber);
+    is( $getitem->location, $location, "The location should have been set to correct location" );
+    is( $getitem->permanent_location, $location, "The permanent_location should have been set to location" );
 
-    ModItem({ location => 'CART' }, $bibnum, $itemnumber);
-    $getitem = GetItem($itemnumber);
-    is( $getitem->{location}, 'CART', "The location should have been set to CART" );
-    is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" );
+    ModItem({ location => 'CART' }, $biblio->biblionumber, $itemnumber);
+    $getitem = Koha::Items->find($itemnumber);
+    is( $getitem->location, 'CART', "The location should have been set to CART" );
+    is( $getitem->permanent_location, $location, "The permanent_location should not have been set to CART" );
 
     t::lib::Mocks::mock_preference('item-level_itypes', '1');
-    $getitem = GetItem($itemnumber);
-    is( $getitem->{itype}, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
+    $getitem = Koha::Items->find($itemnumber);
+    is( $getitem->effective_itemtype, $itemtype->{itemtype}, "Itemtype set correctly when using item-level_itypes" );
     t::lib::Mocks::mock_preference('item-level_itypes', '0');
-    $getitem = GetItem($itemnumber);
-    is( $getitem->{itype}, undef, "Itemtype set correctly when not using item-level_itypes" );
+    $getitem = Koha::Items->find($itemnumber);
+    is( $getitem->effective_itemtype, $biblio->biblioitem->itemtype, "Itemtype set correctly when not using item-level_itypes" );
 
     $schema->storage->txn_rollback;
 };
@@ -142,7 +149,7 @@ subtest 'ModItem tests' => sub {
 
 subtest 'GetHiddenItemnumbers tests' => sub {
 
-    plan tests => 9;
+    plan tests => 11;
 
     # This sub is controlled by the OpacHiddenItems system preference.
 
@@ -162,7 +169,7 @@ subtest 'GetHiddenItemnumbers tests' => sub {
 
     # Create a new biblio
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
-    my ($biblionumber, $biblioitemnumber) = get_biblio();
+    my $biblio = $builder->build_sample_biblio();
 
     # Add two items
     my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
@@ -172,7 +179,7 @@ subtest 'GetHiddenItemnumbers tests' => sub {
             withdrawn     => 1,
             itype         => $itemtype->{itemtype},
         },
-        $biblionumber
+        $biblio->biblionumber
     );
     my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
         {
@@ -181,31 +188,31 @@ subtest 'GetHiddenItemnumbers tests' => sub {
             withdrawn     => 0,
             itype         => $itemtype->{itemtype},
         },
-        $biblionumber
+        $biblio->biblionumber
     );
 
     my $opachiddenitems;
     my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
     my @hidden;
     my @items;
-    push @items, GetItem( $item1_itemnumber );
-    push @items, GetItem( $item2_itemnumber );
+    push @items, Koha::Items->find( $item1_itemnumber )->unblessed;
+    push @items, Koha::Items->find( $item2_itemnumber )->unblessed;
 
     # Empty OpacHiddenItems
     t::lib::Mocks::mock_preference('OpacHiddenItems','');
-    ok( !defined( GetHiddenItemnumbers( @items ) ),
+    ok( !defined( GetHiddenItemnumbers( { items => \@items } ) ),
         "Hidden items list undef if OpacHiddenItems empty");
 
     # Blank spaces
     t::lib::Mocks::mock_preference('OpacHiddenItems','  ');
-    ok( scalar GetHiddenItemnumbers( @items ) == 0,
+    ok( scalar GetHiddenItemnumbers( { items => \@items } ) == 0,
         "Hidden items list empty if OpacHiddenItems only contains blanks");
 
     # One variable / value
     $opachiddenitems = "
         withdrawn: [1]";
     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
-    @hidden = GetHiddenItemnumbers( @items );
+    @hidden = GetHiddenItemnumbers( { items => \@items } );
     ok( scalar @hidden == 1, "Only one hidden item");
     is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
 
@@ -213,7 +220,7 @@ subtest 'GetHiddenItemnumbers tests' => sub {
     $opachiddenitems = "
         withdrawn: [1,0]";
     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
-    @hidden = GetHiddenItemnumbers( @items );
+    @hidden = GetHiddenItemnumbers( { items => \@items } );
     ok( scalar @hidden == 2, "Two items hidden");
     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
 
@@ -223,13 +230,20 @@ subtest 'GetHiddenItemnumbers tests' => sub {
         homebranch: [$library2->{branchcode}]
     ";
     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
-    @hidden = GetHiddenItemnumbers( @items );
+    @hidden = GetHiddenItemnumbers( { items => \@items } );
     ok( scalar @hidden == 2, "Two items hidden");
     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
 
+    # Override hidden with patron category
+    t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'S' );
+    @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'PT' } );
+    ok( scalar @hidden == 2, "Two items still hidden");
+    @hidden = GetHiddenItemnumbers( { items => \@items, borcat => 'S' } );
+    ok( scalar @hidden == 0, "Two items not hidden");
+
     # Valid OpacHiddenItems, empty list
     @items = ();
-    @hidden = GetHiddenItemnumbers( @items );
+    @hidden = GetHiddenItemnumbers( { items => \@items } );
     ok( scalar @hidden == 0, "Empty items list, no item hidden");
 
     $schema->storage->txn_rollback;
@@ -237,7 +251,7 @@ subtest 'GetHiddenItemnumbers tests' => sub {
 
 subtest 'GetItemsInfo tests' => sub {
 
-    plan tests => 4;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
@@ -252,16 +266,27 @@ subtest 'GetItemsInfo tests' => sub {
         source => 'Itemtype',
     });
 
+    Koha::AuthorisedValues->delete;
+    my $av1 = Koha::AuthorisedValue->new(
+        {
+            category         => 'RESTRICTED',
+            authorised_value => '1',
+            lib              => 'Restricted Access',
+            lib_opac         => 'Restricted Access OPAC',
+        }
+    )->store();
+
     # Add a biblio
-    my ($biblionumber, $biblioitemnumber) = get_biblio();
+    my $biblio = $builder->build_sample_biblio();
     # Add an item
     my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
         {
             homebranch    => $library1->{branchcode},
             holdingbranch => $library2->{branchcode},
             itype         => $itemtype->{itemtype},
+            restricted    => 1,
         },
-        $biblionumber
+        $biblio->biblionumber
     );
 
     my $library = Koha::Libraries->find( $library1->{branchcode} );
@@ -272,14 +297,21 @@ subtest 'GetItemsInfo tests' => sub {
     $library->opac_info("holdingbranch OPAC info");
     $library->store;
 
-    my @results = GetItemsInfo( $biblionumber );
+    my @results = GetItemsInfo( $biblio->biblionumber );
     ok( @results, 'GetItemsInfo returns results');
+
     is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
         'GetItemsInfo returns the correct home branch OPAC info notice' );
     is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
         'GetItemsInfo returns the correct holding branch OPAC info notice' );
     is( exists( $results[0]->{ onsite_checkout } ), 1,
         'GetItemsInfo returns a onsite_checkout key' );
+    is( $results[0]->{ restricted }, 1,
+        'GetItemsInfo returns a restricted value code' );
+    is( $results[0]->{ restrictedvalue }, "Restricted Access",
+        'GetItemsInfo returns a restricted value description (staff)' );
+    is( $results[0]->{ restrictedvalueopac }, "Restricted Access OPAC",
+        'GetItemsInfo returns a restricted value description (OPAC)' );
 
     $schema->storage->txn_rollback;
 };
@@ -344,7 +376,8 @@ subtest 'SearchItems test' => sub {
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
     my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
 
-    my ($biblionumber) = get_biblio();
+    my $biblio = $builder->build_sample_biblio({ title => 'Silence in the library' });
+    $builder->build_sample_biblio({ title => 'Silence in the shadow' });
 
     my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
 
@@ -353,12 +386,12 @@ subtest 'SearchItems test' => sub {
         homebranch => $library1->{branchcode},
         holdingbranch => $library1->{branchcode},
         itype => $itemtype->{itemtype},
-    }, $biblionumber);
+    }, $biblio->biblionumber);
     my (undef, undef, $item2_itemnumber) = AddItem({
         homebranch => $library2->{branchcode},
         holdingbranch => $library2->{branchcode},
         itype => $itemtype->{itemtype},
-    }, $biblionumber);
+    }, $biblio->biblionumber);
 
     my ($items, $total_results);
 
@@ -463,7 +496,7 @@ subtest 'SearchItems test' => sub {
         )
     );
     my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
-        $biblionumber);
+        $biblio->biblionumber);
 
     # Search item where item subfield z is "foobar"
     $filter = {
@@ -485,11 +518,11 @@ subtest 'SearchItems test' => sub {
     $cache->clear_from_cache("default_value_for_mod_marc-");
     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
 
-    ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
+    ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber);
 
     # Make sure the link is used
-    my $item3 = GetItem($item3_itemnumber);
-    ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
+    my $item3 = Koha::Items->find($item3_itemnumber);
+    ok($item3->itemnotes eq 'foobar', 'itemnotes eq "foobar"');
 
     # Do the same search again.
     # This time it will search in items.itemnotes
@@ -521,14 +554,14 @@ subtest 'Koha::Item(s) tests' => sub {
 
     # Create a biblio and item for testing
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
-    my ($bibnum, $bibitemnum) = get_biblio();
+    my $biblio = $builder->build_sample_biblio();
     my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
         {
             homebranch    => $library1->{branchcode},
             holdingbranch => $library2->{branchcode},
             itype         => $itemtype->{itemtype},
         },
-        $bibnum
+        $biblio->biblionumber
     );
 
     # Get item.
@@ -547,7 +580,7 @@ subtest 'Koha::Item(s) tests' => sub {
 };
 
 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
-    plan tests => 7;
+    plan tests => 8;
 
     $schema->storage->txn_begin();
 
@@ -562,7 +595,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
         source => 'Itemtype',
     });
 
-    my ( $biblionumber, $biblioitemnumber ) = get_biblio();
+    my $biblio = $builder->build_sample_biblio();
     my $item_infos = [
         { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
         { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
@@ -585,7 +618,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
                 holdingbranch => $item_info->{holdingbanch},
                 itype         => $itemtype->{itemtype},
             },
-            $biblionumber
+            $biblio->biblionumber
         );
         push @itemnumbers, $itemnumber;
     }
@@ -595,21 +628,33 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
 
     my ($itemfield) =
       C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
-    my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
+    my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
     warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
     { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
-      'Should crap is no record passed.';
+      'Should carp is no record passed.';
 
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio->biblionumber });
     my @items = $record->field($itemfield);
     is( scalar @items, $number_of_items, 'Should return all items' );
 
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber,
-        [ $itemnumbers[1], $itemnumbers[3] ] );
+    my $marc_with_items = C4::Biblio::GetMarcBiblio({
+        biblionumber => $biblio->biblionumber,
+        embed_items  => 1 });
+    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
+
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio->biblionumber,
+        item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
     @items = $record->field($itemfield);
     is( scalar @items, 2, 'Should return all items present in the list' );
 
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio->biblionumber,
+        opac         => 1 });
     @items = $record->field($itemfield);
     is( scalar @items, $number_of_items, 'Should return all items for opac' );
 
@@ -617,13 +662,18 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
         homebranch: ['$library1->{branchcode}']";
     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
 
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio->biblionumber });
     @items = $record->field($itemfield);
     is( scalar @items,
         $number_of_items,
         'Even with OpacHiddenItems set, all items should have been embedded' );
 
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio->biblionumber,
+        opac         => 1 });
     @items = $record->field($itemfield);
     is(
         scalar @items,
@@ -634,7 +684,10 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
     $opachiddenitems = "
         homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio->biblionumber,
+        opac         => 1 });
     @items = $record->field($itemfield);
     is(
         scalar @items,
@@ -669,7 +722,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
     my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
 
     # Create a record with a barcode
-    my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
+    my $biblio = $builder->build_sample_biblio({ frameworkcode => $framework->{frameworkcode} });
     my $item_record = new MARC::Record;
     my $a_barcode = 'a barcode';
     my $barcode_field = MARC::Field->new(
@@ -682,24 +735,24 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
         y => $itemtype
     );
     $item_record->append_fields( $barcode_field );
-    my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
+    my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblio->biblionumber);
 
     # Make sure everything has been set up
-    my $item = GetItem($item_itemnumber);
-    is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
+    my $item = Koha::Items->find($item_itemnumber);
+    is( $item->barcode, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
 
     # Delete the barcode field and save the record
     $item_record->delete_fields( $barcode_field );
     $item_record->append_fields( $itemtype_field ); # itemtype is mandatory
-    ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
-    $item = GetItem($item_itemnumber);
-    is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
+    ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber);
+    $item = Koha::Items->find($item_itemnumber);
+    is( $item->barcode, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
 
     # Re-add the barcode field and save the record
     $item_record->append_fields( $barcode_field );
-    ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
-    $item = GetItem($item_itemnumber);
-    is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
+    ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber);
+    $item = Koha::Items->find($item_itemnumber);
+    is( $item->barcode, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
 
     # Remove the mapping for barcode
     Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
@@ -718,9 +771,9 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
     );
     $item_record->append_fields( $another_barcode_field );
     # The DB value should not have been updated
-    ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
-    $item = GetItem($item_itemnumber);
-    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
+    ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber);
+    $item = Koha::Items->find($item_itemnumber);
+    is ( $item->barcode, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
 
     $cache->clear_from_cache("default_value_for_mod_marc-");
     $cache->clear_from_cache( "MarcSubfieldStructure-" );
@@ -779,18 +832,42 @@ subtest '_mod_item_dates' => sub {
 };
 
 subtest 'get_hostitemnumbers_of' => sub {
-    plan tests => 1;
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
+    my $builder = t::lib::TestBuilder->new;
 
-    my $bib = MARC::Record->new();
-    $bib->append_fields(
+    # Host item field without 0 or 9
+    my $bib1 = MARC::Record->new();
+    $bib1->append_fields(
         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
         MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
     );
-    my ($biblionumber, $bibitemnum) = AddBiblio($bib, '');
+    my ($biblionumber1, $bibitemnum1) = AddBiblio($bib1, '');
+    my @itemnumbers1 = C4::Items::get_hostitemnumbers_of( $biblionumber1 );
+    is( scalar @itemnumbers1, 0, '773 without 0 or 9');
+
+    # Correct host item field, analytical records on
+    t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 1);
+    my $hostitem = $builder->build_sample_item();
+    my $bib2 = MARC::Record->new();
+    $bib2->append_fields(
+        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
+        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
+        MARC::Field->new('773', ' ', ' ', 0 => $hostitem->biblionumber , 9 => $hostitem->itemnumber, b => 'b' ),
+    );
+    my ($biblionumber2, $bibitemnum2) = AddBiblio($bib2, '');
+    my @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
+    is( scalar @itemnumbers2, 1, '773 with 0 and 9, EasyAnalyticalRecords on');
 
-    my @itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
-    is( @itemnumbers, 0, );
+    # Correct host item field, analytical records off
+    t::lib::Mocks::mock_preference('EasyAnalyticalRecords', 0);
+    @itemnumbers2 = C4::Items::get_hostitemnumbers_of( $biblionumber2 );
+    is( scalar @itemnumbers2, 0, '773 with 0 and 9, EasyAnalyticalRecords off');
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'Test logging for ModItem' => sub {
@@ -811,38 +888,110 @@ subtest 'Test logging for ModItem' => sub {
 
     # Create a biblio instance for testing
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
-    my ($bibnum, $bibitemnum) = get_biblio();
+    my $biblio = $builder->build_sample_biblio();
 
     # Add an item.
-    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
+    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber);
 
     # False means no logging
     $schema->resultset('ActionLog')->search()->delete();
-    ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 0 });
+    ModItem({ location => $location }, $biblio->biblionumber, $itemnumber, { log_action => 0 });
     is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' );
 
     # True means logging
     $schema->resultset('ActionLog')->search()->delete();
-    ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 1 });
+    ModItem({ location => $location }, $biblio->biblionumber, $itemnumber, { log_action => 1 });
     is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' );
 
     # Undefined defaults to true
     $schema->resultset('ActionLog')->search()->delete();
-    ModItem({ location => $location }, $bibnum, $itemnumber);
+    ModItem({ location => $location }, $biblio->biblionumber, $itemnumber);
     is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' );
 
     $schema->storage->txn_rollback;
 };
 
-# Helper method to set up a Biblio.
-sub get_biblio {
-    my ( $frameworkcode ) = @_;
-    $frameworkcode //= '';
-    my $bib = MARC::Record->new();
-    $bib->append_fields(
-        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
-        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
+subtest 'Check stockrotationitem relationship' => sub {
+    plan tests => 1;
+
+    $schema->storage->txn_begin();
+
+    my $builder = t::lib::TestBuilder->new;
+    my $item = $builder->build({ source => 'Item' });
+
+    $builder->build({
+        source => 'Stockrotationitem',
+        value  => { itemnumber_id => $item->{itemnumber} }
+    });
+
+    my $sritem = Koha::Items->find($item->{itemnumber})->stockrotationitem;
+    isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Check add_to_rota method' => sub {
+    plan tests => 2;
+
+    $schema->storage->txn_begin();
+
+    my $builder = t::lib::TestBuilder->new;
+    my $item = $builder->build({ source => 'Item' });
+    my $rota = $builder->build({ source => 'Stockrotationrota' });
+    my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
+
+    $builder->build({
+        source => 'Stockrotationstage',
+        value  => { rota_id => $rota->{rota_id} },
+    });
+
+    my $sritem = Koha::Items->find($item->{itemnumber});
+    $sritem->add_to_rota($rota->{rota_id});
+
+    is(
+        Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
+        $srrota->stockrotationstages->next->stage_id,
+        "Adding to a rota a new sritem item being assigned to its first stage."
+    );
+
+    my $newrota = $builder->build({ source => 'Stockrotationrota' });
+
+    my $srnewrota = Koha::StockRotationRotas->find($newrota->{rota_id});
+
+    $builder->build({
+        source => 'Stockrotationstage',
+        value  => { rota_id => $newrota->{rota_id} },
+    });
+
+    $sritem->add_to_rota($newrota->{rota_id});
+
+    is(
+        Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
+        $srnewrota->stockrotationstages->next->stage_id,
+        "Moving an item results in that sritem being assigned to the new first stage."
     );
-    my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
-    return ($bibnum, $bibitemnum);
-}
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
+    plan tests => 3;
+    $schema->storage->txn_begin;
+
+    my $builder = t::lib::TestBuilder->new;
+    my $biblio = $builder->build({ source => 'Biblio', value => { frameworkcode => q{} } });
+    my $item = $builder->build({ source => 'Item', value => { biblionumber => $biblio->{biblionumber}, ccode => 'A|B' } });
+
+    Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
+    Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
+    my $mapping = Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '952', tagsubfield => '8', kohafield => 'items.ccode' })->store;
+
+    # Start testing
+    my $marc = C4::Items::Item2Marc( $item, $biblio->{biblionumber} );
+    my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
+    is( @subs, 2, 'Expect two subfields' );
+    is( $subs[0], 'A', 'First subfield matches' );
+    is( $subs[1], 'B', 'Second subfield matches' );
+
+    $schema->storage->txn_rollback;
+};