Bug 14778: Make 3 tests pass
[koha.git] / t / db_dependent / Context.t
blob4541fa4b44050c355114601621b4f5573241b6f4
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Test::More;
7 use Test::MockModule;
8 use vars qw($debug $koha $dbh $config $ret);
10 use Koha::Database;
12 BEGIN {
13 $debug = $ENV{DEBUG} || 0;
15 # Note: The overall number of tests may vary by configuration.
16 # First we need to check your environmental variables
17 for (qw(KOHA_CONF PERL5LIB)) {
18 ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
20 use_ok('C4::Context');
23 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
25 $dbh->begin_work;
26 C4::Context->disable_syspref_cache();
27 C4::Context->set_preference('OPACBaseURL','junk');
28 C4::Context->clear_syspref_cache();
29 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
30 is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
32 C4::Context->set_preference('OPACBaseURL','https://junk');
33 C4::Context->clear_syspref_cache();
34 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
35 is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
37 C4::Context->set_preference('OPACBaseURL','http://junk2');
38 C4::Context->clear_syspref_cache();
39 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
40 is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
42 C4::Context->set_preference('SillyPreference','random');
43 C4::Context->clear_syspref_cache();
44 my $SillyPeference = C4::Context->preference('SillyPreference');
45 is($SillyPeference,'random','SillyPreference saved as specified');
46 C4::Context->clear_syspref_cache();
47 C4::Context->enable_syspref_cache();
48 $dbh->rollback;
50 ok($koha = C4::Context->new, 'C4::Context->new');
51 my @keys = keys %$koha;
52 my $width = 0;
53 if (ok(@keys)) {
54 $width = (sort {$a <=> $b} map {length} @keys)[-1];
55 $debug and diag "widest key is $width";
57 foreach (sort @keys) {
58 ok(exists $koha->{$_},
59 '$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
60 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
63 ok($config = $koha->{config}, 'Getting $koha->{config} ');
65 # Testing syspref caching
66 my $module = new Test::MockModule('C4::Context');
67 $module->mock(
68 '_new_dbh',
69 sub {
70 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
71 || die "Cannot create handle: $DBI::errstr\n";
72 return $dbh;
76 my $history;
78 my $schema = Koha::Database->new()->schema();
79 $schema->storage->debug(1);
80 my $trace_read;
81 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
82 $schema->storage->debugfh( $trace );
84 C4::Context->set_preference('SillyPreference', 'thing1');
85 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
87 my $pref = C4::Context->preference("SillyPreference");
88 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
89 ok( $trace_read, 'Retrieved syspref from database');
90 $trace_read = q{};
92 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
93 is( $trace_read , q{}, 'Did not retrieve syspref from database');
94 $trace_read = q{};
96 C4::Context->disable_syspref_cache();
97 $silly_preference->set( { value => 'thing2' } )->store();
98 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
99 ok($trace_read, 'Retrieved syspref from database');
100 $trace_read = q{};
102 $silly_preference->set( { value => 'thing3' } )->store();
103 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
104 ok($trace_read, 'Retrieved syspref from database');
105 $trace_read = q{};
107 C4::Context->enable_syspref_cache();
108 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
109 is( $trace_read, q{}, 'Did not retrieve syspref from database');
110 $trace_read = q{};
112 $silly_preference->set( { value => 'thing4' } )->store();
113 C4::Context->clear_syspref_cache();
114 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
115 ok($trace_read, 'Retrieved syspref from database');
116 $trace_read = q{};
118 is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
119 is( $trace_read, q{}, 'Did not retrieve syspref from database');
120 $trace_read = q{};
122 my $oConnection = C4::Context->Zconn('biblioserver', 0);
123 isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
124 $oConnection = C4::Context->Zconn('biblioserver', 1);
125 is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
127 $silly_preference->delete();
129 done_testing();
131 sub TransformVersionToNum {
132 my $version = shift;
134 # remove the 3 last . to have a Perl number
135 $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
137 # three X's at the end indicate that you are testing patch with dbrev
138 # change it into 999
139 # prevents error on a < comparison between strings (should be: lt)
140 $version =~ s/XXX$/999/;
141 return $version;