From 020f710e3100b56e30a70d1465e70bd671e3ed17 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Wed, 3 May 2023 15:31:05 -0300 Subject: [PATCH] Bug 32735: (follow-up) Make Koha::Objects::Record::Collections->print_collection work with Authorities and Biblios Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/Authority.pm | 19 ++++++++++++++++--- Koha/Biblio.pm | 14 ++++++++++++++ Koha/Objects/Record/Collections.pm | 9 ++++++--- t/db_dependent/Biblio.t | 16 +++++++++++++++- t/db_dependent/Koha/Authorities.t | 16 +++++++++++++++- 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/Koha/Authority.pm b/Koha/Authority.pm index 828d4c75b2..ee175c2244 100644 --- a/Koha/Authority.pm +++ b/Koha/Authority.pm @@ -148,11 +148,24 @@ Return the MARC::Record for this authority sub record { my ( $self ) = @_; - my $flavour = - C4::Context->preference('marcflavour') eq 'UNIMARC' + my $flavour = $self->record_schema; + return MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour ); +} + +=head3 record_schema + +my $schema = $biblio->record_schema(); + +Returns the record schema (MARC21 or UNIMARCAUTH). + +=cut + +sub record_schema { + my ( $self ) = @_; + + return C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'UNIMARCAUTH' : 'MARC21'; - return MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour ); } =head2 Class Methods diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 82f4c0926b..50d60c8a69 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -105,6 +105,20 @@ sub record { return $self->metadata->record; } +=head3 record_schema + +my $schema = $biblio->record_schema(); + +Returns the record schema (MARC21, USMARC or UNIMARC). + +=cut + +sub record_schema { + my ( $self ) = @_; + + return $self->metadata->schema // C4::Context->preference("marcflavour"); +} + =head3 orders my $orders = $biblio->orders(); diff --git a/Koha/Objects/Record/Collections.pm b/Koha/Objects/Record/Collections.pm index 7874abae7f..f886c8d1d9 100644 --- a/Koha/Objects/Record/Collections.pm +++ b/Koha/Objects/Record/Collections.pm @@ -84,10 +84,13 @@ sub print_collection { $end = MARC::File::XML::footer(); } while ( my $element = $self->next ) { - my $metadata = $element->metadata; - MARC::File::XML->default_record_format( $metadata->schema // C4::Context->preference("marcflavour") ) + my $schema = C4::Context->preference("marcflavour"); + if ($element->can('record_schema')) { + $schema = $element->record_schema; + } + MARC::File::XML->default_record_format( $schema ) if $format eq 'marcxml'; - push @parts, $serializers{$format}->( $metadata->record ); + push @parts, $serializers{$format}->( $element->record ); } return ( defined $start ? $start : '' ) diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index 10df3a8856..99310eb6a2 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 16; +use Test::More tests => 17; use Test::MockModule; use Test::Warn; use List::MoreUtils qw( uniq ); @@ -877,6 +877,20 @@ subtest 'record test' => sub { $biblio->metadata->record->as_formatted ); }; +subtest 'record_schema test' => sub { + plan tests => 1; + + my $marc_record = MARC::Record->new; + $marc_record->append_fields( create_isbn_field( '0590353403', 'MARC21' ) ); + + my ($biblionumber) = C4::Biblio::AddBiblio( $marc_record, '' ); + + my $biblio = Koha::Biblios->find($biblionumber); + + is( $biblio->record_schema, + $biblio->metadata->schema ); +}; + # Cleanup diff --git a/t/db_dependent/Koha/Authorities.t b/t/db_dependent/Koha/Authorities.t index 699ee1c34a..422307c7b9 100755 --- a/t/db_dependent/Koha/Authorities.t +++ b/t/db_dependent/Koha/Authorities.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 10; use MARC::Field; use MARC::File::XML; use MARC::Record; @@ -321,4 +321,18 @@ subtest 'record tests' => sub { }; +subtest 'record_schema tests' => sub { + plan tests => 2; + + my $authority = $builder->build_object({class => 'Koha::Authorities'}); + + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + + is($authority->record_schema, 'MARC21'); + + t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' ); + + is($authority->record_schema, 'UNIMARCAUTH'); +}; + $schema->storage->txn_rollback; -- 2.39.5