3 # Copyright 2008, 2022 Koha development team
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.
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.
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>.
19 use Test::More tests => 4;
25 $schema = Koha::Database->new->schema;
27 subtest 'Environment' => sub {
29 # First we need to check your environmental variables
31 for (qw(KOHA_CONF PERL5LIB)) {
32 ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
36 subtest 'Tests with preferences' => sub {
38 $schema->storage->txn_begin;
41 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
43 C4::Context->set_preference('OPACBaseURL','junk');
44 C4::Context->clear_syspref_cache();
45 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
46 is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
48 C4::Context->set_preference('OPACBaseURL','https://junk');
49 C4::Context->clear_syspref_cache();
50 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
51 is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
53 C4::Context->set_preference('OPACBaseURL','http://junk2');
54 C4::Context->clear_syspref_cache();
55 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
56 is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
58 C4::Context->set_preference('OPACBaseURL', '');
59 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
60 is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
62 C4::Context->set_preference('SillyPreference','random');
63 C4::Context->clear_syspref_cache();
64 my $SillyPeference = C4::Context->preference('SillyPreference');
65 is($SillyPeference,'random','SillyPreference saved as specified');
66 C4::Context->clear_syspref_cache();
67 C4::Context->enable_syspref_cache();
69 # Testing syspref caching
70 $schema->storage->debug(1);
72 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
73 $schema->storage->debugfh( $trace );
75 C4::Context->set_preference('SillyPreference', 'thing1');
76 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
77 is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
79 my $pref = C4::Context->preference("SillyPreference");
80 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
81 ok( $trace_read, 'Retrieved syspref from database');
84 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
85 is( $trace_read , q{}, 'Did not retrieve syspref from database');
88 C4::Context->disable_syspref_cache();
89 $silly_preference->set( { value => 'thing2' } )->store();
90 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
91 ok($trace_read, 'Retrieved syspref from database');
94 $silly_preference->set( { value => 'thing3' } )->store();
95 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
96 ok($trace_read, 'Retrieved syspref from database');
99 C4::Context->enable_syspref_cache();
100 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
101 isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
104 $silly_preference->set( { value => 'thing4' } )->store();
105 C4::Context->clear_syspref_cache();
106 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
107 ok($trace_read, 'Retrieved syspref from database');
110 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
111 is( $trace_read, q{}, 'Did not retrieve syspref from database');
113 $silly_preference->delete();
115 # AutoEmailNewUser should be a YesNo pref
116 C4::Context->set_preference('AutoEmailNewUser', '');
117 my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
118 is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
120 $schema->storage->txn_rollback;
123 subtest 'Check context hash keys' => sub {
125 ok($koha = C4::Context->new, 'C4::Context->new');
126 my @keys = keys %$koha;
128 ok( @keys, 'Expecting entries in context hash' );
130 $width = (sort {$a <=> $b} map {length} @keys)[-1];
132 foreach (sort @keys) {
133 ok(exists $koha->{$_},
134 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
135 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
139 ok($config = $koha->{config}, 'Getting $koha->{config} ');
143 subtest 'Zconn' => sub {
145 my $oConnection = C4::Context->Zconn('biblioserver', 0);
146 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
147 $oConnection = C4::Context->Zconn('biblioserver', 1);
148 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");