Added email address validation to model

Added unit tests for email address validation

Corrected order of params for assertNotContains

EmailRecipient email address validation
This commit is contained in:
Kirk Mayo 2017-01-09 11:06:17 +13:00
parent ef13e85777
commit ac4c1678a2
4 changed files with 63 additions and 1 deletions

View File

@ -442,4 +442,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 ($address && !Email::is_valid_address($trimAddress)) {
$error = _t("UserDefinedForm_EmailRecipient.$translation",
"Invalid email address $trimAddress");
$result->error($error . " ($trimAddress)");
}
}
}
}
return $result;
}
}

View File

@ -230,6 +230,9 @@ en:
EMAILDETAILSTAB: 'Email Details'
PLURALNAME: 'User Defined Form Email Recipients'
SINGULARNAME: 'User Defined Form Email Recipient'
EMAILADDRESSINVALID: 'Email address has a invalid email address'
EMAILFROMINVALID: 'Email from has a invalid email address'
EMAILREPLYTOINVALID: 'Email reply has a invalid email address'
UserDefinedForm_EmailRecipientCondition:
PLURALNAME: 'User Defined Form Email Recipient Conditions'
SINGULARNAME: 'User Defined Form Email Recipient Condition'

View File

@ -435,4 +435,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

@ -227,6 +227,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>'