Koha/t/db_dependent/Biblio/ModBiblioMarc.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

84 lines
3 KiB
Perl
Executable file

#!/usr/bin/perl
# 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 => 2;
use t::lib::Mocks;
use t::lib::TestBuilder;
use MARC::Record;
use C4::Biblio qw( ModBiblio ModBiblioMarc );
use Koha::Database;
use Koha::Biblios;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
subtest "Check MARC field length calculation" => sub {
plan tests => 3;
t::lib::Mocks->mock_preference( 'marcflavour', 'MARC21' );
my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
my $record = MARC::Record->new;
$record->append_fields(
MARC::Field->new( '100', '', '', a => 'My title' ),
);
is( $record->leader, ' 'x24, 'No leader lengths' );
C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
my $savedrec = $biblio->metadata->record;
like( substr($savedrec->leader,0,5), qr/^\d{5}$/, 'Record length found' );
like( substr($savedrec->leader,12,5), qr/^\d{5}$/, 'Base address found' );
};
subtest "StripWhitespaceChars tests" => sub {
plan tests => 4;
t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
t::lib::Mocks::mock_preference('StripWhitespaceChars', 0);
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" ),
);
my $title = $record->title;
is( $title, " My\ntitle\n", 'Title has whitespace characters' );
C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
$biblio = Koha::Biblios->find( $biblio->biblionumber );
my $savedrec = $biblio->metadata->record;
my $savedtitle = $savedrec->title;
is( $savedtitle, " My\ntitle\n", "Title still has whitespace characters because StripWhitespaceChars is disabled" );
t::lib::Mocks::mock_preference('StripWhitespaceChars', 1);
C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
$biblio = Koha::Biblios->find( $biblio->biblionumber );
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;