TASK: Fix ValidationGroups

Fix handling of validation groups. Also added test to cover the feature.
This commit is contained in:
Daniel Siepmann 2017-07-03 10:11:18 +02:00
parent eccc2b1bdd
commit 3da68c2e49
No known key found for this signature in database
GPG key ID: 33D6629915560EF4
5 changed files with 110 additions and 17 deletions

View file

@ -133,7 +133,6 @@ class SettingsValidator extends AbstractValidator
if (is_null($validation)) { if (is_null($validation)) {
continue; continue;
} }
$this->extractValidationGroupsFromOptions($newValidation);
$newValidation['options'] = $validation; $newValidation['options'] = $validation;
$newValidation['validator'] = $validator; $newValidation['validator'] = $validator;
$config[] = $newValidation; $config[] = $newValidation;
@ -145,7 +144,6 @@ class SettingsValidator extends AbstractValidator
if (is_null($options)) { if (is_null($options)) {
continue; continue;
} }
$this->extractValidationGroupsFromOptions($options);
$newValidation['property'] = $propertyName; $newValidation['property'] = $propertyName;
$newValidation['validator'] = $validator; $newValidation['validator'] = $validator;
$newValidation['options'] = $options; $newValidation['options'] = $options;
@ -164,8 +162,8 @@ class SettingsValidator extends AbstractValidator
*/ */
protected function doesValidationGroupsMatch(array &$validatorConfig) protected function doesValidationGroupsMatch(array &$validatorConfig)
{ {
if (isset($validatorConfig['validationGroups']) if (isset($validatorConfig['options']['validationGroups'])
&& count(array_intersect($validatorConfig['validationGroups'], $this->options['validationGroups'])) === 0 && count(array_intersect($validatorConfig['options']['validationGroups'], $this->options['validationGroups'])) === 0
) { ) {
return false; return false;
} }
@ -180,19 +178,8 @@ class SettingsValidator extends AbstractValidator
*/ */
protected function handleValidationGroups(array &$validatorConfig) protected function handleValidationGroups(array &$validatorConfig)
{ {
if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings' && empty($validatorConfig['options']['validationGroups'])) { if ($validatorConfig['validator'] !== 'DigiComp.SettingValidator:Settings' || empty($validatorConfig['options']['validationGroups'])) {
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups']; unset($validatorConfig['options']['validationGroups']);
}
}
/**
* @param $newValidation
*/
protected function extractValidationGroupsFromOptions(&$newValidation)
{
if (isset($newValidation['options']['validationGroups'])) {
$newValidation['validationGroups'] = $newValidation['options']['validationGroups'];
unset($newValidation['options']['validationGroups']);
} }
} }
} }

View file

@ -12,3 +12,17 @@ TrueValidator:
self: self:
BooleanValue: BooleanValue:
expectedValue: true expectedValue: true
GroupValidatorDefault:
self:
BooleanValue:
expectedValue: true
validationGroups:
- Default
GroupValidatorCustom:
self:
BooleanValue:
expectedValue: false
validationGroups:
- Custom

View file

@ -0,0 +1,35 @@
<?php
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
use Neos\Flow\Annotations as Flow;
class TestValidationGroupsCustomObject
{
/**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorDefault", "validationGroups"={"Custom"}})
* @var bool
*/
protected $shouldBeTrue = false;
/**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorCustom", "validationGroups"={"Custom"}})
* @var bool
*/
protected $shouldBeFalse = true;
/**
* @return bool
*/
public function isShouldBeTrue(): bool
{
return $this->shouldBeTrue;
}
/**
* @return bool
*/
public function isShouldBeFalse(): bool
{
return $this->shouldBeFalse;
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
use Neos\Flow\Annotations as Flow;
class TestValidationGroupsDefaultObject
{
/**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorDefault"})
* @var bool
*/
protected $shouldBeTrue = false;
/**
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name": "GroupValidatorCustom"})
* @var bool
*/
protected $shouldBeFalse = true;
/**
* @return bool
*/
public function isShouldBeTrue(): bool
{
return $this->shouldBeTrue;
}
/**
* @return bool
*/
public function isShouldBeFalse(): bool
{
return $this->shouldBeFalse;
}
}

View file

@ -2,6 +2,8 @@
namespace DigiComp\SettingValidator\Tests\Functional; namespace DigiComp\SettingValidator\Tests\Functional;
use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestObject; use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestObject;
use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsCustomObject;
use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsDefaultObject;
use DigiComp\SettingValidator\Validation\Validator\SettingsValidator; use DigiComp\SettingValidator\Validation\Validator\SettingsValidator;
use Neos\Flow\Tests\FunctionalTestCase; use Neos\Flow\Tests\FunctionalTestCase;
use Neos\Flow\Validation\ValidatorResolver; use Neos\Flow\Validation\ValidatorResolver;
@ -31,4 +33,24 @@ class SettingsValidatorTest extends FunctionalTestCase
$this->assertTrue($result->hasErrors()); $this->assertTrue($result->hasErrors());
$this->assertCount(1, $result->getFlattenedErrors()); $this->assertCount(1, $result->getFlattenedErrors());
} }
/**
* @test
*/
public function validationGroupsAreRespected()
{
$validatorResolver = $this->objectManager->get(ValidatorResolver::class);
$validator = $validatorResolver->getBaseValidatorConjunction(TestValidationGroupsDefaultObject::class);
$result = $validator->validate(new TestValidationGroupsDefaultObject());
$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->forProperty('shouldBeTrue')->getErrors(), 'Got no error for shouldBeTrue property');
$validator = $validatorResolver->getBaseValidatorConjunction(TestValidationGroupsCustomObject::class);
$result = $validator->validate(new TestValidationGroupsCustomObject());
$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->forProperty('shouldBeFalse')->getErrors(), 'Got no error for shouldBeFalse property');
}
} }