diff --git a/lib/Models/Model.php b/lib/Models/Model.php index c5fcfdc7a8..78ee023385 100644 --- a/lib/Models/Model.php +++ b/lib/Models/Model.php @@ -9,7 +9,8 @@ class Model extends \Sudzy\ValidModel { function __construct() { $this->_errors = array(); - parent::__construct(); + $validator = new ModelValidator(); + parent::__construct($validator); } static function create() { diff --git a/lib/Models/ModelValidator.php b/lib/Models/ModelValidator.php new file mode 100644 index 0000000000..26ff960999 --- /dev/null +++ b/lib/Models/ModelValidator.php @@ -0,0 +1,30 @@ +validators = array( + 'validEmail' => 'validateEmail' + ); + $this->setupValidators(); + } + + private function setupValidators() { + $_this = $this; + foreach($this->validators as $validator => $action) { + $this->addValidator($validator, function($params) use ($action, $_this) { + return call_user_func(array($this, $action), $params); + }); + } + } + + function validateEmail($email) { + return is_email($email) !== false; + } +} \ No newline at end of file diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index ef2d3b3f04..83dbef7e55 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -22,7 +22,7 @@ class Subscriber extends Model { $this->addValidations('email', array( 'required' => __('Please enter your email address', 'mailpoet'), - 'isEmail' => __('Your email address is invalid!', 'mailpoet') + 'validEmail' => __('Your email address is invalid!', 'mailpoet') )); } diff --git a/tests/unit/Models/ModelValidatorTest.php b/tests/unit/Models/ModelValidatorTest.php new file mode 100644 index 0000000000..466dd4458f --- /dev/null +++ b/tests/unit/Models/ModelValidatorTest.php @@ -0,0 +1,24 @@ +validator = new ModelValidator(); + } + + function testItConfiguresValidators() { + $configured_validators = $this->validator->getValidators(); + foreach(array_keys($this->validator->validators) as $validator) { + expect($configured_validators)->contains($validator); + } + } + + function testItValidatesEmail() { + expect($this->validator->validateEmail('test'))->false(); + expect($this->validator->validateEmail('tést@éxample.com'))->false(); + expect($this->validator->validateEmail('test@example.com'))->true(); + } +} diff --git a/tests/unit/Models/SubscriberTest.php b/tests/unit/Models/SubscriberTest.php index af22e364d3..4ea2c80ddf 100644 --- a/tests/unit/Models/SubscriberTest.php +++ b/tests/unit/Models/SubscriberTest.php @@ -55,15 +55,6 @@ class SubscriberTest extends MailPoetTest { } function testItShouldSetErrors() { - // model validation - $subscriber = Subscriber::create(); - $subscriber->hydrate(array( - 'email' => 'invalid_email' - )); - $subscriber->save(); - $errors = $subscriber->getErrors(); - expect($errors)->contains("Your email address is invalid!"); - // pdo error $subscriber = Subscriber::create(); $subscriber->hydrate(array( @@ -75,6 +66,27 @@ class SubscriberTest extends MailPoetTest { expect($errors[0])->contains("Unknown column 'invalid_column' in 'field list'"); } + function testItValidatesEmailAndSetsErrors() { + // email is required + $subscriber = Subscriber::create(); + $subscriber->save(); + $errors = $subscriber->getErrors(); + expect($errors)->contains("Please enter your email address"); + + // email address should be valid + $subscriber = Subscriber::create(); + $subscriber->email = 'invalid_email'; + $subscriber->save(); + $errors = $subscriber->getErrors(); + expect($errors)->contains("Your email address is invalid!"); + + $subscriber = Subscriber::create(); + $subscriber->email = 'tést@éxample.com'; + $subscriber->save(); + $errors = $subscriber->getErrors(); + expect($errors)->contains("Your email address is invalid!"); + } + function emailMustBeUnique() { $conflict_subscriber = Subscriber::create(); $conflict_subscriber->hydrate($this->data);