From cb4a8c764c9f8332dbfbf0ef3afd9c8687d92e46 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 6 Dec 2013 14:19:17 -0500 Subject: [PATCH] Refactored validators. --- docs/guide/validation.md | 4 +- framework/yii/base/Model.php | 2 +- framework/yii/captcha/CaptchaValidator.php | 32 ++---- framework/yii/validators/BooleanValidator.php | 38 +++----- framework/yii/validators/CompareValidator.php | 75 +++++++------- framework/yii/validators/DateValidator.php | 33 +++---- framework/yii/validators/DefaultValueValidator.php | 4 +- framework/yii/validators/EmailValidator.php | 55 ++++------- framework/yii/validators/ExistValidator.php | 19 ++-- framework/yii/validators/FileValidator.php | 57 +++++------ framework/yii/validators/FilterValidator.php | 9 +- framework/yii/validators/ImageValidator.php | 41 ++++---- framework/yii/validators/InlineValidator.php | 24 +---- framework/yii/validators/NumberValidator.php | 36 +++---- framework/yii/validators/RangeValidator.php | 33 ++----- .../yii/validators/RegularExpressionValidator.php | 33 ++----- framework/yii/validators/RequiredValidator.php | 46 +++------ framework/yii/validators/SafeValidator.php | 6 -- framework/yii/validators/StringValidator.php | 38 ++++---- framework/yii/validators/UniqueValidator.php | 8 +- framework/yii/validators/UrlValidator.php | 36 +++---- framework/yii/validators/Validator.php | 59 ++++++++--- .../framework/validators/BooleanValidatorTest.php | 28 +++--- .../framework/validators/CompareValidatorTest.php | 12 +-- .../framework/validators/DateValidatorTest.php | 18 ++-- .../framework/validators/EmailValidatorTest.php | 94 +++++++++--------- .../framework/validators/ExistValidatorTest.php | 14 +-- .../framework/validators/NumberValidatorTest.php | 108 ++++++++++----------- .../framework/validators/RangeValidatorTest.php | 42 ++++---- .../validators/RegularExpressionValidatorTest.php | 16 +-- .../framework/validators/RequiredValidatorTest.php | 30 +++--- .../framework/validators/StringValidatorTest.php | 52 +++++----- .../unit/framework/validators/UrlValidatorTest.php | 40 ++++---- tests/unit/framework/validators/ValidatorTest.php | 24 ++--- 34 files changed, 501 insertions(+), 665 deletions(-) diff --git a/docs/guide/validation.md b/docs/guide/validation.md index 59242f5..431300f 100644 --- a/docs/guide/validation.md +++ b/docs/guide/validation.md @@ -172,10 +172,10 @@ operate without model do. In our case to validate an email we can do the followi ```php $email = 'test@example.com'; $validator = new yii\validators\EmailValidator(); -if ($validator->validateValue($email)) { +if ($validator->validate($email, $error)) { echo 'Email is valid.'; } else { - echo 'Email is not valid.' + echo $error; } ``` diff --git a/framework/yii/base/Model.php b/framework/yii/base/Model.php index 3668826..22849b2 100644 --- a/framework/yii/base/Model.php +++ b/framework/yii/base/Model.php @@ -321,7 +321,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess } if ($this->beforeValidate()) { foreach ($this->getActiveValidators() as $validator) { - $validator->validate($this, $attributes); + $validator->validateAttributes($this, $attributes); } $this->afterValidate(); return !$this->hasErrors(); diff --git a/framework/yii/captcha/CaptchaValidator.php b/framework/yii/captcha/CaptchaValidator.php index 01c9d11..424de2d 100644 --- a/framework/yii/captcha/CaptchaValidator.php +++ b/framework/yii/captcha/CaptchaValidator.php @@ -38,7 +38,7 @@ class CaptchaValidator extends Validator /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -49,28 +49,13 @@ class CaptchaValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ - public function validateAttribute($object, $attribute) - { - $value = $object->$attribute; - if (!$this->validateValue($value)) { - $this->addError($object, $attribute, $this->message); - } - } - - /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - */ - public function validateValue($value) + protected function validateValue($value) { $captcha = $this->createCaptchaAction(); - return !is_array($value) && $captcha->validate($value, $this->caseSensitive); + $valid = !is_array($value) && $captcha->validate($value, $this->caseSensitive); + return $valid ? null : [$this->message, []]; } /** @@ -93,12 +78,7 @@ class CaptchaValidator extends Validator } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/BooleanValidator.php b/framework/yii/validators/BooleanValidator.php index 9d74705..961ed14 100644 --- a/framework/yii/validators/BooleanValidator.php +++ b/framework/yii/validators/BooleanValidator.php @@ -37,7 +37,7 @@ class BooleanValidator extends Validator public $strict = false; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -48,40 +48,24 @@ class BooleanValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ - public function validateAttribute($object, $attribute) + protected function validateValue($value) { - $value = $object->$attribute; - if (!$this->validateValue($value)) { - $this->addError($object, $attribute, $this->message, [ + $valid = !$this->strict && ($value == $this->trueValue || $value == $this->falseValue) + || $this->strict && ($value === $this->trueValue || $value === $this->falseValue); + if (!$valid) { + return [$this->message, [ 'true' => $this->trueValue, 'false' => $this->falseValue, - ]); + ]]; + } else { + return null; } } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - */ - public function validateValue($value) - { - return !$this->strict && ($value == $this->trueValue || $value == $this->falseValue) - || $this->strict && ($value === $this->trueValue || $value === $this->falseValue); - } - - /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/CompareValidator.php b/framework/yii/validators/CompareValidator.php index 08d4809..69bd6d5 100644 --- a/framework/yii/validators/CompareValidator.php +++ b/framework/yii/validators/CompareValidator.php @@ -41,7 +41,7 @@ class CompareValidator extends Validator */ public $compareAttribute; /** - * @var string the constant value to be compared with. When both this property + * @var mixed the constant value to be compared with. When both this property * and [[compareAttribute]] are set, this property takes precedence. * @see compareAttribute */ @@ -66,12 +66,13 @@ class CompareValidator extends Validator * - `{attribute}`: the label of the attribute being validated * - `{value}`: the value of the attribute being validated * - `{compareValue}`: the value or the attribute label to be compared with + * - `{compareAttribute}`: the label of the attribute to be compared with */ public $message; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -109,11 +110,7 @@ class CompareValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated - * @throws InvalidConfigException if CompareValidator::operator is invalid + * @inheritdoc */ public function validateAttribute($object, $attribute) { @@ -130,18 +127,7 @@ class CompareValidator extends Validator $compareLabel = $object->getAttributeLabel($compareAttribute); } - switch ($this->operator) { - case '==': $valid = $value == $compareValue; break; - case '===': $valid = $value === $compareValue; break; - case '!=': $valid = $value != $compareValue; break; - case '!==': $valid = $value !== $compareValue; break; - case '>': $valid = $value > $compareValue; break; - case '>=': $valid = $value >= $compareValue; break; - case '<': $valid = $value < $compareValue; break; - case '<=': $valid = $value <= $compareValue; break; - default: $valid = false; break; - } - if (!$valid) { + if (!$this->compareValues($this->operator, $value, $compareValue)) { $this->addError($object, $attribute, $this->message, [ 'compareAttribute' => $compareLabel, 'compareValue' => $compareValue, @@ -150,38 +136,47 @@ class CompareValidator extends Validator } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - * @throws InvalidConfigException if [[compareValue]] is not set. + * @inheritdoc */ - public function validateValue($value) + protected function validateValue($value) { if ($this->compareValue === null) { throw new InvalidConfigException('CompareValidator::compareValue must be set.'); } + if (!$this->compareValues($this->operator, $value, $this->compareValue)) { + return [$this->message, [ + 'compareAttribute' => $this->compareValue, + 'compareValue' => $this->compareValue, + ]]; + } else { + return null; + } + } - switch ($this->operator) { - case '==': return $value == $this->compareValue; - case '===': return $value === $this->compareValue; - case '!=': return $value != $this->compareValue; - case '!==': return $value !== $this->compareValue; - case '>': return $value > $this->compareValue; - case '>=': return $value >= $this->compareValue; - case '<': return $value < $this->compareValue; - case '<=': return $value <= $this->compareValue; + /** + * Compares two values with the specified operator. + * @param string $operator the comparison operator + * @param mixed $value the value being compared + * @param mixed $compareValue another value being compared + * @return boolean whether the comparison using the specified operator is true. + */ + protected function compareValues($operator, $value, $compareValue) + { + switch ($operator) { + case '==': return $value == $compareValue; + case '===': return $value === $compareValue; + case '!=': return $value != $compareValue; + case '!==': return $value !== $compareValue; + case '>': return $value > $compareValue; + case '>=': return $value >= $compareValue; + case '<': return $value < $compareValue; + case '<=': return $value <= $compareValue; default: return false; } } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated - * @return string the client-side validation script - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @throws InvalidConfigException if CompareValidator::operator is invalid + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/DateValidator.php b/framework/yii/validators/DateValidator.php index 60210bd..f5544a0 100644 --- a/framework/yii/validators/DateValidator.php +++ b/framework/yii/validators/DateValidator.php @@ -32,7 +32,7 @@ class DateValidator extends Validator public $timestampAttribute; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -43,36 +43,31 @@ class DateValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if (is_array($value)) { - $this->addError($object, $attribute, $this->message); - return; - } - $date = DateTime::createFromFormat($this->format, $value); - $errors = DateTime::getLastErrors(); - if ($date === false || $errors['error_count'] || $errors['warning_count']) { - $this->addError($object, $attribute, $this->message); + $result = $this->validateValue($value); + if (!empty($result)) { + $this->addError($object, $attribute, $result[0], $result[1]); } elseif ($this->timestampAttribute !== null) { + $date = DateTime::createFromFormat($this->format, $value); $object->{$this->timestampAttribute} = $date->getTimestamp(); } } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. + * @inheritdoc */ - public function validateValue($value) + protected function validateValue($value) { - DateTime::createFromFormat($this->format, $value); + if (is_array($value)) { + return [$this->message, []]; + } + $date = DateTime::createFromFormat($this->format, $value); $errors = DateTime::getLastErrors(); - return $errors['error_count'] === 0 && $errors['warning_count'] === 0; + $invalid = $date === false || $errors['error_count'] || $errors['warning_count']; + return $invalid ? [$this->message, []] : null; } } diff --git a/framework/yii/validators/DefaultValueValidator.php b/framework/yii/validators/DefaultValueValidator.php index 20df5fd..0db3a67 100644 --- a/framework/yii/validators/DefaultValueValidator.php +++ b/framework/yii/validators/DefaultValueValidator.php @@ -29,9 +29,7 @@ class DefaultValueValidator extends Validator public $skipOnEmpty = false; /** - * Validates the attribute of the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { diff --git a/framework/yii/validators/EmailValidator.php b/framework/yii/validators/EmailValidator.php index 6b5a07f..24eeaec 100644 --- a/framework/yii/validators/EmailValidator.php +++ b/framework/yii/validators/EmailValidator.php @@ -53,7 +53,7 @@ class EmailValidator extends Validator /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -67,51 +67,30 @@ class EmailValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ - public function validateAttribute($object, $attribute) - { - $value = $object->$attribute; - if (!$this->validateValue($value)) { - $this->addError($object, $attribute, $this->message); - } - } - - /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - */ - public function validateValue($value) + protected function validateValue($value) { // make sure string length is limited to avoid DOS attacks if (!is_string($value) || strlen($value) >= 320) { - return false; - } - if (!preg_match('/^(.*?)$/', $value, $matches)) { - return false; - } - $domain = $matches[3]; - if ($this->enableIDN) { - $value = $matches[1] . idn_to_ascii($matches[2]) . '@' . idn_to_ascii($domain) . $matches[4]; - } - $valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value); - if ($valid && $this->checkDNS) { - $valid = checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'); + $valid = false; + } elseif (!preg_match('/^(.*?)$/', $value, $matches)) { + $valid = false; + } else { + $domain = $matches[3]; + if ($this->enableIDN) { + $value = $matches[1] . idn_to_ascii($matches[2]) . '@' . idn_to_ascii($domain) . $matches[4]; + } + $valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value); + if ($valid && $this->checkDNS) { + $valid = checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'); + } } - return $valid; + return $valid ? null : [$this->message, []]; } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/ExistValidator.php b/framework/yii/validators/ExistValidator.php index ba3f332..585b82f 100644 --- a/framework/yii/validators/ExistValidator.php +++ b/framework/yii/validators/ExistValidator.php @@ -39,7 +39,7 @@ class ExistValidator extends Validator /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -50,11 +50,7 @@ class ExistValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * - * @param \yii\db\ActiveRecord $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { @@ -76,15 +72,12 @@ class ExistValidator extends Validator } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - * @throws InvalidConfigException if either [[className]] or [[attributeName]] is not set. + * @inheritdoc */ - public function validateValue($value) + protected function validateValue($value) { if (is_array($value)) { - return false; + return [$this->message, []]; } if ($this->className === null) { throw new InvalidConfigException('The "className" property must be set.'); @@ -96,6 +89,6 @@ class ExistValidator extends Validator $className = $this->className; $query = $className::find(); $query->where([$this->attributeName => $value]); - return $query->exists(); + return $query->exists() ? null : [$this->message, []]; } } diff --git a/framework/yii/validators/FileValidator.php b/framework/yii/validators/FileValidator.php index 4726c6c..2e6a544 100644 --- a/framework/yii/validators/FileValidator.php +++ b/framework/yii/validators/FileValidator.php @@ -95,7 +95,7 @@ class FileValidator extends Validator public $tooMany; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -124,9 +124,7 @@ class FileValidator extends Validator } /** - * Validates the attribute. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { @@ -149,62 +147,57 @@ class FileValidator extends Validator $this->addError($object, $attribute, $this->tooMany, ['limit' => $this->maxFiles]); } else { foreach ($files as $file) { - $this->validateFile($object, $attribute, $file); + $result = $this->validateValue($file); + if (!empty($result)) { + $this->addError($object, $attribute, $result[0], $result[1]); + } } } } else { - $file = $object->$attribute; - if ($file instanceof UploadedFile && $file->error != UPLOAD_ERR_NO_FILE) { - $this->validateFile($object, $attribute, $file); - } else { - $this->addError($object, $attribute, $this->uploadRequired); + $result = $this->validateValue($object->$attribute); + if (!empty($result)) { + $this->addError($object, $attribute, $result[0], $result[1]); } } } /** - * Internally validates a file object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated - * @param UploadedFile $file uploaded file passed to check against a set of rules + * @inheritdoc */ - public function validateFile($object, $attribute, $file) + protected function validateValue($file) { + if (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE) { + return [$this->uploadRequired, []]; + } switch ($file->error) { case UPLOAD_ERR_OK: if ($this->maxSize !== null && $file->size > $this->maxSize) { - $this->addError($object, $attribute, $this->tooBig, ['file' => $file->name, 'limit' => $this->getSizeLimit()]); - } - if ($this->minSize !== null && $file->size < $this->minSize) { - $this->addError($object, $attribute, $this->tooSmall, ['file' => $file->name, 'limit' => $this->minSize]); - } - if (!empty($this->types) && !in_array(strtolower(pathinfo($file->name, PATHINFO_EXTENSION)), $this->types, true)) { - $this->addError($object, $attribute, $this->wrongType, ['file' => $file->name, 'extensions' => implode(', ', $this->types)]); + return [$this->tooBig, ['file' => $file->name, 'limit' => $this->getSizeLimit()]]; + } elseif ($this->minSize !== null && $file->size < $this->minSize) { + return [$this->tooSmall, ['file' => $file->name, 'limit' => $this->minSize]]; + } elseif (!empty($this->types) && !in_array(strtolower(pathinfo($file->name, PATHINFO_EXTENSION)), $this->types, true)) { + return [$this->wrongType, ['file' => $file->name, 'extensions' => implode(', ', $this->types)]]; } break; case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: - $this->addError($object, $attribute, $this->tooBig, ['file' => $file->name, 'limit' => $this->getSizeLimit()]); - break; + return [$this->tooBig, ['file' => $file->name, 'limit' => $this->getSizeLimit()]]; case UPLOAD_ERR_PARTIAL: - $this->addError($object, $attribute, $this->message); Yii::warning('File was only partially uploaded: ' . $file->name, __METHOD__); - break; + return [$this->message, []]; case UPLOAD_ERR_NO_TMP_DIR: - $this->addError($object, $attribute, $this->message); Yii::warning('Missing the temporary folder to store the uploaded file: ' . $file->name, __METHOD__); - break; + return [$this->message, []]; case UPLOAD_ERR_CANT_WRITE: - $this->addError($object, $attribute, $this->message); Yii::warning('Failed to write the uploaded file to disk: ' . $file->name, __METHOD__); - break; + return [$this->message, []]; case UPLOAD_ERR_EXTENSION: - $this->addError($object, $attribute, $this->message); Yii::warning('File upload was stopped by some PHP extension: ' . $file->name, __METHOD__); - break; + return [$this->message, []]; default: break; } + return null; } /** diff --git a/framework/yii/validators/FilterValidator.php b/framework/yii/validators/FilterValidator.php index 560feb1..685faee 100644 --- a/framework/yii/validators/FilterValidator.php +++ b/framework/yii/validators/FilterValidator.php @@ -46,8 +46,7 @@ class FilterValidator extends Validator public $skipOnEmpty = false; /** - * Initializes the validator. - * @throws InvalidConfigException if [[filter]] is not set. + * @inheritdoc */ public function init() { @@ -58,11 +57,7 @@ class FilterValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated - * @throws InvalidConfigException if filter property is not a valid callback + * @inheritdoc */ public function validateAttribute($object, $attribute) { diff --git a/framework/yii/validators/ImageValidator.php b/framework/yii/validators/ImageValidator.php index a60cc93..b4ac1a8 100644 --- a/framework/yii/validators/ImageValidator.php +++ b/framework/yii/validators/ImageValidator.php @@ -110,7 +110,7 @@ class ImageValidator extends FileValidator public $wrongMimeType; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -140,58 +140,51 @@ class ImageValidator extends FileValidator } /** - * Internally validates a file object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated - * @param UploadedFile $file uploaded file passed to check against a set of rules + * @inheritdoc */ - public function validateFile($object, $attribute, $file) + protected function validateValue($file) { - parent::validateFile($object, $attribute, $file); - - if (!$object->hasErrors($attribute)) { - $this->validateImage($object, $attribute, $file); - } + $result = parent::validateValue($file); + return empty($result) ? $this->validateImage($file) : $result; } /** - * Internally validates a file object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * Validates an image file. * @param UploadedFile $image uploaded file passed to check against a set of rules + * @return array|null the error message and the parameters to be inserted into the error message. + * Null should be returned if the data is valid. */ - public function validateImage($object, $attribute, $image) + protected function validateImage($image) { if (!empty($this->mimeTypes) && !in_array(FileHelper::getMimeType($image->tempName), $this->mimeTypes, true)) { - $this->addError($object, $attribute, $this->wrongMimeType, ['file' => $image->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]); + return [$this->wrongMimeType, ['file' => $image->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]]; } if (false === ($imageInfo = getimagesize($image->tempName))) { - $this->addError($object, $attribute, $this->notImage, ['file' => $image->name]); - return; + return [$this->notImage, ['file' => $image->name]]; } list($width, $height, $type) = $imageInfo; if ($width == 0 || $height == 0) { - $this->addError($object, $attribute, $this->notImage, ['file' => $image->name]); - return; + return [$this->notImage, ['file' => $image->name]]; } if ($this->minWidth !== null && $width < $this->minWidth) { - $this->addError($object, $attribute, $this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]); + return [$this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]]; } if ($this->minHeight !== null && $height < $this->minHeight) { - $this->addError($object, $attribute, $this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]); + return [$this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]]; } if ($this->maxWidth !== null && $width > $this->maxWidth) { - $this->addError($object, $attribute, $this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]); + return [$this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]]; } if ($this->maxHeight !== null && $height > $this->maxHeight) { - $this->addError($object, $attribute, $this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]); + return [$this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]]; } + return null; } } diff --git a/framework/yii/validators/InlineValidator.php b/framework/yii/validators/InlineValidator.php index febf8dc..b769e4e 100644 --- a/framework/yii/validators/InlineValidator.php +++ b/framework/yii/validators/InlineValidator.php @@ -55,9 +55,7 @@ class InlineValidator extends Validator public $clientValidate; /** - * Validates the attribute of the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { @@ -69,25 +67,7 @@ class InlineValidator extends Validator } /** - * Returns the JavaScript needed for performing client-side validation. - * - * You may override this method to return the JavaScript validation code if - * the validator can support client-side validation. - * - * The following JavaScript variables are predefined and can be used in the validation code: - * - * - `attribute`: the name of the attribute being validated. - * - `value`: the value being validated. - * - `messages`: an array used to hold the validation error messages for the attribute. - * - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. Null if the validator does not support - * client-side validation. - * @see enableClientValidation - * @see \yii\web\ActiveForm::enableClientValidation + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/NumberValidator.php b/framework/yii/validators/NumberValidator.php index c114300..60e920a 100644 --- a/framework/yii/validators/NumberValidator.php +++ b/framework/yii/validators/NumberValidator.php @@ -56,7 +56,7 @@ class NumberValidator extends Validator /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -74,10 +74,7 @@ class NumberValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { @@ -99,24 +96,27 @@ class NumberValidator extends Validator } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. + * @inheritdoc */ - public function validateValue($value) + protected function validateValue($value) { - return preg_match($this->integerOnly ? $this->integerPattern : $this->numberPattern, "$value") - && ($this->min === null || $value >= $this->min) - && ($this->max === null || $value <= $this->max); + if (is_array($value)) { + return [Yii::t('yii', '{attribute} is invalid.'), []]; + } + $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; + if (!preg_match($pattern, "$value")) { + return [$this->message, []]; + } elseif ($this->min !== null && $value < $this->min) { + return [$this->tooSmall, ['min' => $this->min]]; + } elseif ($this->max !== null && $value > $this->max) { + return [$this->tooBig, ['max' => $this->max]]; + } else { + return null; + } } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/RangeValidator.php b/framework/yii/validators/RangeValidator.php index 6668969..cfd1f51 100644 --- a/framework/yii/validators/RangeValidator.php +++ b/framework/yii/validators/RangeValidator.php @@ -38,8 +38,7 @@ class RangeValidator extends Validator public $not = false; /** - * Initializes the validator. - * @throws InvalidConfigException if [[range]] is not set. + * @inheritdoc */ public function init() { @@ -53,37 +52,17 @@ class RangeValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ - public function validateAttribute($object, $attribute) + protected function validateValue($value) { - $value = $object->$attribute; - if (!$this->validateValue($value)) { - $this->addError($object, $attribute, $this->message); - } - } - - /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - */ - public function validateValue($value) - { - return !$this->not && in_array($value, $this->range, $this->strict) + $valid = !$this->not && in_array($value, $this->range, $this->strict) || $this->not && !in_array($value, $this->range, $this->strict); + return $valid ? null : [$this->message, []]; } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/RegularExpressionValidator.php b/framework/yii/validators/RegularExpressionValidator.php index efa9e22..7b02381 100644 --- a/framework/yii/validators/RegularExpressionValidator.php +++ b/framework/yii/validators/RegularExpressionValidator.php @@ -34,8 +34,7 @@ class RegularExpressionValidator extends Validator public $not = false; /** - * Initializes the validator. - * @throws InvalidConfigException if [[pattern]] is not set. + * @inheritdoc */ public function init() { @@ -49,38 +48,18 @@ class RegularExpressionValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ - public function validateAttribute($object, $attribute) + protected function validateValue($value) { - $value = $object->$attribute; - if (!$this->validateValue($value)) { - $this->addError($object, $attribute, $this->message); - } - } - - /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - */ - public function validateValue($value) - { - return !is_array($value) && + $valid = !is_array($value) && (!$this->not && preg_match($this->pattern, $value) || $this->not && !preg_match($this->pattern, $value)); + return $valid ? null : [$this->message, []]; } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/RequiredValidator.php b/framework/yii/validators/RequiredValidator.php index 2b5e333..43b40cf 100644 --- a/framework/yii/validators/RequiredValidator.php +++ b/framework/yii/validators/RequiredValidator.php @@ -51,7 +51,7 @@ class RequiredValidator extends Validator public $message; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -63,48 +63,28 @@ class RequiredValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ - public function validateAttribute($object, $attribute) - { - if (!$this->validateValue($object->$attribute)) { - if ($this->requiredValue === null) { - $this->addError($object, $attribute, $this->message); - } else { - $this->addError($object, $attribute, $this->message, [ - 'requiredValue' => $this->requiredValue, - ]); - } - } - } - - /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. - */ - public function validateValue($value) + protected function validateValue($value) { if ($this->requiredValue === null) { if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty($value, true)) { - return true; + return null; } } elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) { - return true; + return null; + } + if ($this->requiredValue === null) { + return [$this->message, []]; + } else { + return [$this->message, [ + 'requiredValue' => $this->requiredValue, + ]]; } - return false; } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/SafeValidator.php b/framework/yii/validators/SafeValidator.php index 25da899..6ef3e4d 100644 --- a/framework/yii/validators/SafeValidator.php +++ b/framework/yii/validators/SafeValidator.php @@ -15,10 +15,4 @@ namespace yii\validators; */ class SafeValidator extends Validator { - /** - * @inheritdoc - */ - public function validateAttribute($object, $attribute) - { - } } diff --git a/framework/yii/validators/StringValidator.php b/framework/yii/validators/StringValidator.php index 92aad56..a93fb72 100644 --- a/framework/yii/validators/StringValidator.php +++ b/framework/yii/validators/StringValidator.php @@ -63,7 +63,7 @@ class StringValidator extends Validator /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -95,10 +95,7 @@ class StringValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { @@ -123,28 +120,31 @@ class StringValidator extends Validator } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. + * @inheritdoc */ - public function validateValue($value) + protected function validateValue($value) { if (!is_string($value)) { - return false; + return [$this->message, []]; } + $length = mb_strlen($value, $this->encoding); - return ($this->min === null || $length >= $this->min) - && ($this->max === null || $length <= $this->max) - && ($this->length === null || $length === $this->length); + + if ($this->min !== null && $length < $this->min) { + return [$this->tooShort, ['min' => $this->min]]; + } + if ($this->max !== null && $length > $this->max) { + return [$this->tooLong, ['max' => $this->max]]; + } + if ($this->length !== null && $length !== $this->length) { + return [$this->notEqual, ['length' => $this->length]]; + } + + return null; } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/UniqueValidator.php b/framework/yii/validators/UniqueValidator.php index d123aad..d9cd587 100644 --- a/framework/yii/validators/UniqueValidator.php +++ b/framework/yii/validators/UniqueValidator.php @@ -35,7 +35,7 @@ class UniqueValidator extends Validator public $attributeName; /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -46,11 +46,7 @@ class UniqueValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\db\ActiveRecord $object the object being validated - * @param string $attribute the attribute being validated - * @throws InvalidConfigException if table doesn't have column specified + * @inheritdoc */ public function validateAttribute($object, $attribute) { diff --git a/framework/yii/validators/UrlValidator.php b/framework/yii/validators/UrlValidator.php index 5400c32..4054a1e 100644 --- a/framework/yii/validators/UrlValidator.php +++ b/framework/yii/validators/UrlValidator.php @@ -48,7 +48,7 @@ class UrlValidator extends Validator /** - * Initializes the validator. + * @inheritdoc */ public function init() { @@ -62,29 +62,23 @@ class UrlValidator extends Validator } /** - * Validates the attribute of the object. - * If there is any error, the error message is added to the object. - * @param \yii\base\Model $object the object being validated - * @param string $attribute the attribute being validated + * @inheritdoc */ public function validateAttribute($object, $attribute) { $value = $object->$attribute; - if ($this->validateValue($value)) { - if ($this->defaultScheme !== null && strpos($value, '://') === false) { - $object->$attribute = $this->defaultScheme . '://' . $value; - } - } else { - $this->addError($object, $attribute, $this->message); + $result = $this->validateValue($value); + if (!empty($result)) { + $this->addError($object, $attribute, $result[0], $result[1]); + } elseif ($this->defaultScheme !== null && strpos($value, '://') === false) { + $object->$attribute = $this->defaultScheme . '://' . $value; } } /** - * Validates the given value. - * @param mixed $value the value to be validated. - * @return boolean whether the value is valid. + * @inheritdoc */ - public function validateValue($value) + protected function validateValue($value) { // make sure the length is limited to avoid DOS attacks if (is_string($value) && strlen($value) < 2000) { @@ -105,20 +99,14 @@ class UrlValidator extends Validator } if (preg_match($pattern, $value)) { - return true; + return null; } } - return false; + return [$this->message, []]; } /** - * Returns the JavaScript needed for performing client-side validation. - * @param \yii\base\Model $object the data object being validated - * @param string $attribute the name of the attribute to be validated. - * @param \yii\web\View $view the view object that is going to be used to render views or view files - * containing a model form with this validator applied. - * @return string the client-side validation script. - * @see \yii\Web\ActiveForm::enableClientValidation + * @inheritdoc */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/Validator.php b/framework/yii/validators/Validator.php index 2cd611b..4b04b87 100644 --- a/framework/yii/validators/Validator.php +++ b/framework/yii/validators/Validator.php @@ -14,7 +14,7 @@ use yii\base\NotSupportedException; /** * Validator is the base class for all validators. * - * Child classes should override the [[validateAttribute()]] method to provide the actual + * Child classes should override the [[validateValue()]] and/or [[validateAttribute()]] methods to provide the actual * logic of performing data validation. Child classes may also override [[clientValidateAttribute()]] * to provide client-side validation support. * @@ -38,13 +38,14 @@ use yii\base\NotSupportedException; * - `required`: [[RequiredValidator]] * - `safe`: [[SafeValidator]] * - `string`: [[StringValidator]] + * - `trim`: [[FilterValidator]] * - `unique`: [[UniqueValidator]] * - `url`: [[UrlValidator]] * * @author Qiang Xue * @since 2.0 */ -abstract class Validator extends Component +class Validator extends Component { /** * @var array list of built-in validators (name => class or configuration) @@ -71,6 +72,10 @@ abstract class Validator extends Component 'required' => 'yii\validators\RequiredValidator', 'safe' => 'yii\validators\SafeValidator', 'string' => 'yii\validators\StringValidator', + 'trim' => [ + 'class' => 'yii\validators\FilterValidator', + 'filter' => 'trim', + ], 'unique' => 'yii\validators\UniqueValidator', 'url' => 'yii\validators\UrlValidator', ]; @@ -116,13 +121,6 @@ abstract class Validator extends Component */ public $enableClientValidation = true; - /** - * Validates a single attribute. - * Child classes must implement this method to provide the actual validation logic. - * @param \yii\base\Model $object the data object to be validated - * @param string $attribute the name of the attribute to be validated. - */ - abstract public function validateAttribute($object, $attribute); /** * Creates a validator object. @@ -177,7 +175,7 @@ abstract class Validator extends Component * it will be ignored. * If this parameter is null, every attribute listed in [[attributes]] will be validated. */ - public function validate($object, $attributes = null) + public function validateAttributes($object, $attributes = null) { if (is_array($attributes)) { $attributes = array_intersect($this->attributes, $attributes); @@ -194,12 +192,49 @@ abstract class Validator extends Component } /** + * Validates a single attribute. + * Child classes must implement this method to provide the actual validation logic. + * @param \yii\base\Model $object the data object to be validated + * @param string $attribute the name of the attribute to be validated. + */ + public function validateAttribute($object, $attribute) + { + $result = $this->validateValue($object->$attribute); + if (!empty($result)) { + $this->addError($object, $attribute, $result[0], $result[1]); + } + } + + /** + * Validates a given value. + * You may use this method to validate a value out of the context of a data model. + * @param mixed $value the data value to be validated. + * @param string $error the error message to be returned, if the validation fails. + * @return boolean whether the data is valid. + */ + public function validate($value, &$error = null) + { + $result = $this->validateValue($value); + if (empty($result)) { + return true; + } else { + list($message, $params) = $result; + $params['attribute'] = Yii::t('yii', 'the input value'); + $params['value'] = is_array($value) ? 'array()' : $value; + $error = Yii::$app->getI18n()->format($message, $params, Yii::$app->language); + return false; + } + } + + /** * Validates a value. * A validator class can implement this method to support data validation out of the context of a data model. * @param mixed $value the data value to be validated. - * @throws NotSupportedException if data validation without a model is not supported + * @return array|null the error message and the parameters to be inserted into the error message. + * Null should be returned if the data is valid. + * @throws NotSupportedException if the validator does not supporting data validation without a model */ - public function validateValue($value) + protected function validateValue($value) { throw new NotSupportedException(get_class($this) . ' does not support validateValue().'); } diff --git a/tests/unit/framework/validators/BooleanValidatorTest.php b/tests/unit/framework/validators/BooleanValidatorTest.php index 0f09daf..5ed0307 100644 --- a/tests/unit/framework/validators/BooleanValidatorTest.php +++ b/tests/unit/framework/validators/BooleanValidatorTest.php @@ -19,23 +19,23 @@ class BooleanValidatorTest extends TestCase public function testValidateValue() { $val = new BooleanValidator; - $this->assertTrue($val->validateValue(true)); - $this->assertTrue($val->validateValue(false)); - $this->assertTrue($val->validateValue('0')); - $this->assertTrue($val->validateValue('1')); - $this->assertFalse($val->validateValue(null)); - $this->assertFalse($val->validateValue([])); + $this->assertTrue($val->validate(true)); + $this->assertTrue($val->validate(false)); + $this->assertTrue($val->validate('0')); + $this->assertTrue($val->validate('1')); + $this->assertFalse($val->validate(null)); + $this->assertFalse($val->validate([])); $val->strict = true; - $this->assertTrue($val->validateValue('0')); - $this->assertTrue($val->validateValue('1')); - $this->assertFalse($val->validateValue(true)); - $this->assertFalse($val->validateValue(false)); + $this->assertTrue($val->validate('0')); + $this->assertTrue($val->validate('1')); + $this->assertFalse($val->validate(true)); + $this->assertFalse($val->validate(false)); $val->trueValue = true; $val->falseValue = false; - $this->assertFalse($val->validateValue('0')); - $this->assertFalse($val->validateValue([])); - $this->assertTrue($val->validateValue(true)); - $this->assertTrue($val->validateValue(false)); + $this->assertFalse($val->validate('0')); + $this->assertFalse($val->validate([])); + $this->assertTrue($val->validate(true)); + $this->assertTrue($val->validate(false)); } public function testValidateAttributeAndError() diff --git a/tests/unit/framework/validators/CompareValidatorTest.php b/tests/unit/framework/validators/CompareValidatorTest.php index d1bdf34..1e18faf 100644 --- a/tests/unit/framework/validators/CompareValidatorTest.php +++ b/tests/unit/framework/validators/CompareValidatorTest.php @@ -20,7 +20,7 @@ class CompareValidatorTest extends TestCase { $this->setExpectedException('yii\base\InvalidConfigException'); $val = new CompareValidator; - $val->validateValue('val'); + $val->validate('val'); } public function testValidateValue() @@ -28,14 +28,14 @@ class CompareValidatorTest extends TestCase $value = 18449; // default config $val = new CompareValidator(['compareValue' => $value]); - $this->assertTrue($val->validateValue($value)); - $this->assertTrue($val->validateValue((string)$value)); - $this->assertFalse($val->validateValue($value + 1)); + $this->assertTrue($val->validate($value)); + $this->assertTrue($val->validate((string)$value)); + $this->assertFalse($val->validate($value + 1)); foreach ($this->getOperationTestData($value) as $op => $tests) { $val = new CompareValidator(['compareValue' => $value]); $val->operator = $op; foreach ($tests as $test) { - $this->assertEquals($test[1], $val->validateValue($test[0])); + $this->assertEquals($test[1], $val->validate($test[0])); } } } @@ -172,4 +172,4 @@ class CompareValidatorTest extends TestCase } $this->fail('InvalidConfigException expected none received'); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/DateValidatorTest.php b/tests/unit/framework/validators/DateValidatorTest.php index d5d0f93..98a114f 100644 --- a/tests/unit/framework/validators/DateValidatorTest.php +++ b/tests/unit/framework/validators/DateValidatorTest.php @@ -25,17 +25,17 @@ class DateValidatorTest extends TestCase public function testValidateValue() { $val = new DateValidator; - $this->assertFalse($val->validateValue('3232-32-32')); - $this->assertTrue($val->validateValue('2013-09-13')); - $this->assertFalse($val->validateValue('31.7.2013')); - $this->assertFalse($val->validateValue('31-7-2013')); - $this->assertFalse($val->validateValue(time())); + $this->assertFalse($val->validate('3232-32-32')); + $this->assertTrue($val->validate('2013-09-13')); + $this->assertFalse($val->validate('31.7.2013')); + $this->assertFalse($val->validate('31-7-2013')); + $this->assertFalse($val->validate(time())); $val->format = 'U'; - $this->assertTrue($val->validateValue(time())); + $this->assertTrue($val->validate(time())); $val->format = 'd.m.Y'; - $this->assertTrue($val->validateValue('31.7.2013')); + $this->assertTrue($val->validate('31.7.2013')); $val->format = 'Y-m-!d H:i:s'; - $this->assertTrue($val->validateValue('2009-02-15 15:16:17')); + $this->assertTrue($val->validate('2009-02-15 15:16:17')); } public function testValidateAttribute() @@ -68,4 +68,4 @@ class DateValidatorTest extends TestCase $this->assertTrue($model->hasErrors('attr_date')); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/EmailValidatorTest.php b/tests/unit/framework/validators/EmailValidatorTest.php index 770914d..3fcd2dd 100644 --- a/tests/unit/framework/validators/EmailValidatorTest.php +++ b/tests/unit/framework/validators/EmailValidatorTest.php @@ -21,29 +21,29 @@ class EmailValidatorTest extends TestCase { $validator = new EmailValidator(); - $this->assertTrue($validator->validateValue('sam@rmcreative.ru')); - $this->assertTrue($validator->validateValue('5011@gmail.com')); - $this->assertFalse($validator->validateValue('rmcreative.ru')); - $this->assertFalse($validator->validateValue('Carsten Brandt ')); - $this->assertFalse($validator->validateValue('"Carsten Brandt" ')); - $this->assertFalse($validator->validateValue('')); - $this->assertFalse($validator->validateValue('info@örtliches.de')); - $this->assertFalse($validator->validateValue('sam@рмкреатиф.ru')); + $this->assertTrue($validator->validate('sam@rmcreative.ru')); + $this->assertTrue($validator->validate('5011@gmail.com')); + $this->assertFalse($validator->validate('rmcreative.ru')); + $this->assertFalse($validator->validate('Carsten Brandt ')); + $this->assertFalse($validator->validate('"Carsten Brandt" ')); + $this->assertFalse($validator->validate('')); + $this->assertFalse($validator->validate('info@örtliches.de')); + $this->assertFalse($validator->validate('sam@рмкреатиф.ru')); $validator->allowName = true; - $this->assertTrue($validator->validateValue('sam@rmcreative.ru')); - $this->assertTrue($validator->validateValue('5011@gmail.com')); - $this->assertFalse($validator->validateValue('rmcreative.ru')); - $this->assertTrue($validator->validateValue('Carsten Brandt ')); - $this->assertTrue($validator->validateValue('"Carsten Brandt" ')); - $this->assertTrue($validator->validateValue('')); - $this->assertFalse($validator->validateValue('info@örtliches.de')); - $this->assertFalse($validator->validateValue('sam@рмкреатиф.ru')); - $this->assertFalse($validator->validateValue('Informtation info@oertliches.de')); - $this->assertTrue($validator->validateValue('test@example.com')); - $this->assertTrue($validator->validateValue('John Smith ')); - $this->assertFalse($validator->validateValue('John Smith ')); + $this->assertTrue($validator->validate('sam@rmcreative.ru')); + $this->assertTrue($validator->validate('5011@gmail.com')); + $this->assertFalse($validator->validate('rmcreative.ru')); + $this->assertTrue($validator->validate('Carsten Brandt ')); + $this->assertTrue($validator->validate('"Carsten Brandt" ')); + $this->assertTrue($validator->validate('')); + $this->assertFalse($validator->validate('info@örtliches.de')); + $this->assertFalse($validator->validate('sam@рмкреатиф.ru')); + $this->assertFalse($validator->validate('Informtation info@oertliches.de')); + $this->assertTrue($validator->validate('test@example.com')); + $this->assertTrue($validator->validate('John Smith ')); + $this->assertFalse($validator->validate('John Smith ')); } public function testValidateValueIdn() @@ -55,33 +55,33 @@ class EmailValidatorTest extends TestCase $validator = new EmailValidator(); $validator->enableIDN = true; - $this->assertTrue($validator->validateValue('5011@example.com')); - $this->assertTrue($validator->validateValue('example@äüößìà.de')); - $this->assertTrue($validator->validateValue('example@xn--zcack7ayc9a.de')); - $this->assertTrue($validator->validateValue('info@örtliches.de')); - $this->assertTrue($validator->validateValue('sam@рмкреатиф.ru')); - $this->assertTrue($validator->validateValue('sam@rmcreative.ru')); - $this->assertTrue($validator->validateValue('5011@gmail.com')); - $this->assertFalse($validator->validateValue('rmcreative.ru')); - $this->assertFalse($validator->validateValue('Carsten Brandt ')); - $this->assertFalse($validator->validateValue('"Carsten Brandt" ')); - $this->assertFalse($validator->validateValue('')); + $this->assertTrue($validator->validate('5011@example.com')); + $this->assertTrue($validator->validate('example@äüößìà.de')); + $this->assertTrue($validator->validate('example@xn--zcack7ayc9a.de')); + $this->assertTrue($validator->validate('info@örtliches.de')); + $this->assertTrue($validator->validate('sam@рмкреатиф.ru')); + $this->assertTrue($validator->validate('sam@rmcreative.ru')); + $this->assertTrue($validator->validate('5011@gmail.com')); + $this->assertFalse($validator->validate('rmcreative.ru')); + $this->assertFalse($validator->validate('Carsten Brandt ')); + $this->assertFalse($validator->validate('"Carsten Brandt" ')); + $this->assertFalse($validator->validate('')); $validator->allowName = true; - $this->assertTrue($validator->validateValue('info@örtliches.de')); - $this->assertTrue($validator->validateValue('Informtation ')); - $this->assertFalse($validator->validateValue('Informtation info@örtliches.de')); - $this->assertTrue($validator->validateValue('sam@рмкреатиф.ru')); - $this->assertTrue($validator->validateValue('sam@rmcreative.ru')); - $this->assertTrue($validator->validateValue('5011@gmail.com')); - $this->assertFalse($validator->validateValue('rmcreative.ru')); - $this->assertTrue($validator->validateValue('Carsten Brandt ')); - $this->assertTrue($validator->validateValue('"Carsten Brandt" ')); - $this->assertTrue($validator->validateValue('')); - $this->assertTrue($validator->validateValue('test@example.com')); - $this->assertTrue($validator->validateValue('John Smith ')); - $this->assertFalse($validator->validateValue('John Smith ')); + $this->assertTrue($validator->validate('info@örtliches.de')); + $this->assertTrue($validator->validate('Informtation ')); + $this->assertFalse($validator->validate('Informtation info@örtliches.de')); + $this->assertTrue($validator->validate('sam@рмкреатиф.ru')); + $this->assertTrue($validator->validate('sam@rmcreative.ru')); + $this->assertTrue($validator->validate('5011@gmail.com')); + $this->assertFalse($validator->validate('rmcreative.ru')); + $this->assertTrue($validator->validate('Carsten Brandt ')); + $this->assertTrue($validator->validate('"Carsten Brandt" ')); + $this->assertTrue($validator->validate('')); + $this->assertTrue($validator->validate('test@example.com')); + $this->assertTrue($validator->validate('John Smith ')); + $this->assertFalse($validator->validate('John Smith ')); } public function testValidateValueMx() @@ -89,12 +89,12 @@ class EmailValidatorTest extends TestCase $validator = new EmailValidator(); $validator->checkDNS = true; - $this->assertTrue($validator->validateValue('5011@gmail.com')); + $this->assertTrue($validator->validate('5011@gmail.com')); $validator->checkDNS = false; - $this->assertTrue($validator->validateValue('test@nonexistingsubdomain.example.com')); + $this->assertTrue($validator->validate('test@nonexistingsubdomain.example.com')); $validator->checkDNS = true; - $this->assertFalse($validator->validateValue('test@nonexistingsubdomain.example.com')); + $this->assertFalse($validator->validate('test@nonexistingsubdomain.example.com')); } public function testValidateAttribute() diff --git a/tests/unit/framework/validators/ExistValidatorTest.php b/tests/unit/framework/validators/ExistValidatorTest.php index a0f0328..45ff5d5 100644 --- a/tests/unit/framework/validators/ExistValidatorTest.php +++ b/tests/unit/framework/validators/ExistValidatorTest.php @@ -26,7 +26,7 @@ class ExistValidatorTest extends DatabaseTestCase { try { $val = new ExistValidator(); - $result = $val->validateValue('ref'); + $result = $val->validate('ref'); $this->fail('Exception should have been thrown at this time'); } catch (Exception $e) { $this->assertInstanceOf('yii\base\InvalidConfigException', $e); @@ -35,7 +35,7 @@ class ExistValidatorTest extends DatabaseTestCase // combine to save the time creating a new db-fixture set (likely ~5 sec) try { $val = new ExistValidator(['className' => ValidatorTestMainModel::className()]); - $val->validateValue('ref'); + $val->validate('ref'); $this->fail('Exception should have been thrown at this time'); } catch (Exception $e) { $this->assertInstanceOf('yii\base\InvalidConfigException', $e); @@ -46,10 +46,10 @@ class ExistValidatorTest extends DatabaseTestCase public function testValidateValue() { $val = new ExistValidator(['className' => ValidatorTestRefModel::className(), 'attributeName' => 'id']); - $this->assertTrue($val->validateValue(2)); - $this->assertTrue($val->validateValue(5)); - $this->assertFalse($val->validateValue(99)); - $this->assertFalse($val->validateValue(['1'])); + $this->assertTrue($val->validate(2)); + $this->assertTrue($val->validate(5)); + $this->assertFalse($val->validate(99)); + $this->assertFalse($val->validate(['1'])); } public function testValidateAttribute() @@ -92,4 +92,4 @@ class ExistValidatorTest extends DatabaseTestCase $val->validateAttribute($m, 'test_val'); $this->assertTrue($m->hasErrors('test_val')); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/NumberValidatorTest.php b/tests/unit/framework/validators/NumberValidatorTest.php index 4e9897e..a7fa195 100644 --- a/tests/unit/framework/validators/NumberValidatorTest.php +++ b/tests/unit/framework/validators/NumberValidatorTest.php @@ -29,84 +29,84 @@ class NumberValidatorTest extends TestCase public function testValidateValueSimple() { $val = new NumberValidator(); - $this->assertTrue($val->validateValue(20)); - $this->assertTrue($val->validateValue(0)); - $this->assertTrue($val->validateValue(-20)); - $this->assertTrue($val->validateValue('20')); - $this->assertTrue($val->validateValue(25.45)); - $this->assertFalse($val->validateValue('25,45')); - $this->assertFalse($val->validateValue('12:45')); + $this->assertTrue($val->validate(20)); + $this->assertTrue($val->validate(0)); + $this->assertTrue($val->validate(-20)); + $this->assertTrue($val->validate('20')); + $this->assertTrue($val->validate(25.45)); + $this->assertFalse($val->validate('25,45')); + $this->assertFalse($val->validate('12:45')); $val = new NumberValidator(['integerOnly' => true]); - $this->assertTrue($val->validateValue(20)); - $this->assertTrue($val->validateValue(0)); - $this->assertFalse($val->validateValue(25.45)); - $this->assertTrue($val->validateValue('20')); - $this->assertFalse($val->validateValue('25,45')); - $this->assertTrue($val->validateValue('020')); - $this->assertTrue($val->validateValue(0x14)); - $this->assertFalse($val->validateValue('0x14')); // todo check this + $this->assertTrue($val->validate(20)); + $this->assertTrue($val->validate(0)); + $this->assertFalse($val->validate(25.45)); + $this->assertTrue($val->validate('20')); + $this->assertFalse($val->validate('25,45')); + $this->assertTrue($val->validate('020')); + $this->assertTrue($val->validate(0x14)); + $this->assertFalse($val->validate('0x14')); // todo check this } public function testValidateValueAdvanced() { $val = new NumberValidator(); - $this->assertTrue($val->validateValue('-1.23')); // signed float - $this->assertTrue($val->validateValue('-4.423e-12')); // signed float + exponent - $this->assertTrue($val->validateValue('12E3')); // integer + exponent - $this->assertFalse($val->validateValue('e12')); // just exponent - $this->assertFalse($val->validateValue('-e3')); - $this->assertFalse($val->validateValue('-4.534-e-12')); // 'signed' exponent - $this->assertFalse($val->validateValue('12.23^4')); // expression instead of value + $this->assertTrue($val->validate('-1.23')); // signed float + $this->assertTrue($val->validate('-4.423e-12')); // signed float + exponent + $this->assertTrue($val->validate('12E3')); // integer + exponent + $this->assertFalse($val->validate('e12')); // just exponent + $this->assertFalse($val->validate('-e3')); + $this->assertFalse($val->validate('-4.534-e-12')); // 'signed' exponent + $this->assertFalse($val->validate('12.23^4')); // expression instead of value $val = new NumberValidator(['integerOnly' => true]); - $this->assertFalse($val->validateValue('-1.23')); - $this->assertFalse($val->validateValue('-4.423e-12')); - $this->assertFalse($val->validateValue('12E3')); - $this->assertFalse($val->validateValue('e12')); - $this->assertFalse($val->validateValue('-e3')); - $this->assertFalse($val->validateValue('-4.534-e-12')); - $this->assertFalse($val->validateValue('12.23^4')); + $this->assertFalse($val->validate('-1.23')); + $this->assertFalse($val->validate('-4.423e-12')); + $this->assertFalse($val->validate('12E3')); + $this->assertFalse($val->validate('e12')); + $this->assertFalse($val->validate('-e3')); + $this->assertFalse($val->validate('-4.534-e-12')); + $this->assertFalse($val->validate('12.23^4')); } public function testValidateValueMin() { $val = new NumberValidator(['min' => 1]); - $this->assertTrue($val->validateValue(1)); - $this->assertFalse($val->validateValue(-1)); - $this->assertFalse($val->validateValue('22e-12')); - $this->assertTrue($val->validateValue(PHP_INT_MAX + 1)); + $this->assertTrue($val->validate(1)); + $this->assertFalse($val->validate(-1)); + $this->assertFalse($val->validate('22e-12')); + $this->assertTrue($val->validate(PHP_INT_MAX + 1)); $val = new NumberValidator(['min' => 1], ['integerOnly' => true]); - $this->assertTrue($val->validateValue(1)); - $this->assertFalse($val->validateValue(-1)); - $this->assertFalse($val->validateValue('22e-12')); - $this->assertTrue($val->validateValue(PHP_INT_MAX + 1)); + $this->assertTrue($val->validate(1)); + $this->assertFalse($val->validate(-1)); + $this->assertFalse($val->validate('22e-12')); + $this->assertTrue($val->validate(PHP_INT_MAX + 1)); } public function testValidateValueMax() { $val = new NumberValidator(['max' => 1.25]); - $this->assertTrue($val->validateValue(1)); - $this->assertFalse($val->validateValue(1.5)); - $this->assertTrue($val->validateValue('22e-12')); - $this->assertTrue($val->validateValue('125e-2')); + $this->assertTrue($val->validate(1)); + $this->assertFalse($val->validate(1.5)); + $this->assertTrue($val->validate('22e-12')); + $this->assertTrue($val->validate('125e-2')); $val = new NumberValidator(['max' => 1.25, 'integerOnly' => true]); - $this->assertTrue($val->validateValue(1)); - $this->assertFalse($val->validateValue(1.5)); - $this->assertFalse($val->validateValue('22e-12')); - $this->assertFalse($val->validateValue('125e-2')); + $this->assertTrue($val->validate(1)); + $this->assertFalse($val->validate(1.5)); + $this->assertFalse($val->validate('22e-12')); + $this->assertFalse($val->validate('125e-2')); } public function testValidateValueRange() { $val = new NumberValidator(['min' => -10, 'max' => 20]); - $this->assertTrue($val->validateValue(0)); - $this->assertTrue($val->validateValue(-10)); - $this->assertFalse($val->validateValue(-11)); - $this->assertFalse($val->validateValue(21)); + $this->assertTrue($val->validate(0)); + $this->assertTrue($val->validate(-10)); + $this->assertFalse($val->validate(-11)); + $this->assertFalse($val->validate(21)); $val = new NumberValidator(['min' => -10, 'max' => 20, 'integerOnly' => true]); - $this->assertTrue($val->validateValue(0)); - $this->assertFalse($val->validateValue(-11)); - $this->assertFalse($val->validateValue(22)); - $this->assertFalse($val->validateValue('20e-1')); + $this->assertTrue($val->validate(0)); + $this->assertFalse($val->validate(-11)); + $this->assertFalse($val->validate(22)); + $this->assertFalse($val->validate('20e-1')); } public function testValidateAttribute() @@ -163,4 +163,4 @@ class NumberValidatorTest extends TestCase $msgs = $model->getErrors('attr_number'); $this->assertSame('attr_number is to small.', $msgs[0]); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/RangeValidatorTest.php b/tests/unit/framework/validators/RangeValidatorTest.php index 345951c..583eeca 100644 --- a/tests/unit/framework/validators/RangeValidatorTest.php +++ b/tests/unit/framework/validators/RangeValidatorTest.php @@ -30,36 +30,36 @@ class RangeValidatorTest extends TestCase public function testValidateValue() { $val = new RangeValidator(['range' => range(1, 10, 1)]); - $this->assertTrue($val->validateValue(1)); - $this->assertFalse($val->validateValue(0)); - $this->assertFalse($val->validateValue(11)); - $this->assertFalse($val->validateValue(5.5)); - $this->assertTrue($val->validateValue(10)); - $this->assertTrue($val->validateValue("10")); - $this->assertTrue($val->validateValue("5")); + $this->assertTrue($val->validate(1)); + $this->assertFalse($val->validate(0)); + $this->assertFalse($val->validate(11)); + $this->assertFalse($val->validate(5.5)); + $this->assertTrue($val->validate(10)); + $this->assertTrue($val->validate("10")); + $this->assertTrue($val->validate("5")); } public function testValidateValueStrict() { $val = new RangeValidator(['range' => range(1, 10, 1), 'strict' => true]); - $this->assertTrue($val->validateValue(1)); - $this->assertTrue($val->validateValue(5)); - $this->assertTrue($val->validateValue(10)); - $this->assertFalse($val->validateValue("1")); - $this->assertFalse($val->validateValue("10")); - $this->assertFalse($val->validateValue("5.5")); + $this->assertTrue($val->validate(1)); + $this->assertTrue($val->validate(5)); + $this->assertTrue($val->validate(10)); + $this->assertFalse($val->validate("1")); + $this->assertFalse($val->validate("10")); + $this->assertFalse($val->validate("5.5")); } public function testValidateValueNot() { $val = new RangeValidator(['range' => range(1, 10, 1), 'not' => true]); - $this->assertFalse($val->validateValue(1)); - $this->assertTrue($val->validateValue(0)); - $this->assertTrue($val->validateValue(11)); - $this->assertTrue($val->validateValue(5.5)); - $this->assertFalse($val->validateValue(10)); - $this->assertFalse($val->validateValue("10")); - $this->assertFalse($val->validateValue("5")); + $this->assertFalse($val->validate(1)); + $this->assertTrue($val->validate(0)); + $this->assertTrue($val->validate(11)); + $this->assertTrue($val->validate(5.5)); + $this->assertFalse($val->validate(10)); + $this->assertFalse($val->validate("10")); + $this->assertFalse($val->validate("5")); } public function testValidateAttribute() @@ -73,4 +73,4 @@ class RangeValidatorTest extends TestCase $err = $m->getErrors('attr_r2'); $this->assertTrue(stripos($err[0], 'attr_r2') !== false); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/RegularExpressionValidatorTest.php b/tests/unit/framework/validators/RegularExpressionValidatorTest.php index e5b33b2..0dd8b04 100644 --- a/tests/unit/framework/validators/RegularExpressionValidatorTest.php +++ b/tests/unit/framework/validators/RegularExpressionValidatorTest.php @@ -18,13 +18,13 @@ class RegularExpressionValidatorTest extends TestCase public function testValidateValue() { $val = new RegularExpressionValidator(['pattern' => '/^[a-zA-Z0-9](\.)?([^\/]*)$/m']); - $this->assertTrue($val->validateValue('b.4')); - $this->assertFalse($val->validateValue('b./')); - $this->assertFalse($val->validateValue(['a', 'b'])); + $this->assertTrue($val->validate('b.4')); + $this->assertFalse($val->validate('b./')); + $this->assertFalse($val->validate(['a', 'b'])); $val->not = true; - $this->assertFalse($val->validateValue('b.4')); - $this->assertTrue($val->validateValue('b./')); - $this->assertFalse($val->validateValue(['a', 'b'])); + $this->assertFalse($val->validate('b.4')); + $this->assertTrue($val->validate('b./')); + $this->assertFalse($val->validate(['a', 'b'])); } public function testValidateAttribute() @@ -48,7 +48,7 @@ class RegularExpressionValidatorTest extends TestCase { $this->setExpectedException('yii\base\InvalidConfigException'); $val = new RegularExpressionValidator(); - $val->validateValue('abc'); + $val->validate('abc'); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/RequiredValidatorTest.php b/tests/unit/framework/validators/RequiredValidatorTest.php index 0708cc9..44102eb 100644 --- a/tests/unit/framework/validators/RequiredValidatorTest.php +++ b/tests/unit/framework/validators/RequiredValidatorTest.php @@ -17,26 +17,26 @@ class RequiredValidatorTest extends TestCase public function testValidateValueWithDefaults() { $val = new RequiredValidator(); - $this->assertFalse($val->validateValue(null)); - $this->assertFalse($val->validateValue([])); - $this->assertTrue($val->validateValue('not empty')); - $this->assertTrue($val->validateValue(['with', 'elements'])); + $this->assertFalse($val->validate(null)); + $this->assertFalse($val->validate([])); + $this->assertTrue($val->validate('not empty')); + $this->assertTrue($val->validate(['with', 'elements'])); } public function testValidateValueWithValue() { $val = new RequiredValidator(['requiredValue' => 55]); - $this->assertTrue($val->validateValue(55)); - $this->assertTrue($val->validateValue("55")); - $this->assertTrue($val->validateValue("0x37")); - $this->assertFalse($val->validateValue("should fail")); - $this->assertTrue($val->validateValue(true)); + $this->assertTrue($val->validate(55)); + $this->assertTrue($val->validate("55")); + $this->assertTrue($val->validate("0x37")); + $this->assertFalse($val->validate("should fail")); + $this->assertTrue($val->validate(true)); $val->strict = true; - $this->assertTrue($val->validateValue(55)); - $this->assertFalse($val->validateValue("55")); - $this->assertFalse($val->validateValue("0x37")); - $this->assertFalse($val->validateValue("should fail")); - $this->assertFalse($val->validateValue(true)); + $this->assertTrue($val->validate(55)); + $this->assertFalse($val->validate("55")); + $this->assertFalse($val->validate("0x37")); + $this->assertFalse($val->validate("should fail")); + $this->assertFalse($val->validate(true)); } public function testValidateAttribute() @@ -57,4 +57,4 @@ class RequiredValidatorTest extends TestCase $val->validateAttribute($m, 'attr_val'); $this->assertFalse($m->hasErrors('attr_val')); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/StringValidatorTest.php b/tests/unit/framework/validators/StringValidatorTest.php index a99567b..50ebbbb 100644 --- a/tests/unit/framework/validators/StringValidatorTest.php +++ b/tests/unit/framework/validators/StringValidatorTest.php @@ -18,47 +18,47 @@ class StringValidatorTest extends TestCase public function testValidateValue() { $val = new StringValidator(); - $this->assertFalse($val->validateValue(['not a string'])); - $this->assertTrue($val->validateValue('Just some string')); + $this->assertFalse($val->validate(['not a string'])); + $this->assertTrue($val->validate('Just some string')); } public function testValidateValueLength() { $val = new StringValidator(['length' => 25]); - $this->assertTrue($val->validateValue(str_repeat('x', 25))); - $this->assertTrue($val->validateValue(str_repeat('€', 25))); - $this->assertFalse($val->validateValue(str_repeat('x', 125))); - $this->assertFalse($val->validateValue('')); + $this->assertTrue($val->validate(str_repeat('x', 25))); + $this->assertTrue($val->validate(str_repeat('€', 25))); + $this->assertFalse($val->validate(str_repeat('x', 125))); + $this->assertFalse($val->validate('')); $val = new StringValidator(['length' => [25]]); - $this->assertTrue($val->validateValue(str_repeat('x', 25))); - $this->assertTrue($val->validateValue(str_repeat('x', 1250))); - $this->assertFalse($val->validateValue(str_repeat('Ä', 24))); - $this->assertFalse($val->validateValue('')); + $this->assertTrue($val->validate(str_repeat('x', 25))); + $this->assertTrue($val->validate(str_repeat('x', 1250))); + $this->assertFalse($val->validate(str_repeat('Ä', 24))); + $this->assertFalse($val->validate('')); $val = new StringValidator(['length' => [10, 20]]); - $this->assertTrue($val->validateValue(str_repeat('x', 15))); - $this->assertTrue($val->validateValue(str_repeat('x', 10))); - $this->assertTrue($val->validateValue(str_repeat('x', 20))); - $this->assertFalse($val->validateValue(str_repeat('x', 5))); - $this->assertFalse($val->validateValue(str_repeat('x', 25))); - $this->assertFalse($val->validateValue('')); + $this->assertTrue($val->validate(str_repeat('x', 15))); + $this->assertTrue($val->validate(str_repeat('x', 10))); + $this->assertTrue($val->validate(str_repeat('x', 20))); + $this->assertFalse($val->validate(str_repeat('x', 5))); + $this->assertFalse($val->validate(str_repeat('x', 25))); + $this->assertFalse($val->validate('')); // make sure min/max are overridden $val = new StringValidator(['length' => [10, 20], 'min' => 25, 'max' => 35]); - $this->assertTrue($val->validateValue(str_repeat('x', 15))); - $this->assertFalse($val->validateValue(str_repeat('x', 30))); + $this->assertTrue($val->validate(str_repeat('x', 15))); + $this->assertFalse($val->validate(str_repeat('x', 30))); } public function testValidateValueMinMax() { $val = new StringValidator(['min' => 10]); - $this->assertTrue($val->validateValue(str_repeat('x', 10))); - $this->assertFalse($val->validateValue('xxxx')); + $this->assertTrue($val->validate(str_repeat('x', 10))); + $this->assertFalse($val->validate('xxxx')); $val = new StringValidator(['max' => 10]); - $this->assertTrue($val->validateValue('xxxx')); - $this->assertFalse($val->validateValue(str_repeat('y', 20))); + $this->assertTrue($val->validate('xxxx')); + $this->assertFalse($val->validate(str_repeat('y', 20))); $val = new StringValidator(['min' => 10, 'max' => 20]); - $this->assertTrue($val->validateValue(str_repeat('y', 15))); - $this->assertFalse($val->validateValue('abc')); - $this->assertFalse($val->validateValue(str_repeat('b', 25))); + $this->assertTrue($val->validate(str_repeat('y', 15))); + $this->assertFalse($val->validate('abc')); + $this->assertFalse($val->validate(str_repeat('b', 25))); } public function testValidateAttribute() @@ -113,4 +113,4 @@ class StringValidatorTest extends TestCase $errorMsg = $model->getErrors('attr_string'); $this->assertEquals('attr_string to short. Min is 5', $errorMsg[0]); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/validators/UrlValidatorTest.php b/tests/unit/framework/validators/UrlValidatorTest.php index 50baa9c..5b2cada 100644 --- a/tests/unit/framework/validators/UrlValidatorTest.php +++ b/tests/unit/framework/validators/UrlValidatorTest.php @@ -18,28 +18,28 @@ class UrlValidatorTest extends TestCase public function testValidateValue() { $val = new UrlValidator; - $this->assertFalse($val->validateValue('google.de')); - $this->assertTrue($val->validateValue('http://google.de')); - $this->assertTrue($val->validateValue('https://google.de')); - $this->assertFalse($val->validateValue('htp://yiiframework.com')); - $this->assertTrue($val->validateValue('https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8' + $this->assertFalse($val->validate('google.de')); + $this->assertTrue($val->validate('http://google.de')); + $this->assertTrue($val->validate('https://google.de')); + $this->assertFalse($val->validate('htp://yiiframework.com')); + $this->assertTrue($val->validate('https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8' .'&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr')); - $this->assertFalse($val->validateValue('ftp://ftp.ruhr-uni-bochum.de/')); - $this->assertFalse($val->validateValue('http://invalid,domain')); - $this->assertFalse($val->validateValue('http://äüö?=!"§$%&/()=}][{³²€.edu')); + $this->assertFalse($val->validate('ftp://ftp.ruhr-uni-bochum.de/')); + $this->assertFalse($val->validate('http://invalid,domain')); + $this->assertFalse($val->validate('http://äüö?=!"§$%&/()=}][{³²€.edu')); } public function testValidateValueWithDefaultScheme() { $val = new UrlValidator(['defaultScheme' => 'https']); - $this->assertTrue($val->validateValue('yiiframework.com')); - $this->assertTrue($val->validateValue('http://yiiframework.com')); + $this->assertTrue($val->validate('yiiframework.com')); + $this->assertTrue($val->validate('http://yiiframework.com')); } public function testValidateValueWithoutScheme() { $val = new UrlValidator(['pattern' => '/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i']); - $this->assertTrue($val->validateValue('yiiframework.com')); + $this->assertTrue($val->validate('yiiframework.com')); } public function testValidateWithCustomScheme() @@ -48,13 +48,13 @@ class UrlValidatorTest extends TestCase 'validSchemes' => ['http', 'https', 'ftp', 'ftps'], 'defaultScheme' => 'http', ]); - $this->assertTrue($val->validateValue('ftp://ftp.ruhr-uni-bochum.de/')); - $this->assertTrue($val->validateValue('google.de')); - $this->assertTrue($val->validateValue('http://google.de')); - $this->assertTrue($val->validateValue('https://google.de')); - $this->assertFalse($val->validateValue('htp://yiiframework.com')); + $this->assertTrue($val->validate('ftp://ftp.ruhr-uni-bochum.de/')); + $this->assertTrue($val->validate('google.de')); + $this->assertTrue($val->validate('http://google.de')); + $this->assertTrue($val->validate('https://google.de')); + $this->assertFalse($val->validate('htp://yiiframework.com')); // relative urls not supported - $this->assertFalse($val->validateValue('//yiiframework.com')); + $this->assertFalse($val->validate('//yiiframework.com')); } public function testValidateWithIdn() @@ -66,16 +66,16 @@ class UrlValidatorTest extends TestCase $val = new UrlValidator([ 'enableIDN' => true, ]); - $this->assertTrue($val->validateValue('http://äüößìà.de')); + $this->assertTrue($val->validate('http://äüößìà.de')); // converted via http://mct.verisign-grs.com/convertServlet - $this->assertTrue($val->validateValue('http://xn--zcack7ayc9a.de')); + $this->assertTrue($val->validate('http://xn--zcack7ayc9a.de')); } public function testValidateLength() { $url = 'http://' . str_pad('base', 2000, 'url') . '.de'; $val = new UrlValidator; - $this->assertFalse($val->validateValue($url)); + $this->assertFalse($val->validate($url)); } public function testValidateAttributeAndError() diff --git a/tests/unit/framework/validators/ValidatorTest.php b/tests/unit/framework/validators/ValidatorTest.php index 5e5385b..e52ccdb 100644 --- a/tests/unit/framework/validators/ValidatorTest.php +++ b/tests/unit/framework/validators/ValidatorTest.php @@ -63,7 +63,7 @@ class ValidatorTest extends TestCase { $val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2']]); $model = $this->getTestModel(); - $val->validate($model); + $val->validateAttributes($model); $this->assertTrue($val->isAttributeValidated('attr_runMe1')); $this->assertTrue($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_skip')); @@ -73,7 +73,7 @@ class ValidatorTest extends TestCase { $val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2']]); $model = $this->getTestModel(); - $val->validate($model, ['attr_runMe1']); + $val->validateAttributes($model, ['attr_runMe1']); $this->assertTrue($val->isAttributeValidated('attr_runMe1')); $this->assertFalse($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_skip')); @@ -83,11 +83,11 @@ class ValidatorTest extends TestCase { $val = new TestValidator(); $model = $this->getTestModel(); - $val->validate($model, ['attr_runMe1']); + $val->validateAttributes($model, ['attr_runMe1']); $this->assertFalse($val->isAttributeValidated('attr_runMe1')); $this->assertFalse($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_skip')); - $val->validate($model); + $val->validateAttributes($model); $this->assertFalse($val->isAttributeValidated('attr_runMe1')); $this->assertFalse($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_skip')); @@ -97,27 +97,27 @@ class ValidatorTest extends TestCase { $val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2'], 'skipOnError' => false]); $model = $this->getTestModel(); - $val->validate($model); + $val->validateAttributes($model); $this->assertTrue($val->isAttributeValidated('attr_runMe1')); $this->assertTrue($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_skip')); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe2')); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); - $val->validate($model, ['attr_runMe2']); + $val->validateAttributes($model, ['attr_runMe2']); $this->assertEquals(2, $val->countAttributeValidations('attr_runMe2')); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); $this->assertEquals(0, $val->countAttributeValidations('attr_skip')); $val = new TestValidator(['attributes' => ['attr_runMe1', 'attr_runMe2'], 'skipOnError' => true]); $model = $this->getTestModel(); $val->enableErrorOnValidateAttribute(); - $val->validate($model); + $val->validateAttributes($model); $this->assertTrue($val->isAttributeValidated('attr_runMe1')); $this->assertTrue($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_skip')); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); $this->assertEquals(0, $val->countAttributeValidations('attr_skip')); - $val->validate($model, ['attr_runMe2']); + $val->validateAttributes($model, ['attr_runMe2']); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe2')); $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); $this->assertEquals(0, $val->countAttributeValidations('attr_skip')); @@ -135,13 +135,13 @@ class ValidatorTest extends TestCase 'skipOnEmpty' => true, ]); $model = $this->getTestModel(['attr_empty1' => '', 'attr_emtpy2' => ' ']); - $val->validate($model); + $val->validateAttributes($model); $this->assertTrue($val->isAttributeValidated('attr_runMe1')); $this->assertTrue($val->isAttributeValidated('attr_runMe2')); $this->assertFalse($val->isAttributeValidated('attr_empty1')); $this->assertFalse($val->isAttributeValidated('attr_empty2')); $model->attr_empty1 = 'not empty anymore'; - $val->validate($model); + $val->validateAttributes($model); $this->assertTrue($val->isAttributeValidated('attr_empty1')); $this->assertFalse($val->isAttributeValidated('attr_empty2')); $val = new TestValidator([ @@ -154,7 +154,7 @@ class ValidatorTest extends TestCase 'skipOnEmpty' => false, ]); $model = $this->getTestModel(['attr_empty1' => '', 'attr_emtpy2' => ' ']); - $val->validate($model); + $val->validateAttributes($model); $this->assertTrue($val->isAttributeValidated('attr_runMe1')); $this->assertTrue($val->isAttributeValidated('attr_runMe2')); $this->assertTrue($val->isAttributeValidated('attr_empty1')); @@ -188,7 +188,7 @@ class ValidatorTest extends TestCase TestValidator::className() . ' does not support validateValue().' ); $val = new TestValidator(); - $val->validateValue('abc'); + $val->validate('abc'); } public function testClientValidateAttribute()