Bug 14570: Add error handling to Koha::Patron::Relationship->store
[koha.git] / t / db_dependent / Koha / Patron.t
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 };