View | Details | Raw Unified | Return to bug 31870
Collapse All | Expand All

(-)a/t/db_dependent/Context.t (-119 / +106 lines)
Lines 11-17 use Koha::Database; Link Here
11
11
12
BEGIN {
12
BEGIN {
13
    my $ret;
13
    my $ret;
14
    # Note: The overall number of tests may vary by configuration.
15
    # First we need to check your environmental variables
14
    # First we need to check your environmental variables
16
    for (qw(KOHA_CONF PERL5LIB)) {
15
    for (qw(KOHA_CONF PERL5LIB)) {
17
        ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
16
        ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
Lines 23-145 our $schema; Link Here
23
$schema = Koha::Database->new->schema;
22
$schema = Koha::Database->new->schema;
24
23
25
subtest 'Original tests' => sub {
24
subtest 'Original tests' => sub {
26
plan tests => 33;
25
    plan tests => 33;
27
$schema->storage->txn_begin;
26
    $schema->storage->txn_begin;
28
27
29
my $dbh;
28
    my $dbh;
30
ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
29
    ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
31
30
32
C4::Context->set_preference('OPACBaseURL','junk');
31
    C4::Context->set_preference('OPACBaseURL','junk');
33
C4::Context->clear_syspref_cache();
32
    C4::Context->clear_syspref_cache();
34
my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
33
    my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
35
is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
34
    is($OPACBaseURL,'http://junk','OPACBaseURL saved with http:// when missing it');
36
35
37
C4::Context->set_preference('OPACBaseURL','https://junk');
36
    C4::Context->set_preference('OPACBaseURL','https://junk');
38
C4::Context->clear_syspref_cache();
37
    C4::Context->clear_syspref_cache();
39
$OPACBaseURL = C4::Context->preference('OPACBaseURL');
38
    $OPACBaseURL = C4::Context->preference('OPACBaseURL');
40
is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
39
    is($OPACBaseURL,'https://junk','OPACBaseURL saved with https:// as specified');
41
40
42
C4::Context->set_preference('OPACBaseURL','http://junk2');
41
    C4::Context->set_preference('OPACBaseURL','http://junk2');
43
C4::Context->clear_syspref_cache();
42
    C4::Context->clear_syspref_cache();
44
$OPACBaseURL = C4::Context->preference('OPACBaseURL');
43
    $OPACBaseURL = C4::Context->preference('OPACBaseURL');
45
is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
44
    is($OPACBaseURL,'http://junk2','OPACBaseURL saved with http:// as specified');
46
45
47
C4::Context->set_preference('OPACBaseURL', '');
46
    C4::Context->set_preference('OPACBaseURL', '');
48
$OPACBaseURL = C4::Context->preference('OPACBaseURL');
47
    $OPACBaseURL = C4::Context->preference('OPACBaseURL');
49
is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
48
    is($OPACBaseURL,'','OPACBaseURL saved empty as specified');
50
49
51
C4::Context->set_preference('SillyPreference','random');
50
    C4::Context->set_preference('SillyPreference','random');
52
C4::Context->clear_syspref_cache();
51
    C4::Context->clear_syspref_cache();
53
my $SillyPeference = C4::Context->preference('SillyPreference');
52
    my $SillyPeference = C4::Context->preference('SillyPreference');
54
is($SillyPeference,'random','SillyPreference saved as specified');
53
    is($SillyPeference,'random','SillyPreference saved as specified');
55
C4::Context->clear_syspref_cache();
54
    C4::Context->clear_syspref_cache();
56
C4::Context->enable_syspref_cache();
55
    C4::Context->enable_syspref_cache();
57
#$dbh->rollback;
56
58
57
    my $koha;
59
my $koha;
58
    ok($koha = C4::Context->new,  'C4::Context->new');
60
ok($koha = C4::Context->new,  'C4::Context->new');
59
    my @keys = keys %$koha;
61
my @keys = keys %$koha;
60
    my $width = 0;
62
my $width = 0;
61
    ok( @keys, 'Expecting entries in context hash' );
63
ok( @keys, 'Expecting entries in context hash' );
62
    if( @keys ) {
64
if( @keys ) {
63
        $width = (sort {$a <=> $b} map {length} @keys)[-1];
65
    $width = (sort {$a <=> $b} map {length} @keys)[-1];
64
    }
66
}
65
    foreach (sort @keys) {
67
foreach (sort @keys) {
66
        ok(exists $koha->{$_},
68
	ok(exists $koha->{$_}, 
67
            '$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
69
		'$koha->{' . sprintf('%' . $width . 's', $_)  . '} exists '
68
            . ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
70
		. ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
69
        );
71
	);
70
    }
72
}
71
    my $config;
73
my $config;
72
    ok($config = $koha->{config}, 'Getting $koha->{config} ');
74
ok($config = $koha->{config}, 'Getting $koha->{config} ');
73
75
74
    # Testing syspref caching
76
# Testing syspref caching
75
77
76
    $schema->storage->debug(1);
78
#my $schema = Koha::Database->new()->schema();
77
    my $trace_read;
79
$schema->storage->debug(1);
78
    open my $trace, '>', \$trace_read or die "Can't open variable: $!";
80
my $trace_read;
79
    $schema->storage->debugfh( $trace );
81
open my $trace, '>', \$trace_read or die "Can't open variable: $!";
80
82
$schema->storage->debugfh( $trace );
81
    C4::Context->set_preference('SillyPreference', 'thing1');
83
82
    my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
84
C4::Context->set_preference('SillyPreference', 'thing1');
83
    is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
85
my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
84
86
is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
85
    my $pref = C4::Context->preference("SillyPreference");
87
86
    is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
88
my $pref = C4::Context->preference("SillyPreference");
87
    ok( $trace_read, 'Retrieved syspref from database');
89
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
88
    $trace_read = q{};
90
ok( $trace_read, 'Retrieved syspref from database');
89
91
$trace_read = q{};
90
    is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
92
91
    is( $trace_read , q{}, 'Did not retrieve syspref from database');
93
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
92
    $trace_read = q{};
94
is( $trace_read , q{}, 'Did not retrieve syspref from database');
93
95
$trace_read = q{};
94
    C4::Context->disable_syspref_cache();
96
95
    $silly_preference->set( { value => 'thing2' } )->store();
97
C4::Context->disable_syspref_cache();
96
    is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
98
$silly_preference->set( { value => 'thing2' } )->store();
97
    ok($trace_read, 'Retrieved syspref from database');
99
is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
98
    $trace_read = q{};
100
ok($trace_read, 'Retrieved syspref from database');
99
101
$trace_read = q{};
100
    $silly_preference->set( { value => 'thing3' } )->store();
102
101
    is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
103
$silly_preference->set( { value => 'thing3' } )->store();
102
    ok($trace_read, 'Retrieved syspref from database');
104
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
103
    $trace_read = q{};
105
ok($trace_read, 'Retrieved syspref from database');
104
106
$trace_read = q{};
105
    C4::Context->enable_syspref_cache();
107
106
    is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
108
C4::Context->enable_syspref_cache();
107
    isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
109
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
108
    $trace_read = q{};
110
isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
109
111
$trace_read = q{};
110
    $silly_preference->set( { value => 'thing4' } )->store();
112
111
    C4::Context->clear_syspref_cache();
113
# FIXME This was added by Robin and does not pass anymore
112
    is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
114
# I don't understand why we should expect thing1 while thing3 is in the cache and in the DB
113
    ok($trace_read, 'Retrieved syspref from database');
115
#$dbh->{mock_clear_history} = 1;
114
    $trace_read = q{};
116
## This gives us the value that was cached on the first call, when the cache was active.
115
117
#is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully from cache");
116
    is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
118
#$history = $dbh->{mock_all_history};
117
    is( $trace_read, q{}, 'Did not retrieve syspref from database');
119
#is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
118
    $trace_read = q{};
120
119
121
$silly_preference->set( { value => 'thing4' } )->store();
120
    my $oConnection = C4::Context->Zconn('biblioserver', 0);
122
C4::Context->clear_syspref_cache();
121
    isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
123
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
122
    $oConnection = C4::Context->Zconn('biblioserver', 1);
124
ok($trace_read, 'Retrieved syspref from database');
123
    is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
125
$trace_read = q{};
124
126
125
    $silly_preference->delete();
127
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
126
128
is( $trace_read, q{}, 'Did not retrieve syspref from database');
127
    # AutoEmailNewUser should be a YesNo pref
129
$trace_read = q{};
128
    C4::Context->set_preference('AutoEmailNewUser', '');
130
129
    my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
131
my $oConnection = C4::Context->Zconn('biblioserver', 0);
130
    is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
132
isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
133
$oConnection = C4::Context->Zconn('biblioserver', 1);
134
is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
135
136
$silly_preference->delete();
137
138
# AutoEmailNewUser should be a YesNo pref
139
C4::Context->set_preference('AutoEmailNewUser', '');
140
my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailNewUser');
141
is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' );
142
143
131
144
    $schema->storage->txn_rollback;
132
    $schema->storage->txn_rollback;
145
};
133
};
146
- 

Return to bug 31870