From e977f16f7eccf6d03a94c435e267b80708fadbb5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 14 Oct 2026 13:36:04 -0400 Subject: [PATCH] Prevent Automagic Forms from being used with Unique Instances Config validation fails when both are enabled. If both end up enabled anyway, the Automagic form 404s and link fields resolve to nothing. Co-Authored-By: Claude Sonnet 5.5 --- lang/en/messages.php | 1 + src/AutomagicForms/AutomagicForms.php | 6 ++ src/AutomagicForms/FormLinkType.php | 2 +- .../Controllers/AutomagicFormController.php | 8 +-- src/Rules/NotWithUniqueInstances.php | 26 ++++++++ src/ServiceProvider.php | 1 + .../AutomagicFormConfigTest.php | 60 +++++++++++++++++++ tests/AutomagicForms/AutomagicFormTest.php | 14 +++++ tests/AutomagicForms/FormLinkTypeTest.php | 10 +++- 9 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 src/Rules/NotWithUniqueInstances.php create mode 100644 tests/AutomagicForms/AutomagicFormConfigTest.php diff --git a/lang/en/messages.php b/lang/en/messages.php index c40fac2..cdd61b8 100644 --- a/lang/en/messages.php +++ b/lang/en/messages.php @@ -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', diff --git a/src/AutomagicForms/AutomagicForms.php b/src/AutomagicForms/AutomagicForms.php index 5e5d2d5..82b4cb3 100644 --- a/src/AutomagicForms/AutomagicForms.php +++ b/src/AutomagicForms/AutomagicForms.php @@ -2,6 +2,7 @@ namespace Statamic\FormsPro\AutomagicForms; +use Statamic\Contracts\Forms\Form; use Statamic\Facades\Site; class AutomagicForms @@ -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(); diff --git a/src/AutomagicForms/FormLinkType.php b/src/AutomagicForms/FormLinkType.php index 4608e60..33e2ceb 100644 --- a/src/AutomagicForms/FormLinkType.php +++ b/src/AutomagicForms/FormLinkType.php @@ -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; } diff --git a/src/Http/Controllers/AutomagicFormController.php b/src/Http/Controllers/AutomagicFormController.php index ff36dfe..d06f417 100644 --- a/src/Http/Controllers/AutomagicFormController.php +++ b/src/Http/Controllers/AutomagicFormController.php @@ -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()])); @@ -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([ @@ -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()])); @@ -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()])); diff --git a/src/Rules/NotWithUniqueInstances.php b/src/Rules/NotWithUniqueInstances.php new file mode 100644 index 0000000..cde9726 --- /dev/null +++ b/src/Rules/NotWithUniqueInstances.php @@ -0,0 +1,26 @@ +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(); + } + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index d35a0f5..892f609 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -64,6 +64,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', diff --git a/tests/AutomagicForms/AutomagicFormConfigTest.php b/tests/AutomagicForms/AutomagicFormConfigTest.php new file mode 100644 index 0000000..6b7079a --- /dev/null +++ b/tests/AutomagicForms/AutomagicFormConfigTest.php @@ -0,0 +1,60 @@ +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, + ]; + } +} diff --git a/tests/AutomagicForms/AutomagicFormTest.php b/tests/AutomagicForms/AutomagicFormTest.php index 3216fe67..ef7a55e 100644 --- a/tests/AutomagicForms/AutomagicFormTest.php +++ b/tests/AutomagicForms/AutomagicFormTest.php @@ -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() { diff --git a/tests/AutomagicForms/FormLinkTypeTest.php b/tests/AutomagicForms/FormLinkTypeTest.php index 8de91b9..a2403a5 100644 --- a/tests/AutomagicForms/FormLinkTypeTest.php +++ b/tests/AutomagicForms/FormLinkTypeTest.php @@ -22,7 +22,7 @@ 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), @@ -30,6 +30,14 @@ public function it_resolves_a_form_to_its_automagic_form_url() ); } + #[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() {