|
Lines 18-24
Link Here
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::NoWarnings; |
20 |
use Test::NoWarnings; |
| 21 |
use Test::More tests => 6; |
21 |
use Test::More tests => 7; |
| 22 |
use Test::Exception; |
22 |
use Test::Exception; |
| 23 |
use Test::MockModule; |
23 |
use Test::MockModule; |
| 24 |
use Test::Warn; |
24 |
use Test::Warn; |
|
Lines 28-33
use t::lib::Mocks;
Link Here
|
| 28 |
|
28 |
|
| 29 |
use C4::Biblio qw( AddBiblio ); |
29 |
use C4::Biblio qw( AddBiblio ); |
| 30 |
use Koha::Database; |
30 |
use Koha::Database; |
|
|
31 |
use MARC::Record; |
| 32 |
use MARC::Field; |
| 31 |
|
33 |
|
| 32 |
BEGIN { |
34 |
BEGIN { |
| 33 |
use_ok('Koha::Biblio::Metadatas'); |
35 |
use_ok('Koha::Biblio::Metadatas'); |
|
Lines 73-112
EOX
Link Here
|
| 73 |
|
75 |
|
| 74 |
lives_ok { $record->store } |
76 |
lives_ok { $record->store } |
| 75 |
'Valid MARCXML record stores successfully'; |
77 |
'Valid MARCXML record stores successfully'; |
|
|
78 |
is( |
| 79 |
$record->metadata_errors->search( { error_type => 'nonxml_stripped' } )->count, |
| 80 |
0, 'No nonxml_stripped error for clean record' |
| 81 |
); |
| 76 |
|
82 |
|
| 77 |
$schema->storage->txn_rollback; |
83 |
$schema->storage->txn_rollback; |
| 78 |
}; |
84 |
}; |
| 79 |
|
85 |
|
| 80 |
subtest 'Invalid MARCXML handling' => sub { |
86 |
subtest 'MARCXML with strippable non-XML characters' => sub { |
|
|
87 |
plan tests => 4; |
| 81 |
$schema->storage->txn_begin; |
88 |
$schema->storage->txn_begin; |
| 82 |
my $invalid_marcxml = <<'EOX'; |
89 |
|
| 83 |
<?xml version="1.0" encoding="UTF-8"?> |
90 |
# Build MARCXML containing a non-XML control character (chr(31) = Unit Separator) |
| 84 |
<record xmlns="http://www.loc.gov/MARC21/slim"> |
91 |
my $bad_title = 'Title with' . chr(31) . ' bad character'; |
| 85 |
<leader>00925nam a22002411a 4500</leader> |
92 |
my $clean_title = 'Title with bad character'; |
| 86 |
<controlfield tag="001">1234567</controlfield> |
93 |
|
| 87 |
<datafield tag="245" ind1="1" ind2="0"> |
94 |
# Generate the MARCXML string with the embedded bad character |
| 88 |
<subfield code="a">This string will break your record</subfield> |
95 |
my $marc_record = MARC::Record->new; |
| 89 |
</datafield> |
96 |
$marc_record->append_fields( MARC::Field->new( '245', '', '', 'a' => $bad_title ) ); |
| 90 |
<!-- Invalid XML structure --> |
97 |
my $marcxml_with_bad_char = $marc_record->as_xml_record('MARC21'); |
| 91 |
</record> |
|
|
| 92 |
EOX |
| 93 |
|
98 |
|
| 94 |
my $record = Koha::Biblio::Metadata->new( |
99 |
my $record = Koha::Biblio::Metadata->new( |
| 95 |
{ |
100 |
{ |
| 96 |
format => 'marcxml', |
101 |
format => 'marcxml', |
| 97 |
metadata => $invalid_marcxml, |
102 |
metadata => $marcxml_with_bad_char, |
| 98 |
biblionumber => $biblio->{biblionumber}, |
103 |
biblionumber => $biblio->{biblionumber}, |
| 99 |
schema => 'MARC21', |
104 |
schema => 'MARC21', |
| 100 |
} |
105 |
} |
| 101 |
); |
106 |
); |
| 102 |
|
107 |
|
| 103 |
throws_ok { $record->store } |
108 |
lives_ok { $record->store } 'MARCXML with strippable characters stores successfully'; |
| 104 |
'Koha::Exceptions::Metadata::Invalid', |
109 |
|
| 105 |
'Invalid MARCXML throws expected exception'; |
110 |
my @errors = $record->metadata_errors->search( { error_type => 'nonxml_stripped' } )->as_list; |
|
|
111 |
ok( scalar @errors, 'nonxml_stripped error(s) recorded after stripping' ); |
| 112 |
like( |
| 113 |
$errors[0]->message, |
| 114 |
qr/245\$a: invalid char value 31 \(U\+001F\) at position \d+/, |
| 115 |
'error message identifies field, char value, and position' |
| 116 |
); |
| 117 |
|
| 118 |
my $stored = Koha::Biblio::Metadatas->find( { biblionumber => $biblio->{biblionumber}, format => 'marcxml' } ); |
| 119 |
is( |
| 120 |
$stored->record->field('245')->subfield('a'), |
| 121 |
$clean_title, |
| 122 |
'Stored record has control character removed' |
| 123 |
); |
| 106 |
|
124 |
|
| 107 |
my $thrown = $@; |
|
|
| 108 |
ok( $thrown->decoding_error, 'Exception contains decoding error message' ); |
| 109 |
is( $thrown->biblionumber, $biblio->{biblionumber}, 'Exception contains correct biblionumber' ); |
| 110 |
$schema->storage->txn_rollback; |
125 |
$schema->storage->txn_rollback; |
| 111 |
}; |
126 |
}; |
| 112 |
|
127 |
|
| 113 |
- |
|
|