Bug 25030: (QA follow-up) Add POD
[koha.git] / t / Koha / Config.t
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 FindBin qw($Bin $Script);
22
23 use_ok('Koha::Config');
24
25 my $config_filepath = "$Bin/../data/koha-conf.xml";
26
27 subtest 'read_from_file() tests' => sub {
28     plan tests => 3;
29
30     is( Koha::Config->read_from_file(undef), undef,
31         "Undef parameter makes function return undef");
32
33     my $got = eval { Koha::Config->read_from_file($config_filepath) };
34     my $expected = {
35         'listen' => {
36             'biblioserver' => {
37                 'content' => 'unix:/home/koha/var/run/zebradb/bibliosocket',
38             },
39             'authorityserver' => {
40                 'content' => 'unix:/home/koha/var/run/zebradb/authoritysocket',
41             },
42         },
43         'server' => {
44             'biblioserver' => {
45                 'listenref' => 'biblioserver',
46                 'directory' => '/home/koha/var/lib/zebradb/biblios',
47                 'config' => '/home/koha/etc/zebradb/zebra-biblios-dom.cfg',
48                 'cql2rpn' => '/home/koha/etc/zebradb/pqf.properties',
49                 'xi:include' => [
50                     {
51                         'href' => '/home/koha/etc/zebradb/retrieval-info-bib-dom.xml',
52                         'xmlns:xi' => 'http://www.w3.org/2001/XInclude'
53                     },
54                     {
55                         'xmlns:xi' => 'http://www.w3.org/2001/XInclude',
56                         'href' => '/home/koha/etc/zebradb/explain-biblios.xml'
57                     }
58                 ],
59             },
60             'authorityserver' => {
61                 'listenref' => 'authorityserver',
62                 'directory' => '/home/koha/var/lib/zebradb/authorities',
63                 'config' => '/home/koha/etc/zebradb/zebra-authorities-dom.cfg',
64                 'cql2rpn' => '/home/koha/etc/zebradb/pqf.properties',
65                 'xi:include' => [
66                     {
67                         'xmlns:xi' => 'http://www.w3.org/2001/XInclude',
68                         'href' => '/home/koha/etc/zebradb/retrieval-info-auth-dom.xml'
69                     },
70                     {
71                         'href' => '/home/koha/etc/zebradb/explain-authorities.xml',
72                         'xmlns:xi' => 'http://www.w3.org/2001/XInclude'
73                     }
74                 ],
75             },
76         },
77         'serverinfo' => {
78             'biblioserver' => {
79                 'ccl2rpn' => '/home/koha/etc/zebradb/ccl.properties',
80                 'user' => 'kohauser',
81                 'password' => 'zebrastripes',
82             },
83             'authorityserver' => {
84                 'ccl2rpn' => '/home/koha/etc/zebradb/ccl.properties',
85                 'user' => 'kohauser',
86                 'password' => 'zebrastripes',
87             }
88         },
89         'config' => {
90             'db_scheme' => 'mysql',
91             'database' => 'koha',
92             'hostname' => 'localhost',
93             'port' => '3306',
94             'user' => 'kohaadmin',
95             'pass' => 'katikoan',
96             'tls' => 'no',
97             'ca' => '',
98             'cert' => '',
99             'key' => '',
100             'biblioserver' => 'biblios',
101             'biblioservershadow' => '1',
102             'authorityserver' => 'authorities',
103             'authorityservershadow' => '1',
104             'pluginsdir' => '/home/koha/var/lib/plugins',
105             'enable_plugins' => '0',
106             'upload_path' => '',
107             'tmp_path' => '',
108             'intranetdir' => '/home/koha/src',
109             'opacdir' => '/home/koha/src/opac',
110             'opachtdocs' => '/home/koha/src/koha-tmpl/opac-tmpl',
111             'intrahtdocs' => '/home/koha/src/koha-tmpl/intranet-tmpl',
112             'includes' => '/home/koha/src/koha-tmpl/intranet-tmpl/prog/en/includes/',
113             'logdir' => '/home/koha/var/log',
114             'docdir' => '/home/koha/doc',
115             'backupdir' => '/home/koha/var/spool',
116             'mana_config' => 'https://mana-kb.koha-community.org',
117             'backup_db_via_tools' => '0',
118             'backup_conf_via_tools' => '0',
119             'install_log' => '/home/koha/misc/koha-install-log',
120             'useldapserver' => '1',
121             'ldapserver' => {
122                 ldapserver => {
123                     hostname => 'ldap://another_ldap_server:389',
124                     user     => 'user',
125                     pass     => 'password',
126                     mapping  => {
127                         firstname => {
128                             is => 'givenName'
129                         }
130                     }
131                 }
132             },
133             'useshibboleth' => '0',
134             'zebra_lockdir' => '/home/koha/var/lock/zebradb',
135             'lockdir' => '__LOCK_DIR__',
136             'use_zebra_facets' => '1',
137             'zebra_max_record_size' => '1024',
138             'log4perl_conf' => '/home/koha/etc/log4perl.conf',
139             'memcached_servers' => '127.0.0.1:11211',
140             'memcached_namespace' => 'KOHA',
141             'template_cache_dir' => '/tmp/koha',
142             'api_secret_passphrase' => 'CHANGEME',
143             'access_dirs' => {
144                 access_dir => [ '/dir_1', '/dir_2' ],
145             },
146             'ttf' => {
147                 'font' => [
148                     {
149                         'type' => 'TR',
150                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',
151                     },
152                     {
153                         'type' => 'TB',
154                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf',
155                     },
156                     {
157                         'type' => 'TI',
158                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSerif-Italic.ttf',
159                     },
160                     {
161                         'type' => 'TBI',
162                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSerif-BoldItalic.ttf',
163                     },
164                     {
165                         'type' => 'C',
166                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',
167                     },
168                     {
169                         'type' => 'CB',
170                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf',
171                     },
172                     {
173                         'type' => 'CO',
174                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf',
175                     },
176                     {
177                         'type' => 'CBO',
178                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSansMono-BoldOblique.ttf',
179                     },
180                     {
181                         'type' => 'H',
182                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
183                     },
184                     {
185                         'type' => 'HO',
186                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf',
187                     },
188                     {
189                         'type' => 'HB',
190                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
191                     },
192                     {
193                         'type' => 'HBO',
194                         'content' => '/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf',
195                     },
196                 ],
197             },
198             'sms_send_config' => '/home/koha/etc/sms_send/',
199             'plack_max_requests' => '50',
200             'plack_workers' => '2',
201             'elasticsearch' => {
202                 'server' => [
203                     'localhost:9200',
204                 ],
205                 'index_name' => 'koha_master',
206                 'cxn_pool' => 'Static',
207             },
208             'interlibrary_loans' => {
209                 'backend_directory' => '/home/koha/src/Koha/Illbackends',
210                 'branch' => {
211                     'code' => 'CPL',
212                     'prefix' => 'ILL',
213                 },
214                 'staff_request_comments' => 'hide',
215                 'reply_date' => 'hide',
216                 'digital_recipient' => 'branch',
217                 'partner_code' => 'ILLLIBS',
218             },
219             'timezone' => '',
220             'bcrypt_settings' => '__BCRYPT_SETTINGS__',
221             'dev_install' => '0',
222             'strict_sql_modes' => '0',
223             'plugin_repos' => '',
224             'koha_xslt_security' => '',
225             'smtp_server' => {
226                 'host' => 'localhost',
227                 'port' => '25',
228                 'timeout' => '120',
229                 'ssl_mode' => 'disabled',
230                 'user_name' => '',
231                 'password' => '',
232                 'debug' => '0',
233             },
234             'message_broker' => {
235                 'hostname' => 'localhost',
236                 'port' => '61613',
237                 'username' => 'guest',
238                 'password' => 'guest',
239                 'vhost' => '',
240             },
241         },
242     };
243     is_deeply( $got, $expected, 'File read correctly' );
244
245     # Reading a Perl script as XML should fail hopefully
246     eval { Koha::Config->read_from_file("$Bin/$Script") };
247     like( $@, qr{.*Error reading file.*}, 'File failing to read raises warning');
248 };
249
250 subtest 'get_instance' => sub {
251     plan tests => 3;
252
253     my $config = Koha::Config->get_instance($config_filepath);
254     isa_ok($config, 'Koha::Config', 'get_instance returns a Koha::Config object');
255     my $same_config = Koha::Config->get_instance($config_filepath);
256     is($config, $same_config, '2nd call to get_instance returns the same object');
257
258     local $ENV{KOHA_CONF} = $config_filepath;
259     my $default_config = Koha::Config->get_instance;
260     is($default_config, $config, 'get_instance without parameters reads $KOHA_CONF');
261 };
262
263 subtest 'get' => sub {
264     plan tests => 7;
265
266     my $config = Koha::Config->get_instance($config_filepath);
267
268     is_deeply(
269         $config->get('biblioserver', 'listen'),
270         { content => 'unix:/home/koha/var/run/zebradb/bibliosocket' },
271     );
272
273     is_deeply(
274         $config->get('biblioserver', 'server'),
275         {
276             'listenref' => 'biblioserver',
277             'directory' => '/home/koha/var/lib/zebradb/biblios',
278             'config' => '/home/koha/etc/zebradb/zebra-biblios-dom.cfg',
279             'cql2rpn' => '/home/koha/etc/zebradb/pqf.properties',
280             'xi:include' => [
281                 {
282                     'href' => '/home/koha/etc/zebradb/retrieval-info-bib-dom.xml',
283                     'xmlns:xi' => 'http://www.w3.org/2001/XInclude'
284                 },
285                 {
286                     'xmlns:xi' => 'http://www.w3.org/2001/XInclude',
287                     'href' => '/home/koha/etc/zebradb/explain-biblios.xml'
288                 }
289             ],
290         },
291     );
292
293     is_deeply(
294         $config->get('biblioserver', 'serverinfo'),
295         {
296             'ccl2rpn' => '/home/koha/etc/zebradb/ccl.properties',
297             'user' => 'kohauser',
298             'password' => 'zebrastripes',
299         },
300     );
301
302     is($config->get('db_scheme'), 'mysql');
303     is($config->get('ca'), '');
304
305     is($config->get('unicorn'), undef, 'returns undef if key does not exist');
306     is_deeply([$config->get('unicorn')], [undef], 'returns undef even in list context');
307 };