From 1d8b1b1b3a0d282d257708c19c334ee36f4718f9 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Wed, 16 Feb 2011 14:04:13 -0600 Subject: [PATCH] migrate ship getEditForm to FormBuilder --- docs/migration.txt | 5 +++ lib/WebGUI/Shop/Ship.pm | 6 +-- lib/WebGUI/Shop/ShipDriver.pm | 32 ++++++------- t/Shop/ShipDriver.t | 85 +++++++++++++++++++++++++++++++---- 4 files changed, 100 insertions(+), 28 deletions(-) diff --git a/docs/migration.txt b/docs/migration.txt index b41f754b8..cd3ce1f50 100644 --- a/docs/migration.txt +++ b/docs/migration.txt @@ -71,6 +71,11 @@ WebGUI::Shop::PayDriver getEditForm now returns a WebGUI::FormBuilder object +WebGUI::Shop::ShipDriver +======================== + +getEditForm now returns a WebGUI::FormBuilder object + WebGUI::Utility =============== This module has been removed. It had many functions that weren't used, and others have better replacements. diff --git a/lib/WebGUI/Shop/Ship.pm b/lib/WebGUI/Shop/Ship.pm index 6a4886d32..8c87b835e 100644 --- a/lib/WebGUI/Shop/Ship.pm +++ b/lib/WebGUI/Shop/Ship.pm @@ -273,7 +273,7 @@ sub www_manage { .WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"}) .WebGUI::Form::hidden($session, {name=>"method", value=>"addDriver"}) .WebGUI::Form::selectBox($session, {name=>"className", options=>$self->getDrivers}) - .WebGUI::Form::submit($session, {value=>$i18n->get("add shipper")}) + .WebGUI::Form::submit($session, {name => 'add', value=>$i18n->get("add shipper")}) .WebGUI::Form::formFooter($session); my $hasShipper = 0; foreach my $shipper (@{$self->getShippers}) { @@ -282,14 +282,14 @@ sub www_manage { .WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"}) .WebGUI::Form::hidden($session, {name=>"method", value=>"deleteDriver"}) .WebGUI::Form::hidden($session, {name=>"driverId", value=>$shipper->getId}) - .WebGUI::Form::submit($session, {value=>$i18n->get("delete"), extras=>'class="backwardButton"'}) + .WebGUI::Form::submit($session, {name => 'delete', value=>$i18n->get("delete"), extras=>'class="backwardButton"'}) .WebGUI::Form::formFooter($session) .WebGUI::Form::formHeader($session, {extras=>'style="float: left;"'}) .WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"}) .WebGUI::Form::hidden($session, {name=>"method", value=>"do"}) .WebGUI::Form::hidden($session, {name=>"do", value=>"edit"}) .WebGUI::Form::hidden($session, {name=>"driverId", value=>$shipper->getId}) - .WebGUI::Form::submit($session, {value=>$i18n->get("edit"), extras=>'class="normalButton"'}) + .WebGUI::Form::submit($session, {name => 'edit', value=>$i18n->get("edit"), extras=>'class="normalButton"'}) .WebGUI::Form::formFooter($session) .' ' .$shipper->get("label") diff --git a/lib/WebGUI/Shop/ShipDriver.pm b/lib/WebGUI/Shop/ShipDriver.pm index 1586d7cbf..390cbc08f 100644 --- a/lib/WebGUI/Shop/ShipDriver.pm +++ b/lib/WebGUI/Shop/ShipDriver.pm @@ -5,7 +5,7 @@ use strict; use Carp qw(croak); use Tie::IxHash; use WebGUI::International; -use WebGUI::HTMLForm; +use WebGUI::FormBuilder; use WebGUI::Exception::Shop; use JSON; @@ -199,27 +199,26 @@ Returns the configuration form for the options of this plugin. sub getEditForm { my $self = shift; - - my $form = WebGUI::HTMLForm->new($self->session); - $form->submit; - - $form->hidden(name => 'shop',value => "ship"); - $form->hidden(name => 'method',value => "do"); - $form->hidden(name => 'do',value => "editSave"); - $form->hidden( + + my $form = WebGUI::FormBuilder->new($self->session, action => $self->session->url->page ); + $form->addField( "submit", name => "submit" ); + + $form->addField( "hidden", name => 'shop', value => "ship"); + $form->addField( "hidden", name => 'method', value => "do"); + $form->addField( "hidden", name => 'do', value => "editSave"); + $form->addField( "hidden", name => 'driverId', value => $self->getId, ); - tie my %form_options, 'Tie::IxHash'; foreach my $property_name ($self->getProperties) { my $property = $self->meta->find_attribute_by_name($property_name); - $form_options{$property_name} = { + my %form_options = ( + name => $property_name, value => $self->$property_name, %{ $self->getFormProperties($property_name)}, - }; + ); + $form->addField( delete $form_options{fieldType}, %form_options ); } - my $definition = [ { properties => \%form_options }, ]; - $form->dynamicForm($definition, 'properties', $self); return $form; } @@ -329,8 +328,9 @@ sub www_edit { my $admin = WebGUI::Shop::Admin->new($session); my $i18n = WebGUI::International->new($session, "Shop"); my $form = $self->getEditForm; - $form->submit; - return $admin->getAdminConsole->render($form->print, $i18n->get("shipping methods")); + $form->addField( "submit", name => "submit" ); + $form->addField( 'csrfToken', name => 'webguiCsrfToken' ); + return '

' . $i18n->get("shipping methods") . '

' . $form->toHtml; } #------------------------------------------------------------------- diff --git a/t/Shop/ShipDriver.t b/t/Shop/ShipDriver.t index e66af5392..cd07887af 100644 --- a/t/Shop/ShipDriver.t +++ b/t/Shop/ShipDriver.t @@ -22,6 +22,7 @@ use HTML::Form; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use WebGUI::Shop::ShipDriver; +use WebGUI::Test::Mechanize; use Clone; #---------------------------------------------------------------------------- @@ -31,7 +32,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 32; +plan tests => 48; #---------------------------------------------------------------------------- # put your tests here @@ -127,16 +128,16 @@ is($driver->get('label'), 'Slow and dangerous', 'get returns a safe copy of the my $form = $driver->getEditForm; -isa_ok($form, 'WebGUI::HTMLForm', 'getEditForm returns an HTMLForm object'); +isa_ok($form, 'WebGUI::FormBuilder', 'getEditForm returns a FormBuilder object'); -my $html = $form->print; +my $html = $form->toHtml; ##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, 10, 'getEditForm: the form has 10 controls'); +is (scalar @inputs, 9, 'getEditForm: the form has 10 controls'); my @interestingFeatures; foreach my $input (@inputs) { @@ -149,11 +150,7 @@ cmp_deeply( \@interestingFeatures, [ { - name => 'webguiCsrfToken', - type => 'hidden', - }, - { - name => undef, + name => 'submit', type => 'submit', }, { @@ -194,6 +191,76 @@ cmp_deeply( ); +my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->file ); +$mech->get_ok( '/' ); +$mech->session->user({ userId => 3 }); + +# Get to the management screen +$mech->get_ok( '?shop=ship;method=manage' ); + +# Click the Add Shipping button +$mech->form_with_fields( 'className', 'add' ); +$mech->select( 'className' => 'WebGUI::Shop::ShipDriver::FlatRate' ); +$mech->click_ok( 'add' ); + +# Fill in the form +$mech->submit_form_ok({ + fields => { + label => 'Blue Box', + enabled => 1, + flatFee => 5.00, + }, + }, + "add a new driver", +); + +# Shipping method added! +$mech->content_contains( 'Blue Box', 'new shipping label shows up in manage screen' ); + +# Find our new shipping driver +my $shipdriverId; +for my $row ( @{ $session->db->buildArrayRefOfHashRefs( 'SELECT * FROM shipper' ) } ) { + my $options = JSON->new->decode( $row->{options} ); + if ( $options->{label} eq 'Blue Box' ) { + $shipdriverId = $row->{shipperId}; + } +} +ok( my $shipdriver = WebGUI::Shop::ShipDriver::FlatRate->new( $mech->session, $shipdriverId ), 'shipdriver can be instanced' ); +WebGUI::Test::addToCleanup( $shipdriver ); +is( $shipdriver->label, 'Blue Box', 'label set correctly' ); +ok( $shipdriver->enabled, 'driver is enabled' ); +is( $shipdriver->flatFee, 5.00, 'flat fee added correctly' ); + +# Edit an existing ShipDriver +# Find the right form and click the Edit button +my $formNumber = 1; +for my $form ( $mech->forms ) { + if ( $form->value( 'do' ) eq 'edit' && $form->value( 'driverId' ) eq $shipdriverId ) { + last; + } + $formNumber++; +} +$mech->submit_form_ok({ + form_number => $formNumber, + }, 'click edit button', +); + +# Fill in the form +$mech->submit_form_ok({ + fields => { + label => "Brown Box", + } + }, + "edit shipping method", +); + +# Shipping method edited! +$mech->content_contains( 'Brown Box', 'new label shows up in manage screen' ); +ok( my $shipdriver = WebGUI::Shop::ShipDriver::FlatRate->new( $mech->session, $shipdriverId ), 'shipdriver can be instanced' ); +is( $shipdriver->label, 'Brown Box', 'label set correctly' ); +ok( $shipdriver->enabled, 'driver is enabled' ); +is( $shipdriver->flatFee, 5.00, 'flat fee still only $5' ); + ####################################################################### # # new