Bug 10274: UT: Acquisition.t needs to create its own data
[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                 use_ok('C4::Utils', qw/ :all /);
20 }
21
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 ok(
27     TransformVersionToNum( C4::Context->final_linear_version ) <=
28       TransformVersionToNum( C4::Context->KOHAVERSION ),
29     'Final linear version is less than or equal to kohaversion.pl'
30 );
31 my @keys = keys %$koha;
32 diag("Number of keys in \%\$koha: " . scalar @keys); 
33 our $width = 0;
34 if (ok(@keys)) { 
35         $width = maxwidth(@keys);
36         $debug and diag "widest key is $width";
37 }
38 foreach (sort @keys) {
39         ok(exists $koha->{$_}, 
40                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
41                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
42         );
43 }
44 diag "Examining defined key values.";
45 foreach (grep {defined $koha->{$_}} sort @keys) {
46         print "\n";
47         hashdump('$koha->{' . sprintf('%' . $width . 's', $_)  . '}', $koha->{$_});
48 }
49 ok($config = $koha->{config}, 'Getting $koha->{config} ');
50
51 diag "Testing syspref caching.";
52
53 my $dbh = C4::Context->dbh;
54 $dbh->disconnect;
55
56 my $module = new Test::MockModule('C4::Context');
57 $module->mock(
58     '_new_dbh',
59     sub {
60         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
61           || die "Cannot create handle: $DBI::errstr\n";
62         return $dbh;
63     }
64 );
65
66 my $history;
67 $dbh = C4::Context->dbh;
68
69 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
70 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
71 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
72 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
73
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}), 1, 'Retrieved syspref from database');
77
78 $dbh->{mock_clear_history} = 1;
79 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
80 $history = $dbh->{mock_all_history};
81 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
82
83 C4::Context->disable_syspref_cache();
84 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
85 $history = $dbh->{mock_all_history};
86 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
87
88 $dbh->{mock_clear_history} = 1;
89 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
90 $history = $dbh->{mock_all_history};
91 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
92
93 C4::Context->enable_syspref_cache();
94 $dbh->{mock_clear_history} = 1;
95 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
96 $history = $dbh->{mock_all_history};
97 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
98
99 C4::Context->clear_syspref_cache();
100 $dbh->{mock_clear_history} = 1;
101 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
102 $history = $dbh->{mock_all_history};
103 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
104
105 $dbh->{mock_clear_history} = 1;
106 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
107 $history = $dbh->{mock_all_history};
108 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
109
110 done_testing();
111
112 sub TransformVersionToNum {
113     my $version = shift;
114
115     # remove the 3 last . to have a Perl number
116     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
117
118     # three X's at the end indicate that you are testing patch with dbrev
119     # change it into 999
120     # prevents error on a < comparison between strings (should be: lt)
121     $version =~ s/XXX$/999/;
122     return $version;
123 }
124 1;