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 / +105 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 46-54 sub store { Link Here
46
48
47
    my $action = $self->in_storage ? 'MODIFY' : 'ADD';
49
    my $action = $self->in_storage ? 'MODIFY' : 'ADD';
48
50
49
    C4::Log::logaction( 'SYSTEMPREFERENCE', $action, undef, $self->variable . ' | ' . $self->value );
51
    my $value = $self->value;
52
    if (ref($value)) {
53
        $value = $self->_dump_datatypes($value);
54
    }
50
55
51
    return $self->SUPER::store($self);
56
    C4::Log::logaction( 'SYSTEMPREFERENCE', $action, undef, $self->variable . ' | ' . $value );
57
58
    $self->SUPER::store($self);
59
    $self->set({ value => $self->_load_datatypes($value) }) unless ref($value);
60
61
    return $self;
52
}
62
}
53
63
54
=head3 delete
64
=head3 delete
Lines 67-72 sub delete { Link Here
67
    return $deleted;
77
    return $deleted;
68
}
78
}
69
79
80
=head3 value
81
82
=cut
83
84
sub value {
85
    my $self = shift;
86
87
    my $value = $self->SUPER::value(@_);
88
    return $value unless $value;
89
90
    $self->_load_datatypes($value);
91
92
    return $value;
93
}
94
95
=head3 _dump_datatypes
96
97
=cut
98
99
sub _dump_datatypes {
100
    my ($self, $value) = @_;
101
102
    $value = $self->_dump_yaml_or_json($value);
103
104
    return $value;
105
}
106
107
=head3 _load_datatypes
108
109
=cut
110
111
sub _load_datatypes {
112
    my ($self, $value) = @_;
113
114
    $value = $self->_load_yaml_or_json($value);
115
116
    return $value;
117
}
118
119
=head3 _load_yaml_or_json
120
121
Loads system preference of YAML / JSON type.
122
123
Throws Koha::Exceptions::Config::InvalidSyntax if not valid YAML / JSON.
124
125
=cut
126
127
sub _load_yaml_or_json {
128
    my ($self, $value) = @_;
129
130
    my $type = defined $self->type ? lc($self->type) : '';
131
    return $value if $type ne 'yaml' and $type ne 'json';
132
    if (!ref($value)) {
133
        eval { $value = Load($value); };
134
    } else {
135
        return $value;
136
    }
137
    if ($@) {
138
        Koha::Exceptions::Config::InvalidSyntax->throw(
139
            error => $@,
140
            value => $value,
141
        );
142
    }
143
    return $value;
144
}
145
146
=head3 _dump_yaml_or_json
147
148
Dumps system preference of YAML / JSON type.
149
150
Throws Koha::Exceptions::Config::InvalidSyntax if not valid YAML / JSON.
151
152
=cut
153
154
sub _dump_yaml_or_json {
155
    my ($self, $value) = @_;
156
157
    my $type = defined $self->type ? lc($self->type) : '';
158
    return $value if $type ne 'yaml' and $type ne 'json';
159
    if (ref($value)) {
160
        eval { $value = Dump($value); };
161
    } else {
162
        return $value;
163
    }
164
    if ($@) {
165
        Koha::Exceptions::Config::InvalidSyntax->throw(
166
            error => $@,
167
            value => $value,
168
        );
169
    }
170
    return $value;
171
}
172
70
=head3 type
173
=head3 type
71
174
72
=cut
175
=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 (-1 / +79 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
        $hashref = qq/---
77
        test:
78
          test: 1
79
        /;
80
        $arrayref = qq/---
81
        [much, elements, in, this, very, dogearray]
82
        /;
83
        $string = qq/---
84
        wat
85
        /;
86
87
        # Check HASHref
88
        C4::Context->set_preference('YAMLpref', $hashref, '', 'yaml', '');
89
        $pref = C4::Context->preference('YAMLpref');
90
        is($pref->{test}->{test}, 1, 'Got hashref');
91
        is_deeply($cache->get_from_cache("syspref_yamlpref"), $pref, 'is_deeply');
92
93
        # Check ARRAYref
94
        C4::Context->set_preference('YAMLpref', $arrayref, '', 'yaml', '');
95
        $pref = C4::Context->preference('YAMLpref');
96
        is($pref->[5], 'dogearray', 'Got arrayref');
97
        is_deeply($cache->get_from_cache("syspref_yamlpref"), $pref, 'is_deeply');
98
99
        # Check string
100
        C4::Context->set_preference('YAMLpref', $string, '', 'yaml', '');
101
        $pref = C4::Context->preference('YAMLpref');
102
        is($pref, 'wat', 'Got string');
103
        is_deeply($cache->get_from_cache("syspref_yamlpref"), $pref, 'is_deeply');
104
    };
105
    subtest 'test JSON' => sub {
106
        $hashref = qq/{"test": {"test": 1}}/;
107
        $arrayref = qq/["much", "elements", "in", "this", "very", "dogearray"]/;
108
        $string = qq/"wat"/;
109
110
        # Check HASHref
111
        C4::Context->set_preference('JSONpref', $hashref, '', 'json', '');
112
        $pref = C4::Context->preference('JSONpref');
113
        is($pref->{test}->{test}, 1, 'Got hashref');
114
        is_deeply($cache->get_from_cache("syspref_jsonpref"), $pref, 'is_deeply');
115
116
        # Check ARRAYref
117
        C4::Context->set_preference('JSONpref', $arrayref, '', 'json', '');
118
        $pref = C4::Context->preference('JSONpref');
119
        is($pref->[5], 'dogearray', 'Got arrayref');
120
        is_deeply($cache->get_from_cache("syspref_jsonpref"), $pref, 'is_deeply');
121
122
        # Check string
123
        C4::Context->set_preference('JSONpref', $string, '', 'json', '');
124
        $pref = C4::Context->preference('JSONpref');
125
        is($pref, 'wat', 'Got string');
126
        is_deeply($cache->get_from_cache("syspref_jsonpref"), $pref, 'is_deeply');
127
    };
128
};
129
51
$dbh->rollback;
130
$dbh->rollback;
52
131
53
ok($koha = C4::Context->new,  'C4::Context->new');
132
ok($koha = C4::Context->new,  'C4::Context->new');
54
- 

Return to bug 20930