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 <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
(cherry picked from commit 506b8f125b)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Jonathan Druart 2021-02-17 12:03:56 +01:00 committed by Fridolin Somers
parent abe0f0360c
commit 2329f57f88
2 changed files with 13 additions and 4 deletions

View File

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

View File

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