Bug 31383: Adjust and add tests
[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         {
106             class => 'Koha::AdditionalContents',
107             value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, expirationdate => undef },
108         }
109     );
110     $html01->translated_contents(
111         [
112             {
113                 lang    => 'default',
114                 content => '1',
115             }
116         ]
117     );
118     my $html02 = $builder->build_object(
119         {
120             class => 'Koha::AdditionalContents',
121             value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, expirationdate => undef },
122         }
123     );
124     $html02->translated_contents(
125         [
126             {
127                 lang    => 'default',
128                 content => '2',
129             },
130             {
131                 lang    => 'nl-NL',
132                 content => '3',
133             }
134         ]
135     );
136     my $html04 = $builder->build_object(
137         {
138             class => 'Koha::AdditionalContents',
139             value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, expirationdate => undef },
140         }
141     );
142     $html04->translated_contents(
143         [
144             {
145                 lang    => 'fr-FR',
146                 content => '4',
147             }
148         ]
149     );
150
151     # Start testing
152     is( $library01->opac_info->content, '2', 'specific library, default language' );
153     is( $library01->opac_info({ lang => 'nl-NL' })->content, '3', 'specific library, specific language' );
154     is( $library01->opac_info({ lang => 'nl-BE' })->content, '2', 'specific library, unknown language' );
155     is( $library02->opac_info->content, '1', 'unknown library, default language' );
156     is( $library02->opac_info({ lang => 'fr-FR' })->content, '4', 'unknown library, specific language' );
157     is( $library02->opac_info({ lang => 'de-DE' })->content, '1', 'unknown library, unknown language' );
158     $html01->delete;
159     is( $library02->opac_info, undef, 'unknown library, default language (after removing html01)' );
160     is( $library02->opac_info({ lang => 'de-DE' }), undef, 'unknown library, unknown language (after removing html01)' );
161
162     $schema->storage->txn_rollback;
163 };