small changes in Thingy's web services methods, added Thingy tests

This commit is contained in:
Yung Han Khoe 2008-09-24 16:24:14 +00:00
parent e49a68d401
commit 976834a5a4
4 changed files with 123 additions and 30 deletions

View file

@ -87,6 +87,7 @@
- fixed: HttpProxy parser now handles self-closing tags correctly
- fixed: HttpProxy parser now doesn't try to rewrite forms unless also rewriting URLs
- fixed: WebGUI::HTML->cleanSegment now also keeps <link> if preserving script and style
- rfe: Teach Thingy to be a Web Service (SDH Consulting Group)
7.5.22
- fixed: Layout template now gets prepared correctly

View file

@ -1,2 +1,2 @@
severity = gentle
severity = gentle
exclude = ProhibitExplicitReturnUndef

View file

@ -964,7 +964,7 @@ sub getThing {
=head2 getViewThingVars ( )
Returns the field values of a thing instance and the title for its view screen in a tmpl var hashref.
If a tmpl var hashref is supplied values will be appended to that.
If a tmpl var hashref is supplied tmpl_var's will be appended to that.
=cut
@ -972,6 +972,8 @@ sub getViewThingVars {
my ($self, $thingId, $thingDataId,$var) = @_;
my $db = $self->session->db;
my (@field_loop, @viewScreenTitleFields, $viewScreenTitle);
return undef unless ($thingId && $thingDataId);
my %thingData = $db->quickHash("select * from ".$db->dbh->quote_identifier("Thingy_".$thingId)
." where thingDataId = ?",[$thingDataId]);
@ -1366,7 +1368,7 @@ sub www_deleteThingDataConfirm {
#-------------------------------------------------------------------
=head2 www_deleteThingDataConfirmViaAjax ( )
=head2 www_deleteThingDataViaAjax ( )
Deletes data in a Thing.
@ -1385,9 +1387,7 @@ sub www_deleteThingDataViaAjax {
unless ($thingId && $thingDataId) {
$session->http->setStatus("400", "Bad Request");
return JSON->new->utf8->encode([
{message => "Can't get thing data without a thingId and a thingDataId."}
]);
return JSON->new->utf8->encode({message => "Can't get thing data without a thingId and a thingDataId."});
}
my $thingProperties = $self->getThing($thingId);
@ -1398,11 +1398,11 @@ sub www_deleteThingDataViaAjax {
$self->deleteThingData($thingId,$thingDataId);
$session->http->setMimeType("application/json");
return JSON->new->utf8->encode([{message => "Data with thingDataId $thingDataId was deleted."}]);
return JSON->new->utf8->encode({message => "Data with thingDataId $thingDataId was deleted."});
}
else {
$session->http->setStatus("404", "Not Found");
return JSON->new->utf8->encode([{message => "The thingId you specified can not be found."}]);
return JSON->new->utf8->encode({message => "The thingId you specified can not be found."});
}
}
@ -2194,9 +2194,7 @@ sub www_editThingDataSaveViaAjax {
unless ($thingId && $thingDataId) {
$session->http->setStatus("400", "Bad Request");
return JSON->new->utf8->encode([
{message => "Can't get thing data without a thingId and a thingDataId."}
]);
return JSON->new->utf8->encode({message => "Can't get thing data without a thingId and a thingDataId."});
}
my $thingProperties = $self->getThing($thingId);
@ -2220,7 +2218,7 @@ sub www_editThingDataSaveViaAjax {
}
else {
$session->http->setStatus("404", "Not Found");
return JSON->new->utf8->encode([{message => "The thingId you requested can not be found."}]);
return JSON->new->utf8->encode({message => "The thingId you requested can not be found."});
}
}
@ -2302,7 +2300,7 @@ sub www_getThingViaAjax {
unless ($thingId) {
$session->http->setStatus("400", "Bad Request");
return JSON->new->utf8->encode([{message => "Can't return thing properties without a thingId."}]);
return JSON->new->utf8->encode({message => "Can't return thing properties without a thingId."});
}
my $thingProperties = $self->getThing($thingId);
@ -2315,7 +2313,7 @@ sub www_getThingViaAjax {
}
else {
$session->http->setStatus("404", "Not Found");
return JSON->new->utf8->encode([{message => "The thingId you requested can not be found."}]);
return JSON->new->utf8->encode({message => "The thingId you requested can not be found."});
}
}
@ -2349,7 +2347,7 @@ sub www_getThingsViaAjax {
}
else {
$session->http->setStatus("404", "Not Found");
return JSON->new->utf8->encode([{message => "No visible Things were found in this Thingy."}]);
return JSON->new->utf8->encode({message => "No visible Things were found in this Thingy."});
}
}
@ -2704,7 +2702,7 @@ sub www_searchViaAjax {
unless ($thingId) {
$session->http->setStatus("400", "Bad Request");
return "Can't perform search without a thingId.";
return JSON->new->utf8->encode({message => "Can't perform search without a thingId."});
}
if ($thingProperties->{thingId}){
@ -2719,7 +2717,7 @@ sub www_searchViaAjax {
}
else {
$session->http->setStatus("404", "Not Found");
return "The thingId you requested can not be found.";
return JSON->new->utf8->encode({message => "The thingId you requested can not be found."});
}
}
@ -3097,7 +3095,7 @@ sub www_viewThingDataViaAjax {
unless ($thingId && $thingDataId) {
$session->http->setStatus("400", "Bad Request");
return JSON->new->utf8->encode([{message => "Can't get thing data without a thingId and a thingDataId."}]);
return JSON->new->utf8->encode({message => "Can't get thing data without a thingId and a thingDataId."});
}
my $thingProperties = $self->getThing($thingId);
@ -3111,12 +3109,12 @@ sub www_viewThingDataViaAjax {
}
else{
$session->http->setStatus("404", "Not Found");
return JSON->new->utf8->encode([{message => "The thingDataId you requested can not be found."}]);
return JSON->new->utf8->encode({message => "The thingDataId you requested can not be found."});
}
}
else {
$session->http->setStatus("404", "Not Found");
return JSON->new->utf8->encode([{message => "The thingId you requested can not be found."}]);
return JSON->new->utf8->encode({message => "The thingId you requested can not be found."});
}
}

View file

@ -17,7 +17,7 @@ use lib "$FindBin::Bin/../../lib";
use WebGUI::Test;
use WebGUI::Session;
use WebGUI::PseudoRequest;
use Test::More tests => 12; # increment this value for each test you create
use Test::More tests => 16; # increment this value for each test you create
use Test::Deep;
use JSON;
use WebGUI::Asset::Wobject::Thingy;
@ -37,8 +37,6 @@ isa_ok($thingy, 'WebGUI::Asset::Wobject::Thingy');
# Test to see if we can set new values
my $newThingySettings = {
templateId=>'testingtestingtesting1',
#searchRoot=>'testingtestingtesting2',
#classLimiter=>'WebGUI::Asset::Wobject::Article',
};
$thingy->update($newThingySettings);
@ -53,6 +51,7 @@ my %thingProperties = (
thingId=>"new",
label=>$i18n->get('assetName'),
editScreenTitle=>$i18n->get('edit screen title label'),
editInstructions=>'',
groupIdAdd=>$groupIdEdit,
groupIdEdit=>$groupIdEdit,
saveButtonLabel=>$i18n->get('default save button label'),
@ -62,6 +61,7 @@ my %thingProperties = (
viewTemplateId=>"ThingyTmpl000000000002",
defaultView=>'searchThing',
searchScreenTitle=>$i18n->get('search screen title label'),
searchDescription=>'',
groupIdSearch=>$groupIdEdit,
groupIdExport=>$groupIdEdit,
groupIdImport=>$groupIdEdit,
@ -82,6 +82,86 @@ is($thingTableNameCheck,$thingTableName,"An empty table: ".$thingTableName." for
is($thingy->get('defaultThingId'),$thingId,"The Thingy assets defaultThingId was set correctly.");
# Test getting the newly added thing by its thingID as JSON
$session->user({userId => 3});
my $json = $thingy->www_getThingViaAjax($thingId);
my $dataFromJSON = JSON->new->utf8->decode($json);
cmp_deeply(
$dataFromJSON,
{
assetId=>$thingy->getId,
thingId=>$thingId,
label=>$i18n->get('assetName'),
editScreenTitle=>$i18n->get('edit screen title label'),
editInstructions=>'',
groupIdAdd=>$groupIdEdit,
groupIdEdit=>$groupIdEdit,
saveButtonLabel=>$i18n->get('default save button label'),
afterSave=>'searchThisThing',
editTemplateId=>"ThingyTmpl000000000003",
groupIdView=>$groupIdEdit,
viewTemplateId=>"ThingyTmpl000000000002",
defaultView=>'searchThing',
searchScreenTitle=>$i18n->get('search screen title label'),
searchDescription=>'',
groupIdSearch=>$groupIdEdit,
groupIdExport=>$groupIdEdit,
groupIdImport=>$groupIdEdit,
searchTemplateId=>"ThingyTmpl000000000004",
thingsPerPage=>25,
display=>undef,
onAddWorkflowId=>undef,
onEditWorkflowId=>undef,
onDeleteWorkflowId=>undef,
sortBy=>undef,
},
'Getting newly added thing as JSON: www_getThingViaAjax returns correct data as JSON.'
);
# Test getting all things in this Thingy as JSON, this should be an array containing only
# the newly created thing.
$json = $thingy->www_getThingsViaAjax();
$dataFromJSON = JSON->new->utf8->decode($json);
cmp_deeply(
$dataFromJSON,
[{
assetId=>$thingy->getId,
thingId=>$thingId,
label=>$i18n->get('assetName'),
editScreenTitle=>$i18n->get('edit screen title label'),
editInstructions=>'',
groupIdAdd=>$groupIdEdit,
groupIdEdit=>$groupIdEdit,
saveButtonLabel=>$i18n->get('default save button label'),
afterSave=>'searchThisThing',
editTemplateId=>"ThingyTmpl000000000003",
groupIdView=>$groupIdEdit,
viewTemplateId=>"ThingyTmpl000000000002",
defaultView=>'searchThing',
searchScreenTitle=>$i18n->get('search screen title label'),
searchDescription=>'',
groupIdSearch=>$groupIdEdit,
groupIdExport=>$groupIdEdit,
groupIdImport=>$groupIdEdit,
searchTemplateId=>"ThingyTmpl000000000004",
thingsPerPage=>25,
display=>undef,
onAddWorkflowId=>undef,
onEditWorkflowId=>undef,
onDeleteWorkflowId=>undef,
sortBy=>undef,
canEdit=>1,
canAdd=>1,
canSearch=>1,
}],
'Getting all things in Thingy as JSON: www_getThingsViaAjax returns correct data as JSON.'
);
# Test adding a field
my %fieldProperties = (
@ -107,7 +187,7 @@ my ($fieldLabel, $columnType, $Null, $Key, $Default, $Extra) = $session->db->qui
is($fieldLabel,"field_".$fieldId,"A column for the new field Field_$fieldId exists.");
is($columnType,"longtext","The columns is the right type");
# Test adding, editing and getting thing data
# Test adding, editing, getting and deleting thing data
my ($newThingDataId,$errors) = $thingy->editThingDataSave($thingId,'new',{"field_".$fieldId => 'test value'});
@ -127,12 +207,11 @@ cmp_deeply(
field_name => "field_".$fieldId,
field_label => $i18n->get('assetName')." field"
}],
'Getting newly added field data: getViewThingVars returns correct field_loop.'
'Getting newly added thing data: getViewThingVars returns correct field_loop.'
);
$session->user({userId => 3});
my $json = $thingy->www_viewThingDataViaAjax($thingId,$newThingDataId);
my $dataFromJSON = JSON->new->utf8->decode($json);
$json = $thingy->www_viewThingDataViaAjax($thingId,$newThingDataId);
$dataFromJSON = JSON->new->utf8->decode($json);
cmp_deeply(
$dataFromJSON,
@ -147,7 +226,7 @@ cmp_deeply(
}],
viewScreenTitle => "",
},
'Getting newly added field data as JSON: www_viewThingDataViaAjax returns correct data as JSON.'
'Getting newly added thing data as JSON: www_viewThingDataViaAjax returns correct data as JSON.'
);
my ($updatedThingDataId,$errors) = $thingy->editThingDataSave($thingId,$newThingDataId,{"field_".$fieldId => 'new test value'});
@ -164,7 +243,22 @@ cmp_deeply(
field_name => "field_".$fieldId,
field_label => $i18n->get('assetName')." field"
}],
'Getting updated field data: getViewThingVars returns correct field_loop with updated value.'
'Getting updated thing data: getViewThingVars returns correct field_loop with updated value.'
);
$thingy->deleteThingData($thingId,$newThingDataId);
is($thingy->getViewThingVars($thingId,$newThingDataId),undef,'Thing data was succesfully deleted, getViewThingVars returns undef.');
$json = $thingy->www_viewThingDataViaAjax($thingId,$newThingDataId);
$dataFromJSON = JSON->new->utf8->decode($json);
cmp_deeply(
$dataFromJSON,
{
message => "The thingDataId you requested can not be found.",
},
'Getting thing data as JSON after deleting: www_viewThingDataViaAjax returns correct message.'
);