+ is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
+ is( Koha::Items->find($itemnumber)->new_status, "this is something" );
+
+ subtest 'cn_sort' => sub {
+ plan tests => 3;
+
+ my $item = $builder->build_sample_item;
+ $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
+ is( $item->cn_sort, 'XXX', 'init values set are expected' );
+
+ my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
+ ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+ is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
+
+ $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
+ ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+ is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
+ };
+
+ subtest 'permanent_location' => sub {
+ plan tests => 10;
+
+ # Make sure items.permanent_location is not mapped
+ Koha::MarcSubfieldStructures->search(
+ {
+ frameworkcode => q{},
+ kohafield => 'items.permanent_location',
+ }
+ )->delete;
+ Koha::MarcSubfieldStructures->search(
+ {
+ frameworkcode => q{},
+ tagfield => '952',
+ tagsubfield => 'C',
+ }
+ )->delete;
+ Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+
+ my $item = $builder->build_sample_item;
+
+ # By default, setting location to something new should set permanent location to the same thing
+ # with the usual exceptions
+ $item->set({ location => 'A', permanent_location => 'A' })->store;
+ is( $item->location, 'A', 'initial location set as expected' );
+ is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
+
+ $item->location('B');
+ my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+ ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+
+ $item = $item->get_from_storage;
+ is( $item->location, 'B', 'new location set as expected' );
+ is( $item->permanent_location, 'B', 'new permanent location set as expected' );
+
+ # Added a marc mapping for permanent location, allows it to be edited independently
+ my $mapping = Koha::MarcSubfieldStructure->new(
+ {
+ frameworkcode => q{},
+ tagfield => '952',
+ tagsubfield => 'C',
+ kohafield => 'items.permanent_location',
+ repeatable => 0,
+ tab => 10,
+ hidden => 0,
+ }
+ )->store;
+ Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+
+ # Now if we change location, and also pass in a permanent location
+ # the permanent_location will not be overwritten by location
+ $item->location('C');
+ $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+ ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+ $item = $item->get_from_storage;
+ is( $item->location, 'C', 'next new location set as expected' );
+ is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
+
+ $item->permanent_location(undef)->more_subfields_xml(undef)->store;
+ # Clear values from the DB
+ $item = $item->get_from_storage;
+
+ # Update the location
+ $item->location('D');
+ $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+ # Remove the permanent_location field from the form
+ $marc->field('952')->delete_subfield("C");
+ ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+ $item = $item->get_from_storage;
+ is( $item->location, 'D', 'next new location set as expected' );
+ is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
+
+ # Clear values from the DB
+ $item->permanent_location(undef)->more_subfields_xml(undef)->store;
+ $item = $item->get_from_storage;
+
+ # This time nothing is set, but we pass an empty string
+ $item->permanent_location("");
+ $item->location('E');
+ $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+ $marc->field('952')->add_subfields( "C", "" );
+ ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+ $item = $item->get_from_storage;
+ is( $item->location, 'E', 'next new location set as expected' );
+ is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
+ };