Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'automagic_forms' => 'Automagic Forms',
'enable_automagic_form' => 'Enable Automagic Form',
'enable_automagic_form_instructions' => "Generates a shareable URL for this form, so you don't need to build a page for it.",
'automagic_form_unique_instances' => "Automagic Forms can't be used with Unique Instances.",
'automagic_form_url' => 'Automagic Form URL',
'automagic_form_url_instructions' => "The URL for this form. Share it with anyone you'd like to fill it out.",
'automagic_form_success_heading' => 'Success Heading',
Expand Down
6 changes: 6 additions & 0 deletions src/AutomagicForms/AutomagicForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\FormsPro\AutomagicForms;

use Statamic\Contracts\Forms\Form;
use Statamic\Facades\Site;

class AutomagicForms
Expand All @@ -11,6 +12,11 @@ public static function enabled(): bool
return config('statamic.forms-pro.automagic_forms.enabled');
}

public static function availableFor(Form $form): bool
{
return $form->get('automagic_form') && ! $form->hasUniqueInstances();
}

public static function route(string $name, array $params = [], ?string $site = null, bool $absolute = true): string
{
$site ??= Site::current()->handle();
Expand Down
2 changes: 1 addition & 1 deletion src/AutomagicForms/FormLinkType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FormLinkType extends LinkType

public function resolve(string $id, $parent = null, bool $localize = false): mixed
{
if (! $form = Form::find($id)) {
if (! ($form = Form::find($id)) || ! AutomagicForms::availableFor($form)) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/Controllers/AutomagicFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function index(Request $request, string $form, ?string $page = null)
throw_unless(AutomagicForms::enabled(), NotFoundHttpException::class);
throw_unless($form = Facades\Form::find($form), NotFoundHttpException::class);
throw_unless($currentPage = $this->currentPage($form, $page), NotFoundHttpException::class);
throw_unless($form->get('automagic_form'), NotFoundHttpException::class);
throw_unless(AutomagicForms::availableFor($form), NotFoundHttpException::class);

if ($form->restricted()) {
return redirect(AutomagicForms::route('restricted', ['form' => $form->handle()]));
Expand Down Expand Up @@ -71,7 +71,7 @@ public function store(Request $request, string $form, string $page, SubmitForm $
throw_unless(AutomagicForms::enabled(), NotFoundHttpException::class);
throw_unless($form = Facades\Form::find($form), NotFoundHttpException::class);
throw_unless($currentPage = $this->currentPage($form, $page), NotFoundHttpException::class);
throw_unless($form->get('automagic_form'), NotFoundHttpException::class);
throw_unless(AutomagicForms::availableFor($form), NotFoundHttpException::class);

if ($form->restricted()) {
return response()->json([
Expand Down Expand Up @@ -137,7 +137,7 @@ public function restricted(Request $request, string $form)
{
throw_unless(AutomagicForms::enabled(), NotFoundHttpException::class);
throw_unless($form = Facades\Form::find($form), NotFoundHttpException::class);
throw_unless($form->get('automagic_form'), NotFoundHttpException::class);
throw_unless(AutomagicForms::availableFor($form), NotFoundHttpException::class);

if (! $form->restricted()) {
return redirect(AutomagicForms::route('index', ['form' => $form->handle()]));
Expand All @@ -153,7 +153,7 @@ public function submitted(Request $request, string $form)
{
throw_unless(AutomagicForms::enabled(), NotFoundHttpException::class);
throw_unless($form = Facades\Form::find($form), NotFoundHttpException::class);
throw_unless($form->get('automagic_form'), NotFoundHttpException::class);
throw_unless(AutomagicForms::availableFor($form), NotFoundHttpException::class);

if (! $request->session()->get("form.{$form->handle()}.submitted")) {
return redirect(AutomagicForms::route('index', ['form' => $form->handle()]));
Expand Down
26 changes: 26 additions & 0 deletions src/Rules/NotWithUniqueInstances.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Statamic\FormsPro\Rules;

use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;

class NotWithUniqueInstances implements DataAwareRule, ValidationRule
{
private array $data = [];

public function setData(array $data): static
{
$this->data = $data;

return $this;
}

public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($value && ($this->data['unique_instances'] ?? false)) {
$fail('forms-pro::messages.automagic_form_unique_instances')->translate();
}
}
}
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private function bootAutomagicForms(): void
'type' => 'toggle',
'display' => __('forms-pro::messages.enable_automagic_form'),
'instructions' => __('forms-pro::messages.enable_automagic_form_instructions'),
'validate' => ['new \\Statamic\\FormsPro\\Rules\\NotWithUniqueInstances'],
],
'automagic_form_url' => [
'type' => 'automagic_form_url',
Expand Down
60 changes: 60 additions & 0 deletions tests/AutomagicForms/AutomagicFormConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tests\AutomagicForms;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades;
use Statamic\Facades\User;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
use Tests\TestCase;

class AutomagicFormConfigTest extends TestCase
{
use PreventsSavingStacheItemsToDisk;

protected function setUp(): void
{
parent::setUp();

Facades\Form::all()->each->delete();
Facades\Form::make('contact')->title('Contact')->save();

$this->actingAs(User::make()->makeSuper());
}

#[Test]
public function it_fails_validation_when_automagic_form_and_unique_instances_are_both_enabled()
{
$this
->patchJson(cp_route('forms.update', 'contact'), $this->payload(automagic: true, uniqueInstances: true))
->assertStatus(422)
->assertJsonValidationErrors(['automagic_form' => "Automagic Forms can't be used with Unique Instances."]);
}

#[Test]
public function it_passes_validation_when_only_automagic_form_is_enabled()
{
$this
->patchJson(cp_route('forms.update', 'contact'), $this->payload(automagic: true, uniqueInstances: false))
->assertOk();
}

#[Test]
public function it_passes_validation_when_only_unique_instances_is_enabled()
{
$this
->patchJson(cp_route('forms.update', 'contact'), $this->payload(automagic: false, uniqueInstances: true))
->assertOk();
}

private function payload(bool $automagic, bool $uniqueInstances): array
{
return [
'title' => 'Contact',
'honeypot' => null,
'store' => true,
'automagic_form' => $automagic,
'unique_instances' => $uniqueInstances,
];
}
}
14 changes: 14 additions & 0 deletions tests/AutomagicForms/AutomagicFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ public function it_404s_when_automagic_forms_is_disabled()
->assertNotFound();
}

#[Test]
public function it_404s_every_action_when_the_form_has_unique_instances()
{
$this->createForm()->set('unique_instances', true)->save();

$this->get(route('statamic.forms-pro.form.default.index', ['form' => 'contact']))->assertNotFound();
$this->postJson(route('statamic.forms-pro.form.default.store', ['form' => 'contact', 'page' => 'main']), [
'name' => 'Han Solo',
'email' => 'han@example.com',
])->assertNotFound();
$this->get(route('statamic.forms-pro.form.default.restricted', ['form' => 'contact']))->assertNotFound();
$this->get(route('statamic.forms-pro.form.default.success', ['form' => 'contact']))->assertNotFound();
}

#[Test]
public function it_404s_when_the_form_does_not_exist()
{
Expand Down
10 changes: 9 additions & 1 deletion tests/AutomagicForms/FormLinkTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ protected function setUp(): void
#[Test]
public function it_resolves_a_form_to_its_automagic_form_url()
{
Facades\Form::make('contact')->title('Contact')->save();
Facades\Form::make('contact')->title('Contact')->set('automagic_form', true)->save();

$this->assertEquals(
route('statamic.forms-pro.form.default.index', ['form' => 'contact'], absolute: false),
(new FormLinkType)->resolve('contact')
);
}

#[Test]
public function it_resolves_nothing_when_the_form_has_unique_instances()
{
Facades\Form::make('contact')->title('Contact')->set('automagic_form', true)->set('unique_instances', true)->save();

$this->assertNull((new FormLinkType)->resolve('contact'));
}

#[Test]
public function it_resolves_nothing_when_the_form_does_not_exist()
{
Expand Down
Loading