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

(-)a/Koha/Exceptions.pm (-1 / +7 lines)
Lines 27-33 use Exception::Class ( Link Here
27
    },
27
    },
28
    'Koha::Exceptions::MissingParameter' => {
28
    'Koha::Exceptions::MissingParameter' => {
29
        isa => 'Koha::Exceptions::Exception',
29
        isa => 'Koha::Exceptions::Exception',
30
        description => 'A required parameter is missing'
30
        description => 'A required parameter is missing',
31
        fields => ['parameter'],
32
    },
33
    'Koha::Exceptions::TooManyParameters' => {
34
        isa => 'Koha::Exceptions::Exception',
35
        description => 'Too many parameters given',
36
        fields => ['parameter'],
31
    },
37
    },
32
    'Koha::Exceptions::WrongParameter' => {
38
    'Koha::Exceptions::WrongParameter' => {
33
        isa => 'Koha::Exceptions::Exception',
39
        isa => 'Koha::Exceptions::Exception',
(-)a/Koha/Patron.pm (+42 lines)
Lines 33-38 use Koha::Patron::Categories; Link Here
33
use Koha::Patron::HouseboundProfile;
33
use Koha::Patron::HouseboundProfile;
34
use Koha::Patron::HouseboundRole;
34
use Koha::Patron::HouseboundRole;
35
use Koha::Patron::Images;
35
use Koha::Patron::Images;
36
use Koha::Patron::Message::Preferences;
36
use Koha::Patrons;
37
use Koha::Patrons;
37
use Koha::Virtualshelves;
38
use Koha::Virtualshelves;
38
use Koha::Club::Enrollments;
39
use Koha::Club::Enrollments;
Lines 656-661 sub account_locked { Link Here
656
          and $self->login_attempts >= $FailedLoginAttempts )? 1 : 0;
657
          and $self->login_attempts >= $FailedLoginAttempts )? 1 : 0;
657
}
658
}
658
659
660
=head3 set_default_messaging_preferences
661
662
    $patron->set_default_messaging_preferences
663
664
Sets default messaging preferences on patron.
665
666
See Koha::Patron::Message::Preference(s) for more documentation, especially on
667
thrown exceptions.
668
669
=cut
670
671
sub set_default_messaging_preferences {
672
    my ($self, $categorycode) = @_;
673
674
    my $options = Koha::Patron::Message::Preferences->get_options;
675
676
    foreach my $option (@$options) {
677
        # Check that this option has preference configuration for this category
678
        unless (Koha::Patron::Message::Preferences->search({
679
            message_attribute_id => $option->{message_attribute_id},
680
            categorycode         => $categorycode || $self->categorycode,
681
        })->count) {
682
            next;
683
        }
684
685
        # Delete current setting
686
        Koha::Patron::Message::Preferences->search({
687
            borrowernumber => $self->borrowernumber,
688
             message_attribute_id => $option->{message_attribute_id},
689
        })->delete;
690
691
        Koha::Patron::Message::Preference->new_from_default({
692
            borrowernumber => $self->borrowernumber,
693
            categorycode   => $categorycode || $self->categorycode,
694
            message_attribute_id => $option->{message_attribute_id},
695
        });
696
    }
697
698
    return $self;
699
}
700
659
=head3 type
701
=head3 type
660
702
661
=cut
703
=cut
(-)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@jns.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@jns.fi>
52
53
=cut
54
55
1;
(-)a/Koha/Patron/Message/Preference.pm (+451 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
            $self->{'_message_transport_types'}->{$type}
408
                = $transport->letter_code;
409
        }
410
    }
411
    return $self;
412
}
413
414
sub _validate_message_transport_types {
415
    my ($self, $params) = @_;
416
417
    if (ref($params) eq 'HASH' && $params->{'message_transport_types'}) {
418
        if (ref($params->{'message_transport_types'}) ne 'ARRAY') {
419
            $params->{'message_transport_types'} = [$params->{'message_transport_types'}];
420
        }
421
        my $types = $params->{'message_transport_types'};
422
423
        foreach my $type (@{$types}) {
424
            unless (Koha::Patron::Message::Transport::Types->find({
425
                message_transport_type => $type
426
            })) {
427
                Koha::Exceptions::BadParameter->throw(
428
                    error => "Message transport type '$type' does not exist",
429
                    parameter => 'message_transport_types',
430
                );
431
            }
432
        }
433
        return $types;
434
    }
435
}
436
437
=head3 type
438
439
=cut
440
441
sub _type {
442
    return 'BorrowerMessagePreference';
443
}
444
445
=head1 AUTHOR
446
447
Lari Taskula <lari.taskula@jns.fi>
448
449
=cut
450
451
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@jns.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@jns.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@jns.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@jns.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@jns.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@jns.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@jns.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 (+719 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 => 6;
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
        $schema->storage->txn_rollback;
343
    };
344
345
    subtest 'new message_transport_types' => sub {
346
        plan tests => 3;
347
348
        $schema->storage->txn_begin;
349
350
        my $patron    = build_a_test_patron();
351
        my $letter    = build_a_test_letter();
352
        my $attribute = build_a_test_attribute();
353
        my $mtt       = build_a_test_transport_type();
354
        Koha::Patron::Message::Transport->new({
355
            message_attribute_id   => $attribute->message_attribute_id,
356
            message_transport_type => $mtt->message_transport_type,
357
            is_digest              => 0,
358
            letter_module          => $letter->module,
359
            letter_code            => $letter->code,
360
        })->store;
361
        ok(my $preference = Koha::Patron::Message::Preference->new({
362
            borrowernumber => $patron->borrowernumber,
363
            message_attribute_id => $attribute->message_attribute_id,
364
            wants_digest => 0,
365
            days_in_advance => undef,
366
            message_transport_types => $mtt->message_transport_type,
367
        })->store, 'Added a new messaging preference and transport types to patron.');
368
        ok($preference->message_transport_types->{$mtt->message_transport_type},
369
           'The transport type is stored in the object.');
370
        my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({
371
            borrower_message_preference_id => $preference->borrower_message_preference_id,
372
        });
373
        is($stored_transports->next->message_transport_type, $mtt->message_transport_type,
374
           'The transport type is stored in the database.');
375
376
        $schema->storage->txn_rollback;
377
    };
378
};
379
380
subtest 'Test Koha::Patron::Message::Preference->message_name' => sub {
381
    plan tests => 1;
382
383
    $schema->storage->txn_begin;
384
385
    my $patron      = build_a_test_patron();
386
    my $attribute   = build_a_test_attribute();
387
    my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
388
        patron => $patron,
389
        attr   => $attribute,
390
    });
391
    my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({
392
        borrowernumber => $patron->{'borrowernumber'},
393
        message_name => $attribute->message_name,
394
    })->next;
395
    is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name");
396
397
    $schema->storage->txn_rollback;
398
};
399
400
subtest 'Test adding a new preference with invalid parameters' => sub {
401
    plan tests => 4;
402
403
    subtest 'Missing parameters' => sub {
404
        plan tests => 1;
405
406
        eval { Koha::Patron::Message::Preference->new->store };
407
        is(ref $@, 'Koha::Exceptions::MissingParameter',
408
            'Adding a message preference without parameters'
409
            .' => Koha::Exceptions::MissingParameter');
410
    };
411
412
    subtest 'Too many parameters' => sub {
413
        plan tests => 1;
414
415
        $schema->storage->txn_begin;
416
417
        my $patron = build_a_test_patron();
418
        eval { Koha::Patron::Message::Preference->new({
419
            borrowernumber => $patron->borrowernumber,
420
            categorycode   => $patron->categorycode,
421
        })->store };
422
        is(ref $@, 'Koha::Exceptions::TooManyParameters',
423
            'Adding a message preference for both borrowernumber and categorycode'
424
            .' => Koha::Exceptions::TooManyParameters');
425
426
        $schema->storage->txn_rollback;
427
    };
428
429
    subtest 'Bad parameter' => sub {
430
        plan tests => 22;
431
432
        $schema->storage->txn_begin;
433
434
        eval { Koha::Patron::Message::Preference->new({
435
                borrowernumber => -999,
436
            })->store };
437
        is(ref $@, 'Koha::Exceptions::BadParameter',
438
            'Adding a message preference with invalid borrowernumber'
439
            .' => Koha::Exceptions::BadParameter');
440
        is ($@->parameter, 'borrowernumber', 'The previous exception tells us it'
441
            .' was the borrowernumber.');
442
443
        eval { Koha::Patron::Message::Preference->new({
444
                categorycode => 'nonexistent',
445
            })->store };
446
        is(ref $@, 'Koha::Exceptions::BadParameter',
447
            'Adding a message preference with invalid categorycode'
448
            .' => Koha::Exceptions::BadParameter');
449
        is($@->parameter, 'categorycode', 'The previous exception tells us it'
450
            .' was the categorycode.');
451
452
        my $attribute = build_a_test_attribute({ takes_days => 0 });
453
        my $patron    = build_a_test_patron();
454
        eval { Koha::Patron::Message::Preference->new({
455
                borrowernumber => $patron->borrowernumber,
456
                message_attribute_id => $attribute->message_attribute_id,
457
                days_in_advance => 10,
458
            })->store };
459
        is(ref $@, 'Koha::Exceptions::BadParameter',
460
            'Adding a message preference with days in advance option when not'
461
            .' available => Koha::Exceptions::BadParameter');
462
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
463
            .' was the days_in_advance.');
464
465
        $attribute->set({ takes_days => 1 })->store;
466
        eval { Koha::Patron::Message::Preference->new({
467
                borrowernumber => $patron->borrowernumber,
468
                message_attribute_id => $attribute->message_attribute_id,
469
                days_in_advance => 31,
470
            })->store };
471
        is(ref $@, 'Koha::Exceptions::BadParameter',
472
            'Adding a message preference with days in advance option too large'
473
            .' => Koha::Exceptions::BadParameter');
474
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
475
            .' was the days_in_advance.');
476
477
        eval { Koha::Patron::Message::Preference->new({
478
                borrowernumber => $patron->borrowernumber,
479
                message_transport_types => ['nonexistent']
480
            })->store };
481
        is (ref $@, 'Koha::Exceptions::BadParameter',
482
            'Adding a message preference with invalid message_transport_type'
483
            .' => Koha::Exceptions::BadParameter');
484
        is ($@->parameter, 'message_transport_types', 'The previous exception '
485
            .'tells us it was the message_transport_types.');
486
487
        my $mtt_new = build_a_test_transport_type();
488
        eval {
489
            Koha::Patron::Message::Preference->new({
490
                borrowernumber => $patron->borrowernumber,
491
                message_attribute_id => $attribute->message_attribute_id,
492
                message_transport_types => [$mtt_new->message_transport_type],
493
                wants_digest => 1,
494
            })->store };
495
        is (ref $@, 'Koha::Exceptions::BadParameter',
496
            'Adding a message preference with invalid message_transport_type'
497
           .' => Koha::Exceptions::BadParameter');
498
        is ($@->parameter, 'message_transport_types', 'The previous exception '
499
            .'tells us it was the message_transport_types.');
500
        like ($@->error, qr/^No transport configured/, 'Exception is because of '
501
            .'given message_transport_type is not a valid option.');
502
503
        eval {
504
            Koha::Patron::Message::Preference->new({
505
                borrowernumber => $patron->borrowernumber,
506
                message_attribute_id => $attribute->message_attribute_id,
507
                message_transport_types => [],
508
                wants_digest => 1,
509
            })->store };
510
        is (ref $@, 'Koha::Exceptions::BadParameter',
511
            'Adding a message preference with invalid message_transport_type'
512
            .' => Koha::Exceptions::BadParameter');
513
        is ($@->parameter, 'wants_digest', 'The previous exception tells us it'
514
            .' was the wants_digest');
515
        like ($@->error, qr/^Digest cannot be selected/, 'Exception s because of'
516
            .' given digest is not available for this transport.');
517
518
        eval {
519
            Koha::Patron::Message::Preference->new({
520
                borrowernumber => $patron->borrowernumber,
521
                message_attribute_id => $attribute->message_attribute_id,
522
                message_transport_types => [],
523
                wants_digest => 0,
524
            })->store };
525
        is (ref $@, 'Koha::Exceptions::BadParameter',
526
            'Adding a message preference with invalid message_transport_type'
527
            .' => Koha::Exceptions::BadParameter');
528
        is ($@->parameter, 'wants_digest', 'The previous exception tells us it'
529
            .' was the wants_digest');
530
        like ($@->error, qr/^Digest must be selected/, 'Exception s because of'
531
            .' digest has to be on for this transport.');
532
533
        eval {
534
            Koha::Patron::Message::Preference->new({
535
                borrowernumber => $patron->borrowernumber,
536
                message_attribute_id => -1,
537
                message_transport_types => [],
538
            })->store };
539
        is (ref $@, 'Koha::Exceptions::BadParameter',
540
            'Adding a message preference with invalid message_transport_type'
541
            .' => Koha::Exceptions::BadParameter');
542
        is ($@->parameter, 'message_attribute_id', 'The previous exception tells'
543
            .' us it was the message_attribute_id');
544
        like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception '
545
            .' is because of given message attribute id is not found.');
546
547
        $schema->storage->txn_rollback;
548
    };
549
550
    subtest 'Duplicate object' => sub {
551
        plan tests => 2;
552
553
        $schema->storage->txn_begin;
554
555
        my $attribute = build_a_test_attribute();
556
        my $letter = build_a_test_letter();
557
        my $mtt = build_a_test_transport_type();
558
        Koha::Patron::Message::Transport->new({
559
            message_attribute_id   => $attribute->message_attribute_id,
560
            message_transport_type => $mtt->message_transport_type,
561
            is_digest              => 0,
562
            letter_module          => $letter->module,
563
            letter_code            => $letter->code,
564
        })->store;
565
        my $patron    = build_a_test_patron();
566
        my $preference = Koha::Patron::Message::Preference->new({
567
            borrowernumber => $patron->borrowernumber,
568
            message_attribute_id => $attribute->message_attribute_id,
569
            wants_digest => 0,
570
            days_in_advance => undef,
571
        })->store;
572
        ok($preference->borrower_message_preference_id,
573
           'Added a new messaging preference for patron.');
574
        eval { Koha::Patron::Message::Preference->new({
575
            borrowernumber => $patron->borrowernumber,
576
            message_attribute_id => $attribute->message_attribute_id,
577
            wants_digest => 0,
578
            days_in_advance => undef,
579
        })->store };
580
        is(ref $@, 'Koha::Exceptions::DuplicateObject',
581
                'Adding a duplicate preference'
582
                .' => Koha::Exceptions::DuplicateObject');
583
584
        $schema->storage->txn_rollback;
585
    };
586
};
587
588
sub build_a_test_attribute {
589
    my ($params) = @_;
590
591
    $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0
592
                            ? 1 : 0;
593
594
    my $attribute = $builder->build({
595
        source => 'MessageAttribute',
596
        value => $params,
597
    });
598
599
    return Koha::Patron::Message::Attributes->find(
600
        $attribute->{message_attribute_id}
601
    );
602
}
603
604
sub build_a_test_category {
605
    my $categorycode   = $builder->build({
606
        source => 'Category' })->{categorycode};
607
608
    return Koha::Patron::Categories->find($categorycode);
609
}
610
611
sub build_a_test_letter {
612
    my ($params) = @_;
613
614
    my $mtt = $params->{mtt} ? $params->{mtt} : 'email';
615
    my $branchcode     = $builder->build({
616
        source => 'Branch' })->{branchcode};
617
    my $letter = $builder->build({
618
        source => 'Letter',
619
        value => {
620
            branchcode => '',
621
            is_html => 0,
622
            message_transport_type => $mtt
623
        }
624
    });
625
626
    return Koha::Notice::Templates->find({
627
        module     => $letter->{module},
628
        code       => $letter->{code},
629
        branchcode => $letter->{branchcode},
630
    });
631
}
632
633
sub build_a_test_patron {
634
    my $categorycode   = $builder->build({
635
        source => 'Category' })->{categorycode};
636
    my $branchcode     = $builder->build({
637
        source => 'Branch' })->{branchcode};
638
    my $borrowernumber = $builder->build({
639
        source => 'Borrower' })->{borrowernumber};
640
641
    return Koha::Patrons->find($borrowernumber);
642
}
643
644
sub build_a_test_transport_type {
645
    my $mtt = $builder->build({
646
        source => 'MessageTransportType' });
647
648
    return Koha::Patron::Message::Transport::Types->find(
649
        $mtt->{message_transport_type}
650
    );
651
}
652
653
sub build_a_test_category_preference {
654
    my ($params) = @_;
655
656
    my $patron = $params->{patron};
657
    my $attr = $params->{attr}
658
                    ? $params->{attr}
659
                    : build_a_test_attribute($params->{days_in_advance});
660
661
    my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter();
662
    my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type();
663
    my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type();
664
665
    Koha::Patron::Message::Transport->new({
666
        message_attribute_id   => $attr->message_attribute_id,
667
        message_transport_type => $mtt1->message_transport_type,
668
        is_digest              => $params->{digest} ? 1 : 0,
669
        letter_module          => $letter->module,
670
        letter_code            => $letter->code,
671
    })->store;
672
673
    Koha::Patron::Message::Transport->new({
674
        message_attribute_id   => $attr->message_attribute_id,
675
        message_transport_type => $mtt2->message_transport_type,
676
        is_digest              => $params->{digest} ? 1 : 0,
677
        letter_module          => $letter->module,
678
        letter_code            => $letter->code,
679
    })->store;
680
681
    my $default = Koha::Patron::Message::Preference->new({
682
        categorycode         => $patron->categorycode,
683
        message_attribute_id => $attr->message_attribute_id,
684
        wants_digest         => $params->{digest} ? 1 : 0,
685
        days_in_advance      => $params->{days_in_advance}
686
                                 ? $params->{days_in_advance} : undef,
687
    })->store;
688
689
    Koha::Patron::Message::Transport::Preference->new({
690
        borrower_message_preference_id => $default->borrower_message_preference_id,
691
        message_transport_type         => $mtt1->message_transport_type,
692
    })->store;
693
    Koha::Patron::Message::Transport::Preference->new({
694
        borrower_message_preference_id => $default->borrower_message_preference_id,
695
        message_transport_type         => $mtt2->message_transport_type,
696
    })->store;
697
698
    return ($default, $mtt1, $mtt2);
699
}
700
701
sub build_a_test_complete_preference {
702
    my ($params) = @_;
703
704
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params);
705
    my $patron = $params->{patron};
706
    $patron->set_default_messaging_preferences;
707
    return (Koha::Patron::Message::Preferences->search({
708
        borrowernumber => $patron->borrowernumber
709
    })->next, $mtt1, $mtt2);
710
}
711
712
sub mytempfile {
713
    my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 );
714
    print $fh $_[0]//'';
715
    close $fh;
716
    return $fn;
717
}
718
719
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