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

(-)a/Koha/Patron/Message/Preference.pm (-3 / +146 lines)
Lines 25-30 use Koha::Patron::Categories; Link Here
25
use Koha::Patron::Message::Attributes;
25
use Koha::Patron::Message::Attributes;
26
use Koha::Patron::Message::Preferences;
26
use Koha::Patron::Message::Preferences;
27
use Koha::Patron::Message::Transport::Preferences;
27
use Koha::Patron::Message::Transport::Preferences;
28
use Koha::Patron::Message::Transport::Types;
28
use Koha::Patron::Message::Transports;
29
use Koha::Patron::Message::Transports;
29
use Koha::Patrons;
30
use Koha::Patrons;
30
31
Lines 46-51 my $preference = Koha::Patron::Message::Preference->new({ Link Here
46
   borrowernumber => 123,
47
   borrowernumber => 123,
47
   #categorycode => 'ABC',
48
   #categorycode => 'ABC',
48
   message_attribute_id => 4,
49
   message_attribute_id => 4,
50
   message_transport_types => ['email', 'sms'], # see documentation below
49
   wants_digest => 1,
51
   wants_digest => 1,
50
   days_in_advance => 7,
52
   days_in_advance => 7,
51
});
53
});
Lines 62-73 You can instantiate a new object without custom validation errors, but when Link Here
62
storing, validation may throw exceptions. See C<validate()> for more
64
storing, validation may throw exceptions. See C<validate()> for more
63
documentation.
65
documentation.
64
66
67
C<message_transport_types> is a parameter that is not actually a column in this
68
Koha-object. Given this parameter, the message transport types will be added as
69
related transport types for this object. For get and set, you can access them via
70
subroutine C<message_transport_types()> in this class.
71
65
=cut
72
=cut
66
73
67
sub new {
74
sub new {
68
    my ($class, $params) = shift;
75
    my ($class, $params) = @_;
76
77
    my $types = $params->{'message_transport_types'};
78
    delete $params->{'message_transport_types'};
69
79
70
    my $self = $class->SUPER::new(@_);
80
    my $self = $class->SUPER::new($params);
81
82
    $self->_set_message_transport_types($types);
71
83
72
    return $self;
84
    return $self;
73
}
85
}
Lines 144-149 sub new_from_default { Link Here
144
    return $self;
156
    return $self;
145
}
157
}
146
158
159
=head3 message_transport_types
160
161
$preference->message_transport_types
162
Returns a HASHREF of message transport types for this messaging preference, e.g.
163
if ($preference->message_transport_types->{'email'}) {
164
    # email is one of the transport preferences
165
}
166
167
$preference->message_transport_types('email', 'sms');
168
Sets the given message transport types for this messaging preference
169
170
=cut
171
172
sub message_transport_types {
173
    my $self = shift;
174
175
    unless (@_) {
176
        if ($self->{'_message_transport_types'}) {
177
            return $self->{'_message_transport_types'};
178
        }
179
        map { $self->{'_message_transport_types'}->{$_->message_transport_type} =
180
                Koha::Patron::Message::Transports->find({
181
                    message_attribute_id => $self->message_attribute_id,
182
                    message_transport_type => $_->message_transport_type,
183
                    is_digest => $self->wants_digest})->letter_code }
184
        Koha::Patron::Message::Transport::Preferences->search({
185
            borrower_message_preference_id => $self->borrower_message_preference_id,
186
        })->as_list;
187
        return $self->{'_message_transport_types'} || {};
188
    }
189
    else {
190
        $self->_set_message_transport_types(@_);
191
        return $self;
192
    }
193
}
194
195
=head3 set
196
197
$preference->set({
198
    message_transport_types => ['sms', 'phone'],
199
    wants_digest => 0,
200
})->store;
201
202
Sets preference object values and additionally message_transport_types if given.
203
204
=cut
205
206
sub set {
207
    my ($self, $params) = @_;
208
209
    if ($params && $params->{'message_transport_types'}) {
210
        $self->message_transport_types($params->{'message_transport_types'});
211
        delete $params->{'message_transport_types'};
212
    }
213
214
    return $self->SUPER::set($params) if $params;
215
}
216
147
=head3 store
217
=head3 store
148
218
149
Makes a validation before actual Koha::Object->store so that proper exceptions
219
Makes a validation before actual Koha::Object->store so that proper exceptions
Lines 154-160 can be thrown. See C<validate()> for documentation about exceptions. Link Here
154
sub store {
224
sub store {
155
    my $self = shift;
225
    my $self = shift;
156
226
157
    return $self->validate->SUPER::store(@_);
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 =>
233
                $self->borrower_message_preference_id,
234
        })->delete;
235
        foreach my $type (keys %{$self->{'_message_transport_types'}}) {
236
            Koha::Patron::Message::Transport::Preference->new({
237
                borrower_message_preference_id =>
238
                    $self->borrower_message_preference_id,
239
                message_transport_type => $type,
240
            })->store;
241
        }
242
    }
243
244
    return $self;
158
}
245
}
159
246
160
=head3 validate
247
=head3 validate
Lines 252-257 sub validate { Link Here
252
    return $self;
339
    return $self;
253
}
340
}
254
341
342
sub _set_message_transport_types {
343
    my $self = shift;
344
345
    return unless $_[0];
346
347
    $self->{'_message_transport_types'} = undef;
348
    my $types = ref $_[0] eq "ARRAY" ? $_[0] : [@_];
349
    return unless $types;
350
    $self->_validate_message_transport_types({ message_transport_types => $types });
351
    foreach my $type (@$types) {
352
        unless (exists $self->{'_message_transport_types'}->{$type}) {
353
            $self->{'_message_transport_types'}->{$type} =
354
            Koha::Patron::Message::Transports->find({
355
                message_attribute_id => $self->message_attribute_id,
356
                message_transport_type => $type,
357
                is_digest => $self->wants_digest})->letter_code;
358
        }
359
    }
360
    return $self;
361
}
362
363
sub _validate_message_transport_types {
364
    my ($self, $params) = @_;
365
366
    if (ref($params) eq 'HASH' && $params->{'message_transport_types'}) {
367
        if (ref($params->{'message_transport_types'}) ne 'ARRAY') {
368
            $params->{'message_transport_types'} = [$params->{'message_transport_types'}];
369
        }
370
        my $types = $params->{'message_transport_types'};
371
372
        foreach my $type (@{$types}) {
373
            unless (Koha::Patron::Message::Transport::Types->find({
374
                message_transport_type => $type
375
            })) {
376
                Koha::Exceptions::BadParameter->throw(
377
                    error => "Message transport type '$type' does not exist",
378
                    parameter => 'message_transport_type',
379
                );
380
            }
381
            my $tmp = (ref($self) eq __PACKAGE__) ? $self->unblessed : $params;
382
            unless (Koha::Patron::Message::Transports->find({
383
                    message_transport_type => $type,
384
                    message_attribute_id => $tmp->{'message_attribute_id'},
385
                    is_digest => $tmp->{'wants_digest'},
386
            })) {
387
                Koha::Exceptions::BadParameter->throw(
388
                    error => "Message transport option for '$type' (".
389
                    ($tmp->{'wants_digest'} ? 'digest':'no digest').") does not exist",
390
                    parameter => 'message_transport_type',
391
                );
392
            }
393
        }
394
        return $types;
395
    }
396
}
397
255
=head3 type
398
=head3 type
256
399
257
=cut
400
=cut
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-3 / +184 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
23
use Test::More tests => 6;
23
24
24
use t::lib::Mocks;
25
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
use t::lib::TestBuilder;
Lines 173-178 subtest 'Add preferences from defaults' => sub { Link Here
173
    $schema->storage->txn_rollback;
174
    $schema->storage->txn_rollback;
174
};
175
};
175
176
177
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
178
    plan tests => 4;
179
180
    ok(Koha::Patron::Message::Preference->can('message_transport_types'),
181
       'Method message_transport_types available');
182
183
    subtest 'get message_transport_types' => sub {
184
        plan tests => 4;
185
186
        $schema->storage->txn_begin;
187
188
        my $patron = build_a_test_patron();
189
        my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
190
            patron => $patron
191
        });
192
        Koha::Patron::Message::Transport::Preferences->search({
193
            borrower_message_preference_id => $preference->borrower_message_preference_id,
194
        })->delete;
195
        Koha::Patron::Message::Transport::Preference->new({
196
            borrower_message_preference_id => $preference->borrower_message_preference_id,
197
            message_transport_type => $mtt1->message_transport_type,
198
        })->store;
199
        Koha::Patron::Message::Transport::Preference->new({
200
            borrower_message_preference_id => $preference->borrower_message_preference_id,
201
            message_transport_type => $mtt2->message_transport_type,
202
        })->store;
203
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
204
            borrower_message_preference_id => $preference->borrower_message_preference_id,
205
        });
206
        my $transport1 = Koha::Patron::Message::Transports->find({
207
            message_attribute_id => $preference->message_attribute_id,
208
            message_transport_type => $mtt1->message_transport_type,
209
        });
210
        my $transport2 = Koha::Patron::Message::Transports->find({
211
            message_attribute_id => $preference->message_attribute_id,
212
            message_transport_type => $mtt2->message_transport_type,
213
        });
214
        my $transports = $preference->message_transport_types;
215
        is(keys %{$transports}, $stored_transports->count,
216
           '->message_transport_types gets correct amount of transport types.');
217
        is($transports->{$stored_transports->next->message_transport_type},
218
           $transport1->letter_code, 'Found correct message transport type and letter code.');
219
        is($transports->{$stored_transports->next->message_transport_type},
220
           $transport2->letter_code, 'Found correct message transport type and letter code.');
221
        ok(!$preference->message_transport_types->{'nonexistent'},
222
           'Didn\'t find nonexistent transport type.');
223
224
        $schema->storage->txn_rollback;
225
    };
226
227
    subtest 'set message_transport_types' => sub {
228
        plan tests => 6;
229
230
        $schema->storage->txn_begin;
231
232
        my $patron = build_a_test_patron();
233
        my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
234
            patron => $patron
235
        });
236
237
        my $mtt1_str = $mtt1->message_transport_type;
238
        my $mtt2_str = $mtt2->message_transport_type;
239
        # 1/3, use message_transport_types(list)
240
        Koha::Patron::Message::Transport::Preferences->search({
241
            borrower_message_preference_id => $preference->borrower_message_preference_id,
242
        })->delete;
243
        ok($preference->message_transport_types($mtt1_str, $mtt2_str)->store,
244
           '1/3 Set returned true.');
245
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
246
            borrower_message_preference_id => $preference->borrower_message_preference_id,
247
            '-or' => [
248
                message_transport_type => $mtt1_str,
249
                message_transport_type => $mtt2_str
250
            ]
251
        });
252
        is($stored_transports->count, 2, 'Two transports selected');
253
254
        # 2/3, use message_transport_types(ARRAYREF)
255
        Koha::Patron::Message::Transport::Preferences->search({
256
            borrower_message_preference_id => $preference->borrower_message_preference_id,
257
        })->delete;
258
        ok($preference->message_transport_types([$mtt1_str, $mtt2_str])->store,
259
           '2/3 Set returned true.');
260
        $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
261
            borrower_message_preference_id => $preference->borrower_message_preference_id,
262
            '-or' => [
263
                message_transport_type => $mtt1_str,
264
                message_transport_type => $mtt2_str
265
            ]
266
        });
267
        is($stored_transports->count, 2, 'Two transports selected');
268
269
        # 3/3, use set({ message_transport_types => ARRAYREF })
270
        Koha::Patron::Message::Transport::Preferences->search({
271
            borrower_message_preference_id => $preference->borrower_message_preference_id,
272
        })->delete;
273
        ok($preference->set({
274
            message_transport_types => [$mtt1_str, $mtt2_str]})->store,
275
           '3/3 Set returned true.');
276
        $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
277
            borrower_message_preference_id => $preference->borrower_message_preference_id,
278
            '-or' => [
279
                message_transport_type => $mtt1_str,
280
                message_transport_type => $mtt2_str
281
            ]
282
        });
283
        is($stored_transports->count, 2, 'Two transports selected');
284
285
        $schema->storage->txn_rollback;
286
    };
287
288
    subtest 'new message_transport_types' => sub {
289
        plan tests => 3;
290
291
        $schema->storage->txn_begin;
292
293
        my $patron    = build_a_test_patron();
294
        my $letter    = build_a_test_letter();
295
        my $attribute = build_a_test_attribute();
296
        my $mtt       = build_a_test_transport_type();
297
        Koha::Patron::Message::Transport->new({
298
            message_attribute_id   => $attribute->message_attribute_id,
299
            message_transport_type => $mtt->message_transport_type,
300
            is_digest              => 0,
301
            letter_module          => $letter->module,
302
            letter_code            => $letter->code,
303
        })->store;
304
        ok(my $preference = Koha::Patron::Message::Preference->new({
305
            borrowernumber => $patron->borrowernumber,
306
            message_attribute_id => $attribute->message_attribute_id,
307
            wants_digest => 0,
308
            days_in_advance => undef,
309
            message_transport_types => $mtt->message_transport_type,
310
        })->store, 'Added a new messaging preference and transport types to patron.');
311
        ok($preference->message_transport_types->{$mtt->message_transport_type},
312
           'The transport type is stored in the object.');
313
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
314
            borrower_message_preference_id => $preference->borrower_message_preference_id,
315
        });
316
        is($stored_transports->next->message_transport_type, $mtt->message_transport_type,
317
           'The transport type is stored in the database.');
318
319
        $schema->storage->txn_rollback;
320
    };
321
};
322
176
subtest 'Test adding a new preference with invalid parameters' => sub {
323
subtest 'Test adding a new preference with invalid parameters' => sub {
177
    plan tests => 4;
324
    plan tests => 4;
178
325
Lines 203-209 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
203
    };
350
    };
204
351
205
    subtest 'Bad parameter' => sub {
352
    subtest 'Bad parameter' => sub {
206
        plan tests => 8;
353
        plan tests => 13;
207
354
208
        $schema->storage->txn_begin;
355
        $schema->storage->txn_begin;
209
356
Lines 250-255 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
250
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
397
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
251
            .' was the days_in_advance.');
398
            .' was the days_in_advance.');
252
399
400
        eval { Koha::Patron::Message::Preference->new({
401
                borrowernumber => $patron->{'borrowernumber'},
402
                message_transport_types => ['nonexistent']
403
            })->store };
404
        is (ref $@, 'Koha::Exceptions::BadParameter',
405
            'Adding a message preference with invalid message_transport_type'
406
            .' => Koha::Exceptions::BadParameter');
407
        is ($@->parameter, 'message_transport_type', 'The previous exception tells us it'
408
            .' was the message_transport_type.');
409
        eval {
410
            Koha::Patron::Message::Preference->new({
411
                borrowernumber => $patron->{'borrowernumber'},
412
                message_attribute_id => $attribute->message_attribute_id,
413
                message_transport_types => ['sms'],
414
                wants_digest => 1,
415
            })->store };
416
        is (ref $@, 'Koha::Exceptions::BadParameter',
417
            'Adding a message preference with invalid message_transport_type'
418
            .' => Koha::Exceptions::BadParameter');
419
        is ($@->parameter, 'message_transport_type', 'The previous exception tells us it'
420
            .' was the message_transport_type.');
421
        like ($@->error, qr/^Message transport option/, 'Exception s because of given'
422
            .' message_transport_type is not a valid option.');
423
253
        $schema->storage->txn_rollback;
424
        $schema->storage->txn_rollback;
254
    };
425
    };
255
426
Lines 395-398 sub build_a_test_category_preference { Link Here
395
    return ($default, $mtt1, $mtt2);
566
    return ($default, $mtt1, $mtt2);
396
}
567
}
397
568
569
sub build_a_test_complete_preference {
570
    my ($params) = @_;
571
572
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params);
573
    my $patron = $params->{patron};
574
    $patron->set_default_messaging_preferences;
575
    return (Koha::Patron::Message::Preferences->search({
576
        borrowernumber => $patron->borrowernumber
577
    })->next, $mtt1, $mtt2);
578
}
579
398
1;
580
1;
399
- 

Return to bug 17499