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

(-)a/Koha/Exceptions/Patron/Relationship.pm (+89 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::DuplicateRelationship' => {
26
        isa         => 'Koha::Exceptions::Patron::Relationship',
27
        description => 'There can only be one relationship between patrons in a direction',
28
        fields      => [ 'guarantor_id', 'guarantee_id' ]
29
    },
30
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => {
31
        isa         => 'Koha::Exceptions::Patron::Relationship',
32
        description => 'The specified relationship is invalid',
33
        fields      =>  ['relationship','no_relationship']
34
    }
35
);
36
37
sub full_message {
38
    my $self = shift;
39
40
    my $msg = $self->message;
41
42
    unless ( $msg) {
43
        if ( $self->isa('Koha::Exceptions::Patron::Relationship::InvalidRelationship') ) {
44
            if ( $self->no_relationship ) {
45
                $msg = sprintf( "No relationship passed." );
46
            }
47
            else {
48
                $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship );
49
            }
50
        }
51
        elsif ( $self->isa('Koha::Exceptions::Patron::Relationship::DuplicateRelationship') ) {
52
            $msg
53
                = sprintf(
54
                "There already exists a relationship for the same guarantor (%s) and guarantee (%s) combination",
55
                $self->guarantor_id, $self->guarantee_id );
56
        }
57
    }
58
59
    return $msg;
60
}
61
62
=head1 NAME
63
64
Koha::Exceptions::Patron::Relationship - Base class for patron relatioship exceptions
65
66
=head1 Exceptions
67
68
=head2 Koha::Exceptions::Patron::Relationship
69
70
Generic Patron exception
71
72
=head2 Koha::Exceptions::Patron::Relationship::DuplicateRelationship
73
74
Exception to be used when more than one relationship is requested for a
75
pair of patrons in the same direction.
76
77
=head2 Koha::Exceptions::Patron::Relationship::InvalidRelationship
78
79
Exception to be used when an invalid relationship is passed.
80
81
=head1 Class methods
82
83
=head2 full_message
84
85
Overloaded method for exception stringifying.
86
87
=cut
88
89
1;
(-)a/Koha/Patron/Relationship.pm (-1 / +36 lines)
Lines 18-25 package Koha::Patron::Relationship; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Carp;
20
use Carp;
21
use List::MoreUtils qw( any );
22
use Try::Tiny;
21
23
22
use Koha::Database;
24
use Koha::Database;
25
use Koha::Exceptions::Patron::Relationship;
23
26
24
use base qw(Koha::Object);
27
use base qw(Koha::Object);
25
28
Lines 32-41 and provides a way to access those relationships. Link Here
32
35
33
=head1 API
36
=head1 API
34
37
35
=head2 Class Methods
38
=head2 Class methods
36
39
37
=cut
40
=cut
38
41
42
=head3 store
43
44
Overloaded method that makes some checks before storing on the DB
45
46
=cut
47
48
sub store {
49
    my ( $self ) = @_;
50
51
    my @valid_relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
52
53
    Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
54
        no_relationship => 1 )
55
        unless defined $self->relationship;
56
57
    Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
58
        relationship => $self->relationship )
59
        unless any { $_ eq $self->relationship } @valid_relationships;
60
61
    return try {
62
        $self->SUPER::store;
63
    }
64
    catch {
65
        if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
66
            Koha::Exceptions::Patron::Relationship::DuplicateRelationship->throw(
67
                guarantee_id => $self->guarantee_id,
68
                guarantor_id => $self->guarantor_id
69
            );
70
        }
71
    };
72
}
73
39
=head3 guarantor
74
=head3 guarantor
40
75
41
Returns the Koha::Patron object for the guarantor, if there is one
76
Returns the Koha::Patron object for the guarantor, if there is one
(-)a/t/Koha/Exceptions.t (-1 / +47 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 => 9;
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
    my $guarantor_id = 1;
155
    my $guarantee_id = 2;
156
157
    throws_ok {
158
        Koha::Exceptions::Patron::Relationship::DuplicateRelationship->throw(
159
            guarantor_id => $guarantor_id,
160
            guarantee_id => $guarantee_id
161
        );
162
    }
163
    'Koha::Exceptions::Patron::Relationship::DuplicateRelationship', 'Exception is thrown :-D';
164
165
    # stringify the exception
166
    is( "$@",
167
        "There already exists a relationship for the same guarantor ($guarantor_id) and guarantee ($guarantee_id) combination",
168
        'Exception stringified correctly'
169
    );
170
171
    throws_ok
172
        { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( "Manual message exception" ) }
173
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
174
        'Exception is thrown :-D';
175
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
176
};
(-)a/t/db_dependent/Koha/Patron.t (+74 lines)
Line 0 Link Here
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 => 6;
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
    $SIG{__WARN__} = sub {}; # FIXME: PrintError = 0 not working!
67
68
    throws_ok
69
        { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father2' }); }
70
        'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
71
        'Exception is thrown for duplicated relationship';
72
73
    $schema->storage->txn_rollback;
74
};
(-)a/t/db_dependent/Koha/Patron/Relationship.t (-1 / +111 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 $dbh     = $schema->storage->dbh;
34
my $builder = t::lib::TestBuilder->new;
35
36
subtest 'store() tests' => sub {
37
38
    plan tests => 9;
39
40
    $schema->storage->txn_begin;
41
42
    t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
43
44
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
45
    my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
46
47
    my $relationship_1 = Koha::Patron::Relationship->new(
48
        {   guarantor_id => $patron_2->borrowernumber,
49
            guarantee_id => $patron_1->borrowernumber
50
        }
51
    );
52
53
    throws_ok
54
        { $relationship_1->store; }
55
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
56
        'Exception is thrown as no relationship passed';
57
58
    is( "$@", "No relationship passed.", 'Exception stringified correctly' );
59
60
    is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
61
        0,
62
        'No guarantors added'
63
    );
64
65
    my $relationship = 'father';
66
67
    throws_ok
68
        { $relationship_1->relationship($relationship)->store; }
69
        'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
70
        'Exception is thrown as a wrong relationship was passed';
71
72
    is( "$@", "Invalid relationship passed, '$relationship' is not defined.", 'Exception stringified correctly' );
73
74
    is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
75
        0,
76
        'No guarantors added'
77
    );
78
79
    $relationship_1->relationship('father1')->store;
80
81
    is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
82
        1,
83
        'Guarantor added'
84
    );
85
86
    my $relationship_2 = Koha::Patron::Relationship->new(
87
        {   guarantor_id => $patron_2->borrowernumber,
88
            guarantee_id => $patron_1->borrowernumber,
89
            relationship => 'father2'
90
        }
91
    );
92
93
    $SIG{__WARN__} = sub {}; # FIXME: PrintError = 0 not working!
94
95
    throws_ok
96
        { $relationship_2->store; }
97
        'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
98
        'Exception is thrown for duplicated relationship';
99
100
    is( "$@",
101
        "There already exists a relationship for the same guarantor ("
102
            . $patron_2->borrowernumber
103
            . ") and guarantee ("
104
            . $patron_1->borrowernumber
105
            . ") combination",
106
        'Exception stringified correctly'
107
    );
108
109
110
    $schema->storage->txn_rollback;
111
};

Return to bug 14570