3 # Copyright 2019 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 1;
27 use Koha::Patron::Relationships;
29 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 my $dbh = $schema->storage->dbh;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'store() tests' => sub {
40 $schema->storage->txn_begin;
42 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' );
44 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
45 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
47 my $relationship_1 = Koha::Patron::Relationship->new(
48 { guarantor_id => $patron_2->borrowernumber,
49 guarantee_id => $patron_1->borrowernumber
54 { $relationship_1->store; }
55 'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
56 'Exception is thrown as no relationship passed';
58 is( "$@", "No relationship passed.", 'Exception stringified correctly' );
60 is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
65 my $relationship = 'father';
68 { $relationship_1->relationship($relationship)->store; }
69 'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
70 'Exception is thrown as a wrong relationship was passed';
72 is( "$@", "Invalid relationship passed, '$relationship' is not defined.", 'Exception stringified correctly' );
74 is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
82 { $relationship_1->relationship($relationship)->store; }
83 'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
84 'Exception is thrown as a wrong relationship was passed';
86 is( "$@", "Invalid relationship passed, '$relationship' is not defined.", 'Exception stringified correctly' );
88 is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
90 'No guarantors added when empty relationship passed and not defined'
93 $relationship = 'father1';
95 $relationship_1->relationship($relationship)->store;
97 is( Koha::Patron::Relationships->search( { guarantee_id => $patron_1->borrowernumber } )->count,
102 my $relationship_2 = Koha::Patron::Relationship->new(
103 { guarantor_id => $patron_2->borrowernumber,
104 guarantee_id => $patron_1->borrowernumber,
105 relationship => 'father2'
111 open STDERR, '>', '/dev/null';
113 { $relationship_2->store; }
114 'Koha::Exceptions::Patron::Relationship::DuplicateRelationship',
115 'Exception is thrown for duplicated relationship';
118 "There already exists a relationship for the same guarantor ("
119 . $patron_2->borrowernumber
120 . ") and guarantee ("
121 . $patron_1->borrowernumber
123 'Exception stringified correctly'
127 t::lib::Mocks::mock_preference( 'borrowerRelationship', '' );
129 my $relationship_3 = Koha::Patron::Relationship->new(
131 guarantor_id => $patron_1->borrowernumber,
132 guarantee_id => $patron_2->borrowernumber,
137 is( $relationship_3->relationship, '', 'Empty relationship allowed' );
139 $schema->storage->txn_rollback;