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 <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomas Cohen Arazi 2023-06-07 17:31:37 -03:00
parent 54477ecc51
commit b6d39225bc
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
5 changed files with 31 additions and 54 deletions

View File

@ -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

View File

@ -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 = {

View File

@ -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;

View File

@ -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" );
};

View File

@ -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();