d73e7ef796a17890105830800b92270191e948f9
[koha.git] / t / db_dependent / Patron / Relationships.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 54;
21
22 use C4::Context;
23
24 use t::lib::TestBuilder;
25
26 BEGIN {
27     use_ok('Koha::Objects');
28     use_ok('Koha::Patrons');
29     use_ok('Koha::Patron::Relationship');
30     use_ok('Koha::Patron::Relationships');
31 }
32
33 my $builder = t::lib::TestBuilder->new();
34
35 # Father
36 my $kyle = Koha::Patrons->find(
37     $builder->build(
38         {
39             source => 'Borrower',
40             value  => {
41                 firstname => 'Kyle',
42                 surname   => 'Hall',
43             }
44         }
45     )->{borrowernumber}
46 );
47
48 # Mother
49 my $chelsea = Koha::Patrons->find(
50     $builder->build(
51         {
52             source => 'Borrower',
53             value  => {
54                 firstname => 'Chelsea',
55                 surname   => 'Hall',
56             }
57         }
58     )->{borrowernumber}
59 );
60
61 # Children
62 my $daria = Koha::Patrons->find(
63     $builder->build(
64         {
65             source => 'Borrower',
66             value  => {
67                 firstname => 'Daria',
68                 surname   => 'Hall',
69             }
70         }
71     )->{borrowernumber}
72 );
73
74 my $kylie = Koha::Patrons->find(
75     $builder->build(
76         {
77             source => 'Borrower',
78             value  => {
79                 firstname => 'Kylie',
80                 surname   => 'Hall',
81             }
82         }
83     )->{borrowernumber}
84 );
85
86 Koha::Patron::Relationship->new({ guarantor_id => $kyle->id, guarantee_id => $daria->id, relationship => 'father' })->store();
87 Koha::Patron::Relationship->new({ guarantor_id => $kyle->id, guarantee_id => $kylie->id, relationship => 'father' })->store();
88 Koha::Patron::Relationship->new({ guarantor_id => $chelsea->id, guarantee_id => $daria->id, relationship => 'mother' })->store();
89 Koha::Patron::Relationship->new({ guarantor_id => $chelsea->id, guarantee_id => $kylie->id, relationship => 'mother' })->store();
90
91 my @gr;
92
93 @gr = $kyle->guarantee_relationships()->as_list;
94 is( @gr, 2, 'Found 2 guarantee relationships for father' );
95 is( $gr[0]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' );
96 is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
97 is( $gr[0]->relationship, 'father', 'Relationship is father' );
98 is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
99 is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
100 is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
101 is( $gr[0]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' );
102
103 is( $gr[1]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' );
104 is( $gr[1]->guarantee_id, $kylie->id, 'Guarantee matches for first relationship' );
105 is( $gr[1]->relationship, 'father', 'Relationship is father' );
106 is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
107 is( $gr[1]->guarantee->id, $kylie->id, 'Koha::Patron returned is the correct guarantee' );
108 is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
109 is( $gr[1]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' );
110
111 @gr = $chelsea->guarantee_relationships()->as_list;
112 is( @gr, 2, 'Found 2 guarantee relationships for mother' );
113 is( $gr[0]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' );
114 is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
115 is( $gr[0]->relationship, 'mother', 'Relationship is mother' );
116 is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
117 is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
118 is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
119 is( $gr[0]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' );
120
121 is( $gr[1]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' );
122 is( $gr[1]->guarantee_id, $kylie->id, 'Guarantee matches for first relationship' );
123 is( $gr[1]->relationship, 'mother', 'Relationship is mother' );
124 is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
125 is( $gr[1]->guarantee->id, $kylie->id, 'Koha::Patron returned is the correct guarantee' );
126 is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
127 is( $gr[1]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' );
128
129 @gr = $daria->guarantor_relationships()->as_list;
130 is( @gr, 2, 'Found 4 guarantor relationships for child' );
131 is( $gr[0]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' );
132 is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
133 is( $gr[0]->relationship, 'father', 'Relationship is father' );
134 is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
135 is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
136 is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
137 is( $gr[0]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' );
138
139 is( $gr[1]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' );
140 is( $gr[1]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' );
141 is( $gr[1]->relationship, 'mother', 'Relationship is mother' );
142 is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' );
143 is( $gr[1]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' );
144 is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' );
145 is( $gr[1]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' );
146
147 my $siblings = $daria->siblings;
148 my $sibling = $siblings->next();
149 is( ref($siblings), 'Koha::Patrons', 'Calling siblings in scalar context results in a Koha::Patrons object' );
150 is( ref($sibling), 'Koha::Patron', 'Method next returns a Koha::Patron' );
151 is( $sibling->firstname, 'Kylie', 'Sibling from scalar first name matches correctly' );
152 is( $sibling->surname, 'Hall', 'Sibling from scalar surname matches correctly' );
153 is( $sibling->id, $kylie->id, 'Sibling from scalar patron id matches correctly' );
154
155 1;