Bug 5010: Fix OPACBaseURL to include protocol
[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($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
22
23 $dbh->begin_work;
24 C4::Context->disable_syspref_cache();
25 C4::Context->set_preference('OPACBaseURL','junk');
26 C4::Context->clear_syspref_cache();
27 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
28 is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
29
30 C4::Context->set_preference('OPACBaseURL','https://junk');
31 C4::Context->clear_syspref_cache();
32 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
33 is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
34
35 C4::Context->set_preference('OPACBaseURL','http://junk2');
36 C4::Context->clear_syspref_cache();
37 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
38 is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
39
40 C4::Context->set_preference('SillyPreference','random');
41 C4::Context->clear_syspref_cache();
42 my $SillyPeference = C4::Context->preference('SillyPreference');
43 is($SillyPeference,'random','SillyPreference saved as specified');
44 C4::Context->clear_syspref_cache();
45 C4::Context->enable_syspref_cache();
46 $dbh->rollback;
47
48 ok($koha = C4::Context->new,  'C4::Context->new');
49 my @keys = keys %$koha;
50 my $width = 0;
51 if (ok(@keys)) { 
52     $width = (sort {$a <=> $b} map {length} @keys)[-1];
53     $debug and diag "widest key is $width";
54 }
55 foreach (sort @keys) {
56         ok(exists $koha->{$_}, 
57                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
58                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
59         );
60 }
61 ok($config = $koha->{config}, 'Getting $koha->{config} ');
62
63 # Testing syspref caching
64 my $module = new Test::MockModule('C4::Context');
65 $module->mock(
66     '_new_dbh',
67     sub {
68         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
69           || die "Cannot create handle: $DBI::errstr\n";
70         return $dbh;
71     }
72 );
73
74 my $history;
75 $dbh = C4::Context->dbh({ new => 1 });
76
77 $dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
78 $dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
79 $dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
80 $dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
81
82 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
83 $history = $dbh->{mock_all_history};
84 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
85
86 $dbh->{mock_clear_history} = 1;
87 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
88 $history = $dbh->{mock_all_history};
89 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
90
91 C4::Context->disable_syspref_cache();
92 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled 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"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
98 $history = $dbh->{mock_all_history};
99 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
100
101 C4::Context->enable_syspref_cache();
102 $dbh->{mock_clear_history} = 1;
103 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
104 $history = $dbh->{mock_all_history};
105 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
106
107 C4::Context->clear_syspref_cache();
108 $dbh->{mock_clear_history} = 1;
109 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
110 $history = $dbh->{mock_all_history};
111 is(scalar(@{$history}), 1, 'Retrieved syspref from database');
112
113 $dbh->{mock_clear_history} = 1;
114 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
115 $history = $dbh->{mock_all_history};
116 is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
117
118 my $oConnection = C4::Context->Zconn('biblioserver', 0);
119 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
120 $oConnection = C4::Context->Zconn('biblioserver', 1);
121 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
122
123 done_testing();
124
125 sub TransformVersionToNum {
126     my $version = shift;
127
128     # remove the 3 last . to have a Perl number
129     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
130
131     # three X's at the end indicate that you are testing patch with dbrev
132     # change it into 999
133     # prevents error on a < comparison between strings (should be: lt)
134     $version =~ s/XXX$/999/;
135     return $version;
136 }
137 1;