Bug 23271: Prevent crash if called without parameters
[koha.git] / t / db_dependent / Koha / Patron / Relationship.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 $dbh     = $schema->storage->dbh;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'store() tests' => sub {
37
38     plan tests => 13;
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 = '';
80
81     throws_ok
82         { $relationship_1->relationship($relationship)->store; }
83         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
84         'Exception is thrown as a wrong relationship was passed';
85
86     is( "$@", "Invalid relationship passed, '$relationship' is not defined.", 'Exception stringified correctly' );
87
88     is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
89         0,
90         'No guarantors added when empty relationship passed and not defined'
91     );
92
93     $relationship = 'father1';
94
95     $relationship_1->relationship($relationship)->store;
96
97     is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
98         1,
99         'Guarantor added'
100     );
101
102     my $relationship_2 = Koha::Patron::Relationship->new(
103         {   guarantor_id => $patron_2->borrowernumber,
104             guarantee_id => $patron_1->borrowernumber,
105             relationship => 'father2'
106         }
107     );
108
109     {
110         local *STDERR;
111         open STDERR, '>', '/dev/null';
112         throws_ok
113             { $relationship_2->store; }
114             'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
115             'Exception is thrown for duplicated relationship';
116
117         is( "$@",
118             "There already exists a relationship for the same guarantor ("
119                 . $patron_2->borrowernumber
120                 . ") and guarantee ("
121                 . $patron_1->borrowernumber
122                 . ") combination",
123             'Exception stringified correctly'
124         );
125     }
126
127     t::lib::Mocks::mock_preference( 'borrowerRelationship', '' );
128
129     my $relationship_3 = Koha::Patron::Relationship->new(
130         {
131             guarantor_id => $patron_1->borrowernumber,
132             guarantee_id => $patron_2->borrowernumber,
133             relationship => ''
134         }
135     )->store();
136
137     is( $relationship_3->relationship, '', 'Empty relationship allowed' );
138
139     $schema->storage->txn_rollback;
140 };