Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2
3 # Copyright 2008, 2022 Koha development team
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 use Test::More tests => 4;
20
21 use C4::Context;
22 use Koha::Database;
23
24 our $schema;
25 $schema = Koha::Database->new->schema;
26
27 subtest 'Environment' => sub {
28     plan tests => 2;
29     # First we need to check your environmental variables
30     my $ret;
31     for (qw(KOHA_CONF PERL5LIB)) {
32         ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
33     }
34 };
35
36 subtest 'Tests with preferences' => sub {
37     plan tests => 22;
38     $schema->storage->txn_begin;
39
40     my $dbh;
41     ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
42
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');
47
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');
52
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');
57
58     C4::Context->set_preference('OPACBaseURL', '');
59     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
60     is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
61
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();
68
69     # Testing syspref caching
70     $schema->storage->debug(1);
71     my $trace_read;
72     open my $trace, '>', \$trace_read or die "Can't open variable: $!";
73     $schema->storage->debugfh( $trace );
74
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' );
78
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');
82     $trace_read = q{};
83
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');
86     $trace_read = q{};
87
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');
92     $trace_read = q{};
93
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');
97     $trace_read = q{};
98
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');
102     $trace_read = q{};
103
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');
108     $trace_read = q{};
109
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');
112     $trace_read = q{};
113     $silly_preference->delete();
114
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' );
119
120     $schema->storage->txn_rollback;
121 };
122
123 subtest 'Check context hash keys' => sub {
124     my $koha;
125     ok($koha = C4::Context->new, 'C4::Context->new');
126     my @keys = keys %$koha;
127     my $width = 0;
128     ok( @keys, 'Expecting entries in context hash' );
129     if( @keys ) {
130         $width = (sort {$a <=> $b} map {length} @keys)[-1];
131     }
132     foreach (sort @keys) {
133         ok(exists $koha->{$_},
134             '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
135             . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
136         );
137     }
138     my $config;
139     ok($config = $koha->{config}, 'Getting $koha->{config} ');
140     done_testing();
141 };
142
143 subtest 'Zconn' => sub {
144     plan tests => 2;
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");
149 };