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

(-)a/C4/Context.pm (-1 / +1 lines)
Lines 533-539 sub set_preference { Link Here
533
    }
533
    }
534
534
535
    if ( $use_syspref_cache ) {
535
    if ( $use_syspref_cache ) {
536
        $syspref_cache->set_in_cache( "syspref_$variable", $value );
536
        $syspref_cache->set_in_cache( "syspref_$variable", $syspref->value );
537
    }
537
    }
538
538
539
    return $syspref;
539
    return $syspref;
(-)a/Koha/Config/SysPref.pm (-2 / +103 lines)
Lines 20-27 package Koha::Config::SysPref; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Carp;
22
use Carp;
23
use YAML::Syck;
23
24
24
use Koha::Database;
25
use Koha::Database;
26
use Koha::Exceptions::Config;
25
27
26
use C4::Log;
28
use C4::Log;
27
29
Lines 73-81 sub store { Link Here
73
75
74
    my $action = $self->in_storage ? 'MODIFY' : 'ADD';
76
    my $action = $self->in_storage ? 'MODIFY' : 'ADD';
75
77
76
    C4::Log::logaction( 'SYSTEMPREFERENCE', $action, undef, $self->variable . ' | ' . $self->value );
78
    my $value = $self->value;
79
    if (ref($value)) {
80
        $value = $self->_dump_datatypes($value);
81
    }
82
83
    C4::Log::logaction( 'SYSTEMPREFERENCE', $action, undef, $self->variable . ' | ' . $value );
84
85
    $self->SUPER::store($self);
86
    $self->set({ value => $self->_load_datatypes($value) }) unless ref($value);
77
87
78
    return $self->SUPER::store($self);
88
    return $self;
79
}
89
}
80
90
81
=head3 delete
91
=head3 delete
Lines 94-99 sub delete { Link Here
94
    return $deleted;
104
    return $deleted;
95
}
105
}
96
106
107
=head3 value
108
109
=cut
110
111
sub value {
112
    my $self = shift;
113
114
    my $value = $self->SUPER::value(@_);
115
    return $value unless $value;
116
117
    return $self->_load_datatypes($value);
118
}
119
120
=head3 _dump_datatypes
121
122
=cut
123
124
sub _dump_datatypes {
125
    my ($self, $value) = @_;
126
127
    $value = $self->_dump_yaml_or_json($value);
128
129
    return $value;
130
}
131
132
=head3 _load_datatypes
133
134
=cut
135
136
sub _load_datatypes {
137
    my ($self, $value) = @_;
138
139
    $value = $self->_load_yaml_or_json($value);
140
141
    return $value;
142
}
143
144
=head3 _load_yaml_or_json
145
146
Loads system preference of YAML / JSON type.
147
148
Throws Koha::Exceptions::Config::InvalidSyntax if not valid YAML / JSON.
149
150
=cut
151
152
sub _load_yaml_or_json {
153
    my ($self, $value) = @_;
154
155
    my $type = defined $self->type ? lc($self->type) : '';
156
    return $value if $type ne 'yaml' and $type ne 'json';
157
    if (!ref($value)) {
158
        eval { $value = Load($value); };
159
    } else {
160
        return $value;
161
    }
162
    if ($@) {
163
        Koha::Exceptions::Config::InvalidSyntax->throw(
164
            error => $@,
165
            value => $value,
166
        );
167
    }
168
    return $value;
169
}
170
171
=head3 _dump_yaml_or_json
172
173
Dumps system preference of YAML / JSON type.
174
175
Throws Koha::Exceptions::Config::InvalidSyntax if not valid YAML / JSON.
176
177
=cut
178
179
sub _dump_yaml_or_json {
180
    my ($self, $value) = @_;
181
182
    my $type = defined $self->type ? lc($self->type) : '';
183
    return $value if $type ne 'yaml' and $type ne 'json';
184
    if (ref($value)) {
185
        eval { $value = Dump($value); };
186
    } else {
187
        return $value;
188
    }
189
    if ($@) {
190
        Koha::Exceptions::Config::InvalidSyntax->throw(
191
            error => $@,
192
            value => $value,
193
        );
194
    }
195
    return $value;
196
}
197
97
=head3 type
198
=head3 type
98
199
99
=cut
200
=cut
(-)a/Koha/Exceptions/Config.pm (+5 lines)
Lines 7-12 use Exception::Class ( Link Here
7
    'Koha::Exceptions::Config' => {
7
    'Koha::Exceptions::Config' => {
8
        description => 'Something went wrong!',
8
        description => 'Something went wrong!',
9
    },
9
    },
10
    'Koha::Exceptions::Config::InvalidSyntax' => {
11
        isa => 'Koha::Exceptions::Config',
12
        description => 'The configuration value has invalid syntax',
13
        fields => ['value'],
14
    },
10
    'Koha::Exceptions::Config::MissingEntry' => {
15
    'Koha::Exceptions::Config::MissingEntry' => {
11
        isa => 'Koha::Exceptions::Config',
16
        isa => 'Koha::Exceptions::Config',
12
        description => 'The required entry is missing in the configuration file'
17
        description => 'The required entry is missing in the configuration file'
(-)a/t/db_dependent/Context.t (-52 / +139 lines)
Lines 8-13 use vars qw($debug $koha $dbh $config $ret); Link Here
8
use t::lib::Mocks;
8
use t::lib::Mocks;
9
9
10
use Koha::Database;
10
use Koha::Database;
11
use Koha::Caches;
11
12
12
BEGIN {
13
BEGIN {
13
    $debug = $ENV{DEBUG} || 0;
14
    $debug = $ENV{DEBUG} || 0;
Lines 48-53 my $SillyPeference = C4::Context->preference('SillyPreference'); Link Here
48
is($SillyPeference,'random','SillyPreference saved as specified');
49
is($SillyPeference,'random','SillyPreference saved as specified');
49
C4::Context->clear_syspref_cache();
50
C4::Context->clear_syspref_cache();
50
C4::Context->enable_syspref_cache();
51
C4::Context->enable_syspref_cache();
52
53
subtest 'test system preference of type YAML or JSON' => sub {
54
    my ($hashref, $arrayref, $string, $pref);
55
    my $cache = Koha::Caches->get_instance('syspref');
56
57
    subtest 'Invalid syntax' => sub {
58
        my $notyaml = ': - asd';
59
        my $notjson = '{"test"": { "fail\' ]}';
60
        eval {
61
            C4::Context->set_preference('YAMLpref', $notyaml, '', 'yaml', '');
62
        };
63
        is(ref($@), 'Koha::Exceptions::Config::InvalidSyntax',
64
           'Invalid YAML syntax');
65
        eval {
66
            C4::Context->set_preference('YAMLpref', $notjson, '', 'json', '');
67
        };
68
        is(ref($@), 'Koha::Exceptions::Config::InvalidSyntax',
69
           'Invalid JSON syntax');
70
        C4::Context->set_preference('Normalpref', $notyaml, '', 'Free', '');
71
        is(C4::Context->preference('Normalpref'), $notyaml, 'Does not affect '
72
           .'preferences with type != json || yaml');
73
    };
74
75
    subtest 'test YAML' => sub {
76
        plan skip_all => 'Cache not active' unless $cache->is_cache_active();
77
78
        $hashref = qq/---
79
        test:
80
          test: 1
81
        /;
82
        $arrayref = qq/---
83
        [much, elements, in, this, very, dogearray]
84
        /;
85
        $string = qq/---
86
        wat
87
        /;
88
89
        # Check HASHref
90
        C4::Context->set_preference('YAMLpref', $hashref, '', 'yaml', '');
91
        $pref = C4::Context->preference('YAMLpref');
92
        is($pref->{test}->{test}, 1, 'Got hashref');
93
        is_deeply($cache->get_from_cache("syspref_yamlpref"), $pref, 'is_deeply');
94
95
        # Check ARRAYref
96
        C4::Context->set_preference('YAMLpref', $arrayref, '', 'yaml', '');
97
        $pref = C4::Context->preference('YAMLpref');
98
        is($pref->[5], 'dogearray', 'Got arrayref');
99
        is_deeply($cache->get_from_cache("syspref_yamlpref"), $pref, 'is_deeply');
100
101
        # Check string
102
        C4::Context->set_preference('YAMLpref', $string, '', 'yaml', '');
103
        $pref = C4::Context->preference('YAMLpref');
104
        is($pref, 'wat', 'Got string');
105
        is_deeply($cache->get_from_cache("syspref_yamlpref"), $pref, 'is_deeply');
106
    };
107
    subtest 'test JSON' => sub {
108
        plan skip_all => 'Cache not active' unless $cache->is_cache_active();
109
110
        $hashref = qq/{"test": {"test": 1}}/;
111
        $arrayref = qq/["much", "elements", "in", "this", "very", "dogearray"]/;
112
        $string = qq/"wat"/;
113
114
        # Check HASHref
115
        C4::Context->set_preference('JSONpref', $hashref, '', 'json', '');
116
        $pref = C4::Context->preference('JSONpref');
117
        is($pref->{test}->{test}, 1, 'Got hashref');
118
        is_deeply($cache->get_from_cache("syspref_jsonpref"), $pref, 'is_deeply');
119
120
        # Check ARRAYref
121
        C4::Context->set_preference('JSONpref', $arrayref, '', 'json', '');
122
        $pref = C4::Context->preference('JSONpref');
123
        is($pref->[5], 'dogearray', 'Got arrayref');
124
        is_deeply($cache->get_from_cache("syspref_jsonpref"), $pref, 'is_deeply');
125
126
        # Check string
127
        C4::Context->set_preference('JSONpref', $string, '', 'json', '');
128
        $pref = C4::Context->preference('JSONpref');
129
        is($pref, 'wat', 'Got string');
130
        is_deeply($cache->get_from_cache("syspref_jsonpref"), $pref, 'is_deeply');
131
    };
132
};
133
51
$dbh->rollback;
134
$dbh->rollback;
52
135
53
ok($koha = C4::Context->new,  'C4::Context->new');
136
ok($koha = C4::Context->new,  'C4::Context->new');
Lines 63-135 foreach (sort @keys) { Link Here
63
		. ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
146
		. ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
64
	);
147
	);
65
}
148
}
66
ok($config = $koha->{config}, 'Getting $koha->{config} ');
67
149
68
# Testing syspref caching
150
subtest 'test system preference caching' => sub {
69
use Test::DBIx::Class;
151
    my $cache = Koha::Caches->get_instance('syspref');
152
    plan skip_all => 'Cache not active' unless $cache->is_cache_active();
70
153
71
my $history;
154
    ok($config = $koha->{config}, 'Getting $koha->{config} ');
72
155
73
my $schema = Koha::Database->new()->schema();
156
    use Test::DBIx::Class;
74
$schema->storage->debug(1);
75
my $trace_read;
76
open my $trace, '>', \$trace_read or die "Can't open variable: $!";
77
$schema->storage->debugfh( $trace );
78
157
79
C4::Context->set_preference('SillyPreference', 'thing1');
158
    my $history;
80
my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
81
is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
82
159
83
my $pref = C4::Context->preference("SillyPreference");
160
    my $schema = Koha::Database->new()->schema();
84
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
161
    $schema->storage->debug(1);
85
ok( $trace_read, 'Retrieved syspref from database');
162
    my $trace_read;
86
$trace_read = q{};
163
    open my $trace, '>', \$trace_read or die "Can't open variable: $!";
164
    $schema->storage->debugfh( $trace );
87
165
88
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
166
    C4::Context->set_preference('SillyPreference', 'thing1');
89
is( $trace_read , q{}, 'Did not retrieve syspref from database');
167
    my $silly_preference = Koha::Config::SysPrefs->find('SillyPreference');
90
$trace_read = q{};
168
    is( $silly_preference->variable, 'SillyPreference', 'set_preference should have kept the case sensitivity' );
91
169
92
C4::Context->disable_syspref_cache();
170
    my $pref = C4::Context->preference("SillyPreference");
93
$silly_preference->set( { value => 'thing2' } )->store();
171
    is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
94
is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
172
    ok( $trace_read, 'Retrieved syspref from database');
95
ok($trace_read, 'Retrieved syspref from database');
173
    $trace_read = q{};
96
$trace_read = q{};
97
174
98
$silly_preference->set( { value => 'thing3' } )->store();
175
    is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
99
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
176
    is( $trace_read , q{}, 'Did not retrieve syspref from database');
100
ok($trace_read, 'Retrieved syspref from database');
177
    $trace_read = q{};
101
$trace_read = q{};
102
178
103
C4::Context->enable_syspref_cache();
179
    C4::Context->disable_syspref_cache();
104
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
180
    $silly_preference->set( { value => 'thing2' } )->store();
105
isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
181
    is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
106
$trace_read = q{};
182
    ok($trace_read, 'Retrieved syspref from database');
107
183
    $trace_read = q{};
108
# FIXME This was added by Robin and does not pass anymore
184
109
# I don't understand why we should expect thing1 while thing3 is in the cache and in the DB
185
    $silly_preference->set( { value => 'thing3' } )->store();
110
#$dbh->{mock_clear_history} = 1;
186
    is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
111
## This gives us the value that was cached on the first call, when the cache was active.
187
    ok($trace_read, 'Retrieved syspref from database');
112
#is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully from cache");
188
    $trace_read = q{};
113
#$history = $dbh->{mock_all_history};
114
#is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
115
116
$silly_preference->set( { value => 'thing4' } )->store();
117
C4::Context->clear_syspref_cache();
118
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
119
ok($trace_read, 'Retrieved syspref from database');
120
$trace_read = q{};
121
189
122
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
190
    C4::Context->enable_syspref_cache();
123
is( $trace_read, q{}, 'Did not retrieve syspref from database');
191
    is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
124
$trace_read = q{};
192
    isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
193
    $trace_read = q{};
194
195
    # FIXME This was added by Robin and does not pass anymore
196
    # I don't understand why we should expect thing1 while thing3 is in the cache and in the DB
197
    #$dbh->{mock_clear_history} = 1;
198
    ## This gives us the value that was cached on the first call, when the cache was active.
199
    #is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully from cache");
200
    #$history = $dbh->{mock_all_history};
201
    #is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
202
203
    $silly_preference->set( { value => 'thing4' } )->store();
204
    C4::Context->clear_syspref_cache();
205
    is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
206
    ok($trace_read, 'Retrieved syspref from database');
207
    $trace_read = q{};
208
209
    is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
210
    is( $trace_read, q{}, 'Did not retrieve syspref from database');
211
    $trace_read = q{};
212
213
    $silly_preference->delete();
214
};
125
215
126
my $oConnection = C4::Context->Zconn('biblioserver', 0);
216
my $oConnection = C4::Context->Zconn('biblioserver', 0);
127
isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
217
isnt($oConnection->option('async'), 1, "ZOOM connection is synchronous");
128
$oConnection = C4::Context->Zconn('biblioserver', 1);
218
$oConnection = C4::Context->Zconn('biblioserver', 1);
129
is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
219
is($oConnection->option('async'), 1, "ZOOM connection is asynchronous");
130
220
131
$silly_preference->delete();
132
133
# AutoEmailOpacUser should be a YesNo pref
221
# AutoEmailOpacUser should be a YesNo pref
134
C4::Context->set_preference('AutoEmailOpacUser', '');
222
C4::Context->set_preference('AutoEmailOpacUser', '');
135
my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailOpacUser');
223
my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailOpacUser');
136
- 

Return to bug 20930