From 168d9ec92349988e928d8ed3c5e55e2b5a9540cf Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 19 Jul 2022 10:41:05 -0400 Subject: [PATCH 1/3] Ensure the toggle fieldtype only cases to a saveable boolean when the value is submitted. --- src/Fieldtypes/Toggle.php | 4 ++++ tests/Fieldtypes/ToggleTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Fieldtypes/Toggle.php b/src/Fieldtypes/Toggle.php index 62d15456e1d..adf8b5f6cdd 100644 --- a/src/Fieldtypes/Toggle.php +++ b/src/Fieldtypes/Toggle.php @@ -36,6 +36,10 @@ public function preProcess($data) public function process($data) { + if (is_null($data)) { + return null; + } + return (bool) $data; } diff --git a/tests/Fieldtypes/ToggleTest.php b/tests/Fieldtypes/ToggleTest.php index 58fce926141..c7b235b8164 100644 --- a/tests/Fieldtypes/ToggleTest.php +++ b/tests/Fieldtypes/ToggleTest.php @@ -17,4 +17,14 @@ public function it_augments_to_a_boolean() $this->assertFalse($field->augment(null)); $this->assertTrue($field->augment(true)); } + + /** @test */ + public function it_processes_to_a_boolean_only_when_value_is_actually_set_or_submitted() + { + $field = (new Toggle)->setField(new Field('test', ['type' => 'toggle'])); + + $this->assertTrue($field->process(true)); + $this->assertFalse($field->process(false)); + $this->assertNull($field->process(null)); + } } From 9225d2ab4634dfb3dd3d53d0adf60bf81b1688ee Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 19 Jul 2022 11:02:23 -0400 Subject: [PATCH 2/3] These toggle values will not be in the `$email` payload unless explicitly set. --- src/Forms/Form.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index e1ad6ad1371..841ebf77a5c 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -182,8 +182,8 @@ public function save() 'title' => $this->title, 'honeypot' => $this->honeypot, 'email' => collect($this->email)->map(function ($email) { - $email['markdown'] = $email['markdown'] ?: null; - $email['attachments'] = $email['attachments'] ?: null; + $email['markdown'] = isset($email['markdown']) ? $email['markdown'] : null; + $email['attachments'] = isset($email['attachments']) ? $email['attachments'] : null; return Arr::removeNullValues($email); })->all(), From d49c0ee3a921240c2a2fb89c4e35740dbc0015ce Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 21 Jul 2022 13:14:54 -0400 Subject: [PATCH 3/3] Adjust incorrect test rather than implementation. This reverts commit 9225d2ab4634dfb3dd3d53d0adf60bf81b1688ee. All the values _would_ be submitted in reality, but the test wasn't submitting what would actually be happening. --- src/Forms/Form.php | 4 +-- tests/Feature/Forms/UpdateFormTest.php | 39 +++++++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 841ebf77a5c..e1ad6ad1371 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -182,8 +182,8 @@ public function save() 'title' => $this->title, 'honeypot' => $this->honeypot, 'email' => collect($this->email)->map(function ($email) { - $email['markdown'] = isset($email['markdown']) ? $email['markdown'] : null; - $email['attachments'] = isset($email['attachments']) ? $email['attachments'] : null; + $email['markdown'] = $email['markdown'] ?: null; + $email['attachments'] = $email['attachments'] ?: null; return Arr::removeNullValues($email); })->all(), diff --git a/tests/Feature/Forms/UpdateFormTest.php b/tests/Feature/Forms/UpdateFormTest.php index a4d4d8feca0..aca09e81254 100644 --- a/tests/Feature/Forms/UpdateFormTest.php +++ b/tests/Feature/Forms/UpdateFormTest.php @@ -64,7 +64,34 @@ public function it_updates_emails() $form = tap(Form::make('test'))->save(); $this->assertNull($form->email()); - $emailConfig = [ + $this + ->actingAs($this->userWithPermission()) + ->update($form, ['email' => [ + [ + 'to' => 'john@example.com', + 'from' => 'jane@example.com', + 'reply_to' => null, + 'subject' => null, + 'text' => null, + 'html' => null, + 'markdown' => false, + 'attachments' => false, + ], + [ + 'to' => 'foo@example.com', + 'from' => 'bar@example.com', + 'reply_to' => null, + 'subject' => null, + 'text' => 'emails.contact.text', + 'html' => 'emails.contact.html', + 'markdown' => false, + 'attachments' => false, + ], + ]]) + ->assertOk(); + + $updated = Form::all()->first(); + $this->assertEquals([ [ 'to' => 'john@example.com', 'from' => 'jane@example.com', @@ -75,15 +102,7 @@ public function it_updates_emails() 'text' => 'emails.contact.text', 'html' => 'emails.contact.html', ], - ]; - - $this - ->actingAs($this->userWithPermission()) - ->update($form, ['email' => $emailConfig]) - ->assertOk(); - - $updated = Form::all()->first(); - $this->assertEquals($emailConfig, $updated->email()); + ], $updated->email()); } private function userWithoutPermission()