PageRenderTime 41ms CodeModel.GetById 8ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/t/render.t

http://github.com/gshank/html-formhandler
text | 250 lines | 210 code | 40 blank | 0 comment | 0 complexity | 1167828097a6d6f89a3accb298d6bbed MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More;
  4
  5use HTML::FormHandler::Field::Text;
  6
  7
  8{
  9   package Test::Form;
 10   use HTML::FormHandler::Moose;
 11   extends 'HTML::FormHandler';
 12   with 'HTML::FormHandler::Render::Simple';
 13
 14   has '+name' => ( default => 'testform' );
 15   has_field 'test_field' => (
 16               input_class => 'test123',
 17               size => 20,
 18               label => 'TEST',
 19               id    => 'f99',
 20            );
 21   has_field 'number';
 22   has_field 'fruit' => ( type => 'Select' );
 23   has_field 'cheese' => ( type => 'Select' );
 24   has_field 'vegetables' => ( type => 'Multiple' );
 25   has_field 'grains' => ( type => 'Multiple' );
 26   has_field 'opt_in' => ( type => 'Select', widget => 'radio_group',
 27      options => [{ value => 0, label => 'No'}, { value => 1, label => 'Yes'} ] );
 28   has_field 'starch' => ( type => 'Multiple', widget => 'checkbox_group',
 29      options => [{ value => 1, label => 'One'}, { value => 2, label => 'Two'},
 30                  { value => 3, label => 'Three' },
 31      ] );
 32   has_field 'active' => ( type => 'Checkbox' );
 33   has_field 'comments' => ( type => 'TextArea', cols => 40, rows => 3 );
 34   has_field 'hidden' => ( type => 'Hidden' );
 35   has_field 'selected' => ( type => 'Boolean' );
 36   has_field 'start_date' => ( type => 'DateTime' );
 37   has_field 'start_date.month' => ( type => 'Integer', range_start => 1,
 38       range_end => 12 );
 39   has_field 'start_date.day' => ( type => 'Integer', range_start => 1,
 40       range_end => 31 );
 41   has_field 'start_date.year' => ( type => 'Integer', range_start => 2000,
 42       range_end => 2020 );
 43
 44   has_field 'two_errors' => (
 45       apply => [
 46          { check   => [ ], message => 'First constraint error' },
 47          { check   => [ ], message => 'Second constraint error' }
 48       ]
 49   );
 50
 51   has_field 'submit' => ( type => 'Submit', value => 'Update' );
 52
 53   has '+dependency' => ( default => sub { [ ['start_date.month',
 54         'start_date.day', 'start_date.year'] ] } );
 55   has_field 'no_render' => ( widget => 'no_render' );
 56   sub options_fruit {
 57       return (
 58           1   => 'apples',
 59           2   => 'oranges',
 60           3   => 'kiwi',
 61       );
 62   }
 63
 64   sub options_vegetables {
 65       return (
 66           1   => 'lettuce',
 67           2   => 'broccoli',
 68           3   => 'carrots',
 69           4   => 'peas',
 70       );
 71   }
 72
 73   sub options_grains {
 74       return [
 75           { value => 1, label => 'maize', disabled => 0 },
 76           { value => 2, label => 'rice', disabled => 1 },
 77           { value => 3, label => 'wheat' },
 78       ];
 79   }
 80
 81   sub options_cheese {
 82       return [
 83           { value => 1, label => 'canastra', disabled => 0 },
 84           { value => 2, label => 'brie', disabled => 1 },
 85           { value => 3, label => 'gorgonzola' },
 86       ];
 87   }
 88}
 89
 90
 91my $form = Test::Form->new;
 92ok( $form, 'create form');
 93
 94my $params = {
 95   test_field => 'something',
 96   number => 0,
 97   fruit => 2,
 98   vegetables => [2,4],
 99   active => 'now',
100   comments => 'Four score and seven years ago...',
101   hidden => '1234',
102   selected => '1',
103   'start_date.month' => '7',
104   'start_date.day' => '14',
105   'start_date.year' => '2006',
106   two_errors => 'aaa',
107   opt_in => 0,
108};
109
110$form->process( $params );
111
112is_deeply( $form->field('starch')->input_without_param, [], 'checkbox group settings' );
113is( $form->field('starch')->not_nullable, 1, 'checkbox group settings' );
114is_deeply( $form->value->{starch}, [], 'checkbox group value' );
115
116is( $form->render_field( $form->field('number') ),
117    '
118<div><label class="label" for="number">Number: </label><input type="text" name="number" id="number" value="0" /></div>
119',
120    "value '0' is rendered"
121);
122
123my $output1 = $form->render_field( $form->field('test_field') );
124is( $output1,
125   '
126<div><label class="label" for="f99">TEST: </label><input type="text" name="test_field" id="f99" size="20" value="something" class="test123" /></div>
127',
128   'output from text field');
129
130my $output2 = $form->render_field( $form->field('fruit') );
131is( $output2,
132   '
133<div><label class="label" for="fruit">Fruit: </label><select name="fruit" id="fruit"><option value="1" id="fruit.0">apples</option><option value="2" id="fruit.1" selected="selected">oranges</option><option value="3" id="fruit.2">kiwi</option></select></div>
134',
135   'output from select field');
136
137my $output12 = $form->render_field( $form->field('cheese') );
138is( $output12,
139   '
140<div><label class="label" for="cheese">Cheese: </label><select name="cheese" id="cheese"><option value="1" id="cheese.0">canastra</option><option value="2" id="cheese.1" disabled="disabled">brie</option><option value="3" id="cheese.2">gorgonzola</option></select></div>
141',
142   'output from select field with disabled option');
143
144my $output3 = $form->render_field( $form->field('vegetables') );
145is( $output3,
146   '
147<div><label class="label" for="vegetables">Vegetables: </label><select name="vegetables" id="vegetables" multiple="multiple" size="5"><option value="1" id="vegetables.0">lettuce</option><option value="2" id="vegetables.1" selected="selected">broccoli</option><option value="3" id="vegetables.2">carrots</option><option value="4" id="vegetables.3" selected="selected">peas</option></select></div>
148',
149'output from select multiple field');
150
151my $output13 = $form->render_field( $form->field('grains') );
152is( $output13,
153   '
154<div><label class="label" for="grains">Grains: </label><select name="grains" id="grains" multiple="multiple" size="5"><option value="1" id="grains.0">maize</option><option value="2" id="grains.1" disabled="disabled">rice</option><option value="3" id="grains.2">wheat</option></select></div>
155',
156'output from select multiple field with disabled option');
157
158my $output4 = $form->render_field( $form->field('active') );
159is( $output4,
160   '
161<div><label class="label" for="active">Active: </label><input type="checkbox" name="active" id="active" value="1" /></div>
162',
163   'output from checkbox field');
164
165my $output5 = $form->render_field( $form->field('comments') );
166is( $output5,
167   '
168<div><label class="label" for="comments">Comments: </label><textarea name="comments" id="comments" rows="3" cols="40">Four score and seven years ago...</textarea></div>
169',
170   'output from textarea' );
171
172my $output6 = $form->render_field( $form->field('hidden') );
173is( $output6,
174   '
175<div><input type="hidden" name="hidden" id="hidden" value="1234" /></div>
176',
177   'output from hidden field' );
178
179my $output7 = $form->render_field( $form->field('selected') );
180is( $output7,
181   '
182<div><label class="label" for="selected">Selected: </label><input type="checkbox" name="selected" id="selected" value="1" checked="checked" /></div>
183',
184   'output from boolean' );
185
186my $output8 = $form->render_field( $form->field('start_date') );
187is( $output8,
188   '
189<div><fieldset class="start_date"><legend>Start date</legend>
190<div><label class="label" for="start_date.month">Month: </label><input type="text" name="start_date.month" id="start_date.month" size="8" value="7" /></div>
191
192<div><label class="label" for="start_date.day">Day: </label><input type="text" name="start_date.day" id="start_date.day" size="8" value="14" /></div>
193
194<div><label class="label" for="start_date.year">Year: </label><input type="text" name="start_date.year" id="start_date.year" size="8" value="2006" /></div>
195</fieldset></div>
196',
197   'output from DateTime' );
198
199my $output9 = $form->render_field( $form->field('submit') );
200is( $output9, '
201<div><input type="submit" name="submit" id="submit" value="Update" /></div>
202', 'output from Submit');
203
204my $output10 = $form->render_field( $form->field('opt_in') );
205is( $output10, '
206<div><label class="label" for="opt_in">Opt in: </label> <br /><label for="opt_in.0"><input type="radio" value="0" name="opt_in" id="opt_in.0" checked="checked" />No</label><br /><label for="opt_in.1"><input type="radio" value="1" name="opt_in" id="opt_in.1" />Yes</label><br /></div>
207', 'output from radio group' );
208
209my $output11 = $form->render_start;
210is( $output11,'<form id="testform" method="post" >
211<fieldset class="main_fieldset">', 'Form start OK' );
212
213my $output = $form->render;
214ok( $output, 'get rendered output from form');
215
216is( $form->render_field( $form->field('no_render')), '', 'no_render' );
217
218{
219    package Test::Field::Rendering;
220    use HTML::FormHandler::Moose;
221    extends 'HTML::FormHandler';
222
223    has_field 'my_html' => ( type => 'Display', html => '<h2>You got here!</h2>' );
224    has_field 'explanation' => ( type => 'Display' );
225    has_field 'between' => ( type => 'Display', set_html => 'between_html' );
226    has_field 'nolabel' => ( type => 'Text', no_render_label => 1 );
227
228    sub html_explanation {
229       my ( $self, $field ) = @_;
230       return "<p>I have an explanation somewhere around here...</p>";
231    }
232
233    sub between_html {
234        my ( $self, $field ) = @_;
235        return "<div>Somewhere, over the rainbow...</div>";
236    }
237
238}
239
240$form = Test::Field::Rendering->new;
241is( $form->field('my_html')->render, '<h2>You got here!</h2>', 'display field renders' );
242is( $form->field('explanation')->render, '<p>I have an explanation somewhere around here...</p>',
243    'display field renders with form method' );
244is( $form->field('between')->render, '<div>Somewhere, over the rainbow...</div>',
245    'set_html field renders' );
246is( $form->field('nolabel')->render, '
247<div><input type="text" name="nolabel" id="nolabel" value="" /></div>
248', 'no_render_label works');
249
250done_testing;