opnform/tests/Feature/Forms/FormTest.php

159 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
use Illuminate\Testing\Fluent\AssertableJson;
it('can create a contact form', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$formData = new \App\Http\Resources\FormResource($form);
$response = $this->postJson(route('open.forms.store', $formData->toArray(request())))
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form created.',
2022-09-20 19:59:52 +00:00
]);
expect($workspace->forms()->count())->toBe(1);
$this->assertDatabaseHas('forms', [
'id' => $response->json('form.id'),
]);
});
it('can fetch forms', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$this->getJson(route('open.workspaces.forms.index', $workspace->id))
->assertSuccessful()
->assertJsonCount(3)
->assertSuccessful()
->assertJsonPath('data.0.id', $form->id)
->assertJsonPath('data.0.title', $form->title);
2022-09-20 19:59:52 +00:00
});
it('can update a form', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$newFormData = $this->makeForm($user, $workspace);
$form->fill((new \App\Http\Resources\FormResource($newFormData))->toArray(request()));
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->putJson(route('open.forms.update', $form->id), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form updated.',
2022-09-20 19:59:52 +00:00
]);
$this->assertDatabaseHas('forms', [
'id' => $form->id,
'title' => $form->title,
'description' => $form->description,
]);
});
it('can regenerate a form url', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$newFormData = $this->makeForm($user, $workspace);
$form->update([
'title' => $newFormData->title,
]);
$form->generateSlug();
$newSlug = $form->slug;
$this->putJson(route('open.forms.regenerate-link', [$form->id, 'uuid']))
->assertSuccessful()
->assertJson(function (AssertableJson $json) {
return $json->where('type', 'success')
->where('form.slug', function ($value) {
2024-02-23 10:54:12 +00:00
if (! is_string($value) || (preg_match(
'/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/',
$value
) !== 1)) {
2022-09-20 19:59:52 +00:00
return false;
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return true;
})
->etc();
});
$this->putJson(route('open.forms.regenerate-link', [$form->id, 'slug']))
->assertSuccessful()
->assertJson(function (AssertableJson $json) use ($newSlug) {
return $json->where('type', 'success')
->where('form.slug', function ($slug) use ($newSlug) {
return substr($slug, 0, -6) == substr($newSlug, 0, -6);
})
2022-09-20 19:59:52 +00:00
->etc();
});
});
it('can duplicate a form', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$response = $this->postJson(route('open.forms.duplicate', $form->id))
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form successfully duplicated.',
2022-09-20 19:59:52 +00:00
]);
expect($user->forms()->count())->toBe(2);
expect($workspace->forms()->count())->toBe(2);
$this->assertDatabaseHas('forms', [
'id' => $response->json('new_form.id'),
'title' => 'Copy of '.$form->title,
'description' => $form->description,
]);
});
it('can delete a form', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$this->deleteJson(route('open.forms.destroy', $form->id))
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form was deleted.',
2022-09-20 19:59:52 +00:00
]);
expect($user->forms()->count())->toBe(0);
expect($workspace->forms()->count())->toBe(0);
});
it('can create form with dark mode', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
2024-02-23 10:54:12 +00:00
'dark_mode' => 'dark',
2022-09-20 19:59:52 +00:00
]);
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->postJson(route('open.forms.store', $formData))
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form created.',
2022-09-20 19:59:52 +00:00
]);
$this->getJson(route('forms.show', $form->slug))
->assertSuccessful()
->assertJson(function (AssertableJson $json) use ($form) {
return $json->where('id', $form->id)
->where('dark_mode', 'dark')
->etc();
});
});