From 6e23fb4e1dc9a6c3f07099352c54191526e75891 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 29 Sep 2020 10:13:59 -0300 Subject: [PATCH] Bug 26651: Add ->reset_messages This patch was orginally part of bug 26555, and introduces a method for cleaning the accumulated messages. The idea is that you might run: $patron->some ->series ->of ->method ->calls; if ( $patron->messages ) { ... } and for next calls, want the messages cleared. So you can call $patron->reset_messages; To test: 1. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => SUCCESS: Tests pass 2. Apply this patch 3. Repeat 1 4. Sign off :-D Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- Koha/Object.pm | 15 +++++++++++++++ t/db_dependent/Koha/Object.t | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 57b1429b890..4c902a38abe 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -379,6 +379,21 @@ sub add_message { return $self; } +=head3 $object->reset_messages + + $object->reset_messages; + +Reset the current object's messages. + +=cut + +sub reset_messages { + my ( $self ) = @_; + + $self->{_messages} = []; + return $self; +} + =head3 $object->TO_JSON Returns an unblessed representation of the object, suitable for JSON output. diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index b78283ce250..e96b34b05a5 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -868,9 +868,9 @@ subtest 'set_or_blank' => sub { $schema->storage->txn_rollback; }; -subtest 'messages() and add_message() tests' => sub { +subtest 'messages(), add_message() and reset_messages() tests' => sub { - plan tests => 7; + plan tests => 10; $schema->storage->txn_begin; @@ -896,5 +896,15 @@ subtest 'messages() and add_message() tests' => sub { isnt( $patron->messages, undef, '->messages initializes the array if required' ); is( scalar @{ $patron->messages }, 0, '->messages returns an empty arrayref' ); + is( $messages[1]->message, 'message_2', 'Right message recorded' ); + + $patron->reset_messages; + @messages = @{ $patron->messages }; + is( scalar @messages, 0, 'No messages are returned' ); + + $patron->reset_messages; + @messages = @{ $patron->messages }; + is( scalar @messages, 0, 'No messages are returned, things ok when calling on already empty list' ); + $schema->storage->txn_rollback; }; -- 2.28.0