|
Lines 17-23
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 5; |
20 |
use Test::More tests => 6; |
| 21 |
use Test::Exception; |
21 |
use Test::Exception; |
| 22 |
use Test::MockModule; |
22 |
use Test::MockModule; |
| 23 |
use Test::Warn; |
23 |
use Test::Warn; |
|
Lines 35-40
BEGIN {
Link Here
|
| 35 |
my $schema = Koha::Database->new->schema; |
35 |
my $schema = Koha::Database->new->schema; |
| 36 |
my $builder = t::lib::TestBuilder->new; |
36 |
my $builder = t::lib::TestBuilder->new; |
| 37 |
|
37 |
|
|
|
38 |
subtest 'Testing store() method' => sub { |
| 39 |
plan tests => 4; |
| 40 |
|
| 41 |
$schema->storage->txn_begin; |
| 42 |
|
| 43 |
# Create a test bibliographic record |
| 44 |
my $biblio = $builder->build( |
| 45 |
{ |
| 46 |
source => 'Biblio', |
| 47 |
} |
| 48 |
); |
| 49 |
|
| 50 |
subtest 'Valid MARCXML storage' => sub { |
| 51 |
$schema->storage->txn_begin; |
| 52 |
|
| 53 |
my $valid_marcxml = <<'EOX'; |
| 54 |
<?xml version="1.0" encoding="UTF-8"?> |
| 55 |
<record xmlns="http://www.loc.gov/MARC21/slim"> |
| 56 |
<leader>00925nam a22002411a 4500</leader> |
| 57 |
<controlfield tag="001">1234567</controlfield> |
| 58 |
<datafield tag="245" ind1="1" ind2="0"> |
| 59 |
<subfield code="a">Test Title</subfield> |
| 60 |
</datafield> |
| 61 |
</record> |
| 62 |
EOX |
| 63 |
|
| 64 |
my $record = Koha::Biblio::Metadata->new( |
| 65 |
{ |
| 66 |
format => 'marcxml', |
| 67 |
metadata => $valid_marcxml, |
| 68 |
biblionumber => $biblio->{biblionumber}, |
| 69 |
schema => 'MARC21', |
| 70 |
} |
| 71 |
); |
| 72 |
|
| 73 |
lives_ok { $record->store } |
| 74 |
'Valid MARCXML record stores successfully'; |
| 75 |
|
| 76 |
$schema->storage->txn_rollback; |
| 77 |
}; |
| 78 |
|
| 79 |
subtest 'Invalid MARCXML handling' => sub { |
| 80 |
$schema->storage->txn_begin; |
| 81 |
my $invalid_marcxml = <<'EOX'; |
| 82 |
<?xml version="1.0" encoding="UTF-8"?> |
| 83 |
<record xmlns="http://www.loc.gov/MARC21/slim"> |
| 84 |
<leader>00925nam a22002411a 4500</leader> |
| 85 |
<controlfield tag="001">1234567</controlfield> |
| 86 |
<datafield tag="245" ind1="1" ind2="0"> |
| 87 |
<subfield code="a">This string will break your record</subfield> |
| 88 |
</datafield> |
| 89 |
<!-- Invalid XML structure --> |
| 90 |
</record> |
| 91 |
EOX |
| 92 |
|
| 93 |
my $record = Koha::Biblio::Metadata->new( |
| 94 |
{ |
| 95 |
format => 'marcxml', |
| 96 |
metadata => $invalid_marcxml, |
| 97 |
biblionumber => $biblio->{biblionumber}, |
| 98 |
schema => 'MARC21', |
| 99 |
} |
| 100 |
); |
| 101 |
|
| 102 |
throws_ok { $record->store } |
| 103 |
'Koha::Exceptions::Metadata::Invalid', |
| 104 |
'Invalid MARCXML throws expected exception'; |
| 105 |
|
| 106 |
my $thrown = $@; |
| 107 |
ok( $thrown->decoding_error, 'Exception contains decoding error message' ); |
| 108 |
is( $thrown->biblionumber, $biblio->{biblionumber}, 'Exception contains correct biblionumber' ); |
| 109 |
$schema->storage->txn_rollback; |
| 110 |
}; |
| 111 |
|
| 112 |
subtest 'Non-MARCXML format' => sub { |
| 113 |
$schema->storage->txn_begin; |
| 114 |
my $other_metadata = '{"title": "Test Title"}'; |
| 115 |
|
| 116 |
my $record = Koha::Biblio::Metadata->new( |
| 117 |
{ |
| 118 |
format => 'json', |
| 119 |
metadata => $other_metadata, |
| 120 |
biblionumber => $biblio->{biblionumber}, |
| 121 |
schema => 'LOCAL', |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
lives_ok { $record->store } |
| 126 |
'Non-MARCXML record stores without validation'; |
| 127 |
$schema->storage->txn_rollback; |
| 128 |
}; |
| 129 |
|
| 130 |
subtest 'Empty MARCXML handling' => sub { |
| 131 |
$schema->storage->txn_begin; |
| 132 |
my $empty_record = Koha::Biblio::Metadata->new( |
| 133 |
{ |
| 134 |
format => 'marcxml', |
| 135 |
metadata => '', |
| 136 |
biblionumber => $biblio->{biblionumber}, |
| 137 |
schema => 'MARC21', |
| 138 |
} |
| 139 |
); |
| 140 |
|
| 141 |
throws_ok { $empty_record->store } |
| 142 |
'Koha::Exceptions::Metadata::Invalid', |
| 143 |
'Empty MARCXML throws expected exception'; |
| 144 |
$schema->storage->txn_rollback; |
| 145 |
}; |
| 146 |
}; |
| 147 |
|
| 38 |
subtest 'record() tests' => sub { |
148 |
subtest 'record() tests' => sub { |
| 39 |
|
149 |
|
| 40 |
plan tests => 11; |
150 |
plan tests => 11; |
|
Lines 117-123
subtest 'record_strip_nonxml() tests' => sub {
Link Here
|
| 117 |
|
227 |
|
| 118 |
$schema->storage->txn_begin; |
228 |
$schema->storage->txn_begin; |
| 119 |
|
229 |
|
| 120 |
my $title = 'Oranges and' . chr(31) . ' Peaches'; |
230 |
my $title = 'Oranges and Peaches'; |
| 121 |
|
231 |
|
| 122 |
# Create a valid record |
232 |
# Create a valid record |
| 123 |
my $record = MARC::Record->new(); |
233 |
my $record = MARC::Record->new(); |
|
Lines 126-132
subtest 'record_strip_nonxml() tests' => sub {
Link Here
|
| 126 |
my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' ); |
236 |
my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' ); |
| 127 |
|
237 |
|
| 128 |
my $metadata = Koha::Biblios->find($biblio_id)->metadata; |
238 |
my $metadata = Koha::Biblios->find($biblio_id)->metadata; |
| 129 |
my $record2 = $metadata->record_strip_nonxml; |
239 |
|
|
|
240 |
# Update the record in the database directly to include our error character |
| 241 |
my $bad_title = 'Oranges and' . chr(31) . ' Peaches'; |
| 242 |
$record = $metadata->record; |
| 243 |
$record->delete_fields( $record->field('245') ); |
| 244 |
$record->insert_fields_ordered( MARC::Field->new( '245', '', '', 'a' => $bad_title ) ); |
| 245 |
$metadata->_result->update( { metadata => $record->as_xml_record } ); |
| 246 |
$metadata->discard_changes; |
| 247 |
|
| 248 |
my $record2 = $metadata->record_strip_nonxml; |
| 130 |
|
249 |
|
| 131 |
is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' ); |
250 |
is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' ); |
| 132 |
is( |
251 |
is( |
|
Lines 150-157
subtest 'record_strip_nonxml() tests' => sub {
Link Here
|
| 150 |
"record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters" |
269 |
"record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters" |
| 151 |
); |
270 |
); |
| 152 |
|
271 |
|
| 153 |
my $builder = t::lib::TestBuilder->new; |
272 |
my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } ); |
| 154 |
my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } ); |
|
|
| 155 |
|
273 |
|
| 156 |
# Emptied the OpacHiddenItems pref |
274 |
# Emptied the OpacHiddenItems pref |
| 157 |
t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' ); |
275 |
t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' ); |
| 158 |
- |
|
|