1#!/usr/bin/perl -w
2
3use strict;
4
5use Test::More tests => 68;
6
7BEGIN
8{
9  use_ok('Rose::HTML::Form::Field::Checkbox');
10  use_ok('Rose::HTML::Form::Field::CheckboxGroup');
11}
12
13my $field = Rose::HTML::Form::Field::CheckboxGroup->new(name => 'fruits');
14
15ok(ref $field eq 'Rose::HTML::Form::Field::CheckboxGroup', 'new()');
16
17is(scalar @{ $field->children }, 0, 'children scalar 1');
18is(scalar(() = $field->children), 0, 'children list 1');
19
20$field->checkboxes(apple  => 'Apple',
21                   orange =>
22                   {
23                     label => 'Orange',
24                   },
25                   Rose::HTML::Form::Field::Checkbox->new(value => 'grape', label => 'Grape'));
26
27is(scalar @{ $field->children }, 0, 'children scalar 2');
28is(scalar(() = $field->children), 0, 'children list 2');
29
30is(join(',', sort $field->labels), 'Apple,Grape,Orange,apple,grape,orange', 'labels()');
31
32is($field->html_field,
33  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
34  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
35  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
36  'html_field() 1');
37
38$field =
39  Rose::HTML::Form::Field::CheckboxGroup->new(
40    name => 'fruits',
41    choices =>
42    [
43      Rose::HTML::Form::Field::Checkbox->new(value => 'apple', label => 'Apple'),
44      orange  => 'Orange',
45      grape =>
46      {
47        label => 'Grape',
48      }
49    ]);
50
51$field->checkbox('apple')->label('<b>Apple</b>');
52$field->escape_html(0);
53
54is($field->html_field,
55  qq(<input name="fruits" type="checkbox" value="apple"> <label><b>Apple</b></label><br>\n) .
56  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
57  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
58  'escape_html() 1');
59
60$field->escape_html(1);
61
62is($field->html_field,
63  qq(<input name="fruits" type="checkbox" value="apple"> <label>&lt;b&gt;Apple&lt;/b&gt;</label><br>\n) .
64  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
65  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
66  'escape_html() 1');
67
68$field->checkbox('apple')->label('Apple');
69
70$field->linebreak(0);
71
72is($field->html_field,
73  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label> ) .
74  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label> ) .
75  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
76  'linebreak()');
77
78$field->linebreak(1);
79
80$field->html_linebreak('<br><br>');
81
82is($field->html_field,
83  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br><br>) .
84  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br><br>) .
85  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
86  'html_linebreak()');
87
88$field->html_linebreak("<br>\n");
89
90$field->xhtml_linebreak('<br /><br />');
91
92is($field->xhtml_field,
93  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br /><br />) .
94  qq(<input name="fruits" type="checkbox" value="orange" /> <label>Orange</label><br /><br />) .
95  qq(<input name="fruits" type="checkbox" value="grape" /> <label>Grape</label>),
96  'xhtml_linebreak()');
97
98$field->xhtml_linebreak("<br />\n");
99
100$field->default('apple');
101
102is($field->html_field,
103  qq(<input checked name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
104  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
105  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
106  'default()');
107
108$field->input_value('orange');
109
110is($field->html_field,
111  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
112  qq(<input checked name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
113  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
114  'value() 1');
115
116$field->value('orange');
117
118my $values = join(',', $field->values);
119
120$field->error("Do not pick orange!");
121
122is($field->html,
123  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
124  qq(<input checked name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
125  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
126  qq(<span class="error">Do not pick orange!</span>),
127  'html()');
128
129$field->error(undef);
130
131$field->add_value('apple');
132
133is($field->html_field,
134  qq(<input checked name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
135  qq(<input checked name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
136  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
137  'add_value()');
138
139is(join(',', $field->output_value), 'apple,orange', 'values()');
140is(join(',', @{$field->output_value}), 'apple,orange', 'value() 2');
141
142$field->input_value(undef);
143
144$field->add_values('orange', 'grape');
145
146is($field->html_field,
147  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
148  qq(<input checked name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
149  qq(<input checked name="fruits" type="checkbox" value="grape"> <label>Grape</label>),
150  'add_values() 1');
151
152is($field->xhtml_field,
153  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
154  qq(<input checked="checked" name="fruits" type="checkbox" value="orange" /> <label>Orange</label><br />\n) .
155  qq(<input checked="checked" name="fruits" type="checkbox" value="grape" /> <label>Grape</label>),
156  'add_values() 2');
157
158is(join(',', $field->values), 'grape,orange', 'values() 2');
159is(join(',', @{$field->value}), 'grape,orange', 'value() 3');
160
161ok($field->is_checked('orange'), 'is_checked() 1');
162ok($field->is_checked('grape'), 'is_checked() 2');
163ok(!$field->is_checked('apple'), 'is_checked() 3');
164ok(!$field->is_checked('foo'), 'is_checked() 4');
165
166ok($field->has_value('orange'), 'has_value() 1');
167ok($field->has_value('grape'), 'has_value() 2');
168ok(!$field->has_value('apple'), 'has_value() 3');
169ok(!$field->has_value('foo'), 'has_value() 4');
170
171$field->add_checkboxes(pear => 'Pear', berry => 'Berry');
172
173is($field->html_field,
174  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
175  qq(<input checked name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
176  qq(<input checked name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
177  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
178  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label>),
179  'add_checkboxes() hash');
180
181$field->add_checkboxes(Rose::HTML::Form::Field::Checkbox->new(value => 'squash', label => 'Squash'),
182                       Rose::HTML::Form::Field::Checkbox->new(value => 'cherry', label => 'Cherry'));
183
184is($field->html_field,
185  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
186  qq(<input checked name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
187  qq(<input checked name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
188  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
189  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
190  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
191  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
192  'add_checkboxes() objects');
193
194is($field->html_hidden_field,
195  qq(<input name="fruits" type="hidden" value="orange">\n) .
196  qq(<input name="fruits" type="hidden" value="grape">),
197  'html_hidden_field()');
198
199is($field->html_hidden_fields,
200  qq(<input name="fruits" type="hidden" value="orange">\n) .
201  qq(<input name="fruits" type="hidden" value="grape">),
202  'html_hidden_fields()');
203
204is(join("\n", map { $_->html } $field->hidden_field),
205  qq(<input name="fruits" type="hidden" value="orange">\n) .
206  qq(<input name="fruits" type="hidden" value="grape">),
207  'hidden_field()');
208
209is(join("\n", map { $_->html } $field->hidden_fields),
210  qq(<input name="fruits" type="hidden" value="orange">\n) .
211  qq(<input name="fruits" type="hidden" value="grape">),
212  'hidden_fields()');
213
214$field->clear;
215
216is($field->html_field,
217  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
218  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
219  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
220  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
221  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
222  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
223  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
224  'clear()');
225
226my $table =<<"EOF";
227<table class="checkbox-group">
228<tr>
229<td><input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>
230<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>
231<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>
232<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>
233<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>
234<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>
235<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label></td>
236</tr>
237</table>
238EOF
239
240is($field->html_table, $table, 'html_table() 1');
241
242$table =<<"EOF";
243<table border="1" cellpadding="1" cellspacing="2" class="xxx checkbox-group">
244<tr>
245<td><input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>
246<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>
247<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>
248<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>
249<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>
250<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>
251<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label></td>
252</tr>
253</table>
254EOF
255
256is($field->html_table(table => { cellpadding => 1, cellspacing => 2, border => 1, class => 'xxx' }),
257   $table, 'xhtml_table() 1');
258
259$table =<<"EOF";
260<table border="1" cellpadding="1" cellspacing="2" class="yyy checkbox-group">
261<tr>
262<td><input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />
263<input name="fruits" type="checkbox" value="orange" /> <label>Orange</label><br />
264<input name="fruits" type="checkbox" value="grape" /> <label>Grape</label><br />
265<input name="fruits" type="checkbox" value="pear" /> <label>Pear</label><br />
266<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />
267<input name="fruits" type="checkbox" value="squash" /> <label>Squash</label><br />
268<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label></td>
269</tr>
270</table>
271EOF
272
273is($field->xhtml_table(class => 'yyy', table => { cellpadding => 1, cellspacing => 2, border => 1 }),
274   $table, 'html_table() 2');
275
276$table =<<"EOF";
277<table class="checkbox-group">
278<tr>
279<td><input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>
280<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>
281<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>
282<input name="fruits" type="checkbox" value="pear"> <label>Pear</label></td>
283<td><input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>
284<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>
285<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label></td>
286</tr>
287</table>
288EOF
289
290is($field->html_table(columns => 2), $table, 'html_table() 3');
291
292$field->reset;
293
294is(join(',', $field->output_value), 'apple', 'reset() 1');
295
296is($field->html_field,
297  qq(<input checked name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
298  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
299  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
300  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
301  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
302  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
303  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
304  'reset() 2');
305
306$field->default(undef);
307
308is(join(',', $field->value), '', 'reset() 3');
309
310is($field->html_field,
311  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
312  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange</label><br>\n) .
313  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
314  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
315  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
316  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
317  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
318  'reset() 4');
319
320my $id = ref($field)->localizer->add_localized_message(
321  name => 'ORANGE_LABEL',
322  text =>
323  {
324    en => 'Orange EN',
325    xx => 'Le Orange',
326  });
327
328$field->checkbox('orange')->label_id($id);
329
330is($field->checkbox('orange')->label->as_string, 'Orange EN', 'localized label 1');
331is($field->html_field,
332  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
333  qq(<input name="fruits" type="checkbox" value="orange"> <label>Orange EN</label><br>\n) .
334  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
335  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
336  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
337  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
338  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
339  'localized label 2');
340
341$field->localizer->locale('xx');
342
343is($field->checkbox('orange')->label->as_string, 'Le Orange', 'localized label 3');
344is($field->html_field,
345  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
346  qq(<input name="fruits" type="checkbox" value="orange"> <label>Le Orange</label><br>\n) .
347  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
348  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
349  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
350  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
351  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
352  'localized label 4');
353
354$field->input_value('grape');
355$field->checkbox('grape')->hide;
356
357is($field->internal_value->[0], undef, 'hidden 0');
358
359is($field->html_field,
360  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
361  qq(<input name="fruits" type="checkbox" value="orange"> <label>Le Orange</label><br>\n) .
362  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
363  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
364  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
365  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
366  'hidden 1');
367
368is($field->xhtml_field,
369  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
370  qq(<input name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
371  qq(<input name="fruits" type="checkbox" value="pear" /> <label>Pear</label><br />\n) .
372  qq(<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
373  qq(<input name="fruits" type="checkbox" value="squash" /> <label>Squash</label><br />\n) .
374  qq(<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
375  'hidden 2');
376
377$field->checkbox('grape')->show;
378
379is($field->html_field,
380  qq(<input name="fruits" type="checkbox" value="apple"> <label>Apple</label><br>\n) .
381  qq(<input name="fruits" type="checkbox" value="orange"> <label>Le Orange</label><br>\n) .
382  qq(<input name="fruits" type="checkbox" value="grape"> <label>Grape</label><br>\n) .
383  qq(<input name="fruits" type="checkbox" value="pear"> <label>Pear</label><br>\n) .
384  qq(<input name="fruits" type="checkbox" value="berry"> <label>Berry</label><br>\n) .
385  qq(<input name="fruits" type="checkbox" value="squash"> <label>Squash</label><br>\n) .
386  qq(<input name="fruits" type="checkbox" value="cherry"> <label>Cherry</label>),
387  'hidden 3');
388
389is($field->xhtml_field,
390  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
391  qq(<input name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
392  qq(<input name="fruits" type="checkbox" value="grape" /> <label>Grape</label><br />\n) .
393  qq(<input name="fruits" type="checkbox" value="pear" /> <label>Pear</label><br />\n) .
394  qq(<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
395  qq(<input name="fruits" type="checkbox" value="squash" /> <label>Squash</label><br />\n) .
396  qq(<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
397  'hidden 4');
398
399$field->hide_all_checkboxes;
400
401is($field->xhtml_field, '', 'hidden 5');
402
403$field->show_all_checkboxes;
404
405is($field->xhtml_field,
406  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
407  qq(<input name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
408  qq(<input name="fruits" type="checkbox" value="grape" /> <label>Grape</label><br />\n) .
409  qq(<input name="fruits" type="checkbox" value="pear" /> <label>Pear</label><br />\n) .
410  qq(<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
411  qq(<input name="fruits" type="checkbox" value="squash" /> <label>Squash</label><br />\n) .
412  qq(<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
413  'hidden 6');
414
415$field->delete_checkbox('squash');
416
417is($field->xhtml_field,
418  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
419  qq(<input name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
420  qq(<input name="fruits" type="checkbox" value="grape" /> <label>Grape</label><br />\n) .
421  qq(<input name="fruits" type="checkbox" value="pear" /> <label>Pear</label><br />\n) .
422  qq(<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
423  qq(<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
424  'delete 1');
425
426$field->delete_checkboxes('squash', 'pear', 'grape');
427
428is($field->xhtml_field,
429  qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
430  qq(<input name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
431  qq(<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
432  qq(<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
433  'delete 2');
434
435my $i = 1;
436
437foreach my $name (qw(items checkboxes))
438{
439  my $method = "${name}_html_attr";
440
441  $field->$method(class => 'bar');
442
443  is($field->xhtml_field,
444    qq(<input class="bar" name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
445    qq(<input class="bar" name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
446    qq(<input class="bar" name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
447    qq(<input class="bar" name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
448    "$method " . $i++);
449
450  is($field->$method('class'), 'bar', "$method " . $i++);
451
452  $method = "delete_${name}_html_attr";
453
454  $field->$method('class');
455
456  is($field->xhtml_field,
457    qq(<input name="fruits" type="checkbox" value="apple" /> <label>Apple</label><br />\n) .
458    qq(<input name="fruits" type="checkbox" value="orange" /> <label>Le Orange</label><br />\n) .
459    qq(<input name="fruits" type="checkbox" value="berry" /> <label>Berry</label><br />\n) .
460    qq(<input name="fruits" type="checkbox" value="cherry" /> <label>Cherry</label>),
461    "$method " . $i++);
462}
463
464$field->input_value('apple');
465
466is($field->is_empty, 0, 'is_empty 3');
467
468$field->input_value('');
469is($field->is_empty, 1, 'is_empty 4');
470