From b842a08589427e628b609d779fc665613eb3c9ba Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 20 Jan 2016 17:56:07 +0000 Subject: [PATCH] Bug 15632: Koha::Patron::Messages - Be sure add and delete will log MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit If the pref BorrowersLog is on, the store and delete method should log into the action_logs table. Easy to do by overloading them. Signed-off-by: Marc Véron Signed-off-by: Brendan A Gallagher --- Koha/Patron/Message.pm | 35 +++++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Messages.t | 24 ++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Koha/Patron/Message.pm b/Koha/Patron/Message.pm index dfe3d82268..501e6ee5cd 100644 --- a/Koha/Patron/Message.pm +++ b/Koha/Patron/Message.pm @@ -19,6 +19,9 @@ use Modern::Perl; use Carp; +use C4::Context; +use C4::Log qw( logaction ); + use Koha::Database; use base qw(Koha::Object); @@ -33,6 +36,38 @@ Koha::Patron::Message - Koha Message Object class =cut +=head3 store + +=cut + +sub store { + my ($self) = @_; + + # This should be done at the DB level + return unless $self->borrowernumber + and $self->message + and $self->message_type + and $self->branchcode; + + C4::Log::logaction( "MEMBERS", "ADDCIRCMESSAGE", $self->borrowernumber, $self->message ) + if C4::Context->preference("BorrowersLog"); + + return $self->SUPER::store($self); +} + +=head3 delete + +=cut + +sub delete { + my ($self) = @_; + + C4::Log::logaction("MEMBERS", "DELCIRCMESSAGE", $self->borrowernumber, $self->message) + if C4::Context->preference("BorrowersLog"); + + return $self->SUPER::delete($self); +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Patron/Messages.t b/t/db_dependent/Koha/Patron/Messages.t index 9cab1307d2..22bf694393 100644 --- a/t/db_dependent/Koha/Patron/Messages.t +++ b/t/db_dependent/Koha/Patron/Messages.t @@ -19,8 +19,10 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 9; +use C4::Context; +use C4::Log; use Koha::Patron::Message; use Koha::Patron::Messages; use Koha::Database; @@ -33,7 +35,10 @@ $schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; my $library = $builder->build( { source => 'Branch' } ); my $patron = $builder->build( { source => 'Borrower', values => { branchcode => $library->{branchcode} } } ); +my $nb_of_logaction = get_nb_of_logactions(); my $nb_of_messages = Koha::Patron::Messages->search->count; + +C4::Context->set_preference('BorrowersLog', 0); my $new_message_1 = Koha::Patron::Message->new( { borrowernumber => $patron->{borrowernumber}, branchcode => $library->{branchcode}, @@ -41,6 +46,9 @@ my $new_message_1 = Koha::Patron::Message->new( message => 'my message 1', } )->store; +is( get_nb_of_logactions(), $nb_of_logaction, 'With BorrowersLog off, no new log should have been added' ); + +C4::Context->set_preference('BorrowersLog', 1); my $new_message_2 = Koha::Patron::Message->new( { borrowernumber => $patron->{borrowernumber}, branchcode => $library->{branchcode}, @@ -48,6 +56,7 @@ my $new_message_2 = Koha::Patron::Message->new( message => 'my message 2', } )->store; +is( get_nb_of_logactions(), $nb_of_logaction + 1, 'With BorrowersLog on, 1 new log should have been added when adding a new message' ); like( $new_message_1->message_id, qr|^\d+$|, 'Adding a new message should have set the message_id'); is( Koha::Patron::Messages->search->count, $nb_of_messages + 2, 'The 2 messages should have been added' ); @@ -55,9 +64,20 @@ is( Koha::Patron::Messages->search->count, $nb_of_messages + 2, 'The 2 messages my $retrieved_message_1 = Koha::Patron::Messages->find( $new_message_1->message_id ); is( $retrieved_message_1->message, $new_message_1->message, 'Find a message by id should return the correct message' ); +C4::Context->set_preference('BorrowersLog', 0); $retrieved_message_1->delete; -is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message' ); +is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message 1' ); +is( get_nb_of_logactions(), $nb_of_logaction + 1, 'With BorrowersLog off, no new log should have been added when deleting a new message' ); + +C4::Context->set_preference('BorrowersLog', 1); +$new_message_2->delete; +is( Koha::Patron::Messages->search->count, $nb_of_messages, 'Delete should have deleted the message 2' ); +is( get_nb_of_logactions(), $nb_of_logaction + 2, 'With BorrowersLog on, 1 new log should have been added when deleting a new message' ); $schema->storage->txn_rollback; +sub get_nb_of_logactions { + return scalar( @{ C4::Log::GetLogs( undef, undef, undef, ['MEMBERS'] ) } ); +} + 1; -- 2.39.5