3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 2;
25 use Koha::SMTP::Servers;
27 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 subtest 'smtp_server() tests' => sub {
36 $schema->storage->txn_begin;
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' });
42 is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
45 $library->smtp_server->unblessed,
46 Koha::SMTP::Servers->get_default->unblessed,
47 'Fresh library is set the default'
50 my $return = $library->smtp_server({ smtp_server => $smtp_server_1 });
51 $library->discard_changes;
53 is( ref($return), 'Koha::Library', 'The setter is chainable' );
54 is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
56 $library->smtp_server->unblessed,
57 $smtp_server_1->unblessed,
58 'SMTP server correctly set for library'
61 $return = $library->smtp_server({ smtp_server => $smtp_server_2 });
62 $library->discard_changes;
64 is( ref($return), 'Koha::Library', 'The setter is chainable' );
65 is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
67 $library->smtp_server->unblessed,
68 $smtp_server_2->unblessed,
69 'SMTP server correctly set for library'
72 $return = $library->smtp_server({ smtp_server => undef });
73 $library->discard_changes;
75 is( ref($return), 'Koha::Library', 'The setter is chainable' );
76 is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
78 $library->smtp_server->unblessed,
79 Koha::SMTP::Servers->get_default->unblessed,
80 'Resetting makes it return the default'
83 $return = $library->smtp_server({ smtp_server => undef });
84 $library->discard_changes;
86 is( ref($return), 'Koha::Library', 'The setter is chainable' );
87 is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
89 $library->smtp_server->unblessed,
90 Koha::SMTP::Servers->get_default->unblessed,
91 q{Resetting twice doesn't explode and has the expected results}
94 $schema->storage->txn_rollback;
97 subtest 'smtp_server_info() tests' => sub {
101 $schema->storage->txn_begin;
103 my $library = $builder->build_object({ class => 'Koha::Libraries' });
104 my $smtp_server = $builder->build_object({ class => 'Koha::SMTP::Servers' });
106 # No SMTP server assigned to library, return system default
107 is_deeply( $library->smtp_server_info, { name => 'system_default' }, 'System default is returned' );
109 # Assign an SMTP server
110 $library->smtp_server({ smtp_server => $smtp_server });
111 is_deeply( $library->smtp_server_info, { name => $smtp_server->name, smtp_server_id => $smtp_server->id }, 'The right information is returned when SMTP server is assigned' );
113 $schema->storage->txn_rollback;