From bb69bbc013433529c32bf2455c154f0fccf6b3bd Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 21 Mar 2018 16:52:10 +0100 Subject: [PATCH] Bug 20310: Introduce Koha::Biblio->host_record This is a more generic method that allows you to get to the host record as defined in MARC21 field 773$w. Test plan: Run t/db_dependent/Koha/Biblio/host_record.t Signed-off-by: Marcel de Rooy Signed-off-by: Maksim Sen Signed-off-by: Hugo Agud --- Koha/Biblio.pm | 32 ++++++++++++++ t/db_dependent/Koha/Biblio/host_record.t | 75 ++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 t/db_dependent/Koha/Biblio/host_record.t diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index b535649b25..8783cdec80 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -469,6 +469,38 @@ sub has_items_waiting_or_intransit { return 0; } +=head3 host_record + + $host = $biblio->host_record; + ( $host, $relatedparts ) = $biblio->host_record; + + Returns host biblio record from MARC21 773 (undef if no 773 present). + Currently, looks only at the first 773 field. + The optional parameter no_items triggers a check if $biblio has items. + If there are, the sub returns undef. + Called in list context, it also returns 773$g (related parts). + +=cut + +sub host_record { + my ($self, $params) = @_; + my $no_items = $params->{no_items}; + + return if C4::Context->preference('marcflavour') eq 'UNIMARC'; # TODO + return if $params->{no_items} && $self->items->count > 0; + my $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $self->biblionumber }); + my $hostfld = $marc->field('773'); + return if !$hostfld; + + # Extract record control number + my $rcn; + if( $hostfld->subfield('w') =~ /\)\s*(\d+)/ ) { + $rcn= $1; + } + my $host = $rcn ? Koha::Biblios->find($rcn) : undef; + return wantarray ? ( $host, $hostfld->subfield('g') ) : $host; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Biblio/host_record.t b/t/db_dependent/Koha/Biblio/host_record.t new file mode 100644 index 0000000000..ab21a0327b --- /dev/null +++ b/t/db_dependent/Koha/Biblio/host_record.t @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Data::Dumper qw/Dumper/; +use MARC::Field; +use MARC::Record; +use Test::MockModule; + +use t::lib::TestBuilder; +use t::lib::Mocks; +use Koha::Database; +use Koha::Biblios; +use C4::Biblio; + + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +our $builder = t::lib::TestBuilder->new; +our $marc; + +subtest 'host_record' => sub { + plan tests => 9; + + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + my $mod = Test::MockModule->new('C4::Biblio'); + $mod->mock( 'GetMarcBiblio', sub { return $marc; } ); + + my $bib1 = $builder->build_object({ class => 'Koha::Biblios' }); + my $bib2 = $builder->build_object({ class => 'Koha::Biblios' }); + $marc = MARC::Record->new; + + is( $bib1->host_record, undef, 'Empty MARC record' ); + $marc->append_fields( + MARC::Field->new( '773', '', '', g => 'relpart', w => 'xyz' ), + ); + is( $bib1->host_record, undef, 'No control number found' ); + $marc->field('773')->update( w => 'xyz)' . ($bib2->biblionumber + 1) ); + is( $bib1->host_record, undef, 'Control number does not exist' ); + # Make it work now + $marc->field('773')->update( w => 'xyz)' . $bib2->biblionumber ); + my $host = $bib1->host_record; + is( ref( $host ), 'Koha::Biblio', 'Correct object returned' ); + is( $host->biblionumber, $bib2->biblionumber, 'Check biblionumber' ); + # Add second 773 + $marc->append_fields( MARC::Field->new( '773', '', '', w => 'second' ) ); + $host = $bib1->host_record; + is( $host->biblionumber, $bib2->biblionumber, 'Two 773s, record still found' ); + # Test no_items flag + $host = $bib1->host_record({ no_items => 1 }); + is( $host->biblionumber, $bib2->biblionumber, 'Record found with no_items' ); + $builder->build({ source => 'Item', value => { biblionumber => $bib1->biblionumber } }); + is( $bib1->host_record({ no_items => 1 }), undef, 'Record not found with no_items flag after adding one item' ); + # Test list context + my @temp = $bib1->host_record; + is( $temp[1], 'relpart', 'Return $g in list context' ); +}; + +$schema->storage->txn_rollback(); -- 2.11.0