diff --git a/database/factories/StepFactory.php b/database/factories/StepFactory.php new file mode 100644 index 000000000..c64c09395 --- /dev/null +++ b/database/factories/StepFactory.php @@ -0,0 +1,16 @@ +define(\ProcessMaker\Model\Step::class, function (Faker $faker) { + return [ + 'STEP_UID' => G::generateUniqueID(), + 'PRO_UID' => G::generateUniqueID(), + 'TAS_UID' => G::generateUniqueID(), + 'STEP_TYPE_OBJ' => 'DYNAFORM', + 'STEP_UID_OBJ' => '0', + 'STEP_CONDITION' => 'None', + 'STEP_POSITION' => 0, + 'STEP_MODE' => 'EDIT' + ]; +}); \ No newline at end of file diff --git a/tests/unit/workflow/engine/classes/CasesTest.php b/tests/unit/workflow/engine/classes/CasesTest.php new file mode 100644 index 000000000..27ab462e0 --- /dev/null +++ b/tests/unit/workflow/engine/classes/CasesTest.php @@ -0,0 +1,370 @@ +create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX); + + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method with step + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_position() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '1 == 1' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method with output document + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_output_document() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '1 == 1', + 'STEP_TYPE_OBJ' => 'OUTPUT_DOCUMENT' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method with input document + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_input_document() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '1 == 1', + 'STEP_TYPE_OBJ' => 'INPUT_DOCUMENT' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method with external document + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_external() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '1 == 1', + 'STEP_TYPE_OBJ' => 'EXTERNAL' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method with message step + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_message() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '1 == 1', + 'STEP_TYPE_OBJ' => 'MESSAGE' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method when the step does not exist + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_step_does_not_exists() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + $cases = new Cases(); + $this->expectExceptionMessage("**ID_STEP_DOES_NOT_EXIST**"); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + } + + /** + * Tests the getNextStep method when there is an exception + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_step_exception() + { + $cases = new Cases(); + $this->expectExceptionMessage("The Application row '' doesn't exist!"); + $res = $cases->getNextStep(); + } + + /** + * Tests the getNextStep method when the result is false + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_step_false() + { + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(); + $appDelegation = factory(Delegation::class)->create(); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX); + $this->assertFalse($res); + } + + /** + * Tests the getNextStep method when there is a gmail account + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_gmail() + { + $_SESSION['gmail'] = ''; + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '1 == 1', + 'STEP_TYPE_OBJ' => 'MESSAGE' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method when there is a gmail account related to the next step + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_gmail_nextstep() + { + $_SESSION['gmail'] = ''; + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 1, + 'STEP_CONDITION' => '1 == 1', + 'STEP_TYPE_OBJ' => 'MESSAGE' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Tests the getNextStep method when the step condition is empty + * + * @covers \Cases::getNextStep() + * @test + */ + public function it_should_test_get_next_step_method_condition_empty() + { + $_SESSION['gmail'] = ''; + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create(['PRO_UID' => $process->PRO_UID]); + $appDelegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'PRO_UID' => $process->PRO_UID, + 'DEL_INDEX' => 2, + 'DEL_PREVIOUS' => $appDelegation->DEL_INDEX + ]); + factory(Step::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'TAS_UID' => $appDelegation->TAS_UID, + 'STEP_POSITION' => 2, + 'STEP_CONDITION' => '', + 'STEP_TYPE_OBJ' => 'MESSAGE' + ]); + $cases = new Cases(); + $res = $cases->getNextStep($process->PRO_UID, $application->APP_UID, $appDelegation->DEL_INDEX, 1); + $this->assertCount(4, $res); + } + + /** + * Call the tearDown method + */ + public function tearDown() + { + // The parent method needs to be override due to errors appearing + } +} \ No newline at end of file diff --git a/workflow/engine/classes/Cases.php b/workflow/engine/classes/Cases.php index 80a4034bb..816e5e19b 100644 --- a/workflow/engine/classes/Cases.php +++ b/workflow/engine/classes/Cases.php @@ -2288,127 +2288,134 @@ class Cases * Get the next step * * @name getNextStep - * @param string $sProUid - * @param string $sAppUid - * @param integer $iDelIndex - * @param integer $iPosition + * @param string $proUid + * @param string $appUid + * @param integer $delIndex + * @param integer $position * @return array */ - public function getNextStep($sProUid = '', $sAppUid = '', $iDelIndex = 0, $iPosition = 0) + public function getNextStep($proUid = '', $appUid = '', $delIndex = 0, $position = 0) { - $oPMScript = new PMScript(); - $oApplication = new Application(); - $aFields = $oApplication->Load($sAppUid); - if (!is_array($aFields['APP_DATA'])) { - $aFields['APP_DATA'] = G::array_merges(G::getSystemConstants(), unserialize($aFields['APP_DATA'])); + $pmScript = new PMScript(); + $application = new Application(); + $fields = $application->Load($appUid); + $data = Cases::unserializeData($fields['APP_DATA']); + unset($data['USER_LOGGED']); + unset($data['USR_USERNAME']); + + if (!is_array($fields['APP_DATA'])) { + $fields['APP_DATA'] = G::array_merges(G::getSystemConstants(), $data); } - $oPMScript->setFields($aFields['APP_DATA']); + + $pmScript->setFields($fields['APP_DATA']); try { //get the current Delegation, and TaskUID $c = new Criteria('workflow'); - $c->add(AppDelegationPeer::PRO_UID, $sProUid); - $c->add(AppDelegationPeer::APP_UID, $sAppUid); - $c->add(AppDelegationPeer::DEL_INDEX, $iDelIndex); - $aRow = AppDelegationPeer::doSelect($c); + $c->add(AppDelegationPeer::PRO_UID, $proUid); + $c->add(AppDelegationPeer::APP_UID, $appUid); + $c->add(AppDelegationPeer::DEL_INDEX, $delIndex); + $rows = AppDelegationPeer::doSelect($c); - if (!isset($aRow[0])) { + if (!isset($rows[0])) { return false; } - $sTaskUid = $aRow[0]->getTasUid(); + $taskUid = $rows[0]->getTasUid(); //get max step for this task $c = new Criteria(); $c->clearSelectColumns(); $c->addSelectColumn('MAX(' . StepPeer::STEP_POSITION . ')'); - $c->add(StepPeer::PRO_UID, $sProUid); - $c->add(StepPeer::TAS_UID, $sTaskUid); + $c->add(StepPeer::PRO_UID, $proUid); + $c->add(StepPeer::TAS_UID, $taskUid); $rs = StepPeer::doSelectRS($c); $rs->next(); $row = $rs->getRow(); - $iLastStep = intval($row[0]); - if ($iPosition != 10000 && $iPosition > $iLastStep) { - throw (new Exception(G::LoadTranslation('ID_STEP_DOES_NOT_EXIST', array(G::LoadTranslation('ID_POSITION'), $iPosition)))); + $lastStep = intval($row[0]); + if ($position != 10000 && $position > $lastStep) { + throw (new Exception(G::LoadTranslation('ID_STEP_DOES_NOT_EXIST', + [G::LoadTranslation('ID_POSITION'), $position]))); } - $iPosition += 1; - $aNextStep = null; - if ($iPosition <= $iLastStep) { - while ($iPosition <= $iLastStep) { - $bAccessStep = false; + $position += 1; + $nextStep = null; + if ($position <= $lastStep) { + while ($position <= $lastStep) { + $accessStep = false; //step - $oStep = new Step; - $oStep = $oStep->loadByProcessTaskPosition($sProUid, $sTaskUid, $iPosition); - if ($oStep) { - if (trim($oStep->getStepCondition()) !== '') { - $oPMScript->setScript($oStep->getStepCondition()); - $oPMScript->setExecutedOn(PMScript::CONDITION); - $bAccessStep = $oPMScript->evaluate(); + $step = new Step; + $step = $step->loadByProcessTaskPosition($proUid, $taskUid, $position); + if ($step) { + if (trim($step->getStepCondition()) !== '') { + $pmScript->setScript($step->getStepCondition()); + $pmScript->setExecutedOn(PMScript::CONDITION); + $accessStep = $pmScript->evaluate(); } else { - $bAccessStep = true; + $accessStep = true; } - if ($bAccessStep) { - switch ($oStep->getStepTypeObj()) { + if ($accessStep) { + switch ($step->getStepTypeObj()) { case 'DYNAFORM': - $sAction = 'EDIT'; + $action = 'EDIT'; break; case 'OUTPUT_DOCUMENT': - $sAction = 'GENERATE'; + $action = 'GENERATE'; break; case 'INPUT_DOCUMENT': - $sAction = 'ATTACH'; + $action = 'ATTACH'; break; case 'EXTERNAL': - $sAction = 'EDIT'; + $action = 'EDIT'; break; case 'MESSAGE': - $sAction = ''; + $action = ''; break; } - if (array_key_exists('gmail', $_SESSION) || (array_key_exists('gmail', $_GET) && $_GET['gmail'] == 1)) { - $aNextStep = array( - 'TYPE' => $oStep->getStepTypeObj(), - 'UID' => $oStep->getStepUidObj(), - 'POSITION' => $oStep->getStepPosition(), - 'PAGE' => 'cases_Step?TYPE=' . $oStep->getStepTypeObj() . '&UID=' . - $oStep->getStepUidObj() . '&POSITION=' . $oStep->getStepPosition() . - '&ACTION=' . $sAction . - '&gmail=1' - ); + if (array_key_exists('gmail', $_SESSION) || (array_key_exists('gmail', + $_GET) && $_GET['gmail'] == 1)) { + $nextStep = [ + 'TYPE' => $step->getStepTypeObj(), + 'UID' => $step->getStepUidObj(), + 'POSITION' => $step->getStepPosition(), + 'PAGE' => 'cases_Step?TYPE=' . $step->getStepTypeObj() . '&UID=' . + $step->getStepUidObj() . '&POSITION=' . $step->getStepPosition() . + '&ACTION=' . $action . + '&gmail=1' + ]; } else { - $aNextStep = array( - 'TYPE' => $oStep->getStepTypeObj(), - 'UID' => $oStep->getStepUidObj(), - 'POSITION' => $oStep->getStepPosition(), - 'PAGE' => 'cases_Step?TYPE=' . $oStep->getStepTypeObj() . '&UID=' . - $oStep->getStepUidObj() . '&POSITION=' . $oStep->getStepPosition() . - '&ACTION=' . $sAction - ); + $nextStep = [ + 'TYPE' => $step->getStepTypeObj(), + 'UID' => $step->getStepUidObj(), + 'POSITION' => $step->getStepPosition(), + 'PAGE' => 'cases_Step?TYPE=' . $step->getStepTypeObj() . '&UID=' . + $step->getStepUidObj() . '&POSITION=' . $step->getStepPosition() . + '&ACTION=' . $action + ]; } - $iPosition = $iLastStep; + $position = $lastStep; } } - $iPosition += 1; + $position += 1; } } - if (!$aNextStep) { + if (!$nextStep) { if (array_key_exists('gmail', $_SESSION) || (array_key_exists('gmail', $_GET) && $_GET['gmail'] == 1)) { - $aNextStep = array( + $nextStep = [ 'TYPE' => 'DERIVATION', 'UID' => -1, - 'POSITION' => ($iLastStep + 1), + 'POSITION' => ($lastStep + 1), 'PAGE' => 'cases_Step?TYPE=ASSIGN_TASK&UID=-1&POSITION=10000&ACTION=ASSIGN&gmail=1' - ); + ]; } else { - $aNextStep = array( + $nextStep = [ 'TYPE' => 'DERIVATION', 'UID' => -1, - 'POSITION' => ($iLastStep + 1), + 'POSITION' => ($lastStep + 1), 'PAGE' => 'cases_Step?TYPE=ASSIGN_TASK&UID=-1&POSITION=10000&ACTION=ASSIGN' - ); + ]; } } - return $aNextStep; + return $nextStep; } catch (exception $e) { throw ($e); } diff --git a/workflow/engine/src/ProcessMaker/Model/Step.php b/workflow/engine/src/ProcessMaker/Model/Step.php new file mode 100644 index 000000000..361af6fbc --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Model/Step.php @@ -0,0 +1,11 @@ +