Bugzilla – Attachment 193996 Details for
Bug 35104
We should warn when attempting to save MARC records that contain characters invalid in XML
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 35104: Add unit tests for exception on store
f39065c.patch (text/plain), 5.98 KB, created by
Martin Renvoize (ashimema)
on 2026-02-26 13:45:43 UTC
(
hide
)
Description:
Bug 35104: Add unit tests for exception on store
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2026-02-26 13:45:43 UTC
Size:
5.98 KB
patch
obsolete
>From f39065c8747fa25f7766310b7e654cbc745b830e Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Tue, 21 Jan 2025 12:20:21 +0000 >Subject: [PATCH] Bug 35104: Add unit tests for exception on store > >We add a new set of unit tests for the exception we introduce here, and >we also update the existing tests to directly manipulate data in the >database rather then using AddBiblio which will now fail due to the >introduced exception on attempting to add bad data. > >Sponsored-by: OpenFifth >--- > t/db_dependent/Koha/Biblio/Metadata.t | 126 +++++++++++++++++++++++++- > 1 file changed, 122 insertions(+), 4 deletions(-) > >diff --git a/t/db_dependent/Koha/Biblio/Metadata.t b/t/db_dependent/Koha/Biblio/Metadata.t >index d2f3194635b..6e5af530cb7 100755 >--- a/t/db_dependent/Koha/Biblio/Metadata.t >+++ b/t/db_dependent/Koha/Biblio/Metadata.t >@@ -36,6 +36,116 @@ BEGIN { > my $schema = Koha::Database->new->schema; > my $builder = t::lib::TestBuilder->new; > >+subtest 'Testing store() method' => sub { >+ plan tests => 4; >+ >+ $schema->storage->txn_begin; >+ >+ # Create a test bibliographic record >+ my $biblio = $builder->build( >+ { >+ source => 'Biblio', >+ } >+ ); >+ >+ subtest 'Valid MARCXML storage' => sub { >+ $schema->storage->txn_begin; >+ >+ my $valid_marcxml = <<'EOX'; >+<?xml version="1.0" encoding="UTF-8"?> >+<record xmlns="http://www.loc.gov/MARC21/slim"> >+ <leader>00925nam a22002411a 4500</leader> >+ <controlfield tag="001">1234567</controlfield> >+ <datafield tag="245" ind1="1" ind2="0"> >+ <subfield code="a">Test Title</subfield> >+ </datafield> >+</record> >+EOX >+ >+ my $record = Koha::Biblio::Metadata->new( >+ { >+ format => 'marcxml', >+ metadata => $valid_marcxml, >+ biblionumber => $biblio->{biblionumber}, >+ schema => 'MARC21', >+ } >+ ); >+ >+ lives_ok { $record->store } >+ 'Valid MARCXML record stores successfully'; >+ >+ $schema->storage->txn_rollback; >+ }; >+ >+ subtest 'Invalid MARCXML handling' => sub { >+ $schema->storage->txn_begin; >+ my $invalid_marcxml = <<'EOX'; >+<?xml version="1.0" encoding="UTF-8"?> >+<record xmlns="http://www.loc.gov/MARC21/slim"> >+ <leader>00925nam a22002411a 4500</leader> >+ <controlfield tag="001">1234567</controlfield> >+ <datafield tag="245" ind1="1" ind2="0"> >+ <subfield code="a">This string will break your record</subfield> >+ </datafield> >+ <!-- Invalid XML structure --> >+</record> >+EOX >+ >+ my $record = Koha::Biblio::Metadata->new( >+ { >+ format => 'marcxml', >+ metadata => $invalid_marcxml, >+ biblionumber => $biblio->{biblionumber}, >+ schema => 'MARC21', >+ } >+ ); >+ >+ throws_ok { $record->store } >+ 'Koha::Exceptions::Metadata::Invalid', >+ 'Invalid MARCXML throws expected exception'; >+ >+ my $thrown = $@; >+ ok( $thrown->decoding_error, 'Exception contains decoding error message' ); >+ is( $thrown->biblionumber, $biblio->{biblionumber}, 'Exception contains correct biblionumber' ); >+ $schema->storage->txn_rollback; >+ }; >+ >+ subtest 'Non-MARCXML format' => sub { >+ $schema->storage->txn_begin; >+ my $other_metadata = '{"title": "Test Title"}'; >+ >+ my $record = Koha::Biblio::Metadata->new( >+ { >+ format => 'json', >+ metadata => $other_metadata, >+ biblionumber => $biblio->{biblionumber}, >+ schema => 'LOCAL', >+ } >+ ); >+ >+ lives_ok { $record->store } >+ 'Non-MARCXML record stores without validation'; >+ $schema->storage->txn_rollback; >+ }; >+ >+ subtest 'Empty MARCXML handling' => sub { >+ $schema->storage->txn_begin; >+ my $empty_record = Koha::Biblio::Metadata->new( >+ { >+ format => 'marcxml', >+ metadata => '', >+ biblionumber => $biblio->{biblionumber}, >+ schema => 'MARC21', >+ } >+ ); >+ >+ throws_ok { $empty_record->store } >+ 'Koha::Exceptions::Metadata::Invalid', >+ 'Empty MARCXML throws expected exception'; >+ $schema->storage->txn_rollback; >+ }; >+}; >+ > subtest 'record() tests' => sub { > > plan tests => 11; >@@ -124,7 +234,7 @@ subtest 'record_strip_nonxml() tests' => sub { > > $schema->storage->txn_begin; > >- my $title = 'Oranges and' . chr(31) . ' Peaches'; >+ my $title = 'Oranges and Peaches'; > > # Create a valid record > my $record = MARC::Record->new(); >@@ -133,7 +243,16 @@ subtest 'record_strip_nonxml() tests' => sub { > my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' ); > > my $metadata = Koha::Biblios->find($biblio_id)->metadata; >- my $record2 = $metadata->record_strip_nonxml; >+ >+ # Update the record in the database directly to include our error character >+ my $bad_title = 'Oranges and' . chr(31) . ' Peaches'; >+ $record = $metadata->record; >+ $record->delete_fields( $record->field('245') ); >+ $record->insert_fields_ordered( MARC::Field->new( '245', '', '', 'a' => $bad_title ) ); >+ $metadata->_result->update( { metadata => $record->as_xml_record } ); >+ $metadata->discard_changes; >+ >+ my $record2 = $metadata->record_strip_nonxml; > > is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' ); > is( >@@ -157,8 +276,7 @@ subtest 'record_strip_nonxml() tests' => sub { > "record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters" > ); > >- my $builder = t::lib::TestBuilder->new; >- my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } ); >+ my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } ); > > # Emptied the OpacHiddenItems pref > t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' ); >-- >2.53.0 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 35104
:
157625
|
157627
|
157628
|
157642
|
159615
|
159620
|
159621
|
159630
|
159810
|
159811
|
159812
|
159813
|
166431
|
173145
|
173146
|
176820
|
176821
|
176850
|
176851
|
176852
|
176860
|
193985
|
193986
|
193987
|
193988
|
193989
|
193990
|
193991
|
193992
|
193993
|
193994
|
193995
|
193996
|
193997
|
193998
|
193999
|
194000
|
194001
|
194004
|
194005
|
194006
|
194007
|
194008
|
194009
|
194010
|
194011
|
194012
|
194013