diff --git a/Doctrine/Phpcr/MediaManager.php b/Doctrine/Phpcr/MediaManager.php index ef3dfa7..28c3cda 100644 --- a/Doctrine/Phpcr/MediaManager.php +++ b/Doctrine/Phpcr/MediaManager.php @@ -39,17 +39,6 @@ public function setManagerName($managerName) $this->managerName = $managerName; } - /** - * Set the class to use to get the file object; - * if not called, the default class will be used. - * - * @param string $class fully qualified class name of file - */ - public function setClass($class) - { - $this->class = $class; - } - /** * Set the root path were the file system is located; * if not called, the default root path will be used. diff --git a/Editor/Helper/UploadCkeditorHelper.php b/Editor/Helper/UploadCkeditorHelper.php index e39dcac..d97f261 100644 --- a/Editor/Helper/UploadCkeditorHelper.php +++ b/Editor/Helper/UploadCkeditorHelper.php @@ -10,9 +10,15 @@ class UploadCkeditorHelper extends UploadDefaultHelper /** * {@inheritdoc} */ - public function getUploadResponse(Request $request, FileInterface $file) + public function getUploadResponse(Request $request, array $files) { - $urlSafePath = $this->mediaManager->getUrlSafePath($file); + if (!isset($files[0]) && !$files[0] instanceof FileInterface) { + throw new \InvalidArgumentException( + 'Provide at least one Symfony\Cmf\Bundle\MediaBundle\FileInterface file.' + ); + } + + $urlSafePath = $this->mediaManager->getUrlSafePath($files[0]); $url = $this->router->generate('cmf_media_image_display', array('path' => $urlSafePath)); $funcNum = $request->query->get('CKEditorFuncNum'); diff --git a/Editor/Helper/UploadDefaultHelper.php b/Editor/Helper/UploadDefaultHelper.php index bb11697..887b7b7 100644 --- a/Editor/Helper/UploadDefaultHelper.php +++ b/Editor/Helper/UploadDefaultHelper.php @@ -4,6 +4,7 @@ use Symfony\Cmf\Bundle\MediaBundle\Editor\UploadEditorHelperInterface; use Symfony\Cmf\Bundle\MediaBundle\FileInterface; +use Symfony\Cmf\Bundle\MediaBundle\ImageInterface; use Symfony\Cmf\Bundle\MediaBundle\MediaManagerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -45,10 +46,20 @@ public function setFileDefaults(Request $request, FileInterface $file) /** * {@inheritdoc} */ - public function getUploadResponse(Request $request, FileInterface $file) + public function getUploadResponse(Request $request, array $files) { - $urlSafePath = $this->mediaManager->getUrlSafePath($file); + if (!isset($files[0]) && !$files[0] instanceof FileInterface) { + throw new \InvalidArgumentException( + 'Provide at least one Symfony\Cmf\Bundle\MediaBundle\FileInterface file.' + ); + } + + $urlSafePath = $this->mediaManager->getUrlSafePath($files[0]); - return new RedirectResponse($this->router->generate('cmf_media_image_display', array('path' => $urlSafePath))); + if ($files[0] instanceof ImageInterface) { + return new RedirectResponse($this->router->generate('cmf_media_image_display', array('path' => $urlSafePath))); + } else { + return new RedirectResponse($request->headers->get('referer')); + } } } \ No newline at end of file diff --git a/Editor/UploadEditorHelperInterface.php b/Editor/UploadEditorHelperInterface.php index 392a253..b8b13f7 100644 --- a/Editor/UploadEditorHelperInterface.php +++ b/Editor/UploadEditorHelperInterface.php @@ -23,7 +23,12 @@ public function setFileDefaults(Request $request, FileInterface $file); /** * Get a response for the upload action of the editor * + * @param Request $request + * @param FileInterface[] $files + * * @return Response + * + * @throws InvalidArgumentException if no FileInterface file is provided */ - public function getUploadResponse(Request $request, FileInterface $file); + public function getUploadResponse(Request $request, array $files); } \ No newline at end of file diff --git a/File/UploadFileHelper.php b/File/UploadFileHelper.php index 608ed70..bba4f1d 100644 --- a/File/UploadFileHelper.php +++ b/File/UploadFileHelper.php @@ -89,8 +89,8 @@ protected function getObjectManager() /** * Add an editor helper * - * @param string $name - * @param EditorHelperInterface $helper + * @param string $name + * @param UploadEditorHelperInterface $helper */ public function addEditorHelper($name, UploadEditorHelperInterface $helper) { @@ -125,14 +125,43 @@ protected function validateFile(UploadedFile $file) return true; } + /** + * Handle the UploadedFile and create a FileInterface object specified by + * the configured class. + * + * @param Request $request + * @param UploadedFile $uploadedFile + * + * @return FileInterface + */ + public function handleUploadedFile(UploadedFile $uploadedFile) + { + $this->validateFile($uploadedFile); + + /** @var $file FileInterface */ + $file = new $this->class; + $file->setName($uploadedFile->getClientOriginalName()); + $file->copyContentFromFile($uploadedFile); + + try { + $this->mediaManager->setDefaults($file, $this->rootPath); + } catch (\RuntimeException $e) { + throw new HttpException(409, $e->getMessage()); + } + + return $file; + } + /** * Process upload and get a response * - * @param Request $request + * @param Request $request + * @param UploadedFile[] $uploadedFiles optionally get the uploaded file(s) + * from the Request yourself * * @return Response */ - public function getUploadResponse(Request $request) + public function getUploadResponse(Request $request, array $uploadedFiles = array()) { /** @var \Symfony\Cmf\Bundle\MediaBundle\Editor\EditorHelperInterface $editorHelper */ $editorHelper = $this->getEditorHelper($request->get('editor', 'default')); @@ -144,30 +173,27 @@ public function getUploadResponse(Request $request) )); } - $files = $request->files; + if (count($uploadedFiles) === 0) { + // by default get the first file + $uploadedFiles = array($request->files->getIterator()->current()); + } - /** @var $file UploadedFile */ - $uploadedFile = $files->getIterator()->current(); - $this->validateFile($uploadedFile); + // handle the uploaded file(s) + $files = array(); + foreach ($uploadedFiles as $uploadedFile) { + $file = $this->handleUploadedFile($uploadedFile); - /** @var $image FileInterface */ - $file = new $this->class; - $file->setName($uploadedFile->getClientOriginalName()); - $file->copyContentFromFile($uploadedFile); + $editorHelper->setFileDefaults($request, $file); - $editorHelper->setFileDefaults($request, $file); + $this->getObjectManager()->persist($file); - try { - $this->mediaManager->setDefaults($file, $this->rootPath); - } catch (\RuntimeException $e) { - throw new HttpException(409, $e->getMessage()); + $files[] = $file; } - // persist - $this->getObjectManager()->persist($file); + // write created FileInterface file(s) to storage $this->getObjectManager()->flush(); // response - return $editorHelper->getUploadResponse($request, $file); + return $editorHelper->getUploadResponse($request, $files); } } \ No newline at end of file diff --git a/Resources/config/routing/file.xml b/Resources/config/routing/file.xml index 76401f6..736a24d 100644 --- a/Resources/config/routing/file.xml +++ b/Resources/config/routing/file.xml @@ -10,7 +10,7 @@ .* - + cmf_media.file.controller:uploadAction default json diff --git a/Resources/config/routing/image.xml b/Resources/config/routing/image.xml index 700a220..a64d890 100644 --- a/Resources/config/routing/image.xml +++ b/Resources/config/routing/image.xml @@ -10,4 +10,11 @@ .* + + cmf_media.image.controller:uploadAction + default + json + POST + + diff --git a/Tests/Functional/Doctrine/Phpcr/MediaManagerTest.php b/Tests/Functional/Doctrine/Phpcr/MediaManagerTest.php new file mode 100644 index 0000000..8692ec1 --- /dev/null +++ b/Tests/Functional/Doctrine/Phpcr/MediaManagerTest.php @@ -0,0 +1,183 @@ +containerMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface') + ->disableOriginalConstructor() + ->getMock() + ; + $this->registryMock = $this->getMockBuilder('Doctrine\Bundle\PHPCRBundle\ManagerRegistry') + ->disableOriginalConstructor() + ->getMock() + ; + $this->dmMock = $this->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') + ->disableOriginalConstructor() + ->getMock() + ; + $this->testRoot = new Directory(); + $this->testRoot->setId('/test/media'); + } + + private function getMediaManager() + { + return new MediaManager($this->registryMock, 'themanager', '/test/media'); + } + + public function testGetPath() + { + $media = new Media(); + $media->setId('/test/media/mymedia'); + $mediaManager = $this->getMediaManager(); + + $this->assertEquals('/test/media/mymedia', $mediaManager->getPath($media)); + } + + public function testGetUrlSafePath() + { + $media = new Media(); + $media->setId('/test/media/mymedia'); + $mediaManager = $this->getMediaManager(); + + $this->assertEquals('test/media/mymedia', $mediaManager->getUrlSafePath($media)); + } + + public function setDefaultsProvider() + { + return array( + array('mymedia', 'mymedia'), + array('mymedia', null, false, '/test/media/mymedia'), + array(null, 'mymedia', true), + ); + } + + /** + * @dataProvider setDefaultsProvider + */ + public function testSetDefaults($expectedName = null, $name = null, $nameExists = false, $id = null) + { + $returnMediaExists = $nameExists ? new Media() : null; + $rootPath = '/test/media/file'; + $managerName = 'anothermanager'; + + $this->registryMock->expects($this->once()) + ->method('getManager') + ->with($this->equalTo($managerName)) + ->will($this->returnValue($this->dmMock)) + ; + $this->dmMock->expects($this->any()) + ->method('find') + ->will($this->returnValueMap(array( + array('Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Media', $rootPath.'/'.$name, $returnMediaExists), + array(null, $rootPath, $this->testRoot), + ))) + ; + + $media = new Media(); + $media->setId($id); + $media->setName($name); + + $mediaManager = $this->getMediaManager(); + $mediaManager->setRootPath($rootPath); + $mediaManager->setManagerName($managerName); + $mediaManager->setDefaults($media); + + $this->assertEquals($this->testRoot, $media->getParent()); + if ($expectedName) { + $this->assertEquals($expectedName, $media->getName()); + } else { + $this->assertNotEquals($name, $media->getName()); + } + } + + /** + * @expectedException \RuntimeException + */ + public function testSetDefaultsException() + { + $media = new Media(); + + $mediaManager = $this->getMediaManager(); + $mediaManager->setDefaults($media); + } + + public function mapPathToIdProvider() + { + return array( + array('/test/media/mymedia', null), + array('/test/media/mymedia', '/test/media'), + ); + } + + /** + * @dataProvider mapPathToIdProvider + */ + public function testMapPathToId($path, $rootPath) + { + $mediaManager = $this->getMediaManager(); + + $this->assertEquals('/test/media/mymedia', $mediaManager->mapPathToId($path, $rootPath)); + } + + /** + * @expectedException \OutOfBoundsException + */ + public function testMapPathToIdException() + { + $mediaManager = $this->getMediaManager(); + + $mediaManager->mapPathToId('/test/media/mymedia', '/out/of/bound'); + } + + public function mapUrlSafePathToIdProvider() + { + return array( + array('test/media/mymedia', null), + array('test/media/mymedia', '/test/media'), + ); + } + + /** + * @dataProvider mapUrlSafePathToIdProvider + */ + public function testMapUrlSafePathToId($path, $rootPath) + { + $mediaManager = $this->getMediaManager(); + + $this->assertEquals('/test/media/mymedia', $mediaManager->mapPathToId($path, $rootPath)); + } + + /** + * @expectedException \OutOfBoundsException + */ + public function testMapUrlSafePathToIdException() + { + $mediaManager = $this->getMediaManager(); + + $mediaManager->mapUrlSafePathToId('test/media/mymedia', '/out/of/bound'); + } +} \ No newline at end of file diff --git a/Tests/Resources/Controller/PhpcrFileTestController.php b/Tests/Resources/Controller/PhpcrFileTestController.php new file mode 100644 index 0000000..ba6c008 --- /dev/null +++ b/Tests/Resources/Controller/PhpcrFileTestController.php @@ -0,0 +1,63 @@ +generateUrl('cmf_media_file_upload', array('editor' => $editor)); + } else { + $action = $this->generateUrl('phpcr_file_test_upload'); + } + + return $this->container->get('form.factory')->createNamedBuilder(null, 'form', null, array('action' => $action)) + ->add('file', 'file') + ->add('submit', 'submit') + ->getForm() + ; + } + + public function fileAction(Request $request) + { + $fileClass = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File'; + $dm = $this->get('doctrine_phpcr')->getManager('default'); + $files = $dm->getRepository($fileClass)->findAll(); + + $uploadForm = $this->getUploadForm(); + $editorUploadForm = $this->getUploadForm('default'); + + return $this->render('::tests/file.html.twig', array( + 'upload_form' => $uploadForm->createView(), + 'editor_form' => $editorUploadForm->createView(), + 'files' => $files, + )); + } + + public function uploadAction(Request $request) + { + $form = $this->getUploadForm(); + $form->handleRequest($request); + + if ($form->isValid()) { + /** @var UploadFileHelper $uploadFileHelper */ + $uploadFileHelper = $this->get('cmf_media.upload_file_helper'); + + $uploadedFile = $request->files->get('file'); + + $file = $uploadFileHelper->handleUploadedFile($uploadedFile); + + // persist + $dm = $this->get('doctrine_phpcr')->getManager('default'); + $dm->persist($file); + $dm->flush(); + } + + return $this->redirect($this->generateUrl('phpcr_file_test')); + } +} diff --git a/Tests/Resources/Controller/TestController.php b/Tests/Resources/Controller/TestController.php index 8654afa..3b4ab85 100644 --- a/Tests/Resources/Controller/TestController.php +++ b/Tests/Resources/Controller/TestController.php @@ -7,21 +7,8 @@ class TestController extends Controller { - private $fileClass = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File'; - public function indexAction(Request $request) { return $this->render('::index.html.twig'); } - - public function fileAction(Request $request) - { - $files = $this->get('doctrine_phpcr') - ->getRepository($this->fileClass) - ->findAll(); - - return $this->render('::tests/file.html.twig', array( - 'files' => $files, - )); - } } diff --git a/Tests/Resources/DataFixtures/Phpcr/LoadMediaData.php b/Tests/Resources/DataFixtures/Phpcr/LoadMediaData.php index 36ec893..e798966 100644 --- a/Tests/Resources/DataFixtures/Phpcr/LoadMediaData.php +++ b/Tests/Resources/DataFixtures/Phpcr/LoadMediaData.php @@ -1,6 +1,6 @@ find(null, '/test/media'); + $root = $manager->find(null, '/test'); + $mediaRoot = new Generic; + $mediaRoot->setNodename('media'); + $mediaRoot->setParent($root); + $manager->persist($mediaRoot); // File $file = new File(); - $file->setParent($root); + $file->setParent($mediaRoot); $file->setName('file-1.txt'); $file->setContentFromString('Test file 1.'); $file->setContentType('text/plain'); diff --git a/Tests/Resources/app/Resources/data/testfile.txt b/Tests/Resources/app/Resources/data/testfile.txt new file mode 100644 index 0000000..24ac5f9 --- /dev/null +++ b/Tests/Resources/app/Resources/data/testfile.txt @@ -0,0 +1 @@ +This is a test file used to test uploads. \ No newline at end of file diff --git a/Tests/Resources/app/Resources/views/index.html.twig b/Tests/Resources/app/Resources/views/index.html.twig index 43a4806..ab09364 100644 --- a/Tests/Resources/app/Resources/views/index.html.twig +++ b/Tests/Resources/app/Resources/views/index.html.twig @@ -1,8 +1,9 @@ {% extends "::layout.html.twig" %} {% block content %}

Tests

+

Phpcr

About

This test application is built into the MediaBundle. You can easily run diff --git a/Tests/Resources/app/Resources/views/tests/file.html.twig b/Tests/Resources/app/Resources/views/tests/file.html.twig index 4dedfbc..386b00b 100644 --- a/Tests/Resources/app/Resources/views/tests/file.html.twig +++ b/Tests/Resources/app/Resources/views/tests/file.html.twig @@ -1,9 +1,24 @@ {% extends "::layout.html.twig" %} {% block content %} -

Download file

- +

Upload file

+

Standard upload

+

The upload is handled by your own controller action. The UploadFileHelper is used to handle the upload and creates a + FileInterface object, writing the object to storage has to be implemented by yourself.

+{{ form(upload_form, { attr: { class: 'standard' } }) }} +

Web editor upload (default)

+

The upload is completely handled by the FileController::uploadAction. The UploadFileHelper will process the upload, + writes the created FileInterface object to storage and returns a response that depends on the editor defined in the + request.

+{{ form(editor_form, { attr: { class: 'editor default' } }) }} + +

Download file(s)

+{% if files is empty %} +

No files found, upload a file first.

+{% else %} + +{% endif %} {% endblock %} diff --git a/Tests/Resources/app/config/config.php b/Tests/Resources/app/config/config.php index 0682570..c74268e 100644 --- a/Tests/Resources/app/config/config.php +++ b/Tests/Resources/app/config/config.php @@ -3,3 +3,4 @@ $loader->import(CMF_TEST_CONFIG_DIR . '/default.php'); $loader->import(CMF_TEST_CONFIG_DIR . '/phpcr_odm.php'); $loader->import(__DIR__.'/cmf_media.yml'); +$loader->import(__DIR__.'/security.yml'); diff --git a/Tests/Resources/app/config/routing/cmf_media.yml b/Tests/Resources/app/config/routing/cmf_media.yml index 279f1c7..df944ab 100644 --- a/Tests/Resources/app/config/routing/cmf_media.yml +++ b/Tests/Resources/app/config/routing/cmf_media.yml @@ -3,10 +3,20 @@ test_index: defaults: _controller: Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller\TestController::indexAction -file_test: - pattern: /file-test +phpcr_file_test: + pattern: /phpcr/file-test defaults: - _controller: Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller\TestController::fileAction + _controller: Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller\PhpcrFileTestController::fileAction + +phpcr_file_test_upload: + pattern: /phpcr/file-test/upload + defaults: + _controller: Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller\PhpcrFileTestController::uploadAction + +phpcr_file_test_upload_editor: + pattern: /phpcr/file-test/upload/editor + defaults: + _controller: Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller\PhpcrFileTestController::editorUploadAction cmf_media_file: resource: "@CmfMediaBundle/Resources/config/routing/file.xml" diff --git a/Tests/Resources/app/config/security.yml b/Tests/Resources/app/config/security.yml new file mode 100644 index 0000000..d7224fa --- /dev/null +++ b/Tests/Resources/app/config/security.yml @@ -0,0 +1,4 @@ +security: + role_hierarchy: + ROLE_ADMIN: [ROLE_USER, ROLE_CAN_UPLOAD_FILE] + ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] diff --git a/Tests/Unit/File/UploadFileHelperTest.php b/Tests/Unit/File/UploadFileHelperTest.php new file mode 100644 index 0000000..3de6829 --- /dev/null +++ b/Tests/Unit/File/UploadFileHelperTest.php @@ -0,0 +1,207 @@ +containerMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface') + ->disableOriginalConstructor() + ->getMock() + ; + $this->registryMock = $this->getMockBuilder('Doctrine\Bundle\PHPCRBundle\ManagerRegistry') + ->disableOriginalConstructor() + ->getMock() + ; + $this->dmMock = $this->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') + ->disableOriginalConstructor() + ->getMock() + ; + $this->mediaManagerMock = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\MediaManager') + ->disableOriginalConstructor() + ->getMock() + ; + $this->class = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File'; + $this->rootPath = '/test/media'; + } + + private function getUploadFileHelper() + { + return new UploadFileHelper($this->registryMock, 'themanager', $this->class, $this->rootPath, $this->mediaManagerMock); + } + + public function testAddGetEditorHelper() + { + $uploadFileHelper = $this->getUploadFileHelper(); + + $this->assertNull($uploadFileHelper->getEditorHelper()); + $this->assertNull($uploadFileHelper->getEditorHelper('unknown')); + + $uploadDefaultHelper = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadDefaultHelper') + ->disableOriginalConstructor() + ->getMock() + ; + $uploadCkeditorHelper = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadCkeditorHelper') + ->disableOriginalConstructor() + ->getMock() + ; + + $uploadFileHelper->addEditorHelper('default', $uploadDefaultHelper); + $uploadFileHelper->addEditorHelper('ckeditor', $uploadCkeditorHelper); + + $this->assertEquals($uploadDefaultHelper, $uploadFileHelper->getEditorHelper('default')); + $this->assertEquals($uploadDefaultHelper, $uploadFileHelper->getEditorHelper('unknown')); + $this->assertEquals($uploadCkeditorHelper, $uploadFileHelper->getEditorHelper('ckeditor')); + } + + public function testHandleUploadedFile() + { + vfsStream::setup('home'); + $testFile = vfsStream::url('home/test.txt'); + file_put_contents($testFile, "Test file content."); + + $class = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File'; + $uploadFileHelper = $this->getUploadFileHelper(); + $uploadFileHelper->setClass($class); + $uploadFileHelper->setRootPath($this->rootPath.'/file'); + $uploadedFile = new UploadedFile($testFile, 'test.txt'); + + $this->mediaManagerMock->expects($this->once()) + ->method('setDefaults') + ->with( + $this->isInstanceOf($this->class), + $this->equalTo($this->rootPath.'/file') + ) + ; + + $file = $uploadFileHelper->handleUploadedFile($uploadedFile); + + $this->assertInstanceOf($class, $file); + $this->assertEquals('test.txt', $file->getName()); + } + + /** + * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testHandleUploadedFileException() + { + vfsStream::setup('home'); + $testFile = vfsStream::url('home/test.txt'); + file_put_contents($testFile, "Test file content."); + + $uploadFileHelper = $this->getUploadFileHelper(); + $uploadedFile = new UploadedFile($testFile, 'test.txt'); + + $this->mediaManagerMock->expects($this->once()) + ->method('setDefaults') + ->will($this->throwException(new \RuntimeException())) + ; + + $uploadFileHelper->handleUploadedFile($uploadedFile); + } + + public function testGetUploadedResponse() + { + vfsStream::setup('home'); + $testFile = vfsStream::url('home/test.txt'); + file_put_contents($testFile, "Test file content."); + + $request = new Request(); + $request->files->set('file', new UploadedFile($testFile, 'test.txt')); + $response = new Response('upload response'); + + $uploadFileHelper = $this->getUploadFileHelper(); + + $uploadDefaultHelper = $this->getMockBuilder('Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadDefaultHelper') + ->disableOriginalConstructor() + ->getMock() + ; + $uploadFileHelper->addEditorHelper('default', $uploadDefaultHelper); + + $uploadDefaultHelper->expects($this->once()) + ->method('setFileDefaults') + ->with( + $this->equalTo($request), + $this->isInstanceOf($this->class) + ) + ; + + $class = $this->class; + $uploadDefaultHelper->expects($this->once()) + ->method('getUploadResponse') + ->with( + $this->equalTo($request), + $this->callback(function ($files) use ($class) { + return isset($files[0]) && $files[0] instanceof $class; + }) + ) + ->will($this->returnValue($response)) + ; + + $this->mediaManagerMock->expects($this->once()) + ->method('setDefaults') + ->with( + $this->isInstanceOf($this->class), + $this->equalTo($this->rootPath) + ) + ; + + $uploadFileHelper->setManagerName('anothermanager'); + $this->registryMock->expects($this->any()) + ->method('getManager') + ->with($this->equalTo('anothermanager')) + ->will($this->returnValue($this->dmMock)) + ; + $this->dmMock->expects($this->once()) + ->method('persist') + ->with($this->isInstanceOf($this->class)) + ; + $this->dmMock->expects($this->once()) + ->method('flush') + ; + + $this->assertEquals($response, $uploadFileHelper->getUploadResponse($request)); + } + + /** + * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testGetUploadResponseException() + { + $uploadFileHelper = $this->getUploadFileHelper(); + + $uploadFileHelper->getUploadResponse(new Request()); + } +} \ No newline at end of file diff --git a/Tests/WebTest/TestApp/FileTest.php b/Tests/WebTest/TestApp/FileTest.php index a7449ee..9981864 100644 --- a/Tests/WebTest/TestApp/FileTest.php +++ b/Tests/WebTest/TestApp/FileTest.php @@ -3,14 +3,80 @@ namespace Symfony\Cmf\Bundle\MediaBundle\Tests\WebTest\TestApp; use Symfony\Cmf\Component\Testing\Functional\BaseTestCase; +use Symfony\Component\HttpFoundation\BinaryFileResponse; class FileTest extends BaseTestCase { + private $testDataDir; + + public function setUp() + { + $this->db('PHPCR')->loadFixtures(array( + 'Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\DataFixtures\Phpcr\LoadMediaData', + )); + $this->testDataDir = $this->getContainer()->get('kernel')->getRootDir() . '/Resources/data'; + } + public function testPage() { $client = $this->createClient(); - $client->request('get', $this->getContainer()->get('router')->generate('file_test')); + $crawler = $client->request('get', $this->getContainer()->get('router')->generate('phpcr_file_test')); + $resp = $client->getResponse(); + + $this->assertEquals(200, $resp->getStatusCode()); + $this->assertGreaterThanOrEqual(1, $crawler->filter('.downloads li a')->count()); + } + + public function testUpload() + { + $client = $this->createClient(); + $crawler = $client->request('get', $this->getContainer()->get('router')->generate('phpcr_file_test')); + $cntDownloadLinks = $crawler->filter('.downloads li a')->count(); + + $buttonCrawlerNode = $crawler->filter('form.standard')->selectButton('submit'); + $form = $buttonCrawlerNode->form(); + $form['file']->upload($this->testDataDir . '/testfile.txt'); + + $client->submit($form); + $crawler = $client->followRedirect(); + $resp = $client->getResponse(); + + $this->assertEquals(200, $resp->getStatusCode()); + $this->assertEquals($cntDownloadLinks + 1, $crawler->filter('.downloads li a')->count()); + } + + public function testEditorUpload() + { + $client = $this->createClient(array(), array( + 'PHP_AUTH_USER' => 'admin', + 'PHP_AUTH_PW' => 'adminpass', + )); + $crawler = $client->request('get', $this->getContainer()->get('router')->generate('phpcr_file_test')); + $cntDownloadLinks = $crawler->filter('.downloads li a')->count(); + + $buttonCrawlerNode = $crawler->filter('form.editor.default')->selectButton('submit'); + $form = $buttonCrawlerNode->form(); + $form['file']->upload($this->testDataDir . '/testfile.txt'); + + $client->submit($form); + $crawler = $client->followRedirect(); + $resp = $client->getResponse(); + + $this->assertEquals(200, $resp->getStatusCode()); + $this->assertEquals($cntDownloadLinks + 1, $crawler->filter('.downloads li a')->count()); + } + + public function testDownload() + { + $client = $this->createClient(); + $crawler = $client->request('get', $this->getContainer()->get('router')->generate('phpcr_file_test')); + + // find first download link + $link = $crawler->filter('.downloads li a')->eq(0)->link(); + $client->click($link); $resp = $client->getResponse(); + $this->assertEquals(200, $resp->getStatusCode()); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\BinaryFileResponse', $resp); } } diff --git a/composer.json b/composer.json index 9903aaa..b2f28ca 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "require-dev": { "symfony-cmf/testing": "1.0.*", "jms/serializer-bundle": "0.12.*", - "liip/imagine-bundle": "dev-master" + "liip/imagine-bundle": "dev-master", + "mikey179/vfsStream": "~1.2" }, "suggest": { "knplabs/gaufrette": "When using the Gaufrette adapter",