Skip to content

Commit

Permalink
Merge pull request #15 from mitydigital/feature/20240827-events
Browse files Browse the repository at this point in the history
Submission events
  • Loading branch information
martyf authored Aug 27, 2024
2 parents d353e19 + c5d196b commit dc06d75
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "laravel"
}
2 changes: 2 additions & 0 deletions resources/lang/en/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
];
5 changes: 5 additions & 0 deletions resources/views/listeners/submission.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>{{ __('statamic-logger::listeners.submission', [
'action' => $handler->action(),
'form' => $data->form
]) }}</div>
<div class="text-xs text-gray-500">{{ __('statamic-logger::listeners.id') }}: {{ $data->id }}</div>
38 changes: 38 additions & 0 deletions src/Listeners/Submission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace MityDigital\StatamicLogger\Listeners;

use MityDigital\StatamicLogger\Abstracts\EventListener;
use Statamic\Events\SubmissionCreated;
use Statamic\Events\SubmissionCreating;
use Statamic\Events\SubmissionDeleted;
use Statamic\Events\SubmissionSaved;
use Statamic\Events\SubmissionSaving;

class Submission extends EventListener
{
public function view(): string
{
return 'statamic-logger::listeners.submission';
}

protected function data($event): array
{
return [
'id' => $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')
};
}
}
12 changes: 12 additions & 0 deletions src/Subscribers/StatamicLoggerEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,

Expand Down
49 changes: 49 additions & 0 deletions tests/Listeners/SubmissionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Illuminate\Support\Facades\Event;
use MityDigital\StatamicLogger\Listeners\Submission;
use Statamic\Events\SubmissionSaved;
use Statamic\Facades\Form;

it('returns the correct submission data structure', function () {
// disable actual events
Event::fake();

// create supporting components
$form = (Form::make('contact'))
->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');
});
11 changes: 11 additions & 0 deletions tests/Subscribers/StatamicLoggerEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,6 +152,12 @@

StaticCacheCleared::class,

SubmissionCreated::class,
SubmissionCreating::class,
SubmissionDeleted::class,
SubmissionSaved::class,
SubmissionSaving::class,

TaxonomyCreated::class,
TaxonomyDeleted::class,
TaxonomySaved::class,
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dc06d75

Please sign in to comment.