Bug 15601: Fix TestBuilder tests
[koha.git] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::MockModule;
8 use vars qw($debug $koha $dbh $config $ret);
9 use t::lib::Mocks;
10
11 use Koha::Database;
12
13 BEGIN {
14     $debug = $ENV{DEBUG} || 0;
15
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" );
20     }
21     use_ok('C4::Context');
22 }
23
24 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
25
26 $dbh->begin_work;
27 C4::Context->disable_syspref_cache();
28 C4::Context->set_preference('OPACBaseURL','junk');
29 C4::Context->clear_syspref_cache();
30 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
31 is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
32
33 C4::Context->set_preference('OPACBaseURL','https://junk');
34 C4::Context->clear_syspref_cache();
35 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
36 is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
37
38 C4::Context->set_preference('OPACBaseURL','http://junk2');
39 C4::Context->clear_syspref_cache();
40 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
41 is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
42
43 C4::Context->set_preference('SillyPreference','random');
44 C4::Context->clear_syspref_cache();
45 my $SillyPeference = C4::Context->preference('SillyPreference');
46 is($SillyPeference,'random','SillyPreference saved as specified');
47 C4::Context->clear_syspref_cache();
48 C4::Context->enable_syspref_cache();
49 $dbh->rollback;
50
51 ok($koha = C4::Context->new,  'C4::Context->new');
52 my @keys = keys %$koha;
53 my $width = 0;
54 if (ok(@keys)) { 
55     $width = (sort {$a <=> $b} map {length} @keys)[-1];
56     $debug and diag "widest key is $width";
57 }
58 foreach (sort @keys) {
59         ok(exists $koha->{$_}, 
60                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
61                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
62         );
63 }
64 ok($config = $koha->{config}, 'Getting $koha->{config} ');
65
66 # Testing syspref caching
67 t::lib::Mocks::mock_dbh;
68
69 my $history;
70
71 my $schema = Koha::Database->new()->schema();
72 $schema->storage->debug(1);
73 my $trace_read;
74 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
75 $schema->storage->debugfh( $trace );
76
77 C4::Context->set_preference('SillyPreference', 'thing1');
78 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
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 is( $trace_read, q{}, 'Did not retrieve syspref from database');
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
115 my $oConnection = C4::Context->Zconn('biblioserver', 0);
116 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
117 $oConnection = C4::Context->Zconn('biblioserver', 1);
118 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
119
120 $silly_preference->delete();
121
122 done_testing();
123
124 sub TransformVersionToNum {
125     my $version = shift;
126
127     # remove the 3 last . to have a Perl number
128     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
129
130     # three X's at the end indicate that you are testing patch with dbrev
131     # change it into 999
132     # prevents error on a < comparison between strings (should be: lt)
133     $version =~ s/XXX$/999/;
134     return $version;
135 }
136 1;