From 7bafb1b13a2fe8b45dcf443f0f60ec30341e354f Mon Sep 17 00:00:00 2001 From: Philipp Reinking Date: Tue, 20 Aug 2024 18:26:35 +0200 Subject: [PATCH] add new import logic for groups --- .../Traits/TemplateExportsAndImports.php | 58 ++++++++++++++----- tests/Feature/FormTemplateTest.php | 49 +++++++++++++++- 2 files changed, 90 insertions(+), 17 deletions(-) diff --git a/app/Models/Traits/TemplateExportsAndImports.php b/app/Models/Traits/TemplateExportsAndImports.php index d0e6f798..c237f46a 100644 --- a/app/Models/Traits/TemplateExportsAndImports.php +++ b/app/Models/Traits/TemplateExportsAndImports.php @@ -4,6 +4,7 @@ use App\Models\Form; use App\Models\FormBlock; +use App\Enums\FormBlockType; use App\Models\FormBlockInteraction; trait TemplateExportsAndImports @@ -41,25 +42,50 @@ public function applyTemplate(array|string $template) $this->formBlocks()->delete(); // Create new form blocks - $blocks->each(function ($item) { - $item = collect($item); - $block = $this->formBlocks() - ->create( - $item - ->only(FormBlock::TEMPLATE_ATTRIBUTES) - ->toArray() - ); + $blocks->each(function ($item) use ($blocks) { + if (isset($item['parent_block'])) { + return; + } - // Attach the form blocks interactions, if they exist - if ($item->has('formBlockInteractions') && count((array) $item->get('formBlockInteractions', []))) { - collect($item->get('formBlockInteractions', []))->each(function ($interaction) use ($block) { - $block->formBlockInteractions()->create( - collect($interaction) - ->only(FormBlockInteraction::TEMPLATE_ATTRIBUTES) - ->toArray() - ); + $block = $this->applyBlockTemplate($item); + + if ($block->type === FormBlockType::group) { + $childBlocks = $blocks->filter(function ($child) use ($item) { + return $item['id'] === $child['parent_block']; + }); + + $childBlocks->each(function ($child) use ($block) { + $this->applyBlockTemplate($child, $block->uuid); }); } }); } + + protected function applyBlockTemplate($item, $newParentBlock = null) + { + $item = collect($item); + + $attributes = $item + ->only(FormBlock::TEMPLATE_ATTRIBUTES) + ->toArray(); + + if ($newParentBlock) { + $attributes['parent_block'] = $newParentBlock; + } + + $block = $this->formBlocks()->create($attributes); + + // Attach the form blocks interactions, if they exist + if ($item->has('formBlockInteractions') && count((array) $item->get('formBlockInteractions', []))) { + collect($item->get('formBlockInteractions', []))->each(function ($interaction) use ($block) { + $block->formBlockInteractions()->create( + collect($interaction) + ->only(FormBlockInteraction::TEMPLATE_ATTRIBUTES) + ->toArray() + ); + }); + } + + return $block; + } } diff --git a/tests/Feature/FormTemplateTest.php b/tests/Feature/FormTemplateTest.php index 4f968f46..41dafd79 100644 --- a/tests/Feature/FormTemplateTest.php +++ b/tests/Feature/FormTemplateTest.php @@ -71,7 +71,7 @@ 'type' => FormBlockType::group, ]); - $childBlock = FormBlock::factory()->create([ + FormBlock::factory()->create([ 'form_id' => $form->id, 'type' => FormBlockType::none, 'parent_block' => $groupBlock->uuid, @@ -89,6 +89,53 @@ $this->assertArrayHasKey('parent_block', $response->json('blocks.1')); }); +test('can import a form that has a group block with a child block', function () { + $form = Form::factory() + ->create([ + 'name' => 'Test Form', + ]); + + $groupBlock = FormBlock::factory()->create([ + 'form_id' => $form->id, + 'type' => FormBlockType::group, + ]); + + FormBlock::factory()->create([ + 'form_id' => $form->id, + 'type' => FormBlockType::none, + 'parent_block' => $groupBlock->uuid, + ]); + + $importTemplateString = $this->actingAs($form->user) + ->json('GET', route('api.forms.template-export', [ + 'form' => $form->uuid, + ]))->assertStatus(200)->content(); + + + // create a new form to import the template + $user = User::factory()->withTeam()->create(); + + $newForm = Form::factory()->create([ + 'name' => 'Test Form', + 'description' => 'A template Import Test', + 'user_id' => $user->id, + ]); + + // import the template + $this->actingAs($user)->post(route('api.forms.template-import', [ + 'form' => $newForm->uuid, + ]), [ + 'template' => $importTemplateString, + ])->assertStatus(200); + + $this->assertFalse($newForm->formBlocks[0]->uuid === $groupBlock->uuid); + $this->assertNull($newForm->formBlocks[0]->parent_block); + $this->assertNotNull($newForm->formBlocks[1]->parent_block); + + // assert that the children reference the correct parent block + $this->assertEquals($newForm->formBlocks[1]->parent_block, $newForm->formBlocks[0]->uuid); +}); + test('can import a string template for an existing form', function () { /** @var User $user */ $user = User::factory()->withTeam()->create();