From b6d39225bccafbdf58b5481e1ab059368f99db79 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 7 Jun 2023 17:31:37 -0300 Subject: [PATCH] Bug 33749: Use TrimFields instead of stripWhitespaceChars This patch replaces the uses of the static method with the new filter. It also moves the tests to the relevant place and removes the (now) unused method. Signed-off-by: David Nind Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- C4/AuthoritiesMarc.pm | 4 +++- C4/Biblio.pm | 5 +++-- Koha/MetadataRecord.pm | 27 --------------------------- t/Koha_MetadataRecord.t | 25 +------------------------ t/RecordProcessor.t | 24 ++++++++++++++++++++++++ 5 files changed, 31 insertions(+), 54 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 6989a6a513..5dc5c3ad46 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -35,6 +35,7 @@ use Koha::Authority::MergeRequests; use Koha::Authority::Types; use Koha::Authority; use Koha::Libraries; +use Koha::RecordProcessor; use Koha::SearchEngine; use Koha::SearchEngine::Indexer; use Koha::SearchEngine::Search; @@ -659,7 +660,8 @@ sub AddAuthority { } if ( C4::Context->preference('StripWhitespaceChars') ) { - $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); + my $p = Koha::RecordProcessor->new({ filters => qw(TrimFields) }); + $p->process( $record ); } # Save record into auth_header, update 001 diff --git a/C4/Biblio.pm b/C4/Biblio.pm index eea56d7e4c..62772d5d54 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -111,11 +111,11 @@ use Koha::Holds; use Koha::ItemTypes; use Koha::MarcOverlayRules; use Koha::Plugins; +use Koha::RecordProcessor; use Koha::SearchEngine; use Koha::SearchEngine::Indexer; use Koha::Libraries; use Koha::Util::MARC; -use Koha::MetadataRecord; =head1 NAME @@ -2841,7 +2841,8 @@ sub ModBiblioMarc { } if ( C4::Context->preference('StripWhitespaceChars') ) { - $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); + my $p = Koha::RecordProcessor->new({ filters => qw(TrimFields) }); + $p->process( $record ); } my $metadata = { diff --git a/Koha/MetadataRecord.pm b/Koha/MetadataRecord.pm index bb7ec62be0..6f0aa1fb4b 100644 --- a/Koha/MetadataRecord.pm +++ b/Koha/MetadataRecord.pm @@ -108,31 +108,4 @@ 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 ) { - 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+$//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 f44700970c..cb24fb8659 100755 --- a/t/Koha_MetadataRecord.t +++ b/t/Koha_MetadataRecord.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 5; use Test::Warn; use Koha::RecordProcessor; @@ -143,26 +143,3 @@ 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" ], - ); - - my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] }); - $p->process( $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" ); -}; diff --git a/t/RecordProcessor.t b/t/RecordProcessor.t index 9a0db02797..89914b3f3e 100755 --- a/t/RecordProcessor.t +++ b/t/RecordProcessor.t @@ -203,4 +203,28 @@ subtest 'options() tests' => sub { ); }; +subtest "'TrimFields' filter 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" ], + ); + + my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] }); + $p->process( $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" ); +}; + done_testing(); -- 2.39.5