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

(-)a/Koha/SMTP/Server.pm (+128 lines)
Line 0 Link Here
1
package Koha::SMTP::Server;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Database;
21
use Koha::Exceptions::Object;
22
use Koha::SMTP::Servers;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::SMTP::Server - Koha SMTP Server Object class
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 store
35
36
    my $server = Koha::SMTP::Server->new({...})->store;
37
    $server->set({...})->store;
38
39
Overloaded I<store> method that takes care of checking unicity of the
40
default server (i.e. when library_id IS NULL).
41
42
FIXME: Replace with a CHECK constraint when the feature is available.
43
44
=cut
45
46
sub store {
47
    my ( $self, $params ) = @_;
48
49
    if ( $self->in_storage ) {
50
51
        # update
52
        if ( !defined $self->library_id ) {
53
54
            # trying to set a default, check manually
55
            # until the DB can handle it
56
            Koha::Exceptions::Object::DuplicateID->throw( duplicate_id => 'library_id' )
57
              if Koha::SMTP::Servers->search(
58
                { library_id => undef, id => { '!=' => $self->id } } )->count > 0;
59
        }
60
    }
61
    else {
62
        # new
63
        if ( !defined $self->library_id ) {
64
65
            # trying to set a default, check manually
66
            # until the DB can handle it
67
            Koha::Exceptions::Object::DuplicateID->throw( duplicate_id => 'library_id' )
68
              if Koha::SMTP::Servers->search( { library_id => undef } )->count > 0;
69
        }
70
    }
71
72
    return $self->SUPER::store;
73
}
74
75
=head3 is_default
76
77
    my $is_default = $server->is_default
78
79
Helper method to determine if the server is the default one
80
81
=cut
82
83
sub is_default {
84
    my ($self) = @_;
85
86
    return (defined $self->library_id) ? 0 : 1;
87
}
88
89
=head3 library
90
91
Returns the Koha::Library object linked to the SMTP server.
92
93
=cut
94
95
sub library {
96
    my ( $self ) = @_;
97
98
    my $library_rs = $self->_result->library;
99
    return unless $library_rs;
100
    return Koha::Library->_new_from_dbic($self->_result->library);
101
}
102
103
=head3 to_api_mapping
104
105
This method returns the mapping for representing a Koha::SMTP::Server object
106
on the API.
107
108
=cut
109
110
sub to_api_mapping {
111
    return {
112
        id => 'smtp_server_id'
113
    };
114
}
115
116
=head2 Internal methods
117
118
=head3 _type
119
120
Return type of Object relating to Schema ResultSet
121
122
=cut
123
124
sub _type {
125
    return 'SmtpServer';
126
}
127
128
1;
(-)a/Koha/SMTP/Servers.pm (+191 lines)
Line 0 Link Here
1
package Koha::SMTP::Servers;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Database;
21
use Koha::Exceptions;
22
23
use Koha::SMTP::Server;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::SMTP::Servers - Koha SMTP Server Object set class
30
31
=head1 API
32
33
=head2 Class methods
34
35
=head3 get_effective_server
36
37
    my $server = Koha::SMTP::Servers->get_effective_server({ library => $library });
38
39
Returns a I<Koha::SMTP::Server> representing the effective configured SMTP for the library.
40
41
=cut
42
43
sub get_effective_server {
44
    my ($self, $args) = @_;
45
46
    my $library = $args->{library};
47
48
    Koha::Exceptions::MissingParameter->throw('Mandatory parameter missing: library')
49
        unless $library;
50
51
    my $servers_rs = Koha::SMTP::Servers->search({ library_id => $library->branchcode });
52
    if ( $servers_rs->count > 0 ) {
53
        return $servers_rs->next;
54
    }
55
56
    return $self->get_default;
57
}
58
59
=head3 get_default
60
61
    my $server = Koha::SMTP::Servers->new->get_default;
62
63
Returns the default I<Koha::SMTP::Server> object.
64
65
=cut
66
67
sub get_default {
68
    my ($self) = @_;
69
70
    my $servers_rs = $self->search({ library_id => undef });
71
72
    my $server;
73
    if ($servers_rs->count > 0) {
74
        $server = $servers_rs->next;
75
    }
76
    else {
77
        $server = Koha::SMTP::Server->new( $self->default_setting );
78
    }
79
80
    return $server;
81
}
82
83
=head3 set_default
84
85
    my $server = Koha::SMTP::Servers->new->set_default;
86
87
Set the default I<Koha::SMTP::Server> server, and returns it.
88
89
=cut
90
91
sub set_default {
92
    my ($self, $params) = @_;
93
94
    Koha::Exceptions::BadParameter->throw( 'library_id must be undef when setting the default SMTP server' )
95
        if defined $params->{library_id};
96
97
    my $smtp_server;
98
    $self->_resultset()->result_source->schema->txn_do( sub {
99
100
        # Delete existing default
101
        $self->search({ library_id => undef })->delete;
102
103
       $smtp_server = Koha::SMTP::Server->new($params)->store;
104
    });
105
106
    return $smtp_server;
107
}
108
109
=head3 set_library_server
110
111
    my $server = Koha::SMTP::Servers->new->set_library_server(
112
        {
113
            name       => $name,
114
            library_id => $library->id,
115
            host       => $smtp_host,
116
            port       => $smtp_port,
117
            timeout    => $smtp_timeout,
118
            ssl        => 1,
119
            user_name  => $user_name,
120
            password   => $password
121
        }
122
    );
123
124
Set the I<Koha::SMTP::Server> server for a library, and return it.
125
126
=cut
127
128
sub set_library_server {
129
    my ( $self, $params ) = @_;
130
131
    Koha::Exceptions::MissingParameter->throw(
132
        'Mandatory parameter missing: library_id')
133
      unless $params->{library_id};
134
135
    my $smtp_server;
136
    $self->_resultset()->result_source->schema->txn_do( sub {
137
        # Delete existing default
138
        $self->search({ library_id => $params->{library_id} })->delete;
139
140
        $smtp_server = Koha::SMTP::Server->new($params)->store;
141
    });
142
143
    return $smtp_server;
144
}
145
146
=head2 Internal methods
147
148
=head3 _type
149
150
Return type of object, relating to Schema ResultSet
151
152
=cut
153
154
sub _type {
155
    return 'SmtpServer';
156
}
157
158
=head3 default_setting
159
160
    my $hash = Koha::SMTP::Servers::default_setting;
161
162
Returns the default setting that is to be used when no user-defined default
163
SMTP server is provided
164
165
=cut
166
167
sub default_setting {
168
    return {
169
        name       => 'localhost',
170
        library_id => undef,
171
        host       => 'localhost',
172
        port       => 25,
173
        timeout    => 120,
174
        ssl_mode  => 'disabled',
175
        user_name  => undef,
176
        password   => undef,
177
        debug      => 0
178
    };
179
}
180
181
=head3 object_class
182
183
Return object class
184
185
=cut
186
187
sub object_class {
188
    return 'Koha::SMTP::Server';
189
}
190
191
1;
(-)a/t/db_dependent/Koha/SMTP/Server.t (+120 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
use Test::Exception;
22
use Test::Warn;
23
24
use Koha::SMTP::Servers;
25
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
subtest 'is_default() tests' => sub {
33
34
    plan tests => 2;
35
36
    $schema->storage->txn_begin;
37
38
    Koha::SMTP::Servers->search->delete;
39
40
    my $default_server = $builder->build_object(
41
        {
42
            class => 'Koha::SMTP::Servers',
43
            value => {
44
                library_id => undef
45
            }
46
        }
47
    );
48
49
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
50
    my $library_specific_server = $builder->build_object(
51
        {
52
            class => 'Koha::SMTP::Servers',
53
            value => {
54
                library_id => $library->branchcode
55
            }
56
        }
57
    );
58
59
    ok( $default_server->is_default, 'Server is the default one' );
60
    ok( !$library_specific_server->is_default,
61
        'Server is not the default one' );
62
63
    $schema->storage->txn_rollback;
64
};
65
66
subtest 'store() tests' => sub {
67
68
    plan tests => 4;
69
70
    $schema->storage->txn_begin;
71
72
    Koha::SMTP::Servers->search->delete;
73
74
    my $default_server = Koha::SMTP::Server->new(
75
        {
76
            library_id => undef,
77
            name       => 'Default SMTP server'
78
        }
79
    )->store;
80
81
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
82
    my $library_specific_server = Koha::SMTP::Server->new(
83
        {
84
            library_id => $library->id,
85
            name       => 'Library-specific SMTP server'
86
        }
87
    )->store;
88
89
    warning_like
90
        {
91
            throws_ok
92
                { Koha::SMTP::Server->new(
93
                    {
94
                        library_id => $library->id,
95
                        name       => 'Some fake name'
96
                    }
97
                )->store; }
98
                'Koha::Exceptions::Object::DuplicateID',
99
                'Cannot define two servers for the same library';
100
        }
101
        qr/DBI Exception: DBD::mysql::st execute failed: Duplicate entry/,
102
        'Warning is printed';
103
104
    throws_ok
105
        { Koha::SMTP::Server->new(
106
            {
107
                library_id => undef,
108
                name       => 'Some fake name'
109
            }
110
        )->store; }
111
        'Koha::Exceptions::Object::DuplicateID',
112
        'Cannot define two default SMTP servers';
113
114
    $default_server->set({ name => 'New name' })->store;
115
    $default_server->discard_changes;
116
117
    is( $default_server->name, 'New name', 'Default server updated correctly' );
118
119
    $schema->storage->txn_rollback;
120
};
(-)a/t/db_dependent/Koha/SMTP/Servers.t (-1 / +258 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 4;
21
use Test::Exception;
22
23
use Koha::SMTP::Servers;
24
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
subtest 'get_default() tests' => sub {
32
33
    plan tests => 3;
34
35
    $schema->storage->txn_begin;
36
37
    Koha::SMTP::Servers->search->delete;
38
39
    my $default_server = $builder->build_object(
40
        {
41
            class => 'Koha::SMTP::Servers',
42
            value => {
43
                library_id => undef
44
            }
45
        }
46
    );
47
48
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
49
    my $library_specific_server = $builder->build_object(
50
        {
51
            class => 'Koha::SMTP::Servers',
52
            value => {
53
                library_id => $library->branchcode
54
            }
55
        }
56
    );
57
58
    my $servers = Koha::SMTP::Servers->new;
59
    my $server  = $servers->get_default;
60
    is( $server->id, $default_server->id,
61
        'The default server is correctly retrieved' );
62
63
    # Delete the default server
64
    $server->delete;
65
66
    # Get the default
67
    $default_server = $servers->get_default;
68
    is( ref($default_server), 'Koha::SMTP::Server',
69
        'An object of the right type is returned' );
70
71
    my $unblessed_server = $default_server->unblessed;
72
    delete $unblessed_server->{id};
73
    is_deeply(
74
        $unblessed_server,
75
        Koha::SMTP::Servers::default_setting,
76
        'The default setting is returned if no user-defined default'
77
    );
78
79
    $schema->storage->txn_rollback;
80
};
81
82
subtest 'set_default() tests' => sub {
83
84
    plan tests => 4;
85
86
    $schema->storage->txn_begin;
87
88
    Koha::SMTP::Servers->search->delete;
89
90
    my $default_server = $builder->build_object(
91
        {
92
            class => 'Koha::SMTP::Servers',
93
            value => {
94
                library_id => undef
95
            }
96
        }
97
    );
98
99
    throws_ok {
100
        Koha::SMTP::Servers->new->set_default(
101
            {
102
                name       => 'A new default',
103
                library_id => 'Whatever',
104
            }
105
        );
106
    }
107
    'Koha::Exceptions::BadParameter',
108
'Exception thrown when trying to set default SMTP server with a library_id';
109
110
    is(
111
        "$@",
112
        'library_id must be undef when setting the default SMTP server',
113
        'Exception message is clear'
114
    );
115
116
    my $new_default = Koha::SMTP::Servers->new->set_default(
117
        {
118
            name       => 'A new default',
119
            library_id => undef,
120
        }
121
    );
122
123
    is( ref($new_default), 'Koha::SMTP::Server', 'Type is correct' );
124
    is(
125
        $new_default->id,
126
        Koha::SMTP::Servers->get_default->id,
127
        'Default SMTP server is correctly set'
128
    );
129
130
    $schema->storage->txn_rollback;
131
};
132
133
subtest 'get_effective_server() tests' => sub {
134
135
    plan tests => 4;
136
137
    $schema->storage->txn_begin;
138
139
    Koha::SMTP::Servers->search->delete;
140
141
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
142
143
    my $default_server = $builder->build_object(
144
        {
145
            class => 'Koha::SMTP::Servers',
146
            value => {
147
                library_id => undef
148
            }
149
        }
150
    );
151
152
    throws_ok { Koha::SMTP::Servers->new->get_effective_server() }
153
    'Koha::Exceptions::MissingParameter', 'Exception thrown';
154
155
    is( "$@", 'Mandatory parameter missing: library' );
156
157
    is(
158
        Koha::SMTP::Servers->new->get_effective_server(
159
            { library => $library }
160
        )->id,
161
        $default_server->id,
162
        'Fallback default server retrieved'
163
    );
164
165
    my $specific_server = $builder->build_object(
166
        {
167
            class => 'Koha::SMTP::Servers',
168
            value => {
169
                library_id => $library->branchcode
170
            }
171
        }
172
    );
173
174
    is(
175
        Koha::SMTP::Servers->new->get_effective_server(
176
            { library => $library }
177
        )->id,
178
        $specific_server->id,
179
        'Library specific server retrieved'
180
    );
181
182
    $schema->storage->txn_rollback;
183
};
184
185
subtest 'set_library_server() tests' => sub {
186
187
    plan tests => 6;
188
189
    $schema->storage->txn_begin;
190
191
    Koha::SMTP::Servers->search->delete;
192
193
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
194
195
    my $default_server = $builder->build_object(
196
        {
197
            class => 'Koha::SMTP::Servers',
198
            value => {
199
                library_id => undef
200
            }
201
        }
202
    );
203
204
    is(
205
        Koha::SMTP::Servers->new->get_effective_server(
206
            { library => $library }
207
        )->id,
208
        $default_server->id,
209
        'Fallback default server retrieved'
210
    );
211
212
    my $specific_server = Koha::SMTP::Servers->new->set_library_server(
213
        {
214
            name       => 'Specific server 1',
215
            library_id => $library->id
216
        }
217
    );
218
219
    is(
220
        Koha::SMTP::Servers->new->get_effective_server(
221
            { library => $library }
222
        )->id,
223
        $specific_server->id,
224
        'Library specific server retrieved'
225
    );
226
227
    throws_ok {
228
        Koha::SMTP::Servers->new->set_library_server(
229
            {
230
                name => 'Specific server 2'
231
            }
232
        );
233
    }
234
    'Koha::Exceptions::MissingParameter',
235
      'Exception thrown on missing parameter';
236
237
    is( "$@", 'Mandatory parameter missing: library_id' );
238
239
    $specific_server = Koha::SMTP::Servers->new->set_library_server(
240
        {
241
            name       => 'Specific server 2',
242
            library_id => $library->id
243
        }
244
    );
245
246
    is(
247
        Koha::SMTP::Servers->new->get_effective_server(
248
            { library => $library }
249
        )->id,
250
        $specific_server->id,
251
        'New library specific server retrieved'
252
    );
253
254
    is( Koha::SMTP::Servers->search( { library_id => $library->id } )->count,
255
        1, 'Only one SMTP server set' );
256
257
    $schema->storage->txn_rollback;
258
};

Return to bug 22343