[Validator] Backported constraint validator tests from 2.5
This commit is contained in:
parent
054f1b5536
commit
845c33cfaa
@ -11,77 +11,96 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
|
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||||
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
||||||
|
use Symfony\Component\Security\Core\SecurityContextInterface;
|
||||||
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
|
||||||
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
|
||||||
|
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
|
||||||
|
|
||||||
class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
|
||||||
{
|
{
|
||||||
const PASSWORD_VALID = true;
|
const PASSWORD = 's3Cr3t';
|
||||||
const PASSWORD_INVALID = false;
|
|
||||||
|
|
||||||
protected $context;
|
const SALT = '^S4lt$';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var SecurityContextInterface
|
||||||
|
*/
|
||||||
|
protected $securityContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var PasswordEncoderInterface
|
||||||
|
*/
|
||||||
|
protected $encoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var EncoderFactoryInterface
|
||||||
|
*/
|
||||||
|
protected $encoderFactory;
|
||||||
|
|
||||||
|
protected function createValidator()
|
||||||
|
{
|
||||||
|
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
|
||||||
|
}
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
$user = $this->createUser();
|
||||||
}
|
$this->securityContext = $this->createSecurityContext($user);
|
||||||
|
$this->encoder = $this->createPasswordEncoder();
|
||||||
|
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
|
||||||
|
|
||||||
protected function tearDown()
|
parent::setUp();
|
||||||
{
|
|
||||||
$this->context = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPasswordIsValid()
|
public function testPasswordIsValid()
|
||||||
{
|
{
|
||||||
$user = $this->createUser();
|
$constraint = new UserPassword(array(
|
||||||
$securityContext = $this->createSecurityContext($user);
|
'message' => 'myMessage',
|
||||||
|
));
|
||||||
|
|
||||||
$encoder = $this->createPasswordEncoder(static::PASSWORD_VALID);
|
$this->encoder->expects($this->once())
|
||||||
$encoderFactory = $this->createEncoderFactory($encoder);
|
->method('isPasswordValid')
|
||||||
|
->with(static::PASSWORD, 'secret', static::SALT)
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
|
$this->validator->validate('secret', $constraint);
|
||||||
$validator->initialize($this->context);
|
|
||||||
|
|
||||||
$this
|
$this->assertNoViolation();
|
||||||
->context
|
|
||||||
->expects($this->never())
|
|
||||||
->method('addViolation')
|
|
||||||
;
|
|
||||||
|
|
||||||
$validator->validate('secret', new UserPassword());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPasswordIsNotValid()
|
public function testPasswordIsNotValid()
|
||||||
{
|
{
|
||||||
$user = $this->createUser();
|
$constraint = new UserPassword(array(
|
||||||
$securityContext = $this->createSecurityContext($user);
|
'message' => 'myMessage',
|
||||||
|
));
|
||||||
|
|
||||||
$encoder = $this->createPasswordEncoder(static::PASSWORD_INVALID);
|
$this->encoder->expects($this->once())
|
||||||
$encoderFactory = $this->createEncoderFactory($encoder);
|
->method('isPasswordValid')
|
||||||
|
->with(static::PASSWORD, 'secret', static::SALT)
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
|
$this->validator->validate('secret', $constraint);
|
||||||
$validator->initialize($this->context);
|
|
||||||
|
|
||||||
$this
|
$this->assertViolation('myMessage');
|
||||||
->context
|
|
||||||
->expects($this->once())
|
|
||||||
->method('addViolation')
|
|
||||||
;
|
|
||||||
|
|
||||||
$validator->validate('secret', new UserPassword());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||||
|
*/
|
||||||
public function testUserIsNotValid()
|
public function testUserIsNotValid()
|
||||||
{
|
{
|
||||||
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
|
||||||
|
|
||||||
$user = $this->getMock('Foo\Bar\User');
|
$user = $this->getMock('Foo\Bar\User');
|
||||||
$encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
|
|
||||||
$securityContext = $this->createSecurityContext($user);
|
|
||||||
|
|
||||||
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
|
$this->securityContext = $this->createSecurityContext($user);
|
||||||
$validator->initialize($this->context);
|
$this->validator = $this->createValidator();
|
||||||
$validator->validate('secret', new UserPassword());
|
$this->validator->initialize($this->context);
|
||||||
|
|
||||||
|
$this->validator->validate('secret', new UserPassword());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createUser()
|
protected function createUser()
|
||||||
@ -89,15 +108,15 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
||||||
|
|
||||||
$mock
|
$mock
|
||||||
->expects($this->once())
|
->expects($this->any())
|
||||||
->method('getPassword')
|
->method('getPassword')
|
||||||
->will($this->returnValue('s3Cr3t'))
|
->will($this->returnValue(static::PASSWORD))
|
||||||
;
|
;
|
||||||
|
|
||||||
$mock
|
$mock
|
||||||
->expects($this->once())
|
->expects($this->any())
|
||||||
->method('getSalt')
|
->method('getSalt')
|
||||||
->will($this->returnValue('^S4lt$'))
|
->will($this->returnValue(static::SALT))
|
||||||
;
|
;
|
||||||
|
|
||||||
return $mock;
|
return $mock;
|
||||||
@ -105,15 +124,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
protected function createPasswordEncoder($isPasswordValid = true)
|
protected function createPasswordEncoder($isPasswordValid = true)
|
||||||
{
|
{
|
||||||
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
|
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
|
||||||
|
|
||||||
$mock
|
|
||||||
->expects($this->once())
|
|
||||||
->method('isPasswordValid')
|
|
||||||
->will($this->returnValue($isPasswordValid))
|
|
||||||
;
|
|
||||||
|
|
||||||
return $mock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createEncoderFactory($encoder = null)
|
protected function createEncoderFactory($encoder = null)
|
||||||
@ -121,7 +132,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
|
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
|
||||||
|
|
||||||
$mock
|
$mock
|
||||||
->expects($this->once())
|
->expects($this->any())
|
||||||
->method('getEncoder')
|
->method('getEncoder')
|
||||||
->will($this->returnValue($encoder))
|
->will($this->returnValue($encoder))
|
||||||
;
|
;
|
||||||
@ -135,7 +146,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
||||||
$mock
|
$mock
|
||||||
->expects($this->once())
|
->expects($this->any())
|
||||||
->method('getToken')
|
->method('getToken')
|
||||||
->will($this->returnValue($token))
|
->will($this->returnValue($token))
|
||||||
;
|
;
|
||||||
@ -147,7 +158,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||||
$mock
|
$mock
|
||||||
->expects($this->once())
|
->expects($this->any())
|
||||||
->method('getUser')
|
->method('getUser')
|
||||||
->will($this->returnValue($user))
|
->will($this->returnValue($user))
|
||||||
;
|
;
|
||||||
|
@ -51,6 +51,7 @@ class ExpressionValidator extends ConstraintValidator
|
|||||||
$variables = array();
|
$variables = array();
|
||||||
|
|
||||||
if (null === $this->context->getPropertyName()) {
|
if (null === $this->context->getPropertyName()) {
|
||||||
|
$variables['value'] = $value;
|
||||||
$variables['this'] = $value;
|
$variables['this'] = $value;
|
||||||
} else {
|
} else {
|
||||||
// Extract the object that the property belongs to from the object
|
// Extract the object that the property belongs to from the object
|
||||||
|
@ -14,184 +14,170 @@ namespace Symfony\Component\Validator\Tests\Constraints;
|
|||||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||||
use Symfony\Component\Validator\Constraints\Expression;
|
use Symfony\Component\Validator\Constraints\Expression;
|
||||||
use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
||||||
|
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||||
|
|
||||||
class ExpressionValidatorTest extends \PHPUnit_Framework_TestCase
|
class ExpressionValidatorTest extends AbstractConstraintValidatorTest
|
||||||
{
|
{
|
||||||
protected $context;
|
protected function createValidator()
|
||||||
protected $validator;
|
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
{
|
||||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
return new ExpressionValidator(PropertyAccess::createPropertyAccessor());
|
||||||
$this->validator = new ExpressionValidator(PropertyAccess::createPropertyAccessor());
|
|
||||||
$this->validator->initialize($this->context);
|
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getClassName')
|
|
||||||
->will($this->returnValue(__CLASS__));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function tearDown()
|
|
||||||
{
|
|
||||||
$this->context = null;
|
|
||||||
$this->validator = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNullIsValid()
|
public function testNullIsValid()
|
||||||
{
|
{
|
||||||
$this->context->expects($this->never())
|
|
||||||
->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate(null, new Expression('value == 1'));
|
$this->validator->validate(null, new Expression('value == 1'));
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmptyStringIsValid()
|
public function testEmptyStringIsValid()
|
||||||
{
|
{
|
||||||
$this->context->expects($this->never())
|
|
||||||
->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate('', new Expression('value == 1'));
|
$this->validator->validate('', new Expression('value == 1'));
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSucceedingExpressionAtObjectLevel()
|
public function testSucceedingExpressionAtObjectLevel()
|
||||||
{
|
{
|
||||||
$constraint = new Expression('this.property == 1');
|
$constraint = new Expression('this.data == 1');
|
||||||
|
|
||||||
$object = (object) array('property' => '1');
|
$object = new Entity();
|
||||||
|
$object->data = '1';
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$this->setObject($object);
|
||||||
->method('getPropertyName')
|
|
||||||
->will($this->returnValue(null));
|
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
|
||||||
->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate($object, $constraint);
|
$this->validator->validate($object, $constraint);
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFailingExpressionAtObjectLevel()
|
public function testFailingExpressionAtObjectLevel()
|
||||||
{
|
{
|
||||||
$constraint = new Expression(array(
|
$constraint = new Expression(array(
|
||||||
'expression' => 'this.property == 1',
|
'expression' => 'this.data == 1',
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
));
|
));
|
||||||
|
|
||||||
$object = (object) array('property' => '2');
|
$object = new Entity();
|
||||||
|
$object->data = '2';
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$this->setObject($object);
|
||||||
->method('getPropertyName')
|
|
||||||
->will($this->returnValue(null));
|
|
||||||
|
|
||||||
$this->context->expects($this->once())
|
|
||||||
->method('addViolation')
|
|
||||||
->with('myMessage');
|
|
||||||
|
|
||||||
$this->validator->validate($object, $constraint);
|
$this->validator->validate($object, $constraint);
|
||||||
|
|
||||||
|
$this->assertViolation('myMessage');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSucceedingExpressionAtPropertyLevel()
|
public function testSucceedingExpressionAtPropertyLevel()
|
||||||
{
|
{
|
||||||
$constraint = new Expression('value == this.expected');
|
$constraint = new Expression('value == this.data');
|
||||||
|
|
||||||
$object = (object) array('expected' => '1');
|
$object = new Entity();
|
||||||
|
$object->data = '1';
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$this->setRoot($object);
|
||||||
->method('getPropertyName')
|
$this->setPropertyPath('data');
|
||||||
->will($this->returnValue('property'));
|
$this->setProperty($object, 'data');
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getPropertyPath')
|
|
||||||
->will($this->returnValue('property'));
|
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getRoot')
|
|
||||||
->will($this->returnValue($object));
|
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
|
||||||
->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate('1', $constraint);
|
$this->validator->validate('1', $constraint);
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFailingExpressionAtPropertyLevel()
|
public function testFailingExpressionAtPropertyLevel()
|
||||||
{
|
{
|
||||||
$constraint = new Expression(array(
|
$constraint = new Expression(array(
|
||||||
'expression' => 'value == this.expected',
|
'expression' => 'value == this.data',
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
));
|
));
|
||||||
|
|
||||||
$object = (object) array('expected' => '1');
|
$object = new Entity();
|
||||||
|
$object->data = '1';
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$this->setRoot($object);
|
||||||
->method('getPropertyName')
|
$this->setPropertyPath('data');
|
||||||
->will($this->returnValue('property'));
|
$this->setProperty($object, 'data');
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getPropertyPath')
|
|
||||||
->will($this->returnValue('property'));
|
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getRoot')
|
|
||||||
->will($this->returnValue($object));
|
|
||||||
|
|
||||||
$this->context->expects($this->once())
|
|
||||||
->method('addViolation')
|
|
||||||
->with('myMessage');
|
|
||||||
|
|
||||||
$this->validator->validate('2', $constraint);
|
$this->validator->validate('2', $constraint);
|
||||||
|
|
||||||
|
$this->assertViolation('myMessage', array(), 'data');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSucceedingExpressionAtNestedPropertyLevel()
|
public function testSucceedingExpressionAtNestedPropertyLevel()
|
||||||
{
|
{
|
||||||
$constraint = new Expression('value == this.expected');
|
$constraint = new Expression('value == this.data');
|
||||||
|
|
||||||
$object = (object) array('expected' => '1');
|
$object = new Entity();
|
||||||
$root = (object) array('nested' => $object);
|
$object->data = '1';
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$root = new Entity();
|
||||||
->method('getPropertyName')
|
$root->reference = $object;
|
||||||
->will($this->returnValue('property'));
|
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$this->setRoot($root);
|
||||||
->method('getPropertyPath')
|
$this->setPropertyPath('reference.data');
|
||||||
->will($this->returnValue('nested.property'));
|
$this->setProperty($object, 'data');
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getRoot')
|
|
||||||
->will($this->returnValue($root));
|
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
|
||||||
->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate('1', $constraint);
|
$this->validator->validate('1', $constraint);
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFailingExpressionAtNestedPropertyLevel()
|
public function testFailingExpressionAtNestedPropertyLevel()
|
||||||
{
|
{
|
||||||
$constraint = new Expression(array(
|
$constraint = new Expression(array(
|
||||||
'expression' => 'value == this.expected',
|
'expression' => 'value == this.data',
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
));
|
));
|
||||||
|
|
||||||
$object = (object) array('expected' => '1');
|
$object = new Entity();
|
||||||
$root = (object) array('nested' => $object);
|
$object->data = '1';
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$root = new Entity();
|
||||||
->method('getPropertyName')
|
$root->reference = $object;
|
||||||
->will($this->returnValue('property'));
|
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
$this->setRoot($root);
|
||||||
->method('getPropertyPath')
|
$this->setPropertyPath('reference.data');
|
||||||
->will($this->returnValue('nested.property'));
|
$this->setProperty($object, 'data');
|
||||||
|
|
||||||
$this->context->expects($this->any())
|
|
||||||
->method('getRoot')
|
|
||||||
->will($this->returnValue($root));
|
|
||||||
|
|
||||||
$this->context->expects($this->once())
|
|
||||||
->method('addViolation')
|
|
||||||
->with('myMessage');
|
|
||||||
|
|
||||||
$this->validator->validate('2', $constraint);
|
$this->validator->validate('2', $constraint);
|
||||||
|
|
||||||
|
$this->assertViolation('myMessage', array(), 'reference.data');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When validatePropertyValue() is called with a class name
|
||||||
|
* https://github.com/symfony/symfony/pull/11498
|
||||||
|
*/
|
||||||
|
public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
|
||||||
|
{
|
||||||
|
$constraint = new Expression('value == "1"');
|
||||||
|
|
||||||
|
$this->setRoot('1');
|
||||||
|
$this->setPropertyPath('');
|
||||||
|
$this->setProperty(null, 'property');
|
||||||
|
|
||||||
|
$this->validator->validate('1', $constraint);
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When validatePropertyValue() is called with a class name
|
||||||
|
* https://github.com/symfony/symfony/pull/11498
|
||||||
|
*/
|
||||||
|
public function testFailingExpressionAtPropertyLevelWithoutRoot()
|
||||||
|
{
|
||||||
|
$constraint = new Expression(array(
|
||||||
|
'expression' => 'value == "1"',
|
||||||
|
'message' => 'myMessage',
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->setRoot('2');
|
||||||
|
$this->setPropertyPath('');
|
||||||
|
$this->setProperty(null, 'property');
|
||||||
|
|
||||||
|
$this->validator->validate('2', $constraint);
|
||||||
|
|
||||||
|
$this->assertViolation('myMessage', array(), '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user