Skip to content

Commit

Permalink
add new import logic for groups
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilReinking committed Aug 20, 2024
1 parent 6cf95d6 commit 7bafb1b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
58 changes: 42 additions & 16 deletions app/Models/Traits/TemplateExportsAndImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Form;
use App\Models\FormBlock;
use App\Enums\FormBlockType;
use App\Models\FormBlockInteraction;

trait TemplateExportsAndImports
Expand Down Expand Up @@ -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;
}
}
49 changes: 48 additions & 1 deletion tests/Feature/FormTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down

0 comments on commit 7bafb1b

Please sign in to comment.