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

(-)a/Koha/Exceptions/Patron/Relationship.pm (+73 lines)
Line 0 Link Here
1
package Koha::Exceptions::Patron::Relationship;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Exception::Class (
21
22
    'Koha::Exceptions::Patron::Relationship' => {
23
        description => 'Something went wrong!',
24
    },
25
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => {
26
        isa         => 'Koha::Exceptions::Patron::Relationship',
27
        description => 'The specified relationship is invalid',
28
        fields      =>  ['relationship','no_relationship']
29
    }
30
);
31
32
sub full_message {
33
    my $self = shift;
34
35
    my $msg = $self->message;
36
37
    unless ( $msg) {
38
        if ( $self->isa('Koha::Exceptions::Patron::Relationship') ) {
39
            if ( $self->no_relationship ) {
40
                $msg = sprintf( "No relationship passed." );
41
            }
42
            else {
43
                $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship );
44
            }
45
        }
46
    }
47
48
    return $msg;
49
}
50
51
=head1 NAME
52
53
Koha::Exceptions::Patron::Relationship - Base class for patron relatioship exceptions
54
55
=head1 Exceptions
56
57
=head2 Koha::Exceptions::Patron::Relationship
58
59
Generic Patron exception
60
61
=head2 Koha::Exceptions::Patron::Relationship::InvalidRelationship
62
63
Exception to be used when an invalid relationship is passed.
64
65
=head1 Class methods
66
67
=head2 full_message
68
69
Overloaded method for exception stringifying.
70
71
=cut
72
73
1;
(-)a/Koha/Patron.pm (+11 lines)
Lines 34-39 use Koha::Club::Enrollments; Link Here
34
use Koha::Database;
34
use Koha::Database;
35
use Koha::DateUtils;
35
use Koha::DateUtils;
36
use Koha::Exceptions::Password;
36
use Koha::Exceptions::Password;
37
use Koha::Exceptions::Patron::Relationship;
37
use Koha::Holds;
38
use Koha::Holds;
38
use Koha::Old::Checkouts;
39
use Koha::Old::Checkouts;
39
use Koha::Patron::Attributes;
40
use Koha::Patron::Attributes;
Lines 1462-1467 sub add_guarantor { Link Here
1462
    my $guarantor_id = $params->{guarantor_id};
1463
    my $guarantor_id = $params->{guarantor_id};
1463
    my $relationship = $params->{relationship};
1464
    my $relationship = $params->{relationship};
1464
1465
1466
    my @valid_relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
1467
1468
    Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
1469
        no_relationship => 1 )
1470
        unless defined $relationship;
1471
1472
    Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
1473
        relationship => $relationship )
1474
        unless any { $_ eq $relationship } @valid_relationships;
1475
1465
    return Koha::Patron::Relationship->new(
1476
    return Koha::Patron::Relationship->new(
1466
        {
1477
        {
1467
            guarantee_id => $self->id,
1478
            guarantee_id => $self->id,
(-)a/t/Koha/Exceptions.t (-1 / +30 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 4;
20
use Test::More tests => 5;
21
use Test::MockObject;
21
use Test::MockObject;
22
use Test::Exception;
22
use Test::Exception;
23
23
Lines 128-130 subtest 'Koha::Exceptions::Metadata tests' => sub { Link Here
128
        'Exception is thrown :-D';
128
        'Exception is thrown :-D';
129
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
129
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
130
};
130
};
131
132
subtest 'Koha::Exceptions::Patron::Relationship tests' => sub {
133
134
    plan tests => 7;
135
136
    use_ok('Koha::Exceptions::Patron::Relationship');
137
138
    throws_ok
139
        { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( no_relationship => 1 ); }
140
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
141
        'Exception is thrown :-D';
142
143
    # stringify the exception
144
    is( "$@", 'No relationship passed.', 'Exception stringified correctly' );
145
146
    throws_ok
147
        { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( relationship => 'some' ); }
148
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
149
        'Exception is thrown :-D';
150
151
    # stringify the exception
152
    is( "$@", "Invalid relationship passed, 'some' is not defined.", 'Exception stringified correctly' );
153
154
    throws_ok
155
        { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( "Manual message exception" ) }
156
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
157
        'Exception is thrown :-D';
158
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
159
};
(-)a/t/db_dependent/Koha/Patron.t (-1 / +67 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2019 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::Database;
26
use Koha::Patrons;
27
use Koha::Patron::Relationships;
28
29
use t::lib::TestBuilder;
30
use t::lib::Mocks;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
subtest 'add_guarantor() tests' => sub {
36
37
    plan tests => 5;
38
39
    $schema->storage->txn_begin;
40
41
    t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
42
43
    my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
44
    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
45
46
    throws_ok
47
        { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); }
48
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
49
        'Exception is thrown as no relationship passed';
50
51
    is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
52
53
    throws_ok
54
        { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father' }); }
55
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
56
        'Exception is thrown as a wrong relationship was passed';
57
58
    is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' );
59
60
    $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' });
61
62
    my $guarantors = $patron_1->guarantor_relationships;
63
64
    is( $guarantors->count, 1, 'No guarantors added' );
65
66
    $schema->storage->txn_rollback;
67
};

Return to bug 14570