4 use Test::More tests => 4;
8 use t::lib::TestBuilder;
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" );
19 use_ok('C4::Context');
23 $schema = Koha::Database->new->schema;
25 subtest 'Original tests' => sub {
27 $schema->storage->txn_begin;
30 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
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');
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');
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');
47 C4::Context->set_preference('OPACBaseURL', '');
48 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
49 is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
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();
60 ok($koha = C4::Context->new, 'C4::Context->new');
61 my @keys = keys %$koha;
63 ok( @keys, 'Expecting entries in context hash' );
65 $width = (sort {$a <=> $b} map {length} @keys)[-1];
67 foreach (sort @keys) {
68 ok(exists $koha->{$_},
69 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
70 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
74 ok($config = $koha->{config}, 'Getting $koha->{config} ');
76 # Testing syspref caching
78 #my $schema = Koha::Database->new()->schema();
79 $schema->storage->debug(1);
81 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
82 $schema->storage->debugfh( $trace );
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' );
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');
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');
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');
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');
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');
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');
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');
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');
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");
136 $silly_preference->delete();
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' );
144 $schema->storage->txn_rollback;