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

(-)a/Koha/Library.pm (+52 lines)
Lines 25-30 use C4::Context; Link Here
25
25
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::StockRotationStages;
27
use Koha::StockRotationStages;
28
use Koha::SMTP::Servers;
28
29
29
use base qw(Koha::Object);
30
use base qw(Koha::Object);
30
31
Lines 65-70 sub get_effective_marcorgcode { Link Here
65
    return $self->marcorgcode || C4::Context->preference("MARCOrgCode");
66
    return $self->marcorgcode || C4::Context->preference("MARCOrgCode");
66
}
67
}
67
68
69
=head3 smtp_server
70
71
    my $smtp_server = $library->smtp_server;
72
    $library->smtp_server({ smtp_server => $smtp_server });
73
    $library->smtp_server({ smtp_server => undef });
74
75
Accessor for getting and setting the library's SMTP server.
76
77
Returns the effective SMTP server configuration to be used on the library. The returned
78
value is always a I<Koha::SMTP::Server> object.
79
80
Setting it to undef will remove the link to a specific SMTP server and effectively
81
make the library use the default setting
82
83
=cut
84
85
sub smtp_server {
86
    my ( $self, $params ) = @_;
87
88
    my $library_smtp_server_rs = $self->_result->library_smtp_server;
89
90
    if ( exists $params->{smtp_server} ) {
91
92
        $self->_result->result_source->schema->txn_do( sub {
93
            $library_smtp_server_rs->delete
94
                if $library_smtp_server_rs;
95
96
            if ( defined $params->{smtp_server} ) {
97
                # Set the new server
98
                # Remove any already set SMTP server
99
100
                my $smtp_server = $params->{smtp_server};
101
                $smtp_server->_result->add_to_library_smtp_servers({ library_id => $self->id });
102
            }
103
        });
104
    } # else => reset to default
105
    else {
106
        # Getter
107
        if ( $library_smtp_server_rs ) {
108
            # use Data::Printer colored => 1;
109
            # p($library_smtp_server_rs);
110
            return Koha::SMTP::Servers->find(
111
                $library_smtp_server_rs->smtp_server_id );
112
        }
113
114
        return Koha::SMTP::Servers->get_default;
115
    }
116
117
    return $self;
118
}
119
68
=head3 inbound_email_address
120
=head3 inbound_email_address
69
121
70
  my $to_email = Koha::Library->inbound_email_address;
122
  my $to_email = Koha::Library->inbound_email_address;
(-)a/t/db_dependent/Koha/Library.t (-1 / +95 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2020 Koha Development team
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 => 1;
23
24
use Koha::Database;
25
use Koha::SMTP::Servers;
26
27
use t::lib::TestBuilder;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
subtest 'smtp_server() tests' => sub {
33
34
    plan tests => 14;
35
36
    $schema->storage->txn_begin;
37
38
    my $library       = $builder->build_object({ class => 'Koha::Libraries' });
39
    my $smtp_server_1 = $builder->build_object({ class => 'Koha::SMTP::Servers' });
40
    my $smtp_server_2 = $builder->build_object({ class => 'Koha::SMTP::Servers' });
41
42
    is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
43
44
    is_deeply(
45
        $library->smtp_server->unblessed,
46
        Koha::SMTP::Servers->get_default->unblessed,
47
        'Fresh library is set the default'
48
    );
49
50
    my $return = $library->smtp_server({ smtp_server => $smtp_server_1 });
51
    $library->discard_changes;
52
53
    is( ref($return), 'Koha::Library', 'The setter is chainable' );
54
    is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
55
    is_deeply(
56
        $library->smtp_server->unblessed,
57
        $smtp_server_1->unblessed,
58
        'SMTP server correctly set for library'
59
    );
60
61
    $return = $library->smtp_server({ smtp_server => $smtp_server_2 });
62
    $library->discard_changes;
63
64
    is( ref($return), 'Koha::Library', 'The setter is chainable' );
65
    is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
66
    is_deeply(
67
        $library->smtp_server->unblessed,
68
        $smtp_server_2->unblessed,
69
        'SMTP server correctly set for library'
70
    );
71
72
    $return = $library->smtp_server({ smtp_server => undef });
73
    $library->discard_changes;
74
75
    is( ref($return), 'Koha::Library', 'The setter is chainable' );
76
    is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
77
    is_deeply(
78
        $library->smtp_server->unblessed,
79
        Koha::SMTP::Servers->get_default->unblessed,
80
        'Resetting makes it return the default'
81
    );
82
83
    $return = $library->smtp_server({ smtp_server => undef });
84
    $library->discard_changes;
85
86
    is( ref($return), 'Koha::Library', 'The setter is chainable' );
87
    is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
88
    is_deeply(
89
        $library->smtp_server->unblessed,
90
        Koha::SMTP::Servers->get_default->unblessed,
91
        q{Resetting twice doesn't explode and has the expected results}
92
    );
93
94
    $schema->storage->txn_rollback;
95
};

Return to bug 22343