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
=> 37;
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
129 club_template_id
=> $club_template->id,
130 name
=> "Test Expired Club",
131 branchcode
=> $branchcode,
132 date_start
=> '1900-01-01',
133 date_end
=> '1900-01-02',
137 my $club = Koha
::Club
->new(
139 club_template_id
=> $club_template->id,
141 branchcode
=> $branchcode,
142 date_start
=> '1900-01-01',
143 date_end
=> '9999-01-01',
147 my $club_field_1 = Koha
::Club
::Field
->new(
149 club_template_field_id
=> $club_template_field_1->id,
150 club_id
=> $club->id,
154 is
( ref($club_field_1), 'Koha::Club::Field', 'Club field 1 created' );
156 $club_field_1->club_template_field->id,
157 $club_template_field_1->id,
158 'Field 2 is linked to correct template field'
161 my $club_field_2 = Koha
::Club
::Field
->new(
163 club_template_field_id
=> $club_template_field_2->id,
164 club_id
=> $club->id,
168 is
( ref($club_field_2), 'Koha::Club::Field', 'Club field 2 created' );
170 $club_field_2->club_template_field->id,
171 $club_template_field_2->id,
172 'Field 2 is linked to correct template field'
175 is
( ref($club), 'Koha::Club', 'Club created' );
176 is
( $club->club_template->id,
177 $club_template->id, 'Club is using correct template' );
178 is
( $club->branch->id, $branchcode, 'Club is using correct branch' );
179 is
( $club->club_fields->count, 2, 'Club has correct number of fields' );
180 is
( Koha
::Clubs
->get_enrollable( { borrower
=> $patron } )->count,
181 1, 'Koha::Clubs->get_enrollable returns 1 enrollable club for patron' );
182 is
( $patron->get_enrollable_clubs->count,
183 1, 'There is 1 enrollable club for patron' );
185 ## Create an enrollment for this club
186 my $club_enrollment = Koha
::Club
::Enrollment
->new(
188 club_id
=> $club->id,
189 borrowernumber
=> $patron->id,
190 branchcode
=> $branchcode,
193 is
( ref($club_enrollment), 'Koha::Club::Enrollment',
194 'Club enrollment created' );
196 my $club_enrollment_field_1 = Koha
::Club
::Enrollment
::Field
->new(
198 club_enrollment_id
=> $club_enrollment->id,
199 club_template_enrollment_field_id
=>
200 $club_template_enrollment_field_1->id,
205 ref($club_enrollment_field_1),
206 'Koha::Club::Enrollment::Field',
207 'Enrollment field 1 created'
210 my $club_enrollment_field_2 = Koha
::Club
::Enrollment
::Field
->new(
212 club_enrollment_id
=> $club_enrollment->id,
213 club_template_enrollment_field_id
=>
214 $club_template_enrollment_field_2->id,
219 ref($club_enrollment_field_2),
220 'Koha::Club::Enrollment::Field',
221 'Enrollment field 2 created'
224 is
( $club_enrollment->club->id, $club->id, 'Got correct club for enrollment' );
225 is
( Koha
::Clubs
->get_enrollable( { borrower
=> $patron } )->count,
226 0, 'Koha::Clubs->get_enrollable returns 0 enrollable clubs for patron' );
227 is
( $patron->get_club_enrollments->count,
228 1, 'Got 1 club enrollment for patron' );
229 is
( $patron->get_enrollable_clubs->count,
230 0, 'No more enrollable clubs for patron' );
231 is
( $club->club_enrollments->count, 1, 'There is 1 enrollment for club' );
233 $schema->storage->txn_rollback();