BUGFIX: Fix broken recursion with validation groups

Provide current validation group for recursive SettingsValidator.
Adjust test.
Provide configuration to outline the issue.
This commit is contained in:
Daniel Siepmann 2017-07-19 12:54:29 +02:00
parent 5c927e9dda
commit 2b41d5f3db
No known key found for this signature in database
GPG key ID: 33D6629915560EF4
5 changed files with 33 additions and 24 deletions

View file

@ -179,12 +179,11 @@ class SettingsValidator extends AbstractValidator
protected function handleValidationGroups(array &$validatorConfig) protected function handleValidationGroups(array &$validatorConfig)
{ {
if (isset($validatorConfig['options']['validationGroups']) if (isset($validatorConfig['options']['validationGroups'])
&& ( && $validatorConfig['validator'] !== 'DigiComp.SettingValidator:Settings'
$validatorConfig['validator'] !== 'DigiComp.SettingValidator:Settings'
|| empty($validatorConfig['options']['validationGroups'])
)
) { ) {
unset($validatorConfig['options']['validationGroups']); unset($validatorConfig['options']['validationGroups']);
} elseif ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings') {
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
} }
} }
} }

View file

@ -13,16 +13,26 @@ TrueValidator:
BooleanValue: BooleanValue:
expectedValue: true expectedValue: true
GroupValidatorDefault: DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsCustomObject:
self: self:
BooleanValue: DigiComp.SettingValidator:Settings:
expectedValue: true name: GroupValidatorCustom
validationGroups:
- Default
GroupValidatorCustom:
self:
BooleanValue:
expectedValue: false
validationGroups:
- Custom
DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsDefaultObject:
self:
DigiComp.SettingValidator:Settings:
name: GroupValidatorDefault
GroupValidatorDefault:
properties:
shouldBeTrue:
BooleanValue:
expectedValue: true
GroupValidatorCustom:
properties:
shouldBeFalse:
BooleanValue:
expectedValue: false
validationGroups:
- Custom

View file

@ -6,13 +6,11 @@ use Neos\Flow\Annotations as Flow;
class TestValidationGroupsCustomObject class TestValidationGroupsCustomObject
{ {
/** /**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorDefault", "validationGroups"={"Custom"}})
* @var bool * @var bool
*/ */
protected $shouldBeTrue = false; protected $shouldBeTrue = false;
/** /**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorCustom", "validationGroups"={"Custom"}})
* @var bool * @var bool
*/ */
protected $shouldBeFalse = true; protected $shouldBeFalse = true;

View file

@ -6,13 +6,11 @@ use Neos\Flow\Annotations as Flow;
class TestValidationGroupsDefaultObject class TestValidationGroupsDefaultObject
{ {
/** /**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorDefault"})
* @var bool * @var bool
*/ */
protected $shouldBeTrue = false; protected $shouldBeTrue = false;
/** /**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorCustom"})
* @var bool * @var bool
*/ */
protected $shouldBeFalse = true; protected $shouldBeFalse = true;

View file

@ -37,17 +37,21 @@ class SettingsValidatorTest extends FunctionalTestCase
/** /**
* @test * @test
*/ */
public function validationGroupsAreRespected() public function defaultValidationGroupWorks()
{ {
$validatorResolver = $this->objectManager->get(ValidatorResolver::class); $validator = $this->objectManager->get(SettingsValidator::class, ['validationGroups' => ['Default']]);
$validator = $validatorResolver->getBaseValidatorConjunction(TestValidationGroupsDefaultObject::class);
$result = $validator->validate(new TestValidationGroupsDefaultObject()); $result = $validator->validate(new TestValidationGroupsDefaultObject());
$this->assertTrue($result->hasErrors(), 'No Errors for validation group "Default"'); $this->assertTrue($result->hasErrors(), 'No Errors for validation group "Default"');
$this->assertCount(1, $result->getFlattenedErrors(), 'Got a non expected number of errors for group "Default"'); $this->assertCount(1, $result->getFlattenedErrors(), 'Got a non expected number of errors for group "Default"');
$this->assertCount(1, $result->forProperty('shouldBeTrue')->getErrors(), 'Got no error for shouldBeTrue property'); $this->assertCount(1, $result->forProperty('shouldBeTrue')->getErrors(), 'Got no error for shouldBeTrue property');
}
$validator = $validatorResolver->getBaseValidatorConjunction(TestValidationGroupsCustomObject::class); /**
* @test
*/
public function customValidationGroupWorks()
{
$validator = $this->objectManager->get(SettingsValidator::class, ['validationGroups' => ['Custom']]);
$result = $validator->validate(new TestValidationGroupsCustomObject()); $result = $validator->validate(new TestValidationGroupsCustomObject());
$this->assertTrue($result->hasErrors(), 'No Errors for validation group "Custom"'); $this->assertTrue($result->hasErrors(), 'No Errors for validation group "Custom"');
$this->assertCount(1, $result->getFlattenedErrors(), 'Got a non expected number of errors for group "Custom"'); $this->assertCount(1, $result->getFlattenedErrors(), 'Got a non expected number of errors for group "Custom"');