Uses WP's is_email() to validate email addresses in Subscriber model
This commit is contained in:
@ -9,7 +9,8 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
$this->_errors = array();
|
$this->_errors = array();
|
||||||
parent::__construct();
|
$validator = new ModelValidator();
|
||||||
|
parent::__construct($validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function create() {
|
static function create() {
|
||||||
|
30
lib/Models/ModelValidator.php
Normal file
30
lib/Models/ModelValidator.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Models;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class ModelValidator extends \Sudzy\Engine {
|
||||||
|
public $validators;
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
@ -22,7 +22,7 @@ class Subscriber extends Model {
|
|||||||
|
|
||||||
$this->addValidations('email', array(
|
$this->addValidations('email', array(
|
||||||
'required' => __('Please enter your email address', 'mailpoet'),
|
'required' => __('Please enter your email address', 'mailpoet'),
|
||||||
'isEmail' => __('Your email address is invalid!', 'mailpoet')
|
'validEmail' => __('Your email address is invalid!', 'mailpoet')
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
tests/unit/Models/ModelValidatorTest.php
Normal file
24
tests/unit/Models/ModelValidatorTest.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MailPoet\Models\ModelValidator;
|
||||||
|
|
||||||
|
class ModelValidatorTest extends MailPoetTest {
|
||||||
|
public $validator;
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
@ -55,15 +55,6 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItShouldSetErrors() {
|
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
|
// pdo error
|
||||||
$subscriber = Subscriber::create();
|
$subscriber = Subscriber::create();
|
||||||
$subscriber->hydrate(array(
|
$subscriber->hydrate(array(
|
||||||
@ -75,6 +66,27 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
expect($errors[0])->contains("Unknown column 'invalid_column' in 'field list'");
|
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() {
|
function emailMustBeUnique() {
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->hydrate($this->data);
|
$conflict_subscriber->hydrate($this->data);
|
||||||
|
Reference in New Issue
Block a user