opnform-host-nginx/api/tests/Feature/Forms/FormTest.php

301 lines
10 KiB
PHP
Raw Normal View History

2022-09-20 21:59:52 +02: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 11:54:12 +01:00
'message' => 'Form created.',
2022-09-20 21:59:52 +02: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 21:59:52 +02:00
});
it('can fetch a form', function () {
$user = $this->actingAsProUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$response = $this->getJson(route('open.forms.show', $form->slug))
->assertSuccessful()
->assertJson([
'id' => $form->id,
'title' => $form->title
]);
});
2022-09-20 21:59:52 +02: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 11:54:12 +01:00
'message' => 'Form updated.',
2022-09-20 21:59:52 +02:00
]);
$this->assertDatabaseHas('forms', [
'id' => $form->id,
'title' => $form->title
2022-09-20 21:59:52 +02:00
]);
});
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) {
if (!is_string($value) || (preg_match(
2024-02-23 11:54:12 +01:00
'/^[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 21:59:52 +02:00
return false;
}
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02: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 21:59:52 +02: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',
'message' => 'Form successfully duplicated. You are now editing the duplicated version of the form.',
2022-09-20 21:59:52 +02: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
2022-09-20 21:59:52 +02:00
]);
});
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 11:54:12 +01:00
'message' => 'Form was deleted.',
2022-09-20 21:59:52 +02: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 11:54:12 +01:00
'dark_mode' => 'dark',
2022-09-20 21:59:52 +02:00
]);
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->postJson(route('open.forms.store', $formData))
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 11:54:12 +01:00
'message' => 'Form created.',
2022-09-20 21:59:52 +02: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();
});
});
it('can create form with custom scripts', function () {
$user = $this->actingAsTrialingUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'custom_code' => "<script>console.log('Hello')</script>",
]);
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->postJson(route('open.forms.store', $formData))
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form successfully created, but the Non-trial features you used will be disabled when sharing your form:',
'form' => ['custom_code' => null]
]);
$this->getJson(route('forms.show', $form->slug))
->assertSuccessful()->assertJson([
'id' => $form->id,
'title' => $form->title,
'custom_code' => null
]);
})->skip(true, 'Trialing custom script form cleaning disabled for now.');
Slug customisation (#755) * Enhance Form Slug Handling and Validation Logic - Updated `FormController.php` to conditionally set the form slug based on the `self_hosted` configuration, ensuring proper slug assignment during form creation and updates. - Introduced `CustomSlugRule.php` to validate custom slugs, enforcing format and uniqueness constraints, and integrated this rule into `UserFormRequest.php`. - Enhanced the `FormCustomSeo.vue` component to include a field for custom URL slugs, improving user experience by allowing users to define unique identifiers for their forms. - Updated API routes to apply middleware for form updates, ensuring proper form resolution during requests. These changes aim to improve the functionality and user experience related to form slug management and validation. * Test case for Custom slug * Update OpenCompleteForm and FormCustomSeo for Improved Functionality and Clarity - Modified `OpenCompleteForm.vue` to ensure `submissionId` is correctly referenced as `submissionId.value`, enhancing data handling during form initialization. - Updated `FormCustomSeo.vue` to rename "Custom URL Slug" to "Custom Form URL" for better clarity and user understanding, ensuring consistent terminology across the application. - Enhanced `useFormInitialization.js` to include `submission_id` in the data passed to `form.resetAndFill`, improving the accuracy of form data handling. These changes aim to improve the functionality and user experience of the form components by ensuring correct data references and clearer labeling. --------- Co-authored-by: Julien Nahum <julien@nahum.net>
2025-05-19 21:06:54 +02:00
it('can not set custom slug when not self hosted', function () {
config(['app.self_hosted' => false]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$form->slug = 'my-custom-slug-123';
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$response = $this->postJson(route('open.forms.store', $formData))
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form created.'
]);
$this->assertNotEquals($response->json('form.slug'), 'my-custom-slug-123');
});
it('can set custom slug when self hosted', function () {
config(['app.self_hosted' => true]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$form->slug = 'my-custom-slug-123';
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$response = $this->postJson(route('open.forms.store', $formData))
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form created.'
]);
$this->assertEquals($response->json('form.slug'), 'my-custom-slug-123');
});
it('rejects invalid custom slug format when self hosted', function () {
config(['app.self_hosted' => true]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$form->slug = 'Invalid Slug!@#';
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->postJson(route('open.forms.store', $formData))
->assertUnprocessable()
->assertJsonValidationErrors(['slug']);
});
it('rejects duplicate custom slug when self hosted', function () {
config(['app.self_hosted' => true]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
// Create first form with custom slug
$form1 = $this->createForm($user, $workspace);
$form1->update(['slug' => 'duplicate-slug']);
// Try to create second form with same slug
$form2 = $this->makeForm($user, $workspace);
$form2->slug = $form1->slug;
$formData = (new \App\Http\Resources\FormResource($form2))->toArray(request());
$this->postJson(route('open.forms.store', $formData))
->assertUnprocessable()
->assertJsonValidationErrors(['slug']);
});
it('allows empty custom slug when self hosted', function () {
config(['app.self_hosted' => true]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$form->slug = null;
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->postJson(route('open.forms.store', $formData))
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form created.',
]);
});
it('can update form with custom slug when self hosted', function () {
config(['app.self_hosted' => true]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$form->slug = 'updated-custom-slug';
$formData = (new \App\Http\Resources\FormResource($form))->toArray(request());
$this->putJson(route('open.forms.update', $form->id), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form updated.',
]);
$this->assertDatabaseHas('forms', [
'id' => $form->id,
'slug' => 'updated-custom-slug'
]);
});