From fd8c2ef4543ea64b6addd95133ee50424de0e9e0 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 30 Jul 2020 10:41:45 +0200 Subject: [PATCH] Bug 25913: Prevent get_coins to crash if record does not have title If a bibliographic record does not have a title, get_coins will crash with Can't call method "as_string" on an undefined value at /kohadevbox/koha/Koha/Biblio.pm line 645 Koha::Biblio::get_coins('Koha::Biblio=HASH(0x5558f91bb740)') called at /kohadevbox/koha/catalogue/ISBDdetail.pl line 144 We can handle that situation easily by checking the existence of the title field. Test plan: 1. Create a record without 245 2. Enable COinSinOpac 4. Go to the ISBD detail view => It must not fail with this patch applied --- Koha/Biblio.pm | 3 ++- t/db_dependent/Koha/Biblio.t | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 830b9c4d73..f430f4135a 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -642,7 +642,8 @@ sub get_coins { push @authors, $au; } } - $title = $record->field('245')->as_string('ab'); + $title = $record->field('245'); + $title &&= $title->as_string('ab'); if ($titletype eq 'a') { $pubyear = $record->field('008') || ''; $pubyear = substr($pubyear->data(), 7, 4) if $pubyear; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index b849fd3160..727c5b57de 100644 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -118,7 +118,7 @@ subtest 'items() tests' => sub { subtest 'get_coins and get_openurl' => sub { - plan tests => 3; + plan tests => 4; $schema->storage->txn_begin; @@ -127,13 +127,22 @@ subtest 'get_coins and get_openurl' => sub { title => 'Title 1', author => 'Author 1' }); - is( $biblio->get_coins, 'ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201', 'GetCOinsBiblio returned right metadata' ); + my $record = MARC::Record->new(); + $record->append_fields( MARC::Field->new('100','','','a' => 'Author 2'), MARC::Field->new('880','','','a' => 'Something') ); + my $biblionumber = C4::Biblio::AddBiblio($record, ''); + my $biblio_no_title = Koha::Biblios->find($biblionumber); + is( + $biblio_no_title->get_coins, + 'ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.au=Author%202', + 'GetCOinsBiblio returned right metadata if biblio does not have a title' + ); + t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/"); is( $biblio->get_openurl, -- 2.20.1