/usr/share/perl5/CGI/Test/Form.pm is in libcgi-test-perl 1.111-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 | package CGI::Test::Form;
use strict;
use warnings;
####################################################################
# $Id: Form.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
# $Name: cgi-test_0-104_t1 $
####################################################################
# Copyright (c) 2001, Raphael Manfredi
#
# You may redistribute only under the terms of the Artistic License,
# as specified in the README file that comes with the distribution.
#
#
# Class interfacing with the content of a <FORM> tag, which comes from
# a CGI::Test::Page object. The tree nodes we are playing with here are
# direct pointers into the node of the page object.
#
use Carp;
#
# We may not create an instance of all those classes, but the cost of
# lazily requiring them would probably outweigh the cost of loading
# them once and for all, on reasonably sized forms.
#
use CGI::Test::Form::Widget::Button::Submit;
use CGI::Test::Form::Widget::Button::Reset;
use CGI::Test::Form::Widget::Button::Image;
use CGI::Test::Form::Widget::Button::Plain;
use CGI::Test::Form::Widget::Input::Text_Field;
use CGI::Test::Form::Widget::Input::Text_Area;
use CGI::Test::Form::Widget::Input::Password;
use CGI::Test::Form::Widget::Input::File;
use CGI::Test::Form::Widget::Menu::List;
use CGI::Test::Form::Widget::Menu::Popup;
use CGI::Test::Form::Widget::Box::Radio;
use CGI::Test::Form::Widget::Box::Check;
use CGI::Test::Form::Widget::Hidden;
######################################################################
#
# ->new
#
# Creation routine
#
######################################################################
sub new
{
my $this = bless {}, shift;
my ($node, $page) = @_;
$this->{tree} = $node; # <FORM> is the root node of the tree
$this->{page} = $page;
$this->{enctype} = $node->attr("enctype")
|| "application/x-www-form-urlencoded";
$this->{method} = uc $node->attr("method") || "POST";
foreach my $attr (qw(action name accept accept-charset))
{
my $oattr = $attr;
$oattr =~ s/-/_/g;
my $value = $node->attr($attr);
$this->{$oattr} = $value if defined $value;
}
#
# Although ACTION is now required in newer HTML DTDs, it was optional
# in HTML 2.0 and defaults to the base URI of the document.
#
$this->{action} = $page->uri->as_string unless exists $this->{action};
return $this;
}
######################################################################
# DEPRECATED
######################################################################
sub make
{ #
my $class = shift;
return $class->new(@_);
}
#
# Attribute access
#
######################################################################
sub tree
{
my $this = shift;
return $this->{tree};
}
######################################################################
sub page
{
my $this = shift;
return $this->{page};
}
######################################################################
sub enctype
{
my $this = shift;
return $this->{enctype};
}
######################################################################
sub action
{
my $this = shift;
return $this->{action};
}
######################################################################
sub method
{
my $this = shift;
return $this->{method};
}
######################################################################
sub name
{
my $this = shift;
return $this->{name};
}
######################################################################
sub accept
{
my $this = shift;
return $this->{accept};
}
######################################################################
sub accept_charset
{
my $this = shift;
return $this->{accept_charset};
}
#
# Lazy attribute access
#
######################################################################
sub buttons
{
my $this = shift;
return $this->{buttons} || $this->_xtract("buttons");
}
######################################################################
sub inputs
{
my $this = shift;
return $this->{inputs} || $this->_xtract("inputs");
}
######################################################################
sub menus
{
my $this = shift;
return $this->{menus} || $this->_xtract("menus");
}
######################################################################
sub radios
{
my $this = shift;
return $this->{radios} || $this->_xtract("radios");
}
######################################################################
sub checkboxes
{
my $this = shift;
return $this->{checkboxes} || $this->_xtract("checkboxes");
}
######################################################################
sub hidden
{
my $this = shift;
return $this->{hidden} || $this->_xtract("hidden");
}
######################################################################
sub widgets
{
my $this = shift;
return $this->{widgets} || $this->_xtract("widgets");
}
#
# Second-order lazy attributes
#
######################################################################
sub submits
{
my $this = shift;
return $this->{submits} || ($this->{submits} = $this->_submits);
}
######################################################################
sub radio_groups
{
my $this = shift;
return $this->radios()
&& $this->{radio_groups};
}
######################################################################
sub checkbox_groups
{
my $this = shift;
return $this->checkboxes()
&& $this->{checkbox_groups};
}
#
# Expanded lists -- syntactic sugar
#
######################################################################
sub button_list
{
my $this = shift;
return @{$this->buttons()};
}
######################################################################
sub input_list
{
my $this = shift;
return @{$this->inputs()};
}
######################################################################
sub menu_list
{
my $this = shift;
return @{$this->menus()};
}
######################################################################
sub radio_list
{
my $this = shift;
return @{$this->radios()};
}
######################################################################
sub checkbox_list
{
my $this = shift;
return @{$this->checkboxes()};
}
######################################################################
sub hidden_list
{
my $this = shift;
return @{$this->hidden()};
}
######################################################################
sub widget_list
{
my $this = shift;
return @{$this->widgets()};
}
######################################################################
sub submit_list
{
my $this = shift;
@{$this->submits()};
}
#
# By parameter-name n-n widget access (one widget returned for each asked)
#
######################################################################
sub button_by_name
{
my $this = shift;
$this->_by_name($this->buttons, @_);
}
######################################################################
sub input_by_name
{
my $this = shift;
$this->_by_name($this->inputs, @_);
}
######################################################################
sub menu_by_name
{
my $this = shift;
$this->_by_name($this->menus, @_);
}
######################################################################
sub radio_by_name
{
my $this = shift;
$this->_by_name($this->radios, @_);
}
######################################################################
sub checkbox_by_name
{
my $this = shift;
$this->_by_name($this->checkboxes, @_);
}
######################################################################
sub hidden_by_name
{
my $this = shift;
$this->_by_name($this->hidden, @_);
}
######################################################################
sub widget_by_name
{
my $this = shift;
$this->_by_name($this->widgets, @_);
}
######################################################################
sub submit_by_name
{
my $this = shift;
return $this->_by_name($this->submits, @_);
}
#
# By parameter-name 1-n widget access (many widgets may be returned, one asked)
#
######################################################################
sub buttons_named
{
my $this = shift;
return $this->_all_named($this->buttons, @_);
}
######################################################################
sub inputs_named
{
my $this = shift;
return $this->_all_named($this->inputs, @_);
}
######################################################################
sub menus_named
{
my $this = shift;
return $this->_all_named($this->menus, @_);
}
######################################################################
sub radios_named
{
my $this = shift;
return $this->_all_named($this->radios, @_);
}
######################################################################
sub checkboxes_named
{
my $this = shift;
return $this->_all_named($this->checkboxes, @_);
}
######################################################################
sub hidden_named
{
my $this = shift;
return $this->_all_named($this->hidden, @_);
}
######################################################################
sub widgets_named
{
my $this = shift;
return $this->_all_named($this->widgets, @_);
}
######################################################################
sub submits_named
{
my $this = shift;
return $this->_all_named($this->submits, @_);
}
#
# Convenience routines around ->_matching().
#
######################################################################
sub buttons_matching
{
my $this = shift;
return $this->_matching($this->buttons, @_);
}
######################################################################
sub inputs_matching
{
my $this = shift;
return $this->_matching($this->inputs, @_);
}
######################################################################
sub menus_matching
{
my $this = shift;
return $this->_matching($this->menus, @_);
}
######################################################################
sub radios_matching
{
my $this = shift;
return $this->_matching($this->radios, @_);
}
######################################################################
sub checkboxes_matching
{
my $this = shift;
return $this->_matching($this->checkboxes, @_);
}
######################################################################
sub hidden_matching
{
my $this = shift;
return $this->_matching($this->hidden, @_);
}
######################################################################
sub widgets_matching
{
my $this = shift;
return $this->_matching($this->widgets, @_);
}
######################################################################
sub submits_matching
{
my $this = shift;
return $this->_matching($this->submits, @_);
}
######################################################################
#
# ->reset
#
# Reset form state, restoring all the widget controls to the value they
# had upon entry.
#
######################################################################
sub reset
{
my $this = shift;
foreach my $w ($this->widget_list)
{
$w->reset_state;
}
return;
}
######################################################################
#
# ->submit
#
# Submit this form.
# Returns resulting CGI::Test::Page.
#
######################################################################
sub submit
{
my $this = shift;
my $method = $this->method;
my $input = $this->_output; # Input to the request we're about to make
my $action = $this->_action_url;
my $page = $this->page;
my $server = $page->server;
my $result;
if ($method eq "GET")
{
confess "GET requests only allowed URL encoding, not %s",
$input->mime_type
unless $input->mime_type eq "application/x-www-form-urlencoded";
$action->query($input->data);
$result = $server->GET($action->as_string, $page->user);
}
elsif ($method eq "POST")
{
$result = $server->POST($action->as_string, $input, $page->user);
}
else
{
confess "unsupported method $method for FORM action";
}
return $result;
}
######################################################################
#
# ->_xtract
#
# Widget extraction routine: traverse the <FORM> tree and create an instance
# of CGI::Test::Form::Widget per encountered widget. The dynamic type depends
# on the widget type, e.g. a button creates a CGI::Test::Form::Widget::Button
# object.
#
# Widgets are also sorted by type, and stored as object attribute:
#
# buttons all buttons
# inputs text area, text fields, password fields
# menus popup menus
# radios radio buttons
# checkboxes all checkboxes
# hidden all hidden fields
# widgets all widgets, whatever their type.
#
# The special attribute `radio_groups' is only built when there is at least
# one radio button.
#
# Although we extract ALL the widgets, caller is only interested in a
# specific list, given in $which. Therefore, returns a list ref on that
# particular set.
#
######################################################################
sub _xtract
{
my $this = shift;
my ($which) = @_;
#
# Initiate traversal to locate all widgets nodes.
#
my %is_widget = map {$_ => 1} qw(input textarea select button isindex);
my @wg = $this->tree->look_down(sub {$is_widget{$_[ 0 ]->tag}});
#
# Initialize all lists to be empty
#
for my $attr ( qw(buttons inputs radios checkboxes hidden menus widgets) )
{
$this->{$attr} = [];
}
#
# And now sort them out.
#
my %input = ( # [ class name, attribute ]
"submit" => [ 'Button::Submit', "buttons" ],
"reset" => [ 'Button::Reset', "buttons" ],
"image" => [ 'Button::Image', "buttons" ],
"text" => [ 'Input::Text_Field', "inputs" ],
"file" => [ 'Input::File', "inputs" ],
"password" => [ 'Input::Password', "inputs" ],
"radio" => [ 'Box::Radio', "radios" ],
"checkbox" => [ 'Box::Check', "checkboxes" ],
"hidden" => [ 'Hidden', "hidden" ],
);
my %button = ( # [ class name, attribute ]
"submit" => [ 'Button::Submit', "buttons" ],
"reset" => [ 'Button::Reset', "buttons" ],
"button" => [ 'Button::Plain', "buttons" ],
);
my $wlist = $this->{widgets}; # All widgets also inserted there
foreach my $node (@wg)
{
my $tag = $node->tag;
my ($class, $attr);
my $hlookup;
if ($tag eq "input")
{
$hlookup = \%input;
}
elsif ($tag eq "textarea")
{
($class, $attr) = ("Input::Text_Area", "inputs");
}
elsif ($tag eq "select")
{
$attr = "menus";
$class =
($node->attr("multiple") || defined $node->attr("size"))
? "Menu::List"
: "Menu::Popup";
}
elsif ($tag eq "button")
{
$hlookup = \%button;
}
elsif ($tag eq "isindex")
{
warn "ISINDEX is deprecated, ignoring %s", $node->starttag;
next;
}
else
{
confess "reached tag '$tag': invalid tree look_down()?";
}
#
# If $hlookup is defined, we need to look at the TYPE attribute
# within the tag to determine the object to build.
#
# This handles <INPUT TYPE="xxx"> and <BUTTON TYPE="xxx">
#
if (defined $hlookup)
{
my $type = $node->attr("type");
unless (defined $type)
{
warn "missing TYPE indication in %s: %s", uc($tag),
$node->starttag;
next;
}
my $info = $hlookup->{lc($type)};
unless (defined $info)
{
warn "unknown TYPE '%s' in %s: %s", $type, uc($tag),
$node->starttag;
next;
}
($class, $attr) = @$info;
}
#
# Create object of given class, insert into attribute list.
# Objects will not keep a reference on the node, but will reference us.
#
my $obj = "CGI::Test::Form::Widget::$class"->new($node, $this);
push @{$this->{$attr}}, $obj;
push @$wlist, $obj;
}
#
# Special handling for radio buttons: they need to be groupped, so that
# selecting one automatically unselects others from the same group.
#
# Special handling for checkboxes: one may wish to get at a "group of
# checkboxes" instead of an individual checkbox widget.
#
my $radios = $this->{radios};
my $checkboxes = $this->{checkboxes};
if (@$radios)
{
require CGI::Test::Form::Group;
$this->{radio_groups} = CGI::Test::Form::Group->new($radios);
}
if (@$checkboxes)
{
require CGI::Test::Form::Group;
$this->{checkbox_groups} = CGI::Test::Form::Group->new($checkboxes);
}
#
# Finally, return the list they asked for.
#
return $this->{
$which};
}
######################################################################
#
# ->_by_name
#
# Access to widgets, by name, in an n-n fashion: one widget returned for
# each name asked, multiple names may be givem.
#
# Extract and return a list of widgets from a list, by comparing names.
# If no widget of corresponding name exists, returns undef.
#
# There is one returned element per requested name.
# When only one name is requested, either scalar or list context may be used.
#
# For widgets which may be groupped (e.g. radios or checkboxes), the item
# selected is the last one bearing that name within the form.
#
######################################################################
sub _by_name
{
my $this = shift;
my ($wlist, @names) = @_;
croak '$wlist is not ARRAY' unless ref $wlist eq 'ARRAY';
my %byname = map {$_->name => $_} @$wlist;
my @results = map {$byname{$_}} @names;
if (@names == 1)
{
return @results if wantarray;
return $results[ 0 ];
}
return @results;
}
######################################################################
#
# ->_all_named
#
# Access to widgets, by name, in a 1-n fashion: from one name, multiple widgets
# may be returned.
#
# Extract and return a list of widgets from a list, by comparing names.
# If no widget of corresponding name exists, returns an empty list.
# Otherwise returns the list of all widgets bearing that name.
#
######################################################################
sub _all_named
{
my $this = shift;
my ($wlist, $name) = @_;
croak 'wlist is not ARRAY' unless ref $wlist eq 'ARRAY';
return grep {$_->name eq $name} @$wlist;
}
######################################################################
#
# ->_matching
#
# Extract widgets from list via matching callback, invoked as:
#
# callback($widget, $context)
#
# where $context is one of the select routine parameters.
# Returns list of widgets for which the callback returned true.
#
######################################################################
sub _matching
{
my $this = shift;
my ($wlist, $code, $context) = @_;
croak '$wlist is not ARRAY' unless ref $wlist eq 'ARRAY';
croak '$code is not CODE reference' unless ref $code eq 'CODE';
return grep {&$code($_, $context)} @$wlist;
}
######################################################################
#
# ->delete
#
# Done with this page, cleanup by breaking circular & multiple refs.
#
######################################################################
sub delete
{
my $this = shift;
$this->{node} = undef;
$this->{page} = undef;
delete $this->{submits};
#
# Handle lazy attributes.
#
if (ref $this->{widgets})
{
#
# Each widget has a reference on us, which must be cleared.
#
foreach my $w (@{$this->{widgets}})
{
$w->delete;
}
#
# All widget objects have two references from here: one through their
# type list, and one through the general "widgets" list. Simply
# break the "widgets" list.
#
$this->{widgets} = undef;
}
$this->{radio_groups}->delete if ref $this->{radio_groups};
$this->{checkbox_groups}->delete if ref $this->{checkbox_groups};
return;
}
######################################################################
#
# ->_output
#
# Create a CGI::Test::Input object and fill it with all the submitable
# widgets. That object can then generate the data to be used as input of
# the form's action URL, depending on the form's encoding type.
#
######################################################################
sub _output
{
my $this = shift;
my $enctype = $this->enctype;
my $input;
#
# Create polymorphic form input object, holding this form's output.
#
# It's called "input" because its data are meant to be the input of the
# target CGI script.
#
if ($enctype eq "multipart/form-data")
{
require CGI::Test::Input::Multipart;
$input = CGI::Test::Input::Multipart->new();
}
else
{
warn "unknown FORM encoding type $enctype, using default"
if $enctype ne "application/x-www-form-urlencoded";
require CGI::Test::Input::URL;
$input = CGI::Test::Input::URL->new();
}
#
# Add all submitable widgets.
#
foreach my $w ($this->widgets_matching(sub {$_[ 0 ]->is_submitable}))
{
$input->add_widget($w);
}
return $input;
}
######################################################################
#
# ->_action_url
#
# Compute the action URL, which is what is going to be requested in response
# to a form submit. It does not contain the query part.
#
# We force re-anchor to the server if the action URL is not tied to it
# explicitely (e.g. ACTION="/cgi-bin/foo").
#
######################################################################
sub _action_url
{
my $this = shift;
my $uri = $this->page->uri; # The URL that generated this form
my $host_port = $uri->host_port;
require URI;
my $action = URI->new($this->action, "http");
$action->scheme("http");
$action->host_port($uri->host_port) unless defined $action->host_port;
return $action;
}
######################################################################
#
# ->_submits
#
# Compute list of submit buttons.
# Returns ref to this list.
#
######################################################################
sub _submits
{
my $this = shift;
my @submit = $this->buttons_matching(sub {$_[ 0 ]->is_submit});
return \@submit;
}
1;
=head1 NAME
CGI::Test::Form - Querying interface to CGI form widgets
=head1 SYNOPSIS
my $form = $page->forms->[0]; # first form in CGI::Test::Page
#
# Querying interface, to access form widgets
#
my @buttons = $form->button_list; # ->buttons would give list ref
my $radio_listref = $form->radios; # ->radios_list would give list
my $passwd_widget = $form->input_by_name("password");
my ($login, $passwd) = $form->input_by_name(qw(login password));
my @menus = $form->widgets_matching(sub { $_[0]->is_menu });
my @menus = $form->menu_list; # same as line above
my $rg = $form->radio_groups; # a CGI::Test::Form::Group or undef
#
# <FORM> attributes, as defined by HTML 4.0
#
my $encoding = $form->enctype;
my $action = $form->action;
my $method = $form->method;
my $name = $form->name;
my $accept = $form->accept;
my $accept_charset = $form->accept_charset;
#
# Miscellaneous
#
# Low-level, direct calls normally not needed
$form->reset;
my $new_page = $form->submit;
# Very low-level access
my $html_tree = $form->tree; # HTML::Element form tree
my $page = $form->page; # Page containing this form
#
# Garbage collection -- needed to break circular references
#
$form->delete;
=head1 DESCRIPTION
The C<CGI::Test::Form> class provides an interface to the content of
the CGI forms. Instances are automatically created by C<CGI::Test> when
it analyzes an HTML output from a GET/POST request and encounters such
beasts.
This class is really the basis of the C<CGI::Test> testing abilities:
it provides the necessary routines to query the CGI widgets present in the
form: buttons, input areas, menus, etc... Queries can be made by type, and
by name. There is also an interface to specifically access groupped widgets
like checkboxes and radio buttons.
All widgets returned by the queries are polymorphic objects, heirs of
C<CGI::Test::Form::Widget>. If the querying interface can be compared to
the human eye, enabling you to locate a particular graphical item on the
browser screen, the widget interface can be compared to the mouse and keyboard,
allowing you to interact with the located graphical components. Please
refer to L<CGI::Test::Form::Widget> for interaction details.
Apart from the widget querying interface, this class also offers a few
services to other C<CGI::Test> components, like handling of I<reset> and
I<submit> actions, which need not be used directly in practice.
Finally, it provides inspection of the <FORM> tag attributes (encoding
type, action, etc...) and, if you really need it, to the HTML tree of
the all <FORM> content. This interface is based on the C<HTML::Element>
class, which represents a tree node. The tree is shared with other
C<CGI::Test> components, it is not a private copy. See L<HTML::Element> if
you are not already familiar with it.
If memory is a problem, you must be aware that circular references are
used almost everywhere within C<CGI::Test>. Because Perl's garbage collector
cannot reclaim objects that are part of such a reference loop, you must
explicitely call the I<delete> method on C<CGI::Test::Form>.
Simply forgetting about the reference to that object is not enough.
Don't bother with it if your regression test scripts die quickly.
=head1 INTERFACE
The interface is mostly a querying interface. Most of the routines return
widget objects, via lists or list references. See L<CGI::Test::Form::Widget>
for details about the interface provided by widget objects, and the
classification.
The order of the widgets returned lists is the same as the order the widgets
appear in the HTML representation.
=head2 Type Querying Interface
There are two groups or routines: one group returns expanded lists, the
other returns list references. They are listed in the table below.
The I<Item Polymorphic Type> column refers to the polymorphic dynamic
type of items held within the list: each item is guaranteed to at least
be of that type, but can be a descendant. Types are listed in the
abridged form, and you have to prepend the string C<CGI::Test::Form::>
in front of them to get the real type.
Expanded List List Reference Item Polymorphic Type
------------- -------------- ----------------------
button_list buttons Widget::Button
checkbox_list checkboxes Widget::Box::Check
hidden_list hidden Widget::Hidden
input_list inputs Widget::Input
menu_list menus Widget::Menu
radio_list radios Widget::Box::Radio
submit_list submits Widget::Button::Submit
widget_list widgets Widget
For instance:
my @widgets = @{$form->widgets}; # heavy style
my @widgets = $form->widget_list; # light style
A given widget may appear in several lists, i.e.the above do not form a
partition over the widget set. For instance, a submit button would appear
in the C<widget_list> (which lists I<all> widgets), in the C<button_list>
and in the C<submit_list>.
=head2 Name Querying Interface
Those routine take a name or a list of names, and return the widgets whose
parameter name is B<exactly> the given name (string comparison). You may
query all widgets, or a particular class, like all buttons, or all input
fields.
There are two groups of routines:
=over 4
=item *
One group allows for multiple name queries, and returns a list of widgets,
one entry for each listed name. Some widgets like radio buttons may have
multiple instances bearing the same name, and in that case only one is
returned. When querying for one name, you are allowed to use scalar context:
my @hidden = $form->hidden_by_name("foo", "bar");
my ($context) = $form->hidden_by_name("context");
my $context = $form->hidden_by_name("context");
When no widget (of that particular type) bearing the requested name is found,
C<undef> is returned for that particular slot, so don't blindly make method
calls on each returned value.
We shall call that group of query routines the B<by-name> group.
=item *
The other group allows for a single name query, but returns a list of all
the widgets (of some particular type when not querying the whole widget list)
bearing that name.
my @hidden = $form->hidden_named("foo");
Don't assume that only radios and checkboxes can have multiple instances
bearing the same name.
We shall call that group of query routines the B<all-named> group.
=back
The available routines are listed in the table below. Note that I<by-name>
queries are singular, because there is at most one returned widget per name
asked, whereas I<all-named> queries are plural, where possible.
The I<Item Polymorphic Type> column refers to the polymorphic dynamic
type of items held within the list: each defined item is guaranteed to at
least be of that type, but can be a descendant. Types are listed in the
abridged form, and you have to prepend the string C<CGI::Test::Form::>
in front of them to get the real type.
By-Name Queries All-Named Queries Item Polymorphic Type
---------------- ----------------- ----------------------
button_by_name buttons_named Widget::Button
checkbox_by_name checkboxes_named Widget::Box::Check
hidden_by_name hidden_named Widget::Hidden
input_by_name inputs_named Widget::Input
menu_by_name menus_named Widget::Menu
radio_by_name radios_named Widget::Box::Radio
submit_by_name submits_named Widget::Button::Submit
widget_by_name widgets_named Widget
=head2 Match Querying Interface
This is a general interface, which invokes a matching callback on each
widget of a particular category. The signature of the matching routines is:
my @matching = $form->widgets_matching(sub {code}, $arg);
and the callback is invoked as:
callback($widget, $arg);
A widget is kept if, and only if, the callback returns true. Be sure to
write your callback so that is only uses calls that apply to the particular
widget. When you know you're matching on menu widgets, you can call
menu-specific features, but should you use that same callback for buttons,
you would get a runtime error.
Each matching routine returns a list of matching widgets. Using the $arg
parameter is optional, and should be avoided unless you have no other choice,
so as to be as stateless as possible.
The following table lists the available matching routines, along with the
polymorphic widget type to be expected in the callback. As usual, you must
prepend the string C<CGI::Test::Form::> to get the real type.
Matching Routine Item Polymorphic Type
------------------- ---------------------
buttons_matching Widget::Button
checkboxes_matching Widget::Box::Check
hidden_matching Widget::Hidden
inputs_matching Widget::Input
menus_matching Widget::Menu
radios_matching Widget::Box::Radio
submits_matching Widget::Button::Submit
widgets_matching Widget
For instance:
my @menus = $form->widgets_matching(sub { $_[0]->is_menu });
my @color = $form->widgets_matching(
sub { $_[0]->is_menu && $_[0]->name eq "color" }
);
is an inefficient way of saying:
my @menus = $form->menu_list;
my @color = $form->menus_matching(sub { $_[0]->name eq "color" });
and the latter can further be rewritten as:
my @color = $form->menus_named("color");
=head2 Form Interface
This provides an interface to get at the attributes of the <FORM> tag.
For instance:
my $enctype = $form->enctype;
to get at the encoding type of that particular form.
The following attributes are available:
accept
accept_charset
action
enctype
method
name
as defined by HTML 4.0.
=head2 Group Querying Interface
There are two kinds of widgets that are architecturally groupped, meaning
more that one instance of that widget can bear the same name: radio buttons
and checkboxes (although you may have a single standalone checkbox).
All radio buttons and checkboxes defined in a form are automatically
inserted into a group of their own, which is an instance of the
C<CGI::Test::Form::Group> class. This class contains all the defined
groups for a particular kind. The routines:
checkbox_groups
radio_groups
give you access to the C<CGI::Test::Form::Group> container. Both routines
may return C<undef> when there is no checkbox or radio button in the form.
See L<CGI::Test::Form::Group> for its querying interface.
=head2 Memory Cleanup
You B<must> call the I<delete> method to break the circular references
if you wish to dispose of the object.
=head2 Internal Interface
The following routines are available internally:
=over 4
=item reset
Reset the form state, restoring all the controls to the value they
had upon entry.
=item submit
Submit the form, returning a C<CGI::Test::Page> reply.
=back
=head1 AUTHORS
The original author is Raphael Manfredi.
Steven Hilton was long time maintainer of this module.
Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
=head1 SEE ALSO
CGI::Test(3), CGI::Test::Form::Widget(3), CGI::Test::Form::Group(3),
CGI::Test::Page(3), HTML::Element(3).
=cut
|