diff --git a/docs/migration.txt b/docs/migration.txt index d10f699f4..b41f754b8 100644 --- a/docs/migration.txt +++ b/docs/migration.txt @@ -66,6 +66,11 @@ WebGUI::Session::ErrorHandler ErrorHandler has been changed to "log" in all circumstances. $session->errorHandler no longer exists, use $session->log. WebGUI::Session::ErrorHandler no longer exists, use WebGUI::Session::Log +WebGUI::Shop::PayDriver +======================= + +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/Pay.pm b/lib/WebGUI/Shop/Pay.pm index 839fb2155..d1f6ec4fa 100644 --- a/lib/WebGUI/Shop/Pay.pm +++ b/lib/WebGUI/Shop/Pay.pm @@ -328,7 +328,7 @@ sub www_manage { .WebGUI::Form::hidden($session, { name => "shop", value => "pay" }) .WebGUI::Form::hidden($session, { name => "method", value => "addPaymentGateway" }) .WebGUI::Form::selectBox($session, { name => "className", options => $self->getDrivers }) - .WebGUI::Form::submit($session, { value => $i18n->get("add payment method") }) + .WebGUI::Form::submit($session, { name => "add", value => $i18n->get("add payment method") }) .WebGUI::Form::formFooter($session); # Add a row with edit/delete buttons for each payment gateway. @@ -339,7 +339,7 @@ sub www_manage { .WebGUI::Form::hidden($session, { name => "shop", value => "pay" }) .WebGUI::Form::hidden($session, { name => "method", value => "deletePaymentGateway" }) .WebGUI::Form::hidden($session, { name => "paymentGatewayId", value => $paymentGateway->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) # Edit button for current payment gateway @@ -348,7 +348,7 @@ sub www_manage { .WebGUI::Form::hidden($session, { name => "method", value => "do" }) .WebGUI::Form::hidden($session, { name => "do", value => "edit" }) .WebGUI::Form::hidden($session, { name => "paymentGatewayId", value => $paymentGateway->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) # Append payment gateway label diff --git a/lib/WebGUI/Shop/PayDriver.pm b/lib/WebGUI/Shop/PayDriver.pm index e1fa31902..fd472248d 100644 --- a/lib/WebGUI/Shop/PayDriver.pm +++ b/lib/WebGUI/Shop/PayDriver.pm @@ -318,30 +318,28 @@ sub getCart { #------------------------------------------------------------------- -=head2 getDoFormTags ( $method, $htmlForm ) +=head2 getDoFormTags ( $method, $fb ) -Returns a string containing the required form fields for doing a www_do method call. If an HTMLForm object is +Returns a string containing the required form fields for doing a www_do method call. If a FormBuilder object is passed the fields are automatically added to it. In that case no form tags a returned by this method. -=head3 $htmlForm +=head3 $fb -The HTMLForm object you want to add the fields to. This is optional. +The FormBuilder object you want to add the fields to. This is optional. =cut sub getDoFormTags { my $self = shift; my $doMethod = shift; - my $htmlForm = shift; + my $fb = shift; my $session = $self->session; - if ($htmlForm) { - $htmlForm->hidden(name => 'shop', value => 'pay'); - $htmlForm->hidden(name => 'method', value => 'do'); - $htmlForm->hidden(name => 'do', value => $doMethod); - $htmlForm->hidden(name => 'paymentGatewayId', value => $self->getId); - - return undef; + if ($fb) { + $fb->addField( "hidden", name => 'shop', value => 'pay'); + $fb->addField( "hidden", name => 'method', value => 'do'); + $fb->addField( "hidden", name => 'do', value => $doMethod); + $fb->addField( "hidden", name => 'paymentGatewayId', value => $self->getId); } else { return WebGUI::Form::hidden($session, { name => 'shop', value => 'pay' }) @@ -363,24 +361,23 @@ Returns the configuration form for the options of this plugin. sub getEditForm { my $self = shift; - my $form = WebGUI::HTMLForm->new($self->session); - $form->submit; + my $form = WebGUI::FormBuilder->new($self->session); + $form->addField( "submit", name => "submit" ); $self->getDoFormTags('editSave', $form); - $form->hidden( + $form->addField( "hidden", name => 'className', value => $self->className, ); - 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; } @@ -651,9 +648,10 @@ sub www_edit { return $session->privilege->insufficient() unless $session->user->isAdmin; my $form = $self->getEditForm; - $form->submit; + $form->addField( 'csrfToken', name => 'csrfToken' ); + $form->addField( "submit", name => "submit" ); - return $admin->getAdminConsole->render($form->print, $i18n->get('payment methods')); + return '

' . $i18n->get('payment methods') . '

' . $form->toHtml; } #------------------------------------------------------------------- diff --git a/t/Shop/PayDriver.t b/t/Shop/PayDriver.t index afb62ef0e..6bd09ae37 100644 --- a/t/Shop/PayDriver.t +++ b/t/Shop/PayDriver.t @@ -27,6 +27,7 @@ use WebGUI::Shop::Credit; use WebGUI::Shop::PayDriver; use Clone; use WebGUI::User; +use WebGUI::Test::Mechanize; #---------------------------------------------------------------------------- # Init @@ -194,16 +195,16 @@ isa_ok ($cart, 'WebGUI::Shop::Cart', 'getCart returns an instantiated WebGU my $form = $driver->getEditForm; -isa_ok ($form, 'WebGUI::HTMLForm', 'getEditForm returns an HTMLForm object'); +isa_ok ($form, 'WebGUI::FormBuilder', 'getEditForm returns an 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, 11, 'getEditForm: the form has 11 controls'); +is (scalar @inputs, 10, 'getEditForm: the form has 10 controls'); my @interestingFeatures; foreach my $input (@inputs) { @@ -216,11 +217,7 @@ cmp_deeply( \@interestingFeatures, [ { - name => 'webguiCsrfToken', - type => 'hidden', - }, - { - name => undef, + name => 'submit', type => 'submit', }, { @@ -264,6 +261,75 @@ cmp_deeply( ); +# Try to add a new PayDriver +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=pay;method=manage' ); + +# Click the Add Payment button +$mech->form_with_fields( 'className', 'add' ); +$mech->select( 'className' => 'WebGUI::Shop::PayDriver::Cash' ); +$mech->click_ok( 'add' ); + +# Fill in the form +$mech->submit_form_ok({ + fields => { + label => 'Authority Scrip', + enabled => '1', + }, + }, + "add a new gateway", +); + +# Payment method added! +$mech->content_contains( 'Authority Scrip', 'new label shows up in manage screen' ); + +# Find our new payment gateway +my $paydriverId; +for my $row ( @{ $session->db->buildArrayRefOfHashRefs( 'SELECT * FROM paymentGateway' ) } ) { + my $options = JSON->new->decode( $row->{options} ); + if ( $options->{label} eq 'Authority Scrip' ) { + $paydriverId = $row->{paymentGatewayId}; + } +} +ok( my $paydriver = WebGUI::Shop::PayDriver->new( $mech->session, $paydriverId ), 'paydriver can be instanced' ); +WebGUI::Test::addToCleanup( $paydriver ); +is( $paydriver->label, 'Authority Scrip', 'label set correctly' ); +ok( $paydriver->enabled, 'driver is enabled' ); + +# Edit an existing PayDriver +# 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( 'paymentGatewayId' ) eq $paydriverId ) { + last; + } + $formNumber++; +} +$mech->submit_form_ok({ + form_number => $formNumber, + }, 'click edit button', +); + +# Fill in the form +$mech->submit_form_ok({ + fields => { + label => 'Free Luna Dollars', + enabled => 1, + }, + }, + "edit an existing method", +); + +# Payment method edited! +$mech->content_contains( 'Free Luna Dollars', 'new label shows up in manage screen' ); +diag( $mech->content ); +ok( my $paydriver = WebGUI::Shop::PayDriver->new( $mech->session, $paydriverId ), 'paydriver can be instanced' ); +is( $paydriver->label, 'Free Luna Dollars', 'label set correctly' ); +ok( $paydriver->enabled, 'driver is enabled' ); ####################################################################### #