From 140f463b9627253b2efe08bd63140cc536c4f4cb Mon Sep 17 00:00:00 2001 From: Mark Stephens Date: Fri, 12 Mar 2010 00:30:48 +0000 Subject: [PATCH] MINOR: Correction on email address validator, and a unit test for EmailField php validation git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@100950 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/EmailField.php | 4 +-- tests/forms/EmailFieldTest.php | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/forms/EmailFieldTest.php diff --git a/forms/EmailField.php b/forms/EmailField.php index 059b90295..97530dbc9 100755 --- a/forms/EmailField.php +++ b/forms/EmailField.php @@ -16,7 +16,7 @@ Behaviour.register({ var el = _CURRENT_FORM.elements[fieldName]; if(!el || !el.value) return true; - if(el.value.match(/^([a-zA-Z0-9_+\-\.\x27]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)) { + if(el.value.match(/^([a-zA-Z0-9_+\.\x27-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)) { return true; } else { validationError(el, "$error","validation"); @@ -42,7 +42,7 @@ JS; function validate($validator){ $this->value = trim($this->value); - if($this->value && !ereg('^([a-zA-Z0-9_+\.\-\x27]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$', $this->value)){ + if($this->value && !ereg('^([a-zA-Z0-9_+\'.-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$', $this->value)){ $validator->validationError( $this->name, _t('EmailField.VALIDATION', "Please enter an email address."), diff --git a/tests/forms/EmailFieldTest.php b/tests/forms/EmailFieldTest.php new file mode 100644 index 000000000..7a3d3bce4 --- /dev/null +++ b/tests/forms/EmailFieldTest.php @@ -0,0 +1,51 @@ + are valid in the local part if the local part is in double quotes + * - "." is valid in the local part as long as its not first or last char + * @return void + */ + function testEmailAddressSyntax() { + $this->internalCheck("blah@blah.com", "Valid, simple", true); + $this->internalCheck("mr.o'connor+on-toast@blah.com", "Valid, special chars", true); + $this->internalCheck("", "Empty email", true); + $this->internalCheck("invalid", "Invalid, simple", false); + $this->internalCheck("invalid@name@domain.com", "Invalid, two @'s", false); + $this->internalCheck("invalid@domain", "Invalid, domain too simple", false); + $this->internalCheck("domain.but.no.user", "Invalid, no user part", false); + } + + function internalCheck($email, $checkText, $expectSuccess) { + $field = new EmailField("MyEmail"); + $field->setValue($email); + + $val = new EmailFieldTest_Validator(); + try { + $field->validate($val); + if (!$expectSuccess) $this->assertTrue(false, $checkText . " (/$email/ passed validation, but not expected to)"); + } catch (Exception $e) { + if ($e instanceof PHPUnit_Framework_AssertionFailedError) throw $e; // re-throw assertion failure + else if ($expectSuccess) $this->assertTrue(false, $checkText . ": " . $e->GetMessage() . " (/$email/ did not pass validation, but was expected to)"); + } + } +} + +class EmailFieldTest_Validator extends Validator { + function validationError($fieldName, $message, $messageType='') { + throw new Exception($message); + } + + function javascript() { + } + + function php($data) { + } +}