From 937ff3d98451d23d8f58053acec6d305c1958df3 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 Signed-off-by: Wainui Witika-Park --- Koha/Authority.pm | 45 ++++++++++++++++- t/db_dependent/Koha/Authorities.t | 81 ++++++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/Koha/Authority.pm b/Koha/Authority.pm index cec6c662f4..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} ) { @@ -114,6 +113,48 @@ sub controlled_indicators { }); } +=head3 get_identifiers + + my $identifiers = $author->get_identifiers; + +Return a list of identifiers of the authors which are in 024$2$a + +=cut + +sub get_identifiers { + my ( $self, $params ) = @_; + + my $record = $self->record; + + my @identifiers; + for my $field ( $record->field('024') ) { + my $sf_2 = $field->subfield('2'); + my $sf_a = $field->subfield('a'); + next unless $sf_2 && $sf_a; + push @identifiers, {source => $sf_2, number => $sf_a, }; + } + + 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 04ad663e28..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 => 7; +use Test::More tests => 9; use MARC::Field; use MARC::File::XML; use MARC::Record; @@ -242,4 +242,83 @@ sub few_marc_records { return [ $marc ]; } +subtest 'get_identifiers' => sub { + plan tests => 1; + + 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); + is_deeply( + $authority->get_identifiers, + [ + { + source => 'orcid', + number => '0000-0002-1234-5678', + }, + { + source => 'scopus', + number => '01234567890', + } + ] + ); +}; + +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