Bug 31870: Groundwork for Context.t
[srvgit] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 4;
5 use Test::MockModule;
6
7 use t::lib::Mocks;
8 use t::lib::TestBuilder;
9
10 use Koha::Database;
11
12 BEGIN {
13     my $ret;
14     # Note: The overall number of tests may vary by configuration.
15     # First we need to check your environmental variables
16     for (qw(KOHA_CONF PERL5LIB)) {
17         ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
18     }
19     use_ok('C4::Context');
20 }
21
22 our $schema;
23 $schema = Koha::Database->new->schema;
24
25 subtest 'Original tests' => sub {
26 plan tests => 33;
27 $schema->storage->txn_begin;
28
29 my $dbh;
30 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
31
32 C4::Context->set_preference('OPACBaseURL','junk');
33 C4::Context->clear_syspref_cache();
34 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
35 is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
36
37 C4::Context->set_preference('OPACBaseURL','https://junk');
38 C4::Context->clear_syspref_cache();
39 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
40 is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
41
42 C4::Context->set_preference('OPACBaseURL','http://junk2');
43 C4::Context->clear_syspref_cache();
44 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
45 is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
46
47 C4::Context->set_preference('OPACBaseURL', '');
48 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
49 is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
50
51 C4::Context->set_preference('SillyPreference','random');
52 C4::Context->clear_syspref_cache();
53 my $SillyPeference = C4::Context->preference('SillyPreference');
54 is($SillyPeference,'random','SillyPreference saved as specified');
55 C4::Context->clear_syspref_cache();
56 C4::Context->enable_syspref_cache();
57 #$dbh->rollback;
58
59 my $koha;
60 ok($koha = C4::Context->new,  'C4::Context->new');
61 my @keys = keys %$koha;
62 my $width = 0;
63 ok( @keys, 'Expecting entries in context hash' );
64 if( @keys ) {
65     $width = (sort {$a <=> $b} map {length} @keys)[-1];
66 }
67 foreach (sort @keys) {
68         ok(exists $koha->{$_}, 
69                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
70                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
71         );
72 }
73 my $config;
74 ok($config = $koha->{config}, 'Getting $koha->{config} ');
75
76 # Testing syspref caching
77
78 #my $schema = Koha::Database->new()->schema();
79 $schema->storage->debug(1);
80 my $trace_read;
81 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
82 $schema->storage->debugfh( $trace );
83
84 C4::Context->set_preference('SillyPreference', 'thing1');
85 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
86 is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
87
88 my $pref = C4::Context->preference("SillyPreference");
89 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
90 ok( $trace_read, 'Retrieved syspref from database');
91 $trace_read = q{};
92
93 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
94 is( $trace_read , q{}, 'Did not retrieve syspref from database');
95 $trace_read = q{};
96
97 C4::Context->disable_syspref_cache();
98 $silly_preference->set( { value => 'thing2' } )->store();
99 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
100 ok($trace_read, 'Retrieved syspref from database');
101 $trace_read = q{};
102
103 $silly_preference->set( { value => 'thing3' } )->store();
104 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
105 ok($trace_read, 'Retrieved syspref from database');
106 $trace_read = q{};
107
108 C4::Context->enable_syspref_cache();
109 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
110 isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
111 $trace_read = q{};
112
113 # FIXME This was added by Robin and does not pass anymore
114 # I don't understand why we should expect thing1 while thing3 is in the cache and in the DB
115 #$dbh->{mock_clear_history} = 1;
116 ## This gives us the value that was cached on the first call, when the cache was active.
117 #is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully from cache");
118 #$history = $dbh->{mock_all_history};
119 #is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
120
121 $silly_preference->set( { value => 'thing4' } )->store();
122 C4::Context->clear_syspref_cache();
123 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
124 ok($trace_read, 'Retrieved syspref from database');
125 $trace_read = q{};
126
127 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
128 is( $trace_read, q{}, 'Did not retrieve syspref from database');
129 $trace_read = q{};
130
131 my $oConnection = C4::Context->Zconn('biblioserver', 0);
132 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
133 $oConnection = C4::Context->Zconn('biblioserver', 1);
134 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
135
136 $silly_preference->delete();
137
138 # AutoEmailNewUser should be a YesNo pref
139 C4::Context->set_preference('AutoEmailNewUser', '');
140 my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
141 is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
142
143
144     $schema->storage->txn_rollback;
145 };