1#!perl
2# Compatibility tests
3
4use strict;
5use warnings;
6use Test::More tests => 50;
7
8use HTML::FillInForm::Lite::Compat;
9
10BEGIN{ use_ok('HTML::FillInForm') }
11
12use CGI;
13use FindBin qw($Bin);
14
15use Fatal qw(open);
16
17{
18	use warnings FATAL => 'portable';
19	eval{
20		HTML::FillInForm->new(escape => 0);
21	};
22	like $@, qr/HTML::FillInForm->new\(\) accepts no options/;
23
24	eval{
25		HTML::FillInForm->fill(escape => 0);
26	};
27	like $@, qr/HTML::FillInForm::Lite-specific option/;
28}
29
30my $o = HTML::FillInForm->new();
31
32isa_ok $o, 'HTML::FillInForm::Lite';
33isa_ok $o, 'HTML::FillInForm';
34
35my $file = "$Bin/test.html";
36my $s    = do{ open my($in), '<', $file; local $/; <$in> };
37
38my %q = (foo => 'bar');
39my $q = CGI->new(\%q);
40
41my(%fdat, $hidden_form_in, $output, $fif, $is_checked, $is_selected, $html, $result);
42
43eval{
44	HTML::FillInForm->fill();
45};
46ok $@, "fill without args";
47
48eval{
49	HTML::FillInForm->fill(scalarref => \$s);
50};
51ok $@, "fill without form data";
52
53eval{
54	HTML::FillInForm->fill(fdat => \%q);
55};
56ok $@, "fill without sources";
57
58$output  = HTML::FillInForm->fill(\$s, \%q);
59
60like $output, qr/value="bar"/, "simple fill()";
61
62is $o->fill(scalarref => \$s,   fdat => \%q),  $output, "fill(scalarref, fdat)";
63is $o->fill(arrayref  => [$s],  fobject =>  $q),  $output, "fill(arrayref,  fobj)";
64is $o->fill(file      => $file, fobject => [$q]), $output, "fill(file, [fobj])";
65
66is $o->fill_scalarref(\$s, fdat => \%q), $output, "fill_scalarref()";
67is $o->fill_arrayref([$s], fdat => \%q), $output, "fill_arrayref()";
68is $o->fill_file($file, fdat => \%q), $output, "fill_file()";
69
70like $o->fill(\'<input type="password" name="foo">', {foo => 'bar'}),
71	qr/value="bar"/, "fill password by default";
72unlike $o->fill(\'<input type="password" name="foo">', {foo => 'bar'}, fill_password => 0),
73	qr/value="bar"/, "fill_password => 0";
74
75
76like $o->fill(\<<'EOT', { foo => '<bar>' }), qr/checked/, "decode entities by default";
77<input type="radio" value="&#60;bar&#62;" name="foo" />
78EOT
79
80{
81	no warnings 'portable';
82	unlike $o->fill(\<<'EOT', { foo => '<bar>' }, decode_entity => 0), qr/checked/, "decode_entity => 0";
83	<input type="radio" value="&#60;bar&#62;" name="foo" />
84EOT
85}
86
87SKIP:{
88	skip "require &utf8::is_utf8", 3
89		unless defined &utf8::is_utf8;
90	skip "require HTML::FillInForm::ForceUTF8", 3
91		unless eval{ require HTML::FillInForm::ForceUTF8; };
92
93	my $o = HTML::FillInForm::ForceUTF8->new();
94
95	isa_ok $o, 'HTML::FillInForm::Lite', "HTML::FillInForm::ForceUTF8";
96	isa_ok $o, 'HTML::FillInForm',       "HTML::FillInForm::ForceUTF8";
97
98	ok utf8::is_utf8( $o->fill(
99		scalarref => \'<input name="foo">',
100		fdat      => { foo => 'bar' }) ),
101		"extented _get_param()";
102}
103
104#==================================================================
105# tests from original HTML::FillInForm distribution/t/03_checkbox.t
106#==================================================================
107
108$hidden_form_in = qq{<input type="checkbox" name="foo1" value="bar1">
109<input type="checkbox" name="foo1" value="bar2">
110<input type="checkbox" name="foo1" value="bar3">
111<input type="checkbox" name="foo2" value="bar1">
112<input type="checkbox" name="foo2" value="bar2">
113<input type="checkbox" name="foo2" value="bar3">
114<input type="checkbox" name="foo3" value="bar1">
115<input type="checkbox" name="foo3" checked="checked" value="bar2">
116<input type="checkbox" name="foo3" value="bar3">
117<input type="checkbox" name="foo4" value="bar1">
118<input type="checkbox" name="foo4" checked="checked" value="bar2">
119<input type="checkbox" name="foo4" value="bar3">
120<input type="checkbox" name="foo5">
121<input type="checkbox" name="foo6">
122<input type="checkbox" name="foo7" checked="checked">
123<input type="checkbox" name="foo8" checked="checked">};
124
125%fdat = (foo1 => 'bar1',
126           foo2 => ['bar1', 'bar2',],
127	   foo3 => '',
128	   foo5 => 'on',
129	   foo6 => '',
130	   foo7 => 'on',
131	   foo8 => '');
132
133$fif = new HTML::FillInForm;
134$output = $fif->fill(scalarref => \$hidden_form_in,
135                       fdat => \%fdat);
136
137$is_checked = join(" ",map { m/checked/ ? "yes" : "no" } split ("\n",$output));
138
139is $is_checked, "yes no no yes yes no no no no no yes no yes no yes no", "checkbox.t";
140
141#==================================================================
142# tests from original HTML::FillInForm distribution/t/04_select.t
143#==================================================================
144
145$hidden_form_in = qq{<select multiple="multiple"  name="foo1">
146	<option value="0">bar1</option>
147	<option value="bar2">bar2</option>
148	<option value="bar3">bar3</option>
149</select>
150<select multiple="multiple"  name="foo2">
151	<option value="bar1">bar1</option>
152	<option value="bar2">bar2</option>
153	<option value="bar3">bar3</option>
154</select>
155<select multiple="multiple"  name="foo3">
156	<option value="bar1">bar1</option>
157	<option selected="selected" value="bar2">bar2</option>
158	<option value="bar3">bar3</option>
159</select>
160<select multiple="multiple"  name="foo4">
161	<option value="bar1">bar1</option>
162	<option selected="selected" value="bar2">bar2</option>
163	<option value="bar3">bar3</option>
164</select>};
165
166$q = new CGI( { foo1 => '0',
167           foo2 => ['bar1', 'bar2',],
168	   foo3 => '' }
169	);
170
171$output = HTML::FillInForm->fill(\$hidden_form_in, $q);
172
173$is_selected = join(" ",map { m/selected/ ? "yes" : "no" } grep /option/, split ("\n",$output));
174
175is $is_selected, "yes no no yes yes no no no no no yes no",
176	"select test 1 from the HTML::FillInForm distribution";
177
178$hidden_form_in = qq{<select multiple="multiple"  name="foo1">
179	<option>bar1</option>
180	<option>bar2</option>
181	<option>bar3</option>
182</select>
183<select multiple="multiple"  name="foo2">
184	<option> bar1</option>
185	<option> bar2</option>
186	<option>bar3</option>
187</select>
188<select multiple="multiple"  name="foo3">
189	<option>bar1</option>
190	<option selected="selected">bar2</option>
191	<option>bar3</option>
192</select>
193<select multiple="multiple"  name="foo4">
194	<option>bar1</option>
195	<option selected="selected">bar2</option>
196	<option>bar3  </option>
197</select>};
198
199$q = new CGI( { foo1 => 'bar1',
200           foo2 => ['bar1', 'bar2',],
201	   foo3 => '' }
202	);
203
204$fif = new HTML::FillInForm;
205$output = $fif->fill(\$hidden_form_in, $q);
206
207$is_selected = join(" ",map { m/selected/ ? "yes" : "no" } grep /option/, split ("\n",$output));
208
209is $is_selected, "yes no no yes yes no no no no no yes no",
210	"select test 2";
211
212# test empty option tag
213
214$hidden_form_in = qq{<select name="x"><option></option></select>};
215$fif = new HTML::FillInForm;
216$output = $fif->fill(\$hidden_form_in, $q);
217
218is $output, $hidden_form_in, "select test 3 with empty option";
219
220$hidden_form_in = qq{<select name="foo1"><option></option><option value="bar1"></option></select>};
221$fif = new HTML::FillInForm;
222$output = $fif->fill(\$hidden_form_in, $q);
223like $output, qr/( selected="selected"| value="bar1"){2}/,
224	"select test 3 with empty option";
225
226
227#==================================================================
228# tests from original HTML::FillInForm distribution/t/16_ignore_fields.t
229#==================================================================
230
231$hidden_form_in = qq{<select multiple="multiple" name="foo1">
232	<option value="0">bar1</option>
233	<option value="bar2">bar2</option>
234	<option value="bar3">bar3</option>
235</select>
236<select multiple="multiple" name="foo2">
237	<option value="bar1">bar1</option>
238	<option value="bar2">bar2</option>
239	<option value="bar3">bar3</option>
240</select>
241<select multiple="multiple" name="foo3">
242	<option value="bar1">bar1</option>
243	<option selected="selected" value="bar2">bar2</option>
244	<option value="bar3">bar3</option>
245</select>
246<select multiple="multiple" name="foo4">
247	<option value="bar1">bar1</option>
248	<option selected="selected" value="bar2">bar2</option>
249	<option value="bar3">bar3</option>
250</select>};
251$q = new CGI( { foo1 => '0',
252           foo2 => ['bar1', 'bar2',],
253	   foo3 => '' }
254	);
255
256$output = HTML::FillInForm->fill(scalarref => \$hidden_form_in,
257                       fobject => $q,
258			ignore_fields => ['asdf','foo1','asdf']);
259
260$is_selected = join(" ",map { m/selected/ ? "yes" : "no" } grep /option/, split ("\n",$output));
261
262is $is_selected, "no no no yes yes no no no no no yes no", "ignore_fields.t";
263
264
265#==================================================================
266# tests from original HTML::FillInForm distribution/t/19_extra.t
267#==================================================================
268
269$html = qq[
270<form>
271<input type="text" name="one" value="not disturbed">
272<input type="text" name="two" value="not disturbed">
273<input type="text" name="three" value="not disturbed">
274</form>
275];
276
277$result = HTML::FillInForm->new->fill_scalarref(
278                                         \$html,
279                                         fdat => {
280                                           two => "new val 2",
281                                           three => "new val 3",
282                                         },
283                                         ignore_fields => 'one',
284                                         );
285
286like($result, qr/(?:not disturbed.+one|one.+not disturbed)/,'scalar value of ignore_fields');
287like($result, qr/(?:new val 2.+two|two.+new val 2)/,'fill_scalarref worked');
288like($result, qr/(?:new val 3.+three|three.+new val 3)/,'fill_scalarref worked 2');
289
290
291$html = qq[
292<form>
293<input type="text" name="one" value="not disturbed">
294<input type="text" name="two" value="not disturbed">
295</form>
296];
297
298my @html_array = split /\n/, $html;
299
300
301{
302    $result = HTML::FillInForm->new->fill_arrayref(
303                                             \@html_array,
304                                             fdat => {
305                                               one => "new val 1",
306                                               two => "new val 2",
307                                             },
308                                             );
309
310    like($result, qr/(?:new val 1.+one|one.+new val 1)/, 'fill_arrayref 1');
311    like($result, qr/(?:new val 2.+two|two.+new val 2)/, 'fill_arrayref 2');
312}
313
314{
315    $result = HTML::FillInForm->fill(
316        \@html_array,
317        {
318            one => "new val 1",
319            two => "new val 2",
320        },
321     );
322
323    like($result, qr/(?:new val 1.+one|one.+new val 1)/, 'fill_arrayref 1');
324    like($result, qr/(?:new val 2.+two|two.+new val 2)/, 'fill_arrayref 2');
325}
326
327
328
329$html = qq[
330<form>
331<input type="text" name="one" value="not disturbed">
332<input type="text" name="two" value="not disturbed">
333</form>
334];
335
336
337$html = qq{<INPUT TYPE="password" NAME="foo1">
338};
339
340%fdat = (foo1 => ['bar2', 'bar3']);
341
342$result = HTML::FillInForm->new->fill(scalarref => \$html,
343                        fdat => \%fdat);
344
345like($result, qr/(?:bar2.+foo1|foo1.+bar2)/,'first array element taken for password fields');
346
347
348$html = qq{<TEXTAREA></TEXTAREA>};
349
350%fdat = (area => 'foo1');
351
352$result = HTML::FillInForm->new->fill(scalarref => \$html,
353                        fdat => \%fdat);
354
355ok($result !~ /foo1/,'textarea with no name');
356
357
358$html = qq{<TEXTAREA NAME="foo1"></TEXTAREA>};
359
360%fdat = (foo1 => ['bar2', 'bar3']);
361
362$result = HTML::FillInForm->new->fill(scalarref => \$html,
363                        fdat => \%fdat);
364
365
366ok($result eq '<TEXTAREA NAME="foo1">bar2</TEXTAREA>','first array element taken for textareas');
367
368
369
370$html = qq[<div></div>
371<!--Comment 1-->
372<form>
373<!--Comment 2-->
374<input type="text" name="foo0" value="not disturbed">
375<!--Comment
376
3773-->
378<TEXTAREA NAME="foo1"></TEXTAREA>
379</form>
380<!--Comment 4-->
381];
382
383%fdat = (foo0 => 'bar1', foo1 => 'bar2');
384
385$result = HTML::FillInForm->new->fill(scalarref => \$html,
386                        fdat => \%fdat);
387like($result, qr/(?:bar1.+foo0|foo0.+bar1)/,'form with comments 1');
388like($result, qr'<TEXTAREA NAME="foo1">bar2</TEXTAREA>','form with comments 2');
389like($result, qr'<!--Comment 1-->','Comment 1');
390like($result, qr'<!--Comment 2-->','Comment 2');
391like($result, qr'<!--Comment\n\n3-->','Comment 3');
392like($result, qr'<!--Comment 4-->','Comment 4');
393
394$html = qq[<div></div>
395<? HTML processing instructions 1 ?>
396<form>
397<? XML processing instructions 2?>
398<input type="text" name="foo0" value="not disturbed">
399<? HTML processing instructions
400
4013><TEXTAREA NAME="foo1"></TEXTAREA>
402</form>
403<?HTML processing instructions 4 >
404];
405
406%fdat = (foo0 => 'bar1', foo1 => 'bar2');
407
408$result = HTML::FillInForm->new->fill(scalarref => \$html,
409                        fdat => \%fdat);
410
411like($result, qr/(?:bar1.+foo0|foo0.+bar1)/,'form with processing 1');
412like($result, qr'<TEXTAREA NAME="foo1">bar2</TEXTAREA>','form with processing 2');
413like($result, qr'<\? HTML processing instructions 1 \?>','processing 1');
414like($result, qr'<\? XML processing instructions 2\?>','processing 2');
415like($result, qr'<\? HTML processing instructions\n\n3>','processing 3');
416like($result, qr'<\?HTML processing instructions 4 >','processing 4');
417
418#END
419