From b0da37ec15c76b960abda2b003e8457ba0123b5e Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Wed, 4 May 2011 15:33:04 -0500 Subject: [PATCH] fix EMSSubmissionForm and all its tests --- lib/WebGUI/Asset/EMSSubmission.pm | 58 ++++++++++++----------- lib/WebGUI/Asset/EMSSubmissionForm.pm | 53 +++++++++++---------- lib/WebGUI/Form/CheckList.pm | 2 +- lib/WebGUI/Form/Control.pm | 2 +- lib/WebGUI/Form/ReadOnly.pm | 16 ++++++- lib/WebGUI/FormBuilder/Role/HasFields.pm | 2 +- lib/WebGUI/FormBuilder/Role/HasObjects.pm | 2 +- t/Asset/EMSSubmissionForm.t | 10 ++-- 8 files changed, 81 insertions(+), 64 deletions(-) diff --git a/lib/WebGUI/Asset/EMSSubmission.pm b/lib/WebGUI/Asset/EMSSubmission.pm index b1c74f18d..e517e9510 100644 --- a/lib/WebGUI/Asset/EMSSubmission.pm +++ b/lib/WebGUI/Asset/EMSSubmission.pm @@ -19,6 +19,10 @@ use Moose; use WebGUI::Definition::Asset; use WebGUI::Asset; use WebGUI::International; +use WebGUI::Form::Combo; +use WebGUI::Form::SelectBox; +use WebGUI::Form::CheckList; + extends 'WebGUI::Asset'; define tableName => 'EMSSubmission'; define assetNae => ['assetName', 'Asset_EMSSubmission']; @@ -204,17 +208,17 @@ sub drawLocationField { my $ems = $self->ems; my $options = { map { $_ => $_ } ( @{ $ems->getSubmissionLocations || [ $ems->getLocations ] } ) } ; if( $ems->isRegistrationStaff ) { - return WebGUI::Form::combo($self->session, { + return WebGUI::Form::Combo->new($self->session, { name => 'location', value => $self->get('location'), options => $options, - }); + })->toHtml; } else { - return WebGUI::Form::selectBox($self->session, { + return WebGUI::Form::SelectBox->new($self->session, { name => 'location', value => $self->get('location'), options => $options, - }); + })->toHtml; } } @@ -228,12 +232,12 @@ Draws the field for the relatedBadgeGroups property. sub drawRelatedBadgeGroupsField { my ($self, $params) = @_; - return WebGUI::Form::checkList($self->session, { + return WebGUI::Form::CheckList->new($self->session, { name => $params->{name}, value => $self->get($params->{name}), vertical => 1, options => $self->getParent->getParent->getBadgeGroups, - }); + })->toHtml; } #------------------------------------------------------------------- @@ -250,12 +254,12 @@ sub drawRelatedRibbonsField { foreach my $ribbon (@{$self->getParent->getParent->getRibbons}) { $ribbons{$ribbon->getId} = $ribbon->getTitle; } - return WebGUI::Form::checkList($self->session, { + return WebGUI::Form::CheckList->new($self->session, { name => $params->{name}, value => $self->get($params->{name}), vertical => 1, options => \%ribbons, - }); + })->toHtml; } #------------------------------------------------------------------- @@ -271,11 +275,11 @@ sub drawStatusField { for my $key ( qw/pending created failed/ ) { delete $options->{$key} unless $currentStatus eq $key; } - return WebGUI::Form::SelectBox($self->session, { + return WebGUI::Form::SelectBox->new($self->session, { name => 'submissionStatus', value => $currentStatus, options => $options, - }); + })->toHtml; } @@ -356,23 +360,23 @@ sub www_editSubmission { my $newform = WebGUI::FormBuilder->new($session,action => $url); $newform->addField( "hidden", name => 'assetId', value => $assetId); my $formDescription = $parent->getFormDescription; - my @defs = reverse @{__PACKAGE__->definition($session)}; my @fieldNames = qw/title submissionStatus startDate duration seatsAvailable location description/; my $fields; - for my $def ( @defs ) { - my $properties = $def->{properties}; - for my $fieldName ( keys %$properties ) { - if( defined $formDescription->{$fieldName} ) { - $fields->{$fieldName} = { %{$properties->{$fieldName}} }; # a simple first level copy - if( $fieldName eq 'description' ) { - $fields->{description}{height} = 200; - $fields->{description}{width} = 350; - } - $fields->{$fieldName}{fieldId} = $fieldName; - $fields->{$fieldName}{name} = $fieldName; - $fields->{$fieldName}{value} = $self->get($fieldName) if $self; - } - } + my $class = 'WebGUI::Asset::EMSSubmission'; + foreach my $fieldName (@fieldNames) { + my $attr = $class->meta->find_attribute_by_name( $fieldName ); + $fields->{$fieldName} = { + fieldId => $fieldName, + name => $fieldName, + fieldType => $attr->fieldType, + noFormPost => $attr->noFormPost, + %{ $class->getFormProperties( $session, $fieldName ) }, + }; + if( $fieldName eq 'description' ) { + $fields->{description}{height} = 200; + $fields->{description}{width} = 350; + } + $fields->{$fieldName}{value} = $self->get($fieldName) if $self; } # add the meta field for my $metaField ( @{$parent->getParent->getEventMetaFields} ) { @@ -397,11 +401,11 @@ sub www_editSubmission { my $drawMethod = __PACKAGE__ . '::' . $field->{customDrawMethod}; if ($asset->can( $drawMethod )) { $field->{value} = $asset->$drawMethod($field); - delete $field->{name}; # don't want readOnly to generate a hidden field + $field->{addHidden} = 0; $field->{fieldType} = "readOnly"; } - $newform->addField( "dynamicField", %$field); + $newform->addField( $field->{fieldType}, %$field); } else { my $value; # TODO see that the data gets formatted diff --git a/lib/WebGUI/Asset/EMSSubmissionForm.pm b/lib/WebGUI/Asset/EMSSubmissionForm.pm index cc51f6825..9ea685c86 100644 --- a/lib/WebGUI/Asset/EMSSubmissionForm.pm +++ b/lib/WebGUI/Asset/EMSSubmissionForm.pm @@ -238,17 +238,16 @@ sub www_editSubmissionForm { $newform->addField( "hidden",name => 'assetId', value => $assetId ); my @fieldNames = qw/title description startDate duration seatsAvailable location/; my $fields; - my @defs = reverse @{ WebGUI::Asset::EMSSubmission->definition($session) }; - - for my $def (@defs) { - foreach my $fieldName (@fieldNames) { - my $properties = $def->{properties}; - if ( defined $properties->{$fieldName} ) { - $fields->{$fieldName} = { %{ $properties->{$fieldName} } }; # a simple first level copy - # field definitions don't contain their own name, we will need it later on - $fields->{$fieldName}{fieldId} = $fieldName; - } - } + my $class = 'WebGUI::Asset::EMSSubmission'; + foreach my $fieldName (@fieldNames) { + my $attr = $class->meta->find_attribute_by_name( $fieldName ); + $fields->{$fieldName} = { + fieldId => $fieldName, + name => $fieldName, + fieldType => $attr->fieldType, + noFormPost => $attr->noFormPost, + %{ $class->getFormProperties( $session, $fieldName ) }, + }; } for my $metaField ( @{ $parent->getEventMetaFields } ) { push @fieldNames, $metaField->{fieldId}; @@ -258,21 +257,23 @@ sub www_editSubmissionForm { $fields->{ $metaField->{fieldId} }{hoverHelp} = $metaField->{helpText}; } $newform->addField( "hidden", name => 'fieldNames', value => join( ' ', @fieldNames ) ); - @defs = reverse @{ WebGUI::Asset::EMSSubmissionForm->definition($session) }; - for my $def (@defs) { - my $properties = $def->{properties}; - for my $fieldName ( - qw/title menuTitle url description canSubmitGroupId daysBeforeCleanup - deleteCreatedItems submissionDeadline pastDeadlineMessage/ - ) - { - if ( defined $properties->{$fieldName} ) { - my %fieldParams = %{ $properties->{$fieldName} }; - $fieldParams{name} = $fieldName; - $fieldParams{value} = $params->{$fieldName} || $self ? $self->get($fieldName) : undef; - $newform->addField( "dynamicField", %fieldParams); - } - } + $class = 'WebGUI::Asset::EMSSubmissionForm'; + for my $fieldName ( + qw/title menuTitle url description canSubmitGroupId daysBeforeCleanup + deleteCreatedItems submissionDeadline pastDeadlineMessage/ + ) + { + my $attr = $class->meta->find_attribute_by_name( $fieldName ); + next unless $attr; + my %fieldParams = ( + fieldId => $fieldName, + name => $fieldName, + fieldType => $attr->fieldType, + noFormPost => $attr->noFormPost, + %{ $class->getFormProperties( $session, $fieldName ) }, + value => $params->{$fieldName} || $self ? $self->get($fieldName) : undef, + ); + $newform->addField( $attr->fieldType, %fieldParams); } my $formDescription = $params->{formDescription} || $self ? $self->getFormDescription : {}; diff --git a/lib/WebGUI/Form/CheckList.pm b/lib/WebGUI/Form/CheckList.pm index 83fe6ee08..93a0a6a4b 100644 --- a/lib/WebGUI/Form/CheckList.pm +++ b/lib/WebGUI/Form/CheckList.pm @@ -162,7 +162,7 @@ sub toHtml { $self->headTags; my $session = $self->session; my $output = '
'; - $output .= WebGUI::Form::Hidden($session, { name => $self->privateName('isIn'), value => 1, }); + $output .= WebGUI::Form::Hidden->new($session, { name => $self->privateName('isIn'), value => 1, })->toHtml; my $alignment = $self->alignmentSeparator; # Add the select all button diff --git a/lib/WebGUI/Form/Control.pm b/lib/WebGUI/Form/Control.pm index 8220bc5a4..b5f1a6927 100644 --- a/lib/WebGUI/Form/Control.pm +++ b/lib/WebGUI/Form/Control.pm @@ -156,7 +156,7 @@ sub definition { my $definition = shift || []; push(@{$definition}, { name=>{ - defaultValue=>undef + defaultValue=>'' }, value=>{ defaultValue=>undef diff --git a/lib/WebGUI/Form/ReadOnly.pm b/lib/WebGUI/Form/ReadOnly.pm index 417a166e2..eb8aa544b 100644 --- a/lib/WebGUI/Form/ReadOnly.pm +++ b/lib/WebGUI/Form/ReadOnly.pm @@ -37,6 +37,18 @@ The following methods are specifically available from this class. Check the supe =cut +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift || []; + push(@{$definition}, { + # Should we show the hidden field too? + addHidden => { + defaultValue => 1, + }, + }); + return $definition; +} #------------------------------------------------------------------- =head2 getName ( session ) @@ -73,7 +85,7 @@ Renders the value and a hidden input type if a "name" attribute was specified. sub toHtml { my $self = shift; my $out = $self->getOriginalValue; - if ($self->get('name') ne '') { + if ($self->get('addHidden') && $self->get('name') ne '') { $out .= $self->toHtmlAsHidden; } return $out; @@ -89,7 +101,7 @@ Outputs nothing unless a "name" attribute was specified. sub toHtmlAsHidden { my $self = shift; - if ($self->get('name') ne '') { + if ($self->get('addHidden') && $self->get('name') ne '') { return $self->SUPER::toHtmlAsHidden; } return undef; diff --git a/lib/WebGUI/FormBuilder/Role/HasFields.pm b/lib/WebGUI/FormBuilder/Role/HasFields.pm index 2295e60aa..b37f7bdc7 100644 --- a/lib/WebGUI/FormBuilder/Role/HasFields.pm +++ b/lib/WebGUI/FormBuilder/Role/HasFields.pm @@ -67,7 +67,7 @@ sub addField { push @{$self->fields}, $field; $self->addObject( $field ); - $self->{_fieldsByName}{ $field->get('name') } = $field; # TODO: Must allow multiple fields per name + $self->{_fieldsByName}{ $field->get('name') || '' } = $field; # TODO: Must allow multiple fields per name return $field; } diff --git a/lib/WebGUI/FormBuilder/Role/HasObjects.pm b/lib/WebGUI/FormBuilder/Role/HasObjects.pm index e27bff45d..5fcf7afd3 100644 --- a/lib/WebGUI/FormBuilder/Role/HasObjects.pm +++ b/lib/WebGUI/FormBuilder/Role/HasObjects.pm @@ -122,7 +122,7 @@ sub toTemplateVars { } # Form field objects when ( $_->isa( 'WebGUI::Form::Control' ) ) { - my $name = $obj->get('name'); + my $name = $obj->get('name') || ''; $props = $obj->toTemplateVars; # Add the whole field to the vars $props->{ field } = $obj->toHtmlWithWrapper; diff --git a/t/Asset/EMSSubmissionForm.t b/t/Asset/EMSSubmissionForm.t index 0f841c01d..3dcc88027 100644 --- a/t/Asset/EMSSubmissionForm.t +++ b/t/Asset/EMSSubmissionForm.t @@ -17,6 +17,7 @@ use strict; use Test::More; use Test::Deep; use Test::Warn; +use Test::Exception; use HTML::Form; use JSON; use WebGUI::Test; # Must use this before any other WebGUI modules @@ -309,7 +310,7 @@ cmp_deeply($sub1->get('comments')->[0],{ comment => 'this is a test comment', rating => 0, date => re( qr/\d{10}/ ), - ip => undef, + ip => ignore(), }, "successfully added comment" ); $sub1->update({ @@ -361,8 +362,7 @@ $cleanupSubmissions->reset; is($cleanupSubmissions->run, 'complete', 'cleanup complete'); is($cleanupSubmissions->run, 'done', 'cleanup done'); -$sub2 = WebGUI::Asset->newById($session, $sub2Id); -is( $sub2, undef, 'submission deleted'); +dies_ok { WebGUI::Asset->newById($session, $sub2Id) } 'submission deleted'; } # end of workflow skip @@ -419,10 +419,10 @@ my $expected = { }, 'description' => undef, '_isValid' => 1, - 'deleteCreatedItems' => undef, + 'deleteCreatedItems' => '0', 'canSubmitGroupId' => '2', 'assetId' => 'new', - 'url' => undef, + 'url' => '', 'daysBeforeCleanup' => '7', 'title' => 'Untitled', } ;