Skip to content

Commit

Permalink
Merge pull request #536 from solnet-aquarium/emailValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Jul 20, 2017
2 parents 61a7032 + 2c4fe28 commit b068bf6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
29 changes: 29 additions & 0 deletions code/model/recipients/UserDefinedForm_EmailRecipient.php
Expand Up @@ -446,4 +446,33 @@ public function getEmailTemplateDropdownValues()

return $templates;
}

/**
* Validate that valid email addresses are being used
*
* @return ValidationResult
*/
public function validate() {
$result = parent::validate();
$checkEmail = array(
'EmailAddress' => 'EMAILADDRESSINVALID',
'EmailFrom' => 'EMAILFROMINVALID',
'EmailReplyTo' => 'EMAILREPLYTOINVALID',
);
foreach ($checkEmail as $check => $translation) {
if ($this->$check) {
//may be a comma separated list of emails
$addresses = explode(',', $this->$check);
foreach ($addresses as $address) {
$trimAddress = trim($address);
if ($trimAddress && !Email::is_valid_address($trimAddress)) {
$error = _t("UserDefinedForm_EmailRecipient.$translation",
"Invalid email address $trimAddress");
$result->error($error . " ($trimAddress)");
}
}
}
}
return $result;
}
}
3 changes: 3 additions & 0 deletions lang/en.yml
Expand Up @@ -236,6 +236,9 @@ en:
EMAILDETAILSTAB: 'Email Details'
PLURALNAME: 'User Defined Form Email Recipients'
SINGULARNAME: 'User Defined Form Email Recipient'
EMAILADDRESSINVALID: '"EmailAddress" is not valid'
EMAILFROMINVALID: '"Email From" is not valid'
EMAILREPLYTOINVALID: '"Email Reply To" is not valid'
UserDefinedForm_EmailRecipientCondition:
PLURALNAME: 'User Defined Form Email Recipient Conditions'
SINGULARNAME: 'User Defined Form Email Recipient Condition'
Expand Down
20 changes: 20 additions & 0 deletions tests/UserDefinedFormTest.php
Expand Up @@ -455,4 +455,24 @@ public function testIndex()
$this->assertNotContains('<p></p>', $body);
$this->assertNotContains('</p><p>Thank you for filling it out</p>', $body);
}

public function testEmailAddressValidation()
{
$this->logInWithPermission('ADMIN');

// test invalid email addresses fail validation
$recipient = $this->objFromFixture('UserDefinedForm_EmailRecipient',
'invalid-recipient-list');
$result = $recipient->validate();
$this->assertFalse($result->valid());
$this->assertContains('filtered.example.com', $result->message());
$this->assertNotContains('filtered2@example.com', $result->message());

// test valid email addresses pass validation
$recipient = $this->objFromFixture('UserDefinedForm_EmailRecipient',
'valid-recipient-list');
$result = $recipient->validate();
$this->assertTrue($result->valid());
$this->assertEmpty($result->message());
}
}
12 changes: 11 additions & 1 deletion tests/UserDefinedFormTest.yml
Expand Up @@ -237,6 +237,16 @@ UserDefinedForm_EmailRecipient:
CustomRules: =>UserDefinedForm_EmailRecipientCondition.group-equals-rule, =>UserDefinedForm_EmailRecipientCondition.group-not-equals-rule
CustomRulesCondition: 'Or'

valid-recipient-list:
EmailAddress: filtered@example.com, filtered2@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com

invalid-recipient-list:
EmailAddress: filtered.example.com, filtered2@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com

UserDefinedForm:
basic-form-page:
Content: '<p>Here is my form</p><p>$UserDefinedForm</p><p>Thank you for filling it out</p>'
Expand Down Expand Up @@ -280,4 +290,4 @@ UserDefinedForm:

email-form:
Title: 'Page with email field'
Fields: =>EditableEmailField.another-email-field, =>EditableTextField.another-required
Fields: =>EditableEmailField.another-email-field, =>EditableTextField.another-required

0 comments on commit b068bf6

Please sign in to comment.