3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 59;
24 use t::lib::TestBuilder;
27 use_ok('Koha::Objects');
28 use_ok('Koha::Patrons');
29 use_ok('Koha::Patron::Relationship');
30 use_ok('Koha::Patron::Relationships');
34 my $dbh = C4::Context->dbh;
35 $dbh->{AutoCommit} = 0;
36 $dbh->{RaiseError} = 1;
38 my $builder = t::lib::TestBuilder->new();
41 my $kyle = Koha::Patrons->find(
54 my $chelsea = Koha::Patrons->find(
59 firstname => 'Chelsea',
67 my $daria = Koha::Patrons->find(
79 my $kylie = Koha::Patrons->find(
91 Koha::Patron::Relationship->new({ guarantor_id => $kyle->id, guarantee_id => $daria->id, relationship => 'father' })->store();
92 Koha::Patron::Relationship->new({ guarantor_id => $kyle->id, guarantee_id => $kylie->id, relationship => 'father' })->store();
93 Koha::Patron::Relationship->new({ guarantor_id => $chelsea->id, guarantee_id => $daria->id, relationship => 'mother' })->store();
94 Koha::Patron::Relationship->new({ guarantor_id => $chelsea->id, guarantee_id => $kylie->id, relationship => 'mother' })->store();
98 @gr = $kyle->guarantee_relationships();
99 is( @gr, 2, 'Found 2 guarantee relationships for father' );
100 is( $gr[0]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' );
101 is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
102 is( $gr[0]->relationship, 'father', 'Relationship is father' );
103 is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
104 is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
105 is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
106 is( $gr[0]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' );
108 is( $gr[1]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' );
109 is( $gr[1]->guarantee_id, $kylie->id, 'Guarantee matches for first relationship' );
110 is( $gr[1]->relationship, 'father', 'Relationship is father' );
111 is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
112 is( $gr[1]->guarantee->id, $kylie->id, 'Koha::Patron returned is the correct guarantee' );
113 is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
114 is( $gr[1]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' );
116 @gr = $chelsea->guarantee_relationships();
117 is( @gr, 2, 'Found 2 guarantee relationships for mother' );
118 is( $gr[0]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' );
119 is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
120 is( $gr[0]->relationship, 'mother', 'Relationship is mother' );
121 is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
122 is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
123 is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
124 is( $gr[0]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' );
126 is( $gr[1]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' );
127 is( $gr[1]->guarantee_id, $kylie->id, 'Guarantee matches for first relationship' );
128 is( $gr[1]->relationship, 'mother', 'Relationship is mother' );
129 is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
130 is( $gr[1]->guarantee->id, $kylie->id, 'Koha::Patron returned is the correct guarantee' );
131 is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
132 is( $gr[1]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' );
134 @gr = $daria->guarantor_relationships();
135 is( @gr, 2, 'Found 4 guarantor relationships for child' );
136 is( $gr[0]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' );
137 is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
138 is( $gr[0]->relationship, 'father', 'Relationship is father' );
139 is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
140 is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
141 is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
142 is( $gr[0]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' );
144 is( $gr[1]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' );
145 is( $gr[1]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
146 is( $gr[1]->relationship, 'mother', 'Relationship is mother' );
147 is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
148 is( $gr[1]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
149 is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
150 is( $gr[1]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' );
152 my @siblings = $daria->siblings;
153 is( @siblings, 1, 'Method siblings called in list context returns list' );
154 is( ref($siblings[0]), 'Koha::Patron', 'List contains a Koha::Patron' );
155 is( $siblings[0]->firstname, 'Kylie', 'Sibling from list first name matches correctly' );
156 is( $siblings[0]->surname, 'Hall', 'Sibling from list surname matches correctly' );
157 is( $siblings[0]->id, $kylie->id, 'Sibling from list patron id matches correctly' );
159 my $siblings = $daria->siblings;
160 my $sibling = $siblings->next();
161 is( ref($siblings), 'Koha::Patrons', 'Calling siblings in scalar context results in a Koha::Patrons object' );
162 is( ref($sibling), 'Koha::Patron', 'Method next returns a Koha::Patron' );
163 is( $sibling->firstname, 'Kylie', 'Sibling from scalar first name matches correctly' );
164 is( $sibling->surname, 'Hall', 'Sibling from scalar surname matches correctly' );
165 is( $sibling->id, $kylie->id, 'Sibling from scalar patron id matches correctly' );