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');
21 ok($koha = C4::Context->new, 'C4::Context->new');
22 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
23 ok($ret = C4::Context->KOHAVERSION, ' (function) KOHAVERSION = ' . ($ret||''));
24 ok($ret = $koha->KOHAVERSION, ' $koha->KOHAVERSION = ' . ($ret||''));
26 TransformVersionToNum( C4::Context->final_linear_version ) <=
27 TransformVersionToNum( C4::Context->KOHAVERSION ),
28 'Final linear version is less than or equal to kohaversion.pl'
30 my @keys = keys %$koha;
31 diag("Number of keys in \%\$koha: " . scalar @keys);
34 $width = (sort {$a <=> $b} map {length} @keys)[-1];
35 $debug and diag "widest key is $width";
37 foreach (sort @keys) {
38 ok(exists $koha->{$_},
39 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
40 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
43 ok($config = $koha->{config}, 'Getting $koha->{config} ');
45 diag "Testing syspref caching.";
47 my $dbh = C4::Context->dbh;
50 my $module = new Test::MockModule('C4::Context');
54 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
55 || die "Cannot create handle: $DBI::errstr\n";
61 $dbh = C4::Context->dbh;
63 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
64 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
65 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
66 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
68 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
69 $history = $dbh->{mock_all_history};
70 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
72 $dbh->{mock_clear_history} = 1;
73 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
74 $history = $dbh->{mock_all_history};
75 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
77 C4::Context->disable_syspref_cache();
78 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
79 $history = $dbh->{mock_all_history};
80 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
82 $dbh->{mock_clear_history} = 1;
83 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
84 $history = $dbh->{mock_all_history};
85 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
87 C4::Context->enable_syspref_cache();
88 $dbh->{mock_clear_history} = 1;
89 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
90 $history = $dbh->{mock_all_history};
91 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
93 C4::Context->clear_syspref_cache();
94 $dbh->{mock_clear_history} = 1;
95 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
96 $history = $dbh->{mock_all_history};
97 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
99 $dbh->{mock_clear_history} = 1;
100 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
101 $history = $dbh->{mock_all_history};
102 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
106 sub TransformVersionToNum {
109 # remove the 3 last . to have a Perl number
110 $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
112 # three X's at the end indicate that you are testing patch with dbrev
114 # prevents error on a < comparison between strings (should be: lt)
115 $version =~ s/XXX$/999/;