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

(-)a/C4/Circulation.pm (-12 / +2 lines)
Lines 57-62 use Koha::RefundLostItemFeeRule; Link Here
57
use Koha::RefundLostItemFeeRules;
57
use Koha::RefundLostItemFeeRules;
58
use Koha::Account::Lines;
58
use Koha::Account::Lines;
59
use Koha::Account::Offsets;
59
use Koha::Account::Offsets;
60
use Koha::Config::SysPrefs;
60
use Carp;
61
use Carp;
61
use List::MoreUtils qw( uniq any );
62
use List::MoreUtils qw( uniq any );
62
use Scalar::Util qw( looks_like_number );
63
use Scalar::Util qw( looks_like_number );
Lines 4108-4125 sub _item_denied_renewal { Link Here
4108
    my $item = $params->{item};
4109
    my $item = $params->{item};
4109
    return unless $item;
4110
    return unless $item;
4110
4111
4111
    my @lines = split /\n/, C4::Context->preference('ItemsDeniedRenewal')//'';
4112
    my $denyingrules = Koha::Config::SysPrefs->find('ItemsDeniedRenewal')->get_yaml_pref_hash();
4112
    my $denyingrules;
4113
    foreach my $line (@lines){
4114
        my ($field,$array) = split /:/, $line;
4115
        next if !$array;
4116
        $field =~ s/^\s*|\s*$//g;
4117
        $array =~ s/[ [\]\r]//g;
4118
        my @array = split /,/, $array;
4119
        @array = map { $_ eq '""' || $_ eq "''" ? '' : $_ } @array;
4120
        @array = map { $_ eq 'NULL' ? undef : $_ } @array;
4121
        $denyingrules->{$field} = \@array;
4122
    }
4123
    return unless $denyingrules;
4113
    return unless $denyingrules;
4124
    foreach my $field (keys %$denyingrules) {
4114
    foreach my $field (keys %$denyingrules) {
4125
        my $val = $item->{$field};
4115
        my $val = $item->{$field};
(-)a/Koha/Config/SysPref.pm (+27 lines)
Lines 37-42 Koha::Config::SysPref - Koha System Preference Object class Link Here
37
37
38
=cut
38
=cut
39
39
40
=head3 get_yaml_pref_hash
41
42
Turn a pref defined via YAML as a hash
43
44
=cut
45
46
sub get_yaml_pref_hash {
47
    my ( $self ) = @_;
48
    return if !defined( $self );
49
50
    my @lines = split /\n/, $self->value//'';
51
    my $pref_as_hash;
52
    foreach my $line (@lines){
53
        my ($field,$array) = split /:/, $line;
54
        next if !$array;
55
        $field =~ s/^\s*|\s*$//g;
56
        $array =~ s/[ [\]\r]//g;
57
        my @array = split /,/, $array;
58
        @array = map { $_ eq '""' || $_ eq "''" ? '' : $_ } @array;
59
        @array = map { $_ eq 'NULL' ? undef : $_ } @array;
60
        $pref_as_hash->{$field} = \@array;
61
    }
62
63
    return $pref_as_hash;
64
}
65
66
40
=head3 store
67
=head3 store
41
68
42
=cut
69
=cut
(-)a/Koha/Util/SystemPreferences.pm (+69 lines)
Line 0 Link Here
1
package Koha::Util::SystemPreferences;
2
3
# Copyright 2018 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 under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use parent qw( Exporter );
23
24
our @EXPORT = qw(
25
  get_yaml_pref_hash
26
);
27
28
=head1 NAME
29
30
Koha::Util::SystemPreferences - utility class with System Preference routines
31
32
=head1 METHODS
33
34
=head2 get_yaml_pref_hash
35
36
Turn a pref defined via YAML as a hash
37
38
=cut
39
40
sub get_yaml_pref_hash {
41
    my ( $pref ) = @_;
42
    return if !defined( $pref );
43
44
    my @lines = split /\n/, C4::Context->preference($pref)//'';
45
    my $pref_as_hash;
46
    foreach my $line (@lines){
47
        my ($field,$array) = split /:/, $line;
48
        next if !$array;
49
        $field =~ s/^\s*|\s*$//g;
50
        $array =~ s/[ [\]\r]//g;
51
        my @array = split /,/, $array;
52
        @array = map { $_ eq '""' || $_ eq "''" ? '' : $_ } @array;
53
        @array = map { $_ eq 'NULL' ? undef : $_ } @array;
54
        $pref_as_hash->{$field} = \@array;
55
    }
56
57
    return $pref_as_hash;
58
}
59
60
1;
61
__END__
62
63
=head1 AUTHOR
64
65
Koha Development Team <http://koha-community.org/>
66
67
Nick Clemens <nick@bywatersolutions.com>
68
69
=cut
(-)a/t/db_dependent/Circulation.t (-9 / +10 lines)
Lines 226-232 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'"); Link Here
226
C4::Context->dbh->do("DELETE FROM accountlines");
226
C4::Context->dbh->do("DELETE FROM accountlines");
227
{
227
{
228
# CanBookBeRenewed tests
228
# CanBookBeRenewed tests
229
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', '' ); #Ensure pref doesn't affect current tests
229
    C4::Context->set_preference('ItemsDeniedRenewal','');
230
    # Generate test biblio
230
    # Generate test biblio
231
    my $title = 'Silence in the library';
231
    my $title = 'Silence in the library';
232
    my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
232
    my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven');
Lines 2214-2220 subtest 'CanBookBeIssued | is_overdue' => sub { Link Here
2214
subtest 'ItemsDeniedRenewal preference' => sub {
2214
subtest 'ItemsDeniedRenewal preference' => sub {
2215
    plan tests => 18;
2215
    plan tests => 18;
2216
2216
2217
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', '' );
2217
    C4::Context->set_preference('ItemsDeniedRenewal','');
2218
2218
2219
    $dbh->do('DELETE FROM issues');
2219
    $dbh->do('DELETE FROM issues');
2220
    $dbh->do('DELETE FROM items');
2220
    $dbh->do('DELETE FROM items');
Lines 2279-2285 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
2279
2279
2280
    $idr_rules="withdrawn: [1]";
2280
    $idr_rules="withdrawn: [1]";
2281
2281
2282
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2282
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2283
    ( $idr_mayrenew, $idr_error ) =
2283
    ( $idr_mayrenew, $idr_error ) =
2284
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2284
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2285
    is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2285
    is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
Lines 2291-2297 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
2291
2291
2292
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2292
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2293
2293
2294
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2294
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2295
    ( $idr_mayrenew, $idr_error ) =
2295
    ( $idr_mayrenew, $idr_error ) =
2296
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2296
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2297
    is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2297
    is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
Lines 2303-2309 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
2303
2303
2304
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2304
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2305
2305
2306
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2306
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2307
    ( $idr_mayrenew, $idr_error ) =
2307
    ( $idr_mayrenew, $idr_error ) =
2308
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2308
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2309
    is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2309
    is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
Lines 2314-2336 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
2314
    is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2314
    is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2315
2315
2316
    $idr_rules="itemcallnumber: [NULL]";
2316
    $idr_rules="itemcallnumber: [NULL]";
2317
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2317
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2318
    ( $idr_mayrenew, $idr_error ) =
2318
    ( $idr_mayrenew, $idr_error ) =
2319
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2319
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2320
    is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2320
    is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2321
    $idr_rules="itemcallnumber: ['']";
2321
    $idr_rules="itemcallnumber: ['']";
2322
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2322
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2323
    ( $idr_mayrenew, $idr_error ) =
2323
    ( $idr_mayrenew, $idr_error ) =
2324
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2324
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2325
    is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2325
    is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2326
2326
2327
    $idr_rules="itemnotes: [NULL]";
2327
    $idr_rules="itemnotes: [NULL]";
2328
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2328
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2329
    ( $idr_mayrenew, $idr_error ) =
2329
    ( $idr_mayrenew, $idr_error ) =
2330
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2330
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2331
    is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2331
    is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2332
    $idr_rules="itemnotes: ['']";
2332
    $idr_rules="itemnotes: ['']";
2333
    t::lib::Mocks::mock_preference( 'ItemsDeniedRenewal', $idr_rules );
2333
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2334
    ( $idr_mayrenew, $idr_error ) =
2334
    ( $idr_mayrenew, $idr_error ) =
2335
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2335
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2336
    is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
2336
    is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
Lines 2493-2498 subtest 'AddReturn should clear items.onloan for unissued items' => sub { Link Here
2493
};
2493
};
2494
2494
2495
$schema->storage->txn_rollback;
2495
$schema->storage->txn_rollback;
2496
C4::Context->clear_syspref_cache();
2496
$cache->clear_from_cache('single_holidays');
2497
$cache->clear_from_cache('single_holidays');
2497
2498
2498
sub set_userenv {
2499
sub set_userenv {
(-)a/t/db_dependent/Koha/Config/SysPrefs.t (-1 / +68 lines)
Line 0 Link Here
0
- 
1
# This file is part of Koha.
2
#
3
# Copyright 2018 Koha Development Team
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Test::More tests => 4;
20
21
use t::lib::Mocks;
22
use t::lib::TestBuilder;
23
use Koha::Config::SysPrefs;
24
25
my $schema = Koha::Database->new->schema;
26
$schema->storage->txn_begin;
27
28
my $builder = t::lib::TestBuilder->new;
29
30
my $nb_of_prefs = Koha::Config::SysPrefs->search->count;
31
my $new_pref = Koha::Config::SysPref->new({
32
    variable => 'ShouldNotBeDone',
33
    value    => 'but a good test?',
34
    options  => undef,
35
    explanation => 'just for CRUD sake',
36
    type     => 'Free'
37
})->store;
38
39
is( Koha::Config::SysPrefs->search->count, $nb_of_prefs + 1, 'The 1 pref should have been added' );
40
my $retrieved_pref = Koha::Config::SysPrefs->find('ShouldNotBeDone');
41
is( $retrieved_pref->value, $new_pref->value, 'Find a pref by variable should return the correct pref' );
42
43
$retrieved_pref->delete;
44
is( Koha::Config::SysPrefs->search->count, $nb_of_prefs, 'Delete should have deleted the pref' );
45
46
subtest 'get_yaml_pref_hash' => sub {
47
48
    plan tests => 1;
49
50
    my $the_pref = Koha::Config::SysPrefs->find({variable=>'ItemsDeniedRenewal'});
51
    $the_pref->value(q{
52
        nulled: [NULL,'']
53
        this: [just_that]
54
        multi_this: [that,another]
55
    });
56
57
    my $expected_hash = {
58
        nulled => [undef,""],
59
        this     => ['just_that'],
60
        multi_this => ['that','another'],
61
    };
62
    my $got_hash = $the_pref->get_yaml_pref_hash();
63
    is_deeply($got_hash,$expected_hash,"Pref fetched and converted correctly");
64
65
};
66
67
$schema->storage->txn_rollback;
68

Return to bug 15494