|
Lines 36-41
BEGIN {
Link Here
|
| 36 |
my $schema = Koha::Database->new->schema; |
36 |
my $schema = Koha::Database->new->schema; |
| 37 |
my $builder = t::lib::TestBuilder->new; |
37 |
my $builder = t::lib::TestBuilder->new; |
| 38 |
|
38 |
|
|
|
39 |
subtest 'Testing store() method' => sub { |
| 40 |
plan tests => 4; |
| 41 |
|
| 42 |
$schema->storage->txn_begin; |
| 43 |
|
| 44 |
# Create a test bibliographic record |
| 45 |
my $biblio = $builder->build( |
| 46 |
{ |
| 47 |
source => 'Biblio', |
| 48 |
} |
| 49 |
); |
| 50 |
|
| 51 |
subtest 'Valid MARCXML storage' => sub { |
| 52 |
$schema->storage->txn_begin; |
| 53 |
|
| 54 |
my $valid_marcxml = <<'EOX'; |
| 55 |
<?xml version="1.0" encoding="UTF-8"?> |
| 56 |
<record xmlns="http://www.loc.gov/MARC21/slim"> |
| 57 |
<leader>00925nam a22002411a 4500</leader> |
| 58 |
<controlfield tag="001">1234567</controlfield> |
| 59 |
<datafield tag="245" ind1="1" ind2="0"> |
| 60 |
<subfield code="a">Test Title</subfield> |
| 61 |
</datafield> |
| 62 |
</record> |
| 63 |
EOX |
| 64 |
|
| 65 |
my $record = Koha::Biblio::Metadata->new( |
| 66 |
{ |
| 67 |
format => 'marcxml', |
| 68 |
metadata => $valid_marcxml, |
| 69 |
biblionumber => $biblio->{biblionumber}, |
| 70 |
schema => 'MARC21', |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
lives_ok { $record->store } |
| 75 |
'Valid MARCXML record stores successfully'; |
| 76 |
|
| 77 |
$schema->storage->txn_rollback; |
| 78 |
}; |
| 79 |
|
| 80 |
subtest 'Invalid MARCXML handling' => sub { |
| 81 |
$schema->storage->txn_begin; |
| 82 |
my $invalid_marcxml = <<'EOX'; |
| 83 |
<?xml version="1.0" encoding="UTF-8"?> |
| 84 |
<record xmlns="http://www.loc.gov/MARC21/slim"> |
| 85 |
<leader>00925nam a22002411a 4500</leader> |
| 86 |
<controlfield tag="001">1234567</controlfield> |
| 87 |
<datafield tag="245" ind1="1" ind2="0"> |
| 88 |
<subfield code="a">This string will break your record</subfield> |
| 89 |
</datafield> |
| 90 |
<!-- Invalid XML structure --> |
| 91 |
</record> |
| 92 |
EOX |
| 93 |
|
| 94 |
my $record = Koha::Biblio::Metadata->new( |
| 95 |
{ |
| 96 |
format => 'marcxml', |
| 97 |
metadata => $invalid_marcxml, |
| 98 |
biblionumber => $biblio->{biblionumber}, |
| 99 |
schema => 'MARC21', |
| 100 |
} |
| 101 |
); |
| 102 |
|
| 103 |
throws_ok { $record->store } |
| 104 |
'Koha::Exceptions::Metadata::Invalid', |
| 105 |
'Invalid MARCXML throws expected exception'; |
| 106 |
|
| 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; |
| 111 |
}; |
| 112 |
|
| 113 |
subtest 'Non-MARCXML format' => sub { |
| 114 |
$schema->storage->txn_begin; |
| 115 |
my $other_metadata = '{"title": "Test Title"}'; |
| 116 |
|
| 117 |
my $record = Koha::Biblio::Metadata->new( |
| 118 |
{ |
| 119 |
format => 'json', |
| 120 |
metadata => $other_metadata, |
| 121 |
biblionumber => $biblio->{biblionumber}, |
| 122 |
schema => 'LOCAL', |
| 123 |
} |
| 124 |
); |
| 125 |
|
| 126 |
lives_ok { $record->store } |
| 127 |
'Non-MARCXML record stores without validation'; |
| 128 |
$schema->storage->txn_rollback; |
| 129 |
}; |
| 130 |
|
| 131 |
subtest 'Empty MARCXML handling' => sub { |
| 132 |
$schema->storage->txn_begin; |
| 133 |
my $empty_record = Koha::Biblio::Metadata->new( |
| 134 |
{ |
| 135 |
format => 'marcxml', |
| 136 |
metadata => '', |
| 137 |
biblionumber => $biblio->{biblionumber}, |
| 138 |
schema => 'MARC21', |
| 139 |
} |
| 140 |
); |
| 141 |
|
| 142 |
throws_ok { $empty_record->store } |
| 143 |
'Koha::Exceptions::Metadata::Invalid', |
| 144 |
'Empty MARCXML throws expected exception'; |
| 145 |
$schema->storage->txn_rollback; |
| 146 |
}; |
| 147 |
}; |
| 148 |
|
| 39 |
subtest 'record() tests' => sub { |
149 |
subtest 'record() tests' => sub { |
| 40 |
|
150 |
|
| 41 |
plan tests => 11; |
151 |
plan tests => 11; |
|
Lines 124-130
subtest 'record_strip_nonxml() tests' => sub {
Link Here
|
| 124 |
|
234 |
|
| 125 |
$schema->storage->txn_begin; |
235 |
$schema->storage->txn_begin; |
| 126 |
|
236 |
|
| 127 |
my $title = 'Oranges and' . chr(31) . ' Peaches'; |
237 |
my $title = 'Oranges and Peaches'; |
| 128 |
|
238 |
|
| 129 |
# Create a valid record |
239 |
# Create a valid record |
| 130 |
my $record = MARC::Record->new(); |
240 |
my $record = MARC::Record->new(); |
|
Lines 133-139
subtest 'record_strip_nonxml() tests' => sub {
Link Here
|
| 133 |
my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' ); |
243 |
my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' ); |
| 134 |
|
244 |
|
| 135 |
my $metadata = Koha::Biblios->find($biblio_id)->metadata; |
245 |
my $metadata = Koha::Biblios->find($biblio_id)->metadata; |
| 136 |
my $record2 = $metadata->record_strip_nonxml; |
246 |
|
|
|
247 |
# Update the record in the database directly to include our error character |
| 248 |
my $bad_title = 'Oranges and' . chr(31) . ' Peaches'; |
| 249 |
$record = $metadata->record; |
| 250 |
$record->delete_fields( $record->field('245') ); |
| 251 |
$record->insert_fields_ordered( MARC::Field->new( '245', '', '', 'a' => $bad_title ) ); |
| 252 |
$metadata->_result->update( { metadata => $record->as_xml_record } ); |
| 253 |
$metadata->discard_changes; |
| 254 |
|
| 255 |
my $record2 = $metadata->record_strip_nonxml; |
| 137 |
|
256 |
|
| 138 |
is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' ); |
257 |
is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' ); |
| 139 |
is( |
258 |
is( |
|
Lines 157-164
subtest 'record_strip_nonxml() tests' => sub {
Link Here
|
| 157 |
"record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters" |
276 |
"record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters" |
| 158 |
); |
277 |
); |
| 159 |
|
278 |
|
| 160 |
my $builder = t::lib::TestBuilder->new; |
279 |
my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } ); |
| 161 |
my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } ); |
|
|
| 162 |
|
280 |
|
| 163 |
# Emptied the OpacHiddenItems pref |
281 |
# Emptied the OpacHiddenItems pref |
| 164 |
t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' ); |
282 |
t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' ); |
| 165 |
- |
|
|