From 8d9628f9055cd8541d2610645b2370b695c45c74 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Thu, 7 May 2020 16:46:03 -0400 Subject: [PATCH] PMCORE-1420 --- .../factories/ProcessVariablesFactory.php | 28 +++++++++ .../BusinessModel/VariableTest.php | 39 +++++++++++++ .../Model/ProcessVariablesTest.php | 37 ++++++++++++ .../ProcessMaker/BusinessModel/Variable.php | 38 +++++++++++++ .../ProcessMaker/Model/ProcessVariables.php | 57 ++++++++++++++++++- .../Services/Api/Project/Variable.php | 43 +++++++++++++- 6 files changed, 239 insertions(+), 3 deletions(-) diff --git a/database/factories/ProcessVariablesFactory.php b/database/factories/ProcessVariablesFactory.php index d7ee63666..77bc3ed8a 100644 --- a/database/factories/ProcessVariablesFactory.php +++ b/database/factories/ProcessVariablesFactory.php @@ -6,9 +6,37 @@ use ProcessMaker\Model\ProcessVariables; $factory->define(ProcessVariables::class, function (Faker $faker) { return [ 'VAR_UID' => G::generateUniqueID(), + 'PRO_ID' => G::generateUniqueID(), 'PRJ_UID' => G::generateUniqueID(), 'VAR_NAME' => $faker->word, 'VAR_FIELD_TYPE' => G::generateUniqueID(), + 'VAR_FIELD_TYPE_ID' => G::generateUniqueID(), + 'VAR_FIELD_SIZE' => 10, + 'VAR_LABEL' => 'string', + 'VAR_DBCONNECTION' => 'workflow', + 'VAR_SQL' => '', + 'VAR_NULL' => 0, + 'VAR_DEFAULT' => '', + 'VAR_ACCEPTED_VALUES' => '[]', + 'INP_DOC_UID' => '' + ]; +}); + +// Create a processVariables with the foreign keys +$factory->state(ProcessVariables::class, 'foreign_keys', function (Faker $faker) { + $types = ['string', 'integer', 'float', 'boolean', 'datetime', 'grid', 'array', 'file', 'multiplefile', 'object']; + $varType = $faker->randomElement($types); + $varTypeId = array_search($varType, $types) + 1; + // Create values in the foreign key relations + $process = factory(\ProcessMaker\Model\Process::class)->create(); + + return [ + 'VAR_UID' => G::generateUniqueID(), + 'PRO_ID' => $process->PRO_ID, + 'PRJ_UID' => $process->PRO_UID, + 'VAR_NAME' => $faker->word, + 'VAR_FIELD_TYPE' => $varType, + 'VAR_FIELD_TYPE_ID' => $varTypeId, 'VAR_FIELD_SIZE' => 10, 'VAR_LABEL' => 'string', 'VAR_DBCONNECTION' => 'workflow', diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php index 794dd14f2..261e43ebd 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php @@ -188,4 +188,43 @@ class VariableTest extends TestCase $this->assertArrayHasKey('var_accepted_values', $res, "The result does not contains 'var_accepted_values' as key"); $this->assertArrayHasKey('inp_doc_uid', $res, "The result does not contains 'inp_doc_uid' as key"); } + + /** + * Test it return the variables by type related to the PRO_UID + * + * @covers \ProcessMaker\BusinessModel\Variable::getVariablesByType() + * @test + */ + public function it_list_variables_by_type_related_a_process() + { + $process = factory(Process::class)->create(); + $varType = 'integer'; + $varTypeId = 2; + for ($x = 1; $x <= 5; $x++) { + $processVar = factory(ProcessVariables::class)->states('foreign_keys')->create([ + 'PRO_ID' => $process->PRO_ID, + 'PRJ_UID' => $process->PRO_UID, + 'VAR_FIELD_TYPE' => $varType, + 'VAR_FIELD_TYPE_ID' => $varTypeId, + 'VAR_NAME' => 'varTestName' . $x, + ]); + } + $variable = new Variable(); + // Get all results + $res = $variable->getVariablesByType($process->PRO_UID, 2); + $this->assertEquals(5, count($res)); + $res = head($res); + $this->assertArrayHasKey('value', $res, "The result does not contains 'value' as key"); + // Get a specific start and limit + $res = $variable->getVariablesByType($process->PRO_UID, 2, 0, 2); + $this->assertNotEmpty($res); + $this->assertEquals(2, count($res)); + // Get a specific search + $res = $variable->getVariablesByType($process->PRO_UID, 2, 0, 4, 'varTest'); + $this->assertNotEmpty($res); + $this->assertEquals(4, count($res)); + // When the search does not match + $res = $variable->getVariablesByType($process->PRO_UID, 2, null, null, 'other'); + $this->assertEmpty($res); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php index ee2b26edc..22388239f 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php @@ -70,4 +70,41 @@ class ProcessVariablesTest extends TestCase $result = ProcessVariables::getVariables($process->PRO_ID); $this->assertNotEmpty($result); } + + /** + * Test it return the variables by type related to the PRO_ID + * + * @covers \ProcessMaker\Model\ProcessVariables::getVariablesByType() + * @test + */ + public function it_list_variables_type_by_process() + { + $process = factory(Process::class)->create(); + $varType = 'integer'; + $varTypeId = 2; + for ($x = 1; $x <= 5; $x++) { + $processVar = factory(ProcessVariables::class)->states('foreign_keys')->create([ + 'PRO_ID' => $process->PRO_ID, + 'PRJ_UID' => $process->PRO_UID, + 'VAR_FIELD_TYPE' => $varType, + 'VAR_FIELD_TYPE_ID' => $varTypeId, + 'VAR_NAME' => 'varTestName' . $x, + ]); + } + + $res = ProcessVariables::getVariablesByType($processVar->PRO_ID, 2, null, null, null); + $this->assertNotEmpty($res); + $this->assertEquals(5, count($res)); + // Get a specific start and limit + $res = ProcessVariables::getVariablesByType($process->PRO_ID, 2, 0, 2); + $this->assertNotEmpty($res); + $this->assertEquals(2, count($res)); + // Get a specific search + $res = ProcessVariables::getVariablesByType($process->PRO_ID, 2, 0, 4, 'varTest'); + $this->assertNotEmpty($res); + $this->assertEquals(4, count($res)); + // When the search does not match + $res = ProcessVariables::getVariablesByType($process->PRO_ID, 2, null, null, 'other'); + $this->assertEmpty($res); + } } \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php index bcd1768b5..1d7f76b78 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php @@ -26,6 +26,16 @@ class Variable 'object' => 10 ]; + /** + * Get the variables types accepted + * + * @return array + */ + public function getVariableTypes() + { + return $this->variableTypes; + } + /** * Create Variable for a Process * @@ -355,6 +365,34 @@ class Variable return $arrayVariables; } + /** + * Get data of Variables related to the specific type + * + * @param string $processUid Unique id of Process + * @param int $typeVarId + * @param int $start + * @param int $limit + * @param string $search + * @param string $prefix + * + * @return array, return an array with data of a DynaForm + * @throws Exception + */ + public function getVariablesByType($processUid, $typeVarId = 0, $start = null, $limit = null, $search = null, $prefix = null) + { + //Verify data + $proId = Validator::proUid($processUid, '$prj_uid'); + $variables = ProcessVariables::getVariablesByType($proId, $typeVarId, $start, $limit, $search); + $arrayVariables = []; + foreach ($variables as $var) { + $arrayVariables[] = [ + 'value' => is_null($prefix) ? $var['VAR_NAME'] : $prefix . $var['VAR_NAME'], + ]; + } + + return $arrayVariables; + } + /** * Verify field definition * diff --git a/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php b/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php index 19693cab9..cdb487ddd 100644 --- a/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php +++ b/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php @@ -72,8 +72,21 @@ class ProcessVariables extends Model */ public function scopeProcessId($query, int $proId) { - return $query->where('PRO_ID', $proId); + return $query->where('PROCESS_VARIABLES.PRO_ID', $proId); } + + /** + * Scope a query to filter an specific type for variable + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param int $typeId + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeTypeId($query, int $typeId) + { + return $query->where('VAR_FIELD_TYPE_ID', $typeId); + } + /** * Return the variables list * @@ -96,4 +109,46 @@ class ProcessVariables extends Model return $variablesList; } + + /** + * Return the variables list + * + * @param int $proId + * @param int $typeId + * @param int $start + * @param int $limit + * @param string $search + * + * @return array + */ + public static function getVariablesByType(int $proId, int $typeId = 0, $start = null, $limit = null, $search = null) + { + $query = ProcessVariables::query()->select(); + $query->leftJoin('DB_SOURCE', function ($join) { + $join->on('DB_SOURCE.PRO_ID', '=', 'PROCESS_VARIABLES.PRO_ID'); + }); + $query->processId($proId); + // Check if we need to filter the type of variables + if ($typeId > 0) { + $query->typeId($typeId); + } + // search a specific variable name + if (!empty($search)) { + $query->where('VAR_NAME', 'LIKE', "${search}%"); + } + // orde by varNane + $query->orderBy('VAR_NAME', 'ASC'); + // Check if we need to add a pagination + if(!is_null($start) && !is_null($limit)) { + $query->offset($start)->limit($limit); + } + // Get records + $results = $query->get(); + $variablesList = []; + $results->each(function ($item, $key) use (&$variablesList) { + $variablesList[] = $item->toArray(); + }); + + return $variablesList; + } } \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php b/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php index 13e65d343..46bbd585b 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php @@ -1,8 +1,12 @@ getVariableTypes())])); + } + // Review if the word has the prefix + $count = preg_match_all('/\@(?:([\@\%\#\?\$\=\&Qq\!])|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*(?:[\\\\][\w\W])?)*)\))((?:\s*\[[\'"]?\w+[\'"]?\])+|\-\>([a-zA-Z\_]\w*))?/', $search, $match, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE); + // Check if the search has some prefix + $prefix = ''; + if ($count) { + $prefix = substr($search,0,2); + $search = substr($search,2); + } + $response = $variable->getVariablesByType($prj_uid, $typeVatId, $start, $limit, $search, $prefix); + + return $response; + } catch (Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + /** * @url GET /:prj_uid/process-variable/:var_uid *