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

(-)a/Koha/Exceptions.pm (-1 / +7 lines)
Lines 24-30 use Exception::Class ( Link Here
24
    },
24
    },
25
    'Koha::Exceptions::MissingParameter' => {
25
    'Koha::Exceptions::MissingParameter' => {
26
        isa => 'Koha::Exceptions::Exception',
26
        isa => 'Koha::Exceptions::Exception',
27
        description => 'A required parameter is missing'
27
        description => 'A required parameter is missing',
28
        fields => ['parameter'],
29
    },
30
    'Koha::Exceptions::TooManyParameters' => {
31
        isa => 'Koha::Exceptions::Exception',
32
        description => 'Too many parameters given',
33
        fields => ['parameter'],
28
    },
34
    },
29
    'Koha::Exceptions::ParameterTooHigh' => {
35
    'Koha::Exceptions::ParameterTooHigh' => {
30
        isa => 'Koha::Exceptions::Exception',
36
        isa => 'Koha::Exceptions::Exception',
(-)a/Koha/Patron.pm (+42 lines)
Lines 43-48 use Koha::Patron::HouseboundProfile; Link Here
43
use Koha::Patron::HouseboundRole;
43
use Koha::Patron::HouseboundRole;
44
use Koha::Patron::Images;
44
use Koha::Patron::Images;
45
use Koha::Patron::Modifications;
45
use Koha::Patron::Modifications;
46
use Koha::Patron::Message::Preferences;
46
use Koha::Patron::Relationships;
47
use Koha::Patron::Relationships;
47
use Koha::Patrons;
48
use Koha::Patrons;
48
use Koha::Plugins;
49
use Koha::Plugins;
Lines 1627-1632 sub get_extended_attribute { Link Here
1627
    return $attribute->next;
1628
    return $attribute->next;
1628
}
1629
}
1629
1630
1631
=head3 set_default_messaging_preferences
1632
1633
    $patron->set_default_messaging_preferences
1634
1635
Sets default messaging preferences on patron.
1636
1637
See Koha::Patron::Message::Preference(s) for more documentation, especially on
1638
thrown exceptions.
1639
1640
=cut
1641
1642
sub set_default_messaging_preferences {
1643
    my ($self, $categorycode) = @_;
1644
1645
    my $options = Koha::Patron::Message::Preferences->get_options;
1646
1647
    foreach my $option (@$options) {
1648
        # Check that this option has preference configuration for this category
1649
        unless (Koha::Patron::Message::Preferences->search({
1650
            message_attribute_id => $option->{message_attribute_id},
1651
            categorycode         => $categorycode || $self->categorycode,
1652
        })->count) {
1653
            next;
1654
        }
1655
1656
        # Delete current setting
1657
        Koha::Patron::Message::Preferences->search({
1658
             borrowernumber => $self->borrowernumber,
1659
             message_attribute_id => $option->{message_attribute_id},
1660
        })->delete;
1661
1662
        Koha::Patron::Message::Preference->new_from_default({
1663
            borrowernumber => $self->borrowernumber,
1664
            categorycode   => $categorycode || $self->categorycode,
1665
            message_attribute_id => $option->{message_attribute_id},
1666
        });
1667
    }
1668
1669
    return $self;
1670
}
1671
1630
=head3 to_api
1672
=head3 to_api
1631
1673
1632
    my $json = $patron->to_api;
1674
    my $json = $patron->to_api;
(-)a/Koha/Patron/Message/Attribute.pm (+50 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Attribute;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.a
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::Patron::Message::Attribute - Koha Patron Message Attribute object class
29
30
=head1 API
31
32
=head2 Class Methods
33
34
=cut
35
36
=head3 type
37
38
=cut
39
40
sub _type {
41
    return 'MessageAttribute';
42
}
43
44
=head1 AUTHOR
45
46
Lari Taskula <lari.taskula@hypernova.fi>
47
48
=cut
49
50
1;
(-)a/Koha/Patron/Message/Attributes.pm (+55 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Attributes;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
use Koha::Patron::Message::Attribute;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Patron::Message::Attributes - Koha Patron Message Attributes object class
30
31
=head1 API
32
33
=head2 Class Methods
34
35
=cut
36
37
=head3 type
38
39
=cut
40
41
sub _type {
42
    return 'MessageAttribute';
43
}
44
45
sub object_class {
46
    return 'Koha::Patron::Message::Attribute';
47
}
48
49
=head1 AUTHOR
50
51
Lari Taskula <lari.taskula@hypernova.fi>
52
53
=cut
54
55
1;
(-)a/Koha/Patron/Message/Preference.pm (+473 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Preference;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.a
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
use Koha::Exceptions;
24
use Koha::Patron::Categories;
25
use Koha::Patron::Message::Attributes;
26
use Koha::Patron::Message::Preferences;
27
use Koha::Patron::Message::Transport::Preferences;
28
use Koha::Patron::Message::Transport::Types;
29
use Koha::Patron::Message::Transports;
30
use Koha::Patrons;
31
32
use base qw(Koha::Object);
33
34
=head1 NAME
35
36
Koha::Patron::Message::Preference - Koha Patron Message Preference object class
37
38
=head1 API
39
40
=head2 Class Methods
41
42
=cut
43
44
=head3 new
45
46
my $preference = Koha::Patron::Message::Preference->new({
47
   borrowernumber => 123,
48
   #categorycode => 'ABC',
49
   message_attribute_id => 4,
50
   message_transport_types => ['email', 'sms'], # see documentation below
51
   wants_digest => 1,
52
   days_in_advance => 7,
53
});
54
55
Takes either borrowernumber or categorycode, but not both.
56
57
days_in_advance may not be available. See message_attributes table for takes_days
58
configuration.
59
60
wants_digest may not be available. See message_transports table for is_digest
61
configuration.
62
63
You can instantiate a new object without custom validation errors, but when
64
storing, validation may throw exceptions. See C<validate()> for more
65
documentation.
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
72
=cut
73
74
sub new {
75
    my ($class, $params) = @_;
76
77
    my $types = $params->{'message_transport_types'};
78
    delete $params->{'message_transport_types'};
79
80
    my $self = $class->SUPER::new($params);
81
82
    $self->_set_message_transport_types($types);
83
84
    return $self;
85
}
86
87
=head3 new_from_default
88
89
my $preference = Koha::Patron::Message::Preference->new_from_default({
90
    borrowernumber => 123,
91
    categorycode   => 'ABC',   # if not given, patron's categorycode will be used
92
    message_attribute_id => 1,
93
});
94
95
NOTE: This subroutine initializes and STORES the object (in order to set
96
message transport types for the preference), so no need to call ->store when
97
preferences are initialized via this method.
98
99
Stores default messaging preference for C<categorycode> to patron for given
100
C<message_attribute_id>.
101
102
Throws Koha::Exceptions::MissingParameter if any of following is missing:
103
- borrowernumber
104
- message_attribute_id
105
106
Throws Koha::Exceptions::ObjectNotFound if default preferences are not found.
107
108
=cut
109
110
sub new_from_default {
111
    my ($class, $params) = @_;
112
113
    my @required = qw(borrowernumber message_attribute_id);
114
    foreach my $p (@required) {
115
        Koha::Exceptions::MissingParameter->throw(
116
            error => 'Missing required parameter.',
117
            parameter => $p,
118
        ) unless exists $params->{$p};
119
    }
120
    unless ($params->{'categorycode'}) {
121
        my $patron = Koha::Patrons->find($params->{borrowernumber});
122
        $params->{'categorycode'} = $patron->categorycode;
123
    }
124
125
    my $default = Koha::Patron::Message::Preferences->find({
126
        categorycode => $params->{'categorycode'},
127
        message_attribute_id => $params->{'message_attribute_id'},
128
    });
129
    Koha::Exceptions::ObjectNotFound->throw(
130
        error => 'Default messaging preference for given categorycode and'
131
        .' message_attribute_id cannot be found.',
132
    ) unless $default;
133
    $default = $default->unblessed;
134
135
    # Add a new messaging preference for patron
136
    my $self = $class->SUPER::new({
137
        borrowernumber => $params->{'borrowernumber'},
138
        message_attribute_id => $default->{'message_attribute_id'},
139
        days_in_advance => $default->{'days_in_advance'},
140
        wants_digest => $default->{'wants_digest'},
141
    })->store;
142
143
    # Set default messaging transport types
144
    my $default_transport_types =
145
    Koha::Patron::Message::Transport::Preferences->search({
146
        borrower_message_preference_id =>
147
                    $default->{'borrower_message_preference_id'}
148
    });
149
    while (my $transport = $default_transport_types->next) {
150
        Koha::Patron::Message::Transport::Preference->new({
151
            borrower_message_preference_id => $self->borrower_message_preference_id,
152
            message_transport_type => $transport->message_transport_type,
153
        })->store;
154
    }
155
156
    return $self;
157
}
158
159
=head3 message_name
160
161
$preference->message_name
162
163
Gets message_name for this messaging preference.
164
165
Setter not implemented.
166
167
=cut
168
169
sub message_name {
170
    my ($self) = @_;
171
172
    if ($self->{'_message_name'}) {
173
        return $self->{'_message_name'};
174
    }
175
    $self->{'_message_name'} = Koha::Patron::Message::Attributes->find({
176
        message_attribute_id => $self->message_attribute_id,
177
    })->message_name;
178
    return $self->{'_message_name'};
179
}
180
181
=head3 message_transport_types
182
183
$preference->message_transport_types
184
Returns a HASHREF of message transport types for this messaging preference, e.g.
185
if ($preference->message_transport_types->{'email'}) {
186
    # email is one of the transport preferences
187
}
188
189
$preference->message_transport_types('email', 'sms');
190
Sets the given message transport types for this messaging preference
191
192
=cut
193
194
sub message_transport_types {
195
    my $self = shift;
196
197
    unless (@_) {
198
        if ($self->{'_message_transport_types'}) {
199
            return $self->{'_message_transport_types'};
200
        }
201
        map {
202
            my $transport = Koha::Patron::Message::Transports->find({
203
                message_attribute_id => $self->message_attribute_id,
204
                message_transport_type => $_->message_transport_type,
205
                is_digest => $self->wants_digest
206
            });
207
            unless ($transport) {
208
                my $logger = Koha::Logger->get;
209
                $logger->warn(
210
                    $self->message_name . ' has no transport with '.
211
                    $_->message_transport_type . ' (digest: '.
212
                    ($self->wants_digest ? 'yes':'no').').'
213
                );
214
            }
215
            $self->{'_message_transport_types'}->{$_->message_transport_type}
216
                = $transport ? $transport->letter_code : ' ';
217
        }
218
        Koha::Patron::Message::Transport::Preferences->search({
219
            borrower_message_preference_id => $self->borrower_message_preference_id,
220
        })->as_list;
221
        return $self->{'_message_transport_types'} || {};
222
    }
223
    else {
224
        $self->_set_message_transport_types(@_);
225
        return $self;
226
    }
227
}
228
229
=head3 set
230
231
$preference->set({
232
    message_transport_types => ['sms', 'phone'],
233
    wants_digest => 0,
234
})->store;
235
236
Sets preference object values and additionally message_transport_types if given.
237
238
=cut
239
240
sub set {
241
    my ($self, $params) = @_;
242
243
    my $mtt = $params->{'message_transport_types'};
244
    delete $params->{'message_transport_types'};
245
246
    $self->SUPER::set($params) if $params;
247
    if ($mtt) {
248
        $self->message_transport_types($mtt);
249
    }
250
251
    return $self;
252
}
253
254
=head3 store
255
256
Makes a validation before actual Koha::Object->store so that proper exceptions
257
can be thrown. See C<validate()> for documentation about exceptions.
258
259
=cut
260
261
sub store {
262
    my $self = shift;
263
264
    $self->validate->SUPER::store(@_);
265
266
    # store message transport types
267
    if (exists $self->{'_message_transport_types'}) {
268
        Koha::Patron::Message::Transport::Preferences->search({
269
            borrower_message_preference_id =>
270
                $self->borrower_message_preference_id,
271
        })->delete;
272
        foreach my $type (keys %{$self->{'_message_transport_types'}}) {
273
            Koha::Patron::Message::Transport::Preference->new({
274
                borrower_message_preference_id =>
275
                    $self->borrower_message_preference_id,
276
                message_transport_type => $type,
277
            })->store;
278
        }
279
    }
280
281
    return $self;
282
}
283
284
=head3 validate
285
286
Makes a basic validation for object.
287
288
Throws following exceptions regarding parameters.
289
- Koha::Exceptions::MissingParameter
290
- Koha::Exceptions::TooManyParameters
291
- Koha::Exceptions::BadParameter
292
293
See $_->parameter to identify the parameter causing the exception.
294
295
Throws Koha::Exceptions::DuplicateObject if this preference already exists.
296
297
Returns Koha::Patron::Message::Preference object.
298
299
=cut
300
301
sub validate {
302
    my ($self) = @_;
303
304
    if ($self->borrowernumber && $self->categorycode) {
305
        Koha::Exceptions::TooManyParameters->throw(
306
            error => 'Both borrowernumber and category given, only one accepted',
307
            parameter => ['borrowernumber', 'categorycode'],
308
        );
309
    }
310
    if (!$self->borrowernumber && !$self->categorycode) {
311
        Koha::Exceptions::MissingParameter->throw(
312
            error => 'borrowernumber or category required, none given',
313
            parameter => ['borrowernumber', 'categorycode'],
314
        );
315
    }
316
    if ($self->borrowernumber) {
317
        Koha::Exceptions::BadParameter->throw(
318
            error => 'Patron not found.',
319
            parameter => 'borrowernumber',
320
        ) unless Koha::Patrons->find($self->borrowernumber);
321
    }
322
    if ($self->categorycode) {
323
        Koha::Exceptions::BadParameter->throw(
324
            error => 'Category not found.',
325
            parameter => 'categorycode',
326
        ) unless Koha::Patron::Categories->find($self->categorycode);
327
    }
328
329
    if (!$self->in_storage) {
330
        my $previous = Koha::Patron::Message::Preferences->search({
331
            borrowernumber => $self->borrowernumber,
332
            categorycode   => $self->categorycode,
333
            message_attribute_id => $self->message_attribute_id,
334
        });
335
        if ($previous->count) {
336
            Koha::Exceptions::DuplicateObject->throw(
337
                error => 'A preference for this borrower/category and'
338
                .' message_attribute_id already exists',
339
            );
340
        }
341
    }
342
343
    my $attr = Koha::Patron::Message::Attributes->find(
344
        $self->message_attribute_id
345
    );
346
    unless ($attr) {
347
        Koha::Exceptions::BadParameter->throw(
348
            error => 'Message attribute with id '.$self->message_attribute_id
349
            .' not found',
350
            parameter => 'message_attribute_id'
351
        );
352
    }
353
    if (defined $self->days_in_advance) {
354
        if ($attr && $attr->takes_days == 0) {
355
            Koha::Exceptions::BadParameter->throw(
356
                error => 'days_in_advance cannot be defined for '.
357
                $attr->message_name . '.',
358
                parameter => 'days_in_advance',
359
            );
360
        }
361
        elsif ($self->days_in_advance < 0 || $self->days_in_advance > 30) {
362
            Koha::Exceptions::BadParameter->throw(
363
                error => 'days_in_advance has to be a value between 0-30 for '.
364
                $attr->message_name . '.',
365
                parameter => 'days_in_advance',
366
            );
367
        }
368
    }
369
    if (defined $self->wants_digest) {
370
        my $transports = Koha::Patron::Message::Transports->search({
371
            message_attribute_id => $self->message_attribute_id,
372
            is_digest            => $self->wants_digest ? 1 : 0,
373
        });
374
        Koha::Exceptions::BadParameter->throw(
375
            error => (!$self->wants_digest ? 'Digest must be selected'
376
                                           : 'Digest cannot be selected')
377
            . ' for '.$attr->message_name.'.',
378
            parameter => 'wants_digest',
379
        ) if $transports->count == 0;
380
    }
381
382
    return $self;
383
}
384
385
sub _set_message_transport_types {
386
    my $self = shift;
387
388
    return unless $_[0];
389
390
    $self->{'_message_transport_types'} = undef;
391
    my $types = ref $_[0] eq "ARRAY" ? $_[0] : [@_];
392
    return unless $types;
393
    $self->_validate_message_transport_types({ message_transport_types => $types });
394
    foreach my $type (@$types) {
395
        unless (exists $self->{'_message_transport_types'}->{$type}) {
396
            my $transport = Koha::Patron::Message::Transports->find({
397
                message_attribute_id => $self->message_attribute_id,
398
                message_transport_type => $type
399
            });
400
            unless ($transport) {
401
                Koha::Exceptions::BadParameter->throw(
402
                    error => 'No transport configured for '.$self->message_name.
403
                        " transport type $type.",
404
                    parameter => 'message_transport_types'
405
                );
406
            }
407
            if (defined $self->borrowernumber) {
408
                my $patron = Koha::Patrons->find($self->borrowernumber);
409
                if ($type eq 'email') {
410
                    if ( !$patron->email )
411
                    {
412
                        Koha::Exceptions::BadParameter->throw(
413
                            error => 'Patron has not set email address, '.
414
                                     'cannot use email as message transport',
415
                            parameter => 'message_transport_types'
416
                        );
417
                    }
418
                }
419
                elsif ($type eq 'sms') {
420
                    if ( !$patron->smsalertnumber ){
421
                        Koha::Exceptions::BadParameter->throw(
422
                            error => 'Patron has not set sms number, '.
423
                                     'cannot set sms as message transport',
424
                            parameter => 'message_transport_types'
425
                        );
426
                    }
427
                }
428
            }
429
            $self->{'_message_transport_types'}->{$type}
430
                = $transport->letter_code;
431
        }
432
    }
433
    return $self;
434
}
435
436
sub _validate_message_transport_types {
437
    my ($self, $params) = @_;
438
439
    if (ref($params) eq 'HASH' && $params->{'message_transport_types'}) {
440
        if (ref($params->{'message_transport_types'}) ne 'ARRAY') {
441
            $params->{'message_transport_types'} = [$params->{'message_transport_types'}];
442
        }
443
        my $types = $params->{'message_transport_types'};
444
445
        foreach my $type (@{$types}) {
446
            unless (Koha::Patron::Message::Transport::Types->find({
447
                message_transport_type => $type
448
            })) {
449
                Koha::Exceptions::BadParameter->throw(
450
                    error => "Message transport type '$type' does not exist",
451
                    parameter => 'message_transport_types',
452
                );
453
            }
454
        }
455
        return $types;
456
    }
457
}
458
459
=head3 type
460
461
=cut
462
463
sub _type {
464
    return 'BorrowerMessagePreference';
465
}
466
467
=head1 AUTHOR
468
469
Lari Taskula <lari.taskula@hypernova.fi>
470
471
=cut
472
473
1;
(-)a/Koha/Patron/Message/Preferences.pm (+146 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Preferences;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
use Koha::Patron::Message::Attributes;
24
use Koha::Patron::Message::Preference;
25
use Koha::Patron::Message::Transports;
26
27
use base qw(Koha::Objects);
28
29
=head1 NAME
30
31
Koha::Patron::Message::Preferences - Koha Patron Message Preferences object class
32
33
=head1 API
34
35
=head2 Class Methods
36
37
=cut
38
39
=head3 find_with_message_name
40
41
Koha::Patron::Message::Preferences->find_with_message_name({
42
    borrowernumber => 123,
43
    message_name => 'Hold_Filled',
44
});
45
46
Converts C<message_name> into C<message_attribute_id> and continues find.
47
48
=cut
49
50
sub find_with_message_name {
51
    my ($self, $id) = @_;
52
53
    if (ref($id) eq "HASH" && $id->{'message_name'}) {
54
        my $attr = Koha::Patron::Message::Attributes->find({
55
            message_name => $id->{'message_name'},
56
        });
57
        $id->{'message_attribute_id'} = ($attr) ?
58
                    $attr->message_attribute_id : undef;
59
        delete $id->{'message_name'};
60
    }
61
62
    return $self->SUPER::find($id);
63
}
64
65
=head3 get_options
66
67
my $messaging_options = Koha::Patron::Message::Preferences->get_options
68
69
Returns an ARRAYref of HASHrefs on available messaging options.
70
71
=cut
72
73
sub get_options {
74
    my ($self) = @_;
75
76
    my $transports = Koha::Patron::Message::Transports->search(undef,
77
        {
78
            join => ['message_attribute'],
79
            '+select' => ['message_attribute.message_name', 'message_attribute.takes_days'],
80
            '+as' => ['message_name', 'takes_days'],
81
        });
82
83
    my $choices;
84
    while (my $transport = $transports->next) {
85
        my $name = $transport->get_column('message_name');
86
        $choices->{$name}->{'message_attribute_id'} = $transport->message_attribute_id;
87
        $choices->{$name}->{'message_name'}         = $name;
88
        $choices->{$name}->{'takes_days'}           = $transport->get_column('takes_days');
89
        $choices->{$name}->{'has_digest'}           ||= 1 if $transport->is_digest;
90
        $choices->{$name}->{'has_digest_off'}       ||= 1 if !$transport->is_digest;
91
        $choices->{$name}->{'transport_'.$transport->get_column('message_transport_type')} = ' ';
92
    }
93
94
    my @return = values %$choices;
95
    @return = sort { $a->{message_attribute_id} <=> $b->{message_attribute_id} } @return;
96
97
    return \@return;
98
}
99
100
=head3 search_with_message_name
101
102
Koha::Patron::Message::Preferences->search_with_message_name({
103
    borrowernumber => 123,
104
    message_name => 'Hold_Filled',
105
});
106
107
Converts C<message_name> into C<message_attribute_id> and continues search. Use
108
Koha::Patron::Message::Preferences->search with a proper join for more complicated
109
searches.
110
111
=cut
112
113
sub search_with_message_name {
114
    my ($self, $params, $attributes) = @_;
115
116
    if (ref($params) eq "HASH" && $params->{'message_name'}) {
117
        my $attr = Koha::Patron::Message::Attributes->find({
118
            message_name => $params->{'message_name'},
119
        });
120
        $params->{'message_attribute_id'} = ($attr) ?
121
                    $attr->message_attribute_id : undef;
122
        delete $params->{'message_name'};
123
    }
124
125
    return $self->SUPER::search($params, $attributes);
126
}
127
128
=head3 type
129
130
=cut
131
132
sub _type {
133
    return 'BorrowerMessagePreference';
134
}
135
136
sub object_class {
137
    return 'Koha::Patron::Message::Preference';
138
}
139
140
=head1 AUTHOR
141
142
Lari Taskula <lari.taskula@hypernova.fi>
143
144
=cut
145
146
1;
(-)a/Koha/Patron/Message/Transport.pm (+50 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Transport;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.a
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::Patron::Message::Transport - Koha Patron Message Transport object class
29
30
=head1 API
31
32
=head2 Class Methods
33
34
=cut
35
36
=head3 type
37
38
=cut
39
40
sub _type {
41
    return 'MessageTransport';
42
}
43
44
=head1 AUTHOR
45
46
Lari Taskula <lari.taskula@hypernova.fi>
47
48
=cut
49
50
1;
(-)a/Koha/Patron/Message/Transport/Preference.pm (+51 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Transport::Preference;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.a
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::Patron::Message::Transport::Preference - Koha Patron Message Transport
29
Preference object class
30
31
=head1 API
32
33
=head2 Class Methods
34
35
=cut
36
37
=head3 type
38
39
=cut
40
41
sub _type {
42
    return 'BorrowerMessageTransportPreference';
43
}
44
45
=head1 AUTHOR
46
47
Lari Taskula <lari.taskula@hypernova.fi>
48
49
=cut
50
51
1;
(-)a/Koha/Patron/Message/Transport/Preferences.pm (+56 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Transport::Preferences;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
use Koha::Patron::Message::Transport::Preference;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Patron::Message::Transport::Preferences - Koha Patron Message Transport
30
Preferences object class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 type
39
40
=cut
41
42
sub _type {
43
    return 'BorrowerMessageTransportPreference';
44
}
45
46
sub object_class {
47
    return 'Koha::Patron::Message::Transport::Preference';
48
}
49
50
=head1 AUTHOR
51
52
Lari Taskula <lari.taskula@hypernova.fi>
53
54
=cut
55
56
1;
(-)a/Koha/Patron/Message/Transport/Type.pm (+51 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Transport::Type;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.a
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::Patron::Message::Transport::Type - Koha Patron Message Transport Type
29
object class
30
31
=head1 API
32
33
=head2 Class Methods
34
35
=cut
36
37
=head3 type
38
39
=cut
40
41
sub _type {
42
    return 'MessageTransportType';
43
}
44
45
=head1 AUTHOR
46
47
Lari Taskula <lari.taskula@hypernova.fi>
48
49
=cut
50
51
1;
(-)a/Koha/Patron/Message/Transport/Types.pm (+56 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Transport::Types;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
use Koha::Patron::Message::Transport::Type;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Patron::Message::Transport::Types - Koha Patron Message Transport Types
30
object class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 type
39
40
=cut
41
42
sub _type {
43
    return 'MessageTransportType';
44
}
45
46
sub object_class {
47
    return 'Koha::Patron::Message::Transport::Type';
48
}
49
50
=head1 AUTHOR
51
52
Lari Taskula <lari.taskula@hypernova.fi>
53
54
=cut
55
56
1;
(-)a/Koha/Patron/Message/Transports.pm (+55 lines)
Line 0 Link Here
1
package Koha::Patron::Message::Transports;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
use Koha::Patron::Message::Transport;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Patron::Message::Transports - Koha Patron Message Transports object class
30
31
=head1 API
32
33
=head2 Class Methods
34
35
=cut
36
37
=head3 type
38
39
=cut
40
41
sub _type {
42
    return 'MessageTransport';
43
}
44
45
sub object_class {
46
    return 'Koha::Patron::Message::Transport';
47
}
48
49
=head1 AUTHOR
50
51
Lari Taskula <lari.taskula@hypernova.fi>
52
53
=cut
54
55
1;
(-)a/t/db_dependent/Koha/Patron/Message/Attributes.t (+74 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
24
use Koha::Database;
25
26
my $schema  = Koha::Database->new->schema;
27
28
subtest 'Test class imports' => sub {
29
    plan tests => 2;
30
31
    use_ok('Koha::Patron::Message::Attribute');
32
    use_ok('Koha::Patron::Message::Attributes');
33
};
34
35
subtest 'Test Koha::Patron::Message::Attributes' => sub {
36
    plan tests => 6;
37
38
    $schema->storage->txn_begin;
39
40
    Koha::Patron::Message::Attribute->new({
41
        message_name => 'Test_Attribute'
42
    })->store;
43
    Koha::Patron::Message::Attribute->new({
44
        message_name => 'Test_Attribute2',
45
        takes_days   => 1
46
    })->store;
47
48
    my $attribute  = Koha::Patron::Message::Attributes->find({
49
        message_name => 'Test_Attribute' });
50
    my $attribute2 = Koha::Patron::Message::Attributes->find({
51
        message_name => 'Test_Attribute2' });
52
53
    is($attribute->message_name, 'Test_Attribute',
54
       'Added a new messaging attribute.');
55
    is($attribute->takes_days, 0,
56
       'For that messaging attribute, takes_days is disabled by default.');
57
    is($attribute2->message_name, 'Test_Attribute2',
58
       'Added another messaging attribute.');
59
    is($attribute2->takes_days, 1,
60
       'takes_days is enabled for that message attribute (as expected).');
61
62
    $attribute->delete;
63
    $attribute2->delete;
64
    is(Koha::Patron::Message::Attributes->find({
65
        message_name => 'Test_Attribute' }), undef,
66
       'Deleted the first message attribute.');
67
    is(Koha::Patron::Message::Attributes->find({
68
        message_name => 'Test_Attribute2' }), undef,
69
       'Deleted the second message attribute.');
70
71
    $schema->storage->txn_rollback;
72
};
73
74
1;
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (+758 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 7;
23
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
27
use C4::Context;
28
29
use Koha::Notice::Templates;
30
use Koha::Patron::Categories;
31
use Koha::Patron::Message::Attributes;
32
use Koha::Patron::Message::Transport::Types;
33
use Koha::Patron::Message::Transports;
34
use Koha::Patrons;
35
36
use File::Temp qw/tempfile/;
37
use Log::Log4perl;
38
39
my $schema  = Koha::Database->new->schema;
40
my $builder = t::lib::TestBuilder->new;
41
42
subtest 'Test class imports' => sub {
43
    plan tests => 2;
44
45
    use_ok('Koha::Patron::Message::Preference');
46
    use_ok('Koha::Patron::Message::Preferences');
47
};
48
49
subtest 'Test Koha::Patron::Message::Preferences' => sub {
50
    plan tests => 2;
51
52
    $schema->storage->txn_begin;
53
54
    my $attribute = build_a_test_attribute();
55
    my $letter = build_a_test_letter();
56
    my $mtt = build_a_test_transport_type();
57
    Koha::Patron::Message::Transport->new({
58
        message_attribute_id   => $attribute->message_attribute_id,
59
        message_transport_type => $mtt->message_transport_type,
60
        is_digest              => 0,
61
        letter_module          => $letter->module,
62
        letter_code            => $letter->code,
63
    })->store;
64
65
    subtest 'Test for a patron' => sub {
66
        plan tests => 3;
67
68
        my $patron = build_a_test_patron();
69
        Koha::Patron::Message::Preference->new({
70
            borrowernumber       => $patron->borrowernumber,
71
            message_attribute_id => $attribute->message_attribute_id,
72
            wants_digest         => 0,
73
            days_in_advance      => undef,
74
        })->store;
75
76
        my $preference = Koha::Patron::Message::Preferences->find({
77
            borrowernumber       => $patron->borrowernumber,
78
            message_attribute_id => $attribute->message_attribute_id
79
        });
80
        ok($preference->borrower_message_preference_id > 0,
81
           'Added a new messaging preference for patron.');
82
83
        subtest 'Test set not throwing an exception on duplicate object' => sub {
84
            plan tests => 1;
85
86
            Koha::Patron::Message::Attributes->find({
87
                message_attribute_id => $attribute->message_attribute_id
88
            })->set({ takes_days => 1 })->store;
89
            $preference->set({ days_in_advance => 1 })->store;
90
            is(ref($preference), 'Koha::Patron::Message::Preference',
91
             'Updating the preference does not cause duplicate object exception');
92
        };
93
94
        $preference->delete;
95
        is(Koha::Patron::Message::Preferences->search({
96
            borrowernumber       => $patron->borrowernumber,
97
            message_attribute_id => $attribute->message_attribute_id
98
        })->count, 0, 'Deleted the messaging preference.');
99
    };
100
101
    subtest 'Test for a category' => sub {
102
        my $category = build_a_test_category();
103
        Koha::Patron::Message::Preference->new({
104
            categorycode         => $category->categorycode,
105
            message_attribute_id => $attribute->message_attribute_id,
106
            wants_digest         => 0,
107
            days_in_advance      => undef,
108
        })->store;
109
110
        my $preference = Koha::Patron::Message::Preferences->find({
111
            categorycode         => $category->categorycode,
112
            message_attribute_id => $attribute->message_attribute_id
113
        });
114
        ok($preference->borrower_message_preference_id > 0,
115
           'Added a new messaging preference for category.');
116
117
        $preference->delete;
118
        is(Koha::Patron::Message::Preferences->search({
119
            categorycode         => $category->categorycode,
120
            message_attribute_id => $attribute->message_attribute_id
121
        })->count, 0, 'Deleted the messaging preference.');
122
    };
123
124
    $schema->storage->txn_rollback;
125
};
126
127
subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub {
128
    plan tests => 2;
129
130
    subtest 'Test method availability and return value' => sub {
131
        plan tests => 3;
132
133
        ok(Koha::Patron::Message::Preferences->can('get_options'),
134
            'Method get_options is available.');
135
        ok(my $options = Koha::Patron::Message::Preferences->get_options,
136
            'Called get_options successfully.');
137
        is(ref($options), 'ARRAY', 'get_options returns a ARRAYref');
138
    };
139
140
    subtest 'Make sure options are correct' => sub {
141
        $schema->storage->txn_begin;
142
        my $options = Koha::Patron::Message::Preferences->get_options;
143
144
        foreach my $option (@$options) {
145
            my $n = $option->{'message_name'};
146
            my $attr = Koha::Patron::Message::Attributes->find($option->{'message_attribute_id'});
147
            is($option->{'message_attribute_id'}, $attr->message_attribute_id,
148
               '$n: message_attribute_id is set');
149
            is($option->{'message_name'}, $attr->message_name, '$n: message_name is set');
150
            is($option->{'takes_days'}, $attr->takes_days, '$n: takes_days is set');
151
            my $transports = Koha::Patron::Message::Transports->search({
152
                message_attribute_id => $option->{'message_attribute_id'},
153
                is_digest => $option->{'has_digest'} || 0,
154
            });
155
            while (my $trnzport = $transports->next) {
156
                is($option->{'has_digest'} || 0, $trnzport->is_digest, '$n: has_digest is set for '.$trnzport->message_transport_type);
157
                is($option->{'transport_'.$trnzport->message_transport_type}, ' ', '$n: transport_'.$trnzport->message_transport_type.' is set');
158
            }
159
        }
160
161
        $schema->storage->txn_rollback;
162
    };
163
};
164
165
subtest 'Add preferences from defaults' => sub {
166
    plan tests => 3;
167
168
    $schema->storage->txn_begin;
169
170
    my $patron = build_a_test_patron();
171
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference({
172
        patron => $patron,
173
    });
174
    ok(Koha::Patron::Message::Preference->new_from_default({
175
        borrowernumber       => $patron->borrowernumber,
176
        categorycode         => $patron->categorycode,
177
        message_attribute_id => $default->message_attribute_id,
178
    })->store, 'Added a default preference to patron.');
179
    ok(my $pref = Koha::Patron::Message::Preferences->find({
180
        borrowernumber       => $patron->borrowernumber,
181
        message_attribute_id => $default->message_attribute_id,
182
    }), 'Found the default preference from patron.');
183
    is(Koha::Patron::Message::Transport::Preferences->search({
184
        borrower_message_preference_id => $pref->borrower_message_preference_id
185
    })->count, 2, 'Found the two transport types that we set earlier');
186
187
    $schema->storage->txn_rollback;
188
};
189
190
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
191
    plan tests => 4;
192
193
    ok(Koha::Patron::Message::Preference->can('message_transport_types'),
194
       'Method message_transport_types available');
195
196
    subtest 'get message_transport_types' => sub {
197
        plan tests => 5;
198
199
        $schema->storage->txn_begin;
200
201
        my $patron = build_a_test_patron();
202
        my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
203
            patron => $patron
204
        });
205
        Koha::Patron::Message::Transport::Preferences->search({
206
            borrower_message_preference_id => $preference->borrower_message_preference_id,
207
        })->delete;
208
        Koha::Patron::Message::Transport::Preference->new({
209
            borrower_message_preference_id => $preference->borrower_message_preference_id,
210
            message_transport_type => $mtt1->message_transport_type,
211
        })->store;
212
        Koha::Patron::Message::Transport::Preference->new({
213
            borrower_message_preference_id => $preference->borrower_message_preference_id,
214
            message_transport_type => $mtt2->message_transport_type,
215
        })->store;
216
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
217
            borrower_message_preference_id => $preference->borrower_message_preference_id,
218
        });
219
        my $transport1 = Koha::Patron::Message::Transports->find({
220
            message_attribute_id => $preference->message_attribute_id,
221
            message_transport_type => $mtt1->message_transport_type,
222
        });
223
        my $transport2 = Koha::Patron::Message::Transports->find({
224
            message_attribute_id => $preference->message_attribute_id,
225
            message_transport_type => $mtt2->message_transport_type,
226
        });
227
        my $transports = $preference->message_transport_types;
228
        is(keys %{$transports}, $stored_transports->count,
229
           '->message_transport_types gets correct amount of transport types.');
230
        is($transports->{$stored_transports->next->message_transport_type},
231
           $transport1->letter_code, 'Found correct message transport type and letter code.');
232
        is($transports->{$stored_transports->next->message_transport_type},
233
           $transport2->letter_code, 'Found correct message transport type and letter code.');
234
        ok(!$preference->message_transport_types->{'nonexistent'},
235
           'Didn\'t find nonexistent transport type.');
236
237
        subtest 'test logging of warnings by invalid message transport type' => sub {
238
            plan tests => 2;
239
240
            my $log = mytempfile();
241
            my $conf = mytempfile( <<"HERE"
242
log4perl.logger.opac = WARN, OPAC
243
log4perl.appender.OPAC=Log::Log4perl::Appender::TestBuffer
244
log4perl.appender.OPAC.filename=$log
245
log4perl.appender.OPAC.mode=append
246
log4perl.appender.OPAC.layout=SimpleLayout
247
log4perl.logger.intranet = WARN, INTRANET
248
log4perl.appender.INTRANET=Log::Log4perl::Appender::TestBuffer
249
log4perl.appender.INTRANET.filename=$log
250
log4perl.appender.INTRANET.mode=append
251
log4perl.appender.INTRANET.layout=SimpleLayout
252
HERE
253
            );
254
            t::lib::Mocks::mock_config('log4perl_conf', $conf);
255
            my $appenders = Log::Log4perl->appenders;
256
            my $appender = Log::Log4perl->appenders->{OPAC};
257
258
            my $pref = Koha::Patron::Message::Preferences->find(
259
                $preference->borrower_message_preference_id
260
            );
261
            my $transports = $pref->message_transport_types;
262
            is($appender, undef, 'Nothing in buffer yet');
263
264
            my $mtt_new = build_a_test_transport_type();
265
            Koha::Patron::Message::Transport::Preference->new({
266
                borrower_message_preference_id =>
267
                                $pref->borrower_message_preference_id,
268
                message_transport_type => $mtt_new->message_transport_type,
269
            })->store;
270
            $pref = Koha::Patron::Message::Preferences->find(
271
                $pref->borrower_message_preference_id
272
            );
273
            $transports = $pref->message_transport_types;
274
            $appender = Log::Log4perl->appenders->{OPAC};
275
            my $name = $pref->message_name;
276
            my $tt = $mtt_new->message_transport_type;
277
            like($appender->buffer, qr/WARN - $name has no transport with $tt/,
278
                 'Logged invalid message transport type');
279
        };
280
281
        $schema->storage->txn_rollback;
282
    };
283
284
    subtest 'set message_transport_types' => sub {
285
        plan tests => 12;
286
287
        $schema->storage->txn_begin;
288
289
        my $patron = build_a_test_patron();
290
        my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
291
            patron => $patron
292
        });
293
294
        my $mtt1_str = $mtt1->message_transport_type;
295
        my $mtt2_str = $mtt2->message_transport_type;
296
        # 1/3, use message_transport_types(list)
297
        Koha::Patron::Message::Transport::Preferences->search({
298
            borrower_message_preference_id => $preference->borrower_message_preference_id,
299
        })->delete;
300
        ok($preference->message_transport_types($mtt1_str, $mtt2_str)->store,
301
           '1/3 Set returned true.');
302
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
303
            borrower_message_preference_id => $preference->borrower_message_preference_id,
304
            '-or' => [
305
                message_transport_type => $mtt1_str,
306
                message_transport_type => $mtt2_str
307
            ]
308
        });
309
        is($stored_transports->count, 2, 'Two transports selected');
310
311
        # 2/3, use message_transport_types(ARRAYREF)
312
        Koha::Patron::Message::Transport::Preferences->search({
313
            borrower_message_preference_id => $preference->borrower_message_preference_id,
314
        })->delete;
315
        ok($preference->message_transport_types([$mtt1_str, $mtt2_str])->store,
316
           '2/3 Set returned true.');
317
        $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
318
            borrower_message_preference_id => $preference->borrower_message_preference_id,
319
            '-or' => [
320
                message_transport_type => $mtt1_str,
321
                message_transport_type => $mtt2_str
322
            ]
323
        });
324
        is($stored_transports->count, 2, 'Two transports selected');
325
326
        # 3/3, use set({ message_transport_types => ARRAYREF })
327
        Koha::Patron::Message::Transport::Preferences->search({
328
            borrower_message_preference_id => $preference->borrower_message_preference_id,
329
        })->delete;
330
        ok($preference->set({
331
            message_transport_types => [$mtt1_str, $mtt2_str]})->store,
332
           '3/3 Set returned true.');
333
        $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
334
            borrower_message_preference_id => $preference->borrower_message_preference_id,
335
            '-or' => [
336
                message_transport_type => $mtt1_str,
337
                message_transport_type => $mtt2_str
338
            ]
339
        });
340
        is($stored_transports->count, 2, 'Two transports selected');
341
342
        # Test email and smsalertnumber validation
343
        eval { Koha::Patron::Message::Transport::Types->new({
344
                message_transport_type => 'email'
345
            })->store };
346
        eval { Koha::Patron::Message::Transport::Types->new({
347
                message_transport_type => 'sms'
348
            })->store };
349
        Koha::Patron::Message::Transport->new({
350
            message_attribute_id   => $preference->message_attribute_id,
351
            message_transport_type => 'email',
352
            is_digest              => 1
353
        })->store;
354
        Koha::Patron::Message::Transport->new({
355
            message_attribute_id   => $preference->message_attribute_id,
356
            message_transport_type => 'sms',
357
            is_digest              => 1
358
        })->store;
359
        $patron->set({ email => '', smsalertnumber => '' })->store;
360
        eval {
361
            $preference->message_transport_types('email')->store;
362
        };
363
        is (ref $@, 'Koha::Exceptions::BadParameter',
364
            'Adding a message preference with invalid message_transport_type'
365
            .' => Koha::Exceptions::BadParameter');
366
        is ($@->parameter, 'message_transport_types', 'The previous exception '
367
            .' tells us it was the message_transport_types');
368
        like ($@->error, qr/^Patron has not set email address/, 'Exception '
369
            .' is because of patron has not set email address.');
370
        eval {
371
            $preference->message_transport_types('sms')->store;
372
        };
373
        is (ref $@, 'Koha::Exceptions::BadParameter',
374
            'Adding a message preference with invalid message_transport_type'
375
            .' => Koha::Exceptions::BadParameter');
376
        is ($@->parameter, 'message_transport_types', 'The previous exception '
377
            .' tells us it was the message_transport_types');
378
        like ($@->error, qr/^Patron has not set sms number/, 'Exception '
379
            .' is because of patron has not set sms number.');
380
381
        $schema->storage->txn_rollback;
382
    };
383
384
    subtest 'new message_transport_types' => sub {
385
        plan tests => 3;
386
387
        $schema->storage->txn_begin;
388
389
        my $patron    = build_a_test_patron();
390
        my $letter    = build_a_test_letter();
391
        my $attribute = build_a_test_attribute();
392
        my $mtt       = build_a_test_transport_type();
393
        Koha::Patron::Message::Transport->new({
394
            message_attribute_id   => $attribute->message_attribute_id,
395
            message_transport_type => $mtt->message_transport_type,
396
            is_digest              => 0,
397
            letter_module          => $letter->module,
398
            letter_code            => $letter->code,
399
        })->store;
400
        ok(my $preference = Koha::Patron::Message::Preference->new({
401
            borrowernumber => $patron->borrowernumber,
402
            message_attribute_id => $attribute->message_attribute_id,
403
            wants_digest => 0,
404
            days_in_advance => undef,
405
            message_transport_types => $mtt->message_transport_type,
406
        })->store, 'Added a new messaging preference and transport types to patron.');
407
        ok($preference->message_transport_types->{$mtt->message_transport_type},
408
           'The transport type is stored in the object.');
409
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
410
            borrower_message_preference_id => $preference->borrower_message_preference_id,
411
        });
412
        is($stored_transports->next->message_transport_type, $mtt->message_transport_type,
413
           'The transport type is stored in the database.');
414
415
        $schema->storage->txn_rollback;
416
    };
417
};
418
419
subtest 'Test Koha::Patron::Message::Preference->message_name' => sub {
420
    plan tests => 1;
421
422
    $schema->storage->txn_begin;
423
424
    my $patron      = build_a_test_patron();
425
    my $attribute   = build_a_test_attribute();
426
    my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
427
        patron => $patron,
428
        attr   => $attribute,
429
    });
430
    my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({
431
        borrowernumber => $patron->{'borrowernumber'},
432
        message_name => $attribute->message_name,
433
    })->next;
434
    is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name");
435
436
    $schema->storage->txn_rollback;
437
};
438
439
subtest 'Test adding a new preference with invalid parameters' => sub {
440
    plan tests => 4;
441
442
    subtest 'Missing parameters' => sub {
443
        plan tests => 1;
444
445
        eval { Koha::Patron::Message::Preference->new->store };
446
        is(ref $@, 'Koha::Exceptions::MissingParameter',
447
            'Adding a message preference without parameters'
448
            .' => Koha::Exceptions::MissingParameter');
449
    };
450
451
    subtest 'Too many parameters' => sub {
452
        plan tests => 1;
453
454
        $schema->storage->txn_begin;
455
456
        my $patron = build_a_test_patron();
457
        eval { Koha::Patron::Message::Preference->new({
458
            borrowernumber => $patron->borrowernumber,
459
            categorycode   => $patron->categorycode,
460
        })->store };
461
        is(ref $@, 'Koha::Exceptions::TooManyParameters',
462
            'Adding a message preference for both borrowernumber and categorycode'
463
            .' => Koha::Exceptions::TooManyParameters');
464
465
        $schema->storage->txn_rollback;
466
    };
467
468
    subtest 'Bad parameter' => sub {
469
        plan tests => 22;
470
471
        $schema->storage->txn_begin;
472
473
        eval { Koha::Patron::Message::Preference->new({
474
                borrowernumber => -999,
475
            })->store };
476
        is(ref $@, 'Koha::Exceptions::BadParameter',
477
            'Adding a message preference with invalid borrowernumber'
478
            .' => Koha::Exceptions::BadParameter');
479
        is ($@->parameter, 'borrowernumber', 'The previous exception tells us it'
480
            .' was the borrowernumber.');
481
482
        eval { Koha::Patron::Message::Preference->new({
483
                categorycode => 'nonexistent',
484
            })->store };
485
        is(ref $@, 'Koha::Exceptions::BadParameter',
486
            'Adding a message preference with invalid categorycode'
487
            .' => Koha::Exceptions::BadParameter');
488
        is($@->parameter, 'categorycode', 'The previous exception tells us it'
489
            .' was the categorycode.');
490
491
        my $attribute = build_a_test_attribute({ takes_days => 0 });
492
        my $patron    = build_a_test_patron();
493
        eval { Koha::Patron::Message::Preference->new({
494
                borrowernumber => $patron->borrowernumber,
495
                message_attribute_id => $attribute->message_attribute_id,
496
                days_in_advance => 10,
497
            })->store };
498
        is(ref $@, 'Koha::Exceptions::BadParameter',
499
            'Adding a message preference with days in advance option when not'
500
            .' available => Koha::Exceptions::BadParameter');
501
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
502
            .' was the days_in_advance.');
503
504
        $attribute->set({ takes_days => 1 })->store;
505
        eval { Koha::Patron::Message::Preference->new({
506
                borrowernumber => $patron->borrowernumber,
507
                message_attribute_id => $attribute->message_attribute_id,
508
                days_in_advance => 31,
509
            })->store };
510
        is(ref $@, 'Koha::Exceptions::BadParameter',
511
            'Adding a message preference with days in advance option too large'
512
            .' => Koha::Exceptions::BadParameter');
513
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
514
            .' was the days_in_advance.');
515
516
        eval { Koha::Patron::Message::Preference->new({
517
                borrowernumber => $patron->borrowernumber,
518
                message_transport_types => ['nonexistent']
519
            })->store };
520
        is (ref $@, 'Koha::Exceptions::BadParameter',
521
            'Adding a message preference with invalid message_transport_type'
522
            .' => Koha::Exceptions::BadParameter');
523
        is ($@->parameter, 'message_transport_types', 'The previous exception '
524
            .'tells us it was the message_transport_types.');
525
526
        my $mtt_new = build_a_test_transport_type();
527
        eval {
528
            Koha::Patron::Message::Preference->new({
529
                borrowernumber => $patron->borrowernumber,
530
                message_attribute_id => $attribute->message_attribute_id,
531
                message_transport_types => [$mtt_new->message_transport_type],
532
                wants_digest => 1,
533
            })->store };
534
        is (ref $@, 'Koha::Exceptions::BadParameter',
535
            'Adding a message preference with invalid message_transport_type'
536
           .' => Koha::Exceptions::BadParameter');
537
        is ($@->parameter, 'message_transport_types', 'The previous exception '
538
            .'tells us it was the message_transport_types.');
539
        like ($@->error, qr/^No transport configured/, 'Exception is because of '
540
            .'given message_transport_type is not a valid option.');
541
542
        eval {
543
            Koha::Patron::Message::Preference->new({
544
                borrowernumber => $patron->borrowernumber,
545
                message_attribute_id => $attribute->message_attribute_id,
546
                message_transport_types => [],
547
                wants_digest => 1,
548
            })->store };
549
        is (ref $@, 'Koha::Exceptions::BadParameter',
550
            'Adding a message preference with invalid message_transport_type'
551
            .' => Koha::Exceptions::BadParameter');
552
        is ($@->parameter, 'wants_digest', 'The previous exception tells us it'
553
            .' was the wants_digest');
554
        like ($@->error, qr/^Digest cannot be selected/, 'Exception s because of'
555
            .' given digest is not available for this transport.');
556
557
        eval {
558
            Koha::Patron::Message::Preference->new({
559
                borrowernumber => $patron->borrowernumber,
560
                message_attribute_id => $attribute->message_attribute_id,
561
                message_transport_types => [],
562
                wants_digest => 0,
563
            })->store };
564
        is (ref $@, 'Koha::Exceptions::BadParameter',
565
            'Adding a message preference with invalid message_transport_type'
566
            .' => Koha::Exceptions::BadParameter');
567
        is ($@->parameter, 'wants_digest', 'The previous exception tells us it'
568
            .' was the wants_digest');
569
        like ($@->error, qr/^Digest must be selected/, 'Exception s because of'
570
            .' digest has to be on for this transport.');
571
572
        eval {
573
            Koha::Patron::Message::Preference->new({
574
                borrowernumber => $patron->borrowernumber,
575
                message_attribute_id => -1,
576
                message_transport_types => [],
577
            })->store };
578
        is (ref $@, 'Koha::Exceptions::BadParameter',
579
            'Adding a message preference with invalid message_transport_type'
580
            .' => Koha::Exceptions::BadParameter');
581
        is ($@->parameter, 'message_attribute_id', 'The previous exception tells'
582
            .' us it was the message_attribute_id');
583
        like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception '
584
            .' is because of given message attribute id is not found.');
585
586
        $schema->storage->txn_rollback;
587
    };
588
589
    subtest 'Duplicate object' => sub {
590
        plan tests => 2;
591
592
        $schema->storage->txn_begin;
593
594
        my $attribute = build_a_test_attribute();
595
        my $letter = build_a_test_letter();
596
        my $mtt = build_a_test_transport_type();
597
        Koha::Patron::Message::Transport->new({
598
            message_attribute_id   => $attribute->message_attribute_id,
599
            message_transport_type => $mtt->message_transport_type,
600
            is_digest              => 0,
601
            letter_module          => $letter->module,
602
            letter_code            => $letter->code,
603
        })->store;
604
        my $patron    = build_a_test_patron();
605
        my $preference = Koha::Patron::Message::Preference->new({
606
            borrowernumber => $patron->borrowernumber,
607
            message_attribute_id => $attribute->message_attribute_id,
608
            wants_digest => 0,
609
            days_in_advance => undef,
610
        })->store;
611
        ok($preference->borrower_message_preference_id,
612
           'Added a new messaging preference for patron.');
613
        eval { Koha::Patron::Message::Preference->new({
614
            borrowernumber => $patron->borrowernumber,
615
            message_attribute_id => $attribute->message_attribute_id,
616
            wants_digest => 0,
617
            days_in_advance => undef,
618
        })->store };
619
        is(ref $@, 'Koha::Exceptions::DuplicateObject',
620
                'Adding a duplicate preference'
621
                .' => Koha::Exceptions::DuplicateObject');
622
623
        $schema->storage->txn_rollback;
624
    };
625
};
626
627
sub build_a_test_attribute {
628
    my ($params) = @_;
629
630
    $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0
631
                            ? 1 : 0;
632
633
    my $attribute = $builder->build({
634
        source => 'MessageAttribute',
635
        value => $params,
636
    });
637
638
    return Koha::Patron::Message::Attributes->find(
639
        $attribute->{message_attribute_id}
640
    );
641
}
642
643
sub build_a_test_category {
644
    my $categorycode   = $builder->build({
645
        source => 'Category' })->{categorycode};
646
647
    return Koha::Patron::Categories->find($categorycode);
648
}
649
650
sub build_a_test_letter {
651
    my ($params) = @_;
652
653
    my $mtt = $params->{mtt} ? $params->{mtt} : 'email';
654
    my $branchcode     = $builder->build({
655
        source => 'Branch' })->{branchcode};
656
    my $letter = $builder->build({
657
        source => 'Letter',
658
        value => {
659
            branchcode => '',
660
            is_html => 0,
661
            message_transport_type => $mtt
662
        }
663
    });
664
665
    return Koha::Notice::Templates->find({
666
        module     => $letter->{module},
667
        code       => $letter->{code},
668
        branchcode => $letter->{branchcode},
669
    });
670
}
671
672
sub build_a_test_patron {
673
    my $categorycode   = $builder->build({
674
        source => 'Category' })->{categorycode};
675
    my $branchcode     = $builder->build({
676
        source => 'Branch' })->{branchcode};
677
    my $borrowernumber = $builder->build({
678
        source => 'Borrower' })->{borrowernumber};
679
680
    return Koha::Patrons->find($borrowernumber);
681
}
682
683
sub build_a_test_transport_type {
684
    my $mtt = $builder->build({
685
        source => 'MessageTransportType' });
686
687
    return Koha::Patron::Message::Transport::Types->find(
688
        $mtt->{message_transport_type}
689
    );
690
}
691
692
sub build_a_test_category_preference {
693
    my ($params) = @_;
694
695
    my $patron = $params->{patron};
696
    my $attr = $params->{attr}
697
                    ? $params->{attr}
698
                    : build_a_test_attribute($params->{days_in_advance});
699
700
    my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter();
701
    my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type();
702
    my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type();
703
704
    Koha::Patron::Message::Transport->new({
705
        message_attribute_id   => $attr->message_attribute_id,
706
        message_transport_type => $mtt1->message_transport_type,
707
        is_digest              => $params->{digest} ? 1 : 0,
708
        letter_module          => $letter->module,
709
        letter_code            => $letter->code,
710
    })->store;
711
712
    Koha::Patron::Message::Transport->new({
713
        message_attribute_id   => $attr->message_attribute_id,
714
        message_transport_type => $mtt2->message_transport_type,
715
        is_digest              => $params->{digest} ? 1 : 0,
716
        letter_module          => $letter->module,
717
        letter_code            => $letter->code,
718
    })->store;
719
720
    my $default = Koha::Patron::Message::Preference->new({
721
        categorycode         => $patron->categorycode,
722
        message_attribute_id => $attr->message_attribute_id,
723
        wants_digest         => $params->{digest} ? 1 : 0,
724
        days_in_advance      => $params->{days_in_advance}
725
                                 ? $params->{days_in_advance} : undef,
726
    })->store;
727
728
    Koha::Patron::Message::Transport::Preference->new({
729
        borrower_message_preference_id => $default->borrower_message_preference_id,
730
        message_transport_type         => $mtt1->message_transport_type,
731
    })->store;
732
    Koha::Patron::Message::Transport::Preference->new({
733
        borrower_message_preference_id => $default->borrower_message_preference_id,
734
        message_transport_type         => $mtt2->message_transport_type,
735
    })->store;
736
737
    return ($default, $mtt1, $mtt2);
738
}
739
740
sub build_a_test_complete_preference {
741
    my ($params) = @_;
742
743
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params);
744
    my $patron = $params->{patron};
745
    $patron->set_default_messaging_preferences;
746
    return (Koha::Patron::Message::Preferences->search({
747
        borrowernumber => $patron->borrowernumber
748
    })->next, $mtt1, $mtt2);
749
}
750
751
sub mytempfile {
752
    my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 );
753
    print $fh $_[0]//'';
754
    close $fh;
755
    return $fn;
756
}
757
758
1;
(-)a/t/db_dependent/Koha/Patron/Message/Transport/Preferences.t (+179 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
27
use Koha::Notice::Templates;
28
use Koha::Patron::Categories;
29
use Koha::Patron::Message::Attributes;
30
use Koha::Patron::Message::Preferences;
31
use Koha::Patron::Message::Transport::Types;
32
use Koha::Patron::Message::Transports;
33
use Koha::Patrons;
34
35
my $schema  = Koha::Database->new->schema;
36
my $builder = t::lib::TestBuilder->new;
37
38
subtest 'Test class imports' => sub {
39
    plan tests => 2;
40
41
    use_ok('Koha::Patron::Message::Transport::Preference');
42
    use_ok('Koha::Patron::Message::Transport::Preferences');
43
};
44
45
subtest 'Test Koha::Patron::Message::Transport::Preferences' => sub {
46
    plan tests => 2;
47
48
    $schema->storage->txn_begin;
49
50
    my $attribute = build_a_test_attribute();
51
    my $mtt       = build_a_test_transport_type();
52
    my $letter    = build_a_test_letter({
53
        mtt => $mtt->message_transport_type
54
    });
55
    Koha::Patron::Message::Transport->new({
56
        message_attribute_id   => $attribute->message_attribute_id,
57
        message_transport_type => $mtt->message_transport_type,
58
        is_digest              => 0,
59
        letter_module          => $letter->module,
60
        letter_code            => $letter->code,
61
    })->store;
62
63
    subtest 'For a patron' => sub {
64
        my $patron    = build_a_test_patron();
65
        my $preference = Koha::Patron::Message::Preference->new({
66
            borrowernumber       => $patron->borrowernumber,
67
            message_attribute_id => $attribute->message_attribute_id,
68
            wants_digest         => 0,
69
            days_in_advance      => undef,
70
        })->store;
71
72
        my $pref_id = $preference->borrower_message_preference_id;
73
        my $transport_pref = Koha::Patron::Message::Transport::Preference->new({
74
            borrower_message_preference_id => $pref_id,
75
            message_transport_type => $mtt->message_transport_type,
76
        })->store;
77
        is(ref($transport_pref), 'Koha::Patron::Message::Transport::Preference',
78
           'Added a new messaging transport preference for patron.');
79
80
        $transport_pref->delete;
81
        is(Koha::Patron::Message::Transport::Preferences->search({
82
            borrower_message_preference_id => $pref_id,
83
            message_transport_type => $mtt->message_transport_type,
84
        })->count, 0, 'Deleted the messaging transport preference.');
85
    };
86
87
    subtest 'For a category' => sub {
88
        my $category   = build_a_test_category();
89
        my $preference = Koha::Patron::Message::Preference->new({
90
            categorycode         => $category->categorycode,
91
            message_attribute_id => $attribute->message_attribute_id,
92
            wants_digest         => 0,
93
            days_in_advance      => undef,
94
        })->store;
95
96
        my $pref_id = $preference->borrower_message_preference_id;
97
        my $transport_pref = Koha::Patron::Message::Transport::Preference->new({
98
            borrower_message_preference_id => $pref_id,
99
            message_transport_type => $mtt->message_transport_type,
100
        })->store;
101
        is(ref($transport_pref), 'Koha::Patron::Message::Transport::Preference',
102
           'Added a new messaging transport preference for category.');
103
104
        $transport_pref->delete;
105
        is(Koha::Patron::Message::Transport::Preferences->search({
106
            borrower_message_preference_id => $pref_id,
107
            message_transport_type => $mtt->message_transport_type,
108
        })->count, 0, 'Deleted the messaging transport preference.');
109
    };
110
111
    $schema->storage->txn_rollback;
112
};
113
114
sub build_a_test_attribute {
115
    my ($params) = @_;
116
117
    $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0
118
                            ? 1 : 0;
119
120
    my $attribute = $builder->build({
121
        source => 'MessageAttribute',
122
        value => $params,
123
    });
124
125
    return Koha::Patron::Message::Attributes->find(
126
        $attribute->{message_attribute_id}
127
    );
128
}
129
130
sub build_a_test_category {
131
    my $categorycode   = $builder->build({
132
        source => 'Category' })->{categorycode};
133
134
    return Koha::Patron::Categories->find($categorycode);
135
}
136
137
sub build_a_test_letter {
138
    my ($params) = @_;
139
140
    my $mtt = $params->{mtt} ? $params->{mtt} : 'email';
141
    my $branchcode     = $builder->build({
142
        source => 'Branch' })->{branchcode};
143
    my $letter = $builder->build({
144
        source => 'Letter',
145
        value => {
146
            branchcode => '',
147
            is_html => 0,
148
            message_transport_type => $mtt
149
        }
150
    });
151
152
    return Koha::Notice::Templates->find({
153
        module => $letter->{module},
154
        code   => $letter->{code},
155
        branchcode => $letter->{branchcode},
156
    });
157
}
158
159
sub build_a_test_patron {
160
    my $categorycode   = $builder->build({
161
        source => 'Category' })->{categorycode};
162
    my $branchcode     = $builder->build({
163
        source => 'Branch' })->{branchcode};
164
    my $borrowernumber = $builder->build({
165
        source => 'Borrower' })->{borrowernumber};
166
167
    return Koha::Patrons->find($borrowernumber);
168
}
169
170
sub build_a_test_transport_type {
171
    my $mtt = $builder->build({
172
        source => 'MessageTransportType' });
173
174
    return Koha::Patron::Message::Transport::Types->find(
175
        $mtt->{message_transport_type}
176
    );
177
}
178
179
1;
(-)a/t/db_dependent/Koha/Patron/Message/Transport/Types.t (+54 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
24
use Koha::Database;
25
26
my $schema  = Koha::Database->new->schema;
27
28
subtest 'Test class imports' => sub {
29
    plan tests => 2;
30
31
    use_ok('Koha::Patron::Message::Transport::Type');
32
    use_ok('Koha::Patron::Message::Transport::Types');
33
};
34
35
subtest 'Test Koha::Patron::Message::Transport::Types' => sub {
36
    plan tests => 2;
37
38
    $schema->storage->txn_begin;
39
40
    my $transport_type = Koha::Patron::Message::Transport::Type->new({
41
        message_transport_type => 'test'
42
    })->store;
43
44
    is($transport_type->message_transport_type, 'test',
45
       'Added a new message transport type.');
46
47
    $transport_type->delete;
48
    is(Koha::Patron::Message::Transport::Types->find('test'), undef,
49
       'Deleted the message transport type.');
50
51
    $schema->storage->txn_rollback;
52
};
53
54
1;
(-)a/t/db_dependent/Koha/Patron/Message/Transports.t (-1 / +119 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
24
use t::lib::TestBuilder;
25
26
use Koha::Notice::Templates;
27
use Koha::Patron::Message::Attributes;
28
use Koha::Patron::Message::Transport::Types;
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
subtest 'Test class imports' => sub {
34
    plan tests => 2;
35
36
    use_ok('Koha::Patron::Message::Transport');
37
    use_ok('Koha::Patron::Message::Transports');
38
};
39
40
subtest 'Test Koha::Patron::Message::Transports' => sub {
41
    plan tests => 2;
42
43
    $schema->storage->txn_begin;
44
45
    my $attribute = build_a_test_attribute();
46
    my $mtt       = build_a_test_transport_type();
47
    my $letter    = build_a_test_letter({
48
        mtt => $mtt->message_transport_type
49
    });
50
51
    my $transport = Koha::Patron::Message::Transport->new({
52
        message_attribute_id   => $attribute->message_attribute_id,
53
        message_transport_type => $mtt->message_transport_type,
54
        is_digest              => 0,
55
        letter_module          => $letter->module,
56
        letter_code            => $letter->code,
57
    })->store;
58
59
    is($transport->message_attribute_id, $attribute->message_attribute_id,
60
       'Added a new messaging transport.');
61
62
    $transport->delete;
63
    is(Koha::Patron::Message::Transports->search({
64
        message_attribute_id => $attribute->message_attribute_id,
65
        message_transport_type => $mtt->message_transport_type,
66
        is_digest => 0
67
    })->count, 0, 'Deleted the messaging transport.');
68
69
    $schema->storage->txn_rollback;
70
};
71
72
sub build_a_test_attribute {
73
    my ($params) = @_;
74
75
    $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0
76
                            ? 1 : 0;
77
78
    my $attribute = $builder->build({
79
        source => 'MessageAttribute',
80
        value => $params,
81
    });
82
83
    return Koha::Patron::Message::Attributes->find(
84
        $attribute->{message_attribute_id}
85
    );
86
}
87
88
sub build_a_test_letter {
89
    my ($params) = @_;
90
91
    my $mtt = $params->{mtt} ? $params->{mtt} : 'email';
92
    my $branchcode     = $builder->build({
93
        source => 'Branch' })->{branchcode};
94
    my $letter = $builder->build({
95
        source => 'Letter',
96
        value => {
97
            branchcode => '',
98
            is_html => 0,
99
            message_transport_type => $mtt
100
        }
101
    });
102
103
    return Koha::Notice::Templates->find({
104
        module => $letter->{module},
105
        code   => $letter->{code},
106
        branchcode => $letter->{branchcode},
107
    });
108
}
109
110
sub build_a_test_transport_type {
111
    my $mtt = $builder->build({
112
        source => 'MessageTransportType' });
113
114
    return Koha::Patron::Message::Transport::Types->find(
115
        $mtt->{message_transport_type}
116
    );
117
}
118
119
1;

Return to bug 17499