Bug 14195: TestBuilder - A random string should not be longer than the DB field
[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         # Note: The overall number of tests may vary by configuration.
14         # 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 my @keys = keys %$koha;
24 my $width = 0;
25 if (ok(@keys)) { 
26     $width = (sort {$a <=> $b} map {length} @keys)[-1];
27     $debug and diag "widest key is $width";
28 }
29 foreach (sort @keys) {
30         ok(exists $koha->{$_}, 
31                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
32                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
33         );
34 }
35 ok($config = $koha->{config}, 'Getting $koha->{config} ');
36
37 # Testing syspref caching
38 my $module = new Test::MockModule('C4::Context');
39 $module->mock(
40     '_new_dbh',
41     sub {
42         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
43           || die "Cannot create handle: $DBI::errstr\n";
44         return $dbh;
45     }
46 );
47
48 my $history;
49 $dbh = C4::Context->dbh({ new => 1 });
50
51 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
52 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
53 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
54 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
55
56 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
57 $history = $dbh->{mock_all_history};
58 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
59
60 $dbh->{mock_clear_history} = 1;
61 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
62 $history = $dbh->{mock_all_history};
63 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
64
65 C4::Context->disable_syspref_cache();
66 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
67 $history = $dbh->{mock_all_history};
68 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
69
70 $dbh->{mock_clear_history} = 1;
71 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
72 $history = $dbh->{mock_all_history};
73 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
74
75 C4::Context->enable_syspref_cache();
76 $dbh->{mock_clear_history} = 1;
77 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
78 $history = $dbh->{mock_all_history};
79 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
80
81 C4::Context->clear_syspref_cache();
82 $dbh->{mock_clear_history} = 1;
83 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
84 $history = $dbh->{mock_all_history};
85 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
86
87 $dbh->{mock_clear_history} = 1;
88 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
89 $history = $dbh->{mock_all_history};
90 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
91
92 my $oConnection = C4::Context->Zconn('biblioserver', 0);
93 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
94 $oConnection = C4::Context->Zconn('biblioserver', 1);
95 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
96
97 done_testing();
98
99 sub TransformVersionToNum {
100     my $version = shift;
101
102     # remove the 3 last . to have a Perl number
103     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
104
105     # three X's at the end indicate that you are testing patch with dbrev
106     # change it into 999
107     # prevents error on a < comparison between strings (should be: lt)
108     $version =~ s/XXX$/999/;
109     return $version;
110 }
111 1;