diff --git a/tests/unit/workflow/engine/classes/PmDynaformTest.php b/tests/unit/workflow/engine/classes/PmDynaformTest.php index d6c033209..8e1e07be4 100644 --- a/tests/unit/workflow/engine/classes/PmDynaformTest.php +++ b/tests/unit/workflow/engine/classes/PmDynaformTest.php @@ -532,4 +532,271 @@ class PmDynaformTest extends TestCase ] ]; } + + /** + * It tests that the json file is getting the defined values when the grid has one undefined control + * + * @test + */ + public function it_should_add_the_correct_fields_with_a_single_undefined_control() + { + //Creates the PmDynaform object + $pmDynaform = new PmDynaform(); + + //A json that contains the text control data and columns + $jsonData = (object)( + [ + "data" => (object)([ + "1" => [ + ["value" => "textControl1", "label" => "textControl1"], + ["value" => "textControl2", "label" => "textControl2"], + ["value" => "", "label" => ""] + ] + ]), + + "columns" => (object)([ + 0 => (object)([ + "id" => "text0000000001", + "name" => "text0000000001", + ]), + 1 => (object)([ + "id" => "textarea0000000001", + "name" => "textarea0000000001", + ]), + 2 => (object)([ + "id" => "text0000000002", + "name" => "text0000000002", + ]) + ]) + ] + ); + + // An array that contains the variables stored on the App Data + $appData = [ + "1" => [ + "text0000000001" => "", + "text0000000001_label" => "", + "textarea0000000001" => "", + "textarea0000000001_label" => "" + ] + ]; + + //Calls the setDataSchema method + $resultText = $pmDynaform->setDataSchema($jsonData, $appData); + + //This assert the result is null + $this->assertNull($resultText); + + //Assert the 'dataSchema' field was added + $this->assertObjectHasAttribute('dataSchema', $jsonData); + + //It asserts the first control is defined + $this->assertTrue($jsonData->dataSchema['1'][0]['defined']); + + //It asserts the second control is defined + $this->assertTrue($jsonData->dataSchema['1'][1]['defined']); + + //It asserts the second control is undefined + $this->assertFalse($jsonData->dataSchema['1'][2]['defined']); + } + + /** + * It tests that the json file is getting the defined values when the grid has more than one undefined control + * + * @test + */ + public function it_should_add_the_correct_fields_with_more_than_one_undefined_control() + { + //Creates the PmDynaform object + $pmDynaform = new PmDynaform(); + + //A json that contains the text control data and columns + $jsonData = (object)( + [ + "data" => (object)([ + "1" => [ + ["value" => "textControl1", "label" => "textControl1"], + ["value" => "textAreaControl2", "label" => "textAreaControl2"], + ["value" => "dropdowncontrol1", "label" => "dropdowncontrol1"], + ["value" => "", "label" => ""], + ["value" => "", "label" => ""] + ] + ]), + "columns" => (object)([ + 0 => (object)([ + "id" => "text0000000001", + "name" => "text0000000001", + ]), + 1 => (object)([ + "id" => "textarea0000000001", + "name" => "textarea0000000001", + ]), + 2 => (object)([ + "id" => "dropdown0000000001", + "name" => "dropdown0000000001", + ]), + 3 => (object)([ + "id" => "text0000000002", + "name" => "text0000000002", + ]), + 4 => (object)([ + "id" => "text0000000003", + "name" => "text0000000003", + ]) + ]) + ] + ); + + // An array that contains the variables stored on the App Data + $appData = [ + "1" => [ + "text0000000001" => "", + "text0000000001_label" => "", + "textarea0000000001" => "", + "textarea0000000001_label" => "", + "dropdown0000000001" => "", + "dropdown0000000001_label" => "" + ] + ]; + + //Calls the setDataSchema method + $resultText = $pmDynaform->setDataSchema($jsonData, $appData); + + //This assert the result is null + $this->assertNull($resultText); + + //Assert the 'dataSchema' field was added + $this->assertObjectHasAttribute('dataSchema', $jsonData); + + //It asserts the first control is defined + $this->assertTrue($jsonData->dataSchema['1'][0]['defined']); + + //It asserts the second control is defined + $this->assertTrue($jsonData->dataSchema['1'][1]['defined']); + + //It asserts the third control is defined + $this->assertTrue($jsonData->dataSchema['1'][2]['defined']); + + //It asserts the fourth control is undefined + $this->assertFalse($jsonData->dataSchema['1'][3]['defined']); + + //It asserts the fifth control is undefined + $this->assertFalse($jsonData->dataSchema['1'][4]['defined']); + } + + /** + * It tests that the json file is getting the defined and undefined values when the grid has more than one row + * + * @test + */ + public function it_should_add_the_correct_fields_with_more_than_one_rows() + { + //Creates the PmDynaform object + $pmDynaform = new PmDynaform(); + + //A json that contains the text control data and columns + $jsonData = (object)( + [ + "data" => (object)([ + "1" => [ + ["value" => "textControl1", "label" => "textControl1"], + ["value" => "textAreaControl2", "label" => "textAreaControl2"], + ["value" => "dropdowncontrol1", "label" => "dropdowncontrol1"], + ["value" => "", "label" => ""], + ["value" => "", "label" => ""] + ], + "2" => [ + ["value" => "textControl1", "label" => "textControl1"], + ["value" => "textAreaControl2", "label" => "textAreaControl2"], + ["value" => "dropdowncontrol1", "label" => "dropdowncontrol1"], + ["value" => "", "label" => ""], + ["value" => "", "label" => ""] + ], + "3" => [ + ["value" => "textControl1", "label" => "textControl1"], + ["value" => "textAreaControl2", "label" => "textAreaControl2"], + ["value" => "dropdowncontrol1", "label" => "dropdowncontrol1"], + ["value" => "", "label" => ""], + ["value" => "", "label" => ""] + ] + ]), + "columns" => (object)([ + 0 => (object)([ + "id" => "text0000000001", + "name" => "text0000000001", + ]), + 1 => (object)([ + "id" => "textarea0000000001", + "name" => "textarea0000000001", + ]), + 2 => (object)([ + "id" => "dropdown0000000001", + "name" => "dropdown0000000001", + ]), + 3 => (object)([ + "id" => "text0000000002", + "name" => "text0000000002", + ]), + 4 => (object)([ + "id" => "text0000000003", + "name" => "text0000000003", + ]) + ]) + ] + ); + + // An array that contains the variables stored on the App Data + $appData = [ + "1" => [ + "text0000000001" => "", + "text0000000001_label" => "", + "textarea0000000001" => "", + "textarea0000000001_label" => "", + "dropdown0000000001" => "", + "dropdown0000000001_label" => "" + ], + "2" => [ + "text0000000001" => "", + "text0000000001_label" => "", + "textarea0000000001" => "", + "textarea0000000001_label" => "", + "dropdown0000000001" => "", + "dropdown0000000001_label" => "" + ], + "3" => [ + "text0000000001" => "", + "text0000000001_label" => "", + "textarea0000000001" => "", + "textarea0000000001_label" => "", + "dropdown0000000001" => "", + "dropdown0000000001_label" => "" + ] + ]; + + //Calls the setDataSchema method + $resultText = $pmDynaform->setDataSchema($jsonData, $appData); + + //This assert the result is null + $this->assertNull($resultText); + + //Assert the 'dataSchema' field was added + $this->assertObjectHasAttribute('dataSchema', $jsonData); + + foreach ($jsonData->dataSchema as $key => $value) { + //It asserts the first control is defined + $this->assertTrue($jsonData->dataSchema[$key][0]['defined']); + + //It asserts the second control is defined + $this->assertTrue($jsonData->dataSchema[$key][1]['defined']); + + //It asserts the third control is defined + $this->assertTrue($jsonData->dataSchema[$key][2]['defined']); + + //It asserts the fourth control is undefined + $this->assertFalse($jsonData->dataSchema[$key][3]['defined']); + + //It asserts the fifth control is undefined + $this->assertFalse($jsonData->dataSchema[$key][4]['defined']); + } + } } diff --git a/workflow/engine/classes/PmDynaform.php b/workflow/engine/classes/PmDynaform.php index a4b8d5262..7ea9d03df 100644 --- a/workflow/engine/classes/PmDynaform.php +++ b/workflow/engine/classes/PmDynaform.php @@ -642,6 +642,8 @@ class PmDynaform } $json->rows = count($rows); $json->data = $rows; + + $this->setDataSchema($json, $this->fields["APP_DATA"][$json->name]); } } //languages @@ -2265,4 +2267,32 @@ class PmDynaform } return $message; } + + /** + * This adds a new definition on the json dynaform + * @param json $json + * + * @link https://wiki.processmaker.com/3.0/Grid_Control + * @see workflow/engine/classes/PmDynaform->jsonr + */ + public function setDataSchema($json, $appDataVariables) + { + foreach ($json->data as $key => $value) { + $columnsData = []; + foreach ($json->columns as $keyData => $valueData) { + foreach ($appDataVariables as $keyAppData => $valueAppData) { + if (array_key_exists($valueData->id, $valueAppData) || array_key_exists($valueData->id . "_label", + $valueAppData) || array_key_exists($valueData->name, + $valueAppData) || array_key_exists($valueData->name . "_label", $valueAppData)) { + array_push($columnsData, ["defined" => true]); + break; + } else { + array_push($columnsData, ["defined" => false]); + break; + } + } + } + $json->dataSchema[$key] = $columnsData; + } + } }