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 => 5;
26 $schema = Koha::Database->new->schema;
28 subtest 'Environment' => sub {
30 # First we need to check your environmental variables
32 for (qw(KOHA_CONF PERL5LIB)) {
33 ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
37 subtest 'Tests with preferences' => sub {
39 $schema->storage->txn_begin;
42 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
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');
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');
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');
59 C4::Context->set_preference('OPACBaseURL', '');
60 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
61 is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
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();
70 # Testing syspref caching
71 $schema->storage->debug(1);
73 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
74 $schema->storage->debugfh( $trace );
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' );
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');
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');
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');
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');
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');
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');
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');
114 $silly_preference->delete();
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' );
121 $schema->storage->txn_rollback;
124 subtest 'Check context hash keys' => sub {
126 ok($koha = C4::Context->new, 'C4::Context->new');
127 my @keys = keys %$koha;
129 ok( @keys, 'Expecting entries in context hash' );
131 $width = (sort {$a <=> $b} map {length} @keys)[-1];
133 foreach (sort @keys) {
134 ok(exists $koha->{$_},
135 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
136 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
140 ok($config = $koha->{config}, 'Getting $koha->{config} ');
144 subtest 'Zconn' => sub {
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");
152 subtest 'get_versions' => sub {
155 my $dbh = C4::Context->dbh;
156 my $mod = Test::MockModule->new( 'C4::Context' );
158 $mod->mock( 'dbh', sub { return $no_dbh ? undef : $dbh; } );
160 my $client = `mysql -V`;
161 my $server = ( C4::Context->dbh->selectrow_array("SHOW VARIABLES LIKE 'version'") )[1];
163 if( $server =~ /(\d+(\.\d+)+)/ ) {
164 $server_version = $1;
165 $server_version =~ s/\./\\./g;
168 like( $v = { C4::Context::get_versions }->{mysqlVersion}, qr/$server_version/, "Server version $v found" );
170 is( $v = { C4::Context::get_versions }->{mysqlVersion}, $client, "Client version $v found" );