public function validationData() { $fileEntity = new File(); $file = new \Symfony\Component\HttpFoundation\File\File(realpath(__DIR__ . '/../Fixtures/testFile/test.txt')); $fileEntity->setFile($file); return ['valid' => ['testClass', $fileEntity, 'testField', "image/*\ntext/plain", true], 'bad_mime' => ['testClass', $fileEntity, 'testField', "image/*", false]]; }
/** * Copy file by $fileUrl (local path or remote file), copy it to temp dir and return Attachment entity record * * @param string $fileUrl * @return File|null */ public function prepareRemoteFile($fileUrl) { try { $fileName = pathinfo($fileUrl)['basename']; $parametersPosition = strpos($fileName, '?'); if ($parametersPosition) { $fileName = substr($fileName, 0, $parametersPosition); } $filesystem = new SymfonyFileSystem(); $tmpFile = realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR . $fileName; $filesystem->copy($fileUrl, $tmpFile, true); $file = new FileType($tmpFile); $attachment = new File(); $attachment->setFile($file); $this->preUpload($attachment); return $attachment; } catch (\Exception $e) { return null; } }
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); }
/** * @param EmailAttachment $emailAttachment * * @return File|null */ protected function copyEmailAttachmentToFileSystem(EmailAttachment $emailAttachment) { $file = new File(); $file->setExtension($emailAttachment->getExtension()); $file->setOriginalFilename($emailAttachment->getFileName()); $file->setMimeType($emailAttachment->getContentType()); $file->setFilename(uniqid() . '.' . $file->getExtension()); $content = ContentDecoder::decode($emailAttachment->getContent()->getContent(), $emailAttachment->getContent()->getContentTransferEncoding()); $this->filesystem->write($file->getFilename(), $content); $f = new ComponentFile($this->getAttachmentFullPath($file->getFilename())); $file->setFile($f); $file->setFileSize($f->getSize()); $file->setUploaded(false); $file->setOwner($this->securityFacadeLink->getService()->getLoggedUser()); return $file; }