Bug 18557 - Mysqlim CURRENT_DATE in Koha::Clubs::get_enrollable
[koha.git] / t / db_dependent / Context.t
blobbce0cef209ba874f9a4c05c4b6aaf696396f0489
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Test::More;
7 use Test::MockModule;
8 use vars qw($debug $koha $dbh $config $ret);
9 use t::lib::Mocks;
11 use Koha::Database;
13 BEGIN {
14 $debug = $ENV{DEBUG} || 0;
16 # Note: The overall number of tests may vary by configuration.
17 # First we need to check your environmental variables
18 for (qw(KOHA_CONF PERL5LIB)) {
19 ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
21 use_ok('C4::Context');
24 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
26 $dbh->begin_work;
27 C4::Context->set_preference('OPACBaseURL','junk');
28 C4::Context->clear_syspref_cache();
29 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
30 is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
32 C4::Context->set_preference('OPACBaseURL','https://junk');
33 C4::Context->clear_syspref_cache();
34 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
35 is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
37 C4::Context->set_preference('OPACBaseURL','http://junk2');
38 C4::Context->clear_syspref_cache();
39 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
40 is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
42 C4::Context->set_preference('OPACBaseURL', '');
43 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
44 is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
46 C4::Context->set_preference('SillyPreference','random');
47 C4::Context->clear_syspref_cache();
48 my $SillyPeference = C4::Context->preference('SillyPreference');
49 is($SillyPeference,'random','SillyPreference saved as specified');
50 C4::Context->clear_syspref_cache();
51 C4::Context->enable_syspref_cache();
52 $dbh->rollback;
54 ok($koha = C4::Context->new, 'C4::Context->new');
55 my @keys = keys %$koha;
56 my $width = 0;
57 if (ok(@keys)) {
58 $width = (sort {$a <=> $b} map {length} @keys)[-1];
59 $debug and diag "widest key is $width";
61 foreach (sort @keys) {
62 ok(exists $koha->{$_},
63 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
64 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
67 ok($config = $koha->{config}, 'Getting $koha->{config} ');
69 # Testing syspref caching
70 t::lib::Mocks::mock_dbh;
72 my $history;
74 my $schema = Koha::Database->new()->schema();
75 $schema->storage->debug(1);
76 my $trace_read;
77 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
78 $schema->storage->debugfh( $trace );
80 C4::Context->set_preference('SillyPreference', 'thing1');
81 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
83 my $pref = C4::Context->preference("SillyPreference");
84 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
85 ok( $trace_read, 'Retrieved syspref from database');
86 $trace_read = q{};
88 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
89 is( $trace_read , q{}, 'Did not retrieve syspref from database');
90 $trace_read = q{};
92 C4::Context->disable_syspref_cache();
93 $silly_preference->set( { value => 'thing2' } )->store();
94 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
95 ok($trace_read, 'Retrieved syspref from database');
96 $trace_read = q{};
98 $silly_preference->set( { value => 'thing3' } )->store();
99 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
100 ok($trace_read, 'Retrieved syspref from database');
101 $trace_read = q{};
103 C4::Context->enable_syspref_cache();
104 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
105 isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
106 $trace_read = q{};
108 # FIXME This was added by Robin and does not pass anymore
109 # I don't understand why we should expect thing1 while thing3 is in the cache and in the DB
110 #$dbh->{mock_clear_history} = 1;
111 ## This gives us the value that was cached on the first call, when the cache was active.
112 #is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully from cache");
113 #$history = $dbh->{mock_all_history};
114 #is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
116 $silly_preference->set( { value => 'thing4' } )->store();
117 C4::Context->clear_syspref_cache();
118 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
119 ok($trace_read, 'Retrieved syspref from database');
120 $trace_read = q{};
122 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
123 is( $trace_read, q{}, 'Did not retrieve syspref from database');
124 $trace_read = q{};
126 my $oConnection = C4::Context->Zconn('biblioserver', 0);
127 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
128 $oConnection = C4::Context->Zconn('biblioserver', 1);
129 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
131 $silly_preference->delete();
133 # AutoEmailOpacUser should be a YesNo pref
134 C4::Context->set_preference('AutoEmailOpacUser', '');
135 my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailOpacUser');
136 is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
138 done_testing();
140 sub TransformVersionToNum {
141 my $version = shift;
143 # remove the 3 last . to have a Perl number
144 $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
146 # three X's at the end indicate that you are testing patch with dbrev
147 # change it into 999
148 # prevents error on a < comparison between strings (should be: lt)
149 $version =~ s/XXX$/999/;
150 return $version;