TASK: Fix ValidationGroups
Fix handling of validation groups. Also added test to cover the feature.
This commit is contained in:
parent
eccc2b1bdd
commit
3da68c2e49
5 changed files with 110 additions and 17 deletions
|
@ -133,7 +133,6 @@ class SettingsValidator extends AbstractValidator
|
|||
if (is_null($validation)) {
|
||||
continue;
|
||||
}
|
||||
$this->extractValidationGroupsFromOptions($newValidation);
|
||||
$newValidation['options'] = $validation;
|
||||
$newValidation['validator'] = $validator;
|
||||
$config[] = $newValidation;
|
||||
|
@ -145,7 +144,6 @@ class SettingsValidator extends AbstractValidator
|
|||
if (is_null($options)) {
|
||||
continue;
|
||||
}
|
||||
$this->extractValidationGroupsFromOptions($options);
|
||||
$newValidation['property'] = $propertyName;
|
||||
$newValidation['validator'] = $validator;
|
||||
$newValidation['options'] = $options;
|
||||
|
@ -164,8 +162,8 @@ class SettingsValidator extends AbstractValidator
|
|||
*/
|
||||
protected function doesValidationGroupsMatch(array &$validatorConfig)
|
||||
{
|
||||
if (isset($validatorConfig['validationGroups'])
|
||||
&& count(array_intersect($validatorConfig['validationGroups'], $this->options['validationGroups'])) === 0
|
||||
if (isset($validatorConfig['options']['validationGroups'])
|
||||
&& count(array_intersect($validatorConfig['options']['validationGroups'], $this->options['validationGroups'])) === 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
@ -180,19 +178,8 @@ class SettingsValidator extends AbstractValidator
|
|||
*/
|
||||
protected function handleValidationGroups(array &$validatorConfig)
|
||||
{
|
||||
if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings' && empty($validatorConfig['options']['validationGroups'])) {
|
||||
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $newValidation
|
||||
*/
|
||||
protected function extractValidationGroupsFromOptions(&$newValidation)
|
||||
{
|
||||
if (isset($newValidation['options']['validationGroups'])) {
|
||||
$newValidation['validationGroups'] = $newValidation['options']['validationGroups'];
|
||||
unset($newValidation['options']['validationGroups']);
|
||||
if ($validatorConfig['validator'] !== 'DigiComp.SettingValidator:Settings' || empty($validatorConfig['options']['validationGroups'])) {
|
||||
unset($validatorConfig['options']['validationGroups']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,3 +12,17 @@ TrueValidator:
|
|||
self:
|
||||
BooleanValue:
|
||||
expectedValue: true
|
||||
|
||||
GroupValidatorDefault:
|
||||
self:
|
||||
BooleanValue:
|
||||
expectedValue: true
|
||||
validationGroups:
|
||||
- Default
|
||||
GroupValidatorCustom:
|
||||
self:
|
||||
BooleanValue:
|
||||
expectedValue: false
|
||||
validationGroups:
|
||||
- Custom
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@
|
|||
namespace DigiComp\SettingValidator\Tests\Functional;
|
||||
|
||||
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 Neos\Flow\Tests\FunctionalTestCase;
|
||||
use Neos\Flow\Validation\ValidatorResolver;
|
||||
|
@ -31,4 +33,24 @@ class SettingsValidatorTest extends FunctionalTestCase
|
|||
$this->assertTrue($result->hasErrors());
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue