Skip to content

Commit 40e99ad

Browse files
authored
Bring FormSubmission save/delete inline with core (#419)
* Bring FormSubmission save/delete inline with core * tests
1 parent c98eb44 commit 40e99ad

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

src/Forms/Submission.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,29 @@ public function save()
7676
return false;
7777
}
7878

79+
$withEvents = $this->withEvents;
80+
$this->withEvents = true;
81+
82+
$afterSaveCallbacks = $this->afterSaveCallbacks;
83+
$this->afterSaveCallbacks = [];
84+
7985
$model = $this->toModel();
8086
$model->save();
8187
$isNew = $model->wasRecentlyCreated;
8288

8389
$this->model($model->fresh());
8490

85-
if ($isNew) {
86-
SubmissionCreated::dispatch($this);
91+
foreach ($afterSaveCallbacks as $callback) {
92+
$callback($this);
93+
}
94+
95+
if ($withEvents) {
96+
if ($isNew) {
97+
SubmissionCreated::dispatch($this);
98+
}
99+
100+
SubmissionSaved::dispatch($this);
87101
}
88-
SubmissionSaved::dispatch($this);
89102
}
90103

91104
public function delete()
@@ -95,8 +108,15 @@ public function delete()
95108
$this->model = $class::findOrNew($this->id);
96109
}
97110

111+
$withEvents = $this->withEvents;
112+
$this->withEvents = true;
113+
98114
$this->model->delete();
99115

100-
SubmissionDeleted::dispatch($this);
116+
if ($withEvents) {
117+
SubmissionDeleted::dispatch($this);
118+
}
119+
120+
return true;
101121
}
102122
}

tests/Forms/FormSubmissionTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
namespace Tests\Forms;
44

55
use Carbon\Carbon;
6+
use Illuminate\Support\Facades\Event;
67
use PHPUnit\Framework\Attributes\Test;
78
use Statamic\Eloquent\Forms\FormModel;
89
use Statamic\Eloquent\Forms\SubmissionModel;
10+
use Statamic\Events\SubmissionCreated;
11+
use Statamic\Events\SubmissionDeleted;
12+
use Statamic\Events\SubmissionSaved;
913
use Statamic\Facades;
1014
use Tests\TestCase;
1115

@@ -115,4 +119,54 @@ public function null_values_are_removed_from_data()
115119

116120
$this->assertArrayNotHasKey('null_value', $submission->model()->data);
117121
}
122+
123+
#[Test]
124+
public function it_should_save_quietly()
125+
{
126+
$form = tap(Facades\Form::make('test')->title('Test'))
127+
->save();
128+
129+
Event::fake();
130+
131+
tap($form->makeSubmission([
132+
'name' => 'John Doe',
133+
]))->saveQuietly();
134+
135+
Event::assertNotDispatched(SubmissionSaved::class);
136+
Event::assertNotDispatched(SubmissionCreated::class);
137+
138+
tap($form->makeSubmission([
139+
'name' => 'John Doe',
140+
]))->save();
141+
142+
Event::assertDispatched(SubmissionSaved::class);
143+
Event::assertDispatched(SubmissionCreated::class);
144+
}
145+
146+
#[Test]
147+
public function it_should_delete_quietly()
148+
{
149+
$form = tap(Facades\Form::make('test')->title('Test'))
150+
->save();
151+
152+
Event::fake();
153+
154+
$submission = tap($form->makeSubmission([
155+
'name' => 'John Doe',
156+
]))->save();
157+
158+
$result = $submission->deleteQuietly();
159+
160+
Event::assertNotDispatched(SubmissionDeleted::class);
161+
$this->assertSame($result, true);
162+
163+
$submission = tap($form->makeSubmission([
164+
'name' => 'John Doe',
165+
]))->save();
166+
167+
$submission->delete();
168+
169+
Event::assertDispatched(SubmissionDeleted::class);
170+
$this->assertSame($result, true);
171+
}
118172
}

0 commit comments

Comments
 (0)