From f86419cea74a6ee4c08f6b6a9209ccccd88dc154 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 6 Dec 2022 17:07:02 -0300 Subject: [PATCH] Bug 35343: Add record accessor method to Koha::Authority Code lifted from bug 31794 to fix already backported bug 26611. Unit tests included. Signed-off-by: Martin Renvoize (cherry picked from commit 40115a2c8cba3e081ffd0710899ef4556a3bbb54) Signed-off-by: Matt Blenkinsop --- Koha/Authority.pm | 28 ++++++++++++++++------- t/db_dependent/Koha/Authorities.t | 38 ++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Koha/Authority.pm b/Koha/Authority.pm index ff45d20a3d..828d4c75b2 100644 --- a/Koha/Authority.pm +++ b/Koha/Authority.pm @@ -95,8 +95,7 @@ sub controlled_indicators { ? 'UNIMARCAUTH' : 'MARC21'; if( !$record ) { - $record = MARC::Record->new_from_xml( - $self->marcxml, 'UTF-8', $flavour ); + $record = $self->record; } if( !$self->{_report_tag} ) { @@ -125,12 +124,7 @@ Return a list of identifiers of the authors which are in 024$2$a sub get_identifiers { my ( $self, $params ) = @_; - my $flavour = - C4::Context->preference('marcflavour') eq 'UNIMARC' - ? 'UNIMARCAUTH' - : 'MARC21'; - my $record = - MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour ); + my $record = $self->record; my @identifiers; for my $field ( $record->field('024') ) { @@ -143,6 +137,24 @@ sub get_identifiers { return \@identifiers; } +=head3 record + + my $record = $authority->record() + +Return the MARC::Record for this authority + +=cut + +sub record { + my ( $self ) = @_; + + my $flavour = + C4::Context->preference('marcflavour') eq 'UNIMARC' + ? 'UNIMARCAUTH' + : 'MARC21'; + return MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour ); +} + =head2 Class Methods =head3 type diff --git a/t/db_dependent/Koha/Authorities.t b/t/db_dependent/Koha/Authorities.t index 5dbfaaa497..699ee1c34a 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 => 8; +use Test::More tests => 9; use MARC::Field; use MARC::File::XML; use MARC::Record; @@ -285,4 +285,40 @@ subtest 'get_identifiers' => sub { ); }; +subtest 'record tests' => sub { + plan tests => 3; + + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + my $record = MARC::Record->new(); + $record->add_fields( + [ + '100', ' ', ' ', + a => 'Lastname, Firstname', + b => 'b', + c => 'c', + i => 'i' + ], + [ + '024', '', '', + a => '0000-0002-1234-5678', + 2 => 'orcid', + 6 => 'https://orcid.org/0000-0002-1234-5678' + ], + [ + '024', '', '', + a => '01234567890', + 2 => 'scopus', + 6 => 'https://www.scopus.com/authid/detail.uri?authorId=01234567890' + ], + ); + my $authid = C4::AuthoritiesMarc::AddAuthority($record, undef, 'PERSO_NAME'); + my $authority = Koha::Authorities->find($authid); + my $authority_record = $authority->record; + is ($authority_record->field('100')->subfield('a'), 'Lastname, Firstname'); + my @fields_024 = $authority_record->field('024'); + is ($fields_024[0]->subfield('a'), '0000-0002-1234-5678'); + is ($fields_024[1]->subfield('a'), '01234567890'); + +}; + $schema->storage->txn_rollback; -- 2.39.5