Bug 33607: Handle default framework
[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 => 5;
20 use Test::MockModule;
21
22 use C4::Context;
23 use Koha::Database;
24
25 our $schema;
26 $schema = Koha::Database->new->schema;
27
28 subtest 'Environment' => sub {
29     plan tests => 2;
30     # First we need to check your environmental variables
31     my $ret;
32     for (qw(KOHA_CONF PERL5LIB)) {
33         ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
34     }
35 };
36
37 subtest 'Tests with preferences' => sub {
38     plan tests => 22;
39     $schema->storage->txn_begin;
40
41     my $dbh;
42     ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
43
44     C4::Context->set_preference('OPACBaseURL','junk');
45     C4::Context->clear_syspref_cache();
46     my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
47     is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
48
49     C4::Context->set_preference('OPACBaseURL','https://junk');
50     C4::Context->clear_syspref_cache();
51     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
52     is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
53
54     C4::Context->set_preference('OPACBaseURL','http://junk2');
55     C4::Context->clear_syspref_cache();
56     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
57     is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
58
59     C4::Context->set_preference('OPACBaseURL', '');
60     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
61     is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
62
63     C4::Context->set_preference('SillyPreference','random');
64     C4::Context->clear_syspref_cache();
65     my $SillyPeference = C4::Context->preference('SillyPreference');
66     is($SillyPeference,'random','SillyPreference saved as specified');
67     C4::Context->clear_syspref_cache();
68     C4::Context->enable_syspref_cache();
69
70     # Testing syspref caching
71     $schema->storage->debug(1);
72     my $trace_read;
73     open my $trace, '>', \$trace_read or die "Can't open variable: $!";
74     $schema->storage->debugfh( $trace );
75
76     C4::Context->set_preference('SillyPreference', 'thing1');
77     my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
78     is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
79
80     my $pref = C4::Context->preference("SillyPreference");
81     is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
82     ok( $trace_read, 'Retrieved syspref from database');
83     $trace_read = q{};
84
85     is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
86     is( $trace_read , q{}, 'Did not retrieve syspref from database');
87     $trace_read = q{};
88
89     C4::Context->disable_syspref_cache();
90     $silly_preference->set( { value => 'thing2' } )->store();
91     is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
92     ok($trace_read, 'Retrieved syspref from database');
93     $trace_read = q{};
94
95     $silly_preference->set( { value => 'thing3' } )->store();
96     is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
97     ok($trace_read, 'Retrieved syspref from database');
98     $trace_read = q{};
99
100     C4::Context->enable_syspref_cache();
101     is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
102     isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
103     $trace_read = q{};
104
105     $silly_preference->set( { value => 'thing4' } )->store();
106     C4::Context->clear_syspref_cache();
107     is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
108     ok($trace_read, 'Retrieved syspref from database');
109     $trace_read = q{};
110
111     is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
112     is( $trace_read, q{}, 'Did not retrieve syspref from database');
113     $trace_read = q{};
114     $silly_preference->delete();
115
116     # AutoEmailNewUser should be a YesNo pref
117     C4::Context->set_preference('AutoEmailNewUser', '');
118     my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
119     is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
120
121     $schema->storage->txn_rollback;
122 };
123
124 subtest 'Check context hash keys' => sub {
125     my $koha;
126     ok($koha = C4::Context->new, 'C4::Context->new');
127     my @keys = keys %$koha;
128     my $width = 0;
129     ok( @keys, 'Expecting entries in context hash' );
130     if( @keys ) {
131         $width = (sort {$a <=> $b} map {length} @keys)[-1];
132     }
133     foreach (sort @keys) {
134         ok(exists $koha->{$_},
135             '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
136             . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
137         );
138     }
139     my $config;
140     ok($config = $koha->{config}, 'Getting $koha->{config} ');
141     done_testing();
142 };
143
144 subtest 'Zconn' => sub {
145     plan tests => 2;
146     my $oConnection = C4::Context->Zconn('biblioserver', 0);
147     isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
148     $oConnection = C4::Context->Zconn('biblioserver', 1);
149     is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
150 };
151
152 subtest 'get_versions' => sub {
153     plan tests => 2;
154
155     my $dbh = C4::Context->dbh;
156     my $mod = Test::MockModule->new( 'C4::Context' );
157     my $no_dbh;
158     $mod->mock( 'dbh', sub { return $no_dbh ? undef : $dbh; } );
159
160     my $client = `mysql -V`;
161     my $server = ( C4::Context->dbh->selectrow_array("SHOW VARIABLES LIKE 'version'") )[1];
162     my $server_version;
163     if( $server =~ /(\d+(\.\d+)+)/ ) {
164         $server_version = $1;
165         $server_version =~ s/\./\\./g;
166     }
167     my $v;
168     like( $v = { C4::Context::get_versions }->{mysqlVersion}, qr/$server_version/, "Server version $v found" );
169     $no_dbh = 1;
170     is( $v = { C4::Context::get_versions }->{mysqlVersion}, $client, "Client version $v found" );
171 };