diff --git a/app/Helpers/CustomFormHelper.php b/app/Helpers/CustomFormHelper.php index 1622a0ae9..a089fee7d 100644 --- a/app/Helpers/CustomFormHelper.php +++ b/app/Helpers/CustomFormHelper.php @@ -21,7 +21,7 @@ class CustomFormHelper * @param string $framework [ppc,terrafund] * @return Form */ - public static function generateFakeForm(string $type, string $framework): Form + public static function generateFakeForm(string $type, string $framework, bool $withRelations = false): Form { switch ($type) { case 'project': @@ -59,16 +59,24 @@ public static function generateFakeForm(string $type, string $framework): Form $form = Form::factory()->create(['framework_key' => $framework, 'model' => $model]); $section = FormSection::factory()->create(['form_id' => $form->uuid]); - foreach (config('wri.linked-fields.models.' . $type . '.fields') as $key => $fieldCfg) { - FormQuestion::factory()->create( - [ - 'input_type' => data_get($fieldCfg, 'input_type'), - 'label' => data_get($fieldCfg, 'label'), - 'name' => data_get($fieldCfg, 'label'), - 'form_section_id' => $section->id, - 'linked_field_key' => $key, - ] - ); + + $generateQuestions = function ($subtype) use ($type, $section) { + foreach (config("wri.linked-fields.models.$type.$subtype") as $key => $fieldCfg) { + FormQuestion::factory()->create( + [ + 'input_type' => data_get($fieldCfg, 'input_type'), + 'label' => data_get($fieldCfg, 'label'), + 'name' => data_get($fieldCfg, 'label'), + 'form_section_id' => $section->id, + 'linked_field_key' => $key, + ] + ); + } + }; + + $generateQuestions('fields'); + if ($withRelations) { + $generateQuestions('relations'); } $conditional = FormQuestion::factory()->conditionalField()->create(['form_section_id' => $section->id]); diff --git a/tests/V2/ProjectReports/UpdateProjectReportWithFormControllerTest.php b/tests/V2/ProjectReports/UpdateProjectReportWithFormControllerTest.php index 18e139e50..bee899891 100644 --- a/tests/V2/ProjectReports/UpdateProjectReportWithFormControllerTest.php +++ b/tests/V2/ProjectReports/UpdateProjectReportWithFormControllerTest.php @@ -44,7 +44,7 @@ public function test_invoke_action() 'status' => EntityStatusStateMachine::STARTED, ]); - $form = CustomFormHelper::generateFakeForm('project-report', 'ppc'); + $form = CustomFormHelper::generateFakeForm('project-report', 'ppc', true); $answers = []; @@ -59,8 +59,24 @@ public function test_invoke_action() if ($question->linked_field_key == 'pro-rep-title') { $answers[$question->uuid] = '* testing title updated *'; } - if ($question->linked_field_key == 'pro-rep-workdays-paid') { - $answers[$question->uuid] = 24; + if ($question->linked_field_key == 'pro-rep-rel-paid-project-management') { + $answers[$question->uuid] = [[ + 'collection' => 'paid-project-management', + 'demographics' => [[ + 'type' => 'gender', + 'name' => 'male', + 'amount' => 24, + ], [ + 'type' => 'age', + 'name' => 'youth', + 'amount' => 24, + ], [ + 'type' => 'ethnicity', + 'subtype' => 'indigenous', + 'name' => 'Ohlone', + 'amount' => 24, + ]], + ]]; } }