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

(-)a/Koha/Patron/Message/Preference.pm (+97 lines)
Lines 20-25 package Koha::Patron::Message::Preference; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::Exceptions;
24
use Koha::Patron::Categories;
25
use Koha::Patron::Message::Attributes;
26
use Koha::Patron::Message::Transports;
27
use Koha::Patrons;
23
28
24
use base qw(Koha::Object);
29
use base qw(Koha::Object);
25
30
Lines 33-38 Koha::Patron::Message::Preference - Koha Patron Message Preference object class Link Here
33
38
34
=cut
39
=cut
35
40
41
=head3 store
42
43
Makes a validation before actual Koha::Object->store so that proper exceptions
44
can be thrown. See C<validate()> for documentation about exceptions.
45
46
=cut
47
48
sub store {
49
    my $self = shift;
50
51
    return $self->validate->SUPER::store(@_);
52
}
53
54
=head3 validate
55
56
Makes a basic validation for object.
57
58
Throws following exceptions regarding parameters.
59
- Koha::Exceptions::MissingParameter
60
- Koha::Exceptions::TooManyParameters
61
- Koha::Exceptions::BadParameter
62
63
See $_->parameter to identify the parameter causing the exception.
64
65
Returns Koha::Patron::Message::Preference object.
66
67
=cut
68
69
sub validate {
70
    my ($self) = @_;
71
72
    if ($self->borrowernumber && $self->categorycode) {
73
        Koha::Exceptions::TooManyParameters->throw(
74
            error => 'Both borrowernumber and category given, only one accepted',
75
            parameter => ['borrowernumber', 'categorycode'],
76
        );
77
    }
78
    if (!$self->borrowernumber && !$self->categorycode) {
79
        Koha::Exceptions::MissingParameter->throw(
80
            error => 'borrowernumber or category required, none given',
81
            parameter => ['borrowernumber', 'categorycode'],
82
        );
83
    }
84
    if ($self->borrowernumber) {
85
        Koha::Exceptions::BadParameter->throw(
86
            error => 'Patron not found.',
87
            parameter => 'borrowernumber',
88
        ) unless Koha::Patrons->find($self->borrowernumber);
89
    }
90
    if ($self->categorycode) {
91
        Koha::Exceptions::BadParameter->throw(
92
            error => 'Category not found.',
93
            parameter => 'categorycode',
94
        ) unless Koha::Patron::Categories->find($self->categorycode);
95
    }
96
97
    my $attr;
98
    if ($self->days_in_advance || $self->wants_digest) {
99
        $attr = Koha::Patron::Message::Attributes->find(
100
            $self->message_attribute_id
101
        );
102
    }
103
    if ($self->days_in_advance) {
104
        if ($attr && $attr->takes_days == 0) {
105
            Koha::Exceptions::BadParameter->throw(
106
                error => 'days_in_advance cannot be defined for '.
107
                $attr->message_name . ' .',
108
                parameter => 'days_in_advance',
109
            );
110
        }
111
        elsif ($self->days_in_advance < 0 || $self->days_in_advance > 30) {
112
            Koha::Exceptions::BadParameter->throw(
113
                error => 'days_in_advance has to be a value between 0-30 for '.
114
                $attr->message_name . ' .',
115
                parameter => 'days_in_advance',
116
            );
117
        }
118
    }
119
    if ($self->wants_digest) {
120
        my $transports = Koha::Patron::Message::Transports->search({
121
            message_attribute_id => $self->message_attribute_id,
122
            is_digest            => 1,
123
        });
124
        Koha::Exceptions::BadParameter->throw(
125
            error => 'Digest not available for '.$attr->message_name.' .',
126
            parameter => 'wants_digest',
127
        ) if $transports->count == 0;
128
    }
129
130
    return $self;
131
}
132
36
=head3 type
133
=head3 type
37
134
38
=cut
135
=cut
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-2 / +82 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
23
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
Lines 99-104 subtest 'Test Koha::Patron::Message::Preferences' => sub { Link Here
99
    $schema->storage->txn_rollback;
99
    $schema->storage->txn_rollback;
100
};
100
};
101
101
102
subtest 'Test adding a new preference with invalid parameters' => sub {
103
    plan tests => 3;
104
105
    subtest 'Missing parameters' => sub {
106
        plan tests => 1;
107
108
        eval { Koha::Patron::Message::Preference->new->store };
109
        is(ref $@, 'Koha::Exceptions::MissingParameter',
110
            'Adding a message preference without parameters'
111
            .' => Koha::Exceptions::MissingParameter');
112
    };
113
114
    subtest 'Too many parameters' => sub {
115
        plan tests => 1;
116
117
        $schema->storage->txn_begin;
118
119
        my $patron = build_a_test_patron();
120
        eval { Koha::Patron::Message::Preference->new({
121
            borrowernumber => $patron->borrowernumber,
122
            categorycode   => $patron->categorycode,
123
        })->store };
124
        is(ref $@, 'Koha::Exceptions::TooManyParameters',
125
            'Adding a message preference for both borrowernumber and categorycode'
126
            .' => Koha::Exceptions::TooManyParameters');
127
128
        $schema->storage->txn_rollback;
129
    };
130
131
    subtest 'Bad parameter' => sub {
132
        plan tests => 8;
133
134
        $schema->storage->txn_begin;
135
136
        eval { Koha::Patron::Message::Preference->new({
137
                borrowernumber => -999,
138
            })->store };
139
        is(ref $@, 'Koha::Exceptions::BadParameter',
140
            'Adding a message preference with invalid borrowernumber'
141
            .' => Koha::Exceptions::BadParameter');
142
        is ($@->parameter, 'borrowernumber', 'The previous exception tells us it'
143
            .' was the borrowernumber.');
144
145
        eval { Koha::Patron::Message::Preference->new({
146
                categorycode => 'nonexistent',
147
            })->store };
148
        is(ref $@, 'Koha::Exceptions::BadParameter',
149
            'Adding a message preference with invalid categorycode'
150
            .' => Koha::Exceptions::BadParameter');
151
        is($@->parameter, 'categorycode', 'The previous exception tells us it'
152
            .' was the categorycode.');
153
154
        my $attribute = build_a_test_attribute({ takes_days => 0 });
155
        my $patron    = build_a_test_patron();
156
        eval { Koha::Patron::Message::Preference->new({
157
                borrowernumber => $patron->borrowernumber,
158
                message_attribute_id => $attribute->message_attribute_id,
159
                days_in_advance => 10,
160
            })->store };
161
        is(ref $@, 'Koha::Exceptions::BadParameter',
162
            'Adding a message preference with days in advance option when not'
163
            .' available => Koha::Exceptions::BadParameter');
164
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
165
            .' was the days_in_advance.');
166
167
        $attribute->set({ takes_days => 1 })->store;
168
        eval { Koha::Patron::Message::Preference->new({
169
                borrowernumber => $patron->borrowernumber,
170
                message_attribute_id => $attribute->message_attribute_id,
171
                days_in_advance => 31,
172
            })->store };
173
        is(ref $@, 'Koha::Exceptions::BadParameter',
174
            'Adding a message preference with days in advance option too large'
175
            .' => Koha::Exceptions::BadParameter');
176
        is($@->parameter, 'days_in_advance', 'The previous exception tells us it'
177
            .' was the days_in_advance.');
178
179
        $schema->storage->txn_rollback;
180
    };
181
};
182
102
sub build_a_test_attribute {
183
sub build_a_test_attribute {
103
    my ($params) = @_;
184
    my ($params) = @_;
104
185
105
- 

Return to bug 17499