Debian Codegraph @

t/repeatable/has_many.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use_ok( 'HTML::FormHandler::Field::Repeatable' );
6 use_ok( 'HTML::FormHandler::Field::Repeatable::Instance' );
7
8 {
9 package Repeatable::Form;
10 use HTML::FormHandler::Moose;
11 extends 'HTML::FormHandler';
12
13 has_field 'my_test';
14 has_field 'addresses' => ( type => 'Repeatable', auto_id => 1 );
15 has_field 'addresses.street';
16 has_field 'addresses.city';
17 has_field 'addresses.country';
18 has_field 'addresses.sector' => ( type => 'Select' );
19
20 sub options_addresses_sector
21 {
22 [ 1 => 'East',
23 2 => 'West',
24 3 => 'South'
25 ]
26 }
27
28 }
29
30 my $form = Repeatable::Form->new;
31
32 ok( $form, 'created hasmany form');
33
34 $form = Repeatable::Form->new;
35 ok( $form->field('addresses')->has_fields, 'created form again with fields');
36
37 # empty form, creating new record
38 $form->process( params => {} );
39 ok( $form->field('addresses')->field('0')->field('city'), 'empty field exists' );
40 is( $form->field('addresses')->field('0')->field('sector')->num_options, 3, 'right number of options');
41
42 my $init_object = {
43 addresses => [
44 {
45 street => 'First Street',
46 city => 'Prime City',
47 country => 'Utopia',
48 id => 0,
49 },
50 {
51 street => 'Second Street',
52 city => 'Secondary City',
53 country => 'Graustark',
54 id => 1,
55 },
56 {
57 street => 'Third Street',
58 city => 'Tertiary City',
59 country => 'Atlantis',
60 id => 2,
61 }
62 ],
63 };
64
65 $form = Repeatable::Form->new( init_object => $init_object );
66 ok( $form, 'created form from initial object' );
67
68 # add in fields in form not in init_object
69 $init_object->{my_test} = undef;
70 $init_object->{addresses}->[0]->{sector} = undef;
71 $init_object->{addresses}->[1]->{sector} = undef;
72 $init_object->{addresses}->[2]->{sector} = undef;
73 is_deeply( $form->values, $init_object, 'get values back out' );
74 delete $init_object->{my_test};
75 is_deeply( $form->field('addresses')->value, $init_object->{addresses}, 'hasmany field value');
76 is_deeply( $form->field('addresses')->field('0')->value, $init_object->{addresses}->[0],
77 'instance field value' );
78 is( $form->field('addresses')->field('0')->field('sector')->num_options, 3, 'right number of options');
79 is( $form->field('addresses')->field('0')->field('city')->value, 'Prime City',
80 'compound subfield value');
81
82 my $fif = {
83 'addresses.0.street' => 'First Street',
84 'addresses.0.city' => 'Prime City',
85 'addresses.0.country' => 'Utopia',
86 'addresses.0.id' => '0',
87 'addresses.0.sector' => '',
88 'addresses.1.street' => 'Second Street',
89 'addresses.1.city' => 'Secondary City',
90 'addresses.1.country' => 'Graustark',
91 'addresses.1.id' => '1',
92 'addresses.1.sector' => '',
93 'addresses.2.street' => 'Third Street',
94 'addresses.2.city' => 'Tertiary City',
95 'addresses.2.country' => 'Atlantis',
96 'addresses.2.id' => '2',
97 'addresses.2.sector' => '',
98 'my_test' => '',
99 };
100 is_deeply( $form->fif, $fif, 'get fill in form');
101 $fif->{'addresses.0.city'} = 'Primary City';
102 $fif->{'addresses.2.country'} = 'Grand Fenwick';
103 delete $fif->{my_test};
104 $form->clear;
105 $form->process($fif);
106 ok($form->validated, 'validate fif');
107 $fif->{my_test} = '';
108 is_deeply( $form->fif, $fif, 'still get right fif');
109 $init_object->{addresses}->[0]->{city} = 'Primary City';
110 $init_object->{addresses}->[2]->{country} = 'Grand Fenwick';
111 is_deeply( $form->values, $init_object, 'still get right values');
112
113 $fif = {
114 'addresses.0.street' => 'First Street',
115 'addresses.0.city' => 'Prime City',
116 'addresses.0.country' => 'Utopia',
117 'addresses.0.id' => '0',
118 'addresses.0.sector' => undef,
119 'addresses.2.street' => 'Third Street',
120 'addresses.2.city' => 'Tertiary City',
121 'addresses.2.country' => 'Atlantis',
122 'addresses.2.id' => '2',
123 'addresses.2.sector' => undef,
124 };
125
126 $form->process($fif);
127
128 ok( $form->validated, 'form validated' );
129 is( $form->field('addresses')->num_fields, 2, 'right number of fields');
130
131 $fif = {
132 'addresses.0.street' => 'Main Street',
133 'addresses.0.city' => 'Prime City',
134 'addresses.0.country' => 'Utopia',
135 'addresses.0.id' => '0',
136 'addresses.0.sector' => undef,
137 };
138
139 ok( $form->process($fif), 'process a single repeatable element');
140 is( $form->field('addresses')->field('0')->field('street')->value, 'Main Street', 'get value');
141 is( $form->field('addresses')->field('0')->field('sector')->num_options, 3, 'right number of options');
142
143 my $values = {
144 'addresses' => [
145 {
146 'city' => 'Prime City',
147 'country' => 'Utopia',
148 'id' => 0,
149 'street' => 'Main Street',
150 'sector' => undef,
151 },
152 ],
153 };
154 is_deeply( $form->values, $values, 'get right values' );
155
156 my $no_repeat = {
157 my_test => 'test'
158 };
159 $form->process( $no_repeat );
160 is_deeply( $form->value()->{addresses}, [], 'Addresses deleted not in params' );
161
162 $form->process( init_object => $init_object );
163 ok( exists $form->value->{addresses}[0], 'Addresses are back' );
164 is( $form->field('addresses')->field('0')->field('sector')->num_options, 3, 'right number of options');
165 $form->clear_init_object;
166 $form->process( { my_test => 'test' } );
167 is_deeply( $form->value()->{addresses}, [], 'Addresses deleted' );
168
169 {
170 package Test::User::Repeatable;
171 use HTML::FormHandler::Moose;
172 extends 'HTML::FormHandler';
173
174 has_field 'user_name';
175 has_field 'occupation';
176 has_field 'employers' => ( type => 'Repeatable' );
177 has_field 'employers.employer_id' => ( type => 'PrimaryKey' );
178 has_field 'employers.name';
179 has_field 'employers.address';
180 }
181 $form = Test::User::Repeatable->new;
182 my $unemployed_params = {
183 user_name => "No Employer",
184 occupation => "Unemployed",
185 'employers.0.employer_id' => '', # empty string
186 'employers.0.name' => '',
187 'employers.0.address' => ''
188 };
189 $form->process( $unemployed_params);
190 ok( $form->validated, "User with empty employer validates" );
191 is_deeply( $form->value, { employers => [], user_name => 'No Employer', occupation => 'Unemployed' },
192 'creates right value for empty repeatable' );
193 is_deeply( $form->fif, $unemployed_params, 'right fif for empty repeatable' );
194
195
196 # following tests Duration in a Repeatable, with no other subfield
197 {
198 package Form::RepeatableCompound;
199
200 use HTML::FormHandler::Moose;
201 extends 'HTML::FormHandler';
202
203 has_field timeslots => (type => 'Repeatable');
204
205 has_field 'timeslots.duration' => (type => 'Duration');
206 has_field 'timeslots.duration.hours' => (type => 'Integer');
207 has_field 'timeslots.duration.minutes' => (type => 'Integer');
208 }
209
210 $form = Form::RepeatableCompound->new;
211 my $params = {
212 'timeslots.0.id' => 'zero',
213 'timeslots.0.duration.hours' => 10,
214 'timeslots.0.duration.minutes' => 12,
215 'timeslots.1.id' => 'one',
216 'timeslots.1.duration.hours' => 2,
217 'timeslots.1.duration.minutes' => 1,
218 };
219
220 $form->process( params => $params );
221 ok( $form->validated, 'form validated' );
222 my $value = $form->value;
223 my $dur0 = DateTime::Duration->new( hours => 10, minutes => 12 );
224 is( $value->{timeslots}->[0]->{duration}->in_units('minutes'), $dur0->in_units('minutes'), 'got same duration' );
225
226 done_testing;