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

(-)a/Koha/Patron/Message/Preference.pm (+62 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::Patrons;
23
26
24
use base qw(Koha::Object);
27
use base qw(Koha::Object);
25
28
Lines 33-38 Koha::Patron::Message::Preference - Koha Patron Message Preference object class Link Here
33
36
34
=cut
37
=cut
35
38
39
=head3 store
40
41
Makes a validation before actual Koha::Object->store so that proper exceptions
42
can be thrown. See C<validate()> for documentation about exceptions.
43
44
=cut
45
46
sub store {
47
    my $self = shift;
48
49
    return $self->validate->SUPER::store(@_);
50
}
51
52
=head3 validate
53
54
Makes a basic validation for object.
55
56
Throws following exceptions regarding parameters.
57
- Koha::Exceptions::MissingParameter
58
- Koha::Exceptions::TooManyParameters
59
- Koha::Exceptions::BadParameter
60
61
See $_->parameter to identify the parameter causing the exception.
62
63
Returns Koha::Patron::Message::Preference object.
64
65
=cut
66
67
sub validate {
68
    my ($self) = @_;
69
70
    if ($self->borrowernumber && $self->categorycode) {
71
        Koha::Exceptions::TooManyParameters->throw(
72
            error => "Both borrowernumber and category given, only one accepted",
73
            parameter => ['borrowernumber', 'categorycode'],
74
        );
75
    }
76
    if (!$self->borrowernumber && !$self->categorycode) {
77
        Koha::Exceptions::MissingParameter->throw(
78
            error => "borrowernumber or category required, none given",
79
            parameter => ['borrowernumber', 'categorycode'],
80
        );
81
    }
82
    if ($self->borrowernumber) {
83
        Koha::Exceptions::BadParameter->throw(
84
            error => "Patron not found.",
85
            parameter => 'borrowernumber',
86
        ) unless Koha::Patrons->find($self->borrowernumber);
87
    }
88
    if ($self->categorycode) {
89
        Koha::Exceptions::BadParameter->throw(
90
            error => "Category not found.",
91
            parameter => 'categorycode',
92
        ) unless Koha::Patron::Categories->find($self->categorycode);
93
    }
94
95
    return $self;
96
}
97
36
=head3 type
98
=head3 type
37
99
38
=cut
100
=cut
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-2 / +32 lines)
Lines 94-100 subtest 'Add a test messaging transport' => sub { Link Here
94
};
94
};
95
95
96
subtest 'Add a messaging preference to patron' => sub {
96
subtest 'Add a messaging preference to patron' => sub {
97
    plan tests => 2;
97
    plan tests => 3;
98
98
99
    ok($preference = Koha::Patron::Message::Preference->new({
99
    ok($preference = Koha::Patron::Message::Preference->new({
100
        borrowernumber => $patron->{'borrowernumber'},
100
        borrowernumber => $patron->{'borrowernumber'},
Lines 105-110 subtest 'Add a messaging preference to patron' => sub { Link Here
105
    is(Koha::Patron::Message::Preferences->find({
105
    is(Koha::Patron::Message::Preferences->find({
106
        borrowernumber => $patron->{'borrowernumber'} })->message_attribute_id,
106
        borrowernumber => $patron->{'borrowernumber'} })->message_attribute_id,
107
       $preference->message_attribute_id, "Found test messaging preference from database.");
107
       $preference->message_attribute_id, "Found test messaging preference from database.");
108
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
109
        plan tests => 6;
110
111
        eval { Koha::Patron::Message::Preference->new->store };
112
        is (ref $@, "Koha::Exceptions::MissingParameter",
113
            "Adding a message preference without parameters"
114
            ." => Koha::Exceptions::MissingParameter");
115
        eval { Koha::Patron::Message::Preference->new({
116
                borrowernumber => $patron->{'borrowernumber'},
117
                categorycode   => $patron->{'categorycode'},
118
            })->store };
119
        is (ref $@, "Koha::Exceptions::TooManyParameters",
120
            "Adding a message preference with both borrowernumber and categorycode"
121
            ." => Koha::Exceptions::TooManyParameters");
122
        eval { Koha::Patron::Message::Preference->new({
123
                borrowernumber => -999,
124
            })->store };
125
        is (ref $@, "Koha::Exceptions::BadParameter",
126
            "Adding a message preference with invalid borrowernumber"
127
            ." => Koha::Exceptions::BadParameter");
128
        is ($@->parameter, "borrowernumber", "The previous exception tells us it"
129
            ." was the borrowernumber.");
130
        eval { Koha::Patron::Message::Preference->new({
131
                categorycode => "nonexistent",
132
            })->store };
133
        is (ref $@, "Koha::Exceptions::BadParameter",
134
            "Adding a message preference with invalid categorycode"
135
            ." => Koha::Exceptions::BadParameter");
136
        is ($@->parameter, "categorycode", "The previous exception tells us it"
137
            ." was the categorycode.");
138
    };
108
};
139
};
109
140
110
subtest 'Add a messaging transport preference to patron' => sub {
141
subtest 'Add a messaging transport preference to patron' => sub {
111
- 

Return to bug 17499