Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phalcon\Validation\Validator\Alpha now correctly validates non-ASCII characters #11873

Merged
merged 1 commit into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@
- Added `Phalcon\Security::hasLibreSsl` and `Phalcon\Security::getSslVersionNumber`
- Added new setter `Phalcon\Escaper::setDoubleEncode()` - to allow setting/disabling double encoding
- Added `Phalcon\Cache\Frontend\Msgpack` - Added Msgpack Support for Frontend Cache
- `Phalcon\Debug\Dump` skip debuging di, fix detecting private/protected properties
- `Phalcon\Debug\Dump` skip debugging di, fix detecting private/protected properties
- Added option to validate multiple fields with one validator(fix uniqueness validator as well), also removes unnecessary `model => $this` in `Phalcon\Validation\Validator\Uniqueness`.
- `Phalcon\Validation\Validator\Alpha` now correctly validates non-ASCII characters

# [2.0.11](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.11) (????-??-??)
- Fix Model magic set functionality to maintain variable visibility and utilize setter methods.[#11286](https://github.com/phalcon/cphalcon/issues/11286)
Expand Down
3 changes: 1 addition & 2 deletions phalcon/validation/validator/alpha.zep
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ use Phalcon\Validation\Validator;
*/
class Alpha extends Validator
{

/**
* Executes the validation
*/
Expand All @@ -55,7 +54,7 @@ class Alpha extends Validator

let value = validation->getValue(field);

if !ctype_alpha(value) {
if preg_match("/[^[:alpha:]]/imu", value) {

let label = this->getOption("label");
if typeof label == "array" {
Expand Down
31 changes: 31 additions & 0 deletions tests/_proxies/Validation/Validator/Alpha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Phalcon\Test\Proxy\Validation\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Validator\Alpha as PhAlpha;

/**
* \Phalcon\Test\Proxy\Validation\Validator\Alpha
* A proxy class for \Phalcon\Validation\Validator\Alpha
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Andres Gutierrez <andres@phalconphp.com>
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @package Phalcon\Test\Proxy\Validation\Validator
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to license@phalconphp.com
* so that we can send you a copy immediately.
*/
class Alpha extends PhAlpha
{
public function validate(Validation $validation, $field)
{
return parent::validate($validation, $field);
}
}
115 changes: 112 additions & 3 deletions tests/unit/Validation/Validator/AlphaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Phalcon\Test\Unit\Validation\Validator;

use Phalcon\Test\Module\UnitTest;
use UnitTester;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Alpha;
use Codeception\Specify;
use Phalcon\Test\Module\UnitTest;
use Phalcon\Validation\Message;
use Phalcon\Validation\Message\Group;
use Phalcon\Test\Proxy\Validation\Validator\Alpha;

/**
* \Phalcon\Test\Unit\Validation\Validator\Alpha
* \Phalcon\Test\Unit\Validation\Validator\AlphaTest
* Tests the \Phalcon\Validation\Validator\Alpha component
*
* @copyright (c) 2011-2016 Phalcon Team
Expand All @@ -16,6 +20,7 @@
* @author Nikolaos Dimopoulos <nikos@phalconphp.com>
* @author Wojciech Ślawski <jurigag@gmail.com>
* @package Phalcon\Test\Unit\Validation\Validator
* @group validation
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
Expand Down Expand Up @@ -58,6 +63,7 @@ public function testMultipleField()
$validation->add(['name', 'type'], new Alpha([
'message' => $validationMessages,
]));

$messages = $validation->validate(['name' => 'Asd', 'type' => 'Asd']);
expect($messages->count())->equals(0);
$messages = $validation->validate(['name' => 'Asd123', 'type' => 'Asd']);
Expand All @@ -68,4 +74,107 @@ public function testMultipleField()
expect($messages->offsetGet(0)->getMessage())->equals($validationMessages['name']);
expect($messages->offsetGet(1)->getMessage())->equals($validationMessages['type']);
}

/**
* Tests Non Alphabetic Characters
*
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @since 2016-06-10
*/
public function testShouldDetectNonAlphabeticCharacters()
{
$this->specify(
"The Alpha Validator does not detect non alphabetic characters",
function ($input) {
$validation = new Validation;
$validation->add('name', new Alpha([
'message' => ':field must contain only letters'
]));

$messages = $validation->validate(['name' => $input]);

$expectedMessages = Group::__set_state([
'_messages' => [
Message::__set_state([
'_type' => 'Alpha',
'_message' => 'name must contain only letters',
'_field' => 'name',
'_code' => '0',
])
],
]);

expect($messages)->equals($expectedMessages);
}, ['examples' => [
['1'],
[123],
['a-b-c-d'],
['a-1-c-2'],
['a1c2'],
['o0o0o0o0'],
]]
);
}

/**
* Tests Alphabetic Characters
*
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @since 2016-06-10
*/
public function testShouldValidateAlphabeticCharacters()
{
$this->specify(
"The Alpha Validator does not validate alphabetic characters",
function ($input) {
$validation = new Validation;
$validation->add('name', new Alpha([
'message' => ':field must contain only letters'
]));

$messages = $validation->validate(['name' => $input]);

expect($messages)->count(0);
}, ['examples' => [
['a'],
['asdavafaiwnoabwiubafpowf'],
['QWERTYUIOPASDFGHJKL'],
['aSdFgHjKl'],
[null],
]]
);
}

/**
* Tests Non Latin Characters
*
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @since 2016-06-10
*/
public function testShouldValidateNonLatinCharacters()
{
$this->specify(
"The Alpha Validator does not validate alphabetic characters",
function ($input) {
$validation = new Validation;
$validation->add('name', new Alpha([
'message' => ':field must contain only letters'
]));

$messages = $validation->validate(['name' => $input]);

expect($messages)->count(0);
}, ['examples' => [
['йцукенг'],
['ждлорпа'],
['Señor'],
['cocoñùт'],
['COCOÑÙТ'],
['JÄGER'],
['šš'],
['あいうえお'],
['零一二三四五'],
]]
);
}
}