From 57e37ce8cf4e0313312815b447e63dcbf0e3f2ea Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 30 Sep 2020 09:42:18 -0300 Subject: [PATCH] Bug 26555: Make sure _messages is always initialized Signed-off-by: Tomas Cohen Arazi --- Koha/Object.pm | 4 ++++ t/db_dependent/Koha/Object.t | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 3e50181672..4c902a38ab 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -345,6 +345,10 @@ Returns the (probably non-fatal) messages that were recorded on the object. sub messages { my ( $self ) = @_; + + $self->{_messages} = [] + unless defined $self->{_messages}; + return $self->{_messages}; } diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index 42fb92fa17..69d20fcaac 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -870,7 +870,9 @@ subtest 'set_or_blank' => sub { subtest 'messages(), add_message() and reset_messages() tests' => sub { - plan tests => 8; + plan tests => 10; + + $schema->storage->txn_begin; my $patron = Koha::Patron->new; @@ -895,4 +897,13 @@ subtest 'messages(), add_message() and reset_messages() tests' => sub { $patron->reset_messages; @messages = @{ $patron->messages }; is( scalar @messages, 0, 'No messages are returned, things ok when calling on already empty list' ); + + my $patron_id = $builder->build_object({ class => 'Koha::Patrons' })->id; + # get a patron from the DB, ->new is not called, ->messages should initialize _messages as an empty arrayref + $patron = Koha::Patrons->find( $patron_id ); + + isnt( $patron->messages, undef, '->messages initializes the array if required' ); + is( scalar @{ $patron->messages }, 0, '->messages returns an empty arrayref' ); + + $schema->storage->txn_rollback; }; -- 2.28.0