Bug 12461 - Add patron clubs feature
[koha.git] / t / db_dependent / Clubs.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 => 36;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::Patrons;
26
27 use t::lib::TestBuilder;
28
29 BEGIN {
30     use_ok('Koha::Club');
31     use_ok('Koha::Clubs');
32     use_ok('Koha::Club::Field');
33     use_ok('Koha::Club::Fields');
34     use_ok('Koha::Club::Template');
35     use_ok('Koha::Club::Templates');
36     use_ok('Koha::Club::Template::Field');
37     use_ok('Koha::Club::Template::Fields');
38     use_ok('Koha::Club::Enrollment::Field');
39     use_ok('Koha::Club::Enrollment::Fields');
40     use_ok('Koha::Club::Template::EnrollmentField');
41     use_ok('Koha::Club::Template::EnrollmentFields');
42 }
43
44 # Start transaction
45 my $database = Koha::Database->new();
46 my $schema   = $database->schema();
47 my $dbh      = C4::Context->dbh;
48 my $builder  = t::lib::TestBuilder->new;
49
50 $schema->storage->txn_begin();
51 $dbh->do("DELETE FROM club_templates");
52
53 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
54 my $branchcode   = $builder->build( { source => 'Branch' } )->{branchcode};
55
56 my $patron = Koha::Patron->new(
57     {
58         surname      => 'Test 1',
59         branchcode   => $branchcode,
60         categorycode => $categorycode
61     }
62 );
63 $patron->store();
64
65 my $club_template = Koha::Club::Template->new(
66     {
67         name                    => "Test Club Template",
68         is_enrollable_from_opac => 0,
69         is_email_required       => 0,
70     }
71 )->store();
72 is( ref($club_template), 'Koha::Club::Template', 'Club template created' );
73
74 # Add some template fields
75 my $club_template_field_1 = Koha::Club::Template::Field->new(
76     {
77         club_template_id => $club_template->id,
78         name             => 'Test Club Template Field 1',
79     }
80 )->store();
81 is( ref($club_template_field_1),
82     'Koha::Club::Template::Field', 'Club template field 1 created' );
83
84 my $club_template_field_2 = Koha::Club::Template::Field->new(
85     {
86         club_template_id => $club_template->id,
87         name             => 'Test Club Template Field 2',
88     }
89 )->store();
90 is( ref($club_template_field_2),
91     'Koha::Club::Template::Field', 'Club template field 2 created' );
92
93 is( $club_template->club_template_fields->count,
94     2, 'Club template has two fields' );
95
96 ## Add some template enrollment fields
97 my $club_template_enrollment_field_1 =
98   Koha::Club::Template::EnrollmentField->new(
99     {
100         club_template_id => $club_template->id,
101         name             => 'Test Club Template EnrollmentField 1',
102     }
103   )->store();
104 is(
105     ref($club_template_enrollment_field_1),
106     'Koha::Club::Template::EnrollmentField',
107     'Club template field 1 created'
108 );
109
110 my $club_template_enrollment_field_2 =
111   Koha::Club::Template::EnrollmentField->new(
112     {
113         club_template_id => $club_template->id,
114         name             => 'Test Club Template EnrollmentField 2',
115     }
116   )->store();
117 is(
118     ref($club_template_enrollment_field_2),
119     'Koha::Club::Template::EnrollmentField',
120     'Club template field 2 created'
121 );
122
123 is( $club_template->club_template_enrollment_fields->count,
124     2, 'Club template has two enrollment fields' );
125
126 ## Create a club based on this template
127 my $club = Koha::Club->new(
128     {
129         club_template_id => $club_template->id,
130         name             => "Test Club",
131         branchcode       => $branchcode,
132     }
133 )->store();
134
135 my $club_field_1 = Koha::Club::Field->new(
136     {
137         club_template_field_id => $club_template_field_1->id,
138         club_id                => $club->id,
139         value                  => 'TEST',
140     }
141 )->store();
142 is( ref($club_field_1), 'Koha::Club::Field', 'Club field 1 created' );
143 is(
144     $club_field_1->club_template_field->id,
145     $club_template_field_1->id,
146     'Field 2 is linked to correct template field'
147 );
148
149 my $club_field_2 = Koha::Club::Field->new(
150     {
151         club_template_field_id => $club_template_field_2->id,
152         club_id                => $club->id,
153         value                  => 'TEST',
154     }
155 )->store();
156 is( ref($club_field_2), 'Koha::Club::Field', 'Club field 2 created' );
157 is(
158     $club_field_2->club_template_field->id,
159     $club_template_field_2->id,
160     'Field 2 is linked to correct template field'
161 );
162
163 is( ref($club), 'Koha::Club', 'Club created' );
164 is( $club->club_template->id,
165     $club_template->id, 'Club is using correct template' );
166 is( $club->branch->id, $branchcode, 'Club is using correct branch' );
167 is( $club->club_fields->count, 2, 'Club has correct number of fields' );
168 is( Koha::Clubs->get_enrollable( { borrower => $patron } )->count,
169     1, 'Koha::Clubs->get_enrollable returns 1 enrollable club for patron' );
170 is( $patron->get_enrollable_clubs->count,
171     1, 'There is 1 enrollable club for patron' );
172
173 ## Create an enrollment for this club
174 my $club_enrollment = Koha::Club::Enrollment->new(
175     {
176         club_id        => $club->id,
177         borrowernumber => $patron->id,
178         branchcode     => $branchcode,
179     }
180 )->store();
181 is( ref($club_enrollment), 'Koha::Club::Enrollment',
182     'Club enrollment created' );
183
184 my $club_enrollment_field_1 = Koha::Club::Enrollment::Field->new(
185     {
186         club_enrollment_id => $club_enrollment->id,
187         club_template_enrollment_field_id =>
188           $club_template_enrollment_field_1->id,
189         value => 'TEST',
190     }
191 )->store();
192 is(
193     ref($club_enrollment_field_1),
194     'Koha::Club::Enrollment::Field',
195     'Enrollment field 1 created'
196 );
197
198 my $club_enrollment_field_2 = Koha::Club::Enrollment::Field->new(
199     {
200         club_enrollment_id => $club_enrollment->id,
201         club_template_enrollment_field_id =>
202           $club_template_enrollment_field_2->id,
203         value => 'TEST',
204     }
205 )->store();
206 is(
207     ref($club_enrollment_field_2),
208     'Koha::Club::Enrollment::Field',
209     'Enrollment field 2 created'
210 );
211
212 is( $club_enrollment->club->id, $club->id, 'Got correct club for enrollment' );
213 is( Koha::Clubs->get_enrollable( { borrower => $patron } )->count,
214     0, 'Koha::Clubs->get_enrollable returns 0 enrollable clubs for patron' );
215 is( $patron->get_club_enrollments->count,
216     1, 'Got 1 club enrollment for patron' );
217 is( $patron->get_enrollable_clubs->count,
218     0, 'No more enrollable clubs for patron' );
219
220 $schema->storage->txn_rollback();
221 1;