Koha/t/Koha_MetadataRecord.t
Aleisha Amohia 39b17d0526
Bug 30358: Strip leading/trailing whitespace characters from input fields when cataloguing
This enhancement adds a system preference StripWhitespaceChars which,
when enabled, will strip leading and trailing whitespace characters from
all fields when cataloguing both bibliographic records and authority
records. Whitespace characters that will be stripped are:
- spaces
- newlines
- carriage returns
- tabs

To test:
1. Apply patch and install database updates
2. Go to Administration, system preferences, find the new
StripWhitespaceChars preference. It should be "Don't strip" by default.
Change it to "Strip".
3. Search for a biblio record and edit it. Put some leading or trailing
whitespace characters in input fields and textarea fields and save.
4. Confirm these characters are removed when you save the record.
5. Repeat steps 3 and 4 for authority records.
6. Confirm tests pass t/db_dependent/Biblio/ModBiblioMarc.t

Sponsored-by: Educational Services Australia SCIS

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Also strip inner newlines

This patch amends the StripWhitespaceChars system preference to also
strip inner newlines (line breaks and carriage returns) when enabled.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Inner newlines should be replaced with a space

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Fixing tests and including for inner newlines

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Clarify syspref wording about fields affected

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Consider field has multiple subfields of same key

To test:

1) Click the clone subfield button to make multiple subfields with the
same key, i.e. 500$a$a$a
2) Save the record and confirm that the fields contain the correct data
after whitespaces are stripped.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Put multiple subfields fix on auth side

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

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

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Fixing ModBiblioMarc.t tests

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Do not strip whitespace from control fields

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 30358: (follow-up) Simplify regex

The regex does the following:
1. Replace newlines and carriage returns with a space
2. Replace leading and trailing whitespace with nothing (strip)

Signed-off-by: Hammat Wele <hammat.wele@inlibro.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-05-16 15:17:26 -03:00

166 lines
5.2 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2013 C & P Bibliography Services
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 6;
use Test::Warn;
use MARC::Record;
BEGIN {
use_ok('Koha::MetadataRecord');
}
my $marcrecord = MARC::Record->new;
$marcrecord->add_fields(
[ '001', '1234' ],
[ '150', ' ', ' ', a => 'Cooking' ],
[ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
);
my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
my $samplehash = [
{
'value' => '1234',
'tag' => '001',
},
{
'subfield' => [
{
'value' => 'Cooking',
'subtag' => 'a'
}
],
'indicator2' => ' ',
'tag' => 150,
'indicator1' => ' ',
},
{
'subfield' => [
{
'value' => 'Cookery',
'subtag' => 'a'
},
{
'value' => 'Instructional manuals',
'subtag' => 'z'
}
],
'indicator2' => ' ',
'tag' => 450,
'indicator1' => ' ',
}
];
my $hash = $record->createMergeHash();
my %fieldkeys;
foreach my $field (@$hash) {
$fieldkeys{delete $field->{'key'}}++;
if (defined $field->{'subfield'}) {
foreach my $subfield (@{$field->{'subfield'}}) {
$fieldkeys{delete $subfield->{'subkey'}}++;
}
}
}
is_deeply($hash, $samplehash, 'Generated hash correctly');
my $dupkeys = grep { $_ > 1 } values %fieldkeys;
is($dupkeys, 0, 'No duplicate keys');
subtest "new() tests" => sub {
plan tests => 14;
# Test default values with a MARC::Record record
my $record = MARC::Record->new();
my $metadata_record;
warning_is { $metadata_record = Koha::MetadataRecord->new({
record => $record }) }
{ carped => 'No schema passed' },
"Metadata schema is mandatory, raise a carped warning if omitted";
is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted");
$metadata_record = Koha::MetadataRecord->new({
record => $record,
schema => 'marc21'
});
is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved');
is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21');
is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc');
is( $metadata_record->id, undef, 'id is optional, undef if unspecifid');
# Test passed values, also no constraint on record type
my $weird_record = {};
bless $weird_record, 'Weird::Class';
$metadata_record = Koha::MetadataRecord->new({
record => $weird_record,
schema => 'something',
format => 'else',
id => 'an id'
});
is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved');
is( $metadata_record->schema, 'something', 'Metadata schema correctly set');
is( $metadata_record->format, 'else', 'Serializacion format correctly set');
is( $metadata_record->id, 'an id', 'The id correctly set');
# Having a record object is mandatory
warning_is { $metadata_record = Koha::MetadataRecord->new({
record => undef,
schema => 'something',
format => 'else',
id => 'an id'
}) }
{ carped => 'No record passed' },
'Undefined record raises carped warning';
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" );
};