File: checkboxgroup.t

package info (click to toggle)
libhtml-formfu-perl 2.07000-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,396 kB
  • sloc: perl: 12,777; makefile: 9; sql: 5
file content (136 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use strict;
use warnings;

use Test::More tests => 5;

use HTML::FormFu;

my $form = HTML::FormFu->new(
    { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

my $field1 = $form->element('Checkboxgroup')->name('foo')->value(2)
    ->options( [ [ 1 => 'One' ], [ 2 => 'Two' ] ] );

# add element to test non-reversed labels
my $field2 = $form->element('Checkboxgroup')->name('foo2')
    ->options( [ [ 'a' => 'A' ], [ 'b' => 'B' ] ] )->reverse_group(0);

# add more elements to test accessor output
$form->element('Checkboxgroup')->name('foo3')->options(
    [   { label => 'Ein',  value => 1 },
        { label => 'Zwei', value => 2, attributes => { class => 'foobar' } },
    ] );
$form->element('Checkboxgroup')->name('bar')->values( [qw/ one two three /] )
    ->value('two')->label('My Bar');

my $field1_xhtml = qq{<fieldset>
<span>
<span>
<input name="foo" type="checkbox" value="1" />
<label>One</label>
</span>
<span>
<input name="foo" type="checkbox" value="2" checked="checked" />
<label>Two</label>
</span>
</span>
</fieldset>};

is( "$field1", $field1_xhtml, 'basic checkboxgroup' );

my $field2_xhtml = qq{<fieldset>
<span>
<span>
<label>A</label>
<input name="foo2" type="checkbox" value="a" />
</span>
<span>
<label>B</label>
<input name="foo2" type="checkbox" value="b" />
</span>
</span>
</fieldset>};

is( "$field2", $field2_xhtml, 'checkboxgroup with reverse_group off' );

my $form_xhtml = <<EOF;
<form action="" method="post">
$field1_xhtml
$field2_xhtml
<fieldset>
<span>
<span>
<input name="foo3" type="checkbox" value="1" />
<label>Ein</label>
</span>
<span>
<input name="foo3" type="checkbox" value="2" class="foobar" />
<label>Zwei</label>
</span>
</span>
</fieldset>
<fieldset>
<legend>My Bar</legend>
<span>
<span>
<input name="bar" type="checkbox" value="one" />
<label>One</label>
</span>
<span>
<input name="bar" type="checkbox" value="two" checked="checked" />
<label>Two</label>
</span>
<span>
<input name="bar" type="checkbox" value="three" />
<label>Three</label>
</span>
</span>
</fieldset>
</form>
EOF

is( "$form", $form_xhtml, 'stringified form' );

# With mocked basic query
{
    $form->process(
        {   foo => [ 1, 2, ],
            bar => 'three',
        } );

    my $foo_xhtml = qq{<fieldset>
<span>
<span>
<input name="foo" type="checkbox" value="1" checked="checked" />
<label>One</label>
</span>
<span>
<input name="foo" type="checkbox" value="2" checked="checked" />
<label>Two</label>
</span>
</span>
</fieldset>};

    is( $form->get_field('foo'), $foo_xhtml, 'checkboxgroup after query' );

    my $bar_xhtml = qq{<fieldset>
<legend>My Bar</legend>
<span>
<span>
<input name="bar" type="checkbox" value="one" />
<label>One</label>
</span>
<span>
<input name="bar" type="checkbox" value="two" />
<label>Two</label>
</span>
<span>
<input name="bar" type="checkbox" value="three" checked="checked" />
<label>Three</label>
</span>
</span>
</fieldset>};

    is( $form->get_field('bar'),
        $bar_xhtml, 'second checkboxgroup after query' );
}