/** * Add a domain record * * @param string $siteNodeName The nodeName of the site rootNode, e.g. "neostypo3org" * @param string $hostPattern The host pattern to match on, e.g. "neos.typo3.org" * @param string $scheme The scheme for linking (http/https) * @param integer $port The port for linking (0-49151) * @return void */ public function addCommand($siteNodeName, $hostPattern, $scheme = null, $port = null) { $site = $this->siteRepository->findOneByNodeName($siteNodeName); if (!$site instanceof Site) { $this->outputLine('<error>No site found with nodeName "%s".</error>', array($siteNodeName)); $this->quit(1); } $domains = $this->domainRepository->findByHostPattern($hostPattern); if ($domains->count() > 0) { $this->outputLine('<error>The host pattern "%s" is not unique.</error>', array($hostPattern)); $this->quit(1); } $domain = new Domain(); if ($scheme !== null) { $domain->setScheme($scheme); } if ($port !== null) { $domain->setPort($port); } $domain->setSite($site); $domain->setHostPattern($hostPattern); $domainValidator = $this->validatorResolver->getBaseValidatorConjunction(Domain::class); $result = $domainValidator->validate($domain); if ($result->hasErrors()) { foreach ($result->getFlattenedErrors() as $propertyName => $errors) { $firstError = array_pop($errors); $this->outputLine('<error>Validation failed for "' . $propertyName . '": ' . $firstError . '</error>'); $this->quit(1); } } $this->domainRepository->add($domain); $this->outputLine('Domain created.'); }
/** * Sends the given HTTP request * * @param Http\Request $httpRequest * @return Http\Response * @throws Http\Exception * @api */ public function sendRequest(Http\Request $httpRequest) { $requestHandler = $this->bootstrap->getActiveRequestHandler(); if (!$requestHandler instanceof FunctionalTestRequestHandler) { throw new Http\Exception('The browser\'s internal request engine has only been designed for use within functional tests.', 1335523749); } $this->securityContext->clearContext(); $this->validatorResolver->reset(); $response = new Http\Response(); $requestHandler->setHttpRequest($httpRequest); $requestHandler->setHttpResponse($response); $objectManager = $this->bootstrap->getObjectManager(); $baseComponentChain = $objectManager->get(\TYPO3\Flow\Http\Component\ComponentChain::class); $componentContext = new ComponentContext($httpRequest, $response); if (version_compare(PHP_VERSION, '6.0.0') >= 0) { try { $baseComponentChain->handle($componentContext); } catch (\Throwable $throwable) { $this->prepareErrorResponse($throwable, $response); } } else { try { $baseComponentChain->handle($componentContext); } catch (\Exception $exception) { $this->prepareErrorResponse($exception, $response); } } $session = $this->bootstrap->getObjectManager()->get(\TYPO3\Flow\Session\SessionInterface::class); if ($session->isStarted()) { $session->close(); } $this->persistenceManager->clearState(); return $response; }
/** * Checks if the given value is a valid electronic address according to its type. * * If at least one error occurred, the result is FALSE and any errors can * be retrieved through the getErrors() method. * * @param mixed $value The value that should be validated * @return void */ public function isValid($value) { if ($value instanceof \TYPO3\Party\Domain\Model\ElectronicAddress) { if ($this->isValidatedAlready($value)) { return; } $addressValidator = $this->validatorResolver->createValidator($value->getType() . 'Address'); if ($addressValidator === NULL) { $this->addError('No validator found for electronic address of type "' . $value->getType() . '".', 1268676030); } else { $result = $addressValidator->validate($value->getIdentifier()); if ($result->hasErrors()) { $this->result = $result; } } } }
/** * @test */ public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() { $mockController = $this->getAccessibleMock('TYPO3\\Flow\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE); $mockReflectionService = $this->getMock('TYPO3\\Flow\\Reflection\\ReflectionService', array(), array(), '', FALSE); $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue(array())); $this->validatorResolver = $this->getAccessibleMock('TYPO3\\Flow\\Validation\\ValidatorResolver', array('createValidator'), array(), '', FALSE); $this->validatorResolver->_set('reflectionService', $mockReflectionService); $result = $this->validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction'); $this->assertSame(array(), $result); }
/** * @test */ public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() { $mockController = $this->getAccessibleMock(\TYPO3\Flow\Mvc\Controller\ActionController::class, array('fooAction'), array(), '', false); $mockReflectionService = $this->getMockBuilder(\TYPO3\Flow\Reflection\ReflectionService::class)->disableOriginalConstructor()->getMock(); $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue(array())); $this->validatorResolver = $this->getAccessibleMock(\TYPO3\Flow\Validation\ValidatorResolver::class, array('createValidator'), array(), '', false); $this->validatorResolver->_set('reflectionService', $mockReflectionService); $result = $this->validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction'); $this->assertSame(array(), $result); }
/** * Checks for a collection and if needed validates the items in the collection. * This is done with the specified element validator or a validator based on * the given element type and validation group. * * Either elementValidator or elementType must be given, otherwise validation * will be skipped. * * @param mixed $value A collection to be validated * @return void */ protected function isValid($value) { foreach ($value as $index => $collectionElement) { if (isset($this->options['elementValidator'])) { $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']); } elseif (isset($this->options['elementType'])) { if (isset($this->options['validationGroups'])) { $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']); } else { $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']); } } else { return; } if ($collectionElementValidator instanceof ObjectValidatorInterface) { $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer); } $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement)); } }
/** * Checks if the given value is a valid electronic address according to its type. * * If at least one error occurred, the result is FALSE and any errors can * be retrieved through the getErrors() method. * * @param mixed $value The value that should be validated * @return void */ public function isValid($value) { if ($value instanceof \TYPO3\Party\Domain\Model\ElectronicAddress) { $addressType = $value->getType(); switch ($addressType) { case 'Email': $addressValidator = $this->validatorResolver->createValidator('EmailAddress'); break; default: $addressValidator = $this->validatorResolver->createValidator('TYPO3.Party:' . $addressType . 'Address'); } if ($addressValidator === NULL) { $this->addError('No validator found for electronic address of type "' . $addressType . '".', 1268676030); } else { $result = $addressValidator->validate($value->getIdentifier()); if ($result->hasErrors()) { $this->result = $result; } } } }
/** * Sends the given HTTP request * * @param \TYPO3\Flow\Http\Request $request * @return \TYPO3\Flow\Http\Response * @throws \TYPO3\Flow\Http\Exception * @api */ public function sendRequest(Request $request) { $requestHandler = $this->bootstrap->getActiveRequestHandler(); if (!$requestHandler instanceof \TYPO3\Flow\Tests\FunctionalTestRequestHandler) { throw new \TYPO3\Flow\Http\Exception('The browser\'s internal request engine has only been designed for use within functional tests.', 1335523749); } $response = new Response(); $requestHandler->setHttpRequest($request); $requestHandler->setHttpResponse($response); try { $actionRequest = $this->router->route($request); $this->securityContext->clearContext(); $this->securityContext->setRequest($actionRequest); $this->validatorResolver->reset(); $this->dispatcher->dispatch($actionRequest, $response); $session = $this->bootstrap->getObjectManager()->get('TYPO3\\Flow\\Session\\SessionInterface'); if ($session->isStarted()) { $session->close(); } } catch (\Exception $exception) { $pathPosition = strpos($exception->getFile(), 'Packages/'); $filePathAndName = $pathPosition !== FALSE ? substr($exception->getFile(), $pathPosition) : $exception->getFile(); $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : ''; $content = PHP_EOL . 'Uncaught Exception in Flow ' . $exceptionCodeNumber . $exception->getMessage() . PHP_EOL; $content .= 'thrown in file ' . $filePathAndName . PHP_EOL; $content .= 'in line ' . $exception->getLine() . PHP_EOL . PHP_EOL; $content .= \TYPO3\Flow\Error\Debugger::getBacktraceCode($exception->getTrace(), FALSE, TRUE) . PHP_EOL; if ($exception instanceof \TYPO3\Flow\Exception) { $statusCode = $exception->getStatusCode(); } else { $statusCode = 500; } $response->setStatus($statusCode); $response->setContent($content); $response->setHeader('X-Flow-ExceptionCode', $exception->getCode()); $response->setHeader('X-Flow-ExceptionMessage', $exception->getMessage()); } return $response; }
/** * intialize the arguments * */ protected function initializeArguments() { $methodParameters = $this->reflectionService->getMethodParameters($this->class, $this->method); $validators = $this->validatorResolver->buildMethodArgumentsValidatorConjunctions($this->class, $this->method); $validationGroups = array('Default', ucfirst($this->method)); foreach ($methodParameters as $name => $methodParameter) { $methodParameterType = $methodParameter['type'] == 'mixed' ? 'string' : $methodParameter['type']; $validator = $validators[$name]; $validator->addValidator($this->validatorResolver->getBaseValidatorConjunction($methodParameterType, $validationGroups)); $argument = new WebserviceCallArgument($name, $methodParameterType); $argument->setPosition($methodParameter['position']); $argument->setRequired(!$methodParameter['optional']); $argument->setDefaultValue($methodParameter['defaultValue']); $argument->setValidator($validator); $this->arguments[$name] = $argument; } }
/** * Validates the given object and throws an exception if validation fails. * * @param object $object * @return void * @throws \TYPO3\Flow\Persistence\Exception\ObjectValidationFailedException * @api */ protected function validateObject($object) { $classSchema = $this->reflectionService->getClassSchema($object); $validator = $this->validatorResolver->getBaseValidatorConjunction($classSchema->getClassName()); if ($validator === null) { return; } $validationResult = $validator->validate($object); if ($validationResult->hasErrors()) { $errorMessages = ''; $allErrors = $validationResult->getFlattenedErrors(); foreach ($allErrors as $path => $errors) { $errorMessages .= $path . ':' . PHP_EOL; foreach ($errors as $error) { $errorMessages .= (string) $error . PHP_EOL; } } throw new \TYPO3\Flow\Persistence\Exception\ObjectValidationFailedException('An instance of "' . get_class($object) . '" failed to pass validation with ' . count($errors) . ' error(s): ' . PHP_EOL . $errorMessages, 1322585162); } }
/** * Validates the given object and throws an exception if validation fails. * * @param object $object * @param \SplObjectStorage $validatedInstancesContainer * @return void * @throws \TYPO3\Flow\Persistence\Exception\ObjectValidationFailedException */ protected function validateObject($object, \SplObjectStorage $validatedInstancesContainer) { $className = $this->entityManager->getClassMetadata(get_class($object))->getName(); $validator = $this->validatorResolver->getBaseValidatorConjunction($className, array('Persistence', 'Default')); if ($validator === null) { return; } $validator->setValidatedInstancesContainer($validatedInstancesContainer); $validationResult = $validator->validate($object); if ($validationResult->hasErrors()) { $errorMessages = ''; $allErrors = $validationResult->getFlattenedErrors(); foreach ($allErrors as $path => $errors) { $errorMessages .= $path . ':' . PHP_EOL; foreach ($errors as $error) { $errorMessages .= (string) $error . PHP_EOL; } } throw new \TYPO3\Flow\Persistence\Exception\ObjectValidationFailedException('An instance of "' . get_class($object) . '" failed to pass validation with ' . count($errors) . ' error(s): ' . PHP_EOL . $errorMessages, 1322585164); } }
/** * Sends the given HTTP request * * @param Http\Request $httpRequest * @return Http\Response * @throws Http\Exception * @api */ public function sendRequest(Http\Request $httpRequest) { $requestHandler = $this->bootstrap->getActiveRequestHandler(); if (!$requestHandler instanceof FunctionalTestRequestHandler) { throw new Http\Exception('The browser\'s internal request engine has only been designed for use within functional tests.', 1335523749); } $this->securityContext->clearContext(); $this->validatorResolver->reset(); $response = new Http\Response(); $requestHandler->setHttpRequest($httpRequest); $requestHandler->setHttpResponse($response); $objectManager = $this->bootstrap->getObjectManager(); $baseComponentChain = $objectManager->get(\TYPO3\Flow\Http\Component\ComponentChain::class); $componentContext = new ComponentContext($httpRequest, $response); try { $baseComponentChain->handle($componentContext); } catch (\Exception $exception) { $pathPosition = strpos($exception->getFile(), 'Packages/'); $filePathAndName = $pathPosition !== false ? substr($exception->getFile(), $pathPosition) : $exception->getFile(); $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : ''; $content = PHP_EOL . 'Uncaught Exception in Flow ' . $exceptionCodeNumber . $exception->getMessage() . PHP_EOL; $content .= 'thrown in file ' . $filePathAndName . PHP_EOL; $content .= 'in line ' . $exception->getLine() . PHP_EOL . PHP_EOL; $content .= Debugger::getBacktraceCode($exception->getTrace(), false, true) . PHP_EOL; if ($exception instanceof Exception) { $statusCode = $exception->getStatusCode(); } else { $statusCode = 500; } $response->setStatus($statusCode); $response->setContent($content); $response->setHeader('X-Flow-ExceptionCode', $exception->getCode()); $response->setHeader('X-Flow-ExceptionMessage', $exception->getMessage()); } $session = $this->bootstrap->getObjectManager()->get(\TYPO3\Flow\Session\SessionInterface::class); if ($session->isStarted()) { $session->close(); } return $response; }
/** * @param AbstractClientToken $token * @throws InvalidPartyDataException */ public function createPartyAndAttachToAccountFor(AbstractClientToken $token) { $userData = $this->authenticationServicesUserData[(string) $token]; $party = new Person(); $party->setName(new PersonName('', $userData['first_name'], '', $userData['last_name'])); // Todo: this is not covered by the Person implementation, we should have a solution for that #$party->setBirthDate(\DateTime::createFromFormat('!m/d/Y', $userData['birthday'], new \DateTimeZone('UTC'))); #$party->setGender(substr($userData['gender'], 0, 1)); $electronicAddress = new ElectronicAddress(); $electronicAddress->setType(ElectronicAddress::TYPE_EMAIL); $electronicAddress->setIdentifier($userData['email']); $party->addElectronicAddress($electronicAddress); $partyValidator = $this->validatorResolver->getBaseValidatorConjunction('TYPO3\\Party\\Domain\\Model\\Person'); $validationResult = $partyValidator->validate($party); if ($validationResult->hasErrors()) { throw new InvalidPartyDataException('The created party does not satisfy the requirements', 1384266207); } $account = $token->getAccount(); $account->setParty($party); // TODO: this must be properly specifiable (the Roles to add) #$account->setRoles(); $this->accountRepository->update($account); $this->partyRepository->add($party); }
public function initializeMergeAction() { if (!$this->request->hasArgument('try')) { return; } $validators = $this->arguments->getArgument('tryagain')->getValidator(); $notempty = $this->validatorResolver->createValidator('NotEmpty'); $validators->addValidator($notempty); }