4 use Test::More tests => 4;
8 use t::lib::TestBuilder;
14 $schema = Koha::Database->new->schema;
16 subtest 'Environment' => sub {
18 # First we need to check your environmental variables
20 for (qw(KOHA_CONF PERL5LIB)) {
21 ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
25 subtest 'Tests with preferences' => 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();
58 # Testing syspref caching
59 $schema->storage->debug(1);
61 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
62 $schema->storage->debugfh( $trace );
64 C4::Context->set_preference('SillyPreference', 'thing1');
65 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
66 is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
68 my $pref = C4::Context->preference("SillyPreference");
69 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
70 ok( $trace_read, 'Retrieved syspref from database');
73 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
74 is( $trace_read , q{}, 'Did not retrieve syspref from database');
77 C4::Context->disable_syspref_cache();
78 $silly_preference->set( { value => 'thing2' } )->store();
79 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
80 ok($trace_read, 'Retrieved syspref from database');
83 $silly_preference->set( { value => 'thing3' } )->store();
84 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
85 ok($trace_read, 'Retrieved syspref from database');
88 C4::Context->enable_syspref_cache();
89 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
90 isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
93 $silly_preference->set( { value => 'thing4' } )->store();
94 C4::Context->clear_syspref_cache();
95 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
96 ok($trace_read, 'Retrieved syspref from database');
99 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
100 is( $trace_read, q{}, 'Did not retrieve syspref from database');
102 $silly_preference->delete();
104 # AutoEmailNewUser should be a YesNo pref
105 C4::Context->set_preference('AutoEmailNewUser', '');
106 my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
107 is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
109 $schema->storage->txn_rollback;
112 subtest 'Check context hash keys' => sub {
114 ok($koha = C4::Context->new, 'C4::Context->new');
115 my @keys = keys %$koha;
117 ok( @keys, 'Expecting entries in context hash' );
119 $width = (sort {$a <=> $b} map {length} @keys)[-1];
121 foreach (sort @keys) {
122 ok(exists $koha->{$_},
123 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
124 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
128 ok($config = $koha->{config}, 'Getting $koha->{config} ');
132 subtest 'Zconn' => sub {
134 my $oConnection = C4::Context->Zconn('biblioserver', 0);
135 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
136 $oConnection = C4::Context->Zconn('biblioserver', 1);
137 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");