Bug 25417: Add unit test
[koha.git] / t / db_dependent / Clubs.t
blobee0f83b2c5c0f59cc048f4dc8383c84ebf6cb4de
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 37;
21 use Test::Warn;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::Patrons;
27 use t::lib::TestBuilder;
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');
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;
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(
58 surname => 'Test 1',
59 branchcode => $branchcode,
60 categorycode => $categorycode
63 $patron->store();
65 my $club_template = Koha::Club::Template->new(
67 name => "Test Club Template",
68 is_enrollable_from_opac => 0,
69 is_email_required => 0,
71 )->store();
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',
80 )->store();
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',
89 )->store();
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',
103 )->store();
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',
116 )->store();
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 Koha::Club->new(
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',
135 )->store();
137 my $club = Koha::Club->new(
139 club_template_id => $club_template->id,
140 name => "Test Club",
141 branchcode => $branchcode,
142 date_start => '1900-01-01',
143 date_end => '9999-01-01',
145 )->store();
147 my $club_field_1 = Koha::Club::Field->new(
149 club_template_field_id => $club_template_field_1->id,
150 club_id => $club->id,
151 value => 'TEST',
153 )->store();
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,
165 value => 'TEST',
167 )->store();
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,
192 )->store();
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,
201 value => 'TEST',
203 )->store();
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,
215 value => 'TEST',
217 )->store();
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();