-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from mitydigital/feature/20240827-events
Submission events
- Loading branch information
Showing
8 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"preset": "laravel" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters