Fix multiple bugs related to Singleton workflows:

Can't use undef in a where clause.
    Have to use the same JSON encoding in all places.
Added tests for singleton workflow instances.
This commit is contained in:
Colin Kuskie 2008-09-12 22:59:43 +00:00
parent 993391a620
commit 5689c08bbc
3 changed files with 30 additions and 16 deletions

View file

@ -38,6 +38,8 @@
- fixed: Thingy Things can now have no fields and still be functional.
- fixed: Can no longer buy badges from receipt or direct link page if quantity is empty
- fixed: Tickets on add to badge screen now show the time at the event's timezone
- It was possible to run multiple singleton workflows at the same time.
7.5.22
- fixed: Layout template now gets prepared correctly
- fixed: When user does not have permissions to search/edit a thing a proper error is shown.

View file

@ -61,12 +61,17 @@ sub create {
my ($class, $session, $properties, $skipRealtime) = @_;
# do singleton check
my ($isSingleton) = $session->db->quickArray("select count(*) from Workflow where workflowId=? and
mode='singleton'",[$properties->{workflowId}]);
my $params = (exists $properties->{parameters})
? JSON->new->utf8->canonical->encode({parameters => $properties->{parameters}})
: undef;
my ($count) = $session->db->quickArray("select count(*) from WorkflowInstance where workflowId=? and parameters=?",[$properties->{workflowId},$params]);
my $placeHolders = [$properties->{workflowId}];
my ($isSingleton) = $session->db->quickArray("select count(*) from Workflow where workflowId=? and mode='singleton'",$placeHolders);
my $sql = "select count(*) from WorkflowInstance where workflowId=?";
if (exists $properties->{parameters}) {
push @{ $placeHolders }, JSON->new->utf8->pretty->encode({parameters => $properties->{parameters}});
$sql .= ' and parameters=?';
}
else {
$sql .= ' and parameters IS NULL';
}
my ($count) = $session->db->quickArray($sql,$placeHolders);
if ($isSingleton && $count) {
$session->log->info("An instance of singleton workflow $properties->{workflowId} already exists, not creating a new one");
return undef

View file

@ -42,23 +42,24 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 8; # Increment this number for each test you create
plan tests => 11; # Increment this number for each test you create
#----------------------------------------------------------------------------
# put your tests here
###############################################################################3
###############################################################################
#
# create a workflow instance
#
###############################################################################3
###############################################################################
my $wf = WebGUI::Workflow->create(
$session,
{
title => 'WebGUI::Workflow::Instance Test',
description => 'Description',
type => 'None'
type => 'None',
mode => 'singleton',
}
);
isa_ok($wf, 'WebGUI::Workflow', 'workflow created for test');
@ -66,9 +67,6 @@ isa_ok($wf, 'WebGUI::Workflow', 'workflow created for test');
# create an instance of $wfId
my $properties = {
workflowId=>$wf->getId,
methodName=>"new",
className=>"None",
parameters=>'encode me',
};
my $dateUpdated = time();
my $instance = WebGUI::Workflow::Instance->create($session, $properties);
@ -79,12 +77,21 @@ is($instance->get('priority'), 2, 'Default instance priority is 2');
cmp_ok(abs ($instance->get('lastUpdate')-$dateUpdated), '<=', 3, 'Date updated field set correctly when instance is created');
##Singleton checks
my $otherInstance = WebGUI::Workflow::Instance->create($session, $properties);
is ($otherInstance, undef, 'create: only allows one instance of a singleton to be created');
###############################################################################3
$instance->set({ 'parameters' => {session => 1}, });
$otherInstance = WebGUI::Workflow::Instance->create($session, {workflowId => $wf->getId, parameters => { session => 1,} });
is($otherInstance, undef, 'create: another singleton instance can not be created if it the same parameters as a currently existing instance');
$otherInstance = WebGUI::Workflow::Instance->create($session, {workflowId => $wf->getId, parameters => { session => 2,}});
isnt ($otherInstance, undef, 'create: another singleton instance can be created if it has different parameters');
$otherInstance->delete;
###############################################################################
#
# getWorkflow
#
###############################################################################3
###############################################################################
my $instanceWorkflow = $instance->getWorkflow;
is($instanceWorkflow->getId, $wf->getId, 'getWorkflow returns a copy of the workflow for the instance');
@ -95,5 +102,5 @@ is($instanceWorkflow->getId, $wf->getId, 'getWorkflow, caching check');
#----------------------------------------------------------------------------
# Cleanup
END {
$wf->delete; ##Deleting a Workflow deletes its instances, too.
#$wf->delete; ##Deleting a Workflow deletes its instances, too.
}