Debian Codegraph @

t/bootstrap/controls.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use HTML::FormHandler::Test;
5
6 {
7 package MyApp::Form::Controls::Theme;
8 use Moose::Role;
9
10 # widget wrapper must be set before the fields are built in BUILD
11 sub before_build {
12 my $self = shift;
13 $self->set_widget_wrapper('Bootstrap');
14 }
15 sub build_do_form_wrapper {1}
16 sub build_form_element_class {['form-horizontal']}
17 sub build_form_wrapper_class {['span8']}
18 sub build_form_tags {{
19 wrapper_tag => 'div',
20 after_start => '<fieldset><legend>Controls Bootstrap supports</legend>',
21 before_end => '</fieldset>',
22 }}
23
24 sub build_update_subfields {{
25 input01 => { tags => { after_element => '<p class="help-block">In addition to freeform text, any HTML5 text-based input appears like so.</p>' },
26 element_attr => { class => 'input-xlarge' },
27 },
28 optionsCheckbox => { option_label => 'Option one is this and that—be sure to include why it’s great' },
29 fileInput => { element_attr => { class => 'input-file' } },
30 textarea => { element_attr => { class => 'input-xlarge' } },
31 actions => { element_attr => { class => 'form-actions' } },
32 }}
33 }
34
35 {
36 package MyApp::Form::Component::Actions;
37 use HTML::FormHandler::Moose::Role;
38
39 has_field 'actions' => ( type => 'Compound', order => 99, widget_wrapper => 'Simple',
40 do_wrapper => 1,
41 do_label => 0, wrapper_attr => { class => 'form-actions' } );
42 has_field 'actions.save' => ( type => 'Submit', widget => 'ButtonTag',
43 element_attr => { class => ['btn', 'btn-primary'] },
44 widget_wrapper => 'None', value => "Save changes" );
45 has_field 'actions.cancel' => ( type => 'Reset', widget => 'ButtonTag',
46 element_attr => { class => ['btn'] },
47 widget_wrapper => 'None', value => "Cancel" );
48 }
49
50 {
51 package MyApp::Form::Controls;
52 use HTML::FormHandler::Moose;
53 extends 'HTML::FormHandler';
54 with 'MyApp::Form::Controls::Theme';
55 with 'MyApp::Form::Component::Actions';
56
57 has '+name' => ( default => 'control_form' );
58 has_field 'input01' => ( type => 'Text', label => 'Text input' );
59 has_field 'optionsCheckbox' => ( type => 'Checkbox', checkbox_value => "option1", label => 'Checkbox' );
60 has_field 'select01' => ( type => 'Select', label => 'Select list' );
61 has_field 'multiSelect' => ( type => 'Multiple', label => 'Multi-select' );
62 has_field 'fileInput' => ( type => 'File', label => 'File input' );
63 has_field 'textarea' => ( type => 'TextArea', cols => 20, rows => 4 );
64 sub options_select01 {
65 return ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 );
66 }
67 sub options_multiSelect {
68 return ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 );
69 }
70 }
71
72 my $form = MyApp::Form::Controls->new;
73 $form->process;
74
75 my $rendered = $form->render_start;
76 my $expected = '<div class="span8"><form class="form-horizontal" id="control_form" method="post"><fieldset><legend>Controls Bootstrap supports</legend>';
77 is($rendered, $expected, 'form start rendered correctly');
78
79 # text field
80 $expected =
81 '<div class="control-group">
82 <label class="control-label" for="input01">Text input</label>
83 <div class="controls">
84 <input type="text" class="input-xlarge" id="input01" name="input01" value="" />
85 <p class="help-block">In addition to freeform text, any HTML5 text-based input appears like so.</p>
86 </div>
87 </div>';
88 $rendered = $form->field('input01')->render;
89 is_html($rendered, $expected, 'input01 field rendered correctly');
90
91 # checkbox
92 $expected =
93 '<div class="control-group">
94 <label class="control-label" for="optionsCheckbox">Checkbox</label>
95 <div class="controls">
96 <label class="checkbox" for="optionsCheckbox">
97 <input type="checkbox" id="optionsCheckbox" name="optionsCheckbox" value="option1" />
98 Option one is this and that&mdash;be sure to include why it’s great
99 </label>
100 </div>
101 </div>';
102 is( $form->field('optionsCheckbox')->widget_wrapper, 'Bootstrap', 'correct wrapper' );
103 $rendered = $form->field('optionsCheckbox')->render;
104 is_html($rendered, $expected, 'optionsCheckbox field rendered correctly');
105
106 # select
107 $expected =
108 '<div class="control-group">
109 <label class="control-label" for="select01">Select list</label>
110 <div class="controls">
111 <select id="select01" name="select01">
112 <option value="1" id="select01.0">1</option>
113 <option value="2" id="select01.1">2</option>
114 <option value="3" id="select01.2">3</option>
115 <option value="4" id="select01.3">4</option>
116 <option value="5" id="select01.4">5</option>
117 </select>
118 </div>
119 </div>';
120 $rendered = $form->field('select01')->render;
121 is_html($rendered, $expected, 'select01 renders ok' );
122
123 # multiple select
124 $expected =
125 '<div class="control-group">
126 <label class="control-label" for="multiSelect">Multi-select</label>
127 <div class="controls">
128 <select multiple="multiple" id="multiSelect" name="multiSelect" size="5">
129 <option value="1" id="multiSelect.0">1</option>
130 <option value="2" id="multiSelect.1">2</option>
131 <option value="3" id="multiSelect.2">3</option>
132 <option value="4" id="multiSelect.3">4</option>
133 <option value="5" id="multiSelect.4">5</option>
134 </select>
135 </div>
136 </div>';
137 $rendered = $form->field('multiSelect')->render;
138 is_html($rendered, $expected, 'multiSelect renders ok' );
139
140 # file
141 $expected =
142 '<div class="control-group">
143 <label class="control-label" for="fileInput">File input</label>
144 <div class="controls">
145 <input class="input-file" id="fileInput" name="fileInput" type="file" />
146 </div>
147 </div>';
148 $rendered = $form->field('fileInput')->render;
149 is_html($rendered, $expected, 'fileInput render ok' );
150
151 # textarea
152 $expected =
153 '<div class="control-group">
154 <label class="control-label" for="textarea">Textarea</label>
155 <div class="controls">
156 <textarea class="input-xlarge" id="textarea" name="textarea" cols="20" rows="4"></textarea>
157 </div>
158 </div>';
159 $rendered = $form->field('textarea')->render;
160 is_html($rendered, $expected, 'textarea renders ok' );
161
162 # actions
163 $expected =
164 '<div class="form-actions" id="actions">
165 <button id="actions.save" name="actions.save" type="submit" class="btn btn-primary">Save changes</button>
166 <button id="actions.cancel" name="actions.cancel" type="reset" class="btn">Cancel</button>
167 </div>';
168 $rendered = $form->field('actions')->render;
169 is_html($rendered, $expected, 'actions renders ok' );
170
171 $expected = '
172 <div class="span8">
173 <form class="form-horizontal" id="control_form" method="post">
174 <fieldset>
175 <legend>Controls Bootstrap supports</legend>
176 <div class="form_messages"></div>
177 <div class="control-group">
178 <label class="control-label" for="input01">Text input</label>
179 <div class="controls">
180 <input type="text" class="input-xlarge" id="input01" name="input01" value="" />
181 <p class="help-block">In addition to freeform text, any HTML5 text-based input appears like so.</p>
182 </div>
183 </div>
184 <div class="control-group">
185 <label class="control-label" for="optionsCheckbox">Checkbox</label>
186 <div class="controls">
187 <label class="checkbox" for="optionsCheckbox">
188 <input type="checkbox" id="optionsCheckbox" name="optionsCheckbox" value="option1" />
189 Option one is this and that&mdash;be sure to include why it’s great
190 </label>
191 </div>
192 </div>
193 <div class="control-group">
194 <label class="control-label" for="select01">Select list</label>
195 <div class="controls">
196 <select id="select01" name="select01">
197 <option value="1" id="select01.0">1</option>
198 <option value="2" id="select01.1">2</option>
199 <option value="3" id="select01.2">3</option>
200 <option value="4" id="select01.3">4</option>
201 <option value="5" id="select01.4">5</option>
202 </select>
203 </div>
204 </div>
205 <div class="control-group">
206 <label class="control-label" for="multiSelect">Multi-select</label>
207 <div class="controls">
208 <select multiple="multiple" id="multiSelect" name="multiSelect" size="5">
209 <option value="1" id="multiSelect.0">1</option>
210 <option value="2" id="multiSelect.1">2</option>
211 <option value="3" id="multiSelect.2">3</option>
212 <option value="4" id="multiSelect.3">4</option>
213 <option value="5" id="multiSelect.4">5</option>
214 </select>
215 </div>
216 </div>
217 <div class="control-group">
218 <label class="control-label" for="fileInput">File input</label>
219 <div class="controls">
220 <input class="input-file" id="fileInput" name="fileInput" type="file" />
221 </div>
222 </div>
223 <div class="control-group">
224 <label class="control-label" for="textarea">Textarea</label>
225 <div class="controls">
226 <textarea class="input-xlarge" id="textarea" name="textarea" cols="20" rows="4"></textarea>
227 </div>
228 </div>
229 <div class="form-actions" id="actions">
230 <button id="actions.save" name="actions.save" type="submit" class="btn btn-primary">Save changes</button>
231 <button id="actions.cancel" name="actions.cancel" type="reset" class="btn">Cancel</button>
232 </div>
233 </fieldset>
234 </form>
235 </div>';
236
237 $rendered = $form->render;
238 is_html( $rendered, $expected, 'form renders correctly' );
239
240 done_testing;