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

(-)a/Koha/Patron/Message/Preference.pm (-4 / +145 lines)
Lines 22-29 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::Attributes;
25
use Koha::Patron::Message::Preferences;
26
use Koha::Patron::Message::Preferences;
26
use Koha::Patron::Message::Transport::Preferences;
27
use Koha::Patron::Message::Transport::Preferences;
28
use Koha::Patron::Message::Transport::Types;
29
use Koha::Patron::Message::Transports;
27
use Koha::Patrons;
30
use Koha::Patrons;
28
31
29
use base qw(Koha::Object);
32
use base qw(Koha::Object);
Lines 44-49 my $preference = Koha::Patron::Message::Preference->new({ Link Here
44
   borrowernumber => 123,
47
   borrowernumber => 123,
45
   #categorycode => 'ABC',
48
   #categorycode => 'ABC',
46
   message_attribute_id => 4,
49
   message_attribute_id => 4,
50
   message_transport_types => ['email', 'sms'], # see documentation below
47
   wants_digest => 1,
51
   wants_digest => 1,
48
   days_in_advance => 7,
52
   days_in_advance => 7,
49
});
53
});
Lines 54-65 either borrowernumber or categorycode, but not both. Link Here
54
Throws Koha::Exceptions::DuplicateObject if trying to create a preference that
58
Throws Koha::Exceptions::DuplicateObject if trying to create a preference that
55
already exists for the same message_attribute_id and borrowernumber/categorycode.
59
already exists for the same message_attribute_id and borrowernumber/categorycode.
56
60
61
C<message_transport_types> is a parameter that is not actually a column in this
62
Koha-object. Given this parameter, the message transport types will be added as
63
related transport types for this object. For get and set, you can access them via
64
subroutine C<message_transport_types()> in this class.
65
57
=cut
66
=cut
58
67
59
sub new {
68
sub new {
60
    my ($class, $params) = shift;
69
    my ($class, $params) = @_;
70
71
    my $types = $params->{'message_transport_types'};
72
    delete $params->{'message_transport_types'};
61
73
62
    my $self = $class->SUPER::new(@_);
74
    my $self = $class->SUPER::new($params);
75
76
    $self->_set_message_transport_types($types);
63
77
64
    my $previous = Koha::Patron::Message::Preferences->search({
78
    my $previous = Koha::Patron::Message::Preferences->search({
65
        '-and' => [
79
        '-and' => [
Lines 140-145 sub new_from_default { Link Here
140
    return $self;
154
    return $self;
141
}
155
}
142
156
157
=head3 message_transport_types
158
159
$preference->message_transport_types
160
Returns a HASHREF of message transport types for this messaging preference, e.g.
161
if ($preference->message_transport_types->{'email'}) {
162
    # email is one of the transport preferences
163
}
164
165
$preference->message_transport_types('email', 'sms');
166
Sets the given message transport types for this messaging preference
167
168
=cut
169
170
sub message_transport_types {
171
    my $self = shift;
172
173
    unless (@_) {
174
        if ($self->{'_message_transport_types'}) {
175
            return $self->{'_message_transport_types'};
176
        }
177
        map { $self->{'_message_transport_types'}->{$_->message_transport_type} =
178
                Koha::Patron::Message::Transports->find({
179
                    message_attribute_id => $self->message_attribute_id,
180
                    message_transport_type => $_->message_transport_type,
181
                    is_digest => $self->wants_digest})->letter_code }
182
        Koha::Patron::Message::Transport::Preferences->search({
183
            borrower_message_preference_id => $self->borrower_message_preference_id,
184
        })->as_list;
185
        return $self->{'_message_transport_types'} || {};
186
    }
187
    else {
188
        $self->_set_message_transport_types(@_);
189
        return $self;
190
    }
191
}
192
193
=head3 set
194
195
$preference->set({
196
    message_transport_types => ['sms', 'phone'],
197
    wants_digest => 0,
198
})->store;
199
200
Sets preference object values and additionally message_transport_types if given.
201
202
=cut
203
204
sub set {
205
    my ($self, $params) = @_;
206
207
    if ($params && $params->{'message_transport_types'}) {
208
        $self->message_transport_types($params->{'message_transport_types'});
209
        delete $params->{'message_transport_types'};
210
    }
211
212
    return $self->SUPER::set($params) if $params;
213
}
214
143
=head3 store
215
=head3 store
144
216
145
Makes a validation before actual Koha::Object->store so that proper exceptions
217
Makes a validation before actual Koha::Object->store so that proper exceptions
Lines 152-160 Deletes any previous messaging preferences to avoid duplication. Link Here
152
sub store {
224
sub store {
153
    my $self = shift;
225
    my $self = shift;
154
226
155
    $self->validate;
227
    $self->validate->SUPER::store(@_);
228
229
    # store message transport types
230
    if (exists $self->{'_message_transport_types'}) {
231
        Koha::Patron::Message::Transport::Preferences->search({
232
            borrower_message_preference_id => $self->borrower_message_preference_id,
233
        })->delete;
234
        foreach my $type (keys %{$self->{'_message_transport_types'}}) {
235
            Koha::Patron::Message::Transport::Preference->new({
236
                borrower_message_preference_id => $self->borrower_message_preference_id,
237
                message_transport_type => $type,
238
            })->store;
239
        }
240
    }
156
241
157
    return $self->SUPER::store(@_);
242
    return $self;
158
}
243
}
159
244
160
=head3 validate
245
=head3 validate
Lines 203-208 sub validate { Link Here
203
    return $self;
288
    return $self;
204
}
289
}
205
290
291
sub _set_message_transport_types {
292
    my $self = shift;
293
294
    return unless $_[0];
295
296
    $self->{'_message_transport_types'} = undef;
297
    my $types = ref $_[0] eq "ARRAY" ? $_[0] : [@_];
298
    return unless $types;
299
    $self->_validate_message_transport_types({ message_transport_types => $types });
300
    foreach my $type (@$types) {
301
        unless (exists $self->{'_message_transport_types'}->{$type}) {
302
            $self->{'_message_transport_types'}->{$type} =
303
            Koha::Patron::Message::Transports->find({
304
                message_attribute_id => $self->message_attribute_id,
305
                message_transport_type => $type,
306
                is_digest => $self->wants_digest})->letter_code;
307
        }
308
    }
309
    return $self;
310
}
311
312
sub _validate_message_transport_types {
313
    my ($self, $params) = @_;
314
315
    if (ref($params) eq 'HASH' && $params->{'message_transport_types'}) {
316
        if (ref($params->{'message_transport_types'}) ne 'ARRAY') {
317
            $params->{'message_transport_types'} = [$params->{'message_transport_types'}];
318
        }
319
        my $types = $params->{'message_transport_types'};
320
321
        foreach my $type (@{$types}) {
322
            unless (Koha::Patron::Message::Transport::Types->find({
323
                message_transport_type => $type
324
            })) {
325
                Koha::Exceptions::BadParameter->throw(
326
                    error => "Message transport type '$type' does not exist",
327
                    parameter => 'message_transport_type',
328
                );
329
            }
330
            my $tmp = (ref($self) eq __PACKAGE__) ? $self->unblessed : $params;
331
            unless (Koha::Patron::Message::Transports->find({
332
                    message_transport_type => $type,
333
                    message_attribute_id => $tmp->{'message_attribute_id'},
334
                    is_digest => $tmp->{'wants_digest'},
335
            })) {
336
                Koha::Exceptions::BadParameter->throw(
337
                    error => "Message transport option for '$type' (".
338
                    ($tmp->{'wants_digest'} ? 'digest':'no digest').") does not exist",
339
                    parameter => 'message_transport_type',
340
                );
341
            }
342
        }
343
        return $types;
344
    }
345
}
346
206
=head3 type
347
=head3 type
207
348
208
=cut
349
=cut
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-5 / +128 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 16;
22
use Test::More tests => 17;
23
23
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
Lines 111-117 subtest 'Add a test messaging attribute' => sub { Link Here
111
};
111
};
112
112
113
subtest 'Add a test messaging transport' => sub {
113
subtest 'Add a test messaging transport' => sub {
114
    plan tests => 2;
114
    plan tests => 4;
115
115
116
    ok(Koha::Patron::Message::Transport->new({
116
    ok(Koha::Patron::Message::Transport->new({
117
        message_attribute_id => $attribute->message_attribute_id,
117
        message_attribute_id => $attribute->message_attribute_id,
Lines 119-125 subtest 'Add a test messaging transport' => sub { Link Here
119
        is_digest => 0,
119
        is_digest => 0,
120
        letter_module => "circulation",
120
        letter_module => "circulation",
121
        letter_code => "CHECKIN",
121
        letter_code => "CHECKIN",
122
    })->store, "Added a new messaging transport type.");
122
    })->store, "Added a new messaging transport type test.");
123
    ok(Koha::Patron::Message::Transport->new({
124
        message_attribute_id => $attribute->message_attribute_id,
125
        message_transport_type => 'sms',
126
        is_digest => 0,
127
        letter_module => "circulation",
128
        letter_code => "CHECKIN",
129
    })->store, "Added a new messaging transport type sms.");
130
    ok(Koha::Patron::Message::Transport->new({
131
        message_attribute_id => $attribute->message_attribute_id,
132
        message_transport_type => 'email',
133
        is_digest => 0,
134
        letter_module => "circulation",
135
        letter_code => "CHECKIN",
136
    })->store, "Added a new messaging transport type email.");
123
    is(Koha::Patron::Message::Transports->find({
137
    is(Koha::Patron::Message::Transports->find({
124
        message_transport_type => "test" })->message_attribute_id,
138
        message_transport_type => "test" })->message_attribute_id,
125
       $attribute->message_attribute_id , "Found test messaging transport from database.");
139
       $attribute->message_attribute_id , "Found test messaging transport from database.");
Lines 182-188 subtest 'Add a messaging preference to patron' => sub { Link Here
182
    };
196
    };
183
197
184
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
198
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
185
        plan tests => 6;
199
        plan tests => 11;
186
200
187
        eval { Koha::Patron::Message::Preference->new->store };
201
        eval { Koha::Patron::Message::Preference->new->store };
188
        is (ref $@, "Koha::Exceptions::MissingParameter",
202
        is (ref $@, "Koha::Exceptions::MissingParameter",
Lines 211-216 subtest 'Add a messaging preference to patron' => sub { Link Here
211
            ." => Koha::Exceptions::BadParameter");
225
            ." => Koha::Exceptions::BadParameter");
212
        is ($@->parameter, "categorycode", "The previous exception tells us it"
226
        is ($@->parameter, "categorycode", "The previous exception tells us it"
213
            ." was the categorycode.");
227
            ." was the categorycode.");
228
        eval { Koha::Patron::Message::Preference->new({
229
                borrowernumber => $patron->{'borrowernumber'},
230
                message_transport_types => ['nonexistent']
231
            })->store };
232
        is (ref $@, "Koha::Exceptions::BadParameter",
233
            "Adding a message preference with invalid message_transport_type"
234
            ." => Koha::Exceptions::BadParameter");
235
        is ($@->parameter, "message_transport_type", "The previous exception tells us it"
236
            ." was the message_transport_type.");
237
        eval {
238
            Koha::Patron::Message::Preference->new({
239
                borrowernumber => $patron->{'borrowernumber'},
240
                message_attribute_id => $attribute->message_attribute_id,
241
                message_transport_types => ['sms'],
242
                wants_digest => 1,
243
            })->store };
244
        is (ref $@, "Koha::Exceptions::BadParameter",
245
            "Adding a message preference with invalid message_transport_type"
246
            ." => Koha::Exceptions::BadParameter");
247
        is ($@->parameter, "message_transport_type", "The previous exception tells us it"
248
            ." was the message_transport_type.");
249
        like ($@->error, qr/^Message transport option/, "Exception s because of given"
250
            ." message_transport_type is not a valid option.");
214
    };
251
    };
215
};
252
};
216
253
Lines 227-232 subtest 'Add a messaging transport preference to patron' => sub { Link Here
227
       "Found test messaging transport preference from database.");
264
       "Found test messaging transport preference from database.");
228
};
265
};
229
266
267
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
268
    plan tests => 3;
269
270
    subtest 'Get' => sub {
271
        plan tests => 5;
272
273
        Koha::Patron::Message::Transport::Preferences->search({
274
            borrower_message_preference_id => $preference->borrower_message_preference_id,
275
        })->delete;
276
        Koha::Patron::Message::Transport::Preference->new({
277
            borrower_message_preference_id => $preference->borrower_message_preference_id,
278
            message_transport_type => 'sms',
279
        })->store;
280
        Koha::Patron::Message::Transport::Preference->new({
281
            borrower_message_preference_id => $preference->borrower_message_preference_id,
282
            message_transport_type => 'email',
283
        })->store;
284
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
285
            borrower_message_preference_id => $preference->borrower_message_preference_id,
286
        });
287
288
        ok(Koha::Patron::Message::Preference->can('message_transport_types'), "Method available-");
289
        my $transports = $preference->message_transport_types;
290
        is(keys $transports, $stored_transports->count, "->message_transport_types gets correct amount of transport types.");
291
        is($transports->{$stored_transports->next->message_transport_type}, "CHECKIN", "Found correct message transport type and letter code.");
292
        is($transports->{$stored_transports->next->message_transport_type}, "CHECKIN", "Found correct message transport type and letter code.");
293
        ok(!$preference->message_transport_types->{'nonexistent'}, "Didn't find nonexistent transport type.");
294
    };
295
296
    subtest 'Set' => sub {
297
        plan tests => 9;
298
299
        # 1/3, use message_transport_types(list)
300
        Koha::Patron::Message::Transport::Preferences->search({
301
            borrower_message_preference_id => $preference->borrower_message_preference_id,
302
        })->delete;
303
        ok($preference->message_transport_types('sms', 'email')->store, "1/3 Set returned true.");
304
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
305
            borrower_message_preference_id => $preference->borrower_message_preference_id,
306
        });
307
        is($stored_transports->next->message_transport_type, 'email', "First type is email.");
308
        is($stored_transports->next->message_transport_type, 'sms', "Second type is sms.");
309
310
        # 2/3, use message_transport_types(ARRAYREF)
311
        Koha::Patron::Message::Transport::Preferences->search({
312
            borrower_message_preference_id => $preference->borrower_message_preference_id,
313
        })->delete;
314
        ok($preference->message_transport_types(['sms', 'email'])->store, "2/3 Set returned true.");
315
        $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
316
            borrower_message_preference_id => $preference->borrower_message_preference_id,
317
        });
318
        is($stored_transports->next->message_transport_type, 'email', "First type is email.");
319
        is($stored_transports->next->message_transport_type, 'sms', "Second type is sms.");
320
321
        # 3/3, use set({ message_transport_types => ARRAYREF })
322
        Koha::Patron::Message::Transport::Preferences->search({
323
            borrower_message_preference_id => $preference->borrower_message_preference_id,
324
        })->delete;
325
        ok($preference->set({ message_transport_types => ['sms', 'email']})->store, "3/3 Set returned true.");
326
        $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
327
            borrower_message_preference_id => $preference->borrower_message_preference_id,
328
        });
329
        is($stored_transports->next->message_transport_type, 'email', "First type is email.");
330
        is($stored_transports->next->message_transport_type, 'sms', "Second type is sms.");
331
    };
332
333
    subtest 'New' => sub {
334
        plan tests => 3;
335
336
        $preference->delete;
337
        ok($preference = Koha::Patron::Message::Preference->new({
338
            borrowernumber => $patron->{'borrowernumber'},
339
            message_attribute_id => $attribute->message_attribute_id,
340
            wants_digest => 0,
341
            days_in_advance => 1,
342
            message_transport_types => $transport_type->message_transport_type,
343
        })->store, "Added a new messaging preference and transport types to patron.");
344
        ok($preference->message_transport_types->{$transport_type->message_transport_type},
345
           "The transport type is stored in the object.");
346
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
347
            borrower_message_preference_id => $preference->borrower_message_preference_id,
348
        });
349
        is($stored_transports->next->message_transport_type, $transport_type->message_transport_type,
350
           "The transport type is stored in the database.");
351
    };
352
};
353
230
$schema->storage->txn_rollback;
354
$schema->storage->txn_rollback;
231
355
232
1;
356
1;
233
- 

Return to bug 17499