Bug 15632: Koha::Patron::Messages - Be sure add and delete will log
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 20 Jan 2016 17:56:07 +0000 (17:56 +0000)
committerBrendan A Gallagher <brendan@bywatersolutions.com>
Thu, 3 Mar 2016 21:22:12 +0000 (21:22 +0000)
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>
Koha/Patron/Message.pm
t/db_dependent/Koha/Patron/Messages.t

index dfe3d82..501e6ee 100644 (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
index 9cab130..22bf694 100644 (file)
 
 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;