Koha/t/Biblio/TransformHtmlToXml.t
Jonathan Druart cc8f3dc1cc Bug 20540: Fix TransformHtmlToXml if last tag is empty
This bug has been found during testing bug 19289.

In some conditions C4::Biblio::TransformHtmlToXml will generate a malformed XML structure. The last </datafield> can be duplicated.

For instance, if a call like:
my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );

with the last value of @field_values is empty, it will return:

<?xml version="1.0" encoding="UTF-8"?>
<collection
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
  xmlns="http://www.loc.gov/MARC21/slim">
<record>
<datafield2 tag="020" ind1=" " ind2=" ">
<subfield code="a">l</subfield>
</datafield>
<datafield1 tag="100" ind1=" " ind2=" ">
<subfield code="a">k</subfield>
</datafield>
<datafield1 tag="245" ind1=" " ind2=" ">
<subfield code="a">k</subfield>
</datafield>
<datafield1 tag="250" ind1=" " ind2=" ">
<subfield code="a">k</subfield>
</datafield>
<datafield1 tag="260" ind1=" " ind2=" ">
<subfield code="b">k</subfield>
<subfield code="c">k</subfield>
</datafield>
</datafield>
</record>
</collection>

Which will result later in the following error:
:23: parser error : Opening and ending tag mismatch: record line 6 and datafield
</datafield>
            ^
:24: parser error : Opening and ending tag mismatch: collection line 2 and record
</record>
         ^
:25: parser error : Extra content at the end of the document
</collection>

Test plan:
You can test it along with bug 19289 and confirm that it fixes the problem
raised on bug 19289 comment 30

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2018-04-11 16:45:19 -03:00

79 lines
2.3 KiB
Perl

#!/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 => 3;
use t::lib::Mocks;
use XML::Simple;
use C4::Biblio qw/TransformHtmlToXml/;
sub run_tests {
my ($marc_flavour) = @_;
t::lib::Mocks::mock_preference('marcflavour', $marc_flavour);
my ( $tags, $subfields );
if ( $marc_flavour eq 'UNIMARC' ) {
$tags= [ '001', '600', '200', '200', '400' ];
$subfields = [ '', 'a', 'a', 'c', 'a' ];
} else {
$tags= [ '001', '100', '245', '245', '400' ];
$subfields = [ '', 'a', 'a', 'c', 'a' ];
}
my $values = [ '12345', 'author', 'title', 'resp', '' ];
my $ind = [ ' ', '00', ' 9', ' ', ' ' ];
my $xml = TransformHtmlToXml( $tags, $subfields, $values, $ind, undef, $marc_flavour );
my $xmlh = XML::Simple->new->XMLin( $xml );
# check number of controlfields
is( ref $xmlh->{record}->{controlfield}, 'HASH', 'One controlfield' );
# check datafields
my $cnt = @{$xmlh->{record}->{datafield}};
if ( $marc_flavour eq 'UNIMARC' ) {
is( $cnt, 3, 'Three datafields' ); # 100$a is automatically created
} else {
is( $cnt, 2, 'Two datafields' );
}
# check value of 245c
is( $xmlh->{record}->{datafield}->[1]->{subfield}->[1]->{content}, 'resp', 'Check value' );
# check second indicator of 245
is( $xmlh->{record}->{datafield}->[1]->{ind2}, '9', 'Check indicator' );
}
subtest "->TransformHtmlToXml (MARC21) tests" => sub {
plan tests => 4;
run_tests('MARC21');
};
subtest "->TransformHtmlToXml (UNIMARC) tests" => sub {
plan tests => 4;
run_tests('UNIMARC');
};
subtest "->TransformHtmlToXml (NORMARC) tests" => sub {
plan tests => 4;
run_tests('NORMARC');
};