/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $result = null; $entity = $this->attachmentManager->prepareRemoteFile($data); if ($entity) { $violations = $this->validator->validate($context['entityName'], $entity, $context['fieldName']); if (!$violations->count()) { $this->attachmentManager->upload($entity); $result = $entity; } } return $result; }
/** * @dataProvider validationData */ public function testValidate($dataClass, $entity, $fieldName, $mimeTypes, $isValid) { $entityAttachmentConfig = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\Config')->disableOriginalConstructor()->getMock(); $this->attachmentConfigProvider->expects($this->once())->method('getConfig')->with($dataClass)->will($this->returnValue($entityAttachmentConfig)); $fieldConfigId = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\Id\\FieldConfigId')->disableOriginalConstructor()->getMock(); $entityAttachmentConfig->expects($this->any())->method('getId')->will($this->returnValue($fieldConfigId)); $fieldConfigId->expects($this->once())->method('getFieldType')->will($this->returnValue('file')); $this->config->expects($this->once())->method('get')->with('oro_attachment.upload_file_mime_types')->will($this->returnValue($mimeTypes)); $entityAttachmentConfig->expects($this->once())->method('get')->with('maxsize')->will($this->returnValue(1)); $result = $this->configValidator->validate($dataClass, $entity, $fieldName); if ($isValid) { $this->assertEquals(0, $result->count()); } else { $this->assertNotEquals(0, $result->count()); } }
/** * Validate attachment field * * @param FormInterface $form * @param File|Attachment $entity */ protected function validate(FormInterface $form, $entity) { $fieldName = $form->getName(); if ($form->getParent()->getConfig()->getOption('parentEntityClass', null)) { $dataClass = $form->getParent()->getConfig()->getOption('parentEntityClass', null); $fieldName = ''; } else { $dataClass = $form->getParent() ? $form->getParent()->getConfig()->getDataClass() : $form->getConfig()->getDataClass(); if (!$dataClass) { $dataClass = $form->getParent()->getParent()->getConfig()->getDataClass(); } } $violations = $this->validator->validate($dataClass, $entity, $fieldName); if (!empty($violations)) { $fileField = $form->get('file'); /** @var $violation ConstraintViolation */ foreach ($violations as $violation) { $error = new FormError($violation->getMessage(), $violation->getMessageTemplate(), $violation->getMessageParameters()); $fileField->addError($error); } } }
public function testValidate() { $dataClass = 'testClass'; $entity = new File(); $file = new \Symfony\Component\HttpFoundation\File\File(realpath(__DIR__ . '/../Fixtures/testFile/test.txt')); $entity->setFile($file); $fieldName = 'testField'; $mimeTypes = "image/*\ntext/plain"; $maxsize = 1; // 1Mb $entityAttachmentConfig = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\Config')->disableOriginalConstructor()->getMock(); $this->attachmentConfigProvider->expects($this->once())->method('getConfig')->with($dataClass)->will($this->returnValue($entityAttachmentConfig)); $fieldConfigId = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\Id\\FieldConfigId')->disableOriginalConstructor()->getMock(); $entityAttachmentConfig->expects($this->any())->method('getId')->will($this->returnValue($fieldConfigId)); $fieldConfigId->expects($this->once())->method('getFieldType')->will($this->returnValue('file')); $this->config->expects($this->once())->method('get')->with('oro_attachment.upload_file_mime_types')->will($this->returnValue($mimeTypes)); $entityAttachmentConfig->expects($this->once())->method('get')->with('maxsize')->will($this->returnValue($maxsize)); $violationList = $this->getMockBuilder('Symfony\\Component\\Validator\\ConstraintViolationList')->disableOriginalConstructor()->getMock(); $this->validator->expects($this->once())->method('validate')->with($this->identicalTo($entity->getFile()), [new FileConstraint(['maxSize' => $maxsize * 1024 * 1024, 'mimeTypes' => explode("\n", $mimeTypes)])])->willReturn($violationList); $result = $this->configValidator->validate($dataClass, $entity, $fieldName); $this->assertSame($violationList, $result); }