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

(-)a/Koha/Patron/Message/Preference.pm (+48 lines)
Lines 38-43 Koha::Patron::Message::Preference - Koha Patron Message Preference object class Link Here
38
38
39
=cut
39
=cut
40
40
41
=head3 new
42
43
my $preference = Koha::Patron::Message::Preference->new({
44
   borrowernumber => 123,
45
   #categorycode => 'ABC',
46
   message_attribute_id => 4,
47
   wants_digest => 1,
48
   days_in_advance => 7,
49
});
50
51
Takes either borrowernumber or categorycode, but not both.
52
53
days_in_advance may not be available. See message_attributes table for takes_days
54
configuration.
55
56
wants_digest may not be available. See message_transports table for is_digest
57
configuration.
58
59
You can instantiate a new object without custom validation errors, but when
60
storing, validation may throw exceptions. See C<validate()> for more
61
documentation.
62
63
=cut
64
65
sub new {
66
    my ($class, $params) = shift;
67
68
    my $self = $class->SUPER::new(@_);
69
70
    return $self;
71
}
72
41
=head3 store
73
=head3 store
42
74
43
Makes a validation before actual Koha::Object->store so that proper exceptions
75
Makes a validation before actual Koha::Object->store so that proper exceptions
Lines 62-67 Throws following exceptions regarding parameters. Link Here
62
94
63
See $_->parameter to identify the parameter causing the exception.
95
See $_->parameter to identify the parameter causing the exception.
64
96
97
Throws Koha::Exceptions::DuplicateObject if this preference already exists.
98
65
Returns Koha::Patron::Message::Preference object.
99
Returns Koha::Patron::Message::Preference object.
66
100
67
=cut
101
=cut
Lines 94-99 sub validate { Link Here
94
        ) unless Koha::Patron::Categories->find($self->categorycode);
128
        ) unless Koha::Patron::Categories->find($self->categorycode);
95
    }
129
    }
96
130
131
    if (!$self->in_storage) {
132
        my $previous = Koha::Patron::Message::Preferences->search({
133
            borrowernumber => $self->borrowernumber,
134
            categorycode   => $self->categorycode,
135
            message_attribute_id => $self->message_attribute_id,
136
        });
137
        if ($previous->count) {
138
            Koha::Exceptions::DuplicateObject->throw(
139
                error => 'A preference for this borrower/category and'
140
                .' message_attribute_id already exists',
141
            );
142
        }
143
    }
144
97
    my $attr;
145
    my $attr;
98
    if ($self->days_in_advance || $self->wants_digest) {
146
    if ($self->days_in_advance || $self->wants_digest) {
99
        $attr = Koha::Patron::Message::Attributes->find(
147
        $attr = Koha::Patron::Message::Attributes->find(
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-3 / +41 lines)
Lines 49-55 subtest 'Test Koha::Patron::Message::Preferences' => sub { Link Here
49
    my $attribute = build_a_test_attribute();
49
    my $attribute = build_a_test_attribute();
50
50
51
    subtest 'Test for a patron' => sub {
51
    subtest 'Test for a patron' => sub {
52
        plan tests => 2;
52
        plan tests => 3;
53
53
54
        my $patron = build_a_test_patron();
54
        my $patron = build_a_test_patron();
55
        Koha::Patron::Message::Preference->new({
55
        Koha::Patron::Message::Preference->new({
Lines 66-71 subtest 'Test Koha::Patron::Message::Preferences' => sub { Link Here
66
        ok($preference->borrower_message_preference_id > 0,
66
        ok($preference->borrower_message_preference_id > 0,
67
           'Added a new messaging preference for patron.');
67
           'Added a new messaging preference for patron.');
68
68
69
        subtest 'Test set not throwing an exception on duplicate object' => sub {
70
            plan tests => 1;
71
72
            Koha::Patron::Message::Attributes->find({
73
                message_attribute_id => $attribute->message_attribute_id
74
            })->set({ takes_days => 1 })->store;
75
            $preference->set({ days_in_advance => 1 })->store;
76
            is(ref($preference), 'Koha::Patron::Message::Preference',
77
             'Updating the preference does not cause duplicate object exception');
78
        };
79
69
        $preference->delete;
80
        $preference->delete;
70
        is(Koha::Patron::Message::Preferences->search({
81
        is(Koha::Patron::Message::Preferences->search({
71
            borrowernumber       => $patron->borrowernumber,
82
            borrowernumber       => $patron->borrowernumber,
Lines 100-106 subtest 'Test Koha::Patron::Message::Preferences' => sub { Link Here
100
};
111
};
101
112
102
subtest 'Test adding a new preference with invalid parameters' => sub {
113
subtest 'Test adding a new preference with invalid parameters' => sub {
103
    plan tests => 3;
114
    plan tests => 4;
104
115
105
    subtest 'Missing parameters' => sub {
116
    subtest 'Missing parameters' => sub {
106
        plan tests => 1;
117
        plan tests => 1;
Lines 178-183 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
178
189
179
        $schema->storage->txn_rollback;
190
        $schema->storage->txn_rollback;
180
    };
191
    };
192
193
    subtest 'Duplicate object' => sub {
194
        plan tests => 2;
195
196
        $schema->storage->txn_begin;
197
198
        my $attribute = build_a_test_attribute();
199
        my $patron    = build_a_test_patron();
200
        my $preference = Koha::Patron::Message::Preference->new({
201
            borrowernumber => $patron->borrowernumber,
202
            message_attribute_id => $attribute->message_attribute_id,
203
            wants_digest => 0,
204
            days_in_advance => undef,
205
        })->store;
206
        ok($preference->borrower_message_preference_id,
207
           'Added a new messaging preference for patron.');
208
        eval { Koha::Patron::Message::Preference->new({
209
            borrowernumber => $patron->borrowernumber,
210
            message_attribute_id => $attribute->message_attribute_id,
211
            wants_digest => 0,
212
            days_in_advance => undef,
213
        })->store };
214
        is(ref $@, 'Koha::Exceptions::DuplicateObject',
215
                'Adding a duplicate preference'
216
                .' => Koha::Exceptions::DuplicateObject');
217
218
        $schema->storage->txn_rollback;
219
    };
181
};
220
};
182
221
183
sub build_a_test_attribute {
222
sub build_a_test_attribute {
184
- 

Return to bug 17499