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

(-)a/Koha/Exceptions.pm (+4 lines)
Lines 32-37 use Exception::Class ( Link Here
32
        isa => 'Koha::Exception',
32
        isa => 'Koha::Exception',
33
        description => 'The default value cannot be deleted'
33
        description => 'The default value cannot be deleted'
34
    },
34
    },
35
    'Koha::Exceptions::CannotDeleteSystem' => {
36
        isa => 'Koha::Exception',
37
        description => 'The system value cannot be deleted'
38
    },
35
    'Koha::Exceptions::MissingParameter' => {
39
    'Koha::Exceptions::MissingParameter' => {
36
        isa => 'Koha::Exception',
40
        isa => 'Koha::Exception',
37
        description => 'A required parameter is missing'
41
        description => 'A required parameter is missing'
(-)a/Koha/RestrictionType.pm (-23 / +26 lines)
Lines 1-4 Link Here
1
package Koha::RestrictionType;
1
package Koha::Patron::Restriction::Type;
2
2
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
Lines 19-30 use Modern::Perl; Link Here
19
19
20
use base qw(Koha::Object);
20
use base qw(Koha::Object);
21
21
22
use Koha::RestrictionTypes;
22
use Koha::Exceptions;
23
24
use Koha::Patron::Restriction::Types;
23
use C4::Context;
25
use C4::Context;
24
26
25
=head1 NAME
27
=head1 NAME
26
28
27
Koha::RestrictionType - Koha RestrictionType Object class
29
Koha::Patron::Restriction::Type - Koha RestrictionType Object class
28
30
29
=head1 API
31
=head1 API
30
32
Lines 39-66 Overloaded delete method that does extra clean up: Link Here
39
=cut
41
=cut
40
42
41
sub delete {
43
sub delete {
42
    my ( $self ) = @_;
44
    my ($self) = @_;
43
45
44
    # Find out what the default is
46
    # Ensure we can't delete the current default
45
    my $default = Koha::RestrictionTypes->find({ is_default => 1 })->code;
47
    Koha::Exceptions::CannotDeleteDefault->throw if $self->is_default;
46
    # Ensure we're not trying to delete a is_system type (this includes
48
47
    # the default type)
49
    # Ensure we can't delete system values
48
    return 0 if $self->is_system == 1;
50
    Koha::Exceptions::CannotDeleteSystem->throw if $self->is_system;
49
    # We can't use Koha objects here because Koha::Patron::Debarments
50
    # is not a Koha object. So we'll do it old skool
51
    my $rows = C4::Context->dbh->do(
52
        "UPDATE borrower_debarments SET type = ? WHERE type = ?",
53
        undef,
54
        ($default, $self->code)
55
    );
56
51
57
    # Now do the delete if the update was successful
52
    return $self->_result->result_source->schema->txn_do(
58
    if ($rows) {
53
        sub {
59
        my $deleted = $self->SUPER::delete($self);
54
            # Update all linked restrictions to the default
60
        return $deleted
55
            my $default =
61
    }
56
              Koha::Patron::Restriction::Types->find( { is_default => 1 } )->code;
57
58
            # We can't use Koha objects here because Koha::Patron::Debarments
59
            # is not a Koha object. So we'll do it old skool
60
            my $rows = C4::Context->dbh->do(
61
                "UPDATE borrower_debarments SET type = ? WHERE type = ?",
62
                undef, ( $default, $self->code ) );
62
63
63
    return 0;
64
            return $self->SUPER::delete;
65
        }
66
    );
64
}
67
}
65
68
66
=head3 make_default
69
=head3 make_default
Lines 75-81 sub make_default { Link Here
75
    $self->_result->result_source->schema->txn_do(
78
    $self->_result->result_source->schema->txn_do(
76
        sub {
79
        sub {
77
            my $types =
80
            my $types =
78
              Koha::RestrictionTypes->search( { code => { '!=' => $self->code } } );
81
              Koha::Patron::Restriction::Types->search( { code => { '!=' => $self->code } } );
79
            $types->update( { is_default => 0 } );
82
            $types->update( { is_default => 0 } );
80
            $self->set( { is_default => 1 } );
83
            $self->set( { is_default => 1 } );
81
            $self->SUPER::store;
84
            $self->SUPER::store;
(-)a/Koha/RestrictionTypes.pm (-4 / +4 lines)
Lines 1-4 Link Here
1
package Koha::RestrictionTypes;
1
package Koha::Patron::Restriction::Types;
2
2
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
Lines 18-30 package Koha::RestrictionTypes; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Database;
20
use Koha::Database;
21
use Koha::RestrictionType;
21
use Koha::Patron::Restriction::Type;
22
22
23
use base qw(Koha::Objects);
23
use base qw(Koha::Objects);
24
24
25
=head1 NAME
25
=head1 NAME
26
26
27
Koha::RestrictionTypes - Koha Restriction Types Object set class
27
Koha::Patron::Restriction::Types - Koha Restriction Types Object set class
28
28
29
=head1 API
29
=head1 API
30
30
Lines 63-69 sub _type { Link Here
63
=cut
63
=cut
64
64
65
sub object_class {
65
sub object_class {
66
    return 'Koha::RestrictionType';
66
    return 'Koha::Patron::Restriction::Type';
67
}
67
}
68
68
69
1;
69
1;
(-)a/Koha/Schema/Result/RestrictionType.pm (-2 / +2 lines)
Lines 98-107 __PACKAGE__->add_columns( Link Here
98
);
98
);
99
99
100
sub koha_object_class {
100
sub koha_object_class {
101
    'Koha::RestrictionType';
101
    'Koha::Patron::Restriction::Type';
102
}
102
}
103
sub koha_objects_class {
103
sub koha_objects_class {
104
    'Koha::RestrictionTypes';
104
    'Koha::Patron::Restriction::Types';
105
}
105
}
106
106
107
1;
107
1;
(-)a/admin/restrictions.pl (-11 / +27 lines)
Lines 20-35 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Try::Tiny qw( try catch );
24
23
use C4::Auth qw( get_template_and_user );
25
use C4::Auth qw( get_template_and_user );
24
use C4::Output qw( output_html_with_http_headers );
26
use C4::Output qw( output_html_with_http_headers );
25
use Koha::RestrictionTypes;
27
use Koha::Patron::Restriction::Types;
26
28
27
my $input = CGI->new;
29
my $input = CGI->new;
28
my $op    = $input->param('op') // 'list';
30
my $op    = $input->param('op') // 'list';
29
my $code  = uc $input->param('code');
31
my $code  = uc $input->param('code');
30
my @messages = ();
32
my @messages = ();
31
33
32
33
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34
    {
35
    {
35
        template_name   => "admin/restrictions.tt",
36
        template_name   => "admin/restrictions.tt",
Lines 43-53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
43
if ( $op eq 'add_form') {
44
if ( $op eq 'add_form') {
44
    # Get all existing restrictions, so we can do client-side validation
45
    # Get all existing restrictions, so we can do client-side validation
45
    $template->param(
46
    $template->param(
46
        existing => scalar Koha::RestrictionTypes->search()
47
        existing => scalar Koha::Patron::Restriction::Types->search()
47
    );
48
    );
48
    if ($code) {
49
    if ($code) {
49
        $template->param(
50
        $template->param(
50
            restriction => scalar Koha::RestrictionTypes->find($code)
51
            restriction => scalar Koha::Patron::Restriction::Types->find($code)
51
        );
52
        );
52
    }
53
    }
53
} elsif ( $op eq 'add_validate' ) {
54
} elsif ( $op eq 'add_validate' ) {
Lines 57-63 if ( $op eq 'add_form') { Link Here
57
58
58
    if ($is_a_modif) {
59
    if ($is_a_modif) {
59
        # Check whether another restriction already has this display text
60
        # Check whether another restriction already has this display text
60
        my $dupe = Koha::RestrictionTypes->find({
61
        my $dupe = Koha::Patron::Restriction::Types->find({
61
            display_text => $display_text
62
            display_text => $display_text
62
        });
63
        });
63
        if ($dupe) {
64
        if ($dupe) {
Lines 65-77 if ( $op eq 'add_form') { Link Here
65
                type => 'error', code => 'duplicate_display_text'
66
                type => 'error', code => 'duplicate_display_text'
66
            };
67
            };
67
        } else {
68
        } else {
68
            my $restriction = Koha::RestrictionTypes->find($code);
69
            my $restriction = Koha::Patron::Restriction::Types->find($code);
69
            $restriction->display_text($display_text);
70
            $restriction->display_text($display_text);
70
            $restriction->store;
71
            $restriction->store;
72
            push @messages, { type => 'message', code => 'update_success' };
71
        }
73
        }
72
    } else {
74
    } else {
73
        # Check whether another restriction already has this code
75
        # Check whether another restriction already has this code
74
        my $dupe = Koha::RestrictionTypes->find($code);
76
        my $dupe = Koha::Patron::Restriction::Types->find($code);
75
        if ($dupe) {
77
        if ($dupe) {
76
            push @messages, {
78
            push @messages, {
77
                type => 'error', code => 'duplicate_code'
79
                type => 'error', code => 'duplicate_code'
Lines 82-100 if ( $op eq 'add_form') { Link Here
82
                display_text => $display_text
84
                display_text => $display_text
83
            });
85
            });
84
            $restriction->store;
86
            $restriction->store;
87
            push @messages, { type => 'message', code => 'add_success' };
85
        }
88
        }
86
    }
89
    }
87
    $op = 'list';
90
    $op = 'list';
88
} elsif ( $op eq 'make_default' ) {
91
} elsif ( $op eq 'make_default' ) {
89
    my $restriction = Koha::RestrictionTypes->find($code);
92
    my $restriction = Koha::Patron::Restriction::Types->find($code);
90
    $restriction->make_default;
93
    $restriction->make_default;
91
    $op = 'list';
94
    $op = 'list';
92
} elsif ( $op eq 'delete_confirm' ) {
95
} elsif ( $op eq 'delete_confirm' ) {
93
    $template->param(
96
    $template->param(
94
        restriction => scalar Koha::RestrictionTypes->find($code)
97
        restriction => scalar Koha::Patron::Restriction::Types->find($code)
95
    );
98
    );
96
} elsif ( $op eq 'delete_confirmed' ) {
99
} elsif ( $op eq 'delete_confirmed' ) {
97
    Koha::RestrictionTypes->find($code)->delete;
100
    try {
101
        Koha::Patron::Restriction::Types->find($code)->delete;
102
        push @messages, { type => 'message', code => 'delete_success' };
103
    }
104
    catch {
105
        if ( blessed $_ ) {
106
            if ( $_->isa('Koha::Exceptions::CannotDeleteDefault') ) {
107
                push @messages, { type => 'error', code => 'delete_default' };
108
            }
109
            elsif ( $_->isa('Koha::Exceptions::CannotDeleteSystem') ) {
110
                push @messages, { type => 'error', code => 'delete_system' };
111
            }
112
        }
113
    }
98
    $op = 'list';
114
    $op = 'list';
99
}
115
}
100
116
Lines 104-110 $template->param( Link Here
104
);
120
);
105
121
106
if ( $op eq 'list' ) {
122
if ( $op eq 'list' ) {
107
    my $restrictions = Koha::RestrictionTypes->search();
123
    my $restrictions = Koha::Patron::Restriction::Types->search();
108
    $template->param(
124
    $template->param(
109
        restrictions => $restrictions,
125
        restrictions => $restrictions,
110
    )
126
    )
(-)a/circ/circulation.pl (-2 / +2 lines)
Lines 46-52 use Koha::CsvProfiles; Link Here
46
use Koha::Patrons;
46
use Koha::Patrons;
47
use Koha::Patron::Debarments qw( GetDebarments );
47
use Koha::Patron::Debarments qw( GetDebarments );
48
use Koha::DateUtils qw( dt_from_string );
48
use Koha::DateUtils qw( dt_from_string );
49
use Koha::RestrictionTypes;
49
use Koha::Patron::Restriction::Types;
50
use Koha::Plugins;
50
use Koha::Plugins;
51
use Koha::Database;
51
use Koha::Database;
52
use Koha::BiblioFrameworks;
52
use Koha::BiblioFrameworks;
Lines 628-634 $template->param( Link Here
628
    PatronAutoComplete      => C4::Context->preference("PatronAutoComplete"),
628
    PatronAutoComplete      => C4::Context->preference("PatronAutoComplete"),
629
    debarments                => scalar GetDebarments({ borrowernumber => $borrowernumber }),
629
    debarments                => scalar GetDebarments({ borrowernumber => $borrowernumber }),
630
    todaysdate                => dt_from_string()->set(hour => 23)->set(minute => 59),
630
    todaysdate                => dt_from_string()->set(hour => 23)->set(minute => 59),
631
    restriction_types         => scalar Koha::RestrictionTypes->keyed_on_code(),
631
    restriction_types         => scalar Koha::Patron::Restriction::Types->keyed_on_code(),
632
    has_modifications         => $has_modifications,
632
    has_modifications         => $has_modifications,
633
    override_high_holds       => $override_high_holds,
633
    override_high_holds       => $override_high_holds,
634
    nopermission              => scalar $query->param('nopermission'),
634
    nopermission              => scalar $query->param('nopermission'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt (+10 lines)
Lines 71-80 Link Here
71
[% FOR m IN messages %]
71
[% FOR m IN messages %]
72
    <div class="dialog [% m.type | html %]">
72
    <div class="dialog [% m.type | html %]">
73
        [% SWITCH m.code %]
73
        [% SWITCH m.code %]
74
        [% CASE 'add_success' %]
75
            Type added
76
        [% CASE 'update_success' %]
77
            Type updated
74
        [% CASE 'duplicate_display_text' %]
78
        [% CASE 'duplicate_display_text' %]
75
            Another restriction already has this label
79
            Another restriction already has this label
76
        [% CASE 'duplicate_code' %]
80
        [% CASE 'duplicate_code' %]
77
            Another restriction already has this code
81
            Another restriction already has this code
82
        [% CASE 'delete_success' %]
83
            Type deleted
84
        [% CASE 'delete_default' %]
85
            Cannot delete the default type
86
        [% CASE 'delete_system' %]
87
            Cannot delete a system type
78
        [% CASE %]
88
        [% CASE %]
79
            [% m.code | html %]
89
            [% m.code | html %]
80
        [% END %]
90
        [% END %]
(-)a/members/memberentry.pl (-2 / +2 lines)
Lines 36-42 use Koha::AuthUtils; Link Here
36
use Koha::AuthorisedValues;
36
use Koha::AuthorisedValues;
37
use Koha::Email;
37
use Koha::Email;
38
use Koha::Patron::Debarments qw( AddDebarment DelDebarment GetDebarments );
38
use Koha::Patron::Debarments qw( AddDebarment DelDebarment GetDebarments );
39
use Koha::RestrictionTypes;
39
use Koha::Patron::Restriction::Types;
40
use Koha::Cities;
40
use Koha::Cities;
41
use Koha::DateUtils qw( dt_from_string );
41
use Koha::DateUtils qw( dt_from_string );
42
use Koha::Libraries;
42
use Koha::Libraries;
Lines 119-125 foreach my $id ( @delete_guarantor ) { Link Here
119
## Deal with debarments
119
## Deal with debarments
120
$template->param(
120
$template->param(
121
    debarments => scalar GetDebarments( { borrowernumber => $borrowernumber } ),
121
    debarments => scalar GetDebarments( { borrowernumber => $borrowernumber } ),
122
    restriction_types => scalar Koha::RestrictionTypes->keyed_on_code()
122
    restriction_types => scalar Koha::Patron::Restriction::Types->keyed_on_code()
123
);
123
);
124
my @debarments_to_remove = $input->multi_param('remove_debarment');
124
my @debarments_to_remove = $input->multi_param('remove_debarment');
125
foreach my $d ( @debarments_to_remove ) {
125
foreach my $d ( @debarments_to_remove ) {
(-)a/members/moremember.pl (-2 / +2 lines)
Lines 37-43 use List::MoreUtils qw( uniq ); Link Here
37
use Scalar::Util qw( looks_like_number );
37
use Scalar::Util qw( looks_like_number );
38
use Koha::Patron::Attribute::Types;
38
use Koha::Patron::Attribute::Types;
39
use Koha::Patron::Debarments qw( GetDebarments );
39
use Koha::Patron::Debarments qw( GetDebarments );
40
use Koha::RestrictionTypes;
40
use Koha::Patron::Restriction::Types;
41
use Koha::Patron::Messages;
41
use Koha::Patron::Messages;
42
use Koha::CsvProfiles;
42
use Koha::CsvProfiles;
43
use Koha::Holds;
43
use Koha::Holds;
Lines 81-87 for (qw(gonenoaddress lost borrowernotes is_debarred)) { Link Here
81
}
81
}
82
82
83
$template->param(
83
$template->param(
84
    restriction_types => scalar Koha::RestrictionTypes->keyed_on_code()
84
    restriction_types => scalar Koha::Patron::Restriction::Types->keyed_on_code()
85
);
85
);
86
86
87
if ( $patron->is_debarred ) {
87
if ( $patron->is_debarred ) {
(-)a/t/db_dependent/Koha/Patron/Restriction/Type.t (+124 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2022 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 => 3;
23
use Test::Exception;
24
25
use Koha::Database;
26
27
use t::lib::TestBuilder;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
use_ok('Koha::Patron::Restriction::Type');
33
34
subtest 'delete() tests' => sub {
35
36
    plan tests => 4;
37
38
    $schema->storage->txn_begin;
39
40
    # Default restriction type
41
    my $default_restriction_type = Koha::Patron::Restriction::Types->find({ is_default => 1 });
42
    throws_ok { $default_restriction_type->delete }
43
    'Koha::Exceptions::CannotDeleteDefault',
44
      'Delete exception thrown on current default';
45
46
    # System restriction type
47
    my $system_restriction_type = $builder->build_object(
48
        {
49
            class => 'Koha::Patron::Restriction::Types',
50
            value => {
51
                is_system  => 1,
52
                is_default => 0,
53
            }
54
        }
55
    )->store;
56
    throws_ok { $system_restriction_type->delete }
57
    'Koha::Exceptions::CannotDeleteSystem',
58
      'Delete exception thrown on system type';
59
60
    # Used restriction type
61
    my $used_restriction_type = $builder->build_object(
62
        {
63
            class => 'Koha::Patron::Restriction::Types',
64
            value => {
65
                is_system  => 0,
66
                is_default => 0,
67
            }
68
        }
69
    )->store;
70
    my $patron = $builder->build_object(
71
        {
72
            class => 'Koha::Patrons',
73
        }
74
    )->store;
75
    Koha::Patron::Debarments::AddDebarment(
76
        {
77
            borrowernumber => $patron->borrowernumber,
78
            expiration     => '9999-06-10',
79
            type           => $used_restriction_type->code,
80
            comment        => 'Test 1',
81
        }
82
    );
83
    ok( $used_restriction_type->delete, 'Used restriction type deleted' );
84
    my $debarments = Koha::Patron::Debarments::GetDebarments(
85
        {
86
            borrowernumber => $patron->borrowernumber
87
        }
88
    );
89
    is(
90
        $debarments->[0]->{type},
91
        $default_restriction_type->code,
92
        'Used restriction updated to default'
93
    );
94
95
    $schema->storage->txn_rollback;
96
};
97
98
subtest 'make_default() tests' => sub {
99
    
100
    plan tests => 2;
101
102
    $schema->storage->txn_begin;
103
104
    # Get current default restriction type
105
    my $current_default = Koha::Patron::Restriction::Types->find({ is_default => 1 });
106
107
    # New non-default restriction type
108
    my $new_default = $builder->build_object(
109
        {
110
            class => 'Koha::Patron::Restriction::Types',
111
            value => { is_system => 0, is_default => 0 }
112
        }
113
    );
114
115
    $new_default->make_default;
116
    
117
    $current_default->discard_changes;
118
    is($current_default->is_default, 0, 'is_default set to false on prior default');
119
    is($new_default->is_default, 1, 'is_default set true on new default');
120
121
    $schema->storage->txn_rollback;
122
};
123
124
1;
(-)a/t/db_dependent/RestrictionType.t (-7 / +6 lines)
Lines 6-27 use C4::Context; Link Here
6
use Koha::Database;
6
use Koha::Database;
7
use t::lib::TestBuilder;
7
use t::lib::TestBuilder;
8
8
9
use Test::More tests => 3;
9
use Test::More tests => 2;
10
10
11
my $schema = Koha::Database->new->schema;
11
my $schema = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
12
$schema->storage->txn_begin;
13
my $dbh     = C4::Context->dbh;
13
my $dbh     = C4::Context->dbh;
14
my $builder = t::lib::TestBuilder->new;
14
my $builder = t::lib::TestBuilder->new;
15
15
16
use_ok('Koha::RestrictionType');
16
use_ok('Koha::Patron::Restriction::Types');
17
use_ok('Koha::RestrictionTypes');
18
17
19
$dbh->do(q|DELETE FROM borrower_debarments|);
18
$dbh->do(q|DELETE FROM borrower_debarments|);
20
$dbh->do(q|DELETE FROM debarment_types|);
19
$dbh->do(q|DELETE FROM restriction_types|);
21
20
22
$builder->build(
21
$builder->build(
23
    {
22
    {
24
        source => 'DebarmentType',
23
        source => 'RestrictionType',
25
        value  => {
24
        value  => {
26
            code         => 'ONE',
25
            code         => 'ONE',
27
            display_text => 'One',
26
            display_text => 'One',
Lines 32-38 $builder->build( Link Here
32
);
31
);
33
$builder->build(
32
$builder->build(
34
    {
33
    {
35
        source => 'DebarmentType',
34
        source => 'RestrictionType',
36
        value  => {
35
        value  => {
37
            code         => 'TWO',
36
            code         => 'TWO',
38
            display_text => 'Two',
37
            display_text => 'Two',
Lines 43-49 $builder->build( Link Here
43
);
42
);
44
43
45
# keyed_on_code
44
# keyed_on_code
46
my $keyed     = Koha::RestrictionTypes->keyed_on_code;
45
my $keyed     = Koha::Patron::Restriction::Types->keyed_on_code;
47
my $expecting = {
46
my $expecting = {
48
    ONE => {
47
    ONE => {
49
        code         => 'ONE',
48
        code         => 'ONE',
(-)a/t/db_dependent/RestrictionTypes.t (-121 lines)
Lines 1-120 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use C4::Context;
6
use Koha::Database;
7
use t::lib::TestBuilder;
8
9
use Test::More tests => 6;
10
11
my $schema = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
13
my $dbh     = C4::Context->dbh;
14
my $builder = t::lib::TestBuilder->new;
15
16
use_ok('Koha::RestrictionType');
17
use_ok('Koha::RestrictionTypes');
18
19
$dbh->do(q|DELETE FROM borrower_debarments|);
20
$dbh->do(q|DELETE FROM debarment_types|);
21
22
$builder->build(
23
    {
24
        source => 'DebarmentType',
25
        value  => {
26
            code         => 'ONE',
27
            display_text => 'One',
28
            is_system    => 1,
29
            is_default   => 0,
30
        }
31
    }
32
);
33
$builder->build(
34
    {
35
        source => 'DebarmentType',
36
        value  => {
37
            code         => 'TWO',
38
            display_text => 'Two',
39
            is_system    => 1,
40
            is_default   => 1,
41
        }
42
    }
43
);
44
$builder->build(
45
    {
46
        source => 'DebarmentType',
47
        value  => {
48
            code         => 'THREE',
49
            display_text => 'Three',
50
            is_system    => 1,
51
            is_default   => 0,
52
        }
53
    }
54
);
55
$builder->build(
56
    {
57
        source => 'DebarmentType',
58
        value  => {
59
            code         => 'FOUR',
60
            display_text => 'Four',
61
            is_system    => 0,
62
            is_default   => 0,
63
        }
64
    }
65
);
66
$builder->build(
67
    {
68
        source => 'DebarmentType',
69
        value  => {
70
            code         => 'FIVE',
71
            display_text => 'Five',
72
            is_system    => 0,
73
            is_default   => 0,
74
        }
75
    }
76
);
77
78
# Can we create RestrictionTypes
79
my $created = Koha::RestrictionTypes->find( { code => 'ONE' } );
80
ok( $created->display_text eq 'One', 'Restrictions created' );
81
82
# Can we delete RestrictionTypes, when appropriate
83
my $deleted = Koha::RestrictionTypes->find( { code => 'FOUR' } )->delete;
84
ok( $deleted, 'Restriction deleted' );
85
my $not_deleted = Koha::RestrictionTypes->find( { code => 'TWO' } )->delete;
86
ok( !$not_deleted, 'Read only restriction not deleted' );
87
88
# Add a patron with a debarment
89
my $library = $builder->build( { source => 'Branch' } );
90
91
my $patron_category = $builder->build( { source => 'Category' } );
92
my $borrowernumber  = Koha::Patron->new(
93
    {
94
        firstname    => 'my firstname',
95
        surname      => 'my surname',
96
        categorycode => $patron_category->{categorycode},
97
        branchcode   => $library->{branchcode},
98
    }
99
)->store->borrowernumber;
100
101
Koha::Patron::Debarments::AddDebarment(
102
    {
103
        borrowernumber => $borrowernumber,
104
        expiration     => '9999-06-10',
105
        type           => 'FIVE',
106
        comment        => 'Test 1',
107
    }
108
);
109
110
# Now delete a code and check debarments using that code switch to
111
# using the default
112
my $to_delete  = Koha::RestrictionTypes->find( { code => 'FIVE' } )->delete;
113
my $debarments = Koha::Patron::Debarments::GetDebarments(
114
    {
115
        borrowernumber => $borrowernumber
116
    }
117
);
118
is( $debarments->[0]->{type}, 'TWO', 'Debarments update with restrictions' );
119
120
$schema->storage->txn_rollback;
121
- 

Return to bug 23681