示例#1
0
 /**
  * {@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());
     }
 }
 public function testLinkEmailAttachmentToTargetEntityNotValid()
 {
     $file = $this->getMockBuilder('Oro\\Bundle\\AttachmentBundle\\Entity\\File')->setMethods(['getFilename'])->getMock();
     $countable = $this->getMockBuilder('Countable')->getMock();
     $countable->expects($this->never())->method('count')->will($this->returnValue(2));
     $this->configFileValidator->expects($this->never())->method('validate')->will($this->returnValue($countable));
     $emailAttachment = $this->getEmailAttachment();
     $this->emailAttachmentManager->method('buildAttachmentInstance')->withAnyParameters()->will($this->returnValue($this->attachment));
     $emailAttachment->expects($this->any())->method('getFile')->will($this->returnValue($file));
     $this->emailAttachmentManager->linkEmailAttachmentToTargetEntity($emailAttachment, new SomeEntity());
 }
示例#4
0
 /**
  * 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);
 }