From ae7528b8ef78359395870d3265f6294b494c30d0 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Thu, 10 Feb 2011 16:34:03 -0600 Subject: [PATCH] migrate Thingy import from csv to FormBuilder --- lib/WebGUI/Asset/Wobject/Thingy.pm | 51 ++++++++++++------------ t/Asset/Wobject/Thingy.t | 62 +++++++++++++++++++++++++++++- t/supporting_collateral/thingy.csv | 2 + 3 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 t/supporting_collateral/thingy.csv diff --git a/lib/WebGUI/Asset/Wobject/Thingy.pm b/lib/WebGUI/Asset/Wobject/Thingy.pm index ad9446332..f0231961c 100644 --- a/lib/WebGUI/Asset/Wobject/Thingy.pm +++ b/lib/WebGUI/Asset/Wobject/Thingy.pm @@ -2836,8 +2836,11 @@ sub www_import { while (my $field = $fields->hashRef) { push(@insertColumns, $field) if ($session->form->process("fileContains_".$field->{fieldId})); } + my $log = $self->session->log; + use Data::Dumper; + $log->info( "Importing columns: " . Dumper( \@insertColumns ) ); my $storage = WebGUI::Storage->createTemp($self->session); $handleDuplicates = $session->form->process("handleDuplicates"); @@ -2955,32 +2958,32 @@ sub www_importForm { $i18n = WebGUI::International->new($self->session, "Asset_Thingy"); $output = "

".$i18n->get("import label")."

"; - $form = WebGUI::HTMLForm->new($self->session,-action=>$self->getUrl); - $form->hidden( - -name => "thingId", - -value => $thingId + $form = WebGUI::FormBuilder->new($self->session,action=>$self->getUrl); + $form->addField( "hidden", + name => "thingId", + value => $thingId ); - $form->hidden( - -name => "func", - -value => "import" + $form->addField( "hidden", + name => "func", + value => "import" ); - $form->file( - -name => "importFile", - -label => $i18n->get("import file label"), + $form->addField( "file", + name => "importFile", + label => $i18n->get("import file label"), ); - $form->selectBox( - -name => "handleDuplicates", - -label=> $i18n->get("duplicates label"), - -options=> { + $form->addField( "selectBox", + name => "handleDuplicates", + label=> $i18n->get("duplicates label"), + options=> { "skip" => $i18n->get("skip label"), "overwrite" => $i18n->get("overwrite label"), }, ); - $form->yesNo( - -name=>"ignoreFirstLine", - -label=>$i18n->get("ignore first line label"), + $form->addField( "yesNo", + name=>"ignoreFirstLine", + label=>$i18n->get("ignore first line label"), ); $fieldOptions = "" @@ -2993,24 +2996,24 @@ sub www_importForm { [$self->getId,$thingId]); while (my $field = $fields->hashRef) { $fieldOptions .= ""; } $fieldOptions .= "
".$field->{label}.""; - $fieldOptions .= WebGUI::Form::checkbox($self->session, { + $fieldOptions .= WebGUI::Form::Checkbox->new($self->session, { checked => "", name => "fileContains_".$field->{fieldId}, value => 1, - }); + })->toHtml; $fieldOptions .= ""; - $fieldOptions .= WebGUI::Form::checkbox($self->session, { + $fieldOptions .= WebGUI::Form::Checkbox->new($self->session, { checked => "", name => "checkDuplicates_".$field->{fieldId}, value => 1, - }); + })->toHtml; $fieldOptions .= "
"; - $form->raw($fieldOptions); - $form->submit; + $form->addField( "ReadOnly", name => 'fieldOptions', value => $fieldOptions ); + $form->addField( "submit", name => "submit" ); - $output .= $form->print; + $output .= $form->toHtml; return $self->processStyle($output); } diff --git a/t/Asset/Wobject/Thingy.t b/t/Asset/Wobject/Thingy.t index 6ca4d5d8b..a150553a8 100644 --- a/t/Asset/Wobject/Thingy.t +++ b/t/Asset/Wobject/Thingy.t @@ -15,7 +15,7 @@ use strict; use WebGUI::Test; use WebGUI::Test::MockAsset; use WebGUI::Session; -use Test::More tests => 35; # increment this value for each test you create +use Test::More tests => 39; # increment this value for each test you create use Test::Deep; use JSON; use WebGUI::Asset::Wobject::Thingy; @@ -466,3 +466,63 @@ my $field = $session->db->quickHashRef( ); ok( $field, "field exists" ); cmp_deeply( $field, superhashof( \%fieldInfo ), 'field info saved' ); + +#---------------------------------------------------------------------------- +# www_importForm + +my $thingy = WebGUI::Test->asset( + className => 'WebGUI::Asset::Wobject::Thingy', +); +my $thingId = $thingy->addThing(); +my @fieldIds = ( + $thingy->addField({ + assetId => $thingy->getId, + thingId => $thingId, + sequenceNumber => 1, + label => 'Name', + fieldType => 'text', + }), + $thingy->addField({ + assetId => $thingy->getId, + thingId => $thingId, + sequenceNumber => 2, + label => 'Age', + fieldType => 'Integer', + }), +); + +my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->file ); +$mech->get_ok( '/' ); +$mech->session->user({ userId => 3 }); + +$mech->get_ok( $thingy->getUrl( 'func=importForm;thingId=' . $thingId ) ); +$mech->submit_form_ok({ + fields => { + importFile_file => WebGUI::Test::collateral( "thingy.csv" ), + "fileContains_$fieldIds[0]" => 1, + "fileContains_$fieldIds[1]" => 1, + }, + }, + "Import data into thing", +); + +my @thingData = $session->db->buildArrayRefOfHashRefs( + "select * from ". $session->db->quote_identifier("Thingy_".$thingId), +); +cmp_deeply( + @thingData, + bag( + superhashof({ + "field_$fieldIds[0]" => "Andy Dufresne", + "field_$fieldIds[1]" => "42", + }), + superhashof({ + "field_$fieldIds[0]" => "Red Ellis", + "field_$fieldIds[1]" => "47", + }), + ), + " All rows imported ", +) or diag( explain \@thingData ); + + + diff --git a/t/supporting_collateral/thingy.csv b/t/supporting_collateral/thingy.csv new file mode 100644 index 000000000..615e1169d --- /dev/null +++ b/t/supporting_collateral/thingy.csv @@ -0,0 +1,2 @@ +"Andy Dufresne",42 +"Red Ellis",47