Bug 15632: Koha::Patron::Messages - Be sure add and delete will log

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 <veron@veron.ch>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2016-01-20 17:56:07 +00:00 committed by Brendan A Gallagher
parent dd31253441
commit b842a08589
2 changed files with 57 additions and 2 deletions

View file

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

View file

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