diff --git a/lib/WebGUI/Shop/ShipDriver.pm b/lib/WebGUI/Shop/ShipDriver.pm index 148ac2c40..f4e2ff0e3 100644 --- a/lib/WebGUI/Shop/ShipDriver.pm +++ b/lib/WebGUI/Shop/ShipDriver.pm @@ -6,6 +6,7 @@ use Class::InsideOut qw{ :std }; use Carp qw(croak); use Tie::IxHash; use WebGUI::International; +use WebGUI::HTMLForm; use JSON; =head1 NAME @@ -184,7 +185,32 @@ sub get { #------------------------------------------------------------------- -=head2 getID ( ) +=head2 getEditForm ( ) + +Dynamically generate an HTMLForm based on the contents +of the definition sub, and return the form. +=cut + +sub getEditForm { + my $self = shift; + my $definition = $self->definition($self->session); + my $form = WebGUI::HTMLForm->new($self->session); + $form->submit; + $form->hidden( + name => 'shipperId', + value => $self->getId, + ); + $form->hidden( + name => 'className', + value => $self->className, + ); + $form->dynamicForm($definition, 'fields', $self); + return $form; +} + +#------------------------------------------------------------------- + +=head2 getId ( ) Returns the shipperId. This is an alias for shipperId provided since a lot of WebGUI classes have a getId method. diff --git a/t/Shop/ShipDriver.t b/t/Shop/ShipDriver.t index 1da8f8966..a7a0db5db 100644 --- a/t/Shop/ShipDriver.t +++ b/t/Shop/ShipDriver.t @@ -19,6 +19,8 @@ use lib "$FindBin::Bin/../lib"; use Test::More; use Test::Deep; use JSON; +use HTML::Form; + use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; @@ -29,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 24; +my $tests = 28; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -164,6 +166,55 @@ is($driver->get('label'), 'Slow and dangerous', 'get the label entry from the # ####################################################################### +my $form = $driver->getEditForm; + +isa_ok($form, 'WebGUI::HTMLForm', 'getEditForm returns an HTMLForm object'); + +my $html = $form->print; + +##Any URL is fine, really +my @forms = HTML::Form->parse($html, 'http://www.webgui.org'); +is (scalar @forms, 1, 'getEditForm generates just 1 form'); + +my @inputs = $forms[0]->inputs; +is (scalar @inputs, 5, 'getEditForm: the form has 5 controls'); + +my @interestingFeatures; +foreach my $input (@inputs) { + my $name = $input->name; + my $type = $input->type; + push @interestingFeatures, { name => $name, type => $type }; +} + +cmp_deeply( + \@interestingFeatures, + [ + { + name => undef, + type => 'submit', + }, + { + name => 'shipperId', + type => 'hidden', + }, + { + name => 'className', + type => 'hidden', + }, + { + name => 'label', + type => 'text', + }, + { + name => 'enabled', + type => 'radio', + }, + ], + 'getEditForm made the correct form with all the elements' + +); + + ####################################################################### # # new