Merge branch 'release/3.0.0'

This commit is contained in:
Merten Koch 2017-08-28 16:29:54 +02:00
commit 3ba6b33f0a
12 changed files with 540 additions and 143 deletions

View file

@ -1,33 +0,0 @@
<?php
namespace DigiComp\SettingValidator;
use Doctrine\ORM\Mapping as ORM;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Configuration\ConfigurationManager;
use TYPO3\Flow\Core\Bootstrap;
use \TYPO3\Flow\Package\Package as BasePackage;
/**
* @Flow\Scope("prototype")
*/
class Package extends BasePackage
{
public function boot(Bootstrap $bootstrap)
{
parent::boot($bootstrap);
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(
'TYPO3\Flow\Configuration\ConfigurationManager',
'configurationManagerReady',
function (ConfigurationManager $configurationManager) {
$configurationManager->registerConfigurationType(
'Validation',
ConfigurationManager::CONFIGURATION_PROCESSING_TYPE_DEFAULT,
true
);
}
);
}
}

View file

@ -1,96 +0,0 @@
<?php
namespace DigiComp\SettingValidator\Validation\Validator;
use Doctrine\ORM\Mapping as ORM;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Configuration\ConfigurationManager;
use TYPO3\Flow\Reflection\ObjectAccess;
use TYPO3\Flow\Reflection\ReflectionService;
use TYPO3\Flow\Validation\Exception\InvalidValidationConfigurationException;
use TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException;
use TYPO3\Flow\Validation\Validator\AbstractValidator;
use TYPO3\Flow\Validation\ValidatorResolver;
/**
* Validator resolving other Validators defined in Validation.yaml
*
* @Flow\Scope("prototype")
*/
class SettingsValidator extends AbstractValidator
{
/**
* @var ValidatorResolver
* @Flow\Inject
*/
protected $validatorResolver;
/**
* @var array
*/
protected $validations;
/**
* @var \TYPO3\Flow\Configuration\ConfigurationManager
*/
protected $configurationManager;
public function injectConfigurationManager(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
$this->validations = $this->configurationManager->getConfiguration('Validation');
}
/**
* @var ReflectionService
* @Flow\Inject
*/
protected $reflectionService;
protected $supportedOptions = array(
'name' => array('', 'Set the name of the setting-array to use', 'string', false)
);
/**
* Check if $value is valid. If it is not valid, needs to add an error
* to Result.
*
* @param mixed $value
*
* @throws InvalidValidationOptionsException
* @throws InvalidValidationConfigurationException
*/
protected function isValid($value)
{
$name = $this->options['name'] ? $this->options['name'] : $this->reflectionService->getClassNameByObject(
$value
);
if (!isset($this->validations[$name])) {
throw new InvalidValidationOptionsException(
'The name ' . $name . ' has not been defined in Validation.yaml!',
1397821438
);
}
$config = &$this->validations[$name];
foreach ($config as $validatorConfig) {
$validator = $this->validatorResolver->createValidator(
$validatorConfig['validator'],
$validatorConfig['options']
);
if (!$validator) {
throw new InvalidValidationConfigurationException(
'Validator could not be resolved: ' .
$validatorConfig['validator'] . '. Check your Validation.yaml',
1402326139
);
}
if (isset($validatorConfig['property'])) {
$this->result->forProperty($validatorConfig['property'])->merge(
$validator->validate(ObjectAccess::getPropertyPath($value, $validatorConfig['property']))
);
} else {
$this->result->merge($validator->validate($value));
}
}
}
}

44
Classes/Package.php Normal file
View file

@ -0,0 +1,44 @@
<?php
namespace DigiComp\SettingValidator;
/*
* This file is part of the DigiComp.SettingValidator package.
*
* (c) digital competence
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Package\Package as BasePackage;
/**
* Package base class of the DigiComp.SettingValidator package.
*/
class Package extends BasePackage
{
const CONFIGURATION_TYPE_VALIDATION = 'Validation';
/**
* @param Bootstrap $bootstrap
*/
public function boot(Bootstrap $bootstrap)
{
parent::boot($bootstrap);
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(ConfigurationManager::class, 'configurationManagerReady',
function ($configurationManager) {
/** @var ConfigurationManager $configurationManager */
$configurationManager->registerConfigurationType(
static::CONFIGURATION_TYPE_VALIDATION,
ConfigurationManager::CONFIGURATION_PROCESSING_TYPE_DEFAULT,
true
);
}
);
}
}

View file

@ -0,0 +1,194 @@
<?php
namespace DigiComp\SettingValidator\Validation\Validator;
/*
* This file is part of the DigiComp.SettingValidator package.
*
* (c) digital competence
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Reflection\ReflectionService;
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
use Neos\Flow\Validation\Validator\AbstractValidator;
use Neos\Flow\Validation\ValidatorResolver;
use Neos\Utility\ObjectAccess;
use Neos\Utility\TypeHandling;
/**
* Validator resolving other Validators defined in Validation.yaml
*
* @Flow\Scope("prototype")
*/
class SettingsValidator extends AbstractValidator
{
/**
* @var ValidatorResolver
* @Flow\Inject
*/
protected $validatorResolver;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @var ReflectionService
* @Flow\Inject
*/
protected $reflectionService;
/**
* @var array
*/
protected $supportedOptions = [
'name' => ['', 'Set the name of the setting-array to use', 'string', false],
'validationGroups' => [
['Default'],
'Same as "Validation Groups" of Flow Framework. Defines the groups to execute.',
'array',
false
],
];
/**
* @var array
*/
protected $validations;
/**
* @param ConfigurationManager $configurationManager
*/
public function injectConfigurationManager(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
$this->validations = $this->configurationManager->getConfiguration('Validation');
}
/**
* Check if $value is valid. If it is not valid, needs to add an error
* to Result.
*
* @param mixed $value
*
* @throws InvalidValidationOptionsException
* @throws InvalidValidationConfigurationException
*/
protected function isValid($value)
{
$name = $this->options['name'] ? $this->options['name'] : TypeHandling::getTypeForValue($value);
if (! isset($this->validations[$name])) {
throw new InvalidValidationOptionsException(
'The name ' . $name . ' has not been defined in Validation.yaml!',
1397821438
);
}
$config = $this->getConfigForName($name);
foreach ($config as $validatorConfig) {
if (! $this->doesValidationGroupsMatch($validatorConfig)) {
continue;
}
$this->handleValidationGroups($validatorConfig);
$validator = $this->validatorResolver->createValidator(
$validatorConfig['validator'],
$validatorConfig['options']
);
if (! $validator) {
throw new InvalidValidationConfigurationException(
sprintf(
'Validator could not be resolved: "%s" Check your Validation.yaml',
$validatorConfig['validator']
),
1402326139
);
}
if (isset($validatorConfig['property'])) {
$this->result->forProperty($validatorConfig['property'])->merge(
$validator->validate(ObjectAccess::getPropertyPath($value, $validatorConfig['property']))
);
} else {
$this->result->merge($validator->validate($value));
}
}
}
/**
* @param $name
*
* @return array
*/
protected function getConfigForName($name): array
{
$config = [];
if (isset($this->validations[$name]['self'])) {
foreach ($this->validations[$name]['self'] as $validator => &$validation) {
if (is_null($validation)) {
continue;
}
$newValidation['options'] = $validation;
$newValidation['validator'] = $validator;
$config[] = $newValidation;
}
}
if (isset($this->validations[$name]['properties'])) {
foreach ($this->validations[$name]['properties'] as $propertyName => &$validation) {
foreach ($validation as $validator => &$options) {
if (is_null($options)) {
continue;
}
$newValidation['property'] = $propertyName;
$newValidation['validator'] = $validator;
$newValidation['options'] = $options;
$config[] = $newValidation;
}
}
}
return $config;
}
/**
* Check whether at least one configured group does match, if any is configured.
*
* @param array $validatorConfig
* @return bool
*/
protected function doesValidationGroupsMatch(array &$validatorConfig)
{
if (isset($validatorConfig['options']['validationGroups'])
&& count(array_intersect($validatorConfig['options']['validationGroups'], $this->options['validationGroups'])) === 0
) {
return false;
}
return true;
}
/**
* Add validation groups for recursion if necessary.
*
* @param array $validatorConfig
*/
protected function handleValidationGroups(array &$validatorConfig)
{
if (isset($validatorConfig['options']['validationGroups'])
&& $validatorConfig['validator'] !== 'DigiComp.SettingValidator:Settings'
) {
unset($validatorConfig['options']['validationGroups']);
} elseif ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings') {
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
}
}
}

View file

@ -0,0 +1,38 @@
DigiComp\SettingValidator\Tests\Functional\Fixtures\TestObject:
properties:
shouldBeTrue:
BooleanValue:
expectedValue: true
shouldBeFalse:
BooleanValue:
expectedValue: false
Grumble: ~
TrueValidator:
self:
BooleanValue:
expectedValue: true
DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsCustomObject:
self:
DigiComp.SettingValidator:Settings:
name: GroupValidatorCustom
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

@ -0,0 +1,55 @@
<?php
namespace Neos\Flow\Core\Migrations;
/*
* This file is part of the DigiComp.SettingValidator package.
*
* (c) digital competence
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use DigiComp\SettingValidator\Package;
/**
* Restructures Validation.yamls to new format
*/
class Version20170603120900 extends AbstractMigration
{
public function getIdentifier()
{
return 'DigiComp.SettingValidator-20170603120900';
}
/**
* @return void
*/
public function up()
{
$this->processConfiguration(Package::CONFIGURATION_TYPE_VALIDATION,
function (&$configuration) {
foreach ($configuration as $validatorName => &$validators) {
$newConfiguration = ['properties' => [], 'self' => []];
foreach ($validators as $key => &$validator) {
if (!isset($validator['validator']) || !isset($validator['options'])) {
$this->showWarning('The Validation.yaml files contained no validator or options for ' .
'validation: "' . $validatorName . '.' . $key . '". It was not be migrated.');
continue;
}
if (isset($validator['property'])) {
$newConfiguration['properties'][$validator['property']][$validator['validator']]
= $validator['options'];
} else {
$newConfiguration['self'][$validator['validator']] = $validator['options'];
}
}
$validators = $newConfiguration;
}
},
true
);
}
}

View file

@ -1,11 +1,11 @@
type: dictionary type: dictionary
additionalProperties: additionalProperties:
type: array type: dictionary
items: additionalProperties: false
properties:
type: dictionary type: dictionary
additionalProperties: false additionalProperties: false
# This validation sadly only hits the first level of validation, and options are not coverable with current validator
properties: properties:
validator: {type: string, required: TRUE} type: dictionary
options: {type: dictionary, required: TRUE} self:
property: {type: string, required: FALSE} type: dictionary

View file

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

View file

@ -0,0 +1,33 @@
<?php
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
use Neos\Flow\Annotations as Flow;
class TestValidationGroupsCustomObject
{
/**
* @var bool
*/
protected $shouldBeTrue = false;
/**
* @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,33 @@
<?php
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
use Neos\Flow\Annotations as Flow;
class TestValidationGroupsDefaultObject
{
/**
* @var bool
*/
protected $shouldBeTrue = false;
/**
* @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,60 @@
<?php
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;
class SettingsValidatorTest extends FunctionalTestCase
{
/**
* @test
*/
public function ifNoNameIsGivenClassNameIsUsed()
{
$validator = $this->objectManager->get(SettingsValidator::class);
$result = $validator->validate(new TestObject());
$this->assertTrue($result->hasErrors());
$this->assertCount(1, $result->getFlattenedErrors());
$this->assertCount(1, $result->forProperty('shouldBeFalse')->getErrors());
}
/**
* @test
*/
public function conjunctionValidationWorksAsExpected()
{
$validatorResolver = $this->objectManager->get(ValidatorResolver::class);
$validator = $validatorResolver->getBaseValidatorConjunction(TestObject::class);
$result = $validator->validate(new TestObject());
$this->assertTrue($result->hasErrors());
$this->assertCount(1, $result->getFlattenedErrors());
}
/**
* @test
*/
public function defaultValidationGroupWorks()
{
$validator = $this->objectManager->get(SettingsValidator::class, ['validationGroups' => ['Default']]);
$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 property');
}
/**
* @test
*/
public function customValidationGroupWorks()
{
$validator = $this->objectManager->get(SettingsValidator::class, ['validationGroups' => ['Custom']]);
$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 property');
}
}

View file

@ -1,7 +1,7 @@
{ {
"name": "digicomp/settingvalidator", "name": "digicomp/settingvalidator",
"type": "typo3-flow-package", "type": "neos-package",
"description": "Just a TYPO3\\Flow Validator resolving other Validators with Configuration/Validation.yaml", "description": "Just a Neos\\Flow Validator resolving other Validators with Configuration/Validation.yaml",
"authors": [ "authors": [
{ {
"name": "Ferdinand Kuhl", "name": "Ferdinand Kuhl",
@ -13,21 +13,27 @@
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/fcool/DigiComp.SettingValidator", "homepage": "https://github.com/fcool/DigiComp.SettingValidator",
"keywords": [ "keywords": [
"Neos",
"Flow", "Flow",
"validation" "validation"
], ],
"require": { "require": {
"typo3/flow": "~2.0|~3.0", "neos/flow": "~4.1"
"php": ">=5.3.0"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-4": {
"DigiComp\\SettingValidator": "Classes" "DigiComp\\SettingValidator\\": "Classes"
}
},
"autoload-dev": {
"psr-4": {
"DigiComp\\SettingValidator\\Tests\\": "Tests"
} }
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.0.x-dev" "dev-master": "1.0.x-dev",
"dev-feature/neos-flow4": "2.0.x-dev"
}, },
"applied-flow-migrations": [ "applied-flow-migrations": [
"Inwebs.Basket-201409170938", "Inwebs.Basket-201409170938",
@ -44,7 +50,23 @@
"TYPO3.Fluid-20141113120800", "TYPO3.Fluid-20141113120800",
"TYPO3.Flow-20141113121400", "TYPO3.Flow-20141113121400",
"TYPO3.Fluid-20141121091700", "TYPO3.Fluid-20141121091700",
"TYPO3.Fluid-20150214130800" "TYPO3.Fluid-20150214130800",
"TYPO3.Flow-20151113161300",
"TYPO3.Flow-20161115140400",
"TYPO3.Flow-20161115140430",
"Neos.Flow-20161124204700",
"Neos.Flow-20161124204701",
"Neos.Flow-20161124224015",
"Neos.Eel-20161124230101",
"Neos.Imagine-20161124231742",
"Neos.Media-20161124233100",
"Neos.Flow-20161125124112",
"Neos.SwiftMailer-20161130105617",
"TYPO3.FluidAdaptor-20161130112935",
"Neos.Media-20161219094126",
"Neos.Flow-20170125103800",
"Neos.Flow-20170127183102",
"DigiComp.SettingValidator-20170603120900"
] ]
} }
} }