From bb91b7a8a66dc642cbec5a32bf44d7f127973368 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 10 Dec 2020 15:10:10 +0100 Subject: [PATCH] Bug 24398: Fix 500 when viewing a single news item at the OPAC Test plan: 0. Set the NewsAuthorDisplay preference to 'OPAC' or 'Both OPAC and staff client.' 1. Open the OPAC main page and click on an individual news item Without this patch you get an ugly 500 Template process failed: undef error - The method Koha::NewsItem->author_title is not covered by tests! With this patch applied you see the news with the author's info Signed-off-by: Lucas Gass Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Nick Clemens Signed-off-by: Jonathan Druart (cherry picked from commit c596f450d96e04befe6d5335d8ad12679541d220) Signed-off-by: Fridolin Somers (cherry picked from commit 9266846b911f8f69b159ace559a6c119d366336f) Signed-off-by: Andrew Fuerste-Henry (cherry picked from commit 1be8ada8a7cf4bbeca81575d1fc0933bc3fe3368) Signed-off-by: Victor Grousset/tuxayo --- Koha/NewsItem.pm | 8 +++++++- koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt | 7 ++++++- t/db_dependent/Koha/News.t | 9 ++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Koha/NewsItem.pm b/Koha/NewsItem.pm index 9d0dd63f9a..45f64e94a3 100644 --- a/Koha/NewsItem.pm +++ b/Koha/NewsItem.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::Patrons; use base qw(Koha::Object); @@ -37,7 +38,12 @@ Koha::NewsItem represents a single piece of news from the opac_news table =cut -=head3 type +sub author { + my ( $self ) = @_; + return Koha::Patron->_new_from_dbic($self->_result->borrowernumber); +} + +=head3 _type =cut diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt index 970893b17a..b8955d7dd3 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -94,7 +94,12 @@
Published on [% koha_new.timestamp | $KohaDates with_hours = 1 %] [% IF ( (newsdisp == 'opac' || newsdisp == 'both') && koha_new.borrowernumber ) %] - by [% koha_new.author_title | html %] [% koha_new.author_firstname | html %] [% koha_new.author_surname | html %] + [% IF news_item %] + [% SET author = koha_new.author %] + by [% author.title | html %] [% author.firstname | html %] [% author.surname | html %] + [% ELSE %] + by [% koha_new.author_title | html %] [% koha_new.author_firstname | html %] [% koha_new.author_surname | html %] + [% END %] [% END %] [% IF ( news_item ) %] • Show all news diff --git a/t/db_dependent/Koha/News.t b/t/db_dependent/Koha/News.t index e1b56f5f3f..731a801a5c 100644 --- a/t/db_dependent/Koha/News.t +++ b/t/db_dependent/Koha/News.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Koha::NewsItem; use Koha::News; @@ -54,5 +54,12 @@ is( $retrieved_news_item_1->content, $new_news_item_1->content, 'The content met $retrieved_news_item_1->delete; is( Koha::News->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' ); +subtest '->author' => sub { + plan tests => 1; + + my $news_item = $builder->build_object({ class => 'Koha::News' }); + is( ref($news_item->author), 'Koha::Patron', 'Koha::NewsItem->author returns a Koha::Patron object' ); +}; + $schema->storage->txn_rollback; -- 2.39.5