Debian Codegraph @

t/validation/types.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5
6 use HTML::FormHandler::Types (':all');
7
8 use HTML::FormHandler::I18N;
9 $ENV{LANGUAGE_HANDLE} = 'en_en';
10
11 {
12 package Test::Form;
13 use HTML::FormHandler::Moose;
14 extends 'HTML::FormHandler';
15 use HTML::FormHandler::Types (':all');
16 use Moose::Util::TypeConstraints;
17
18 subtype 'GreaterThan10'
19 => as 'Int'
20 => where { $_ > 10 }
21 => message { "This number ($_) is not greater than 10" };
22
23 has 'posint' => ( is => 'rw', isa => PositiveInt);
24 has_field 'test' => ( apply => [ PositiveInt ] );
25 has_field 'text_gt' => ( apply=> [ 'GreaterThan10' ] );
26 has_field 'text_both' => ( apply => [ PositiveInt, 'GreaterThan10' ] );
27 has_field 'field_wtype' => ( apply => [
28 { type => PositiveInt, message => 'Not a positive number' } ] );
29 has_field 'state' => ( apply => [ State ] );
30
31 }
32
33 my $form = Test::Form->new;
34
35 ok( $form, 'get form');
36 $form->posint(100);
37
38 my $params = {
39 test => '-100',
40 text_gt => 5,
41 text_both => 6,
42 state => 'GG',
43 field_wtype => '-10',
44 };
45
46 $form->process($params);
47 ok( !$form->validated, 'form did not validate' );
48 ok( $form->field('test')->has_errors, 'errors on MooseX type');
49 ok( $form->field('text_gt')->has_errors, 'errors on subtype');
50 ok( $form->field('text_both')->has_errors, 'errors on both');
51 ok( $form->field('field_wtype')->has_errors, 'errors on type with message');
52 ok( $form->field('state')->has_errors, 'errors on state' );
53
54 $params = {
55 test => 100,
56 text_gt => 21,
57 text_both => 15,
58 state => 'NY',
59 };
60
61 $form->process($params);
62 ok( $form->validated, 'form validated' );
63 ok( !$form->field('test')->has_errors, 'no errors on MooseX type');
64 ok( !$form->field('text_gt')->has_errors, 'no errors on subtype');
65 ok( !$form->field('text_both')->has_errors, 'no errors on both');
66 ok( !$form->field('state')->has_errors, 'no errors on state' );
67
68 $params = {
69 test => 100,
70 text_gt => 21,
71 text_both => 15,
72 state => [ qw(NY IL) ],
73 };
74
75 $form->process($params);
76 ok( !$form->validated, 'form did not validate' );
77 ok( !$form->field('test')->has_errors, 'no errors on MooseX type');
78 ok( !$form->field('text_gt')->has_errors, 'no errors on subtype');
79 ok( !$form->field('text_both')->has_errors, 'no errors on both');
80 ok( $form->field('state')->has_errors, 'errors on state' );
81
82 # State
83 my $field = HTML::FormHandler::Field->new( name => 'Test1', apply => [ State ] );
84 $field->build_result;
85 ok( $field, 'created field with type' );
86 $field->_set_input('GG');
87 ok( !$field->validate_field, 'field did not validate');
88 is( $field->errors->[0], 'Not a valid state', 'correct error message for State' );
89 $field->_set_input('NY');
90 ok( $field->validate_field, 'state field validated');
91 # Email
92 $field = HTML::FormHandler::Field->new( name => 'Test', apply => [ Email ] );
93 $field->build_result;
94 $field->_set_input('[email protected]');
95 ok( $field->validate_field, 'email field validated' );
96 ok( !$field->has_errors, 'email field is valid');
97 $field->_set_input('not_an_email');
98 $field->validate_field;
99 is( $field->errors->[0], 'Email is not valid', 'error from Email' );
100
101 my @test = (
102 IPAddress => \&IPAddress =>
103 [qw(0.0.0.0 01.001.0.00 198.168.0.101 255.255.255.255)],
104 [qw(1 2.33 4.56.789 198.300.0.101 0.-1.13.255)],
105 'Not a valid IP address',
106 NoSpaces => \&NoSpaces =>
107 [qw(a 1 _+~ *), '#'], ['a b', "x\ny", "foo\tbar"],
108 'Must not contain spaces',
109 WordChars => \&WordChars =>
110 [qw(abc 8 ___ 90_i 0)],
111 ['a b', "x\ny", "foo\tbar", 'c++', 'C#', '$1,000,000'],
112 'Must be made up of letters, digits, and underscores',
113 NotAllDigits => \&NotAllDigits =>
114 [qw(a 1a . a=1 1.23), 'a 1'], [qw(0 1 12 03450)],
115 'Must not be all digits',
116 # does not work at all!!!
117 # Printable => \&Printable =>
118 # [qw(a 1 $ % *), '# ?'], [0x00, "foo\tbar", "x\ny"],
119 # 'Field contains non-printable characters',
120 SingleWord => \&SingleWord =>
121 [qw(a 1a _ a_1 1_234)], ['a b', '1.23', 'a=1'],
122 'Field must contain a single word',
123 );
124
125 while (my ($name, $type, $good, $bad, $error_msg) = splice @test, 0, 5) {
126 $field = HTML::FormHandler::Field->new(name => 'Test', apply => [&$type]);
127 $field->build_result;
128 for (@$good) {
129 $field->_set_input($_);
130 ok($field->validate_field, "$name validated");
131 ok(!$field->has_errors, "$name field is valid");
132 }
133 for (@$bad) {
134 $field->_set_input($_);
135 ok(!$field->validate_field, "$name validation failed");
136 is($field->errors->[0], $error_msg, "error from $name");
137 }
138 }
139
140 @test = (
141 Lower => \&Lower =>
142 [A => 'a', AB => 'ab', Abc => 'abc', abc => 'abc', 'A-z' => 'a-z', '1 + X' => '1 + x'],
143 Upper => \&Upper =>
144 [a => 'A', ab => 'AB', Abc => 'ABC', ABC => 'ABC', 'A-z' => 'A-Z', '1 + x' => '1 + X'],
145 );
146
147 while (my ($name, $type, $trans) = splice @test, 0, 3) {
148 my @trans = @$trans;
149 $field = HTML::FormHandler::Field->new(name => 'Test', apply => [&$type]);
150 $field->build_result;
151 while (my ($from, $to) = splice @trans, 0, 2) {
152 $field->_set_input($from);
153 ok($field->validate_field, "$name validated");
154 is($field->value, $to , "$name field transformation");
155 }
156 }
157
158 SKIP: {
159 eval { require Type::Tiny };
160
161 skip "Type::Tiny not installed", 9 if $@;
162
163 {
164 package Test::Form::Type::Tiny;
165
166 use HTML::FormHandler::Moose;
167 extends 'HTML::FormHandler';
168
169 my $NUM = Type::Tiny->new(
170 name => "Number",
171 constraint => sub { $_ =~ /^\d+$/ },
172 message => sub { "$_ ain't a number" },
173 );
174
175 has_field 'test_a' => ( apply => [ $NUM ] );
176 has_field 'test_b' => ( apply => [ { type => $NUM } ] );
177 }
178
179 my $form = Test::Form::Type::Tiny->new;
180
181 ok($form, 'get form');
182
183 my $params = {
184 test_a => 'str1',
185 test_b => 'str2',
186 };
187 $form->process($params);
188 ok( !$form->validated, 'form did not validate' );
189 ok( $form->field('test_a')->has_errors, 'errors on Type::Tiny type');
190 ok( $form->field('test_b')->has_errors, 'errors on Type::Tiny type');
191 is( $form->field('test_a')->errors->[0], "str1 ain't a number", 'error from Type::Tiny' );
192 is( $form->field('test_b')->errors->[0], "str2 ain't a number", 'error from Type::Tiny' );
193
194 $params = {
195 test_a => '123',
196 test_b => '456',
197 };
198 $form->process($params);
199 ok( $form->validated, 'form validated' );
200 ok( !$form->field('test_a')->has_errors, 'no errors on Type::Tiny type');
201 ok( !$form->field('test_b')->has_errors, 'no errors on Type::Tiny type');
202 }
203
204 done_testing;