Design/Util/ValidateForm.pm untested

filter: Statementstmt 0.0% 0 / 235 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 363 CC 25 SCAR 35.4
CC
25
Cov
0%
process_flower_images
56.0
process_flower_urls
56.0
flower_details
42.0
CRAP 34.5
1 package WWW::Garden::Design::Util::ValidateForm;
2  
3 use strict;
4 use warnings;
5  
6 use boolean;
7  
8 use Data::Dumper::Concise; # For Dumper().
9  
10 use MojoX::Validate::Util;
11  
12 use Moo;
13  
14 use Types::Standard qw/Object/;
15  
16 has validator =>
17 (
18 default => sub{return MojoX::Validate::Util -> new},
19 is => 'ro',
20 isa => Object,
21 required => 0,
22 );
23  
24 our $VERSION = '0.97';
25  
26 # -----------------------------------------------
27  
28 sub design_details
29 {
30 my($self, $controller, $defaults) = @_;
31 my($app) = $controller -> app;
32 my($joiner) = $$defaults{joiner};
33 my($params) = $controller -> req -> params -> to_hash;
34 $$params{errors} = {};
35 $$params{message} = '';
36 $$params{success} = false;
37  
38 $app -> log -> debug("$_ => $$params{$_}") for sort keys %$params;
39  
40 # %errors is declared at this level so various methods can store into it.
41  
42 my(%errors);
43  
44 $self -> process_design_property($app, $defaults, \%errors, $joiner, $params);
45  
46 ###############################
47 $$params{success} = true; # TODO.
48 ###############################
49  
50 return $params;
51  
52 } # End of design_details.
53  
54 # -----------------------------------------------
55  
56 sub flower_details
57 {
58 my($self, $controller, $defaults) = @_;
59 my($app) = $controller -> app;
60 my($params) = $controller -> req -> params -> to_hash;
61 $$params{errors} = {};
62 $$params{message} = '';
63 $$params{success} = false;
64  
65 $app -> log -> debug("$_ => $$params{$_}") for sort keys %$params;
66 $app -> log -> debug('CSRF. session' . $controller -> session('csrf_token') . ". params: $$params{csrf_token}");
67  
68 # %errors is declared at this level so various methods can store into it.
69  
70 my(%errors);
71  
72 if ($$params{common_name} && $$params{scientific_name})
73 {
74 my($csrf_ok) = $controller -> session('csrf_token') eq $$params{csrf_token} ? 1 : 0;
75 my($joiner) = $$defaults{joiner};
76  
77 $self -> process_flower_attributes($app, $defaults, \%errors, $joiner, $params);
78 $self -> process_flower_dimensions($app, $defaults, \%errors, $params);
79 $self -> process_flower_images($app, $defaults, \%errors, $joiner, $params);
80 $self -> process_flower_notes($app, $defaults, \%errors, $joiner, $params);
81 $self -> validator -> check_member($params, 'publish', ['Yes', 'No']);
82 $self -> process_flower_urls($app, $defaults, \%errors, $joiner, $params);
83  
84 if ($csrf_ok == 1)
85 {
86 $app -> log -> debug('Validated params: ' . Dumper($self -> validator -> validation -> output) );
87  
88 my(@args, $args);
89 my($result);
90 my($suffix);
91 my($test);
92  
93 # Warning: Inside this loop, don't use $$params{$name} because of cases like $$params{url_list},
94 # which splits into url_1, url_2, etc. Here, $name assumes these latter values, which in turn
95 # means $$params{url_list} is defined but $$params{url_1} etc are all undef!
96  
97 for my $name (@{$self -> validator -> validation -> failed})
98 {
99 ($test, $result, @args) = @{$self -> validator -> validation -> error($name)};
100 $args = ($#args >= 0) ? join(', ', @args) : '';
101 $errors{$name} = [$$params{$name}, $test, $args];
102 }
103  
104 if (scalar keys %errors == 0)
105 {
106 $$params{message} = 'All fields were validated successfully';
107 $$params{success} = true;
108 }
109 else
110 {
111 $$params{errors} = \%errors;
112 $$params{message} = 'These fields failed validation';
113 }
114 }
115 else
116 {
117 $$params{message} = 'Detected apparent CSRF activity';
118 }
119 }
120 else
121 {
122 $$params{message} = 'Missing common name or scientific name';
123 }
124  
125 return $params;
126  
127 } # End of flower_details.
128  
129 # -----------------------------------------------
130  
131 sub process_design_property
132 {
133 my($self, $app, $defaults, $errors, $joiner, $params) = @_;
134  
135 $app -> log -> debug('ValidateForm.process_design_property(...)');
136  
137 for my $index_name (qw/garden_index property_index/)
138 {
139 $self -> validator -> check_ascii_digits($params, $index_name);
140 }
141  
142 for my $param_name (qw/garden_name property_name/)
143 {
144 $self -> validator -> check_required($params, $param_name);
145 }
146  
147 } # End of process_design_property.
148  
149 # -----------------------------------------------
150  
151 sub process_flower_attributes
152 {
153 my($self, $app, $defaults, $errors, $joiner, $params) = @_;
154 my($attribute_list) = $$params{attribute_list};
155 my(@attributes) = split(/$joiner/, $attribute_list);
156 my($attributes) = {};
157  
158 $app -> log -> debug('ValidateForm.process_flower_attributes(...)');
159  
160 my($key);
161  
162 for (my($i) = 0; $i < $#attributes; $i += 2)
163 {
164 $key = $attributes[$i] =~ s/_/ /gr;
165 $$attributes{$key} = [] if (! $$attributes{$key});
166  
167 push @{$$attributes{$key} }, $attributes[$i + 1];
168 }
169  
170 my($field);
171 my($temp_name);
172 my($validated);
173  
174 for $key (keys %$attributes)
175 {
176 # 1: Test the name of the attribute.
177  
178 $temp_name = "attribute_$key";
179  
180 $self -> validator -> check_member({$temp_name => $key}, $temp_name, $$defaults{attribute_type_names});
181  
182 # 2: If that test worked, test all values of the attribute.
183  
184 $validated = $self -> validator -> validation -> output;
185  
186 if (exists $$validated{$temp_name})
187 {
188 for $field (@{$$attributes{$key} })
189 {
190 $temp_name = "attribute_${key}_$field";
191  
192 $self -> validator -> check_member({$temp_name => $field}, $temp_name, $$defaults{attribute_type_fields}{$key});
193 }
194 }
195 }
196  
197 } # End of process_flower_attributes.
198  
199 # -----------------------------------------------
200  
201 sub process_flower_dimensions
202 {
203 my($self, $app, $defaults, $errors, $params) = @_;
204 my($height) = $$params{height};
205 my($width) = $$params{width};
206  
207 $app -> log -> debug("ValidateForm.process_flower_dimensions(height: $height, width: $width)");
208 $self -> validator -> check_dimension({height => $height}, 'height', ['cm', 'm']);
209 $self -> validator -> check_dimension({width => $width}, 'width', ['cm', 'm']);
210  
211 } # End of process_flower_dimensions.
212  
213 # -----------------------------------------------
214  
215 sub process_flower_images
216 {
217 my($self, $app, $defaults, $errors, $joiner, $params) = @_;
218 my($image_list) = $$params{image_list};
219 my(@images) = map{defined($_) ? $_ : ''} split(/$joiner/, $image_list);
220  
221 $app -> log -> debug('ValidateForm.process_flower_images(...)');
222  
223 # I'm currently accepting duplicate file names and duplicate descriptions.
224 # Also, accept empty image description without generating an error msg.
225 #
226 # Expected format of @images:
227 # o [$i]: A string id of the form 'image_\d+'.
228 # o [$i + 1]: The image's name.
229 # o [$i + 2]: The image's description.
230  
231 my(@id_length) = (7, 8); # (Min, Max).
232 my($id_message) = "$id_length[0] .. $id_length[1] chars";
233 my($prefix_length) = 20;
234 my(@image_length) = (4, 250); # (Min, Max).
235 my($image_message) = "$image_length[0] .. $image_length[1] chars";
236  
237 my($id);
238 my($image_length);
239  
240 for (my($i) = 0; $i < $#images; $i += 3)
241 {
242 $image_length = length($images[$i]);
243  
244 # Ignore empty id without generating an error msg.
245  
246 next if ($image_length == 0);
247  
248 if ( ($image_length < $id_length[0]) || ($image_length > $id_length[1]) || ($images[$i] !~ /^image_([0-9]{1,2})/) )
249 {
250 $$errors{'image_id'} = [substr($images[$i], 0, $prefix_length), 'length', $id_message];
251  
252 next;
253 }
254  
255 $id = $1;
256 $image_length = length($images[$i + 1]);
257  
258 # Ignore empty image file name without generating an error msg.
259  
260 next if ($image_length == 0);
261  
262 $image_length = length($images[$i + 2]);
263  
264 next if ( (length($images[$i + 1]) > $image_length[1]) || ($image_length > $image_length[1]) );
265  
266 if ( ($id >= 1) && ($id <= $$defaults{constants_table}{max_image_count}) )
267 {
268 # We put the individual notes back into %$params for display if necessary (e.g. as errors).
269  
270 $$params{$images[$i]} = $images[$i + 1];
271  
272 $self -> validator -> check_required({$images[$i] => "$images[$i + 1]$joiner$images[$i + 2]"}, $images[$i]);
273 }
274 else
275 {
276 $$errors{'image_id'} = [substr($images[$i], 0, $prefix_length), 'length', $id_message];
277 }
278 }
279  
280 } # End of process_flower_images.
281  
282 # -----------------------------------------------
283  
284 sub process_flower_notes
285 {
286 my($self, $app, $defaults, $errors, $joiner, $params) = @_;
287 my($note_list) = $$params{note_list};
288 my(@notes) = map{defined($_) ? $_ : ''} split(/$joiner/, $note_list);
289  
290 $app -> log -> debug('ValidateForm.process_flower_notes(...)');
291  
292 # I'm currently accepting duplicate notes.
293 # Also, ignore empty note without generating an error msg.
294 #
295 # Expected format of @notes:
296 # o [$i]: A string id of the form 'note_\d+'.
297 # o [$i + 1]: The note's text.
298  
299 my(@id_length) = (6, 7); # (Min, Max).
300 my($id_message) = "$id_length[0] .. $id_length[1] chars";
301 my($prefix_length) = 20;
302 my(@note_length) = (0, 250); # (Min, Max).
303 my($note_message) = "$note_length[0] .. $note_length[1] chars";
304  
305 my($id);
306 my($note_length);
307  
308 for (my($i) = 0; $i < $#notes; $i += 2)
309 {
310 $note_length = length($notes[$i]);
311  
312 # Ignore empty id without generating an error msg.
313  
314 next if ($note_length == 0);
315  
316 if ( ($note_length < $id_length[0]) || ($note_length > $id_length[1]) || ($notes[$i] !~ /^note_([0-9]{1,2})/) )
317 {
318 $$errors{'note_id'} = [substr($notes[$i], 0, $prefix_length), 'length', $id_message];
319  
320 next;
321 }
322  
323 $id = $1;
324 $note_length = length($notes[$i + 1]);
325  
326 if ($note_length > $note_length[1])
327 {
328 $$errors{$notes[$i]} = [substr($notes[$i + 1], 0, $id_length[1]), 'length', $note_message];
329  
330 next;
331 }
332  
333 if ( ($id >= 1) && ($id <= $$defaults{constants_table}{max_note_count}) )
334 {
335 # We put the individual notes back into %$params for display if necessary (e.g. as errors).
336  
337 $$params{$notes[$i]} = $notes[$i + 1];
338  
339 $self -> validator -> check_optional({$notes[$i] => $notes[$i + 1]}, $notes[$i]);
340 }
341 else
342 {
343 $$errors{'note_id'} = [substr($notes[$i], 0, $prefix_length), 'length', $id_message];
344 }
345 }
346  
347 } # End of process_flower_notes.
348  
349 # -----------------------------------------------
350  
351 sub process_flower_urls
352 {
353 my($self, $app, $defaults, $errors, $joiner, $params) = @_;
354 my($url_list) = $$params{url_list};
355 my(@urls) = map{defined($_) ? $_ : ''} split(/$joiner/, $url_list);
356  
357 $app -> log -> debug('ValidateForm.process_flower_urls(...)');
358  
359 # Expected format of @urls:
360 # o [$i]: A string id of the form 'url_\d+'.
361 # o [$i + 1]: The url itself.
362  
363 my(@id_length) = (5, 6); # (Min, Max).
364 my($id_message) = "$id_length[0] .. $id_length[1] chars";
365 my($prefix_length) = 20;
366 my(@url_length) = (5, 120); # (Min, Max).
367 my($url_message) = "$url_length[0] .. $url_length[1] chars";
368  
369 my($id);
370 my($url_length);
371  
372 for (my($i) = 0; $i < $#urls; $i += 2)
373 {
374 $url_length = length($urls[$i]);
375  
376 # Ignore empty id without generating an error msg.
377  
378 next if ($url_length == 0);
379  
380 if ( ($url_length < $id_length[0]) || ($url_length > $id_length[1]) || ($urls[$i] !~ /^url_([0-9]{1,2})/) )
381 {
382 $$errors{'url_id'} = [substr($urls[$i], 0, $prefix_length), 'length', $id_message];
383  
384 next;
385 }
386  
387 $id = $1;
388 $url_length = length($urls[$i + 1]);
389  
390 # Ignore empty url without generating an error msg.
391  
392 next if ($url_length == 0);
393  
394 if ( ($url_length < $url_length[0]) || ($url_length > $url_length[1]) )
395 {
396 $$errors{$urls[$i]} = [substr($urls[$i + 1], 0, $id_length[1]), 'length', $url_message];
397  
398 next;
399 }
400  
401 if ( ($id >= 1) && ($id <= $$defaults{constants_table}{max_url_count}) )
402 {
403 # We put the individual urls back into %$params for display if necessary (e.g. as errors).
404  
405 $$params{$urls[$i]} = $urls[$i + 1];
406  
407 $self -> validator -> check_url({$urls[$i] => $urls[$i + 1]}, $urls[$i]);
408 }
409 else
410 {
411 $$errors{'url_id'} = [substr($urls[$i], 0, $prefix_length), 'length', $id_message];
412 }
413 }
414  
415 } # End of process_flower_urls.
416  
417 # -----------------------------------------------
418  
419 1;
420  
421 =pod
422  
423 =head1 NAME
424  
425 WWW::Garden::Design::Util::ValidateForm - Manage the 'flowers' database
426  
427 =head1 Machine-Readable Change Log
428  
429 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
430  
431 =head1 Version Numbers
432  
433 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
434  
435 =head1 Repository
436  
437 L<https://github.com/ronsavage/WWW-Garden-Design>
438  
439 =head1 Support
440  
441 Email the author, or log a bug on RT:
442  
443 L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>.
444  
445 =head1 Author
446  
447 L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014.
448  
449 My homepage: L<https://savage.net.au/>.
450  
451 =head1 Copyright
452  
453 Australian copyright (c) 2018, Ron Savage.
454  
455 All Programs of mine are 'OSI Certified Open Source Software';
456 you can redistribute them and/or modify them under the terms of
457 The Perl License, a copy of which is available at:
458 http://dev.perl.org/licenses/
459  
460 =cut