- Made changes as per Marco's comments in #54
- Implemented database cleanup for tests. Closes #56
This commit is contained in:
34
RoboFile.php
34
RoboFile.php
@ -15,10 +15,15 @@ class RoboFile extends \Robo\Tasks {
|
|||||||
|
|
||||||
function watch() {
|
function watch() {
|
||||||
$files = array(// global admin styles
|
$files = array(// global admin styles
|
||||||
'assets/css/src/admin.styl', // rtl specific styles
|
'assets/css/src/admin.styl',
|
||||||
'assets/css/src/rtl.styl');
|
// rtl specific styles
|
||||||
|
'assets/css/src/rtl.styl'
|
||||||
|
);
|
||||||
|
|
||||||
$command = array('./node_modules/stylus/bin/stylus -u', ' nib -w' . join(' ', $files) . ' -o assets/css/');
|
$command = array(
|
||||||
|
'./node_modules/stylus/bin/stylus -u',
|
||||||
|
' nib -w' . join(' ', $files) . ' -o assets/css/'
|
||||||
|
);
|
||||||
$this->_exec(join(' ', $command));
|
$this->_exec(join(' ', $command));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,8 +36,8 @@ class RoboFile extends \Robo\Tasks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testUnit() {
|
function testUnit() {
|
||||||
$this->_exec('vendor/bin/codecept build');
|
|
||||||
$this->loadEnv();
|
$this->loadEnv();
|
||||||
|
$this->_exec('vendor/bin/codecept build');
|
||||||
$this->_exec('vendor/bin/codecept run unit');
|
$this->_exec('vendor/bin/codecept run unit');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,15 +45,17 @@ class RoboFile extends \Robo\Tasks {
|
|||||||
if (!$unit) {
|
if (!$unit) {
|
||||||
throw new Exception("Your need to specify what you want to test (e.g.: test:unit-single models/SubscriberCest)");
|
throw new Exception("Your need to specify what you want to test (e.g.: test:unit-single models/SubscriberCest)");
|
||||||
}
|
}
|
||||||
$this->_exec('vendor/bin/codecept build');
|
|
||||||
$this->loadEnv();
|
$this->loadEnv();
|
||||||
|
$this->_exec('vendor/bin/codecept build');
|
||||||
$this->_exec('vendor/bin/codecept run unit ' . $unit);
|
$this->_exec('vendor/bin/codecept run unit ' . $unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testAcceptance() {
|
function testAcceptance() {
|
||||||
$this->_exec('vendor/bin/codecept build');
|
|
||||||
$this->loadEnv();
|
$this->loadEnv();
|
||||||
$this->taskExec('phantomjs --webdriver=4444')->background()->run();
|
$this->_exec('vendor/bin/codecept build');
|
||||||
|
$this->taskExec('phantomjs --webdriver=4444')
|
||||||
|
->background()
|
||||||
|
->run();
|
||||||
sleep(2);
|
sleep(2);
|
||||||
$this->_exec('vendor/bin/codecept run acceptance');
|
$this->_exec('vendor/bin/codecept run acceptance');
|
||||||
}
|
}
|
||||||
@ -56,7 +63,9 @@ class RoboFile extends \Robo\Tasks {
|
|||||||
function testAll() {
|
function testAll() {
|
||||||
$this->_exec('vendor/bin/codecept build');
|
$this->_exec('vendor/bin/codecept build');
|
||||||
$this->loadEnv();
|
$this->loadEnv();
|
||||||
$this->taskexec('phantomjs --webdriver=4444')->background()->run();
|
$this->taskexec('phantomjs --webdriver=4444')
|
||||||
|
->background()
|
||||||
|
->run();
|
||||||
sleep(2);
|
sleep(2);
|
||||||
$this->_exec('vendor/bin/codecept run');
|
$this->_exec('vendor/bin/codecept run');
|
||||||
}
|
}
|
||||||
@ -71,8 +80,13 @@ class RoboFile extends \Robo\Tasks {
|
|||||||
$dotenv = new Dotenv\Dotenv(__DIR__);
|
$dotenv = new Dotenv\Dotenv(__DIR__);
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
$this->taskWriteToFile('tests/acceptance.suite.yml')->textFromFile('tests/acceptance.suite.src')->run();
|
$this->taskWriteToFile('tests/acceptance.suite.yml')
|
||||||
|
->textFromFile('tests/acceptance.suite.src')
|
||||||
|
->run();
|
||||||
|
|
||||||
$this->taskReplaceInFile('tests/acceptance.suite.yml')->regex("/url.*/")->to('url: ' . "'" . getenv('WP_TEST_URL') . "'")->run();
|
$this->taskReplaceInFile('tests/acceptance.suite.yml')
|
||||||
|
->regex("/url.*/")
|
||||||
|
->to('url: ' . "'" . getenv('WP_TEST_URL') . "'")
|
||||||
|
->run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,4 @@
|
|||||||
{
|
{
|
||||||
"repositories": [
|
|
||||||
{
|
|
||||||
"type": "vcs",
|
|
||||||
"url": "https://github.com/mailpoet/sudzy"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.3",
|
"php": ">=5.3.3",
|
||||||
"twig/twig": "1.*",
|
"twig/twig": "1.*",
|
||||||
@ -12,9 +6,7 @@
|
|||||||
"cerdic/css-tidy": "*",
|
"cerdic/css-tidy": "*",
|
||||||
"sunra/php-simple-html-dom-parser": "*",
|
"sunra/php-simple-html-dom-parser": "*",
|
||||||
"tburry/pquery": "*",
|
"tburry/pquery": "*",
|
||||||
"j4mie/paris": "1.5.4 as 1.4",
|
"j4mie/paris": "1.5.4 as 1.4"
|
||||||
"tag/sudzy" : "dev-master"
|
|
||||||
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"codeception/codeception": "*",
|
"codeception/codeception": "*",
|
||||||
@ -22,11 +14,12 @@
|
|||||||
"codegyre/robo": "*",
|
"codegyre/robo": "*",
|
||||||
"vlucas/phpdotenv": "*",
|
"vlucas/phpdotenv": "*",
|
||||||
"umpirsky/twig-gettext-extractor": "1.1.*",
|
"umpirsky/twig-gettext-extractor": "1.1.*",
|
||||||
"raveren/kint": "^1.0"
|
"raveren/kint": "^1.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"MailPoet\\": "lib/"
|
"MailPoet\\": "lib/",
|
||||||
|
"Sudzy\\": "lib/Util/Sudzy"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
55
composer.lock
generated
55
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"hash": "99a6abaeb9ef73071b63a330b58242cc",
|
"hash": "16954b64754d0fb7dd118fb4dba102b7",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "cerdic/css-tidy",
|
"name": "cerdic/css-tidy",
|
||||||
@ -255,55 +255,6 @@
|
|||||||
],
|
],
|
||||||
"time": "2013-05-04 14:32:03"
|
"time": "2013-05-04 14:32:03"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "tag/sudzy",
|
|
||||||
"version": "dev-master",
|
|
||||||
"source": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/mailpoet/sudzy.git",
|
|
||||||
"reference": "329e1b339baa1e662cb263e5bc30930456ab5227"
|
|
||||||
},
|
|
||||||
"dist": {
|
|
||||||
"type": "zip",
|
|
||||||
"url": "https://api.github.com/repos/mailpoet/sudzy/zipball/329e1b339baa1e662cb263e5bc30930456ab5227",
|
|
||||||
"reference": "329e1b339baa1e662cb263e5bc30930456ab5227",
|
|
||||||
"shasum": ""
|
|
||||||
},
|
|
||||||
"require": {
|
|
||||||
"j4mie/paris": ">=1.4",
|
|
||||||
"php": ">=5.3.0"
|
|
||||||
},
|
|
||||||
"type": "library",
|
|
||||||
"autoload": {
|
|
||||||
"psr-0": {
|
|
||||||
"Sudzy": ".",
|
|
||||||
"ValidationException": "Sudzy"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"license": [
|
|
||||||
"BSD"
|
|
||||||
],
|
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "Tom Gregory",
|
|
||||||
"email": "tom@alt-tag.com",
|
|
||||||
"homepage": "http://alt-tag.com/",
|
|
||||||
"role": "Developer"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "Model validator for use with Paris (and Idiorm).",
|
|
||||||
"homepage": "https://github.com/tag/sudzy",
|
|
||||||
"keywords": [
|
|
||||||
"idiorm",
|
|
||||||
"model",
|
|
||||||
"paris",
|
|
||||||
"validator"
|
|
||||||
],
|
|
||||||
"support": {
|
|
||||||
"source": "https://github.com/mailpoet/sudzy/tree/master"
|
|
||||||
},
|
|
||||||
"time": "2015-08-11 20:30:34"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "tburry/pquery",
|
"name": "tburry/pquery",
|
||||||
"version": "v1.1.0",
|
"version": "v1.1.0",
|
||||||
@ -3005,9 +2956,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"minimum-stability": "stable",
|
"minimum-stability": "stable",
|
||||||
"stability-flags": {
|
"stability-flags": [],
|
||||||
"tag/sudzy": 20
|
|
||||||
},
|
|
||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
"prefer-lowest": false,
|
"prefer-lowest": false,
|
||||||
"platform": {
|
"platform": {
|
||||||
|
@ -7,12 +7,9 @@ class CustomValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
$this->validator->addValidator(
|
$this->validator->addValidator('isString', function ($val) {
|
||||||
'isString',
|
return is_string($val);
|
||||||
function ($val) {
|
});
|
||||||
return is_string($val);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->validator;
|
return $this->validator;
|
||||||
}
|
}
|
||||||
|
@ -11,21 +11,17 @@ class Setting extends Model {
|
|||||||
function __construct() {
|
function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->addValidations(
|
$this->addValidations("name", array(
|
||||||
"name",
|
"required" => "name_is_blank",
|
||||||
array("required" => "validation_option_name_blank",
|
"isString" => "name_is_not_string",
|
||||||
"isString" => "validation_option_name_string",
|
"minLength|2" => "name_is_short"
|
||||||
"minLength|2" => "validation_option_name_length"
|
));
|
||||||
)
|
$this->addValidations("value", array(
|
||||||
);
|
"required" => "value_is_blank",
|
||||||
$this->addValidations(
|
"isString" => "value_is_not_string",
|
||||||
"value",
|
"minLength|2" => "value_is_short"
|
||||||
array("required" => "validation_option_value_blank",
|
));
|
||||||
"isString" => "validation_option_value_string",
|
|
||||||
"minLength|2" => "validation_option_value_length"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -9,24 +9,18 @@ class Subscriber extends Model {
|
|||||||
public static $_table = MP_SUBSCRIBERS_TABLE;
|
public static $_table = MP_SUBSCRIBERS_TABLE;
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
$this->addValidations(
|
$this->addValidations('email', array(
|
||||||
'email',
|
'required' => "email_is_blank",
|
||||||
array('required' => "validation_email_blank",
|
'isEmail' => "email_is_invalid"
|
||||||
'isEmail' => "validation_email_invalid"
|
));
|
||||||
)
|
$this->addValidations('first_name', array(
|
||||||
);
|
'required' => "first_name_is_blank",
|
||||||
$this->addValidations(
|
'minLength|2' => "first_name_is_short"
|
||||||
'first_name',
|
));
|
||||||
array('required' => "validation_first_name_blank",
|
$this->addValidations('last_name', array(
|
||||||
'minLength|2' => "validation_first_name_length"
|
'required' => "last_name_is_blank",
|
||||||
)
|
'minLength|2' => "last_name_is_short"
|
||||||
);
|
));
|
||||||
$this->addValidations(
|
|
||||||
'last_name',
|
|
||||||
array('required' => "validation_last_name_blank",
|
|
||||||
'minLength|2' => "validation_last_name_length"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
104
lib/Util/Sudzy/Engine.php
Normal file
104
lib/Util/Sudzy/Engine.php
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Sudzy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton valdation engine
|
||||||
|
**/
|
||||||
|
class Engine
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Validation methods are stored here so they can easily be overwritten
|
||||||
|
*/
|
||||||
|
protected $_checks;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->_checks = array(
|
||||||
|
'required' => array($this, '_required'),
|
||||||
|
'minLength' => array($this, '_minLength'),
|
||||||
|
'maxLength' => array($this, '_maxLength'),
|
||||||
|
'isEmail' => array($this, '_isEmail'),
|
||||||
|
'isInteger' => array($this, '_isInteger'),
|
||||||
|
'isNumeric' => array($this, '_isNumeric')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __call($name, $args)
|
||||||
|
{
|
||||||
|
if (!isset($this->_checks[$name]))
|
||||||
|
throw new \InvalidArgumentException("{$name} is not a valid validation function.");
|
||||||
|
|
||||||
|
$val = array_shift($args);
|
||||||
|
$args = array_shift($args);
|
||||||
|
|
||||||
|
return call_user_func($this->_checks[$name], $val, $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function executeOne($check, $val, $params=array())
|
||||||
|
{
|
||||||
|
return call_user_func(__NAMESPACE__ .'\Engine::'.$check, $val, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string label used to call function
|
||||||
|
* @param Callable function with params (value, additional params as array)
|
||||||
|
*/
|
||||||
|
public function addValidator($label, $function)
|
||||||
|
{
|
||||||
|
if (isset($this->_checks[$label])) throw Exception();
|
||||||
|
$this->setValidator($label, $function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setValidator($label, $function)
|
||||||
|
{
|
||||||
|
$this->_checks[$label] = $function;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeValidator($label)
|
||||||
|
{
|
||||||
|
unset($this->_checks[$label]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string The list of usable validator methods
|
||||||
|
*/
|
||||||
|
public function getValidators()
|
||||||
|
{
|
||||||
|
return array_keys($this->_checks);
|
||||||
|
}
|
||||||
|
|
||||||
|
///// Validator methods
|
||||||
|
protected function _isEmail($val, $params)
|
||||||
|
{
|
||||||
|
return FALSE !== filter_var($val, FILTER_VALIDATE_EMAIL);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _isInteger($val, $params)
|
||||||
|
{
|
||||||
|
if (!is_numeric($val)) return false;
|
||||||
|
return intval($val) == $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _isNumeric($val, $params)
|
||||||
|
{
|
||||||
|
return is_numeric($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _minLength($val, $params)
|
||||||
|
{
|
||||||
|
$len = array_shift($params);
|
||||||
|
return strlen($val) >= $len;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _maxLength($val, $params)
|
||||||
|
{
|
||||||
|
$len = array_shift($params);
|
||||||
|
return strlen($val) <= $len;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _required($val, $params=array())
|
||||||
|
{
|
||||||
|
return !(($val === null) || ('' === trim($val)));
|
||||||
|
}
|
||||||
|
}
|
174
lib/Util/Sudzy/ValidModel.php
Normal file
174
lib/Util/Sudzy/ValidModel.php
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
<?php
|
||||||
|
namespace Sudzy;
|
||||||
|
|
||||||
|
abstract class ValidModel extends \Model
|
||||||
|
{
|
||||||
|
protected $_validator = null; // Reference to Sudzy validator object
|
||||||
|
protected $_validations = array(); // Array of validations
|
||||||
|
protected $_validationErrors = array(); // Array of error messages
|
||||||
|
protected $_validationOptions = array(
|
||||||
|
'indexedErrors' => false, // If True getValidationErrors will return an array with the index
|
||||||
|
// being the field name and the value the error. If multiple errors
|
||||||
|
// are triggered for a field only the first will be kept.
|
||||||
|
'throw' => self::ON_SAVE // One of self::ON_SET|ON_SAVE|NEVER.
|
||||||
|
// + ON_SET throws immediately when field is set()
|
||||||
|
// + ON_SAVE throws on save()
|
||||||
|
// + NEVER means an exception is never thrown; check for ->getValidaionErrors()
|
||||||
|
);
|
||||||
|
|
||||||
|
const ON_SET = 'set';
|
||||||
|
const ON_SAVE = 'save';
|
||||||
|
const NEVER = null;
|
||||||
|
|
||||||
|
public function __construct($validatorInstance = null) {
|
||||||
|
$this->_validator = $validatorInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setValidationOptions($options)
|
||||||
|
{
|
||||||
|
$this->_validationOptions = array_merge($this->_validationOptions, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addValidation($field, $validation, $message) {
|
||||||
|
if (!isset($this->_validations[$field])) {
|
||||||
|
$this->_validations[$field] = array();
|
||||||
|
}
|
||||||
|
$this->_validations[$field][] = array(
|
||||||
|
'validation' => $validation,
|
||||||
|
'message' => $message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addValidations($field, $validators) {
|
||||||
|
foreach ($validators as $validation => $message) {
|
||||||
|
$this->addValidation($field, $validation, $message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * Checks, without throwing exceptions, model fields with validations
|
||||||
|
// *
|
||||||
|
// * @return bool If false, running $this->doValidationError() will respond appropriately
|
||||||
|
// */
|
||||||
|
// public function validate()
|
||||||
|
// {
|
||||||
|
// $fields = array_keys($this->_validations);
|
||||||
|
// $success = true;
|
||||||
|
// foreach ($fields as $f) {
|
||||||
|
// $success = $success && $this->validateField($f, $this->$f);
|
||||||
|
// }
|
||||||
|
// return $success;
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool Will set a message if returning false
|
||||||
|
**/
|
||||||
|
public function validateField($field, $value)
|
||||||
|
{
|
||||||
|
$this->setupValidationEngine();
|
||||||
|
|
||||||
|
if (!isset($this->_validations[$field])) {
|
||||||
|
return true; // No validations, return true by default
|
||||||
|
}
|
||||||
|
|
||||||
|
$success = true;
|
||||||
|
foreach ($this->_validations[$field] as $v) {
|
||||||
|
$checks = explode(' ', $v['validation']);
|
||||||
|
foreach ($checks as $check) {
|
||||||
|
$params = explode('|', $check);
|
||||||
|
$check = array_shift($params);
|
||||||
|
|
||||||
|
if ($this->_validator->executeOne($check, $value, $params)) {
|
||||||
|
$success = $success && true;
|
||||||
|
} else {
|
||||||
|
$this->addValidationError($v['message'], $field);
|
||||||
|
$success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidationErrors()
|
||||||
|
{
|
||||||
|
return $this->_validationErrors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resetValidationErrors()
|
||||||
|
{
|
||||||
|
$this->_validationErrors = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////
|
||||||
|
// Overloaded methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload __set to call validateAndSet
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
$this->validateAndSet($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload save; checks if errors exist before saving
|
||||||
|
*/
|
||||||
|
public function save()
|
||||||
|
{
|
||||||
|
if ($this->isNew()) { //Fields populated by create() or hydrate() don't pass through set()
|
||||||
|
foreach( array_keys($this->_validations) as $field) {
|
||||||
|
$this->validateField($field, $this->$field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$errs = $this->getValidationErrors();
|
||||||
|
if (!empty($errs))
|
||||||
|
$this->doValidationError(self::ON_SAVE);
|
||||||
|
|
||||||
|
parent::save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload set; to call validateAndSet
|
||||||
|
* // TODO: handle multiple sets if $name is a field=>val array
|
||||||
|
*/
|
||||||
|
public function set($name, $value = null)
|
||||||
|
{
|
||||||
|
$this->validateAndSet($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
// Protected methods
|
||||||
|
protected function doValidationError($context)
|
||||||
|
{
|
||||||
|
if ($context == $this->_validationOptions['throw'])
|
||||||
|
throw new \ValidationException($this->_validationErrors);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addValidationError($msg, $field = null)
|
||||||
|
{
|
||||||
|
if ($this->_validationOptions['indexedErrors'] && $field !== null) {
|
||||||
|
// Only keep the first error found on a field
|
||||||
|
if (!isset($this->_validationErrors[$field])) {
|
||||||
|
$this->_validationErrors[$field] = $msg;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->_validationErrors[] = $msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload set; to call validateAndSet
|
||||||
|
*/
|
||||||
|
protected function validateAndSet($name, $value)
|
||||||
|
{
|
||||||
|
if (!$this->validateField($name, $value)) $this->doValidationError(self::ON_SET);
|
||||||
|
parent::set($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setupValidationEngine()
|
||||||
|
{
|
||||||
|
if (null == $this->_validator) $this->_validator = new \Sudzy\Engine(); // Is lazy setup worth it?
|
||||||
|
}
|
||||||
|
}
|
17
lib/Util/Sudzy/ValidationException.php
Normal file
17
lib/Util/Sudzy/ValidationException.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class ValidationException extends \Exception
|
||||||
|
{
|
||||||
|
protected $_validationErrors;
|
||||||
|
|
||||||
|
public function __construct($errs)
|
||||||
|
{
|
||||||
|
$this->_validationErrors = $errs;
|
||||||
|
parent::__construct(implode("\n", $errs));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidationErrors()
|
||||||
|
{
|
||||||
|
return $this->_validationErrors;
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
// This is global bootstrap for autoloading
|
|
||||||
|
use MailPoet\Config\Env;
|
||||||
|
|
||||||
|
$wordpress_path = getenv('WP_TEST_PATH');
|
||||||
|
|
||||||
|
if ($wordpress_path) {
|
||||||
|
if (file_exists($wordpress_path . '/wp-load.php')) {
|
||||||
|
require_once(getenv('WP_TEST_PATH') . '/wp-load.php');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Exception("You need to specify the path to your WordPress installation\n`WP_TEST_PATH` in your .env file");
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
// clean database on each run
|
||||||
|
$truncate_commands = $wpdb->get_results("SELECT concat('TRUNCATE TABLE `', TABLE_NAME, '`;')
|
||||||
|
FROM INFORMATION_SCHEMA.TABLES
|
||||||
|
WHERE TABLE_SCHEMA = '" . Env::$db_name . "' AND TABLE_NAME LIKE '" . Env::$db_prefix . "%'", ARRAY_A);
|
||||||
|
foreach ($truncate_commands as $command) {
|
||||||
|
$wpdb->query(array_values($command)[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,55 +4,54 @@ use MailPoet\Models\Setting;
|
|||||||
class SettingCest {
|
class SettingCest {
|
||||||
|
|
||||||
function _before() {
|
function _before() {
|
||||||
$this->data = array('name' => 'sending_method', 'value' => 'smtp');
|
$this->data = array(
|
||||||
|
'name' => 'sending_method',
|
||||||
// clean up after previously failed test
|
'value' => 'smtp'
|
||||||
$setting = Setting::where('name', $this->data['name'])->findOne();
|
);
|
||||||
if ($setting !== false) {
|
|
||||||
$setting->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
$setting = Setting::create();
|
$setting = Setting::create();
|
||||||
$setting->name = $this->data['name'];
|
$setting->hydrate($this->data);
|
||||||
$setting->value = $this->data['value'];
|
|
||||||
$setting->save();
|
$setting->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCanBeCreated() {
|
function itCanBeCreated() {
|
||||||
$setting = Setting::where('name', $this->data['name'])->findOne();
|
$setting = Setting::where('name', $this->data['name'])
|
||||||
|
->findOne();
|
||||||
expect($setting->id)->notNull();
|
expect($setting->id)->notNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
function nameShouldValidate() {
|
function nameShouldValidate() {
|
||||||
$conflict_setting = Setting::create();
|
$conflict_setting = Setting::create();
|
||||||
$conflict_setting->validateField('name', '');
|
$conflict_setting->validateField('name', '');
|
||||||
expect($conflict_setting->getValidationErrors()[0])->equals('validation_option_name_blank');
|
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_blank');
|
||||||
|
|
||||||
$conflict_setting = Setting::create();
|
$conflict_setting = Setting::create();
|
||||||
$conflict_setting->validateField('name', 31337);
|
$conflict_setting->validateField('name', 31337);
|
||||||
expect($conflict_setting->getValidationErrors()[0])->equals('validation_option_name_string');
|
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_not_string');
|
||||||
|
|
||||||
$conflict_setting = Setting::create();
|
$conflict_setting = Setting::create();
|
||||||
$conflict_setting->validateField('name', 'a');
|
$conflict_setting->validateField('name', 'a');
|
||||||
expect($conflict_setting->getValidationErrors()[0])->equals('validation_option_name_length');
|
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_short');
|
||||||
}
|
}
|
||||||
|
|
||||||
function valueShouldValidate() {
|
function valueShouldValidate() {
|
||||||
$conflict_setting = Setting::create();
|
$conflict_setting = Setting::create();
|
||||||
$conflict_setting->validateField('value', '');
|
$conflict_setting->validateField('value', '');
|
||||||
expect($conflict_setting->getValidationErrors()[0])->equals('validation_option_value_blank');
|
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_blank');
|
||||||
|
|
||||||
$conflict_setting = Setting::create();
|
$conflict_setting = Setting::create();
|
||||||
$conflict_setting->validateField('value', 31337);
|
$conflict_setting->validateField('value', 31337);
|
||||||
expect($conflict_setting->getValidationErrors()[0])->equals('validation_option_value_string');
|
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_not_string');
|
||||||
|
|
||||||
$conflict_setting = Setting::create();
|
$conflict_setting = Setting::create();
|
||||||
$conflict_setting->validateField('value', 'a');
|
$conflict_setting->validateField('value', 'a');
|
||||||
expect($conflict_setting->getValidationErrors()[0])->equals('validation_option_value_length');
|
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_short');
|
||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
$setting = Setting::where('name', $this->data['name'])->findOne()->delete();
|
$setting = Setting::where('name', $this->data['name'])
|
||||||
|
->findOne()
|
||||||
|
->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,50 +4,44 @@ use MailPoet\Models\Subscriber;
|
|||||||
class SubscriberCest {
|
class SubscriberCest {
|
||||||
|
|
||||||
function _before() {
|
function _before() {
|
||||||
$this->data = array('first_name' => 'John', 'last_name' => 'Mailer', 'email' => 'john@mailpoet.com');
|
$this->data = array(
|
||||||
|
'first_name' => 'John',
|
||||||
// clean up after previously failed test
|
'last_name' => 'Mailer',
|
||||||
$subscriber = Subscriber::where('email', $this->data['email'])->findOne();
|
'email' => 'john@mailpoet.com'
|
||||||
if ($subscriber !== false) {
|
);
|
||||||
$subscriber->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->subscriber = Subscriber::create();
|
$this->subscriber = Subscriber::create();
|
||||||
|
$this->subscriber->hydrate($this->data);
|
||||||
$this->subscriber->first_name = $this->data['first_name'];
|
|
||||||
|
|
||||||
$this->subscriber->last_name = $this->data['last_name'];
|
|
||||||
|
|
||||||
$this->subscriber->email = $this->data['email'];
|
|
||||||
|
|
||||||
$this->subscriber->save();
|
$this->subscriber->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
function itCanBeCreated() {
|
function itCanBeCreated() {
|
||||||
$subscriber = Subscriber::where('email', $this->data['email'])->findOne();
|
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||||
|
->findOne();
|
||||||
expect($subscriber->id)->notNull();
|
expect($subscriber->id)->notNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
function itHasAFirstName() {
|
function itHasAFirstName() {
|
||||||
$subscriber = Subscriber::where('email', $this->data['email'])->findOne();
|
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||||
|
->findOne();
|
||||||
expect($subscriber->first_name)->equals($this->data['first_name']);
|
expect($subscriber->first_name)->equals($this->data['first_name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function itHasALastName() {
|
function itHasALastName() {
|
||||||
$subscriber = Subscriber::where('email', $this->data['email'])->findOne();
|
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||||
|
->findOne();
|
||||||
expect($subscriber->last_name)->equals($this->data['last_name']);
|
expect($subscriber->last_name)->equals($this->data['last_name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function itHasAnEmail() {
|
function itHasAnEmail() {
|
||||||
$subscriber = Subscriber::where('email', $this->data['email'])->findOne();
|
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||||
|
->findOne();
|
||||||
expect($subscriber->email)->equals($this->data['email']);
|
expect($subscriber->email)->equals($this->data['email']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function emailMustBeUnique() {
|
function emailMustBeUnique() {
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->first_name = 'First';
|
$conflict_subscriber->hydrate($this->data);
|
||||||
$conflict_subscriber->last_name = 'Last';
|
|
||||||
$conflict_subscriber->email = $this->data['email'];
|
|
||||||
$conflicted = false;
|
$conflicted = false;
|
||||||
try {
|
try {
|
||||||
$conflict_subscriber->save();
|
$conflict_subscriber->save();
|
||||||
@ -60,36 +54,38 @@ class SubscriberCest {
|
|||||||
function emailShouldValidate() {
|
function emailShouldValidate() {
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->validateField('email', '');
|
$conflict_subscriber->validateField('email', '');
|
||||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('validation_email_blank');
|
expect($conflict_subscriber->getValidationErrors()[0])->equals('email_is_blank');
|
||||||
|
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->validateField('email', 'some @ email . com');
|
$conflict_subscriber->validateField('email', 'some @ email . com');
|
||||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('validation_email_invalid');
|
expect($conflict_subscriber->getValidationErrors()[0])->equals('email_is_invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
function firstNameShouldValidate() {
|
function firstNameShouldValidate() {
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->validateField('first_name', '');
|
$conflict_subscriber->validateField('first_name', '');
|
||||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('validation_first_name_blank');
|
expect($conflict_subscriber->getValidationErrors()[0])->equals('first_name_is_blank');
|
||||||
|
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->validateField('first_name', 'a');
|
$conflict_subscriber->validateField('first_name', 'a');
|
||||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('validation_first_name_length');
|
expect($conflict_subscriber->getValidationErrors()[0])->equals('first_name_is_short');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function lastNameShouldValidate() {
|
function lastNameShouldValidate() {
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->validateField('last_name', '');
|
$conflict_subscriber->validateField('last_name', '');
|
||||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('validation_last_name_blank');
|
expect($conflict_subscriber->getValidationErrors()[0])->equals('last_name_is_blank');
|
||||||
|
|
||||||
$conflict_subscriber = Subscriber::create();
|
$conflict_subscriber = Subscriber::create();
|
||||||
$conflict_subscriber->validateField('last_name', 'a');
|
$conflict_subscriber->validateField('last_name', 'a');
|
||||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('validation_last_name_length');
|
expect($conflict_subscriber->getValidationErrors()[0])->equals('last_name_is_short');
|
||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
$subscriber = Subscriber::where('email', $this->data['email'])->findOne()->delete();
|
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||||
|
->findOne()
|
||||||
|
->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$wordpress_path = getenv('WP_TEST_PATH');
|
|
||||||
|
|
||||||
if($wordpress_path) {
|
|
||||||
if(file_exists($wordpress_path.'/wp-load.php')) {
|
|
||||||
require_once(getenv('WP_TEST_PATH').'/wp-load.php');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Exception("You need to specify the path to your WordPress installation\n`WP_TEST_PATH` in your .env file");
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user