Bug 33360: Add Koha::Notice::Util for mail domain limits
[koha.git] / t / db_dependent / Koha / Library.t
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 => 2;
23
24 use Koha::Database;
25 use Koha::AdditionalContents;
26 use Koha::SMTP::Servers;
27
28 use t::lib::TestBuilder;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 subtest 'smtp_server() tests' => sub {
34
35     plan tests => 14;
36
37     $schema->storage->txn_begin;
38
39     my $library       = $builder->build_object({ class => 'Koha::Libraries' });
40     my $smtp_server_1 = $builder->build_object({ class => 'Koha::SMTP::Servers' });
41     my $smtp_server_2 = $builder->build_object({ class => 'Koha::SMTP::Servers' });
42
43     is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
44
45     is_deeply(
46         $library->smtp_server->unblessed,
47         Koha::SMTP::Servers->get_default->unblessed,
48         'Fresh library is set the default'
49     );
50
51     my $return = $library->smtp_server({ smtp_server => $smtp_server_1 });
52     $library->discard_changes;
53
54     is( ref($return), 'Koha::Library', 'The setter is chainable' );
55     is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
56     is_deeply(
57         $library->smtp_server->unblessed,
58         $smtp_server_1->unblessed,
59         'SMTP server correctly set for library'
60     );
61
62     $return = $library->smtp_server({ smtp_server => $smtp_server_2 });
63     $library->discard_changes;
64
65     is( ref($return), 'Koha::Library', 'The setter is chainable' );
66     is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
67     is_deeply(
68         $library->smtp_server->unblessed,
69         $smtp_server_2->unblessed,
70         'SMTP server correctly set for library'
71     );
72
73     $return = $library->smtp_server({ smtp_server => undef });
74     $library->discard_changes;
75
76     is( ref($return), 'Koha::Library', 'The setter is chainable' );
77     is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
78     is_deeply(
79         $library->smtp_server->unblessed,
80         Koha::SMTP::Servers->get_default->unblessed,
81         'Resetting makes it return the default'
82     );
83
84     $return = $library->smtp_server({ smtp_server => undef });
85     $library->discard_changes;
86
87     is( ref($return), 'Koha::Library', 'The setter is chainable' );
88     is( ref($library->smtp_server), 'Koha::SMTP::Server', 'Type is correct' );
89     is_deeply(
90         $library->smtp_server->unblessed,
91         Koha::SMTP::Servers->get_default->unblessed,
92         q{Resetting twice doesn't explode and has the expected results}
93     );
94
95     $schema->storage->txn_rollback;
96 };
97
98 subtest 'opac_info tests' => sub {
99     plan tests => 8;
100     $schema->storage->txn_begin;
101     my $library01 = $builder->build_object({ class => 'Koha::Libraries' });
102     my $library02 = $builder->build_object({ class => 'Koha::Libraries' });
103
104     my $html01 = $builder->build_object({
105         class => 'Koha::AdditionalContents',
106         value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'default', content => '1', expirationdate => undef },
107     });
108     my $html02 = $builder->build_object({
109         class => 'Koha::AdditionalContents',
110         value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'default', content => '2', expirationdate => undef },
111     });
112     my $html03 = $builder->build_object({
113         class => 'Koha::AdditionalContents',
114         value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'nl-NL', content => '3', expirationdate => undef },
115     });
116     my $html04 = $builder->build_object({
117         class => 'Koha::AdditionalContents',
118         value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'fr-FR', content => '4', expirationdate => undef },
119     });
120
121     # Start testing
122     is( $library01->opac_info->content, '2', 'specific library, default language' );
123     is( $library01->opac_info({ lang => 'nl-NL' })->content, '3', 'specific library, specific language' );
124     is( $library01->opac_info({ lang => 'nl-BE' })->content, '2', 'specific library, unknown language' );
125     is( $library02->opac_info->content, '1', 'unknown library, default language' );
126     is( $library02->opac_info({ lang => 'fr-FR' })->content, '4', 'unknown library, specific language' );
127     is( $library02->opac_info({ lang => 'de-DE' })->content, '1', 'unknown library, unknown language' );
128     $html01->delete;
129     is( $library02->opac_info, undef, 'unknown library, default language (after removing html01)' );
130     is( $library02->opac_info({ lang => 'de-DE' }), undef, 'unknown library, unknown language (after removing html01)' );
131
132     $schema->storage->txn_rollback;
133 };