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

(-)a/Koha/Configuration.pm (+90 lines)
Line 0 Link Here
1
package Koha::Configuration;
2
3
# Copyright Theke Solutions 2020
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use base qw(Koha::Object);
23
24
use Koha::Libraries;
25
use Koha::Patron::Categories;
26
use Koha::ItemTypes;
27
28
=head1 NAME
29
30
Koha::Configuration - Koha Configuration object class
31
32
=head1 API
33
34
=head2 Class methods
35
36
=cut
37
38
=head3 library
39
40
Returns a Koha::Library object if the configuration entry is tied to a specific library.
41
42
=cut
43
44
sub library {
45
    my ($self) = @_;
46
47
    my $library_rs = $self->_result->library;
48
    return unless $library_rs;
49
    return Koha::Library->_new_from_dbic( $library_rs );
50
}
51
52
=head3 category
53
54
Returns a Koha::Patron::Category if the configuration entry is tied to a specific category.
55
56
=cut
57
58
sub category {
59
    my ($self) = @_;
60
61
    my $category_rs = $self->_result->category;
62
    return unless $category_rs;
63
    return Koha::Patron::Category->_new_from_dbic( $category_rs );
64
}
65
66
=head3 item_type
67
68
Returns a Koha::ItemType if the configuration entry is tied to a specific item type.
69
70
=cut
71
72
sub item_type {
73
    my ($self) = @_;
74
75
    my $item_type_rs = $self->_result->item_type;
76
    return unless $item_type_rs;
77
    return Koha::ItemType->_new_from_dbic( $item_type_rs );
78
}
79
80
=head2 Internal methods
81
82
=head3 _type
83
84
=cut
85
86
sub _type {
87
    return 'Configuration';
88
}
89
90
1;
(-)a/Koha/Configurations.pm (+255 lines)
Line 0 Link Here
1
package Koha::Configurations;
2
3
# Copyright Theke Solutions 2020
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Koha::Configuration;
23
use Koha::Exceptions;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Configurations - Koha Configuration Object set class
30
31
=head1 API
32
33
=head2 Class methods
34
35
=head3 get_effective_config
36
37
    my $config = Koha::Configurations->get_effective_config(
38
        {
39
            name        => $name,
40
            library_id  => $library_id,
41
            category_id => $category_id,
42
            item_type   => $item_type
43
        }
44
    );
45
46
Get the effective configuration value for I<name>, given the specified parameters.
47
B<undef> means no specific parameter needs to be queried.
48
49
I<name> is mandatory and an exception will be thrownif omitted.
50
51
=cut
52
53
sub get_effective_config {
54
    my ( $self, $params ) = @_;
55
56
    my $name = $params->{name};
57
    Koha::Exceptions::MissingParameter->throw(
58
        "Required parameter 'name' missing")
59
      unless $name;
60
61
    $params->{category_id} //= undef;
62
    $params->{library_id}  //= undef;
63
    $params->{item_type}   //= undef;
64
65
    my $category_id = $params->{category_id};
66
    my $item_type   = $params->{item_type};
67
    my $library_id  = $params->{library_id};
68
69
    my $order_by = $params->{order_by}
70
      // { -desc => [ 'library_id', 'category_id', 'item_type' ] };
71
72
    my $search_params;
73
    $search_params->{name} = $name;
74
75
    $search_params->{category_id} =
76
      defined $category_id ? [ $category_id, undef ] : undef;
77
    $search_params->{item_type} =
78
      defined $item_type ? [ $item_type, undef ] : undef;
79
    $search_params->{library_id} =
80
      defined $library_id ? [ $library_id, undef ] : undef;
81
82
    my $config = $self->search(
83
        $search_params,
84
        {
85
            order_by => $order_by,
86
            rows     => 1,
87
        }
88
    )->single;
89
90
    return $config;
91
}
92
93
=head3 get_effective_configs
94
95
    my $configs = Koha::Configurations->get_effective_configs(
96
        {
97
            names => [
98
                'smtp_host',
99
                'smtp_port',
100
                'smtp_ssl'
101
            ],
102
            library_id  => $library_id,
103
            category_id => $category_id,
104
            item_type   => $item_type
105
        }
106
    );
107
108
Get the effective configuration values for the specified I<names>, given the specified
109
parameters. B<undef> means no specific parameter needs to be queried.
110
111
=cut
112
113
sub get_effective_configs {
114
    my ( $self, $params ) = @_;
115
116
    my $names       = $params->{names};
117
    my $category_id = $params->{category_id};
118
    my $item_type   = $params->{item_type};
119
    my $library_id  = $params->{library_id};
120
121
    my $configs;
122
    foreach my $name (@$names) {
123
        my $effective_conf = $self->get_effective_conf(
124
            {
125
                name        => $name,
126
                category_id => $category_id,
127
                item_type   => $item_type,
128
                library_id  => $library_id,
129
            }
130
        );
131
132
        $configs->{$name} = $effective_conf->get_value if $effective_conf;
133
    }
134
135
    return $configs;
136
}
137
138
=head3 set_config
139
140
    my $config = Koha::Configurations->set_config(
141
        {
142
            name        => $name,
143
            library_id  => $library_id,
144
            category_id => $category_id,
145
            item_type   => $item_type,
146
            type        => $config_entry_type
147
        }
148
    );
149
150
Sets the configuration value for I<name>, given the specified parameters.
151
B<undef> means no specific parameter needs to be queried.
152
153
I<name> and I<type> are mandatory and an exception will be thrownif omitted.
154
155
B<type> can currently take the following values:
156
- text
157
- boolean
158
- integer
159
160
=cut
161
162
sub set_config {
163
    my ( $self, $params ) = @_;
164
165
    for my $mandatory_parameter (qw( name value )) {
166
        Koha::Exceptions::MissingParameter->throw(
167
            "Required parameter '$mandatory_parameter' missing")
168
          unless exists $params->{$mandatory_parameter};
169
    }
170
171
    my $library_id  = $params->{library_id};
172
    my $category_id = $params->{category_id};
173
    my $item_type   = $params->{item_type};
174
    my $name        = $params->{name};
175
    my $value       = $params->{value};
176
    my $type        = $params->{type};
177
178
    my $config = $self->search(
179
        {
180
            name        => $name,
181
            library_id  => $library_id,
182
            category_id => $category_id,
183
            item_type   => $item_type,
184
        }
185
    )->next();
186
187
    if ($config) {
188
        $config->set( { value => $value } )->store;
189
    }
190
    else {
191
        $config = Koha::Configuration->new(
192
            {
193
                library_id  => $library_id,
194
                category_id => $category_id,
195
                item_type   => $item_type,
196
                name        => $name,
197
                value       => $value,
198
                type        => $type
199
            }
200
        )->store;
201
    }
202
203
    return $config;
204
}
205
206
=head3 set_configs
207
208
=cut
209
210
sub set_configs {
211
    my ( $self, $params ) = @_;
212
213
    my %set_params;
214
    $set_params{library_id} = $params->{library_id}
215
      if exists $params->{library_id};
216
    $set_params{category_id} = $params->{category_id}
217
      if exists $params->{category_id};
218
    $set_params{item_type} = $params->{item_type}
219
      if exists $params->{item_type};
220
    my $configs = $params->{configs};
221
222
    my $config_objects = [];
223
    while ( my ( $rule_name, $rule_value ) = each %$configs ) {
224
        my $config_object = Koha::Configurations->set_config(
225
            {
226
                %set_params,
227
                name  => $rule_name,
228
                value => $rule_value,
229
            }
230
        );
231
        push( @$config_objects, $config_object );
232
    }
233
234
    return $config_objects;
235
}
236
237
=head2 Internal methods
238
239
=head3 _type
240
241
=cut
242
243
sub _type {
244
    return 'Configuration';
245
}
246
247
=head3 object_class
248
249
=cut
250
251
sub object_class {
252
    return 'Koha::Configuration';
253
}
254
255
1;
(-)a/t/db_dependent/Koha/Configurations.t (-1 / +283 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2020 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 1;
23
use Test::Exception;
24
25
use Koha::Configurations;
26
use Koha::Database;
27
28
use t::lib::TestBuilder;
29
use t::lib::Mocks;
30
31
my $schema = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
subtest 'set_config + get_effective_config' => sub {
35
36
    plan tests => 17;
37
38
    $schema->storage->txn_begin;
39
40
    my $category_id  = $builder->build_object( { class => 'Koha::Patron::Categories' } )->categorycode;
41
    my $item_type    = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype;
42
    my $library_id   = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
43
    my $library_id_2 = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
44
45
    my $name          = 'smtp_server';
46
    my $default_value = 'localhost';
47
48
    my $server_1 = 'smtp.demo1.edu';
49
    my $server_2 = 'smtp.demo2.edu';
50
    my $server_3 = 'smtp.demo3.edu';
51
    my $server_4 = 'smtp.demo4.edu';
52
    my $server_5 = 'smtp.demo5.edu';
53
    my $server_6 = 'smtp.demo6.edu';
54
    my $server_7 = 'smtp.demo7.edu';
55
56
    my $config;
57
58
    Koha::Configurations->delete;
59
60
    throws_ok { Koha::Configurations->get_effective_config }
61
    'Koha::Exceptions::MissingParameter',
62
    "Exception should be raised if get_effective_config is called without name parameter";
63
64
    $config = Koha::Configurations->get_effective_config(
65
        {
66
            library_id  => $library_id,
67
            category_id => $category_id,
68
            item_type   => $item_type,
69
            name        => $name,
70
        }
71
    );
72
    is( $config, undef, 'Undef should be returned if no rule exist' );
73
74
    # Set a default value
75
    Koha::Configurations->set_config(
76
        {
77
            library_id  => undef,
78
            category_id => undef,
79
            item_type   => undef,
80
            name        => $name,
81
            value       => $default_value,
82
        }
83
    );
84
85
    $config = Koha::Configurations->get_effective_config(
86
        {
87
            library_id  => undef,
88
            category_id => undef,
89
            item_type   => undef,
90
            name        => $name,
91
        }
92
    );
93
    is( $config->value, $default_value, 'undef means default' );
94
95
    Koha::Configurations->set_config(
96
        {
97
            library_id  => undef,
98
            category_id => undef,
99
            item_type   => $item_type,
100
            name        => $name,
101
            value       => $server_1,
102
        }
103
    );
104
105
    $config = Koha::Configurations->get_effective_config(
106
        {
107
            library_id  => $library_id,
108
            category_id => $category_id,
109
            item_type   => $item_type,
110
            name        => $name,
111
        }
112
    );
113
    is( $config->value, $server_1,
114
        'More specific rule is returned when item_type is given' );
115
116
    $config = Koha::Configurations->get_effective_config(
117
        {
118
            library_id  => $library_id_2,
119
            category_id => undef,
120
            item_type   => undef,
121
            name        => $name,
122
        }
123
    );
124
    is( $config->value, $default_value,
125
        'Default rule is returned if there is no rule for this library_id' );
126
127
    Koha::Configurations->set_config(
128
        {
129
            library_id  => undef,
130
            category_id => $category_id,
131
            item_type   => undef,
132
            name        => $name,
133
            value       => $server_2,
134
        }
135
    );
136
137
    $config = Koha::Configurations->get_effective_config(
138
        {
139
140
            library_id  => $library_id,
141
            category_id => $category_id,
142
            item_type   => $item_type,
143
            name        => $name,
144
        }
145
    );
146
    is( $config->value, $server_2,
147
        'More specific rule is returned when category_id exists' );
148
149
    Koha::Configurations->set_config(
150
        {
151
            library_id  => undef,
152
            category_id => $category_id,
153
            item_type   => $item_type,
154
            name        => $name,
155
            value       => $server_3,
156
        }
157
    );
158
    $config = Koha::Configurations->get_effective_config(
159
        {
160
            library_id  => $library_id,
161
            category_id => $category_id,
162
            item_type   => $item_type,
163
            name        => $name,
164
        }
165
    );
166
    is( $config->value, $server_3,
167
        'More specific rule is returned when category_id and item_type exist' );
168
169
    Koha::Configurations->set_config(
170
        {
171
            library_id  => $library_id,
172
            category_id => undef,
173
            item_type   => undef,
174
            name        => $name,
175
            value       => $server_4,
176
        }
177
    );
178
    $config = Koha::Configurations->get_effective_config(
179
        {
180
            library_id  => $library_id,
181
            category_id => $category_id,
182
            item_type   => $item_type,
183
            name        => $name,
184
        }
185
    );
186
    is( $config->value, $server_4,
187
        'More specific rule is returned when library_id exists' );
188
189
    Koha::Configurations->set_config(
190
        {
191
            library_id  => $library_id,
192
            category_id => undef,
193
            item_type   => $item_type,
194
            name        => $name,
195
            value       => $server_5,
196
        }
197
    );
198
    $config = Koha::Configurations->get_effective_config(
199
        {
200
            library_id  => $library_id,
201
            category_id => $category_id,
202
            item_type   => $item_type,
203
            name        => $name,
204
        }
205
    );
206
    is( $config->value, $server_5,
207
        'More specific rule is returned when library_id and item_type exists' );
208
209
    Koha::Configurations->set_config(
210
        {
211
            library_id  => $library_id,
212
            category_id => $category_id,
213
            item_type   => undef,
214
            name        => $name,
215
            value       => $server_6,
216
        }
217
    );
218
    $config = Koha::Configurations->get_effective_config(
219
        {
220
            library_id  => $library_id,
221
            category_id => $category_id,
222
            item_type   => $item_type,
223
            name        => $name,
224
        }
225
    );
226
    is( $config->value, $server_6,
227
        'More specific rule is returned when library_id and category_id exist'
228
    );
229
230
    Koha::Configurations->set_config(
231
        {
232
            library_id  => $library_id,
233
            category_id => $category_id,
234
            item_type   => $item_type,
235
            name        => $name,
236
            value       => $server_7,
237
        }
238
    );
239
    $config = Koha::Configurations->get_effective_config(
240
        {
241
            library_id  => $library_id,
242
            category_id => $category_id,
243
            item_type   => $item_type,
244
            name        => $name,
245
        }
246
    );
247
    is( $config->value, $server_7,
248
        'More specific rule is returned when library_id, category_id and item_type exist'
249
    );
250
251
    my $library_configs = Koha::Configurations->search({ library_id => $library_id });
252
    is( $library_configs->count, 4, "We added 8 rules");
253
    $library_configs->delete;
254
    is( $library_configs->count, 0, "We deleted 8 rules");
255
256
    throws_ok {
257
        Koha::Configurations->set_config(
258
            {
259
                library_id  => $library_id,
260
                category_id => $category_id,
261
                item_type   => $item_type,
262
                value       => $server_7,
263
            }
264
        );
265
    } 'Koha::Exceptions::MissingParameter', 'Exceptions thrown on missing parameter';
266
267
    is( "$@", "Required parameter 'name' missing", "Expected exception message" );
268
269
    throws_ok {
270
        Koha::Configurations->set_config(
271
            {
272
                library_id  => $library_id,
273
                category_id => $category_id,
274
                item_type   => $item_type,
275
                name        => $name,
276
            }
277
        );
278
    } 'Koha::Exceptions::MissingParameter', 'Exceptions thrown on missing parameter';
279
280
    is( "$@", "Required parameter 'value' missing", "Expected exception message" );
281
282
    $schema->storage->txn_rollback;
283
};

Return to bug 26129