Bug 10611: Use mysql_auto_reconnect instead of ping
[koha.git] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2 #
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Test::MockModule;
9 use vars qw($debug $koha $dbh $config $ret);
10
11 BEGIN {
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");
17                 }
18                 use_ok('C4::Context');
19 }
20
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||''));
25 ok(
26     TransformVersionToNum( C4::Context->final_linear_version ) <=
27       TransformVersionToNum( C4::Context->KOHAVERSION ),
28     'Final linear version is less than or equal to kohaversion.pl'
29 );
30 my @keys = keys %$koha;
31 diag("Number of keys in \%\$koha: " . scalar @keys); 
32 my $width = 0;
33 if (ok(@keys)) { 
34     $width = (sort {$a <=> $b} map {length} @keys)[-1];
35     $debug and diag "widest key is $width";
36 }
37 foreach (sort @keys) {
38         ok(exists $koha->{$_}, 
39                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
40                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
41         );
42 }
43 ok($config = $koha->{config}, 'Getting $koha->{config} ');
44
45 diag "Testing syspref caching.";
46
47 my $module = new Test::MockModule('C4::Context');
48 $module->mock(
49     '_new_dbh',
50     sub {
51         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
52           || die "Cannot create handle: $DBI::errstr\n";
53         return $dbh;
54     }
55 );
56
57 my $history;
58 $dbh = C4::Context->dbh({ new => 1 });
59
60 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
61 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
62 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
63 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
64
65 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
66 $history = $dbh->{mock_all_history};
67 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
68
69 $dbh->{mock_clear_history} = 1;
70 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
71 $history = $dbh->{mock_all_history};
72 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
73
74 C4::Context->disable_syspref_cache();
75 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
76 $history = $dbh->{mock_all_history};
77 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
78
79 $dbh->{mock_clear_history} = 1;
80 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
81 $history = $dbh->{mock_all_history};
82 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
83
84 C4::Context->enable_syspref_cache();
85 $dbh->{mock_clear_history} = 1;
86 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
87 $history = $dbh->{mock_all_history};
88 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
89
90 C4::Context->clear_syspref_cache();
91 $dbh->{mock_clear_history} = 1;
92 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
93 $history = $dbh->{mock_all_history};
94 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
95
96 $dbh->{mock_clear_history} = 1;
97 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
98 $history = $dbh->{mock_all_history};
99 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
100
101 done_testing();
102
103 sub TransformVersionToNum {
104     my $version = shift;
105
106     # remove the 3 last . to have a Perl number
107     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
108
109     # three X's at the end indicate that you are testing patch with dbrev
110     # change it into 999
111     # prevents error on a < comparison between strings (should be: lt)
112     $version =~ s/XXX$/999/;
113     return $version;
114 }
115 1;