From c5d196bd379323a422608dc51617be35c2325983 Mon Sep 17 00:00:00 2001 From: Marty Friedel <1491079+martyf@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:22:17 +0930 Subject: [PATCH] Submission events --- pint.json | 3 ++ resources/lang/en/listeners.php | 2 + .../views/listeners/submission.blade.php | 5 ++ src/Listeners/Submission.php | 38 ++++++++++++++ .../StatamicLoggerEventSubscriber.php | 12 +++++ tests/Listeners/SubmissionTest.php | 49 +++++++++++++++++++ .../StatamicLoggerEventSubscriberTest.php | 11 +++++ tests/TestCase.php | 4 ++ 8 files changed, 124 insertions(+) create mode 100644 pint.json create mode 100644 resources/views/listeners/submission.blade.php create mode 100644 src/Listeners/Submission.php create mode 100644 tests/Listeners/SubmissionTest.php diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..70b0e18 --- /dev/null +++ b/pint.json @@ -0,0 +1,3 @@ +{ + "preset": "laravel" +} \ No newline at end of file diff --git a/resources/lang/en/listeners.php b/resources/lang/en/listeners.php index 4963275..20b98b6 100644 --- a/resources/lang/en/listeners.php +++ b/resources/lang/en/listeners.php @@ -17,5 +17,7 @@ 'nav_tree' => ":action ':id' Nav order in the ':site' Site", + 'submission' => ":action submission for form ':form'", + 'term' => ":action ':name' in the ':taxonomy_name' Taxonomy", ]; diff --git a/resources/views/listeners/submission.blade.php b/resources/views/listeners/submission.blade.php new file mode 100644 index 0000000..8c961ca --- /dev/null +++ b/resources/views/listeners/submission.blade.php @@ -0,0 +1,5 @@ +
{{ __('statamic-logger::listeners.submission', [ + 'action' => $handler->action(), + 'form' => $data->form +]) }}
+
{{ __('statamic-logger::listeners.id') }}: {{ $data->id }}
\ No newline at end of file diff --git a/src/Listeners/Submission.php b/src/Listeners/Submission.php new file mode 100644 index 0000000..29d9696 --- /dev/null +++ b/src/Listeners/Submission.php @@ -0,0 +1,38 @@ + $event->submission->id(), + 'form' => $event->submission->form->title(), + 'submission' => $event->submission->data()->toArray(), + ]; + } + + protected function verb($event): string + { + return match ($event) { + SubmissionCreated::class => __('statamic-logger::verbs.created'), + SubmissionCreating::class => __('statamic-logger::verbs.creating'), + SubmissionDeleted::class => __('statamic-logger::verbs.deleted'), + SubmissionSaved::class => __('statamic-logger::verbs.saved'), + SubmissionSaving::class => __('statamic-logger::verbs.saving') + }; + } +} diff --git a/src/Subscribers/StatamicLoggerEventSubscriber.php b/src/Subscribers/StatamicLoggerEventSubscriber.php index a12e1b8..6bd1ba7 100644 --- a/src/Subscribers/StatamicLoggerEventSubscriber.php +++ b/src/Subscribers/StatamicLoggerEventSubscriber.php @@ -22,6 +22,7 @@ use MityDigital\StatamicLogger\Listeners\NavTree; use MityDigital\StatamicLogger\Listeners\Role; use MityDigital\StatamicLogger\Listeners\Site; +use MityDigital\StatamicLogger\Listeners\Submission; use MityDigital\StatamicLogger\Listeners\Taxonomy; use MityDigital\StatamicLogger\Listeners\Term; use MityDigital\StatamicLogger\Listeners\User; @@ -76,6 +77,11 @@ use Statamic\Events\StacheCleared; use Statamic\Events\StacheWarmed; use Statamic\Events\StaticCacheCleared; +use Statamic\Events\SubmissionCreated; +use Statamic\Events\SubmissionCreating; +use Statamic\Events\SubmissionDeleted; +use Statamic\Events\SubmissionSaved; +use Statamic\Events\SubmissionSaving; use Statamic\Events\TaxonomyCreated; use Statamic\Events\TaxonomyDeleted; use Statamic\Events\TaxonomySaved; @@ -168,6 +174,12 @@ public function subscribe(Dispatcher $events): array SiteDeleted::class => Site::class, SiteSaved::class => Site::class, + SubmissionCreated::class => Submission::class, + SubmissionCreating::class => Submission::class, + SubmissionDeleted::class => Submission::class, + SubmissionSaved::class => Submission::class, + SubmissionSaving::class => Submission::class, + StacheCleared::class => Utility::class, StacheWarmed::class => Utility::class, diff --git a/tests/Listeners/SubmissionTest.php b/tests/Listeners/SubmissionTest.php new file mode 100644 index 0000000..e4cdb6d --- /dev/null +++ b/tests/Listeners/SubmissionTest.php @@ -0,0 +1,49 @@ +title('Contact'); + $form->save(); + + $submission = $form->makeSubmission(); + $submission->data([ + 'name_first' => 'First Name', + ]); + $submission->save(); + + // create the event + $event = new SubmissionSaved($submission); + + // create the listener + $listener = new Submission; + $data = getEventHandlerData($listener, $event); + + expect($data) + ->toHaveCount(3) + // id + ->toHaveKey('id') + ->and($data['id'])->toBe($submission->id()) + // form + ->and($data)->toHaveKey('form') + ->and($data['form'])->toBe($form->title()) + // submission + ->and($data)->toHaveKey('submission') + ->and($data['submission'])->toBeArray()->toBe([ + 'name_first' => 'First Name', + ]); +}); + +it('returns the correct view', function () { + $listener = new Submission; + + expect($listener->view())->toBe('statamic-logger::listeners.submission'); +}); diff --git a/tests/Subscribers/StatamicLoggerEventSubscriberTest.php b/tests/Subscribers/StatamicLoggerEventSubscriberTest.php index 6284c9f..0227811 100644 --- a/tests/Subscribers/StatamicLoggerEventSubscriberTest.php +++ b/tests/Subscribers/StatamicLoggerEventSubscriberTest.php @@ -59,6 +59,11 @@ use Statamic\Events\StacheCleared; use Statamic\Events\StacheWarmed; use Statamic\Events\StaticCacheCleared; +use Statamic\Events\SubmissionCreated; +use Statamic\Events\SubmissionCreating; +use Statamic\Events\SubmissionDeleted; +use Statamic\Events\SubmissionSaved; +use Statamic\Events\SubmissionSaving; use Statamic\Events\TaxonomyCreated; use Statamic\Events\TaxonomyDeleted; use Statamic\Events\TaxonomySaved; @@ -147,6 +152,12 @@ StaticCacheCleared::class, + SubmissionCreated::class, + SubmissionCreating::class, + SubmissionDeleted::class, + SubmissionSaved::class, + SubmissionSaving::class, + TaxonomyCreated::class, TaxonomyDeleted::class, TaxonomySaved::class, diff --git a/tests/TestCase.php b/tests/TestCase.php index 5b6269a..ce253fd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -52,6 +52,10 @@ protected function getEnvironmentSetUp($app) // users $app['config']->set('statamic.stache.stores.users.directory', $this->getTempDirectory('/content/users')); + + // forms submissions + $app['config']->set('statamic.stache.stores.form-submissions.directory', + $this->getTempDirectory('/content/submissions')); } public function getTempDirectory($suffix = ''): string