From 80f665ece4dec9a105883788c4e0e30889abeadf Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 17 Feb 2021 12:03:56 +0100 Subject: [PATCH] Bug 27714: Prevent Koha::NewsItem->author to explode If the author of a news has been removed, Koha::NewsItem->author must not explode DBIC result _type isn't of the _type Borrower at t/db_dependent/Koha/News.t line 68. Test plan: prove t/db_dependent/Koha/News.t must return green, and changes must be consistent Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart (cherry picked from commit 506b8f125b7c7dda1dc70b4963026ea975a8c655) Signed-off-by: Fridolin Somers (cherry picked from commit 2329f57f882fa493fb2943b3f242fbc6626ea61d) Signed-off-by: Andrew Fuerste-Henry --- Koha/NewsItem.pm | 6 ++++-- t/db_dependent/Koha/News.t | 11 +++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Koha/NewsItem.pm b/Koha/NewsItem.pm index d2fbcc05fe..75dc78723e 100644 --- a/Koha/NewsItem.pm +++ b/Koha/NewsItem.pm @@ -47,8 +47,10 @@ Return the Koha::Patron object for the patron who authored this news item =cut sub author { - my ( $self ) = @_; - return Koha::Patron->_new_from_dbic($self->_result->borrowernumber); + my ($self) = @_; + my $author_rs = $self->_result->borrowernumber; + return unless $author_rs; + return Koha::Patron->_new_from_dbic($author_rs); } =head3 _type diff --git a/t/db_dependent/Koha/News.t b/t/db_dependent/Koha/News.t index 731a801a5c..a37567a2b4 100644 --- a/t/db_dependent/Koha/News.t +++ b/t/db_dependent/Koha/News.t @@ -55,10 +55,17 @@ $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; + plan tests => 3; my $news_item = $builder->build_object({ class => 'Koha::News' }); - is( ref($news_item->author), 'Koha::Patron', 'Koha::NewsItem->author returns a Koha::Patron object' ); + my $author = $news_item->author; + is( ref($author), 'Koha::Patron', 'Koha::NewsItem->author returns a Koha::Patron object' ); + + $author->delete; + + $news_item = Koha::News->find($news_item->idnew); + is( ref($news_item), 'Koha::NewsItem', 'News are not deleted alongwith the author' ); + is( $news_item->author, undef, '->author returns undef is the author has been deleted' ); }; $schema->storage->txn_rollback; -- 2.39.5