Add formtemplate to form index

This commit is contained in:
philipp lang 2024-01-01 16:53:32 +01:00
parent 5cf49489d5
commit c3db854eb5
4 changed files with 42 additions and 3 deletions

View File

@ -3,6 +3,8 @@
namespace App\Form\Resources;
use App\Form\Models\Form;
use App\Form\Models\Formtemplate;
use App\Lib\HasMeta;
use Illuminate\Http\Resources\Json\JsonResource;
/**
@ -10,6 +12,9 @@ use Illuminate\Http\Resources\Json\JsonResource;
*/
class FormResource extends JsonResource
{
use HasMeta;
/**
* Transform the resource into an array.
*
@ -33,4 +38,14 @@ class FormResource extends JsonResource
'config' => $this->config,
];
}
public static function meta(): array
{
return [
'links' => [
'store' => route('form.store'),
],
'templates' => FormtemplateResource::collection(Formtemplate::get()),
];
}
}

View File

@ -4,9 +4,11 @@ namespace Database\Factories\Form\Models;
use App\Form\Models\Formtemplate;
use Illuminate\Database\Eloquent\Factories\Factory;
use Tests\Feature\Form\FormtemplateSectionRequest;
/**
* @extends Factory<Formtemplate>
* @method self name(string $name)
*/
class FormtemplateFactory extends Factory
{
@ -25,4 +27,20 @@ class FormtemplateFactory extends Factory
'config' => [],
];
}
/**
* @param array<int, FormtemplateSectionRequest> $sections
*/
public function sections(array $sections): self
{
return $this->state(['config' => ['sections' => $sections]]);
}
/**
* @param mixed $args
*/
public function __call($method, $parameters): self
{
return $this->state([str($method)->snake()->toString() => $parameters[0]]);
}
}

View File

@ -3,6 +3,7 @@
namespace Tests\Feature\Form;
use App\Form\Models\Form;
use App\Form\Models\Formtemplate;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
@ -14,7 +15,8 @@ class FormIndexActionTest extends TestCase
public function testItDisplaysForms(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()
Formtemplate::factory()->name('tname')->sections([FormtemplateSectionRequest::new()->name('sname')->create()])->create();
Form::factory()
->name('lala')
->excerpt('fff')
->description('desc')
@ -40,6 +42,9 @@ class FormIndexActionTest extends TestCase
->assertInertiaPath('data.data.0.from', '2023-05-05')
->assertInertiaPath('data.data.0.to', '2023-06-07')
->assertInertiaPath('data.data.0.registration_from', '2023-05-06 04:00:00')
->assertInertiaPath('data.data.0.registration_until', '2023-04-01 05:00:00');
->assertInertiaPath('data.data.0.registration_until', '2023-04-01 05:00:00')
->assertInertiaPath('data.meta.links.store', route('form.store'))
->assertInertiaPath('data.meta.templates.0.name', 'tname')
->assertInertiaPath('data.meta.templates.0.config.sections.0.name', 'sname');
}
}

View File

@ -14,7 +14,7 @@ class FormtemplateIndexActionTest extends TestCase
public function testItDisplaysIndexPage(): void
{
$formtemplate = Formtemplate::factory()->create();
$formtemplate = Formtemplate::factory()->sections([FormtemplateSectionRequest::new()->name('sname')->create()])->create();
$group = Group::factory()->has(Group::factory()->state(['inner_name' => 'child']), 'children')->create(['inner_name' => 'root']);
$this->login()->loginNami(12345, 'pasword', $group)->withoutExceptionHandling();
@ -23,6 +23,7 @@ class FormtemplateIndexActionTest extends TestCase
->assertInertiaPath('data.data.0.links', [
'update' => route('formtemplate.update', ['formtemplate' => $formtemplate]),
])
->assertInertiaPath('data.data.0.config.sections.0.name', 'sname')
->assertInertiaPath('data.meta.groups', [
['id' => $group->id, 'name' => 'root'],
['id' => $group->children->first()->id, 'name' => '-- child'],