/** * Overriding an exiting file is not allowed and an exception is thrown. */ public function testOverrideExistingFileNotAllowed() { $mockFile = $this->getMockBuilder('Reskume\\FileBundle\\Entity\\File')->getMock(); $mockFile->expects($this->once())->method('isTmpFile')->willReturn(true); $attachment = new Attachment(); $attachment->setFile($mockFile); $this->expectException('\\RuntimeException'); $attachment->setFile(new File(TestPathProvider::getPath() . '/Fixtures/files/test.txt')); }
/** * Test that a base64 encoded string is returned for the specified pathname. */ public function testCreateUploadThumbnailReturnBase64EncodedString() { $mockEntityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $configuration = new TestFileStorageConfiguration(); $twigExtension = new ReskumeFileBundleExtension($configuration, $mockEntityManager); $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test_image.jpeg'); $this->assertEquals(ThumbnailHelper::getBase64Thumb(new HttpFile(TestPathProvider::getPath() . '/Fixtures/files/thumbs/thumb_test_image.jpeg')), $twigExtension->createThumbnailFunction($file)); $this->assertEquals(ThumbnailHelper::getBase64Thumb(new HttpFile(TestPathProvider::getPath() . '/Fixtures/files/thumbs/thumb_test_image.jpeg')), $twigExtension->createThumbnailFunction($file, 'base64')); }
/** * Test that expected thumbnail is created. */ public function testThumbnailCreation() { $container = static::$kernel->getContainer(); $fileStorageConfiguration = $container->get('reskume_file.configuration.file_storage_configuration'); $entityManager = $container->get('doctrine.orm.entity_manager'); $thumbnailHelper = new ThumbnailHelper($fileStorageConfiguration, $entityManager); $thumb = $thumbnailHelper->createScaledThumb(TestPathProvider::getPath() . '/Fixtures/files/test_image.jpeg', 50); $this->assertFileExists($thumb->getPathname()); $thumbBase64Value = $thumbnailHelper->getBase64Thumb($thumb); // get base64 value of fixture test thumbnail $testThumb = new File(TestPathProvider::getPath() . '/Fixtures/files/thumbs/thumb_test_image_50px.jpeg'); $expectedThumbBase64Value = $thumbnailHelper->getBase64Thumb($thumb); $this->assertEquals($expectedThumbBase64Value, $thumbBase64Value); }
/** * Test that MIME types that are defined as "not allowed" in the upload configuration class trigger a validation * error. */ public function testInvalidUploadConfigMimeTypeNotAllowed() { $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test_image.bmp')->setOrigFilename('test_image.jpeg')->setPath('/test/storage/path')->setFileMetadata(); $attachment = new Attachment(); $attachment->setFile($file)->setDescription('This is a test description.'); $constraintViolationBuilder = $this->getMockBuilder('Symfony\\Component\\Validator\\Violation\\ConstraintViolationBuilder')->disableOriginalConstructor()->getMock(); $mockContext = $this->getMockBuilder('Symfony\\Component\\Validator\\Context\\ExecutionContextInterface')->disableOriginalConstructor()->getMock(); $mockContext->expects($this->once())->method('buildViolation')->willReturn($constraintViolationBuilder); $constraintViolationBuilder->expects($this->any())->method('setParameter')->willReturnSelf(); $constraintViolationBuilder->expects($this->once())->method('setCode')->willReturnSelf(); $constraintViolationBuilder->expects($this->once())->method('addViolation'); $validator = static::$kernel->getContainer()->get('reskume_file.validator.file_attachment'); $validator->initialize($mockContext); $constraint = new FileConstraint(); $constraint->mimeTypes = array('image/jpeg'); $validator->validate($attachment, $constraint); }
/** * Test removing file from storage backend. */ public function testRemoveFile() { $container = static::$kernel->getContainer(); $fileStorageConfiguration = $container->get('reskume_file.configuration.file_storage_configuration'); $fileStorage = $fileStorageConfiguration->createFileStorage($this->em); // clean and copy files for testing purposes $filesystem = new Filesystem(); $this->cleanDir(TestPathProvider::getPath() . '/App/tmp'); // clean 'tmp' dir $filesystem->copy(TestPathProvider::getPath() . '/Fixtures/files/test_image.jpeg', TestPathProvider::getPath() . '/App/tmp/uploadFiles/test_image_1.jpeg'); $file = new File(); $file->setOrigFilename('test_image_1.jpeg')->setPath('test/images')->setTmpPathname(TestPathProvider::getPath() . '/App/tmp/uploadFiles/test_image_1.jpeg')->createKey()->setFileMetadata(); $this->em->persist($file); $this->em->flush(); // file storage persist is automatically called $this->em->clear(); // get file via storage backend $file = $fileStorage->getFile($file->getKey()); $this->em->remove($file); $this->em->flush(); $this->expectException('League\\Flysystem\\FileNotFoundException'); $fileStorage->getContent($file); }
/** * Test that the returned JSON response contains a valid single file attachment wrapper div container. */ public function testSingleFileAttachmentJsonResponseContentIsValid() { $container = static::$kernel->getContainer(); // clean test directories $filesystem = new Filesystem(); $this->cleanDir(TestPathProvider::getPath() . '/App/tmp'); // clean 'tmp' dir // copy files for testing purposes $originalName = 'test_image_1.jpeg'; $tmpUploadFilepath = TestPathProvider::getPath() . '/App/tmp/tmp_upload/' . $originalName; $filesystem->copy(TestPathProvider::getPath() . '/Fixtures/files/test_image.jpeg', $tmpUploadFilepath); $tokenId = 'testEntity[attachment]'; $tokenManager = $container->get('security.csrf.token_manager'); $formToken = $tokenManager->getToken($tokenId); $fileBagParameters = array('file' => array(new UploadedFile($tmpUploadFilepath, $originalName, null, null, 0, true))); $request = new Request(array(), array('formName' => $tokenId, 'formId' => 'testEntity_attachment', 'formToken' => $formToken->getValue(), 'attachmentIndex' => 0), array(), array(), $fileBagParameters, array(), $content = null); $attachmentController = new AttachmentController(); $attachmentController->setContainer($container); $jsonResponse = $attachmentController->uploadAction($request); $fieldIds = ResponseContentUtil::getFieldIds($jsonResponse->getContent()); $fields = array('id' => reset($fieldIds), 'originalName' => $originalName, 'filepath' => $tmpUploadFilepath); $expectedResponse = ResponseContentUtil::getSingleAttachmentResponse($fields, $container->get('twig')); $this->assertSame(ResponseContentUtil::makeComparable($expectedResponse->getContent()), ResponseContentUtil::makeComparable($jsonResponse->getContent())); }
/** * @return File */ public function create() { $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test.txt')->setOrigFilename('test.txt')->setPath('/test/storage/path')->setFileMetadata(); return $file; }
/** * Test that key string is created. */ public function testCreateKey() { $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test.txt')->setFileMetadata()->setPath('/test/path')->createKey(); $this->assertEquals(1, preg_match('/^(\\/\\w+)+\\.[a-zA-Z]+$/', $file->getKey())); }
/** * {@inheritdoc} */ public function getThumbDir() : string { return TestPathProvider::getPath() . '/Fixtures/files/thumbs'; }
/** * @param array $fields * @param \Twig_Environment $twig * * @return JsonResponse */ public static function getSingleAttachmentResponse(array $fields, \Twig_Environment $twig) { $responseString = $twig->render(TestPathProvider::getPath() . '/Fixtures/files/responseContent/uploadedFileAttachment.html.twig', $fields); return new JsonResponse(array('content' => $responseString, 'message' => ''), 200); }