Merge pull request #536 from solnet-aquarium/emailValidation

This commit is contained in:
Daniel Hensby 2017-07-20 10:40:34 +01:00
commit b068bf65bf
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
4 changed files with 63 additions and 1 deletions

View File

@ -446,4 +446,33 @@ class UserDefinedForm_EmailRecipient extends DataObject
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;
}
}

View File

@ -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'

View File

@ -455,4 +455,24 @@ class UserDefinedFormTest extends FunctionalTest
$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());
}
}

View File

@ -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>'