diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index 9f5304cb178..080ee3de0b1 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -36,6 +36,7 @@ - Moved `method` parameter in `Phalcon\Mvc\Model\Manager::getBelongsToRecords()` to the last position. [#14115](https://github.com/phalcon/cphalcon/issues/14115) - Moved `method` parameter in `Phalcon\Mvc\Model\Manager::getHasOneRecords()` to the last position. [#14115](https://github.com/phalcon/cphalcon/issues/14115) - Moved `method` parameter in `Phalcon\Mvc\Model\Manager::getHasManyRecords()` to the last position. [#14115](https://github.com/phalcon/cphalcon/issues/14115) +- Validator messages were moved into each validator. [#13208](https://github.com/phalcon/cphalcon/issues/13208) - `Phalcon\Paginator\Repository::getProperty()` now uses `Phalcon\Helper\Arr::get()`. - Renamed `Phalcon\Collection` to `Phalcon\Collection\Collection`. [#14154](https://github.com/phalcon/cphalcon/pull/14154) - Refactored `Phalcon\Collection\Collection` to allow conditional key case sensitivity. [#14154](https://github.com/phalcon/cphalcon/pull/14154) diff --git a/phalcon/Validation.zep b/phalcon/Validation.zep index 59482e28317..349771d7725 100644 --- a/phalcon/Validation.zep +++ b/phalcon/Validation.zep @@ -28,7 +28,6 @@ class Validation extends Injectable implements ValidationInterface { protected combinedFieldsValidators; protected data { get }; - protected defaultMessages; protected entity; protected filters = []; protected labels = []; @@ -55,8 +54,6 @@ class Validation extends Injectable implements ValidationInterface } ); - this->setDefaultMessages(); - /** * Check for an 'initialize' method */ @@ -132,20 +129,6 @@ class Validation extends Injectable implements ValidationInterface return this; } - /** - * Get default message for validator type - */ - public function getDefaultMessage(string! type) -> string - { - var defaultMessage; - - if !fetch defaultMessage, this->defaultMessages[type] { - return ""; - } - - return defaultMessage; - } - /** * Returns the bound entity */ @@ -344,46 +327,6 @@ class Validation extends Injectable implements ValidationInterface return this; } - /** - * Adds default messages to validators - */ - public function setDefaultMessages(array messages = []) -> array - { - var defaultMessages; - - let defaultMessages = [ - "Alnum": "Field :field must contain only letters and numbers", - "Alpha": "Field :field must contain only letters", - "Between": "Field :field must be within the range of :min to :max", - "Confirmation": "Field :field must be the same as :with", - "Digit": "Field :field must be numeric", - "Email": "Field :field must be an email address", - "ExclusionIn": "Field :field must not be a part of list: :domain", - "FileEmpty": "Field :field must not be empty", - "FileIniSize": "File :field exceeds the maximum file size", - "FileMaxResolution": "File :field must not exceed :max resolution", - "FileMinResolution": "File :field must be at least :min resolution", - "FileSize": "File :field exceeds the size of :max", - "FileType": "File :field must be of type: :types", - "FileValid": "Field :field is not valid", - "Identical": "Field :field does not have the expected value", - "InclusionIn": "Field :field must be a part of list: :domain", - "Numericality": "Field :field does not have a valid numeric format", - "PresenceOf": "Field :field is required", - "Regex": "Field :field does not match the required format", - "TooLong": "Field :field must not exceed :max characters long", - "TooShort": "Field :field must be at least :min characters long", - "Uniqueness": "Field :field must be unique", - "Url": "Field :field must be a url", - "CreditCard": "Field :field is not valid for a credit card number", - "Date": "Field :field is not a valid date" - ]; - - let this->defaultMessages = array_merge(defaultMessages, messages); - - return this->defaultMessages; - } - /** * Sets the bound entity * diff --git a/phalcon/Validation/Validator.zep b/phalcon/Validation/Validator.zep index 366216dbfb7..59d88cffd5e 100644 --- a/phalcon/Validation/Validator.zep +++ b/phalcon/Validation/Validator.zep @@ -10,6 +10,9 @@ namespace Phalcon\Validation; +use Phalcon\Collection; +use Phalcon\Helper\Arr; +use Phalcon\Messages\Message; use Phalcon\Validation; use Phalcon\Validation\Exception; use Phalcon\Validation\ValidatorInterface; @@ -21,6 +24,20 @@ use Phalcon\Validation\ValidatorInterface; */ abstract class Validator implements ValidatorInterface { + /** + * Message template + * + * @var string|null + */ + protected template; + + /** + * Message templates + * + * @var array + */ + protected templates = []; + protected options; /** @@ -28,9 +45,90 @@ abstract class Validator implements ValidatorInterface */ public function __construct(array! options = []) -> void { + var template; + + let template = current(Arr::whiteList(options, ["template", "message", 0])); + + if typeof template == "array" { + this->setTemplates(template); + } elseif typeof template == "string" { + this->setTemplate(template); + } + + if (template) { + unset options["template"]; + unset options["message"]; + unset options[0]; + } + let this->options = options; } + /** + * Get the template message + * + * @return string + * @throw InvalidArgumentException When the field does not exists + */ + public function getTemplate(string! field = null) -> string + { + // there is a template in field + if field !== null && isset this->templates[field] { + return this->templates[field]; + } + + // there is a custom template + if (this->template) { + return this->template; + } + + // default template message + return "The field :field is not valid for " . get_class(this); + } + + /** + * Get templates collection object + * + * @return array + */ + public function getTemplates() -> array + { + return this->templates; + } + + /** + * Clear current templates and set new from an array, + * + * @return Validator + */ + public function setTemplates(array! templates) -> + { + var field, template; + + let this->templates = []; + + for field, template in templates { + let field = (string) field; + let template = (string) template; + + let this->templates[field] = template; + } + + return this; + } + + /** + * Set a new template message + * + * @return Validator + */ + public function setTemplate(string! template) -> + { + let this->template = template; + + return this; + } + /** * Returns an option in the validator's options * Returns null if the option hasn't set @@ -114,22 +212,36 @@ abstract class Validator implements ValidatorInterface } /** - * Prepares a validation message. - */ - protected function prepareMessage( validation, string! field, string! type, string! option = "message") -> var + * Create a default message by factory + * + * @return Message + * + * @throw Exception + */ + public function messageFactory( validation, var field, array! replacements = []) -> { - var message; - - let message = this->getOption(option); - - if typeof message == "array" { - let message = message[field]; - } - - if empty message { - let message = validation->getDefaultMessage(type); + var singleField; + + if (is_array(field)) { + let singleField = implode(", ", field); + } elseif (is_string(field)) { + let singleField = field; + } else { + throw new Exception("The field can not be printed"); } - return message; + let replacements = array_merge( + [ + ":field" : this->prepareLabel(validation, singleField) + ], + replacements + ); + + return new Message( + strtr(this->getTemplate(singleField), replacements), + field, + get_class(this), + this->prepareCode(singleField) + ); } } diff --git a/phalcon/Validation/Validator/Alnum.zep b/phalcon/Validation/Validator/Alnum.zep index a668da55cb6..a8a176825ff 100644 --- a/phalcon/Validation/Validator/Alnum.zep +++ b/phalcon/Validation/Validator/Alnum.zep @@ -10,7 +10,6 @@ namespace Phalcon\Validation\Validator; -use Phalcon\Messages\Message; use Phalcon\Validation; use Phalcon\Validation\Validator; @@ -52,31 +51,20 @@ use Phalcon\Validation\Validator; */ class Alnum extends Validator { + protected template = "Field :field must contain only letters and numbers"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, message, label, replacePairs, code; + var value; let value = validation->getValue(field); if !ctype_alnum(value) { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Alnum"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Alnum", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Alpha.zep b/phalcon/Validation/Validator/Alpha.zep index 51a359be6a5..40699c026c0 100644 --- a/phalcon/Validation/Validator/Alpha.zep +++ b/phalcon/Validation/Validator/Alpha.zep @@ -52,31 +52,20 @@ use Phalcon\Validation\Validator; */ class Alpha extends Validator { + protected template = "Field :field must contain only letters"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, message, label, replacePairs, code; + var value; let value = validation->getValue(field); if preg_match("/[^[:alpha:]]/imu", value) { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Alpha"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Alpha", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Between.zep b/phalcon/Validation/Validator/Between.zep index 0ebd1614ac9..642c03122f2 100644 --- a/phalcon/Validation/Validator/Between.zep +++ b/phalcon/Validation/Validator/Between.zep @@ -63,16 +63,18 @@ use Phalcon\Validation\Validator; */ class Between extends Validator { + protected template = "Field :field must be within the range of :min to :max"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, minimum, maximum, message, label, replacePairs, code; + var value, minimum, maximum, replacePairs; let value = validation->getValue(field), - minimum = this->getOption("minimum"), - maximum = this->getOption("maximum"); + minimum = this->getOption("minimum"), + maximum = this->getOption("maximum"); if typeof minimum == "array" { let minimum = minimum[field]; @@ -83,23 +85,13 @@ class Between extends Validator } if value < minimum || value > maximum { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Between"), - code = this->prepareCode(field); - let replacePairs = [ - ":field": label, ":min": minimum, ":max": maximum ]; validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Between", - code - ) + this->messageFactory(validation, field, replacePairs) ); return false; diff --git a/phalcon/Validation/Validator/Callback.zep b/phalcon/Validation/Validator/Callback.zep index 400fddfb482..34eb6cbe9d8 100644 --- a/phalcon/Validation/Validator/Callback.zep +++ b/phalcon/Validation/Validator/Callback.zep @@ -62,12 +62,14 @@ use Phalcon\Validation\Validator; */ class Callback extends Validator { + protected template = "Field :field must match the callback function"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var message, label, replacePairs, code, callback, returnedValue, data; + var callback, returnedValue, data; let callback = this->getOption("callback"); @@ -82,27 +84,8 @@ class Callback extends Validator if typeof returnedValue == "boolean" { if !returnedValue { - let label = this->prepareLabel(validation, field); - - let message = this->prepareMessage( - validation, - field, - "Callback" - ); - - let code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Callback", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Confirmation.zep b/phalcon/Validation/Validator/Confirmation.zep index 8a7daf69093..1350b44ba89 100644 --- a/phalcon/Validation/Validator/Confirmation.zep +++ b/phalcon/Validation/Validator/Confirmation.zep @@ -58,13 +58,14 @@ use Phalcon\Validation\Validator; */ class Confirmation extends Validator { + protected template = "Field :field must be the same as :with"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var fieldWith, value, valueWith, message, label, labelWith, - replacePairs, code; + var fieldWith, value, valueWith, labelWith, replacePairs; let fieldWith = this->getOption("with"); @@ -76,16 +77,6 @@ class Confirmation extends Validator valueWith = validation->getValue(fieldWith); if !this->compare(value, valueWith) { - let label = this->prepareLabel(validation, field); - - let message = this->prepareMessage( - validation, - field, - "Confirmation" - ); - - let code = this->prepareCode(field); - let labelWith = this->getOption("labelWith"); if typeof labelWith == "array" { @@ -97,17 +88,11 @@ class Confirmation extends Validator } let replacePairs = [ - ":field": label, ":with": labelWith ]; validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Confirmation", - code - ) + this->messageFactory(validation, field, replacePairs) ); return false; diff --git a/phalcon/Validation/Validator/CreditCard.zep b/phalcon/Validation/Validator/CreditCard.zep index 5a4b41f3d29..f6b2ca5f8d8 100644 --- a/phalcon/Validation/Validator/CreditCard.zep +++ b/phalcon/Validation/Validator/CreditCard.zep @@ -52,33 +52,22 @@ use Phalcon\Validation\Validator; */ class CreditCard extends Validator { + protected template = "Field :field is not valid for a credit card number"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var message, label, replacePairs, value, valid, code; + var value, valid; let value = validation->getValue(field); let valid = this->verifyByLuhnAlgorithm(value); if !valid { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "CreditCard"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "CreditCard", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Date.zep b/phalcon/Validation/Validator/Date.zep index 54b9ffb405b..32418e1dc52 100644 --- a/phalcon/Validation/Validator/Date.zep +++ b/phalcon/Validation/Validator/Date.zep @@ -57,12 +57,14 @@ use Phalcon\Validation\Validator; */ class Date extends Validator { + protected template = "Field :field is not a valid date"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, format, label, message, replacePairs, code; + var value, format; let value = validation->getValue(field); let format = this->getOption("format"); @@ -76,21 +78,8 @@ class Date extends Validator } if !this->checkDate(value, format) { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Date"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Date", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Digit.zep b/phalcon/Validation/Validator/Digit.zep index 5c5bb617a50..0441fcf9bdb 100644 --- a/phalcon/Validation/Validator/Digit.zep +++ b/phalcon/Validation/Validator/Digit.zep @@ -52,12 +52,14 @@ use Phalcon\Validation\Validator; */ class Digit extends Validator { + protected template = "Field :field must be numeric"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, message, label, replacePairs, code; + var value; let value = validation->getValue(field); @@ -65,21 +67,8 @@ class Digit extends Validator return true; } - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Digit"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Digit", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Email.zep b/phalcon/Validation/Validator/Email.zep index 1ff27440fb4..62049cb38af 100644 --- a/phalcon/Validation/Validator/Email.zep +++ b/phalcon/Validation/Validator/Email.zep @@ -52,31 +52,19 @@ use Phalcon\Validation\Validator; */ class Email extends Validator { + protected template = "Field :field must be an email address"; /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, message, label, replacePairs, code; + var value; let value = validation->getValue(field); if !filter_var(value, FILTER_VALIDATE_EMAIL) { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Email"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Email", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/ExclusionIn.zep b/phalcon/Validation/Validator/ExclusionIn.zep index 94ee1cdf3db..d5674432e14 100644 --- a/phalcon/Validation/Validator/ExclusionIn.zep +++ b/phalcon/Validation/Validator/ExclusionIn.zep @@ -64,13 +64,14 @@ use Phalcon\Validation\Exception; */ class ExclusionIn extends Validator { + protected template = "Field :field must not be a part of list: :domain"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, domain, message, label, replacePairs, strict, fieldDomain, - code; + var value, domain, replacePairs, strict, fieldDomain; let value = validation->getValue(field); @@ -107,28 +108,12 @@ class ExclusionIn extends Validator * Check if the value is contained by the array */ if in_array(value, domain, strict) { - let label = this->prepareLabel(validation, field); - - let message = this->prepareMessage( - validation, - field, - "ExclusionIn" - ); - - let code = this->prepareCode(field); - let replacePairs = [ - ":field": label, ":domain": join(", ", domain) ]; validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "ExclusionIn", - code - ) + this->messageFactory(validation, field, replacePairs) ); return false; diff --git a/phalcon/Validation/Validator/File.zep b/phalcon/Validation/Validator/File.zep index 5f9c1122a40..0ab71e2d303 100644 --- a/phalcon/Validation/Validator/File.zep +++ b/phalcon/Validation/Validator/File.zep @@ -12,7 +12,14 @@ namespace Phalcon\Validation\Validator; use Phalcon\Messages\Message; use Phalcon\Validation; -use Phalcon\Validation\Validator; +use Phalcon\Validation\ValidatorComposite; +use Phalcon\Validation\Validator\File\MimeType; +use Phalcon\Validation\Validator\File\Resolution\Equal as EqualResolution; +use Phalcon\Validation\Validator\File\Resolution\Max as MaxResolution; +use Phalcon\Validation\Validator\File\Resolution\Min as MinResolution; +use Phalcon\Validation\Validator\File\Size\Equal as EqualFileSize; +use Phalcon\Validation\Validator\File\Size\Max as MaxFileSize; +use Phalcon\Validation\Validator\File\Size\Min as MinFileSize; /** * Phalcon\Validation\Validator\File @@ -30,14 +37,14 @@ use Phalcon\Validation\Validator; * new FileValidator( * [ * "maxSize" => "2M", - * "messageSize" => ":field exceeds the max filesize (:max)", + * "messageSize" => ":field exceeds the max filesize (:size)", * "allowedTypes" => [ * "image/jpeg", * "image/png", * ], * "messageType" => "Allowed file types are :types", * "maxResolution" => "800x600", - * "messageMaxResolution" => "Max resolution of :field is :max", + * "messageMaxResolution" => "Max resolution of :field is :resolution", * ] * ) * ); @@ -83,295 +90,171 @@ use Phalcon\Validation\Validator; * ); * */ -class File extends Validator +class File extends ValidatorComposite { /** - * Check on empty + * Constructor */ - public function isAllowEmpty( validation, string! field) -> bool + public function __construct(array! options = []) { - var value; - - let value = validation->getValue(field); - - return empty value || isset value["error"] && value["error"] === UPLOAD_ERR_NO_FILE; - } - - /** - * Executes the validation - */ - public function validate( validation, var field) -> bool - { - var value, message, label, replacePairs, types, byteUnits, unit, - maxSize, matches, bytes, mime, tmp, width, height, minResolution, - maxResolution, minWidth, maxWidth, minHeight, maxHeight, fieldTypes, - code, minResolutionArray, maxResolutionArray; - - let value = validation->getValue(field), - label = this->prepareLabel(validation, field), - code = this->prepareCode(field); - - // Upload is larger than PHP allowed size (post_max_size or upload_max_filesize) - if _SERVER["REQUEST_METHOD"] == "POST" && empty _POST && empty _FILES && _SERVER["CONTENT_LENGTH"] > 0 || isset value["error"] && value["error"] === UPLOAD_ERR_INI_SIZE { - let message = this->prepareMessage( - validation, - field, - "FileIniSize", - "messageIniSize" - ); - - let replacePairs = [ - ":field": label - ]; - - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileIniSize", - code - ) - ); - - return false; - } - - if !isset value["error"] || !isset value["tmp_name"] || value["error"] !== UPLOAD_ERR_OK || !is_uploaded_file(value["tmp_name"]) { - let message = this->prepareMessage( - validation, - field, - "FileEmpty", - "messageEmpty" - ); - - let replacePairs = [":field": label]; - - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileEmpty", - code - ) - ); - - return false; - } - - if !isset value["name"] || !isset value["type"] || !isset value["size"] { - let message = this->prepareMessage( - validation, - field, - "FileValid", - "messageValid" - ); - - let replacePairs = [ - ":field": label - ]; - - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileValid", - code - ) - ); - - return false; - } - - if this->hasOption("maxSize") { - let byteUnits = [ - "B": 0, - "K": 10, - "M": 20, - "G": 30, - "T": 40, - "KB": 10, - "MB": 20, - "GB": 30, - "TB": 40 - ]; - - let maxSize = this->getOption("maxSize"), - matches = null, - unit = "B"; + var included = null, key, message = null, validator, value; + + // create individual validators + for key, value in options { + // min filesize + if strtolower(key) === "minSize" { + // get custom message + if isset options["messageMinSize"] { + let message = options["messageMinSize"]; + } - if typeof maxSize == "array" { - let maxSize = maxSize[field]; - } + // get included option + if isset options["includedMinSize"] { + let included = options["includedMinSize"]; + } - preg_match( - "/^([0-9]+(?:\\.[0-9]+)?)(" . implode("|", array_keys(byteUnits)) . ")?$/Di", - maxSize, - matches - ); + let validator = new MinFileSize( + [ + "size" : value, + "message" : message, + "included" : included + ] + ); - if isset matches[2] { - let unit = matches[2]; + unset options["messageMinSize"]; + unset options["includedMinSize"]; } - let bytes = floatval(matches[1]) * pow(2, byteUnits[unit]); - - if floatval(value["size"]) > floatval(bytes) { - let message = this->prepareMessage( - validation, - field, - "FileSize", - "messageSize" - ); + // max filesize + elseif strtolower(key) === "maxSize" { + // get custom message + if isset options["messageSize"] { + let message = options["messageSize"]; + } - let replacePairs = [ - ":field": label, - ":max": maxSize - ]; + // get included option + if isset options["includedSize"] { + let included = options["includedSize"]; + } - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileSize", - code - ) + let validator = new MaxFileSize( + [ + "size" : value, + "message" : message, + "included" : included + ] ); - return false; + unset options["maxSize"]; + unset options["messageSize"]; + unset options["includedSize"]; } - } - if this->hasOption("allowedTypes") { - let types = this->getOption("allowedTypes"); - - if fetch fieldTypes, types[field] { - let types = fieldTypes; - } + // equal filesize + elseif strtolower(key) === "equalSize" { + // get custom message + if isset options["messageEqualSize"] { + let message = options["messageEqualSize"]; + } - if unlikely typeof types != "array" { - throw new \Phalcon\Validation\Exception( - "Option 'allowedTypes' must be an array" + let validator = new EqualFileSize( + [ + "size" : value, + "message" : message + ] ); - } - if function_exists("finfo_open") { - let tmp = finfo_open(FILEINFO_MIME_TYPE), - mime = finfo_file(tmp, value["tmp_name"]); - - finfo_close(tmp); - } else { - let mime = value["type"]; + unset options["equalSize"]; + unset options["messageEqualSize"]; } - if !in_array(mime, types) { - let message = this->prepareMessage( - validation, - field, - "FileType", - "messageType" - ); - - let replacePairs = [ - ":field": label, - ":types": join(", ", types) - ]; + // mime types + elseif strtolower(key) === "allowedTypes" { + if isset options["messageType"] { + let message = options["messageType"]; + } - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileType", - code - ) + let validator = new MimeType( + [ + "types" : value, + "message" : message + ] ); - return false; + unset options["allowedTypes"]; + unset options["messageType"]; } - } - - if this->hasOption("minResolution") || this->hasOption("maxResolution") { - let tmp = getimagesize(value["tmp_name"]), - width = tmp[0], - height = tmp[1]; - if this->hasOption("minResolution") { - let minResolution = this->getOption("minResolution"); - - if typeof minResolution == "array" { - let minResolution = minResolution[field]; + // max resolution + elseif strtolower(key) === "maxResolution" { + if isset options["messageMaxResolution"] { + let message = options["messageMaxResolution"]; } - let minResolutionArray = explode("x", minResolution), - minWidth = minResolutionArray[0], - minHeight = minResolutionArray[1]; - } else { - let minWidth = 1, - minHeight = 1; - } - - if width < minWidth || height < minHeight { - let message = this->prepareMessage( - validation, - field, - "FileMinResolution", - "messageMinResolution" - ); - - let replacePairs = [ - ":field": label, - ":min": minResolution - ]; + // get included option + if isset options["includedMaxResolution"] { + let included = options["includedMaxResolution"]; + } - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileMinResolution", - code - ) + let validator = new MaxResolution( + [ + "resolution" : value, + "included" : included, + "message" : message + ] ); - return false; + unset options["maxResolution"]; + unset options["includedMaxResolution"]; + unset options["messageMaxResolution"]; } - if this->hasOption("maxResolution") { - let maxResolution = this->getOption("maxResolution"); + // min resolution + elseif strtolower(key) === "minResolution" { + if isset options["messageMinResolution"] { + let message = options["messageMinResolution"]; + } - if typeof maxResolution == "array" { - let maxResolution = maxResolution[field]; + // get included option + if isset options["includedMinResolution"] { + let included = options["includedMinResolution"]; } - let maxResolutionArray = explode("x", maxResolution), - maxWidth = maxResolutionArray[0], - maxHeight = maxResolutionArray[1]; + let validator = new MinResolution( + [ + "resolution" : value, + "included" : included, + "message" : message + ] + ); - if width > maxWidth || height > maxHeight { - let message = this->prepareMessage( - validation, - field, - "FileMaxResolution", - "messageMaxResolution" - ); + unset options["minResolution"]; + unset options["includedMinResolution"]; + unset options["messageMinResolution"]; + } - let replacePairs = [ - ":field": label, - ":max": maxResolution - ]; + // equal resolution + elseif strtolower(key) === "equalResolution" { + if isset options["messageEqualResolution"] { + let message = options["messageEqualResolution"]; + } - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "FileMaxResolution", - code - ) - ); + let validator = new EqualResolution( + [ + "resolution" : value, + "message" : message + ] + ); - return false; - } + unset options["equalResolution"]; + unset options["messageEqualResolution"]; + } else { + continue; } + + let this->validators[] = validator; } - return true; + parent::__construct(options); } } diff --git a/phalcon/Validation/Validator/File/FileAbstract.zep b/phalcon/Validation/Validator/File/FileAbstract.zep new file mode 100644 index 00000000000..016c76400da --- /dev/null +++ b/phalcon/Validation/Validator/File/FileAbstract.zep @@ -0,0 +1,241 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator; + +/** + * Phalcon\Validation\Validator\File + * + * Checks if a value has a correct file + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Size; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Size( + * [ + * "maxSize" => "2M", + * "messageSize" => ":field exceeds the max filesize (:size)", + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new FileValidator( + * [ + * "maxSize" => [ + * "file" => "2M", + * "anotherFile" => "4M", + * ], + * "messageSize" => [ + * "file" => "file exceeds the max filesize 2M", + * "anotherFile" => "anotherFile exceeds the max filesize 4M", + * ], + * ] + * ) + * ); + * + */ +abstract class FileAbstract extends Validator +{ + /** + * Empty is empty + */ + protected messageFileEmpty = "Field :field must not be empty" { get, set }; + + /** + * File exceeed the file size setted in PHP configuration + */ + protected messageIniSize = "File :field exceeds the maximum file size" { get, set }; + + /** + * File is not valid + */ + protected messageValid = "Field :field is not valid" { get, set }; + + /** + * Check on empty + * + * @param Valiation $validation + * @param mixed $field + * @return bool + */ + public function isAllowEmpty( validation, string! field) -> bool + { + var value; + + let value = validation->getValue(field); + + return empty value || isset value["error"] && value["error"] === UPLOAD_ERR_NO_FILE; + } + + /** + * Check upload + * + * @param Valiation $validation + * @param mixed $field + * @return bool + */ + public function checkUpload( validation, var field) -> bool + { + return this->checkUploadMaxSize(validation, field) && + this->checkUploadIsEmpty(validation, field) && + this->checkUploadIsValid(validation, field); + } + + /** + * Check if uploaded file is larger than PHP allowed size + * + * @param Valiation $validation + * @param mixed $field + * @return boolean + */ + public function checkUploadMaxSize( validation, var field) -> bool + { + var label, replacePairs, value; + + let value = validation->getValue(field); + + // Upload is larger than PHP allowed size (post_max_size or upload_max_filesize) + if _SERVER["REQUEST_METHOD"] == "POST" && + empty _POST && + empty _FILES && + _SERVER["CONTENT_LENGTH"] > 0 || + isset value["error"] && value["error"] === UPLOAD_ERR_INI_SIZE + { + let label = this->prepareLabel(validation, field), + replacePairs = [ + ":field": label + ]; + + validation->appendMessage( + new Message( + strtr(this->getMessageIniSize(), replacePairs), + field, + get_class(this), + this->prepareCode(field) + ) + ); + + return false; + } + + return true; + } + + /** + * Check if upload is empty + * + * @param Valiation $validation + * @param mixed $field + * @return boolean + */ + public function checkUploadIsEmpty( validation, var field) -> bool + { + var label, replacePairs, value; + + if !isset value["error"] || !isset value["tmp_name"] || value["error"] !== UPLOAD_ERR_OK || !is_uploaded_file(value["tmp_name"]) { + let label = this->prepareLabel(validation, field), + replacePairs = [ + ":field": label + ]; + + validation->appendMessage( + new Message( + strtr(this->getMessageFileEmpty(), replacePairs), + field, + get_class(this), + this->prepareCode(field) + ) + ); + + return false; + } + + return true; + } + + /** + * Check if upload is valid + * + * @param Valiation $validation + * @param mixed $field + * @return boolean + */ + public function checkUploadIsValid( validation, var field) -> bool + { + var label, replacePairs, value; + + if !isset value["name"] || !isset value["type"] || !isset value["size"] { + let label = this->prepareLabel(validation, field), + replacePairs = [ + ":field": label + ]; + + validation->appendMessage( + new Message( + strtr(this->getMessageValid(), replacePairs), + field, + get_class(this), + this->prepareCode(field) + ) + ); + + return false; + } + + return true; + } + + /** + * Convert a string like "2.5MB" in bytes + * + * @param string $size + * @return float + */ + public function getFileSizeInBytes(string! size) -> float + { + var byteUnits = [ + "B": 0, + "K": 10, + "M": 20, + "G": 30, + "T": 40, + "KB": 10, + "MB": 20, + "GB": 30, + "TB": 40 + ], unit = "B", matches = null; + + preg_match( + "/^([0-9]+(?:\\.[0-9]+)?)(" . implode("|", array_keys(byteUnits)) . ")?$/Di", + size, + matches + ); + + if isset matches[2] { + let unit = matches[2]; + } + + return floatval(matches[1]) * pow(2, byteUnits[unit]); + } +} diff --git a/phalcon/Validation/Validator/File/MimeType.zep b/phalcon/Validation/Validator/File/MimeType.zep new file mode 100644 index 00000000000..df6f53fd708 --- /dev/null +++ b/phalcon/Validation/Validator/File/MimeType.zep @@ -0,0 +1,124 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File + * + * Checks if a value has a correct file mime type + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\MimeType; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new MimeType( + * [ + * "types" => [ + * "image/jpeg", + * "image/png", + * ], + * "message" => "Allowed file types are :types" + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new MimeType( + * [ + * "types" => [ + * "file" => [ + * "image/jpeg", + * "image/png", + * ], + * "anotherFile" => [ + * "image/gif", + * "image/bmp", + * ], + * ], + * "message" => [ + * "file" => "Allowed file types are image/jpeg and image/png", + * "anotherFile" => "Allowed file types are image/gif and image/bmp", + * ] + * ] + * ) + * ); + * + */ +class MimeType extends FileAbstract +{ + protected template = "File :field must be of type: :types"; + + /** + * Executes the validation + * + * @param Valiation $validation + * @param mixed $field + * @return bool + */ + public function validate( validation, var field) -> bool + { + var fieldTypes, mime, replacePairs, tmp, types, value; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let value = validation->getValue(field); + + let types = this->getOption("types"); + + if fetch fieldTypes, types[field] { + let types = fieldTypes; + } + + if unlikely typeof types != "array" { + throw new \Phalcon\Validation\Exception( + "Option 'allowedTypes' must be an array" + ); + } + + if function_exists("finfo_open") { + let tmp = finfo_open(FILEINFO_MIME_TYPE), + mime = finfo_file(tmp, value["tmp_name"]); + + finfo_close(tmp); + } else { + let mime = value["type"]; + } + + if !in_array(mime, types) { + let replacePairs = [ + ":types": join(", ", types) + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/File/Resolution/Equal.zep b/phalcon/Validation/Validator/File/Resolution/Equal.zep new file mode 100644 index 00000000000..3605afd5328 --- /dev/null +++ b/phalcon/Validation/Validator/File/Resolution/Equal.zep @@ -0,0 +1,108 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File\Resolution; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File\Resolution\Equal + * + * Checks if a file has the rigth resolution + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Resolution\Equal; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Equal( + * [ + * "resolution" => "800x600", + * "message" => "The resolution of the field :field has to be equal :resolution", + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new Equal( + * [ + * "resolution" => [ + * "file" => "800x600", + * "anotherFile" => "1024x768", + * ], + * "message" => [ + * "file" => "Equal resolution of file has to be 800x600", + * "anotherFile" => "Equal resolution of file has to be 1024x768", + * ], + * ] + * ) + * ); + * + */ +class Equal extends FileAbstract +{ + protected template = "The resolution of the field :field has to be equal :resolution"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var height, equalHeight, equalWidth, resolution, resolutionArray, + tmp, value, width, replacePairs; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let tmp = getimagesize(value["tmp_name"]), + width = tmp[0], + height = tmp[1], + value = validation->getValue(field); + + let resolution = this->getOption("resolution"); + + if typeof resolution == "array" { + let resolution = resolution[field]; + } + + let resolutionArray = explode("x", resolution), + equalWidth = resolutionArray[0], + equalHeight = resolutionArray[1]; + + if typeof resolution == "array" { + let resolution = resolution[field]; + } + + if (width != equalWidth || height != equalHeight) { + let replacePairs = [ + ":resolution" : resolution + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/File/Resolution/Max.zep b/phalcon/Validation/Validator/File/Resolution/Max.zep new file mode 100644 index 00000000000..83a953ece01 --- /dev/null +++ b/phalcon/Validation/Validator/File/Resolution/Max.zep @@ -0,0 +1,127 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File\Resolution; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File\Resolution\Max + * + * Checks if a file has the rigth resolution + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Resolution\Max; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Max( + * [ + * "resolution" => "800x600", + * "message" => "Max resolution of :field is :resolution", + * "included" => true, + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new Max( + * [ + * "resolution" => [ + * "file" => "800x600", + * "anotherFile" => "1024x768", + * ], + * "included" => [ + * "file" => false, + * "anotherFile" => true, + * ], + * "message" => [ + * "file" => "Max resolution of file is 800x600", + * "anotherFile" => "Max resolution of file is 1024x768", + * ], + * ] + * ) + * ); + * + */ +class Max extends FileAbstract +{ + protected template = "File :field exceeds the maximum resolution of :resolution"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var height, maxHeight, maxWidth, resolution, resolutionArray, + tmp, value, width, replacePairs, included = false, result; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let tmp = getimagesize(value["tmp_name"]), + width = tmp[0], + height = tmp[1], + value = validation->getValue(field); + + let resolution = this->getOption("resolution"); + + if typeof resolution == "array" { + let resolution = resolution[field]; + } + + let resolutionArray = explode("x", resolution), + maxWidth = resolutionArray[0], + maxHeight = resolutionArray[1]; + + let included = this->getOption("included"); + + if typeof included == "array" { + let included = (bool) included[field]; + } else { + let included = (bool) included; + } + + if (included) { + let result = width >= maxWidth || height >= maxHeight; + } else { + let result = width > maxWidth || height > maxHeight; + } + + if typeof resolution == "array" { + let resolution = resolution[field]; + } + + if (result) { + let replacePairs = [ + ":resolution" : resolution + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/File/Resolution/Min.zep b/phalcon/Validation/Validator/File/Resolution/Min.zep new file mode 100644 index 00000000000..9eb631e7d8d --- /dev/null +++ b/phalcon/Validation/Validator/File/Resolution/Min.zep @@ -0,0 +1,127 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File\Resolution; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File\Resolution\Min + * + * Checks if a file has the rigth resolution + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Resolution\Min; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Min( + * [ + * "resolution" => "800x600", + * "message" => "Min resolution of :field is :resolution", + * "included" => true, + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new Min( + * [ + * "resolution" => [ + * "file" => "800x600", + * "anotherFile" => "1024x768", + * ], + * "included" => [ + * "file" => false, + * "anotherFile" => true, + * ], + * "message" => [ + * "file" => "Min resolution of file is 800x600", + * "anotherFile" => "Min resolution of file is 1024x768", + * ], + * ] + * ) + * ); + * + */ +class Min extends FileAbstract +{ + protected template = "File :field can not have the minimum resolution of :resolution"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var height, minHeight, minWidth, resolution, resolutionArray, + tmp, value, width, replacePairs, included = false, result; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let tmp = getimagesize(value["tmp_name"]), + width = tmp[0], + height = tmp[1], + value = validation->getValue(field); + + let resolution = this->getOption("resolution"); + + if typeof resolution == "array" { + let resolution = resolution[field]; + } + + let resolutionArray = explode("x", resolution), + minWidth = resolutionArray[0], + minHeight = resolutionArray[1]; + + let included = this->getOption("included"); + + if typeof included == "array" { + let included = (bool) included[field]; + } else { + let included = (bool) included; + } + + if (included) { + let result = width <= minWidth || height <= minHeight; + } else { + let result = width < minWidth || height < minHeight; + } + + if typeof resolution == "array" { + let resolution = resolution[field]; + } + + if (result) { + let replacePairs = [ + ":resolution" : resolution + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/File/Size/Equal.zep b/phalcon/Validation/Validator/File/Size/Equal.zep new file mode 100644 index 00000000000..a52e3fc0dd1 --- /dev/null +++ b/phalcon/Validation/Validator/File/Size/Equal.zep @@ -0,0 +1,104 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File\Size; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\Size\Equal; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File + * + * Checks if a value has a correct file + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Size; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Equal( + * [ + * "size" => "2M", + * "included" => true, + * "message" => ":field exceeds the equal filesize (:size)", + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new Equal( + * [ + * "size" => [ + * "file" => "2M", + * "anotherFile" => "4M", + * ], + * "included" => [ + * "file" => false, + * "anotherFile" => true, + * ], + * "message" => [ + * "file" => "file does not have the rigth filesize", + * "anotherFile" => "anotherFile wrong filesize (4MB)", + * ], + * ] + * ) + * ); + * + */ +class Equal extends FileAbstract +{ + protected template = "File :field does not have the exact :size filesize"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var bytes, fileSize, replacePairs, size, value; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let value = validation->getValue(field), + size = this->getOption("size"); + + if typeof size == "array" { + let size = size[field]; + } + + let bytes = round(this->getFileSizeInBytes(size), 6), + fileSize = round(floatval(value["size"]), 6); + + if bytes !== fileSize { + let replacePairs = [ + ":size" : size + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/File/Size/Max.zep b/phalcon/Validation/Validator/File/Size/Max.zep new file mode 100644 index 00000000000..a6214a5b39e --- /dev/null +++ b/phalcon/Validation/Validator/File/Size/Max.zep @@ -0,0 +1,118 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File\Size; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\Size\Max; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File + * + * Checks if a value has a correct file + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Size; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Max( + * [ + * "size" => "2M", + * "included" => true, + * "message" => ":field exceeds the max filesize (:size)", + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new Max( + * [ + * "size" => [ + * "file" => "2M", + * "anotherFile" => "4M", + * ], + * "included" => [ + * "file" => false, + * "anotherFile" => true, + * ], + * "message" => [ + * "file" => "file exceeds the max filesize 2M", + * "anotherFile" => "anotherFile exceeds the max filesize 4M", + * ], + * ] + * ) + * ); + * + */ +class Max extends FileAbstract +{ + protected template = "File :field exceeds the size of :size"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var bytes, fileSize, included = false, replacePairs, result, size, value; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let value = validation->getValue(field), + size = this->getOption("size"); + + if typeof size == "array" { + let size = size[field]; + } + + let bytes = round(this->getFileSizeInBytes(size), 6), + fileSize = round(floatval(value["size"]), 6); + + let included = this->getOption("included"); + + if typeof included == "array" { + let included = (bool) included[field]; + } else { + let included = (bool) included; + } + + if (included) { + let result = fileSize >= bytes; + } else { + let result = fileSize > bytes; + } + + if (result) { + let replacePairs = [ + ":size" : size + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/File/Size/Min.zep b/phalcon/Validation/Validator/File/Size/Min.zep new file mode 100644 index 00000000000..882c16f14ef --- /dev/null +++ b/phalcon/Validation/Validator/File/Size/Min.zep @@ -0,0 +1,118 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\File\Size; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator\File\Size\Min; +use Phalcon\Validation\Validator\File\FileAbstract; + +/** + * Phalcon\Validation\Validator\File + * + * Checks if a value has a correct file + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\File\Size; + * + * $validator = new Validation(); + * + * $validator->add( + * "file", + * new Min( + * [ + * "size" => "2M", + * "included" => true, + * "message" => ":field exceeds the min filesize (:size)", + * ] + * ) + * ); + * + * $validator->add( + * [ + * "file", + * "anotherFile", + * ], + * new Min( + * [ + * "size" => [ + * "file" => "2M", + * "anotherFile" => "4M", + * ], + * "included" => [ + * "file" => false, + * "anotherFile" => true, + * ], + * "message" => [ + * "file" => "file exceeds the min filesize 2M", + * "anotherFile" => "anotherFile exceeds the min filesize 4M", + * ], + * ] + * ) + * ); + * + */ +class Min extends FileAbstract +{ + protected template = "File :field can not have the minimum size of :size"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var bytes, fileSize, included = false, replacePairs, result, size, value; + + // Check file upload + if (this->checkUpload(validation, field) === false) { + return false; + } + + let value = validation->getValue(field), + size = this->getOption("size"); + + if typeof size == "array" { + let size = size[field]; + } + + let bytes = round(this->getFileSizeInBytes(size), 6), + fileSize = round(floatval(value["size"]), 6); + + let included = this->getOption("included"); + + if typeof included == "array" { + let included = (bool) included[field]; + } else { + let included = (bool) included; + } + + if (included) { + let result = fileSize >= bytes; + } else { + let result = fileSize > bytes; + } + + if (result) { + let replacePairs = [ + ":size" : size + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/Identical.zep b/phalcon/Validation/Validator/Identical.zep index e2003ba9d4f..11e31ca90b0 100644 --- a/phalcon/Validation/Validator/Identical.zep +++ b/phalcon/Validation/Validator/Identical.zep @@ -57,13 +57,14 @@ use Phalcon\Validation\Validator; */ class Identical extends Validator { + protected template = "Field :field does not have the expected value"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var message, label, value, accepted, code; - array replacePairs; + var value, accepted; bool valid; let value = validation->getValue(field); @@ -83,21 +84,8 @@ class Identical extends Validator } if !valid { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Identical"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Identical", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/InclusionIn.zep b/phalcon/Validation/Validator/InclusionIn.zep index 169bec53d54..b3bd01e0a35 100644 --- a/phalcon/Validation/Validator/InclusionIn.zep +++ b/phalcon/Validation/Validator/InclusionIn.zep @@ -58,13 +58,14 @@ use Phalcon\Validation\Exception; */ class InclusionIn extends Validator { + protected template = "Field :field must be a part of list: :domain"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, domain, message, label, replacePairs, strict, fieldDomain, - code; + var value, domain, replacePairs, strict, fieldDomain; let value = validation->getValue(field); @@ -101,28 +102,12 @@ class InclusionIn extends Validator * Check if the value is contained by the array */ if !in_array(value, domain, strict) { - let label = this->prepareLabel(validation, field); - - let message = this->prepareMessage( - validation, - field, - "InclusionIn" - ); - - let code = this->prepareCode(field); - let replacePairs = [ - ":field": label, ":domain": join(", ", domain) ]; validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "InclusionIn", - code - ) + this->messageFactory(validation, field, replacePairs) ); return false; diff --git a/phalcon/Validation/Validator/Ip.zep b/phalcon/Validation/Validator/Ip.zep index 434d7520fb4..db5aaf0528a 100644 --- a/phalcon/Validation/Validator/Ip.zep +++ b/phalcon/Validation/Validator/Ip.zep @@ -72,37 +72,17 @@ class Ip extends Validator const VERSION_4 = FILTER_FLAG_IPV4; const VERSION_6 = FILTER_FLAG_IPV6; + protected template = "Field :field must be a valid IP address"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, version, allowPrivate, allowReserved, allowEmpty, message, - label, replacePairs, options; - - let value = validation->getValue(field); - - let label = this->getOption("label"); - - if typeof label == "array" { - let label = label[field]; - } - - if empty label { - let label = validation->getLabel(field); - } + var value, version, allowPrivate, allowReserved, allowEmpty, options; - let message = this->getOption("message"); - - if typeof message == "array" { - let message = message[field]; - } - - if empty message { - let message = validation->getDefaultMessage("Ip"); - } - - let version = this->getOption("version", FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); + let value = validation->getValue(field), + version = this->getOption("version", FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); if typeof version == "array" { let version = version[field]; @@ -138,16 +118,8 @@ class Ip extends Validator ]; if !filter_var(value, FILTER_VALIDATE_IP, options) { - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Ip" - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Numericality.zep b/phalcon/Validation/Validator/Numericality.zep index b2d9e3fd268..4dcd27a219e 100644 --- a/phalcon/Validation/Validator/Numericality.zep +++ b/phalcon/Validation/Validator/Numericality.zep @@ -52,12 +52,14 @@ use Phalcon\Validation\Validator; */ class Numericality extends Validator { + protected template = "Field :field does not have a valid numeric format"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var code, label, message, replacePairs, value; + var value; string pattern; // Dump spaces in the string if we have any @@ -67,27 +69,8 @@ class Numericality extends Validator pattern = "/((^[-]?[0-9,]+(.[0-9]+)?$)|(^[-]?[0-9.]+(,[0-9]+)?$))/"; if !preg_match(pattern, value) { - let label = this->prepareLabel(validation, field); - - let message = this->prepareMessage( - validation, - field, - "Numericality" - ); - - let code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Numericality", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/PresenceOf.zep b/phalcon/Validation/Validator/PresenceOf.zep index 32a9abfee1c..225256079c9 100644 --- a/phalcon/Validation/Validator/PresenceOf.zep +++ b/phalcon/Validation/Validator/PresenceOf.zep @@ -52,31 +52,20 @@ use Phalcon\Validation\Validator; */ class PresenceOf extends Validator { + protected template = "Field :field is required"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var value, message, label, replacePairs, code; + var value; let value = validation->getValue(field); if value === null || value === "" { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "PresenceOf"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "PresenceOf", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Regex.zep b/phalcon/Validation/Validator/Regex.zep index e3c96b30b1c..eba06b02c3b 100644 --- a/phalcon/Validation/Validator/Regex.zep +++ b/phalcon/Validation/Validator/Regex.zep @@ -57,12 +57,14 @@ use Phalcon\Validation\Validator; */ class Regex extends Validator { + protected template = "Field :field does not match the required format"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var matches, message, value, label, replacePairs, code, pattern; + var matches, value, pattern; bool failed; // Regular expression is set in the option 'pattern' @@ -83,21 +85,8 @@ class Regex extends Validator } if failed { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Regex"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Regex", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/StringLength.zep b/phalcon/Validation/Validator/StringLength.zep index 4de437a3024..3ddae20d7ae 100644 --- a/phalcon/Validation/Validator/StringLength.zep +++ b/phalcon/Validation/Validator/StringLength.zep @@ -11,8 +11,10 @@ namespace Phalcon\Validation\Validator; use Phalcon\Messages\Message; -use Phalcon\Validation; use Phalcon\Validation\Validator; +use Phalcon\Validation\ValidatorComposite; +use Phalcon\Validation\Validator\StringLength\Max; +use Phalcon\Validation\Validator\StringLength\Min; use Phalcon\Validation\Exception; /** @@ -21,6 +23,7 @@ use Phalcon\Validation\Exception; * Validates that a string has the specified maximum and minimum constraints * The test is passed if for a string's length L, min<=L<=max, i.e. L must * be at least min, and at most max. + * Since Phalcon v4.0 this valitor works like a container * * * use Phalcon\Validation; @@ -32,10 +35,12 @@ use Phalcon\Validation\Exception; * "name_last", * new StringLength( * [ - * "max" => 50, - * "min" => 2, - * "messageMaximum" => "We don't like really long names", - * "messageMinimum" => "We want more than just their initials", + * "max" => 50, + * "min" => 2, + * "messageMaximum" => "We don't like really long names", + * "messageMinimum" => "We want more than just their initials", + * "includedMaximum" => true, + * "includedMinimum" => false, * ] * ) * ); @@ -62,113 +67,94 @@ use Phalcon\Validation\Exception; * "messageMinimum" => [ * "name_last" => "We don't like too short last names", * "name_first" => "We don't like too short first names", + * ], + * "includedMaximum" => [ + * "name_last" => false, + * "name_first" => true, + * ], + * "includedMinimum" => [ + * "name_last" => false, + * "name_first" => true, * ] * ] * ) * ); * */ -class StringLength extends Validator +class StringLength extends ValidatorComposite { /** - * Executes the validation + * Constructor */ - public function validate( validation, var field) -> bool + public function __construct(array! options = []) -> void { - var isSetMin, isSetMax, value, length, message, minimum, maximum, label, - replacePairs, code; - - // At least one of 'min' or 'max' must be set - let isSetMin = this->hasOption("min"), - isSetMax = this->hasOption("max"); - - if unlikely (!isSetMin && !isSetMax) { - throw new Exception("A minimum or maximum must be set"); - } - - let value = validation->getValue(field), - label = this->prepareLabel(validation, field), - code = this->prepareCode(field); - - // Check if mbstring is available to calculate the correct length - if function_exists("mb_strlen") { - let length = mb_strlen(value); - } else { - let length = strlen(value); - } - - /** - * Maximum length - */ - if isSetMax { - let maximum = this->getOption("max"); - - if typeof maximum == "array" { - let maximum = maximum[field]; - } - - if length > maximum { - let message = this->prepareMessage( - validation, - field, - "TooLong", - "messageMaximum" + var included = null, key, message = null, validator, value; + + // create individual validators + for key, value in options { + if strtolower(key) === "min" { + // get custom message + if isset options["message"] { + let message = options["message"]; + } elseif isset options["messageMinimum"] { + let message = options["messageMinimum"]; + } + + // get included option + if isset options["included"] { + let included = options["included"]; + } elseif isset options["includedMinimum"] { + let included = options["includedMinimum"]; + } + + let validator = new Min( + [ + "min" : value, + "message" : message, + "included" : included + ] ); - let replacePairs = [ - ":field": label, - ":max": maximum - ]; - - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "TooLong", - code - ) + unset options["min"]; + unset options["message"]; + unset options["messageMinimum"]; + unset options["included"]; + unset options["includedMinimum"]; + } elseif strtolower(key) === "max" { + // get custom message + if isset options["message"] { + let message = options["message"]; + } elseif isset options["messageMaximum"] { + let message = options["messageMaximum"]; + } + + // get included option + if isset options["included"] { + let included = options["included"]; + } elseif isset options["includedMaximum"] { + let included = options["includedMaximum"]; + } + + let validator = new Max( + [ + "max" : value, + "message" : message, + "included" : included + ] ); - return false; - } - } - - /** - * Minimum length - */ - if isSetMin { - let minimum = this->getOption("min"); - - if typeof minimum == "array" { - let minimum = minimum[field]; + unset options["max"]; + unset options["message"]; + unset options["messageMaximum"]; + unset options["included"]; + unset options["includedMaximum"]; + } else { + continue; } - if length < minimum { - let message = this->prepareMessage( - validation, - field, - "TooShort", - "messageMinimum" - ); - - let replacePairs = [ - ":field": label, - ":min": minimum - ]; - - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "TooShort", - code - ) - ); - - return false; - } + let this->validators[] = validator; } - return true; + parent::__construct(options); } } diff --git a/phalcon/Validation/Validator/StringLength/Max.zep b/phalcon/Validation/Validator/StringLength/Max.zep new file mode 100644 index 00000000000..6399c46ca91 --- /dev/null +++ b/phalcon/Validation/Validator/StringLength/Max.zep @@ -0,0 +1,120 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\StringLength; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator; +use Phalcon\Validation\Exception; + +/** + * Phalcon\Validation\Validator\StringLength + * + * Validates that a string has the specified maximum constraints + * The test is passed if for a string's length L, L<=max, i.e. L must + * be at most max. + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\StringLength\Max; + * + * $validator = new Validation(); + * + * $validation->add( + * "name_last", + * new Max( + * [ + * "max" => 50, + * "message" => "We don't like really long names", + * "included" => true + * ] + * ) + * ); + * + * $validation->add( + * [ + * "name_last", + * "name_first", + * ], + * new Max( + * [ + * "max" => [ + * "name_last" => 50, + * "name_first" => 40, + * ], + * "message" => [ + * "name_last" => "We don't like really long last names", + * "name_first" => "We don't like really long first names", + * ], + * "included" => [ + * "name_last" => false, + * "name_first" => true, + * ] + * ] + * ) + * ); + * + */ +class Max extends Validator +{ + protected template = "Field :field must not exceed :max characters long"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var value, length, maximum, replacePairs, included, result; + + let value = validation->getValue(field); + + // Check if mbstring is available to calculate the correct length + if function_exists("mb_strlen") { + let length = mb_strlen(value); + } else { + let length = strlen(value); + } + + let maximum = this->getOption("max"); + + if typeof maximum == "array" { + let maximum = maximum[field]; + } + + let included = this->getOption("included"); + + if typeof included == "array" { + let included = (bool) included[field]; + } else { + let included = (bool) included; + } + + if (included) { + let result = length >= maximum; + } else { + let result = length > maximum; + } + + if result { + let replacePairs = [ + ":max" : maximum + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/StringLength/Min.zep b/phalcon/Validation/Validator/StringLength/Min.zep new file mode 100644 index 00000000000..6f8dc196e10 --- /dev/null +++ b/phalcon/Validation/Validator/StringLength/Min.zep @@ -0,0 +1,120 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation\Validator\StringLength; + +use Phalcon\Messages\Message; +use Phalcon\Validation; +use Phalcon\Validation\Validator; +use Phalcon\Validation\Exception; + +/** + * Phalcon\Validation\Validator\StringLength + * + * Validates that a string has the specified minimum constraints + * The test is passed if for a string's length L, min<=L, i.e. L must + * be at least min. + * + * + * use Phalcon\Validation; + * use Phalcon\Validation\Validator\StringLength\Min; + * + * $validator = new Validation(); + * + * $validation->add( + * "name_last", + * new Min( + * [ + * "min" => 2, + * "message" => "We want more than just their initials", + * "included" => true + * ] + * ) + * ); + * + * $validation->add( + * [ + * "name_last", + * "name_first", + * ], + * new Min( + * [ + * "min" => [ + * "name_last" => 2, + * "name_first" => 4, + * ], + * "message" => [ + * "name_last" => "We don't like too short last names", + * "name_first" => "We don't like too short first names", + * ], + * "included" => [ + * "name_last" => false, + * "name_first" => true, + * ] + * ] + * ) + * ); + * + */ +class Min extends Validator +{ + protected template = "Field :field must be at least :min characters long"; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var value, length, minimum, replacePairs, included, result; + + let value = validation->getValue(field); + + // Check if mbstring is available to calculate the correct length + if function_exists("mb_strlen") { + let length = mb_strlen(value); + } else { + let length = strlen(value); + } + + let minimum = this->getOption("min"); + + if typeof minimum == "array" { + let minimum = minimum[field]; + } + + let included = this->getOption("included"); + + if typeof included == "array" { + let included = (bool) included[field]; + } else { + let included = (bool) included; + } + + if (included) { + let result = length <= minimum; + } else { + let result = length < minimum; + } + + if result { + let replacePairs = [ + ":min" : minimum + ]; + + validation->appendMessage( + this->messageFactory(validation, field, replacePairs) + ); + + return false; + } + + return true; + } +} diff --git a/phalcon/Validation/Validator/Uniqueness.zep b/phalcon/Validation/Validator/Uniqueness.zep index 47dde0ce3e8..b55dd264130 100644 --- a/phalcon/Validation/Validator/Uniqueness.zep +++ b/phalcon/Validation/Validator/Uniqueness.zep @@ -93,6 +93,8 @@ use Phalcon\Mvc\Collection; */ class Uniqueness extends CombinedFieldsValidator { + protected template = "Field :field must be unique"; + private columnMap = null; /** @@ -100,32 +102,9 @@ class Uniqueness extends CombinedFieldsValidator */ public function validate( validation, var field) -> bool { - var message, label; - if !this->isUniqueness(validation, field) { - let label = this->getOption("label"), - message = this->getOption("message"); - - if empty label { - let label = validation->getLabel(field); - } - - if empty message { - let message = validation->getDefaultMessage("Uniqueness"); - } - validation->appendMessage( - new Message( - strtr( - message, - [ - ":field": label - ] - ), - field, - "Uniqueness", - this->getOption("code") - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/Validator/Url.zep b/phalcon/Validation/Validator/Url.zep index f73305df6b7..79d21c7db1d 100644 --- a/phalcon/Validation/Validator/Url.zep +++ b/phalcon/Validation/Validator/Url.zep @@ -52,12 +52,14 @@ use Phalcon\Validation\Validator; */ class Url extends Validator { + protected template = "Field :field must be a url"; + /** * Executes the validation */ public function validate( validation, var field) -> bool { - var code, label, message, options, replacePairs, result, value; + var options, result, value; let value = validation->getValue(field); @@ -68,21 +70,8 @@ class Url extends Validator } if !result { - let label = this->prepareLabel(validation, field), - message = this->prepareMessage(validation, field, "Url"), - code = this->prepareCode(field); - - let replacePairs = [ - ":field": label - ]; - validation->appendMessage( - new Message( - strtr(message, replacePairs), - field, - "Url", - code - ) + this->messageFactory(validation, field) ); return false; diff --git a/phalcon/Validation/ValidatorComposite.zep b/phalcon/Validation/ValidatorComposite.zep new file mode 100644 index 00000000000..4c0d8e78f96 --- /dev/null +++ b/phalcon/Validation/ValidatorComposite.zep @@ -0,0 +1,46 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation; + +use Phalcon\Validation; + +/** + * Phalcon\Validation\CombinedFieldsValidator + * + * This is a base class for combined fields validators + */ +abstract class ValidatorComposite extends Validator implements ValidatorCompositeInterface +{ + /** + * @var array + */ + protected validators = [] { get }; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool + { + var validator; + + if unlikely count(this->getValidators()) === 0 { + throw new Exception(get_class(this) . " does not have any validator added"); + } + + for validator in this->getValidators() { + if validator->validate(validation, field) === false { + return false; + } + } + + return true; + } +} diff --git a/phalcon/Validation/ValidatorCompositeInterface.zep b/phalcon/Validation/ValidatorCompositeInterface.zep new file mode 100644 index 00000000000..d91826718b3 --- /dev/null +++ b/phalcon/Validation/ValidatorCompositeInterface.zep @@ -0,0 +1,31 @@ + +/** + * This file is part of the Phalcon Framework. + * + * (c) Phalcon Team + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Validation; + +use Phalcon\Validation; + +/** + * Phalcon\Validation\CombinedFieldsValidator + * + * This is a base class for combined fields validators + */ +interface ValidatorCompositeInterface +{ + /** + * Executes the validation + */ + public function getValidators() -> array; + + /** + * Executes the validation + */ + public function validate( validation, var field) -> bool; +} diff --git a/phalcon/Validation/ValidatorInterface.zep b/phalcon/Validation/ValidatorInterface.zep index a0b4f3fee7b..a1fb077a812 100644 --- a/phalcon/Validation/ValidatorInterface.zep +++ b/phalcon/Validation/ValidatorInterface.zep @@ -10,6 +10,9 @@ namespace Phalcon\Validation; +use Phalcon\Collection; +use Phalcon\Validation; + /** * Phalcon\Validation\ValidatorInterface * @@ -20,16 +23,51 @@ interface ValidatorInterface /** * Returns an option in the validator's options * Returns null if the option hasn't set + * + * @return mixed */ public function getOption(string! key, var defaultValue = null) -> var; /** * Checks if an option is defined + * + * @return boolean */ public function hasOption(string! key) -> bool; /** * Executes the validation + * + * @return boolean */ - public function validate(<\Phalcon\Validation> validation, var field) -> bool; + public function validate( validation, var field) -> bool; + + /** + * Get the template message + * + * @return string + * @throw InvalidArgumentException When the field does not exists + */ + public function getTemplate(string! field) -> string; + + /** + * Get message templates + * + * @return array + */ + public function getTemplates() -> array; + + /** + * Clear current template and set new from an array, + * + * @return ValidatorInterface + */ + public function setTemplates(array! templates) -> ; + + /** + * Set a new temlate message + * + * @return ValidatorInterface + */ + public function setTemplate(string! template) -> ; } diff --git a/phalcon/ValidationInterface.zep b/phalcon/ValidationInterface.zep index 31d714c6394..b19f676d6b3 100644 --- a/phalcon/ValidationInterface.zep +++ b/phalcon/ValidationInterface.zep @@ -42,11 +42,6 @@ interface ValidationInterface */ public function bind(entity, data) -> ; - /** - * Get default message for validator type - */ - public function getDefaultMessage(string! type) -> string; - /** * Returns the bound entity */ @@ -87,11 +82,6 @@ interface ValidationInterface */ public function rules(string! field, array! validators) -> ; - /** - * Adds default messages to validators - */ - public function setDefaultMessages(array messages = []) -> array; - /** * Adds filters to the field * diff --git a/tests/_data/fixtures/Traits/ValidationTrait.php b/tests/_data/fixtures/Traits/ValidationTrait.php index 5671c48b0ea..ba6a98a1de5 100644 --- a/tests/_data/fixtures/Traits/ValidationTrait.php +++ b/tests/_data/fixtures/Traits/ValidationTrait.php @@ -89,10 +89,6 @@ private function checkHasOption(IntegrationTester $I, ValidatorInterface $valida $I->assertFalse( $validator->hasOption('option') ); - - $I->assertTrue( - $validator->hasOption('message') - ); } /** diff --git a/tests/integration/Forms/Form/ClearCest.php b/tests/integration/Forms/Form/ClearCest.php index aba3e3cd30f..d90fcc21359 100644 --- a/tests/integration/Forms/Form/ClearCest.php +++ b/tests/integration/Forms/Form/ClearCest.php @@ -23,6 +23,7 @@ use Phalcon\Test\Models\Select as MvcModel; use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\StringLength; +use Phalcon\Validation\Validator\StringLength\Min; class ClearCest { @@ -260,7 +261,7 @@ public function clearFormElementsAndUsingValidation(IntegrationTester $I) new Message( 'The text is too short', 'password', - 'TooShort', + Min::class, 0 ), ] diff --git a/tests/integration/Forms/Form/GetMessagesCest.php b/tests/integration/Forms/Form/GetMessagesCest.php index 1b4698900ac..f347397576f 100644 --- a/tests/integration/Forms/Form/GetMessagesCest.php +++ b/tests/integration/Forms/Form/GetMessagesCest.php @@ -97,13 +97,13 @@ public function testGetElementMessagesFromForm(IntegrationTester $I) new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), new Message( 'The telephone is required', 'telephone', - 'PresenceOf', + PresenceOf::class, 0 ), ] diff --git a/tests/integration/Forms/Form/HasMessagesForCest.php b/tests/integration/Forms/Form/HasMessagesForCest.php index 9d44ccb6d3b..e516fbe64d2 100644 --- a/tests/integration/Forms/Form/HasMessagesForCest.php +++ b/tests/integration/Forms/Form/HasMessagesForCest.php @@ -85,7 +85,7 @@ public function testFormHasMessagesFor(IntegrationTester $I) new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), ] @@ -105,7 +105,6 @@ public function testFormHasMessagesFor(IntegrationTester $I) ); - $I->assertTrue( $form->hasMessagesFor('telephone') ); diff --git a/tests/integration/Forms/Form/IsValidCest.php b/tests/integration/Forms/Form/IsValidCest.php index a298d7691f7..e3dc96850c8 100644 --- a/tests/integration/Forms/Form/IsValidCest.php +++ b/tests/integration/Forms/Form/IsValidCest.php @@ -109,13 +109,13 @@ public function testMergeValidators(IntegrationTester $I) new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), new Message( 'The telephone is required', 'telephone', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -188,7 +188,7 @@ public function formsFormRemoveIsValidCancelOnFail(IntegrationTester $I) new Message( 'your fullname is required', 'fullname', - 'PresenceOf' + PresenceOf::class ), ] ); diff --git a/tests/integration/Forms/Form/SetValidationCest.php b/tests/integration/Forms/Form/SetValidationCest.php index 4c17e124015..890dcdea80c 100644 --- a/tests/integration/Forms/Form/SetValidationCest.php +++ b/tests/integration/Forms/Form/SetValidationCest.php @@ -102,7 +102,7 @@ public function testCustomValidation(IntegrationTester $I) new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), ] diff --git a/tests/integration/Forms/FormCest.php b/tests/integration/Forms/FormCest.php index c0b1845130f..647c02b9cd2 100644 --- a/tests/integration/Forms/FormCest.php +++ b/tests/integration/Forms/FormCest.php @@ -21,6 +21,7 @@ use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\Regex; use Phalcon\Validation\Validator\StringLength; +use Phalcon\Validation\Validator\StringLength\Min; class FormCest { @@ -188,25 +189,25 @@ public function testFormValidator(IntegrationTester $I) new Message( 'The telephone is required', 'telephone', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'The telephone is too short', 'telephone', - 'TooShort', + Min::class, 0 ), new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), new Message( 'The address is required', 'address', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -233,7 +234,7 @@ public function testFormValidator(IntegrationTester $I) new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), ] @@ -310,7 +311,7 @@ public function testElementMessages(IntegrationTester $I) new Message( 'The telephone has an invalid format', 'telephone', - 'Regex', + Regex::class, 0 ), ] diff --git a/tests/integration/Forms/FormElementsCest.php b/tests/integration/Forms/FormElementsCest.php index 8056ffa14eb..f79c9d88546 100644 --- a/tests/integration/Forms/FormElementsCest.php +++ b/tests/integration/Forms/FormElementsCest.php @@ -133,12 +133,12 @@ public function shouldCancelValidationOnFirstFail(IntegrationTester $I) new Message( 'user.lastName.presenceOf', 'lastName', - 'PresenceOf' + PresenceOf::class ), new Message( 'user.firstName.presenceOf', 'firstName', - 'PresenceOf' + PresenceOf::class ), ] ); diff --git a/tests/integration/Mvc/Model/Refactor-ModelsValidatorsCest.php b/tests/integration/Mvc/Model/Refactor-ModelsValidatorsCest.php index e5cc325b0fd..99a1c43d7b8 100644 --- a/tests/integration/Mvc/Model/Refactor-ModelsValidatorsCest.php +++ b/tests/integration/Mvc/Model/Refactor-ModelsValidatorsCest.php @@ -6,6 +6,14 @@ use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Models\Abonnes; +use Phalcon\Validation\Validator\Email; +use Phalcon\Validation\Validator\ExclusionIn; +use Phalcon\Validation\Validator\InclusionIn; +use Phalcon\Validation\Validator\PresenceOf; +use Phalcon\Validation\Validator\Regex; +use Phalcon\Validation\Validator\StringLength\Max; +use Phalcon\Validation\Validator\StringLength\Min; +use Phalcon\Validation\Validator\Uniqueness; class ModelsValidatorsCest { @@ -85,7 +93,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $I->assertCount(1, $messages); $I->assertEquals( - 'PresenceOf', + PresenceOf::class, $messages[0]->getType() ); @@ -120,7 +128,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'Email', + Email::class, $messages[0]->getType() ); $I->assertEquals( @@ -145,7 +153,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'ExclusionIn', + ExclusionIn::class, $messages[0]->getType() ); $I->assertEquals( @@ -169,7 +177,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'InclusionIn', + InclusionIn::class, $messages[0]->getType() ); @@ -196,7 +204,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'Uniqueness', + Uniqueness::class, $messages[0]->getType() ); @@ -221,7 +229,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) ); $messages = $abonne->getMessages(); - $I->assertEquals($messages[0]->getType(), 'Regex'); + $I->assertEquals($messages[0]->getType(), Regex::class); $I->assertEquals($messages[0]->getField(), 'statut'); $I->assertEquals($messages[0]->getMessage(), "L'état ne correspond pas à l'expression régulière"); @@ -238,7 +246,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'TooLong', + Max::class, $messages[0]->getType() ); @@ -265,7 +273,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'TooShort', + Min::class, $messages[0]->getType() ); @@ -302,7 +310,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $messages = $abonne->getMessages(); $I->assertEquals( - 'PresenceOf', + PresenceOf::class, $messages[0]->getType() ); @@ -319,7 +327,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $I->assertEquals( - 'Email', + Email::class, $messages[1]->getType() ); @@ -340,7 +348,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $I->assertCount(1, $messages); $I->assertEquals( - 'PresenceOf', + PresenceOf::class, $messages[0]->getType() ); @@ -361,7 +369,7 @@ protected function testValidatorsRenamed(IntegrationTester $I) $I->assertCount(1, $messages); $I->assertEquals( - 'Email', + Email::class, $messages[0]->getType() ); diff --git a/tests/integration/Refactor-ValidationCest.php b/tests/integration/Refactor-ValidationCest.php index a01b3cff2a9..2e206f154fd 100644 --- a/tests/integration/Refactor-ValidationCest.php +++ b/tests/integration/Refactor-ValidationCest.php @@ -50,7 +50,7 @@ public function appendValidationMessageToTheNonObject(IntegrationTester $I) new Message( 'Field foo is required', 'foo', - 'PresenceOf', + PresenceOf::class, 0 ), ] diff --git a/tests/integration/Validation/AddCest.php b/tests/integration/Validation/AddCest.php index 7dda339c9f0..63faeb67aa3 100644 --- a/tests/integration/Validation/AddCest.php +++ b/tests/integration/Validation/AddCest.php @@ -53,7 +53,7 @@ public function validationAdd(IntegrationTester $I) $I->assertEquals( [ - 'name' => [ + 'name' => [ $alpha, $presenceOf, ], diff --git a/tests/integration/Validation/ConstructCest.php b/tests/integration/Validation/ConstructCest.php index 35105195de0..18930a6b561 100644 --- a/tests/integration/Validation/ConstructCest.php +++ b/tests/integration/Validation/ConstructCest.php @@ -30,7 +30,7 @@ public function validationConstruct(IntegrationTester $I) $I->wantToTest('Validation - __construct()'); $validators = [ - 'date' => [ + 'date' => [ new Date(), ], 'email' => [ diff --git a/tests/integration/Validation/RuleCest.php b/tests/integration/Validation/RuleCest.php index c38c898740b..8dda2fe0296 100644 --- a/tests/integration/Validation/RuleCest.php +++ b/tests/integration/Validation/RuleCest.php @@ -53,7 +53,7 @@ public function validationRule(IntegrationTester $I) $I->assertEquals( [ - 'name' => [ + 'name' => [ $alpha, $presenceOf, ], diff --git a/tests/integration/Validation/RulesCest.php b/tests/integration/Validation/RulesCest.php index 03ba0156e0b..b9c0c6fd066 100644 --- a/tests/integration/Validation/RulesCest.php +++ b/tests/integration/Validation/RulesCest.php @@ -53,7 +53,7 @@ public function validationRules(IntegrationTester $I) $I->assertEquals( [ - 'name' => [ + 'name' => [ $alpha, $presenceOf, ], diff --git a/tests/integration/Validation/SetFiltersCest.php b/tests/integration/Validation/SetFiltersCest.php index 67583ca2b95..e7897798a5c 100644 --- a/tests/integration/Validation/SetFiltersCest.php +++ b/tests/integration/Validation/SetFiltersCest.php @@ -53,7 +53,6 @@ public function validationSetFilters(IntegrationTester $I) ); - $messages = $validation->validate( [ 'name' => ' ', diff --git a/tests/integration/Validation/SetValidatorsCest.php b/tests/integration/Validation/SetValidatorsCest.php index 1a96b594f77..c2316ad39f0 100644 --- a/tests/integration/Validation/SetValidatorsCest.php +++ b/tests/integration/Validation/SetValidatorsCest.php @@ -30,7 +30,7 @@ public function validationSetValidators(IntegrationTester $I) $I->wantToTest('Validation - setValidators()'); $validators = [ - 'date' => [ + 'date' => [ new Date(), ], 'email' => [ diff --git a/tests/integration/Validation/ValidationCest.php b/tests/integration/Validation/ValidationCest.php index 629e1b1b424..7a879bf6807 100644 --- a/tests/integration/Validation/ValidationCest.php +++ b/tests/integration/Validation/ValidationCest.php @@ -12,6 +12,7 @@ use Phalcon\Validation\Validator\Email; use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\StringLength; +use Phalcon\Validation\Validator\StringLength\Min; use Phalcon\Validation\Validator\Url; use stdClass; @@ -97,7 +98,7 @@ public function appendValidationMessageToTheNonObject(IntegrationTester $I) new Message( 'Field foo is required', 'foo', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -140,7 +141,7 @@ public function testWithEntityAndFilter(IntegrationTester $I) new Message( 'Name cant be empty.', 'name', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -173,15 +174,6 @@ public function testFilteringEntity(IntegrationTester $I) ); } - public function testGetDefaultValidationMessageShouldReturnEmptyStringIfNoneIsSet(IntegrationTester $I) - { - $validation = new Validation(); - - $I->assertIsEmpty( - $validation->getDefaultMessage('_notexistentvalidationmessage_') - ); - } - public function testValidationFiltering(IntegrationTester $I) { $validation = new Validation(); @@ -227,7 +219,7 @@ public function testValidationFiltering(IntegrationTester $I) new Message( 'The email is required', 'email', - 'PresenceOf', + PresenceOf::class, 0 ), ]; @@ -295,25 +287,25 @@ public function testValidationSetLabels(IntegrationTester $I) new Message( 'The email is required', 'email', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'The E-mail must be email', 'email', - 'Email', + Email::class, 0 ), new Message( 'The First name is required', 'firstname', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'The First name is too short', 'firstname', - 'TooShort', + Min::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/Alnum/ValidateCest.php b/tests/integration/Validation/Validator/Alnum/ValidateCest.php index a6e2f80f534..3b7503796bf 100644 --- a/tests/integration/Validation/Validator/Alnum/ValidateCest.php +++ b/tests/integration/Validation/Validator/Alnum/ValidateCest.php @@ -75,18 +75,23 @@ public function validationValidatorAlnumValidateMultipleField(IntegrationTester 'type' => 'Type must be alnum', ]; + $al = new Alnum( + [ + 'message' => $validationMessages, + ] + ); + $validation->add( [ 'name', 'type', ], - new Alnum( - [ - 'message' => $validationMessages, - ] - ) + $al ); + codecept_debug($validation); + codecept_debug($al); + $messages = $validation->validate( [ diff --git a/tests/integration/Validation/Validator/Alpha/ValidateCest.php b/tests/integration/Validation/Validator/Alpha/ValidateCest.php index 9dc2c098022..2b8df109595 100644 --- a/tests/integration/Validation/Validator/Alpha/ValidateCest.php +++ b/tests/integration/Validation/Validator/Alpha/ValidateCest.php @@ -175,7 +175,7 @@ public function validationValidatorAlphaValidateNonAlphabeticCharacters(Integrat new Message( 'name must contain only letters', 'name', - 'Alpha', + Alpha::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/Between/ValidateCest.php b/tests/integration/Validation/Validator/Between/ValidateCest.php index e54ad13fb30..f5d60493fc4 100644 --- a/tests/integration/Validation/Validator/Between/ValidateCest.php +++ b/tests/integration/Validation/Validator/Between/ValidateCest.php @@ -57,7 +57,7 @@ public function validationValidatorBetweenValidateSingleField(IntegrationTester new Message( 'Field price must be within the range of 1 to 3', 'price', - 'Between', + Between::class, 0 ), ] @@ -211,7 +211,7 @@ public function validationValidatorBetweenValidateCustomMessage(IntegrationTeste new Message( 'The price must be between 1 and 3', 'price', - 'Between', + Between::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/Callback/ValidateCest.php b/tests/integration/Validation/Validator/Callback/ValidateCest.php index 75a5a475fbc..33e2238ea58 100644 --- a/tests/integration/Validation/Validator/Callback/ValidateCest.php +++ b/tests/integration/Validation/Validator/Callback/ValidateCest.php @@ -20,6 +20,7 @@ use Phalcon\Validation\Validator\Exception; use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\StringLength; +use Phalcon\Validation\Validator\StringLength\Min; class ValidateCest { @@ -87,7 +88,7 @@ public function validationValidatorCallbackValidateSingleFieldBoolean(Integratio new Message( 'You cant provide both admin and user.', 'user', - 'Callback', + Callback::class, 0 ), ] @@ -167,7 +168,7 @@ public function validationValidatorCallbackValidateSingleFieldValidator(Integrat new Message( 'User name should be minimum 4 characters.', 'user', - 'TooShort', + Min::class, 0 ), ] @@ -243,13 +244,13 @@ public function validationValidatorCallbackValidateMultipleFieldBoolean(Integrat new Message( 'There must be only an user or admin set', 'user', - 'Callback', + Callback::class, 0 ), new Message( 'There must be only an user or admin set', 'admin', - 'Callback', + Callback::class, 0 ), ] @@ -311,13 +312,13 @@ public function validationValidatorCallbackValidateMultipleFieldValidator(Integr new Message( 'You must provide admin or user', 'user', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'You must provide admin or user', 'admin', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -361,13 +362,13 @@ public function validationValidatorCallbackValidateMultipleFieldValidator(Integr new Message( 'There must be only an user or admin set', 'user', - 'Callback', + Callback::class, 0 ), new Message( 'There must be only an user or admin set', 'admin', - 'Callback', + Callback::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/Confirmation/ValidateCest.php b/tests/integration/Validation/Validator/Confirmation/ValidateCest.php index 006d9d4f82d..724387f2c25 100644 --- a/tests/integration/Validation/Validator/Confirmation/ValidateCest.php +++ b/tests/integration/Validation/Validator/Confirmation/ValidateCest.php @@ -184,7 +184,7 @@ public function validationValidatorConfirmationValidateEmptyValues(IntegrationTe new Message( 'Field password must be the same as password2', 'password', - 'Confirmation', + Confirmation::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/CreditCard/ValidateCest.php b/tests/integration/Validation/Validator/CreditCard/ValidateCest.php index 6eba9558bb5..09f88e3ee9c 100644 --- a/tests/integration/Validation/Validator/CreditCard/ValidateCest.php +++ b/tests/integration/Validation/Validator/CreditCard/ValidateCest.php @@ -216,7 +216,7 @@ public function validationValidatorCreditCardValidateInvalidCreditCard(Integrati new Message( 'Field creditCard is not valid for a credit card number', 'creditCard', - 'CreditCard', + CreditCard::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/DateCest.php b/tests/integration/Validation/Validator/DateCest.php index 25f3b2b97a2..4ec5d48d875 100644 --- a/tests/integration/Validation/Validator/DateCest.php +++ b/tests/integration/Validation/Validator/DateCest.php @@ -141,8 +141,8 @@ public function validationValidatorMultipleField(IntegrationTester $I) /** * Tests detect valid dates * - * @author Gustavo Verzola - * @since 2015-03-09 + * @author Gustavo Verzola + * @since 2015-03-09 * * @dataProvider shouldDetectValidDatesProvider */ @@ -177,8 +177,8 @@ public function shouldDetectValidDates(IntegrationTester $I, Example $example) /** * Tests detect invalid dates * - * @author Gustavo Verzola - * @since 2015-03-09 + * @author Gustavo Verzola + * @since 2015-03-09 * * @dataProvider shouldDetectInvalidDatesProvider */ @@ -203,7 +203,7 @@ public function shouldDetectInvalidDates(IntegrationTester $I, Example $example) new Message( 'Field date is not a valid date', 'date', - 'Date', + Date::class, 0 ), ] @@ -240,5 +240,40 @@ protected function shouldDetectInvalidDatesProvider(): array ['2015-01', 'Y-m-d'], ['2015-01-01', 'd-m-Y'], ]; + + foreach ($dates as $item) { + $date = $item[0]; + $format = $item[1]; + + $validation = new Validation(); + + $validation->add( + 'date', + new Date( + [ + 'format' => $format, + ] + ) + ); + + $expected = new Messages( + [ + new Message( + 'Field date is not a valid date', + 'date', + Date::class, + 0 + ), + ] + ); + + $actual = $validation->validate( + [ + 'date' => $date, + ] + ); + + $I->assertEquals($expected, $actual); + } } } diff --git a/tests/integration/Validation/Validator/EmailCest.php b/tests/integration/Validation/Validator/EmailCest.php index 56b6e5c3bb8..547742c8e8c 100644 --- a/tests/integration/Validation/Validator/EmailCest.php +++ b/tests/integration/Validation/Validator/EmailCest.php @@ -35,7 +35,6 @@ public function validationValidatorSingleField(IntegrationTester $I) ); - $messages = $validation->validate( [ 'email' => 'test@somemail.com', @@ -48,7 +47,6 @@ public function validationValidatorSingleField(IntegrationTester $I) ); - $messages = $validation->validate( [ 'email' => 'rootlocalhost', @@ -65,7 +63,7 @@ public function validationValidatorSingleField(IntegrationTester $I) new Message( 'Field email must be an email address', 'email', - 'Email', + Email::class, 0 ), ] @@ -102,7 +100,6 @@ public function validationValidatorMultipleField(IntegrationTester $I) ); - $messages = $validation->validate( [ 'email' => 'test@somemail.com', @@ -116,7 +113,6 @@ public function validationValidatorMultipleField(IntegrationTester $I) ); - $messages = $validation->validate( [ 'email' => 'rootlocalhost', @@ -135,7 +131,6 @@ public function validationValidatorMultipleField(IntegrationTester $I) ); - $messages = $validation->validate( [ 'email' => 'rootlocalhost', @@ -178,7 +173,7 @@ public function validationValidatorCustomMessage(IntegrationTester $I) new Message( 'The email is not valid', 'email', - 'Email', + Email::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/ExclusionInCest.php b/tests/integration/Validation/Validator/ExclusionInCest.php index 24648359ef3..a9a5c189b37 100644 --- a/tests/integration/Validation/Validator/ExclusionInCest.php +++ b/tests/integration/Validation/Validator/ExclusionInCest.php @@ -49,7 +49,7 @@ public function validationValidatorSingleField(IntegrationTester $I) new Message( 'Field status must not be a part of list: A, I', 'status', - 'ExclusionIn', + ExclusionIn::class, 0 ), ] @@ -202,7 +202,6 @@ public function validationValidatorCustomMessage(IntegrationTester $I) ); - $messages = $validation->validate( [ 'status' => 'A', @@ -214,7 +213,7 @@ public function validationValidatorCustomMessage(IntegrationTester $I) new Message( 'The status must not be A=Active or I=Inactive', 'status', - 'ExclusionIn', + ExclusionIn::class, 0 ), ] @@ -223,7 +222,6 @@ public function validationValidatorCustomMessage(IntegrationTester $I) $I->assertEquals($expected, $messages); - $messages = $validation->validate( [ 'status' => 'A', @@ -233,7 +231,6 @@ public function validationValidatorCustomMessage(IntegrationTester $I) $I->assertEquals($expected, $messages); - $messages = $validation->validate( [ 'status' => 'X', diff --git a/tests/integration/Validation/Validator/IdenticalCest.php b/tests/integration/Validation/Validator/IdenticalCest.php index 664bf1d2ca1..6cd7aaf6d13 100644 --- a/tests/integration/Validation/Validator/IdenticalCest.php +++ b/tests/integration/Validation/Validator/IdenticalCest.php @@ -68,7 +68,7 @@ public function validationValidatorSingleField(IntegrationTester $I) new Message( 'Field name does not have the expected value', 'name', - 'Identical', + Identical::class, 0 ), ] @@ -271,7 +271,7 @@ public function validationValidatorCustomMessage(IntegrationTester $I) $validation->add( 'name', - new Validation\Validator\Identical( + new Identical( [ 'accepted' => 'Peter', 'message' => 'The name must be peter', @@ -289,7 +289,7 @@ public function validationValidatorCustomMessage(IntegrationTester $I) new Message( 'The name must be peter', 'name', - 'Identical', + Identical::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/InclusionInCest.php b/tests/integration/Validation/Validator/InclusionInCest.php index ae183bd7c5f..aafe63bf516 100644 --- a/tests/integration/Validation/Validator/InclusionInCest.php +++ b/tests/integration/Validation/Validator/InclusionInCest.php @@ -48,7 +48,7 @@ public function validationValidatorSingleField(IntegrationTester $I) new Message( 'Field status must be a part of list: A, I', 'status', - 'InclusionIn', + InclusionIn::class, 0 ), ] @@ -293,7 +293,7 @@ public function validationValidatorCustomMessage(IntegrationTester $I) new Message( 'The status must be A=Active or I=Inactive', 'status', - 'InclusionIn', + InclusionIn::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/Numericality/ValidateCest.php b/tests/integration/Validation/Validator/Numericality/ValidateCest.php index 9036cad545b..7af3906c0d5 100644 --- a/tests/integration/Validation/Validator/Numericality/ValidateCest.php +++ b/tests/integration/Validation/Validator/Numericality/ValidateCest.php @@ -28,14 +28,14 @@ class ValidateCest * * @dataProvider getExamples * - * @author Phalcon Team - * @since 2018-11-13 + * @author Phalcon Team + * @since 2018-11-13 */ public function validationValidatorNumericalityValidate(IntegrationTester $I, Example $example) { $I->wantToTest('Validation\Validator\Numericality - validate() ' . $example[0]); - $entity = new stdClass(); + $entity = new stdClass(); $entity->price = $example[0]; $validation = new Validation(); diff --git a/tests/integration/Validation/Validator/PresenceOfCest.php b/tests/integration/Validation/Validator/PresenceOfCest.php index a5da41d4093..378f74ab810 100644 --- a/tests/integration/Validation/Validator/PresenceOfCest.php +++ b/tests/integration/Validation/Validator/PresenceOfCest.php @@ -110,7 +110,7 @@ public function shouldValidateMultipleField(IntegrationTester $I) new Message( 'Name cant be empty.', 'name', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -146,13 +146,13 @@ public function shouldValidateMultipleField(IntegrationTester $I) new Message( 'Name cant be empty.', 'name', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'Type cant be empty.', 'type', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -182,19 +182,19 @@ public function shouldValidateMixedFields(IntegrationTester $I) new Message( 'The name is required', 'name', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'The email is required', 'email', - 'PresenceOf', + PresenceOf::class, 0 ), new Message( 'The login is required', 'login', - 'PresenceOf', + PresenceOf::class, 0 ), ] @@ -231,17 +231,17 @@ public function shouldCancelOnFail(IntegrationTester $I) new Message( 'The name is required', 'name', - 'PresenceOf' + PresenceOf::class ), new Message( 'The email is required', 'email', - 'PresenceOf' + PresenceOf::class ), new Message( 'The login is required', 'login', - 'PresenceOf' + PresenceOf::class ), ] ); diff --git a/tests/integration/Validation/Validator/RegexCest.php b/tests/integration/Validation/Validator/RegexCest.php index 2582ca1c851..7e884230e0a 100644 --- a/tests/integration/Validation/Validator/RegexCest.php +++ b/tests/integration/Validation/Validator/RegexCest.php @@ -47,7 +47,7 @@ public function validationValidatorSingleField(IntegrationTester $I) new Message( 'Field car_plate does not match the required format', 'car_plate', - 'Regex', + Regex::class, 0 ), ] @@ -273,7 +273,7 @@ public function validationValidatorCustomMessage(IntegrationTester $I) new Message( 'The car plate is not valid', 'car_plate', - 'Regex', + Regex::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/StringLength/ConstructCest.php b/tests/integration/Validation/Validator/StringLength/ConstructCest.php index 1c0b6303bb7..7f0b40773df 100644 --- a/tests/integration/Validation/Validator/StringLength/ConstructCest.php +++ b/tests/integration/Validation/Validator/StringLength/ConstructCest.php @@ -15,6 +15,7 @@ use IntegrationTester; use Phalcon\Test\Fixtures\Traits\ValidationTrait; use Phalcon\Validation\Validator\StringLength; +use Phalcon\Validation\ValidatorCompositeInterface; /** * Class ConstructCest @@ -26,6 +27,8 @@ class ConstructCest /** * Tests Phalcon\Validation\Validator\StringLength :: __construct() * + * @param IntegrationTester $I + * * @author Phalcon Team * @since 2018-11-13 */ @@ -36,5 +39,11 @@ public function validationValidatorStringLengthConstruct(IntegrationTester $I) $validator = new StringLength(); $this->checkConstruct($I, $validator); + + $I->assertInstanceOf( + ValidatorCompositeInterface::class, + $validator, + 'StringLength implements ValidatorCompositeInterface' + ); } } diff --git a/tests/integration/Validation/Validator/StringLength/GetOptionCest.php b/tests/integration/Validation/Validator/StringLength/GetOptionCest.php index 72ce55c5434..42d69bf993d 100644 --- a/tests/integration/Validation/Validator/StringLength/GetOptionCest.php +++ b/tests/integration/Validation/Validator/StringLength/GetOptionCest.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Phalcon\Test\Integration\Validation\Validator\StringLength; +namespace Phalcon\Test\Integration\Validation\Validator\StringLength2; use IntegrationTester; use Phalcon\Test\Fixtures\Traits\ValidationTrait; diff --git a/tests/integration/Validation/Validator/StringLength/GetTemplateCest.php b/tests/integration/Validation/Validator/StringLength/GetTemplateCest.php new file mode 100644 index 00000000000..911bb2b25d0 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/GetTemplateCest.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength; + +use Phalcon\Validation\Validator\StringLength; +use UnitTester; + +class GetTemplateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength :: getTemplate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthGetTemplate(UnitTester $I) + { + $I->wantToTest('Validation\Validator\StringLength - getTemplate()'); + + $validator = new StringLength(); + + $expected = 'The field :field is not valid for ' . StringLength::class; + + $I->assertEquals( + $expected, + $validator->getTemplate(), + 'Default template message' + ); + + $expected = 'New custom template message'; + + $actual = $validator->setTemplate($expected); + + $I->assertInstanceOf(StringLength::class, $actual, 'Instance of StringLenght'); + + $I->assertEquals( + $expected, + $validator->getTemplate(), + 'Get equals template message' + ); + } +} diff --git a/tests/integration/Validation/GetDefaultMessageCest.php b/tests/integration/Validation/Validator/StringLength/GetTemplatesCest.php similarity index 53% rename from tests/integration/Validation/GetDefaultMessageCest.php rename to tests/integration/Validation/Validator/StringLength/GetTemplatesCest.php index 68ec3e7036a..05f11fa799f 100644 --- a/tests/integration/Validation/GetDefaultMessageCest.php +++ b/tests/integration/Validation/Validator/StringLength/GetTemplatesCest.php @@ -10,24 +10,21 @@ * file that was distributed with this source code. */ -namespace Phalcon\Test\Integration\Validation; +namespace Phalcon\Test\Unit\Validation\Validator\StringLength; -use IntegrationTester; +use UnitTester; -/** - * Class GetDefaultMessageCest - */ -class GetDefaultMessageCest +class GetTemplatesCest { /** - * Tests Phalcon\Validation :: getDefaultMessage() + * Tests Phalcon\Validation\Validator\StringLength :: getTemplates() * * @author Phalcon Team - * @since 2019-04-16 + * @since 2019-05-23 */ - public function validationGetDefaultMessage(IntegrationTester $I) + public function validationValidatorStringLengthGetTemplates(UnitTester $I) { - $I->wantToTest('Validation - getDefaultMessage()'); + $I->wantToTest('Validation\Validator\StringLength - getTemplates()'); $I->skipTest('Need implementation'); } diff --git a/tests/integration/Validation/Validator/StringLength/GetValidatorsCest.php b/tests/integration/Validation/Validator/StringLength/GetValidatorsCest.php new file mode 100644 index 00000000000..775fc96cb79 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/GetValidatorsCest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength; + +use Phalcon\Validation\Validator\StringLength; +use UnitTester; + +class GetValidatorsCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength :: getValidators() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthGetValidators(UnitTester $I) + { + $I->wantToTest('Validation\Validator\StringLength - getValidators()'); + + $validator = new StringLength(); + + $I->assertTrue(is_array($validator->getValidators()), 'Is array'); + + $I->assertEmpty($validator->getValidators(), 'Empty validators'); + $I->assertCount(0, $validator->getValidators(), 'Empty validators'); + + $validator = new StringLength([ + 'min' => 5, + 'max' => 15, + ]); + + $I->assertTrue(is_array($validator->getValidators()), 'Is array'); + + $I->assertNotEmpty($validator->getValidators(), 'Not empty validators'); + $I->assertCount(2, $validator->getValidators(), 'Has 2 validators'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/HasOptionCest.php b/tests/integration/Validation/Validator/StringLength/HasOptionCest.php index 6e1fefe7afe..74f8236b546 100644 --- a/tests/integration/Validation/Validator/StringLength/HasOptionCest.php +++ b/tests/integration/Validation/Validator/StringLength/HasOptionCest.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Phalcon\Test\Integration\Validation\Validator\StringLength; +namespace Phalcon\Test\Integration\Validation\Validator\StringLength2; use IntegrationTester; use Phalcon\Test\Fixtures\Traits\ValidationTrait; diff --git a/tests/integration/Validation/Validator/StringLength/Max/ConstructCest.php b/tests/integration/Validation/Validator/StringLength/Max/ConstructCest.php new file mode 100644 index 00000000000..fdded6add32 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/ConstructCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\ValidationTrait; +use Phalcon\Validation\Validator\StringLength\Max; + +class ConstructCest +{ + use ValidationTrait; + + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: __construct() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxConstruct(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - __construct()'); + + $validator = new Max(); + + $this->checkConstruct($I, $validator); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/GetOptionCest.php b/tests/integration/Validation/Validator/StringLength/Max/GetOptionCest.php new file mode 100644 index 00000000000..945d3017d14 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/GetOptionCest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Max; + +class GetOptionCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: getOption() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxGetOption(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - getOption()'); + + $validator = new Max(); + + $I->assertEquals(null, $validator->getOption('max'), 'Max option is null by default'); + + $expected = 1234; + $validator->setOption('max', $expected); + $I->assertSame($expected, $validator->getOption('max'), 'Max option is 1234'); + + $expected = "1234"; + $validator->setOption('max', $expected); + $I->assertEquals($expected, $validator->getOption('max'), 'Max option is "1234"'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/GetTemplateCest.php b/tests/integration/Validation/Validator/StringLength/Max/GetTemplateCest.php new file mode 100644 index 00000000000..bf8692cccc5 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/GetTemplateCest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Max; + +class GetTemplateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: getTemplate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxGetTemplate(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - getTemplate()'); + + $validator = new Max(); + + $expected = "Field :field must not exceed :max characters long"; + $I->assertEquals($expected, $validator->getTemplate(), 'Default template message for Max'); + + $validator->setTemplate(''); + + $expected = "The field :field is not valid for " . Max::class; + $I->assertEquals($expected, $validator->getTemplate(), 'Default template message'); + + $expected = 'New custom template'; + $validator->setTemplate($expected); + + $I->assertEquals($expected, $validator->getTemplate(), 'New template message'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/GetTemplatesCest.php b/tests/integration/Validation/Validator/StringLength/Max/GetTemplatesCest.php new file mode 100644 index 00000000000..1e291d4e638 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/GetTemplatesCest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Max; + +class GetTemplatesCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: getTemplates() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxGetTemplates(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - getTemplates()'); + + $validator = new Max(); + + $I->assertTrue(is_array($validator->getTemplates()), 'Templates have to be a array'); + $I->assertCount(0, $validator->getTemplates(), 'templates count 0'); + + $messageLastName = "We don't like really long last names"; + $messageFirstName = "We don't like really long first names"; + + $validator = new Max( + [ + "max" => [ + "name_last" => 50, + "name_first" => 40, + ], + "message" => [ + "name_last" => $messageLastName, + "name_first" => $messageFirstName, + ], + "included" => [ + "name_last" => false, + "name_first" => true, + ], + ] + ); + + $I->assertTrue(is_array($validator->getTemplates()), 'Multi templates have to be an array'); + $I->assertEquals( + $messageLastName, + $validator->getTemplate('name_last'), + 'Last name template' + ); + $I->assertEquals( + $messageFirstName, + $validator->getTemplate('name_first'), + 'First name template' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/HasOptionCest.php b/tests/integration/Validation/Validator/StringLength/Max/HasOptionCest.php new file mode 100644 index 00000000000..35a92803ac2 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/HasOptionCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Max; + +class HasOptionCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: hasOption() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxHasOption(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - hasOption()'); + + $validator = new Max(); + + $I->assertFalse($validator->hasOption('max'), 'Max option does not exists'); + + $validator->setOption('max', 1234); + $I->assertTrue($validator->hasOption('max'), 'Max option does exists'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/MessageFactoryCest.php b/tests/integration/Validation/Validator/StringLength/Max/MessageFactoryCest.php new file mode 100644 index 00000000000..388c04b07e4 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/MessageFactoryCest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Messages\Message; +use Phalcon\Messages\Messages; +use Phalcon\Validation; +use Phalcon\Validation\Validator\StringLength\Max; + +class MessageFactoryCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: messageFactory() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxMessageFactory(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - messageFactory()'); + + $validator = new Max([ + "max" => [ + "last_name" => 10, + ], + ]); + + $validation = new Validation(); + $validation->add( + [ + "last_name", + ], + $validator + ); + + $messages = $validation->validate(["last_name" => "A name too long"]); + + $I->assertInstanceOf(Messages::class, $messages, 'Failed validation instance of Messages\Messages'); + $I->assertEquals(1, $messages->count(), 'Has 1 Message'); + + $actual = $validator->messageFactory( + $validation, + "last_name", + [":max" => 10] + ); + + $expected = new Message( + 'Field last_name must not exceed 10 characters long', + 'last_name', + Max::class + ); + + $I->assertInstanceOf(Message::class, $actual, 'Failed validation message instanceof Message'); + $I->assertEquals($expected, $actual, 'Failed validation message and factory message are the same'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/SetOptionCest.php b/tests/integration/Validation/Validator/StringLength/Max/SetOptionCest.php new file mode 100644 index 00000000000..45d84600450 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/SetOptionCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\ValidationTrait; +use Phalcon\Validation\Validator\StringLength; + +class SetOptionCest +{ + use ValidationTrait; + + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: setOption() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxSetOption(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - setOption()'); + + $validator = new StringLength(); + + $this->checkSetOption($I, $validator); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/SetTemplateCest.php b/tests/integration/Validation/Validator/StringLength/Max/SetTemplateCest.php new file mode 100644 index 00000000000..3f38286681b --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/SetTemplateCest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Max; + +class SetTemplateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: setTemplate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxSetTemplate(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - setTemplate()'); + + $validator = new Max(); + + $expected = 'New custom template message'; + + $actual = $validator->setTemplate($expected); + + $I->assertInstanceOf(Max::class, $actual, 'Instance of Max'); + + $I->assertEquals( + $expected, + $validator->getTemplate(), + 'Get equals template message' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/SetTemplatesCest.php b/tests/integration/Validation/Validator/StringLength/Max/SetTemplatesCest.php new file mode 100644 index 00000000000..e7960c10d9d --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/SetTemplatesCest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Max; + +class SetTemplatesCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: setTemplates() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxSetTemplates(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - setTemplates()'); + + $validator = new Max(); + + $expected = [ + 'key-1' => 'value-1', + 'key-2' => 'value-2', + 'key-3' => 'value-3', + ]; + + $actual = $validator->setTemplates($expected); + + $I->assertInstanceOf(Max::class, $actual, 'Instance of Max'); + + $I->assertEquals( + $expected, + $validator->getTemplates(), + 'Get equals templates' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Max/ValidateCest.php b/tests/integration/Validation/Validator/StringLength/Max/ValidateCest.php new file mode 100644 index 00000000000..5af85ee17a1 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Max/ValidateCest.php @@ -0,0 +1,137 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Max; + +use IntegrationTester; +use Phalcon\Validation; +use Phalcon\Validation\Validator\StringLength\Max; + +class ValidateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Max :: validate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMaxValidate(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Max - validate()'); + } + + /** + * Tests Phalcon\Validation\Validator\StringLength :: validate() - single + * field + * + * @author Wojciech Ślawski + * @since 2016-06-05 + */ + public function validationValidatorMaxStringLengthValidateSingleField(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength :: validate() - single field'); + + $validation = new Validation(); + + $validation->add( + 'name', + new Max( + [ + 'max' => 9, + ] + ) + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue', + ] + ); + + $I->assertEquals( + 0, + $messages->count() + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue123', + ] + ); + + $I->assertEquals( + 1, + $messages->count() + ); + } + + /** + * Tests Phalcon\Validation\Validator\StringLength :: validate() - single + * field + * + * @author Wojciech Ślawski + * @since 2016-06-05 + */ + public function validationValidatorMaxOrEqualStringLengthValidateSingleField(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength :: validate() - single field'); + + $validation = new Validation(); + + $validation->add( + 'name', + new Max( + [ + 'max' => 9, + 'included' => true, + ] + ) + ); + + + $messages = $validation->validate( + [ + 'name' => 'short', + ] + ); + + $I->assertEquals( + 0, + $messages->count() + ); + + $messages = $validation->validate( + [ + 'name' => 'SomeValue', + ] + ); + + $I->assertEquals( + 1, + $messages->count() + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue123', + ] + ); + + $I->assertEquals( + 1, + $messages->count() + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/MessageFactoryCest.php b/tests/integration/Validation/Validator/StringLength/MessageFactoryCest.php new file mode 100644 index 00000000000..af80c7fa09b --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/MessageFactoryCest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength; + +use UnitTester; + +class MessageFactoryCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength :: messageFactory() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMessageFactory(UnitTester $I) + { + $I->wantToTest('Validation\Validator\StringLength - messageFactory()'); + + $I->skipTest('Need implementation'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/ConstructCest.php b/tests/integration/Validation/Validator/StringLength/Min/ConstructCest.php new file mode 100644 index 00000000000..d5a937a17f3 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/ConstructCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\ValidationTrait; +use Phalcon\Validation\Validator\StringLength\Min; + +class ConstructCest +{ + use ValidationTrait; + + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: __construct() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinConstruct(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - __construct()'); + + $validator = new Min(); + + $this->checkConstruct($I, $validator); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/GetOptionCest.php b/tests/integration/Validation/Validator/StringLength/Min/GetOptionCest.php new file mode 100644 index 00000000000..059ed70c396 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/GetOptionCest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Min; + +class GetOptionCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: getOption() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinGetOption(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - getOption()'); + + $validator = new Min(); + + $I->assertEquals(null, $validator->getOption('min'), 'Min option is null by default'); + + $expected = 1234; + $validator->setOption('min', $expected); + $I->assertSame($expected, $validator->getOption('min'), 'Min option is 1234'); + + $expected = "1234"; + $validator->setOption('min', $expected); + $I->assertEquals($expected, $validator->getOption('min'), 'Min option is "1234"'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/GetTemplateCest.php b/tests/integration/Validation/Validator/StringLength/Min/GetTemplateCest.php new file mode 100644 index 00000000000..a857036b140 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/GetTemplateCest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Min; + +class GetTemplateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: getTemplate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinGetTemplate(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - getTemplate()'); + + $validator = new Min(); + + $expected = "Field :field must be at least :min characters long"; + $I->assertEquals($expected, $validator->getTemplate(), 'Default template message for Min'); + + $validator->setTemplate(''); + + $expected = "The field :field is not valid for " . Min::class; + $I->assertEquals($expected, $validator->getTemplate(), 'Default template message'); + + $expected = 'New custom template'; + $validator->setTemplate($expected); + + $I->assertEquals($expected, $validator->getTemplate(), 'New template message'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/GetTemplatesCest.php b/tests/integration/Validation/Validator/StringLength/Min/GetTemplatesCest.php new file mode 100644 index 00000000000..b58017a557f --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/GetTemplatesCest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Min; + +class GetTemplatesCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: getTemplates() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinGetTemplates(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - getTemplates()'); + + $validator = new Min(); + + $I->assertTrue(is_array($validator->getTemplates()), 'Templates have to be an array'); + $I->assertCount(0, $validator->getTemplates(), 'templates count 0'); + + $messageLastName = "We don't like really long last names"; + $messageFirstName = "We don't like really long first names"; + + $validator = new Min( + [ + "min" => [ + "name_last" => 50, + "name_first" => 40, + ], + "message" => [ + "name_last" => $messageLastName, + "name_first" => $messageFirstName, + ], + "included" => [ + "name_last" => false, + "name_first" => true, + ], + ] + ); + + $I->assertTrue(is_array($validator->getTemplates()), 'Multi templates have to be an array'); + $I->assertEquals( + $messageLastName, + $validator->getTemplate('name_last'), + 'Last name template' + ); + $I->assertEquals( + $messageFirstName, + $validator->getTemplate('name_first'), + 'First name template' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/HasOptionCest.php b/tests/integration/Validation/Validator/StringLength/Min/HasOptionCest.php new file mode 100644 index 00000000000..0e236fe9311 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/HasOptionCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Min; + +class HasOptionCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: hasOption() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinHasOption(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - hasOption()'); + + $validator = new Min(); + + $I->assertFalse($validator->hasOption('min'), 'Min option does not exists'); + + $validator->setOption('min', 1234); + $I->assertTrue($validator->hasOption('min'), 'Min option does exists'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/MessageFactoryCest.php b/tests/integration/Validation/Validator/StringLength/Min/MessageFactoryCest.php new file mode 100644 index 00000000000..da0e5f6882d --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/MessageFactoryCest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Messages\Message; +use Phalcon\Messages\Messages; +use Phalcon\Validation; +use Phalcon\Validation\Validator\StringLength\Min; + +class MessageFactoryCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: messageFactory() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinMessageFactory(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - messageFactory()'); + + $validator = new Min([ + "min" => [ + "last_name" => 20, + ], + ]); + + $validation = new Validation(); + $validation->add( + [ + "last_name", + ], + $validator + ); + + $messages = $validation->validate(["last_name" => "A name too short"]); + + $I->assertInstanceOf(Messages::class, $messages, 'Failed validation instance of Messages\Messages'); + $I->assertEquals(1, $messages->count(), 'Has 1 Message'); + + $actual = $validator->messageFactory( + $validation, + "last_name", + [":min" => 10] + ); + + $expected = new Message( + 'Field last_name must be at least 10 characters long', + 'last_name', + Min::class + ); + + $I->assertInstanceOf(Message::class, $actual, 'Failed validation message instanceof Message'); + $I->assertEquals($expected, $actual, 'Failed validation message and factory message are the same'); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/SetOptionCest.php b/tests/integration/Validation/Validator/StringLength/Min/SetOptionCest.php new file mode 100644 index 00000000000..eb20e858d1f --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/SetOptionCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\ValidationTrait; +use Phalcon\Validation\Validator\StringLength; + +class SetOptionCest +{ + use ValidationTrait; + + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: setOption() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinSetOption(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - setOption()'); + + $validator = new StringLength(); + + $this->checkSetOption($I, $validator); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/SetTemplateCest.php b/tests/integration/Validation/Validator/StringLength/Min/SetTemplateCest.php new file mode 100644 index 00000000000..3dbb396092c --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/SetTemplateCest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Min; + +class SetTemplateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: setTemplate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinSetTemplate(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - setTemplate()'); + + $validator = new Min(); + + $expected = 'New custom template message'; + + $actual = $validator->setTemplate($expected); + + $I->assertInstanceOf(Min::class, $actual, 'Instance of Min'); + + $I->assertEquals( + $expected, + $validator->getTemplate(), + 'Get equals template message' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/SetTemplatesCest.php b/tests/integration/Validation/Validator/StringLength/Min/SetTemplatesCest.php new file mode 100644 index 00000000000..f31bf44b5c8 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/SetTemplatesCest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation\Validator\StringLength\Min; + +class SetTemplatesCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength\Min :: setTemplates() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthMinSetTemplates(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength\Min - setTemplates()'); + + $validator = new Min(); + + $expected = [ + 'key-1' => 'value-1', + 'key-2' => 'value-2', + 'key-3' => 'value-3', + ]; + + $actual = $validator->setTemplates($expected); + + $I->assertInstanceOf(Min::class, $actual, 'Instance of Min'); + + $I->assertEquals( + $expected, + $validator->getTemplates(), + 'Get equals templates' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/Min/ValidateCest.php b/tests/integration/Validation/Validator/StringLength/Min/ValidateCest.php new file mode 100644 index 00000000000..facad0a33ef --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/Min/ValidateCest.php @@ -0,0 +1,115 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Validation\Validator\StringLength\Min; + +use IntegrationTester; +use Phalcon\Validation; +use Phalcon\Validation\Validator\StringLength\Min; + +class ValidateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength :: validate() - single + * field + * + * @author Wojciech Ślawski + * @since 2016-06-05 + */ + public function validationValidatorMinStringLengthValidateSingleField(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength :: validate() - single field'); + + $validation = new Validation(); + + $validation->add( + 'name', + new Min( + [ + 'min' => 9, + ] + ) + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue', + ] + ); + + $I->assertEquals( + 0, + $messages->count() + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue123', + ] + ); + + $I->assertEquals( + 0, + $messages->count() + ); + } + + /** + * Tests Phalcon\Validation\Validator\StringLength :: validate() - single + * field + * + * @author Wojciech Ślawski + * @since 2016-06-05 + */ + public function validationValidatorMinOrEqualStringLengthValidateSingleField(IntegrationTester $I) + { + $I->wantToTest('Validation\Validator\StringLength :: validate() - single field'); + + $validation = new Validation(); + + $validation->add( + 'name', + new Min( + [ + 'min' => 9, + 'included' => true, + ] + ) + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue', + ] + ); + + $I->assertEquals( + 1, + $messages->count() + ); + + + $messages = $validation->validate( + [ + 'name' => 'SomeValue123', + ] + ); + + $I->assertEquals( + 0, + $messages->count() + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/SetOptionCest.php b/tests/integration/Validation/Validator/StringLength/SetOptionCest.php index 3cca8b8b08d..baf5803bf28 100644 --- a/tests/integration/Validation/Validator/StringLength/SetOptionCest.php +++ b/tests/integration/Validation/Validator/StringLength/SetOptionCest.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Phalcon\Test\Integration\Validation\Validator\StringLength; +namespace Phalcon\Test\Integration\Validation\Validator\StringLength2; use IntegrationTester; use Phalcon\Test\Fixtures\Traits\ValidationTrait; diff --git a/tests/integration/Validation/Validator/StringLength/SetTemplateCest.php b/tests/integration/Validation/Validator/StringLength/SetTemplateCest.php new file mode 100644 index 00000000000..c20c309918c --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/SetTemplateCest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength; + +use Phalcon\Validation\Validator\StringLength; +use UnitTester; + +class SetTemplateCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength :: setTemplate() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthSetTemplate(UnitTester $I) + { + $I->wantToTest('Validation\Validator\StringLength - setTemplate()'); + + $validator = new StringLength(); + + $expected = 'New custom template message'; + + $actual = $validator->setTemplate($expected); + + $I->assertInstanceOf(StringLength::class, $actual, 'Instance of StringLenght'); + + $I->assertEquals( + $expected, + $validator->getTemplate(), + 'Get equals template message' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/SetTemplatesCest.php b/tests/integration/Validation/Validator/StringLength/SetTemplatesCest.php new file mode 100644 index 00000000000..428975c3ae6 --- /dev/null +++ b/tests/integration/Validation/Validator/StringLength/SetTemplatesCest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Validation\Validator\StringLength; + +use Phalcon\Validation\Validator\StringLength; +use UnitTester; + +class SetTemplatesCest +{ + /** + * Tests Phalcon\Validation\Validator\StringLength :: setTemplates() + * + * @author Phalcon Team + * @since 2019-05-23 + */ + public function validationValidatorStringLengthSetTemplates(UnitTester $I) + { + $I->wantToTest('Validation\Validator\StringLength - setTemplates()'); + + $validator = new StringLength(); + + $expected = [ + 'key-1' => 'value-1', + 'key-2' => 'value-2', + 'key-3' => 'value-3', + ]; + + $actual = $validator->setTemplates($expected); + + $I->assertInstanceOf(StringLength::class, $actual, 'Instance of StringLenght'); + + $I->assertEquals( + $expected, + $validator->getTemplates(), + 'Get equals templates' + ); + } +} diff --git a/tests/integration/Validation/Validator/StringLength/ValidateCest.php b/tests/integration/Validation/Validator/StringLength/ValidateCest.php index 15a1647847e..0ac3beec889 100644 --- a/tests/integration/Validation/Validator/StringLength/ValidateCest.php +++ b/tests/integration/Validation/Validator/StringLength/ValidateCest.php @@ -17,6 +17,8 @@ use Phalcon\Messages\Messages; use Phalcon\Validation; use Phalcon\Validation\Validator\StringLength; +use Phalcon\Validation\Validator\StringLength\Max; +use Phalcon\Validation\Validator\StringLength\Min; /** * Class ValidateCest @@ -110,7 +112,7 @@ public function validationValidatorStringLengthValidateMinimum(IntegrationTester new Message( 'Field name must be at least 3 characters long', 'name', - 'TooShort', + Min::class, 0 ), ] @@ -168,7 +170,7 @@ public function validationValidatorStringLengthValidateMinimumWithCustomMessage( new Message( 'The message is too short', 'message', - 'TooShort', + Min::class, 0 ), ] @@ -222,7 +224,7 @@ public function validationValidatorStringLengthValidateMaximum(IntegrationTester new Message( 'Field name must not exceed 4 characters long', 'name', - 'TooLong', + Max::class, 0 ), ] @@ -280,7 +282,7 @@ public function validationValidatorStringLengthValidateMaximumWithCustomMessage( new Message( 'The message is too long', 'message', - 'TooLong', + Max::class, 0 ), ] diff --git a/tests/integration/Validation/Validator/UniquenessCest.php b/tests/integration/Validation/Validator/UniquenessCest.php index 321dd2a1c73..9f577999617 100644 --- a/tests/integration/Validation/Validator/UniquenessCest.php +++ b/tests/integration/Validation/Validator/UniquenessCest.php @@ -11,7 +11,6 @@ namespace Phalcon\Test\Integration\Validation\Validator; -use function date; use Exception; use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; @@ -19,6 +18,7 @@ use Phalcon\Test\Models\Some\Robotters; use Phalcon\Validation; use Phalcon\Validation\Validator\Uniqueness; +use function date; class UniquenessCest { @@ -277,7 +277,6 @@ public function testMultipleFieldsWithNull(IntegrationTester $I) $messages->count() ); - $messages = $validation->validate(null, $this->anotherRobot); $I->assertEquals( @@ -285,7 +284,6 @@ public function testMultipleFieldsWithNull(IntegrationTester $I) $messages->count() ); - $messages = $validation->validate(null, $this->deletedRobot); $I->assertEquals( diff --git a/tests/integration/Validation/Validator/Url/ValidateCest.php b/tests/integration/Validation/Validator/Url/ValidateCest.php index efbdb0490b2..cd00d5f4490 100644 --- a/tests/integration/Validation/Validator/Url/ValidateCest.php +++ b/tests/integration/Validation/Validator/Url/ValidateCest.php @@ -13,13 +13,13 @@ namespace Phalcon\Test\Integration\Validation\Validator\Url; use Codeception\Example; -use const FILTER_FLAG_PATH_REQUIRED; -use const FILTER_FLAG_QUERY_REQUIRED; use IntegrationTester; use Phalcon\Messages\Message; use Phalcon\Messages\Messages; use Phalcon\Validation; use Phalcon\Validation\Validator\Url; +use const FILTER_FLAG_PATH_REQUIRED; +use const FILTER_FLAG_QUERY_REQUIRED; /** * Class ValidateCest @@ -53,7 +53,7 @@ public function validationValidatorUrlSingleField(IntegrationTester $I) new Message( 'Field url must be a url', 'url', - 'Url', + Url::class, 0 ), ] @@ -194,7 +194,7 @@ public function validationValidatorUrlCustomMessage(IntegrationTester $I) new Message( 'The url is not valid', 'url', - 'Url', + Url::class, 0 ), ] @@ -263,7 +263,7 @@ public function validationValidatorUrlFlags(IntegrationTester $I, Example $examp new Message( 'Field url must be a url', 'url', - 'Url', + Url::class, 0 ), ]