/** * Checks if the given value is valid according to the validators of the conjunction. * * @param mixed $value The value that should be validated * @return \TYPO3\FLOW3\Error\Result * @api */ public function validate($value) { $result = new \TYPO3\FLOW3\Error\Result(); foreach ($this->validators as $validator) { $result->merge($validator->validate($value)); } return $result; }
/** * Checks if the given value is valid according to the property validators * Note: a value of NULL or empty string ('') is considered valid * * @param mixed $object The value that should be validated * @return void * @api */ protected function isValid($object) { $messages = new \TYPO3\FLOW3\Error\Result(); foreach ($this->propertyValidators as $propertyName => $validators) { $propertyValue = $this->getPropertyValue($object, $propertyName); $this->checkProperty($propertyValue, $validators, $messages->forProperty($propertyName)); } $this->result = $messages; return; }
/** * @test */ public function validatorConjunctionReturnsErrorsIfOneValidatorReturnsErrors() { $validatorConjunction = new \TYPO3\FLOW3\Validation\Validator\ConjunctionValidator(array()); $validatorObject = $this->getMock('TYPO3\\FLOW3\\Validation\\Validator\\ValidatorInterface'); $errors = new \TYPO3\FLOW3\Error\Result(); $errors->addError(new \TYPO3\FLOW3\Error\Error('Error', 123)); $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors)); $validatorConjunction->addValidator($validatorObject); $this->assertTrue($validatorConjunction->validate('some subject')->hasErrors()); }
/** * @test */ public function validateReturnsAllErrorsIfAllValidatorsReturnErrrors() { $validatorDisjunction = new \TYPO3\FLOW3\Validation\Validator\DisjunctionValidator(array()); $error1 = new \TYPO3\FLOW3\Error\Error('Error', 123); $error2 = new \TYPO3\FLOW3\Error\Error('Error2', 123); $errors1 = new \TYPO3\FLOW3\Error\Result(); $errors1->addError($error1); $validatorObject = $this->getMock('TYPO3\\FLOW3\\Validation\\Validator\\ValidatorInterface'); $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors1)); $errors2 = new \TYPO3\FLOW3\Error\Result(); $errors2->addError($error2); $secondValidatorObject = $this->getMock('TYPO3\\FLOW3\\Validation\\Validator\\ValidatorInterface'); $secondValidatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors2)); $validatorDisjunction->addValidator($validatorObject); $validatorDisjunction->addValidator($secondValidatorObject); $this->assertEquals(array($error1, $error2), $validatorDisjunction->validate('some subject')->getErrors()); }
/** * Checks if the given value is valid according to the validators of the * disjunction. * * If all validators fail, the result is FALSE. * * @param mixed $value The value that should be validated * @return \TYPO3\FLOW3\Error\Result * @api */ public function validate($value) { $result = new \TYPO3\FLOW3\Error\Result(); $oneWithoutErrors = FALSE; foreach ($this->validators as $validator) { $validatorResult = $validator->validate($value); if ($validatorResult->hasErrors()) { $result->merge($validatorResult); } else { $oneWithoutErrors = TRUE; } } if ($oneWithoutErrors === TRUE) { $result = new \TYPO3\FLOW3\Error\Result(); } return $result; }
/** * @test */ public function mergeShouldMergeTwoResults() { $notice1 = $this->getMockMessage('Notice'); $notice2 = $this->getMockMessage('Notice'); $notice3 = $this->getMockMessage('Notice'); $warning1 = $this->getMockMessage('Warning'); $warning2 = $this->getMockMessage('Warning'); $warning3 = $this->getMockMessage('Warning'); $error1 = $this->getMockMessage('Error'); $error2 = $this->getMockMessage('Error'); $error3 = $this->getMockMessage('Error'); $otherResult = new \TYPO3\FLOW3\Error\Result(); $otherResult->addNotice($notice1); $otherResult->forProperty('foo.bar')->addNotice($notice2); $this->result->forProperty('foo')->addNotice($notice3); $otherResult->addWarning($warning1); $this->result->addWarning($warning2); $this->result->addWarning($warning3); $otherResult->forProperty('foo')->addError($error1); $otherResult->forProperty('foo')->addError($error2); $otherResult->addError($error3); $this->result->merge($otherResult); $this->assertSame(array($notice1), $this->result->getNotices(), 'Notices are not merged correctly without recursion'); $this->assertSame(array($notice3), $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.'); $this->assertSame(array($notice2), $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.'); $this->assertSame(array($warning2, $warning3, $warning1), $this->result->getWarnings()); $this->assertSame(array($error3), $this->result->getErrors()); $this->assertSame(array($error1, $error2), $this->result->forProperty('foo')->getErrors()); }
/** * @test */ public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed() { $error = new \TYPO3\FLOW3\Error\Error('Some Error', 1234); $mockValidator = $this->getMock('TYPO3\\FLOW3\\Validation\\Validator\\ValidatorInterface'); $validationMessages = new \TYPO3\FLOW3\Error\Result(); $validationMessages->addError($error); $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages)); $this->simpleValueArgument->setValidator($mockValidator); $this->setupPropertyMapperAndSetValue(); $this->assertFalse($this->simpleValueArgument->isValid()); $this->assertEquals(array($error), $this->simpleValueArgument->getValidationResults()->getErrors()); }
/** * Validate the given configuration * * ./flow3 configuration:validate --type Settings --path TYPO3.FLOW3.persistence * * The schemas are searched in the path "Resources/Private/Schema" of all * active Packages. The schema-filenames must match the pattern * __type__.__path__.schema.yaml. The type and/or the path can also be * expressed as subdirectories of Resources/Private/Schema. So * Settings/TYPO3/FLOW3.persistence.schema.yaml will match the same pathes * like Settings.TYPO3.FLOW3.persistence.schema.yaml or * Settings/TYPO3.FLOW3/persistence.schema.yaml * * @param string $type Configuration type to validate * @param string $path path to the subconfiguration separated by "." like "TYPO3.FLOW3" * @return void */ public function validateCommand($type = NULL, $path = NULL) { $availableConfigurationTypes = $this->configurationManager->getAvailableConfigurationTypes(); if (in_array($type, $availableConfigurationTypes) === FALSE) { if ($type !== NULL) { $this->outputLine('<b>Configuration type "%s" was not found!</b>', array($type)); $this->outputLine(); } $this->outputLine('<b>Available configuration types:</b>'); foreach ($availableConfigurationTypes as $availableConfigurationType) { $this->outputLine(' ' . $availableConfigurationType); } $this->outputLine(); $this->outputLine('Hint: <b>%s configuration:validate --type <configurationType></b>', array($this->getFlow3InvocationString())); $this->outputLine(' validates the configuration of the specified type.'); return; } $configuration = $this->configurationManager->getConfiguration($type); $this->outputLine('<b>Validating configuration for type: "' . $type . '"' . ($path !== NULL ? ' and path: "' . $path . '"' : '') . '</b>'); // find schema files for the given type and path $schemaFileInfos = array(); $activePackages = $this->packageManager->getActivePackages(); foreach ($activePackages as $package) { $packageKey = $package->getPackageKey(); $packageSchemaPath = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array($package->getResourcesPath(), 'Private/Schema')); if (is_dir($packageSchemaPath)) { $packageSchemaFiles = \TYPO3\FLOW3\Utility\Files::readDirectoryRecursively($packageSchemaPath, '.schema.yaml'); foreach ($packageSchemaFiles as $schemaFile) { $schemaName = substr($schemaFile, strlen($packageSchemaPath) + 1, -strlen('.schema.yaml')); $schemaNameParts = explode('.', str_replace('/', '.', $schemaName), 2); $schemaType = $schemaNameParts[0]; $schemaPath = isset($schemaNameParts[1]) ? $schemaNameParts[1] : NULL; if ($schemaType === $type && ($path === NULL || strpos($schemaPath, $path) === 0)) { $schemaFileInfos[] = array('file' => $schemaFile, 'name' => $schemaName, 'path' => $schemaPath, 'packageKey' => $packageKey); } } } } $this->outputLine(); if (count($schemaFileInfos) > 0) { $this->outputLine('%s schema files were found:', array(count($schemaFileInfos))); $result = new \TYPO3\FLOW3\Error\Result(); foreach ($schemaFileInfos as $schemaFileInfo) { if ($schemaFileInfo['path'] !== NULL) { $data = \TYPO3\FLOW3\Utility\Arrays::getValueByPath($configuration, $schemaFileInfo['path']); } else { $data = $configuration; } if (empty($data)) { $result->forProperty($schemaFileInfo['path'])->addError(new \TYPO3\FLOW3\Error\Error('configuration in path ' . $schemaFileInfo['path'] . ' is empty')); $this->outputLine(' - package: "' . $schemaFileInfo['packageKey'] . '" schema: "' . $schemaFileInfo['name'] . '" -> <b>configuration is empty</b>'); } else { $parsedSchema = \Symfony\Component\Yaml\Yaml::parse($schemaFileInfo['file']); $schemaResult = $this->schemaValidator->validate($data, $parsedSchema); if ($schemaResult->hasErrors()) { $this->outputLine(' - package:"' . $schemaFileInfo['packageKey'] . '" schema:"' . $schemaFileInfo['name'] . '" -> <b>' . count($schemaResult->getFlattenedErrors()) . ' errors</b>'); } else { $this->outputLine(' - package:"' . $schemaFileInfo['packageKey'] . '" schema:"' . $schemaFileInfo['name'] . '" -> <b>is valid</b>'); } if ($schemaFileInfo['path'] !== NULL) { $result->forProperty($schemaFileInfo['path'])->merge($schemaResult); } else { $result->merge($schemaResult); } } } } else { $this->outputLine('No matching schema-files were found!'); return; } $this->outputLine(); if ($result->hasErrors()) { $errors = $result->getFlattenedErrors(); $this->outputLine('<b>%s errors were found:</b>', array(count($errors))); foreach ($errors as $path => $pathErrors) { foreach ($pathErrors as $error) { $this->outputLine(' - %s -> %s', array($path, $error->render())); } } } else { $this->outputLine('<b>The configuration is valid!</b>'); } }
/** * Validate a null value with the given schema * * @param mixed $value * @param array $schema * @return \TYPO3\FLOW3\Error\Result */ protected function validateNullType($value, array $schema) { $result = new \TYPO3\FLOW3\Error\Result(); if ($value !== NULL) { $result->addError($this->createError('type=NULL', 'type=' . gettype($value))); } return $result; }
/** * @test */ public function getValidationResultsShouldFetchAllValidationResltsFromArguments() { $error1 = new \TYPO3\FLOW3\Error\Error('Validation error', 1234); $error2 = new \TYPO3\FLOW3\Error\Error('Validation error 2', 1235); $results1 = new \TYPO3\FLOW3\Error\Result(); $results1->addError($error1); $results2 = new \TYPO3\FLOW3\Error\Result(); $results2->addError($error2); $argument1 = $this->getMock('TYPO3\\FLOW3\\Mvc\\Controller\\Argument', array('getValidationResults'), array('name1', 'string')); $argument1->expects($this->once())->method('getValidationResults')->will($this->returnValue($results1)); $argument2 = $this->getMock('TYPO3\\FLOW3\\Mvc\\Controller\\Argument', array('getValidationResults'), array('name2', 'string')); $argument2->expects($this->once())->method('getValidationResults')->will($this->returnValue($results2)); $arguments = new \TYPO3\FLOW3\Mvc\Controller\Arguments(); $arguments->addArgument($argument1); $arguments->addArgument($argument2); $this->assertSame(array('name1' => array($error1), 'name2' => array($error2)), $arguments->getValidationResults()->getFlattenedErrors()); }
/** * @test */ public function validateDetectsFailuresInRecursiveTargetsII() { $classNameA = 'A' . md5(uniqid(mt_rand(), TRUE)); eval('class ' . $classNameA . '{ public $b; public $uuid = 0xF; }'); $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE)); eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }'); $A = new $classNameA(); $B = new $classNameB(); $A->b = $B; $B->a = $A; $aValidator = $this->getValidator(); $bValidator = $this->getValidator(); $aValidator->addPropertyValidator('b', $bValidator); $bValidator->addPropertyValidator('a', $aValidator); $error1 = new \TYPO3\FLOW3\Error\Error('error1', 123); $result1 = new \TYPO3\FLOW3\Error\Result(); $result1->addError($error1); $mockUuidValidator = $this->getMock('TYPO3\\FLOW3\\Validation\\Validator\\ValidatorInterface'); $mockUuidValidator->expects($this->any())->method('validate')->with(0xf)->will($this->returnValue($result1)); $aValidator->addPropertyValidator('uuid', $mockUuidValidator); $bValidator->addPropertyValidator('uuid', $mockUuidValidator); $this->assertSame(array('b.uuid' => array($error1), 'uuid' => array($error1)), $aValidator->validate($A)->getFlattenedErrors()); }
/** * Get all property mapping / validation errors * * @return \TYPO3\FLOW3\Error\Result */ public function getValidationResults() { $results = new \TYPO3\FLOW3\Error\Result(); foreach ($this as $argument) { $argumentValidationResults = $argument->getValidationResults(); if ($argumentValidationResults === NULL) { continue; } $results->forProperty($argument->getName())->merge($argumentValidationResults); } return $results; }