From 59cd87d842ce7006857f80e296a9e8938ba8ae47 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 14 Jul 2020 16:41:36 +1200 Subject: [PATCH] FIX Trim recipient email addresses before write --- code/Model/Recipient/EmailRecipient.php | 11 +++++++++++ tests/Model/Recipient/EmailRecipientTest.php | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/code/Model/Recipient/EmailRecipient.php b/code/Model/Recipient/EmailRecipient.php index ff3487d..0fa482d 100644 --- a/code/Model/Recipient/EmailRecipient.php +++ b/code/Model/Recipient/EmailRecipient.php @@ -116,6 +116,17 @@ class EmailRecipient extends DataObject DB::query("UPDATE \"UserDefinedForm_EmailRecipient\" SET \"FormClass\" = 'Page' WHERE \"FormClass\" IS NULL"); } + public function onBeforeWrite() + { + parent::onBeforeWrite(); + + // email addresses have trim() applied to them during validation for a slightly nicer UX + // apply trim() here too before saving to the database + $this->EmailAddress = trim($this->EmailAddress); + $this->EmailFrom = trim($this->EmailFrom); + $this->EmailReplyTo = trim($this->EmailReplyTo); + } + public function summaryFields() { $fields = parent::summaryFields(); diff --git a/tests/Model/Recipient/EmailRecipientTest.php b/tests/Model/Recipient/EmailRecipientTest.php index 30cf0cc..652a612 100644 --- a/tests/Model/Recipient/EmailRecipientTest.php +++ b/tests/Model/Recipient/EmailRecipientTest.php @@ -38,4 +38,16 @@ class EmailRecipientTest extends SapphireTest $recipient->EmailFrom = 'test@example.com'; $recipient->write(); } + + public function testEmailAddressesTrimmed() + { + $recipient = new EmailRecipient(); + $recipient->EmailAddress = 'test1@example.com '; + $recipient->EmailFrom = 'test2@example.com '; + $recipient->EmailReplyTo = 'test3@example.com '; + $recipient->write(); + $this->assertSame('test1@example.com', $recipient->EmailAddress); + $this->assertSame('test2@example.com', $recipient->EmailFrom); + $this->assertSame('test3@example.com', $recipient->EmailReplyTo); + } }