コード例 #1
0
 /**
  * @param string          $dataClass Parent entity class name
  * @param File|Attachment $entity    File entity
  * @param string          $fieldName Field name where new file/image field was added
  *
  * @return \Symfony\Component\Validator\ConstraintViolationListInterface
  */
 public function validate($dataClass, $entity, $fieldName = '')
 {
     /** @var Config $entityAttachmentConfig */
     if ($fieldName === '') {
         $entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass);
         $mimeTypes = $this->getMimeArray($entityAttachmentConfig->get('mimetypes'));
         if (!$mimeTypes) {
             $mimeTypes = array_merge($this->getMimeArray($this->config->get('oro_attachment.upload_file_mime_types')), $this->getMimeArray($this->config->get('oro_attachment.upload_image_mime_types')));
         }
     } else {
         $entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass, $fieldName);
         /** @var FieldConfigId $fieldConfigId */
         $fieldConfigId = $entityAttachmentConfig->getId();
         if ($fieldConfigId->getFieldType() === 'file') {
             $configValue = 'upload_file_mime_types';
         } else {
             $configValue = 'upload_image_mime_types';
         }
         $mimeTypes = $this->getMimeArray($this->config->get('oro_attachment.' . $configValue));
     }
     $fileSize = $entityAttachmentConfig->get('maxsize') * 1024 * 1024;
     foreach ($mimeTypes as $id => $value) {
         $mimeTypes[$id] = trim($value);
     }
     return $this->validator->validate($entity->getFile(), [new FileConstraint(['maxSize' => $fileSize, 'mimeTypes' => $mimeTypes])]);
 }
コード例 #2
0
 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);
 }
コード例 #3
0
 /**
  * Upload attachment file
  *
  * @param File $entity
  */
 public function upload(File $entity)
 {
     if ($entity->getFile() !== null && $entity->getFile()->isFile()) {
         $file = $entity->getFile();
         $this->copyLocalFileToStorage($file->getPathname(), $entity->getFilename());
     }
 }