From 9fd71632a2c0ddd6aa6591139c77a19aad4ce2e1 Mon Sep 17 00:00:00 2001 From: "Paula.Quispe" Date: Fri, 8 Apr 2022 12:51:19 -0400 Subject: [PATCH] PMCORE-3709 --- .../BusinessModel/VariableTest.php | 29 ++++++++++- .../Model/AdditionalTablesTest.php | 25 +++++++++ .../src/ProcessMaker/Model/FieldsTest.php | 15 ++++++ .../Model/ProcessVariablesTest.php | 15 ++++++ .../translations/english/processmaker.en.po | 4 +- workflow/engine/data/mysql/insert.sql | 2 +- .../ProcessMaker/BusinessModel/Variable.php | 27 +++++----- .../ProcessMaker/Model/AdditionalTables.php | 28 ++++++++++ .../engine/src/ProcessMaker/Model/Fields.php | 51 +++++++++++++++++++ .../ProcessMaker/Model/ProcessVariables.php | 17 +++++++ 10 files changed, 197 insertions(+), 16 deletions(-) diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php index ed9935a66..f71211f80 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/VariableTest.php @@ -5,8 +5,10 @@ namespace ProcessMaker\BusinessModel; use Exception; use G; use ProcessMaker\BusinessModel\Variable; +use ProcessMaker\Model\AdditionalTables; use ProcessMaker\Model\Application; use ProcessMaker\Model\Dynaform; +use ProcessMaker\Model\Fields; use ProcessMaker\Model\Process; use ProcessMaker\Model\ProcessVariables; use Tests\TestCase; @@ -16,7 +18,6 @@ use Tests\TestCase; */ class VariableTest extends TestCase { - /** * Test it create variables related to the process * @@ -272,4 +273,30 @@ class VariableTest extends TestCase $variable = new Variable(); $result = $variable->executeSqlControl(null, []); } + + /** + * This verify the exception + * @test + * @covers \ProcessMaker\BusinessModel\Variable::throwExceptionIfVariableIsAssociatedAditionalTable() + */ + public function it_should_test_exception_when_a_variable_is_related_table() + { + //assert + $this->expectException(Exception::class); + // Create process variable + $variable = factory(ProcessVariables::class)->create(); + $result = ProcessVariables::getVariable($variable->VAR_UID); + $this->assertNotEmpty($result); + // Create tables + $table = factory(AdditionalTables::class)->create([ + 'PRO_UID' => $variable->PRO_UID, + ]); + // Create fields + $fields = factory(Fields::class)->create([ + 'ADD_TAB_UID' => $table->ADD_TAB_UID, + 'FLD_NAME' => $variable->VAR_NAME, + ]); + $variable = new Variable(); + $res = $variable->throwExceptionIfVariableIsAssociatedAditionalTable($variable->VAR_UID); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/AdditionalTablesTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/AdditionalTablesTest.php index 1b4e38aa3..6ff4e3f4a 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/AdditionalTablesTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/AdditionalTablesTest.php @@ -44,6 +44,31 @@ class AdditionalTablesTest extends TestCase $this->assertCount(1, $table->offline([$table->ADD_TAB_OFFLINE])->get()); } + /** + * Test scope query to get the offline tables + * + * @covers \ProcessMaker\Model\AdditionalTables::scopeProcess() + * @test + */ + public function it_filter_process() + { + $table = factory(AdditionalTables::class)->create(); + $this->assertCount(1, $table->process($table->PRO_UID)->get()); + } + + /** + * Test scope query to get tables related to the process + * + * @covers \ProcessMaker\Model\AdditionalTables::getTables() + * @test + */ + public function it_get_tables_related_to_process() + { + $table = factory(AdditionalTables::class)->create(); + $result = AdditionalTables::getTables($table->PRO_UID); + $this->assertNotEmpty($result); + } + /** * Test get the structure of offline tables * diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/FieldsTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/FieldsTest.php index 9a629c24c..4bac32be7 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/FieldsTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/FieldsTest.php @@ -42,4 +42,19 @@ class FieldsTest extends TestCase $result = Fields::getFields($fields->ADD_TAB_UID); $this->assertNotEmpty($result); } + + /** + * Test scope and search a field related to the specific ADD_TAB_UID + * + * @covers \ProcessMaker\Model\Fields::scopeField() + * @covers \ProcessMaker\Model\Fields::scopeFieldOrLabel() + * @covers \ProcessMaker\Model\Fields::searchVariable() + * @test + */ + public function it_search_field() + { + $fields = factory(Fields::class)->create(); + $result = Fields::searchVariable($fields->ADD_TAB_UID, $fields->FLD_NAME); + $this->assertNotEmpty($result); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php index fcdbae846..bbe19a0c4 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessVariablesTest.php @@ -54,6 +54,19 @@ class ProcessVariablesTest extends TestCase $this->assertEquals($process[0]['PRO_UID'], $result[1]['PRJ_UID']); } + /** + * Test it return a variable related to the VAR_UID + * + * @covers \ProcessMaker\Model\ProcessVariables::getVariable() + * @test + */ + public function it_get_variable() + { + $table = factory(ProcessVariables::class)->create(); + $result = ProcessVariables::getVariable($table->VAR_UID); + $this->assertNotEmpty($result); + } + /** * Test it return the variables related to the PRO_ID * @@ -76,6 +89,8 @@ class ProcessVariablesTest extends TestCase /** * Test it return the variables by type related to the PRO_ID * + * @covers \ProcessMaker\Model\ProcessVariables::scopeProcessId() + * @covers \ProcessMaker\Model\ProcessVariables::scopeTypeId() * @covers \ProcessMaker\Model\ProcessVariables::getVariablesByType() * @test */ diff --git a/workflow/engine/content/translations/english/processmaker.en.po b/workflow/engine/content/translations/english/processmaker.en.po index 30091b7e5..2aa2d818b 100755 --- a/workflow/engine/content/translations/english/processmaker.en.po +++ b/workflow/engine/content/translations/english/processmaker.en.po @@ -28538,8 +28538,8 @@ msgstr "Variable for Value Based Assignment" # TRANSLATION # LABEL/ID_VARIABLE_ASSOCIATED_WITH_REPORT_TABLE #: LABEL/ID_VARIABLE_ASSOCIATED_WITH_REPORT_TABLE -msgid "The variable with \"{0}\", it is associated with a report table." -msgstr "The variable with \"{0}\", it is associated with a report table." +msgid "The variable \"{0}\" Is associated with a report table, that requires to be removed first before allowing deleting it." +msgstr "The variable \"{0}\" Is associated with a report table, that requires to be removed first before allowing deleting it."" # TRANSLATION # LABEL/ID_VARIABLE_IN_USE diff --git a/workflow/engine/data/mysql/insert.sql b/workflow/engine/data/mysql/insert.sql index ad4443d74..453774339 100755 --- a/workflow/engine/data/mysql/insert.sql +++ b/workflow/engine/data/mysql/insert.sql @@ -61742,7 +61742,7 @@ INSERT INTO TRANSLATION (TRN_CATEGORY,TRN_ID,TRN_LANG,TRN_VALUE,TRN_UPDATE_DATE ( 'LABEL','ID_VARIABLES_RECEIVED','en','variables received','2014-01-15') , ( 'LABEL','ID_VARIABLES_SENT','en','variables sent','2014-01-15') , ( 'LABEL','ID_VARIABLES_VALUE_ASSIGNMENT','en','Variable for Value Based Assignment','2014-01-15') , -( 'LABEL','ID_VARIABLE_ASSOCIATED_WITH_REPORT_TABLE','en','The variable with "{0}", it is associated with a report table.','2016-02-05') , +( 'LABEL','ID_VARIABLE_ASSOCIATED_WITH_REPORT_TABLE','en','The variable \"{0}\" Is associated with a report table, that requires to be removed first before allowing deleting it.','2022-04-08') , ( 'LABEL','ID_VARIABLE_IN_USE','en','This variable can not be deleted because it is being used in DynaForm : {0}. To delete it, first remove it from the DynaForm.','2015-04-08') , ( 'LABEL','ID_VARIABLE_NO_IS_GRID','en','The Variable with {0}: "{1}" is not a grid','2016-02-23') , ( 'LABEL','ID_VARIABLE_PICKER','en','pmVariablePicker','2014-01-15') , diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php index b47467564..1d01f9132 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php @@ -7,6 +7,8 @@ use Cases as ClassesCases; use Exception; use G; use PmDynaform; +use ProcessMaker\Model\AdditionalTables as AT; +use ProcessMaker\Model\Fields; use ProcessMaker\Model\ProcessVariables; use ProcessMaker\Util\Common; @@ -570,18 +572,19 @@ class Variable public function throwExceptionIfVariableIsAssociatedAditionalTable($variableUid) { try { - $criteria = new \Criteria('workflow'); - $criteria->addSelectColumn(\ProcessVariablesPeer::VAR_UID); - $criteria->addJoin(\ProcessVariablesPeer::PRJ_UID, \AdditionalTablesPeer::PRO_UID, \Criteria::INNER_JOIN); - $arrayCondition = []; - $arrayCondition[] = array(\AdditionalTablesPeer::ADD_TAB_UID, \FieldsPeer::ADD_TAB_UID, \Criteria::EQUAL); - $arrayCondition[] = array(\ProcessVariablesPeer::VAR_NAME, \FieldsPeer::FLD_NAME, \Criteria::EQUAL); - $criteria->addJoinMC($arrayCondition, \Criteria::INNER_JOIN); - $criteria->add(\ProcessVariablesPeer::VAR_UID, $variableUid, \Criteria::EQUAL); - $rsCriteria = \ProcessVariablesPeer::doSelectRS($criteria); - $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - if ($rsCriteria->next()) { - throw new Exception(G::LoadTranslation('ID_VARIABLE_ASSOCIATED_WITH_REPORT_TABLE', array($variableUid))); + // Get variable name + $varInfo = ProcessVariables::getVariable($variableUid); + $varName = $varInfo['VAR_NAME']; + $proUid = $varInfo['PRJ_UID']; + // Get the tables related to the process + $tables = AT::getTables($proUid); + if (!empty($tables)) { + foreach ($tables as $value) { + $exist = Fields::searchVariable($value['ADD_TAB_UID'], $varName); + if ($exist) { + throw new Exception(G::LoadTranslation('ID_VARIABLE_ASSOCIATED_WITH_REPORT_TABLE', [$varName])); + } + } } } catch (Exception $e) { throw $e; diff --git a/workflow/engine/src/ProcessMaker/Model/AdditionalTables.php b/workflow/engine/src/ProcessMaker/Model/AdditionalTables.php index c25583b75..c9e77ab40 100644 --- a/workflow/engine/src/ProcessMaker/Model/AdditionalTables.php +++ b/workflow/engine/src/ProcessMaker/Model/AdditionalTables.php @@ -32,6 +32,34 @@ class AdditionalTables extends Model return $query->where('ADD_TAB_OFFLINE', '=', 1); } + /** + * Scope a query to get the tables related to the process + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $proUid + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeProcess($query, string $proUid) + { + return $query->where('PRO_UID', $proUid); + } + + /** + * Get tables related to the process + * + * @param string $proUid + * @return array + */ + public static function getTables(string $proUid) + { + $query = AdditionalTables::query()->select(); + $query->process($proUid); + $result = $query->get()->values()->toArray(); + + return $result; + } + /** * Get the structure of offline tables * diff --git a/workflow/engine/src/ProcessMaker/Model/Fields.php b/workflow/engine/src/ProcessMaker/Model/Fields.php index 6283dd69c..e5841b8d6 100644 --- a/workflow/engine/src/ProcessMaker/Model/Fields.php +++ b/workflow/engine/src/ProcessMaker/Model/Fields.php @@ -32,6 +32,39 @@ class Fields extends Model return $query->where('ADD_TAB_UID', '=', $tabUid); } + /** + * Scope a query to get the field name + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $name + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeField($query, $name) + { + return $query->where('FLD_NAME', $name); + } + + /** + * Scope a query to get the field name or label name + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $field + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeFieldOrLabel($query, $field) + { + $query->where(function ($query) use ($field) { + $query->field($field); + $fieldLabel = $field . '_label'; + $query->orWhere(function ($query) use ($fieldLabel) { + $query->field($fieldLabel); + }); + }); + return $query; + } + /** * Get the offline tables * @@ -52,4 +85,22 @@ class Fields extends Model return $data; } + + /** + * Search a field related to the table + * + * @param string $tabUid + * @param string $field + * + * @return bool + */ + public static function searchVariable(string $tabUid, string $field) + { + $query = Fields::query(); + $query->table($tabUid); + $query->fieldOrLabel($field); + $result = $query->get()->values()->toArray(); + + return !empty($result); + } } \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php b/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php index 8647380e7..53cd4d7d2 100644 --- a/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php +++ b/workflow/engine/src/ProcessMaker/Model/ProcessVariables.php @@ -87,6 +87,23 @@ class ProcessVariables extends Model return $query->where('VAR_FIELD_TYPE_ID', $typeId); } + /** + * Return the variable information + * + * @param string $varUid + * + * @return array + */ + public static function getVariable(string $varUid) + { + $query = ProcessVariables::query()->select(); + $query->where('VAR_UID', $varUid)->limit(1); + $result = $query->get()->values()->toArray(); + $result = head($result); + + return $result; + } + /** * Return the variables list *