From 3da68c2e490e71edd206bb491ee8bb73b89eb490 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 3 Jul 2017 10:11:18 +0200 Subject: [PATCH] TASK: Fix ValidationGroups Fix handling of validation groups. Also added test to cover the feature. --- .../Validator/SettingsValidator.php | 21 +++-------- Configuration/Testing/Validation.yaml | 14 ++++++++ .../TestValidationGroupsCustomObject.php | 35 +++++++++++++++++++ .../TestValidationGroupsDefaultObject.php | 35 +++++++++++++++++++ Tests/Functional/SettingsValidatorTest.php | 22 ++++++++++++ 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 Tests/Functional/Fixtures/TestValidationGroupsCustomObject.php create mode 100644 Tests/Functional/Fixtures/TestValidationGroupsDefaultObject.php diff --git a/Classes/Validation/Validator/SettingsValidator.php b/Classes/Validation/Validator/SettingsValidator.php index 3752ed7..3f1bb0d 100644 --- a/Classes/Validation/Validator/SettingsValidator.php +++ b/Classes/Validation/Validator/SettingsValidator.php @@ -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']); } } } diff --git a/Configuration/Testing/Validation.yaml b/Configuration/Testing/Validation.yaml index bc17503..2ae4032 100644 --- a/Configuration/Testing/Validation.yaml +++ b/Configuration/Testing/Validation.yaml @@ -12,3 +12,17 @@ TrueValidator: self: BooleanValue: expectedValue: true + +GroupValidatorDefault: + self: + BooleanValue: + expectedValue: true + validationGroups: + - Default +GroupValidatorCustom: + self: + BooleanValue: + expectedValue: false + validationGroups: + - Custom + diff --git a/Tests/Functional/Fixtures/TestValidationGroupsCustomObject.php b/Tests/Functional/Fixtures/TestValidationGroupsCustomObject.php new file mode 100644 index 0000000..78606ab --- /dev/null +++ b/Tests/Functional/Fixtures/TestValidationGroupsCustomObject.php @@ -0,0 +1,35 @@ +shouldBeTrue; + } + + /** + * @return bool + */ + public function isShouldBeFalse(): bool + { + return $this->shouldBeFalse; + } +} diff --git a/Tests/Functional/Fixtures/TestValidationGroupsDefaultObject.php b/Tests/Functional/Fixtures/TestValidationGroupsDefaultObject.php new file mode 100644 index 0000000..9afe2c1 --- /dev/null +++ b/Tests/Functional/Fixtures/TestValidationGroupsDefaultObject.php @@ -0,0 +1,35 @@ +shouldBeTrue; + } + + /** + * @return bool + */ + public function isShouldBeFalse(): bool + { + return $this->shouldBeFalse; + } +} diff --git a/Tests/Functional/SettingsValidatorTest.php b/Tests/Functional/SettingsValidatorTest.php index 8feae6d..2b5493f 100644 --- a/Tests/Functional/SettingsValidatorTest.php +++ b/Tests/Functional/SettingsValidatorTest.php @@ -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'); + } }