View | Details | Raw Unified | Return to bug 15632
Collapse All | Expand All

(-)a/Koha/Patron/Message.pm (+35 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use Carp;
20
use Carp;
21
21
22
use C4::Context;
23
use C4::Log qw( logaction );
24
22
use Koha::Database;
25
use Koha::Database;
23
26
24
use base qw(Koha::Object);
27
use base qw(Koha::Object);
Lines 33-38 Koha::Patron::Message - Koha Message Object class Link Here
33
36
34
=cut
37
=cut
35
38
39
=head3 store
40
41
=cut
42
43
sub store {
44
    my ($self) = @_;
45
46
    # This should be done at the DB level
47
    return unless $self->borrowernumber
48
              and $self->message
49
              and $self->message_type
50
              and $self->branchcode;
51
52
    C4::Log::logaction( "MEMBERS", "ADDCIRCMESSAGE", $self->borrowernumber, $self->message )
53
        if C4::Context->preference("BorrowersLog");
54
55
    return $self->SUPER::store($self);
56
}
57
58
=head3 delete
59
60
=cut
61
62
sub delete {
63
    my ($self) = @_;
64
65
    C4::Log::logaction("MEMBERS", "DELCIRCMESSAGE", $self->borrowernumber, $self->message)
66
        if C4::Context->preference("BorrowersLog");
67
68
    return $self->SUPER::delete($self);
69
}
70
36
=head3 type
71
=head3 type
37
72
38
=cut
73
=cut
(-)a/t/db_dependent/Koha/Patron/Messages.t (-3 / +22 lines)
Lines 19-26 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 9;
23
23
24
use C4::Context;
25
use C4::Log;
24
use Koha::Patron::Message;
26
use Koha::Patron::Message;
25
use Koha::Patron::Messages;
27
use Koha::Patron::Messages;
26
use Koha::Database;
28
use Koha::Database;
Lines 33-39 $schema->storage->txn_begin; Link Here
33
my $builder        = t::lib::TestBuilder->new;
35
my $builder        = t::lib::TestBuilder->new;
34
my $library        = $builder->build( { source => 'Branch' } );
36
my $library        = $builder->build( { source => 'Branch' } );
35
my $patron         = $builder->build( { source => 'Borrower', values => { branchcode => $library->{branchcode} } } );
37
my $patron         = $builder->build( { source => 'Borrower', values => { branchcode => $library->{branchcode} } } );
38
my $nb_of_logaction = get_nb_of_logactions();
36
my $nb_of_messages = Koha::Patron::Messages->search->count;
39
my $nb_of_messages = Koha::Patron::Messages->search->count;
40
41
C4::Context->set_preference('BorrowersLog', 0);
37
my $new_message_1  = Koha::Patron::Message->new(
42
my $new_message_1  = Koha::Patron::Message->new(
38
    {   borrowernumber => $patron->{borrowernumber},
43
    {   borrowernumber => $patron->{borrowernumber},
39
        branchcode     => $library->{branchcode},
44
        branchcode     => $library->{branchcode},
Lines 41-46 my $new_message_1 = Koha::Patron::Message->new( Link Here
41
        message        => 'my message 1',
46
        message        => 'my message 1',
42
    }
47
    }
43
)->store;
48
)->store;
49
is( get_nb_of_logactions(), $nb_of_logaction, 'With BorrowersLog off, no new log should have been added' );
50
51
C4::Context->set_preference('BorrowersLog', 1);
44
my $new_message_2  = Koha::Patron::Message->new(
52
my $new_message_2  = Koha::Patron::Message->new(
45
    {   borrowernumber => $patron->{borrowernumber},
53
    {   borrowernumber => $patron->{borrowernumber},
46
        branchcode     => $library->{branchcode},
54
        branchcode     => $library->{branchcode},
Lines 48-53 my $new_message_2 = Koha::Patron::Message->new( Link Here
48
        message        => 'my message 2',
56
        message        => 'my message 2',
49
    }
57
    }
50
)->store;
58
)->store;
59
is( get_nb_of_logactions(), $nb_of_logaction + 1, 'With BorrowersLog on, 1 new log should have been added when adding a new message' );
51
60
52
like( $new_message_1->message_id, qr|^\d+$|, 'Adding a new message should have set the message_id');
61
like( $new_message_1->message_id, qr|^\d+$|, 'Adding a new message should have set the message_id');
53
is( Koha::Patron::Messages->search->count, $nb_of_messages + 2, 'The 2 messages should have been added' );
62
is( Koha::Patron::Messages->search->count, $nb_of_messages + 2, 'The 2 messages should have been added' );
Lines 55-63 is( Koha::Patron::Messages->search->count, $nb_of_messages + 2, 'The 2 messages Link Here
55
my $retrieved_message_1 = Koha::Patron::Messages->find( $new_message_1->message_id );
64
my $retrieved_message_1 = Koha::Patron::Messages->find( $new_message_1->message_id );
56
is( $retrieved_message_1->message, $new_message_1->message, 'Find a message by id should return the correct message' );
65
is( $retrieved_message_1->message, $new_message_1->message, 'Find a message by id should return the correct message' );
57
66
67
C4::Context->set_preference('BorrowersLog', 0);
58
$retrieved_message_1->delete;
68
$retrieved_message_1->delete;
59
is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message' );
69
is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message 1' );
70
is( get_nb_of_logactions(), $nb_of_logaction + 1, 'With BorrowersLog off, no new log should have been added when deleting a new message' );
71
72
C4::Context->set_preference('BorrowersLog', 1);
73
$new_message_2->delete;
74
is( Koha::Patron::Messages->search->count, $nb_of_messages, 'Delete should have deleted the message 2' );
75
is( get_nb_of_logactions(), $nb_of_logaction + 2, 'With BorrowersLog on, 1 new log should have been added when deleting a new message' );
60
76
61
$schema->storage->txn_rollback;
77
$schema->storage->txn_rollback;
62
78
79
sub get_nb_of_logactions {
80
    return scalar( @{ C4::Log::GetLogs( undef, undef, undef, ['MEMBERS'] ) } );
81
}
82
63
1;
83
1;
64
- 

Return to bug 15632