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 => 36;
27 use t::lib::TestBuilder;
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');
45 my $database = Koha::Database->new();
46 my $schema = $database->schema();
47 my $dbh = C4::Context->dbh;
48 my $builder = t::lib::TestBuilder->new;
50 $schema->storage->txn_begin();
51 $dbh->do("DELETE FROM club_templates");
53 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
54 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
56 my $patron = Koha::Patron->new(
59 branchcode => $branchcode,
60 categorycode => $categorycode
65 my $club_template = Koha::Club::Template->new(
67 name => "Test Club Template",
68 is_enrollable_from_opac => 0,
69 is_email_required => 0,
72 is( ref($club_template), 'Koha::Club::Template', 'Club template created' );
74 # Add some template fields
75 my $club_template_field_1 = Koha::Club::Template::Field->new(
77 club_template_id => $club_template->id,
78 name => 'Test Club Template Field 1',
81 is( ref($club_template_field_1),
82 'Koha::Club::Template::Field', 'Club template field 1 created' );
84 my $club_template_field_2 = Koha::Club::Template::Field->new(
86 club_template_id => $club_template->id,
87 name => 'Test Club Template Field 2',
90 is( ref($club_template_field_2),
91 'Koha::Club::Template::Field', 'Club template field 2 created' );
93 is( $club_template->club_template_fields->count,
94 2, 'Club template has two fields' );
96 ## Add some template enrollment fields
97 my $club_template_enrollment_field_1 =
98 Koha::Club::Template::EnrollmentField->new(
100 club_template_id => $club_template->id,
101 name => 'Test Club Template EnrollmentField 1',
105 ref($club_template_enrollment_field_1),
106 'Koha::Club::Template::EnrollmentField',
107 'Club template field 1 created'
110 my $club_template_enrollment_field_2 =
111 Koha::Club::Template::EnrollmentField->new(
113 club_template_id => $club_template->id,
114 name => 'Test Club Template EnrollmentField 2',
118 ref($club_template_enrollment_field_2),
119 'Koha::Club::Template::EnrollmentField',
120 'Club template field 2 created'
123 is( $club_template->club_template_enrollment_fields->count,
124 2, 'Club template has two enrollment fields' );
126 ## Create a club based on this template
127 my $club = Koha::Club->new(
129 club_template_id => $club_template->id,
131 branchcode => $branchcode,
135 my $club_field_1 = Koha::Club::Field->new(
137 club_template_field_id => $club_template_field_1->id,
138 club_id => $club->id,
142 is( ref($club_field_1), 'Koha::Club::Field', 'Club field 1 created' );
144 $club_field_1->club_template_field->id,
145 $club_template_field_1->id,
146 'Field 2 is linked to correct template field'
149 my $club_field_2 = Koha::Club::Field->new(
151 club_template_field_id => $club_template_field_2->id,
152 club_id => $club->id,
156 is( ref($club_field_2), 'Koha::Club::Field', 'Club field 2 created' );
158 $club_field_2->club_template_field->id,
159 $club_template_field_2->id,
160 'Field 2 is linked to correct template field'
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' );
173 ## Create an enrollment for this club
174 my $club_enrollment = Koha::Club::Enrollment->new(
176 club_id => $club->id,
177 borrowernumber => $patron->id,
178 branchcode => $branchcode,
181 is( ref($club_enrollment), 'Koha::Club::Enrollment',
182 'Club enrollment created' );
184 my $club_enrollment_field_1 = Koha::Club::Enrollment::Field->new(
186 club_enrollment_id => $club_enrollment->id,
187 club_template_enrollment_field_id =>
188 $club_template_enrollment_field_1->id,
193 ref($club_enrollment_field_1),
194 'Koha::Club::Enrollment::Field',
195 'Enrollment field 1 created'
198 my $club_enrollment_field_2 = Koha::Club::Enrollment::Field->new(
200 club_enrollment_id => $club_enrollment->id,
201 club_template_enrollment_field_id =>
202 $club_template_enrollment_field_2->id,
207 ref($club_enrollment_field_2),
208 'Koha::Club::Enrollment::Field',
209 'Enrollment field 2 created'
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' );
220 $schema->storage->txn_rollback();