5 0_ex_00_base.t - Testing of the base CGI::Ex module.
10 use Test::More tests => 63;
14 my $cgix = CGI::Ex->new;
15 ok($cgix, "Got object");
17 ### test out form and cookies from the CGI object
19 skip("CGI.pm not found", 9) if ! eval { require CGI };
20 local $ENV{'REQUEST_METHOD'} = 'GET';
21 local $ENV{'QUERY_STRING'} = 'foo=bar&foo=baz&us=them';
22 local $ENV{'HTTP_COOKIE'} = 'bar=baz; bing=blam';
24 my $form = $cgix->form;
25 ok($form, "Got form");
26 ok(ref($form) eq 'HASH', "Good form");
27 ok($form->{'foo'}, "Found foo");
28 ok(ref($form->{'foo'}) eq 'ARRAY', "Foo is array");
29 ok(@{ $form->{'foo'} } == 2, "Correct number");
30 ok($form->{'us'}, "Found us");
31 ok($form->{'us'} eq 'them', "Us is correct");
33 my $cookies = $cgix->cookies;
34 ok($cookies, "Got cookies");
35 ok($cookies->{'bar'} eq 'baz', "Found correct bar");
39 my $form = {foo => 'bar', mult => [qw(a b c)]};
41 $cgix->cookies($form);
44 ok($form->{'foo'} eq 'bar', "Could set form");
46 my $cookies = $cgix->cookies;
47 ok($cookies->{'foo'} eq 'bar', "Could set form");
51 my $str = $cgix->make_form($form);
52 ok($str =~ /foo=bar/, "Make form works");
53 ok($str =~ /mult=a&mult=b&mult=c/, "Make form works 2");
55 $str = $cgix->make_form($form, ['foo']);
56 ok($str eq 'foo=bar', "Make form works with keys");
58 ### can't test these without being in apache (well we could test STDOUT - but that is for another day - TODO)
75 ok($cgix->can($meth), "Has method $meth");
80 ok(($sec = CGI::Ex::time_calc('1m')) == time + 60, "Time_calc ($sec)");
81 ok(($sec = CGI::Ex::time_calc('-1m')) == time - 60, "Time_calc ($sec)");
82 ok(($sec = CGI::Ex::time_calc('1 m')) == time + 60, "Time_calc ($sec)");
83 ok(($sec = CGI::Ex::time_calc('1 min')) == time + 60, "Time_calc ($sec)");
84 ok(($sec = CGI::Ex::time_calc('1')) == 1, "Time_calc ($sec)");
85 ok(($sec = CGI::Ex::time_calc('now')) == time, "Time_calc ($sec)");
86 ok(($sec = CGI::Ex::time_calc(__FILE__)), "Time_calc ($sec)");
88 ###----------------------------------------------------------------###
90 my $html = "<input type=text name=foo value=''>";
91 $form = {foo => 'bar'};
94 ok(($out = $cgix->fill(scalarref => \$html, form => $form)) =~ /value=([\"\'])bar\1/, "Filled $out");
95 ok(($out = $cgix->fill(arrayref => [$html], form => $form)) =~ /value=([\"\'])bar\1/, "Filled $out");
97 $cgix->fill(text => \$html, form => $form);
98 ok($html =~ /value=([\"\'])bar\1/, "Filled $html");
100 $html = "<form name=foo><input type=text name=baz value=''></form><form name=bar><input type=password name=bim value=''></form>";
102 $form = {baz => 'bing', bim => 'bang'};
104 $out = $cgix->fill(scalarref => \$html, form => $form, target => 'foo');
105 ok($out =~ /bing/, "Got bing");
106 ok($out !~ /bang/, "Didn't get bang");
108 $out = $cgix->fill(scalarref => \$html, form => $form, target => 'bar');
109 ok($out =~ /bang/, "Got bang");
110 ok($out !~ /bing/, "Didn't get bing");
112 $out = $cgix->fill(scalarref => \$html, form => $form, ignore_fields => ['baz']);
113 ok($out =~ /bang/, "Got bang");
114 ok($out !~ /bing/, "Didn't get bing");
116 $out = $cgix->fill(scalarref => \$html, form => $form, ignore_fields => ['bim']);
117 ok($out =~ /bing/, "Got bing");
118 ok($out !~ /bang/, "Didn't get bang");
120 $out = $cgix->fill(scalarref => \$html, form => $form, fill_password => 1);
121 ok($out =~ /bing/, "Got bing");
122 ok($out =~ /bang/, "Got bang");
124 $out = $cgix->fill(scalarref => \$html, form => $form, fill_password => undef);
125 ok($out =~ /bing/, "Got bing");
126 ok($out =~ /bang/, "Got bang");
128 $out = $cgix->fill(scalarref => \$html, form => $form, fill_password => 0);
129 ok($out =~ /bing/, "Got bing");
130 ok($out !~ /bang/, "Didn't get bang");
132 ###----------------------------------------------------------------###
134 $form = {foo => 'bar'};
135 my $val = {foo => {'required' => 1}};
137 my $e = $cgix->validate($form, $val);
138 ok(! $e, "No error");
141 $e = $cgix->validate($form, $val);
143 ok("$e" =~ /required/i, "Had error message ($e)");
145 ###----------------------------------------------------------------###
147 ### defer testing to the conf test modules
148 foreach my $meth (qw(
152 ok($cgix->can($meth), "Has method $meth");
155 ###----------------------------------------------------------------###
157 $form = {foo => 'bar'};
158 my $args = {VARIABLES => {bim => 'bam'}};
159 my $temp = "([% foo %])([% bim %])";
161 $out = $cgix->swap_template($temp, $form, $args);
162 ok($out =~ /bar/, "Got bar");
163 ok($out =~ /bam/, "Got bam");
165 $cgix->swap_template(\$temp, $form, $args);
166 ok($temp =~ /bar/, "Got bar");
167 ok($temp =~ /bam/, "Got bam");
169 ###----------------------------------------------------------------###