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 <lucas@bywatersolutions.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
(cherry picked from commit c596f450d9)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
(cherry picked from commit 9266846b91)

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2020-12-10 15:10:10 +01:00 committed by Andrew Fuerste-Henry
parent 427a7cc555
commit 1be8ada8a7
3 changed files with 21 additions and 3 deletions

View File

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

View File

@ -96,7 +96,12 @@
<div class="newsfooter">
Published on [% koha_new.timestamp | $KohaDates with_hours = 1 %]
[% IF ( (newsdisp == 'opac' || newsdisp == 'both') && koha_new.borrowernumber ) %]
by <span class="newsauthor_title">[% koha_new.author_title | html %] </span>[% koha_new.author_firstname | html %] [% koha_new.author_surname | html %]
[% IF news_item %]
[% SET author = koha_new.author %]
by <span class="newsauthor_title">[% author.title | html %] </span>[% author.firstname | html %] [% author.surname | html %]
[% ELSE %]
by <span class="newsauthor_title">[% koha_new.author_title | html %] </span>[% koha_new.author_firstname | html %] [% koha_new.author_surname | html %]
[% END %]
[% END %]
[% IF ( news_item ) %]
&bull; <a href="/cgi-bin/koha/opac-main.pl">Show all news</a>

View File

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