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 <martin.renvoize@ptfs-europe.com>
(cherry picked from commit 40115a2c8cba3e081ffd0710899ef4556a3bbb54)
Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Wainui Witika-Park <wainuiwitikapark@catalyst.net.nz>
This commit is contained in:
Martin Renvoize 2022-12-06 17:07:02 -03:00 committed by Wainui Witika-Park
parent 40be3d3ac4
commit b98bdc5a3a
2 changed files with 123 additions and 3 deletions

View File

@ -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

View File

@ -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;