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 |
}; |