Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More;
6 use Test::MockModule;
7 use vars qw($koha $dbh $config $ret);
8 use t::lib::Mocks;
9
10 use Koha::Database;
11
12 BEGIN {
13
14     # Note: The overall number of tests may vary by configuration.
15     # First we need to check your environmental variables
16     for (qw(KOHA_CONF PERL5LIB)) {
17         ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
18     }
19     use_ok('C4::Context');
20 }
21
22 ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
23
24 $dbh->begin_work;
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('OPACBaseURL', '');
41 $OPACBaseURL = C4::Context->preference('OPACBaseURL');
42 is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
43
44 C4::Context->set_preference('SillyPreference','random');
45 C4::Context->clear_syspref_cache();
46 my $SillyPeference = C4::Context->preference('SillyPreference');
47 is($SillyPeference,'random','SillyPreference saved as specified');
48 C4::Context->clear_syspref_cache();
49 C4::Context->enable_syspref_cache();
50 $dbh->rollback;
51
52 ok($koha = C4::Context->new,  'C4::Context->new');
53 my @keys = keys %$koha;
54 my $width = 0;
55 if (ok(@keys)) { 
56     $width = (sort {$a <=> $b} map {length} @keys)[-1];
57 }
58 foreach (sort @keys) {
59         ok(exists $koha->{$_}, 
60                 '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
61                 . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
62         );
63 }
64 ok($config = $koha->{config}, 'Getting $koha->{config} ');
65
66 # Testing syspref caching
67 use Test::DBIx::Class;
68
69 my $schema = Koha::Database->new()->schema();
70 $schema->storage->debug(1);
71 my $trace_read;
72 open my $trace, '>', \$trace_read or die "Can't open variable: $!";
73 $schema->storage->debugfh( $trace );
74
75 C4::Context->set_preference('SillyPreference', 'thing1');
76 my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
77 is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
78
79 my $pref = C4::Context->preference("SillyPreference");
80 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
81 ok( $trace_read, 'Retrieved syspref from database');
82 $trace_read = q{};
83
84 is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
85 is( $trace_read , q{}, 'Did not retrieve syspref from database');
86 $trace_read = q{};
87
88 C4::Context->disable_syspref_cache();
89 $silly_preference->set( { value => 'thing2' } )->store();
90 is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
91 ok($trace_read, 'Retrieved syspref from database');
92 $trace_read = q{};
93
94 $silly_preference->set( { value => 'thing3' } )->store();
95 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
96 ok($trace_read, 'Retrieved syspref from database');
97 $trace_read = q{};
98
99 C4::Context->enable_syspref_cache();
100 is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
101 isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
102 $trace_read = q{};
103
104 # FIXME This was added by Robin and does not pass anymore
105 # I don't understand why we should expect thing1 while thing3 is in the cache and in the DB
106 #$dbh->{mock_clear_history} = 1;
107 ## This gives us the value that was cached on the first call, when the cache was active.
108 #is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully from cache");
109 #$history = $dbh->{mock_all_history};
110 #is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
111
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{};
117
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{};
121
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");
126
127 $silly_preference->delete();
128
129 # AutoEmailOpacUser should be a YesNo pref
130 C4::Context->set_preference('AutoEmailOpacUser', '');
131 my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailOpacUser');
132 is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
133
134 done_testing();
135
136 sub TransformVersionToNum {
137     my $version = shift;
138
139     # remove the 3 last . to have a Perl number
140     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
141
142     # three X's at the end indicate that you are testing patch with dbrev
143     # change it into 999
144     # prevents error on a < comparison between strings (should be: lt)
145     $version =~ s/XXX$/999/;
146     return $version;
147 }