| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Test::Differences; |
| 5 |
use HTML::FormHandler::Test; |
| 6 |
|
| 7 |
use_ok('HTML::FormHandler::Result'); |
| 8 |
|
| 9 |
{ |
| 10 |
package Test::Form; |
| 11 |
use HTML::FormHandler::Moose; |
| 12 |
extends 'HTML::FormHandler'; |
| 13 |
|
| 14 |
has '+widget_form' => ( default => 'Simple' ); |
| 15 |
has '+name' => ( default => 'testform' ); |
| 16 |
has_field 'test_field' => ( |
| 17 |
size => 20, |
| 18 |
label => 'TEST', |
| 19 |
id => 'f99', |
| 20 |
); |
| 21 |
has_field 'number'; |
| 22 |
has_field 'fruit' => ( type => 'Select' ); |
| 23 |
has_field 'vegetables' => ( type => 'Multiple' ); |
| 24 |
has_field 'opt_in' => ( type => 'Select', widget => 'RadioGroup', |
| 25 |
options => [{ value => 0, label => 'No'}, { value => 1, label => 'Yes'} ] ); |
| 26 |
has_field 'active' => ( type => 'Checkbox' ); |
| 27 |
has_field 'comments' => ( type => 'TextArea' ); |
| 28 |
has_field 'hidden' => ( type => 'Hidden' ); |
| 29 |
has_field 'selected' => ( type => 'Boolean' ); |
| 30 |
has_field 'start_date' => ( type => 'DateTime' ); |
| 31 |
has_field 'start_date.month' => ( type => 'Integer', range_start => 1, |
| 32 |
range_end => 12 ); |
| 33 |
has_field 'start_date.day' => ( type => 'Integer', range_start => 1, |
| 34 |
range_end => 31 ); |
| 35 |
has_field 'start_date.year' => ( type => 'Integer', range_start => 2000, |
| 36 |
range_end => 2020 ); |
| 37 |
|
| 38 |
has_field 'two_errors' => ( |
| 39 |
apply => [ |
| 40 |
{ check => [ ], message => 'First constraint error' }, |
| 41 |
{ check => [ ], message => 'Second constraint error' } |
| 42 |
] |
| 43 |
); |
| 44 |
|
| 45 |
has_field 'submit' => ( type => 'Submit', value => 'Update' ); |
| 46 |
|
| 47 |
has '+dependency' => ( default => sub { [ ['start_date.month', |
| 48 |
'start_date.day', 'start_date.year'] ] } ); |
| 49 |
has_field 'no_render' => ( widget => 'NoRender' ); |
| 50 |
sub options_fruit { |
| 51 |
return ( |
| 52 |
1 => 'apples', |
| 53 |
2 => 'oranges', |
| 54 |
3 => 'kiwi', |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
sub options_vegetables { |
| 59 |
return ( |
| 60 |
1 => 'lettuce', |
| 61 |
2 => 'broccoli', |
| 62 |
3 => 'carrots', |
| 63 |
4 => 'peas', |
| 64 |
); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
my $form = Test::Form->new; |
| 69 |
ok( $form, 'create form'); |
| 70 |
|
| 71 |
|
| 72 |
my $params1 = { |
| 73 |
test_field => 'something', |
| 74 |
number => 0, |
| 75 |
fruit => 2, |
| 76 |
vegetables => [2,4], |
| 77 |
active => 'now', |
| 78 |
comments => 'Four score and seven years ago...', |
| 79 |
hidden => '1234', |
| 80 |
selected => '1', |
| 81 |
'start_date.month' => '7', |
| 82 |
'start_date.day' => '14', |
| 83 |
'start_date.year' => '2006', |
| 84 |
two_errors => 'aaa', |
| 85 |
opt_in => 0, |
| 86 |
}; |
| 87 |
|
| 88 |
$form->process( $params1 ); |
| 89 |
|
| 90 |
my $outputf = $form->render; |
| 91 |
ok( $outputf, 'get rendered output from form'); |
| 92 |
|
| 93 |
my $result1 = $form->result; |
| 94 |
ok( $result1, 'get result' ); |
| 95 |
|
| 96 |
my $outputr = $result1->render; |
| 97 |
ok( $outputr, 'get render from result'); |
| 98 |
|
| 99 |
is_html( $outputf, $outputr, 'no diff form and result'); |
| 100 |
|
| 101 |
my $params2 = { |
| 102 |
test_field => 'anything', |
| 103 |
number => 2, |
| 104 |
fruit => 3, |
| 105 |
vegetables => [2,4], |
| 106 |
active => 'now', |
| 107 |
comments => 'Four centuries and seven years ago...', |
| 108 |
hidden => '5678', |
| 109 |
selected => '0', |
| 110 |
'start_date.month' => '9', |
| 111 |
'start_date.day' => '14', |
| 112 |
'start_date.year' => '2008', |
| 113 |
two_errors => 'aaa', |
| 114 |
opt_in => 1, |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
my $result2 = $form->run($params2); |
| 119 |
my $outputr2 = $result1->render; |
| 120 |
is_html( $outputr, $outputr2, 'no diff second execution'); |
| 121 |
|
| 122 |
#$outputr > io('first_result.txt'); |
| 123 |
#$outputr2 > io('second_result.txt'); |
| 124 |
#my $diff = `diff first_result.txt second_result.txt`; |
| 125 |
#diag( $diff ); |
| 126 |
|
| 127 |
$form->process($params1); |
| 128 |
|
| 129 |
is_html( $result1->field('test_field')->render, $form->field('test_field')->render, 'no diff test_field' ); |
| 130 |
is_html( $result1->field('number')->render, $form->field('number')->render, 'no diff number' ); |
| 131 |
is_html( $result1->field('fruit')->render, $form->field('fruit')->render, 'no diff fruit' ); |
| 132 |
is_html( $result1->field('vegetables')->render, $form->field('vegetables')->render, 'no diff vegetables' ); |
| 133 |
is_html( $result1->field('active')->render, $form->field('active')->render, 'no diff active' ); |
| 134 |
is_html( $result1->field('comments')->render, $form->field('comments')->render, 'no diff comments' ); |
| 135 |
is_html( $result1->field('hidden')->render, $form->field('hidden')->render, 'no diff hidden' ); |
| 136 |
is_html( $result1->field('selected')->render, $form->field('selected')->render, 'no diff selected' ); |
| 137 |
is_html( $result1->field('start_date.month')->render, $form->field('start_date.month')->render, 'no diff start_date.month' ); |
| 138 |
is_html( $result1->field('start_date.day')->render, $form->field('start_date.day')->render, 'no diff start_date.day' ); |
| 139 |
is_html( $result1->field('start_date.year')->render, $form->field('start_date.year')->render, 'no diff start_date.year' ); |
| 140 |
is_html( $result1->field('two_errors')->render, $form->field('two_errors')->render, 'no diff two_errors' ); |
| 141 |
is_html( $result1->field('opt_in')->render, $form->field('opt_in')->render, 'no diff opt_in' ); |
| 142 |
|
| 143 |
|
| 144 |
done_testing; |