/** * Handle the assertion that the given result object has no errors * * @param Error\Result $result * @param boolean $expectSuccess * @return void */ protected function assertSuccess(Error\Result $result, $expectSuccess = true) { if ($expectSuccess === true) { $this->assertFalse($result->hasErrors()); } else { $this->assertTrue($result->hasErrors()); } }
/** * @test */ public function validatorConjunctionReturnsErrorsIfOneValidatorReturnsErrors() { $validatorConjunction = new ConjunctionValidator([]); $validatorObject = $this->createMock(ValidatorInterface::class); $errors = new Error\Result(); $errors->addError(new 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 returnsAndRendersThenChildIfResultsHaveErrors() { $result = new Result(); $result->addError(new Error('I am an error', 1386163707)); /** @var $requestMock \PHPUnit_Framework_MockObject_MockObject */ $requestMock = $this->request; $requestMock->expects($this->once())->method('getInternalArgument')->with('__submittedArgumentValidationResults')->will($this->returnValue($result)); $this->viewHelper->expects($this->once())->method('renderThenChild')->will($this->returnValue('ThenChild')); $this->assertEquals('ThenChild', $this->viewHelper->render()); }
/** * Checks if the given value is valid according to the property validators. * * @param mixed $object The value that should be validated * @return void * @api */ protected function isValid($object) { $messages = new ErrorResult(); foreach ($this->propertyValidators as $propertyName => $validators) { $propertyValue = $this->getPropertyValue($object, $propertyName); $result = $this->checkProperty($propertyValue, $validators); if ($result !== null) { $messages->forProperty($propertyName)->merge($result); } } $this->result = $messages; }
/** * @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 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([$notice1], $this->result->getNotices(), 'Notices are not merged correctly without recursion'); $this->assertSame([$notice3], $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.'); $this->assertSame([$notice2], $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.'); $this->assertSame([$warning2, $warning3, $warning1], $this->result->getWarnings()); $this->assertSame([$error3], $this->result->getErrors()); $this->assertSame([$error1, $error2], $this->result->forProperty('foo')->getErrors()); }
/** * @test */ public function validateReturnsAllErrorsIfAllValidatorsReturnErrrors() { $validatorDisjunction = new DisjunctionValidator([]); $error1 = new Error\Error('Error', 123); $error2 = new Error\Error('Error2', 123); $errors1 = new Error\Result(); $errors1->addError($error1); $validatorObject = $this->createMock(ValidatorInterface::class); $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors1)); $errors2 = new Error\Result(); $errors2->addError($error2); $secondValidatorObject = $this->createMock(ValidatorInterface::class); $secondValidatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors2)); $validatorDisjunction->addValidator($validatorObject); $validatorDisjunction->addValidator($secondValidatorObject); $this->assertEquals([$error1, $error2], $validatorDisjunction->validate('some subject')->getErrors()); }
/** * @param mixed $value * @return mixed */ public function process($value) { if ($this->dataType !== null) { $value = $this->propertyMapper->convert($value, $this->dataType, $this->propertyMappingConfiguration); $messages = $this->propertyMapper->getMessages(); } else { $messages = new \Neos\Error\Messages\Result(); } $validationResult = $this->validator->validate($value); $messages->merge($validationResult); $this->processingMessages->merge($messages); return $value; }
/** * Sets the value of this argument. * * @param mixed $rawValue The value of this argument * @return Argument $this */ public function setValue($rawValue) { if ($rawValue === null) { $this->value = null; return $this; } if (is_object($rawValue) && $rawValue instanceof $this->dataType) { $this->value = $rawValue; return $this; } $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->getPropertyMappingConfiguration()); $this->validationResults = $this->propertyMapper->getMessages(); if ($this->validator !== null) { $validationMessages = $this->validator->validate($this->value); $this->validationResults->merge($validationMessages); } return $this; }
/** * Internal function which actually does the property mapping. * * @param mixed $source the source data to map. MUST be a simple type, NO object allowed! * @param string $targetType The type of the target; can be either a class name or a simple type. * @param PropertyMappingConfigurationInterface $configuration Configuration for the property mapping. * @param array $currentPropertyPath The property path currently being mapped; used for knowing the context in case an exception is thrown. * @return mixed an instance of $targetType * @throws Exception\TypeConverterException * @throws Exception\InvalidPropertyMappingConfigurationException */ protected function doMapping($source, $targetType, PropertyMappingConfigurationInterface $configuration, &$currentPropertyPath) { if (is_object($source)) { $targetClass = TypeHandling::truncateElementType($targetType); if ($source instanceof $targetClass) { return $source; } } if ($source === null) { $source = ''; } $typeConverter = $this->findTypeConverter($source, $targetType, $configuration); $targetType = $typeConverter->getTargetTypeForSource($source, $targetType, $configuration); if (!is_object($typeConverter) || !$typeConverter instanceof TypeConverterInterface) { throw new Exception\TypeConverterException('Type converter for "' . $source . '" -> "' . $targetType . '" not found.'); } $convertedChildProperties = []; foreach ($typeConverter->getSourceChildPropertiesToBeConverted($source) as $sourcePropertyName => $sourcePropertyValue) { $targetPropertyName = $configuration->getTargetPropertyName($sourcePropertyName); if ($configuration->shouldSkip($targetPropertyName)) { continue; } if (!$configuration->shouldMap($targetPropertyName)) { if ($configuration->shouldSkipUnknownProperties()) { continue; } throw new Exception\InvalidPropertyMappingConfigurationException('It is not allowed to map property "' . $targetPropertyName . '". You need to use $propertyMappingConfiguration->allowProperties(\'' . $targetPropertyName . '\') to enable mapping of this property.', 1335969887); } $targetPropertyType = $typeConverter->getTypeOfChildProperty($targetType, $targetPropertyName, $configuration); $subConfiguration = $configuration->getConfigurationFor($targetPropertyName); $currentPropertyPath[] = $targetPropertyName; $targetPropertyValue = $this->doMapping($sourcePropertyValue, $targetPropertyType, $subConfiguration, $currentPropertyPath); array_pop($currentPropertyPath); if (!$targetPropertyValue instanceof Error) { $convertedChildProperties[$targetPropertyName] = $targetPropertyValue; } } $result = $typeConverter->convertFrom($source, $targetType, $convertedChildProperties, $configuration); if ($result instanceof Error) { $this->messages->forProperty(implode('.', $currentPropertyPath))->addError($result); } return $result; }
/** * @return boolean TRUE if the argument is valid, FALSE otherwise * @api * @deprecated Will be removed for next major Flow version. */ public function isValid() { return !$this->validationResults->hasErrors(); }
/** * Validate a null value with the given schema * * @param mixed $value * @param array $schema * @return ErrorResult */ protected function validateNullType($value, array $schema) { $result = new ErrorResult(); if ($value !== null) { $result->addError($this->createError('type=NULL', 'type=' . gettype($value))); } return $result; }
/** * @test */ public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed() { $error = new FLowError\Error('Some Error', 1234); $mockValidator = $this->createMock(ValidatorInterface::class); $validationMessages = new FLowError\Result(); $validationMessages->addError($error); $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages)); $this->simpleValueArgument->setValidator($mockValidator); $this->setupPropertyMapperAndSetValue(); $this->assertEquals([$error], $this->simpleValueArgument->getValidationResults()->getErrors()); }
/** * @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 Error\Error('error1', 123); $result1 = new Error\Result(); $result1->addError($error1); $mockUuidValidator = $this->createMock(ValidatorInterface::class); $mockUuidValidator->expects($this->any())->method('validate')->with(0xf)->will($this->returnValue($result1)); $aValidator->addPropertyValidator('uuid', $mockUuidValidator); $bValidator->addPropertyValidator('uuid', $mockUuidValidator); $this->assertSame(['b.uuid' => [$error1], 'uuid' => [$error1]], $aValidator->validate($A)->getFlattenedErrors()); }
/** * Get all property mapping / validation errors * * @return Result */ public function getValidationResults() { $results = new Result(); foreach ($this as $argument) { $argumentValidationResults = $argument->getValidationResults(); if ($argumentValidationResults === null) { continue; } $results->forProperty($argument->getName())->merge($argumentValidationResults); } return $results; }
/** * Creates a new validation error object and adds it to $this->errors * * @param string $message The error message * @param integer $code The error code (a unix timestamp) * @param array $arguments Arguments to be replaced in message * @return void * @api */ protected function addError($message, $code, array $arguments = []) { $this->result->addError(new ValidationError($message, $code, $arguments)); }
/** * Validate a single configuration type * * @param string $configurationType the configuration typr to validate * @param string $path configuration path to validate, or NULL. * @param array $loadedSchemaFiles will be filled with a list of loaded schema files * @return \Neos\Error\Messages\Result * @throws Exception\SchemaValidationException */ protected function validateSingleType($configurationType, $path, &$loadedSchemaFiles) { $availableConfigurationTypes = $this->configurationManager->getAvailableConfigurationTypes(); if (in_array($configurationType, $availableConfigurationTypes) === false) { throw new Exception\SchemaValidationException('The configuration type "' . $configurationType . '" was not found. Only the following configuration types are supported: "' . implode('", "', $availableConfigurationTypes) . '"', 1364984886); } $configuration = $this->configurationManager->getConfiguration($configurationType); // find schema files for the given type and path $schemaFileInfos = []; $activePackages = $this->packageManager->getActivePackages(); foreach ($activePackages as $package) { $packageKey = $package->getPackageKey(); $packageSchemaPath = Files::concatenatePaths([$package->getResourcesPath(), 'Private/Schema']); if (is_dir($packageSchemaPath)) { foreach (Files::getRecursiveDirectoryGenerator($packageSchemaPath, '.schema.yaml') 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 === $configurationType && ($path === null || strpos($schemaPath, $path) === 0)) { $schemaFileInfos[] = ['file' => $schemaFile, 'name' => $schemaName, 'path' => $schemaPath, 'packageKey' => $packageKey]; } } } } if (count($schemaFileInfos) === 0) { throw new Exception\SchemaValidationException('No schema files found for configuration type "' . $configurationType . '"' . ($path !== null ? ' and path "' . $path . '".' : '.'), 1364985056); } $result = new Result(); foreach ($schemaFileInfos as $schemaFileInfo) { $loadedSchemaFiles[] = $schemaFileInfo['file']; if ($schemaFileInfo['path'] !== null) { $data = Arrays::getValueByPath($configuration, $schemaFileInfo['path']); } else { $data = $configuration; } if (empty($data)) { $result->addNotice(new Notice('No configuration found, skipping schema "%s".', 1364985445, [substr($schemaFileInfo['file'], strlen(FLOW_PATH_ROOT))])); } else { $parsedSchema = Yaml::parse($schemaFileInfo['file']); $validationResultForSingleSchema = $this->schemaValidator->validate($data, $parsedSchema); if ($schemaFileInfo['path'] !== null) { $result->forProperty($schemaFileInfo['path'])->merge($validationResultForSingleSchema); } else { $result->merge($validationResultForSingleSchema); } } } return $result; }
/** * @test */ public function getValidationResultsShouldFetchAllValidationResltsFromArguments() { $error1 = new FlowError\Error('Validation error', 1234); $error2 = new FlowError\Error('Validation error 2', 1235); $results1 = new FlowError\Result(); $results1->addError($error1); $results2 = new FlowError\Result(); $results2->addError($error2); $argument1 = $this->getMockBuilder(Argument::class)->setMethods(['getValidationResults'])->setConstructorArgs(['name1', 'string'])->getMock(); $argument1->expects($this->once())->method('getValidationResults')->will($this->returnValue($results1)); $argument2 = $this->getMockBuilder(Argument::class)->setMethods(['getValidationResults'])->setConstructorArgs(['name2', 'string'])->getMock(); $argument2->expects($this->once())->method('getValidationResults')->will($this->returnValue($results2)); $arguments = new Arguments(); $arguments->addArgument($argument1); $arguments->addArgument($argument2); $this->assertSame(['name1' => [$error1], 'name2' => [$error2]], $arguments->getValidationResults()->getFlattenedErrors()); }
/** * @param Result $validationResult * @return void */ protected function assertValidationResultContainsNoErrors(Result $validationResult) { if ($validationResult->hasErrors()) { $errors = $validationResult->getFlattenedErrors(); /** @var Error $error */ $output = ''; foreach ($errors as $path => $pathErrors) { foreach ($pathErrors as $error) { $output .= sprintf('%s -> %s' . PHP_EOL, $path, $error->render()); } } $this->fail($output); } $this->assertFalse($validationResult->hasErrors()); }