From 0097b4ac7c302f4aa57ebddf7dcff3d85a7a9b15 Mon Sep 17 00:00:00 2001
From: Aleisha Amohia <aleishaamohia@hotmail.com>
Date: Mon, 21 Nov 2022 21:34:32 +0000
Subject: [PATCH] Bug 30358 (follow-up): Do not strip whitespace from control
 fields

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 Koha/MetadataRecord.pm                | 16 +++++++++-------
 t/db_dependent/Biblio/ModBiblioMarc.t |  6 +++++-
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/Koha/MetadataRecord.pm b/Koha/MetadataRecord.pm
index 80e8ecdd48..a1a54c2613 100644
--- a/Koha/MetadataRecord.pm
+++ b/Koha/MetadataRecord.pm
@@ -120,13 +120,15 @@ sub stripWhitespaceChars {
     my ( $record ) = @_;
 
     foreach my $field ( $record->fields ) {
-        foreach my $subfield ( $field->subfields ) {
-            my $key = $subfield->[0];
-            my $value = $subfield->[1];
-            $value =~ s/[\n\r]+/ /g;
-            $value =~ s/^\s+|\s+$|^\t+|\t+$//g;
-            $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
-            $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
+        unless ( $field->is_control_field ) {
+            foreach my $subfield ( $field->subfields ) {
+                my $key = $subfield->[0];
+                my $value = $subfield->[1];
+                $value =~ s/[\n\r]+/ /g;
+                $value =~ s/^\s+|\s+$|^\t+|\t+$//g;
+                $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
+                $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
+            }
         }
     }
 
diff --git a/t/db_dependent/Biblio/ModBiblioMarc.t b/t/db_dependent/Biblio/ModBiblioMarc.t
index e049598da9..fc38684c39 100755
--- a/t/db_dependent/Biblio/ModBiblioMarc.t
+++ b/t/db_dependent/Biblio/ModBiblioMarc.t
@@ -48,7 +48,7 @@ subtest "Check MARC field length calculation" => sub {
 };
 
 subtest "StripWhitespaceChars tests" => sub {
-    plan tests => 3;
+    plan tests => 4;
 
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
     t::lib::Mocks::mock_preference('StripWhitespaceChars', 0);
@@ -56,6 +56,7 @@ subtest "StripWhitespaceChars tests" => sub {
     my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
     my $record = MARC::Record->new;
     $record->append_fields(
+        MARC::Field->new( '003', "abcdefg\n" ),
         MARC::Field->new( '245', '', '', a => "  My\ntitle\n" ),
     );
 
@@ -75,6 +76,9 @@ subtest "StripWhitespaceChars tests" => sub {
     my $amendedrec = $biblio->metadata->record;
     my $amendedtitle = $amendedrec->title;
     is( $amendedtitle, "My title", "Whitespace characters removed from title because StripWhitespaceChars is enabled" );
+
+    my $f003 = $record->field('003')->data;
+    is( $f003, "abcdefg\n", "Whitespace characters are not stripped from control fields" );
 };
 
 $schema->storage->txn_rollback;
-- 
2.30.2