From df5f54d5f22e1d85719175783f474a95c59b4aed 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 --- 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 100755 --- 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.20.1