View | Details | Raw Unified | Return to bug 33270
Collapse All | Expand All

(-)a/Koha/Biblio/Metadata.pm (+37 lines)
Lines 21-26 use MARC::File::XML; Link Here
21
use Scalar::Util qw( blessed );
21
use Scalar::Util qw( blessed );
22
22
23
use C4::Biblio qw( GetMarcFromKohaField );
23
use C4::Biblio qw( GetMarcFromKohaField );
24
use C4::Charset qw( StripNonXmlChars );
24
use C4::Items qw( GetMarcItem );
25
use C4::Items qw( GetMarcItem );
25
use Koha::Database;
26
use Koha::Database;
26
use Koha::Exceptions::Metadata;
27
use Koha::Exceptions::Metadata;
Lines 133-138 sub record { Link Here
133
    return $record;
134
    return $record;
134
}
135
}
135
136
137
=head3 record_strip_nonxml
138
139
my $record = $metadata->record_strip_nonxml;
140
141
This subroutine is intended for cases where we encounter a record that cannot be parsed, but want
142
to make a good effort to present the record (for harvesting, deletion, editing) rather than throwing
143
an exception
144
145
Will return undef if the record cannot be built
146
147
=cut
148
149
sub record_strip_nonxml {
150
151
    my ($self, $params) = @_;
152
153
    my $record;
154
    my $marcxml_error;
155
156
    eval {
157
        $record = MARC::Record->new_from_xml(
158
            StripNonXmlChars( $self->metadata ), 'UTF-8',
159
            $self->schema
160
        );
161
    };
162
    if( $@ ){
163
        $marcxml_error = $@;
164
        chomp $marcxml_error;
165
        warn $marcxml_error;
166
        return;
167
    }
168
169
    $params->{record} = $record;
170
    return $self->record( $params );
171
}
172
136
=head2 Internal methods
173
=head2 Internal methods
137
174
138
=head3 _embed_items
175
=head3 _embed_items
(-)a/t/db_dependent/Koha/Biblio/Metadata.t (-2 / +56 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 3;
20
use Test::More tests => 4;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 89-94 subtest 'record() tests' => sub { Link Here
89
    $schema->storage->txn_rollback;
89
    $schema->storage->txn_rollback;
90
};
90
};
91
91
92
subtest 'record_strip_nonxml() tests' => sub {
93
94
    plan tests => 5;
95
96
    $schema->storage->txn_begin;
97
98
    my $title = 'Oranges and' . chr(31) . ' Peaches';
99
100
    # Create a valid record
101
    my $record = MARC::Record->new();
102
    my $field  = MARC::Field->new( '245', '', '', 'a' => $title );
103
    $record->append_fields($field);
104
    my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
105
106
    my $metadata = Koha::Biblios->find($biblio_id)->metadata;
107
    my $record2  = $metadata->record_strip_nonxml;
108
109
    is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
110
    is(
111
        $record2->field('245')->subfield("a"),
112
        "Oranges and Peaches", 'Title in 245$a matches title with control character removed'
113
    );
114
115
    my $bad_data = $builder->build_object(
116
        {
117
            class => 'Koha::Biblio::Metadatas',
118
            value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
119
        }
120
    );
121
122
    warning_like { $record2 = $bad_data->record_strip_nonxml; }
123
    qr/parser error : Start tag expected, '<' not found/,
124
        'Warning thrown explicitly';
125
126
    is(
127
        $record2, undef,
128
        "record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters"
129
    );
130
131
    my $builder = t::lib::TestBuilder->new;
132
    my $item    = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } );
133
134
    # Emptied the OpacHiddenItems pref
135
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
136
    my ($itemfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber');
137
138
    $record2 = $metadata->record_strip_nonxml( { embed_items => 1 } );
139
140
    my @items = $record2->field($itemfield);
141
142
    is( scalar @items, 1, "We got back our item" );
143
144
    $schema->storage->txn_rollback;
145
};
146
92
subtest '_embed_items' => sub {
147
subtest '_embed_items' => sub {
93
    plan tests => 10;
148
    plan tests => 10;
94
149
95
- 

Return to bug 33270