File: select.t

package info (click to toggle)
libhtml-formhandler-perl 0.40057-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,320 kB
  • ctags: 685
  • sloc: perl: 8,849; makefile: 2
file content (226 lines) | stat: -rw-r--r-- 7,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use strict;
use warnings;
use Test::More;
use HTML::FormHandler::Field::Text;


{
   package Test::Form;
   use HTML::FormHandler::Moose;
   extends 'HTML::FormHandler';

   has '+name' => ( default => 'options_form' );
   has_field 'test_field' => (
               type => 'Text',
               label => 'TEST',
               id    => 'f99',
            );
   has_field 'fruit' => ( type => 'Select' );
   has_field 'vegetables' => ( type => 'Multiple' );
   has_field 'empty' => ( type => 'Multiple', no_option_validation => 1 );
   has_field 'build_attr' => ( type => 'Select' );

   sub default_fruit { 2 }

   # the following sometimes happens with db options
   sub options_empty { ([]) }

   has 'options_fruit' => ( is => 'rw', traits => ['Array'],
       default => sub { [1 => 'apples', 2 => 'oranges',
           3 => 'kiwi'] } );

   sub options_vegetables {
       return (
           1   => 'lettuce',
           2   => 'broccoli',
           3   => 'carrots',
           4   => 'peas',
       );
   }

   has 'options_build_attr' => ( is => 'ro', traits => ['Array'], lazy_build => 1 );

   sub _build_options_build_attr {
       return [
           1 => 'testing',
           2 => 'moose',
           3 => 'attr builder',
       ];
   }
}


my $form = Test::Form->new;
ok( $form, 'create form');

my $veg_options =   [ {'label' => 'lettuce',
      'value' => 1 },
     {'label' => 'broccoli',
      'value' => 2 },
     {'label' => 'carrots',
      'value' => 3 },
     {'label' => 'peas',
      'value' => 4 } ];
my $field_options = $form->field('vegetables')->options_ref;
is_deeply( $field_options, $veg_options,
   'get options for vegetables' );

my $fruit_options = [ {'label' => 'apples',
       'value' => 1 },
      {'label' => 'oranges',
       'value' => 2 },
      {'label' => 'kiwi',
       'value' => 3 } ];
$field_options = $form->field('fruit')->options;
is_deeply( $field_options, $fruit_options,
    'get options for fruit' );

my $build_attr_options = [ {'label' => 'testing',
       'value' => 1 },
      {'label' => 'moose',
       'value' => 2 },
      {'label' => 'attr builder',
       'value' => 3 } ];
$field_options = $form->field('build_attr')->options_ref;
is_deeply( $field_options, $build_attr_options,
    'get options for fruit' );

is( $form->field('fruit')->value, 2, 'initial value ok');

$form->process( params => {},
    init_object => { vegetables => undef, fruit => undef, build_attr => undef } );
$field_options = $form->field('vegetables')->options;
is_deeply( $field_options, $veg_options,
   'get options for vegetables after process' );
$field_options = $form->field('fruit')->options;
is_deeply( $field_options, $fruit_options,
    'get options for fruit after process' );
$field_options = $form->field('build_attr')->options;
is_deeply( $field_options, $build_attr_options,
    'get options for fruit after process' );

my $params = {
   fruit => 2,
   vegetables => [2,4],
   empty => 'test',
};

$form->process( $params );
ok( $form->validated, 'form validated' );
is( $form->field('fruit')->value, 2, 'fruit value is correct');
is_deeply( $form->field('vegetables')->value, [2,4], 'vegetables value is correct');

is_deeply( $form->fif, { fruit => 2, vegetables => [2, 4], empty => ['test'], test_field => '', build_attr => '' },
    'fif is correct');
is_deeply( $form->values, { fruit => 2, vegetables => [2, 4], empty => ['test'], build_attr => undef },
    'values are correct');
is( $form->field('vegetables')->as_label, 'broccoli, peas', 'multiple as_label works');
is( $form->field('vegetables')->as_label([3,4]), 'carrots, peas', 'pass in multiple as_label works');

$params = {
    fruit => 2,
    vegetables => 4,
};
$form->process($params);
is_deeply( $form->field('vegetables')->value, [4], 'value for vegetables correct' );
is_deeply( $form->field('vegetables')->fif, [4], 'fif for vegetables correct' );

{
    package Test::Form2;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler';

    has_field 'my_list' => ( type => 'Select' );

    # this adds a 'selected' hash key to an option as an alternative
    # to setting the default for the field.
    sub options_my_list {
        return [
            {
                value => 1,
                label => 'One',
                selected => 1,
            },
            {
                value => 2,
                label => 'Two',
            },
            {
                value => 3,
                label  => 'Three',
            }
        ];
    }

}

$form = Test::Form2->new;
ok( $form, 'form built' );

my $rendered_field = $form->field('my_list')->render;
like( $rendered_field, qr/<option value="1" id="my_list\.0" selected="selected">/, 'element is selected' );
# the 'value' of the field should reflect the selected values
is_deeply( $form->value, { my_list => 1 },  'value ok' );
ok( $form->field('my_list')->fif, 'fif value');
$form->process( { my_list => 2 } );
is_deeply( $form->fif, { my_list => 2 }, 'fif is correct' );
$rendered_field = $form->field('my_list')->render;
like( $rendered_field, qr/<option value="2" id="my_list\.1" selected="selected">/, 'element is selected' );

# following test is for 'has_many' select field flag
{
    package Test::HasMany;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler';

    has_field 'foo' => ( default => 'my_foo' );
    has_field 'hm_bar' => ( type => 'Multiple',
        has_many => 'my_id', default => [3] );

    sub options_hm_bar { [1, 2, 3, 4] }
}
$form = Test::HasMany->new;
ok( $form, 'has many form built' );
$form->process( params => {} );
my $fif_expected = { foo => 'my_foo', hm_bar => [3] };
is_deeply( $form->fif, $fif_expected, 'got expected fif' );
$form->process( params => { foo => 'my_foo', hm_bar => [4] } );
my $val_expected = { foo => 'my_foo', hm_bar => [ { my_id => 4 } ] };
is_deeply( $form->value, $val_expected, 'got expected value' );
$fif_expected = { foo => 'my_foo', hm_bar => [4] };
is_deeply( $form->fif, $fif_expected, 'got expected fif' );
$form->process( params => { foo => 'my_foo', hm_bar => [1,2] } );
$fif_expected = { foo => 'my_foo', hm_bar => [1,2] };
is_deeply( $form->fif, $fif_expected, 'got expected fif again' );
$val_expected = { foo => 'my_foo', hm_bar => [ { my_id => 1 }, { my_id => 2 } ] };
is_deeply( $form->value, $val_expected, 'got expected value agina' );

{
    package Test::Multiple::InitObject;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler';

    has_field 'foo' => ( default => 'my_foo' );
    has_field 'bar' => ( type => 'Multiple' );

   sub options_bar {
       return (
           1   => 'one',
           2   => 'two',
           3   => 'three',
           4   => 'four',
       );
   }


}

$form = Test::Multiple::InitObject->new;
my $init_object = { foo => 'new_foo', bar => [3,4] };
$form->process(init_object => $init_object, params => {} );
my $rendered = $form->render;
like($rendered, qr/<option value="4" id="bar.1" selected="selected">four<\/option>/, 'rendered option');
my $value = $form->value;
is_deeply( $value, $init_object, 'correct value');

done_testing;