Bug 31870: Context.t: Reorganize subtests
[koha.git] / t / db_dependent / Context.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 4;
5 use Test::MockModule;
6
7 use t::lib::Mocks;
8 use t::lib::TestBuilder;
9
10 use C4::Context;
11 use Koha::Database;
12
13 our $schema;
14 $schema = Koha::Database->new->schema;
15
16 subtest 'Environment' => sub {
17     plan tests => 2;
18     # First we need to check your environmental variables
19     my $ret;
20     for (qw(KOHA_CONF PERL5LIB)) {
21         ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
22     }
23 };
24
25 subtest 'Tests with preferences' => sub {
26     plan tests => 22;
27     $schema->storage->txn_begin;
28
29     my $dbh;
30     ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
31
32     C4::Context->set_preference('OPACBaseURL','junk');
33     C4::Context->clear_syspref_cache();
34     my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
35     is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
36
37     C4::Context->set_preference('OPACBaseURL','https://junk');
38     C4::Context->clear_syspref_cache();
39     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
40     is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
41
42     C4::Context->set_preference('OPACBaseURL','http://junk2');
43     C4::Context->clear_syspref_cache();
44     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
45     is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
46
47     C4::Context->set_preference('OPACBaseURL', '');
48     $OPACBaseURL = C4::Context->preference('OPACBaseURL');
49     is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
50
51     C4::Context->set_preference('SillyPreference','random');
52     C4::Context->clear_syspref_cache();
53     my $SillyPeference = C4::Context->preference('SillyPreference');
54     is($SillyPeference,'random','SillyPreference saved as specified');
55     C4::Context->clear_syspref_cache();
56     C4::Context->enable_syspref_cache();
57
58     # Testing syspref caching
59     $schema->storage->debug(1);
60     my $trace_read;
61     open my $trace, '>', \$trace_read or die "Can't open variable: $!";
62     $schema->storage->debugfh( $trace );
63
64     C4::Context->set_preference('SillyPreference', 'thing1');
65     my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
66     is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
67
68     my $pref = C4::Context->preference("SillyPreference");
69     is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
70     ok( $trace_read, 'Retrieved syspref from database');
71     $trace_read = q{};
72
73     is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
74     is( $trace_read , q{}, 'Did not retrieve syspref from database');
75     $trace_read = q{};
76
77     C4::Context->disable_syspref_cache();
78     $silly_preference->set( { value => 'thing2' } )->store();
79     is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
80     ok($trace_read, 'Retrieved syspref from database');
81     $trace_read = q{};
82
83     $silly_preference->set( { value => 'thing3' } )->store();
84     is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
85     ok($trace_read, 'Retrieved syspref from database');
86     $trace_read = q{};
87
88     C4::Context->enable_syspref_cache();
89     is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
90     isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
91     $trace_read = q{};
92
93     $silly_preference->set( { value => 'thing4' } )->store();
94     C4::Context->clear_syspref_cache();
95     is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
96     ok($trace_read, 'Retrieved syspref from database');
97     $trace_read = q{};
98
99     is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
100     is( $trace_read, q{}, 'Did not retrieve syspref from database');
101     $trace_read = q{};
102     $silly_preference->delete();
103
104     # AutoEmailNewUser should be a YesNo pref
105     C4::Context->set_preference('AutoEmailNewUser', '');
106     my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
107     is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
108
109     $schema->storage->txn_rollback;
110 };
111
112 subtest 'Check context hash keys' => sub {
113     my $koha;
114     ok($koha = C4::Context->new, 'C4::Context->new');
115     my @keys = keys %$koha;
116     my $width = 0;
117     ok( @keys, 'Expecting entries in context hash' );
118     if( @keys ) {
119         $width = (sort {$a <=> $b} map {length} @keys)[-1];
120     }
121     foreach (sort @keys) {
122         ok(exists $koha->{$_},
123             '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
124             . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
125         );
126     }
127     my $config;
128     ok($config = $koha->{config}, 'Getting $koha->{config} ');
129     done_testing();
130 };
131
132 subtest 'Zconn' => sub {
133     plan tests => 2;
134     my $oConnection = C4::Context->Zconn('biblioserver', 0);
135     isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
136     $oConnection = C4::Context->Zconn('biblioserver', 1);
137     is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
138
139 };