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

(-)a/Koha/Patron/Message/Preference.pm (-1 / +46 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::Exceptions;
23
use Koha::Exceptions;
24
use Koha::Patron::Categories;
24
use Koha::Patron::Categories;
25
use Koha::Patron::Message::Preferences;
25
use Koha::Patrons;
26
use Koha::Patrons;
26
27
27
use base qw(Koha::Object);
28
use base qw(Koha::Object);
Lines 36-52 Koha::Patron::Message::Preference - Koha Patron Message Preference object class Link Here
36
37
37
=cut
38
=cut
38
39
40
=head3 new
41
42
my $preference = Koha::Patron::Message::Preference->new({
43
   borrowernumber => 123,
44
   #categorycode => 'ABC',
45
   message_attribute_id => 4,
46
   wants_digest => 1,
47
   days_in_advance => 7,
48
});
49
50
Creates a new messaging preference if no existing duplicates are found. Takes
51
either borrowernumber or categorycode, but not both.
52
53
Throws Koha::Exceptions::DuplicateObject if trying to create a preference that
54
already exists for the same message_attribute_id and borrowernumber/categorycode.
55
56
=cut
57
58
sub new {
59
    my ($class, $params) = shift;
60
61
    my $self = $class->SUPER::new(@_);
62
63
    my $previous = Koha::Patron::Message::Preferences->search({
64
        '-and' => [
65
            borrowernumber => $self->borrowernumber,
66
            categorycode   => $self->categorycode,
67
            message_attribute_id => $self->message_attribute_id,
68
        ]
69
    });
70
    if ($previous->count) {
71
        Koha::Exceptions::DuplicateObject->throw(
72
            error => "A preference for this borrower/category and"
73
            ." message_attribute_id already exists",
74
        );
75
    }
76
77
    return $self;
78
}
79
39
=head3 store
80
=head3 store
40
81
41
Makes a validation before actual Koha::Object->store so that proper exceptions
82
Makes a validation before actual Koha::Object->store so that proper exceptions
42
can be thrown. See C<validate()> for documentation about exceptions.
83
can be thrown. See C<validate()> for documentation about exceptions.
43
84
85
Deletes any previous messaging preferences to avoid duplication.
86
44
=cut
87
=cut
45
88
46
sub store {
89
sub store {
47
    my $self = shift;
90
    my $self = shift;
48
91
49
    return $self->validate->SUPER::store(@_);
92
    $self->validate;
93
94
    return $self->SUPER::store(@_);
50
}
95
}
51
96
52
=head3 validate
97
=head3 validate
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-2 / +11 lines)
Lines 94-100 subtest 'Add a test messaging transport' => sub { Link Here
94
};
94
};
95
95
96
subtest 'Add a messaging preference to patron' => sub {
96
subtest 'Add a messaging preference to patron' => sub {
97
    plan tests => 3;
97
    plan tests => 4;
98
98
99
    ok($preference = Koha::Patron::Message::Preference->new({
99
    ok($preference = Koha::Patron::Message::Preference->new({
100
        borrowernumber => $patron->{'borrowernumber'},
100
        borrowernumber => $patron->{'borrowernumber'},
Lines 105-110 subtest 'Add a messaging preference to patron' => sub { Link Here
105
    is(Koha::Patron::Message::Preferences->find({
105
    is(Koha::Patron::Message::Preferences->find({
106
        borrowernumber => $patron->{'borrowernumber'} })->message_attribute_id,
106
        borrowernumber => $patron->{'borrowernumber'} })->message_attribute_id,
107
       $preference->message_attribute_id, "Found test messaging preference from database.");
107
       $preference->message_attribute_id, "Found test messaging preference from database.");
108
    eval { Koha::Patron::Message::Preference->new({
109
        borrowernumber => $patron->{'borrowernumber'},
110
        message_attribute_id => $attribute->message_attribute_id,
111
        wants_digest => 0,
112
        days_in_advance => 1,
113
    })->store };
114
    is(ref $@, "Koha::Exceptions::DuplicateObject",
115
            "Adding a duplicate preference"
116
            ." => Koha::Exceptions::DuplicateObject");
117
108
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
118
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
109
        plan tests => 6;
119
        plan tests => 6;
110
120
111
- 

Return to bug 17499