From 9f6b49813289056c540574da96f79ab6d0e21492 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Mon, 15 Aug 2022 16:48:55 +1200 Subject: [PATCH] Bug 30358: (follow-up) stripWhitespaceChars subroutine and tests To test: Confirm test plan above still works as expected and tests pass in t/Koha_MetadataRecord.t Sponsored-by: Catalyst IT Signed-off-by: David Nind --- C4/AuthoritiesMarc.pm | 11 +---------- C4/Biblio.pm | 11 +---------- Koha/MetadataRecord.pm | 25 +++++++++++++++++++++++++ t/Koha_MetadataRecord.t | 23 ++++++++++++++++++++++- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 677f03d03c..d48bb72571 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -642,16 +642,7 @@ sub AddAuthority { } if ( C4::Context->preference('StripWhitespaceChars') ) { - 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 - } - } + $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); } # Save record into auth_header, update 001 diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 6aafa6ac0b..10299c2c35 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2753,16 +2753,7 @@ sub ModBiblioMarc { } if ( C4::Context->preference('StripWhitespaceChars') ) { - 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 - } - } + $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); } my $metadata = { diff --git a/Koha/MetadataRecord.pm b/Koha/MetadataRecord.pm index 6f0aa1fb4b..80e8ecdd48 100644 --- a/Koha/MetadataRecord.pm +++ b/Koha/MetadataRecord.pm @@ -108,4 +108,29 @@ sub createMergeHash { } } +=head2 stripWhitespaceChars + + $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); + +Strip leading and trailing whitespace characters from input fields. + +=cut + +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 + } + } + + return $record; +} + 1; diff --git a/t/Koha_MetadataRecord.t b/t/Koha_MetadataRecord.t index 0bcc4690dc..7202f4a86f 100755 --- a/t/Koha_MetadataRecord.t +++ b/t/Koha_MetadataRecord.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Test::Warn; use MARC::Record; @@ -143,3 +143,24 @@ subtest "new() tests" => sub { is( $metadata_record, undef, 'record object mandatory') }; +subtest "stripWhitespaceChars() tests" => sub { + plan tests => 2; + + # Test default values with a MARC::Record record + my $record = MARC::Record->new(); + + $record->add_fields( + [ '001', '1234' ], + [ '150', ' ', ' ', a => 'Test' ], + [ '520', ' ', ' ', a => "This is\na test!\t" ], + [ '521', ' ', ' ', a => "This is a\t test!\t" ], + ); + + $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); + + my $get520a = $record->subfield('520','a'); + is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" ); + + my $get521a = $record->subfield('521','a'); + is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" ); +}; -- 2.30.2