diff --git a/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php b/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php index 76db8b3d2e..eaaf022640 100644 --- a/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php +++ b/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php @@ -81,7 +81,7 @@ public function isNull() /** * Move the file to the given uploadDir and save the filename */ - public function upload($webDir, Filesystem $fileSystem) + public function upload($uploadDir, $webDir, ?Filesystem $fileSystem) { // the file property can be empty if the field is not required if (null === $this->file) { @@ -94,9 +94,20 @@ public function upload($webDir, Filesystem $fileSystem) $uuid = uniqid(); $this->setUuid($uuid); - $url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName); - $content = \file_get_contents($this->file->getPathname()); - $fileSystem->write($url, $content); + if ($fileSystem) { + $url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName); + $content = \file_get_contents($this->file->getPathname()); + $fileSystem->write($url, $content); + } else { + @trigger_error( + 'Not passing the filesystem as the third argument of upload is deprecated since KunstmaanMediaBundle 5.7 and will be required in KunstmaanMediaBundle 6.0.', + E_USER_DEPRECATED + ); + + // move takes the target directory and then the target filename to move to + $this->file->move(sprintf('%s/%s', $uploadDir, $uuid), $safeFileName); + $url = sprintf('%s%s/', $webDir, $uuid) . $safeFileName; + } // set the path property to the filename where you'ved saved the file $this->fileName = $safeFileName; @@ -118,9 +129,15 @@ public function upload($webDir, Filesystem $fileSystem) */ public function onValidPost(Form $form, FormBuilderInterface $formBuilder, Request $request, ContainerInterface $container) { + $uploadDir = $container->getParameter('form_submission_rootdir'); $webDir = $container->getParameter('form_submission_webdir'); - $fileSystem = $container->get('kunstmaan_form.filesystem'); - $this->upload($webDir, $fileSystem); + + $fileSystem = null; + if ($container->has('kunstmaan_form.filesystem')) { + $fileSystem = $container->get('kunstmaan_form.filesystem'); + } + + $this->upload($uploadDir, $webDir, $fileSystem); } /** diff --git a/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php b/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php index 11dd3bb615..8175c3d6ec 100644 --- a/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php +++ b/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php @@ -115,12 +115,57 @@ public function testUpload() ->disableOriginalConstructor() ->getMock(); + $container->expects($this->any()) + ->method('has') + ->willReturn(true); + $container->expects($this->any()) ->method('get') ->willReturn($filesystem); $object->onValidPost($form, $builder, $request, $container); - $this->assertNull($object->upload('..', $filesystem)); - $object->upload(__DIR__ . '/../../Resources/assets/', $filesystem); + $this->assertNull($object->upload('...', '..', $filesystem)); + $object->upload(__DIR__ . '/../../Resources/assets/', __DIR__ . '/../../Resources/assets/', $filesystem); + } + + public function testUploadBC() + { + $object = $this->object; + $this->assertNull($object->upload('..', '..', null)); + + $file = $this->getMockBuilder(UploadedFile::class) + ->disableOriginalConstructor() + ->getMock(); + + $file->expects($this->any()) + ->method('move') + ->willReturn(true); + + $object->file = $file; + $object->upload(__DIR__ . '/../../Resources/assets/', __DIR__ . '/../../Resources/assets/', null); + + $form = $this->getMockBuilder(Form::class) + ->disableOriginalConstructor() + ->getMock(); + + $builder = $this->getMockBuilder(FormBuilder::class) + ->disableOriginalConstructor() + ->getMock(); + + $request = new Request(); + + $container = $this->getMockBuilder(Container::class) + ->disableOriginalConstructor() + ->getMock(); + + $container->expects($this->any()) + ->method('getParameter') + ->willReturn('whatever'); + + $container->expects($this->any()) + ->method('has') + ->willReturn(false); + + $object->onValidPost($form, $builder, $request, $container); } }