From 7c77b7c95f19467ee40a3dc97a2f2c9f005ead12 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 5 Oct 2021 13:57:04 +0200 Subject: [PATCH] Bug 29172: Make CustomCoverImagesURL compatible with control fields Bug 22445 added CustomCoverImagesURL that deals with MARC fields, but control fields were not supported. Test plan: Turn CustomCoverImages on Fill CustomCoverImagesURL with https://covers.openlibrary.org/b/isbn/{006}-M.jpg Add an isbn in 006 (0596001738 will work) Go to the detail page of the bibliographic record and confirm that you see a cover image. Signed-off-by: David Nind Signed-off-by: Katrin Fischer Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall (cherry picked from commit 931ef856f9bf1ed1bdba828a66af5e099b792c3c) Signed-off-by: Fridolin Somers --- Koha/Biblio.pm | 10 ++++++++-- t/db_dependent/Koha/Biblios.t | 9 ++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index ba670608c6..00146e6c18 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -770,12 +770,18 @@ sub custom_cover_image_url { $url =~ s|{issn}|$issn|g; } - my $re = qr|{(?\d{3})\$(?.)}|; + my $re = qr|{(?\d{3})(\$(?.))?}|; if ( $url =~ $re ) { my $field = $+{field}; my $subfield = $+{subfield}; my $marc_record = $self->metadata->record; - my $value = $marc_record->subfield($field, $subfield); + my $value; + if ( $subfield ) { + $value = $marc_record->subfield( $field, $subfield ); + } else { + my $controlfield = $marc_record->field($field); + $value = $controlfield->data() if $controlfield; + } return unless $value; $url =~ s|$re|$value|; } diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t index 82229f1f35..b3d10c52dd 100755 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -189,12 +189,13 @@ subtest 'can_be_transferred' => sub { }; subtest 'custom_cover_image_url' => sub { - plan tests => 4; + plan tests => 6; t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' ); my $isbn = '0553573403 | 9780553573404 (pbk.).png'; my $issn = 'my_issn'; + my $cf_value = 'from_control_field'; my $marc_record = MARC::Record->new; my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, ''); @@ -217,6 +218,12 @@ subtest 'custom_cover_image_url' => sub { $biblio->biblioitem->isbn('')->store; is( $biblio->custom_cover_image_url, undef, "Don't generate the url if the biblio does not have the value needed to generate it" ); + t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{001}.png' ); + is( $biblio->custom_cover_image_url, undef, 'Record does not have 001' ); + $marc_record->append_fields(MARC::Field->new('001', $cf_value)); + C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber ); + $biblio = Koha::Biblios->find( $biblionumber ); + is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/$cf_value.png", 'URL generated using 001' ); }; $schema->storage->txn_rollback; -- 2.39.5