9 use vars qw($debug $koha $dbh $config $ret);
12 $debug = $ENV{DEBUG} || 0;
13 diag("Note: The overall number of tests may vary by configuration.");
14 diag("First we need to check your environmental variables");
15 for (qw(KOHA_CONF PERL5LIB)) {
16 ok($ret = $ENV{$_}, "ENV{$_} = $ret");
18 use_ok('C4::Context');
19 use_ok('C4::Utils', qw/ :all /);
22 ok($koha = C4::Context->new, 'C4::Context->new');
23 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
24 ok($ret = C4::Context->KOHAVERSION, ' (function) KOHAVERSION = ' . ($ret||''));
25 ok($ret = $koha->KOHAVERSION, ' $koha->KOHAVERSION = ' . ($ret||''));
26 my @keys = keys %$koha;
27 diag("Number of keys in \%\$koha: " . scalar @keys);
30 $width = maxwidth(@keys);
31 $debug and diag "widest key is $width";
33 foreach (sort @keys) {
34 ok(exists $koha->{$_},
35 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
36 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
39 diag "Examining defined key values.";
40 foreach (grep {defined $koha->{$_}} sort @keys) {
42 hashdump('$koha->{' . sprintf('%' . $width . 's', $_) . '}', $koha->{$_});
44 ok($config = $koha->{config}, 'Getting $koha->{config} ');
46 diag "Testing syspref caching.";
48 my $dbh = C4::Context->dbh;
51 my $module = new Test::MockModule('C4::Context');
55 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
56 || die "Cannot create handle: $DBI::errstr\n";
62 $dbh = C4::Context->dbh;
64 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
65 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
66 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
67 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
69 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
70 $history = $dbh->{mock_all_history};
71 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
73 $dbh->{mock_clear_history} = 1;
74 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
75 $history = $dbh->{mock_all_history};
76 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
78 C4::Context->disable_syspref_cache();
79 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
80 $history = $dbh->{mock_all_history};
81 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
83 $dbh->{mock_clear_history} = 1;
84 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
85 $history = $dbh->{mock_all_history};
86 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
88 C4::Context->enable_syspref_cache();
89 $dbh->{mock_clear_history} = 1;
90 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
91 $history = $dbh->{mock_all_history};
92 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
94 C4::Context->clear_syspref_cache();
95 $dbh->{mock_clear_history} = 1;
96 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
97 $history = $dbh->{mock_all_history};
98 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
100 $dbh->{mock_clear_history} = 1;
101 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
102 $history = $dbh->{mock_all_history};
103 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');