diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-19 02:52:12 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-19 02:52:13 +0300 |
commit | 7d393650e7e231a68038ccb9fd069cd95746806b (patch) | |
tree | 1ff0b8eab9bc03c84d995269b00afd40d928e0d8 /tests/unit | |
parent | 8a002c4633c896c531d60d4042fe5276172bb157 (diff) |
Add download API
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/controller/FilesApiControllerTest.php | 38 | ||||
-rw-r--r-- | tests/unit/controller/FilesControllerTest.php | 202 | ||||
-rw-r--r-- | tests/unit/controller/FilesPublicControllerTest.php | 37 |
3 files changed, 277 insertions, 0 deletions
diff --git a/tests/unit/controller/FilesApiControllerTest.php b/tests/unit/controller/FilesApiControllerTest.php new file mode 100644 index 00000000..e0f00ddb --- /dev/null +++ b/tests/unit/controller/FilesApiControllerTest.php @@ -0,0 +1,38 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Controller; + +require_once __DIR__ . '/FilesControllerTest.php'; + +/** + * Class FilesApiControllerTest + * + * @package OCA\Gallery\Controller + */ +class FilesApiControllerTest extends FilesControllerTest { + + public function setUp() { + parent::setUp(); + $this->controller = new FilesController( + $this->appName, + $this->request, + $this->urlGenerator, + $this->searchFolderService, + $this->configService, + $this->searchMediaService, + $this->downloadService, + $this->logger + ); + } + +} diff --git a/tests/unit/controller/FilesControllerTest.php b/tests/unit/controller/FilesControllerTest.php new file mode 100644 index 00000000..8aaaf9fe --- /dev/null +++ b/tests/unit/controller/FilesControllerTest.php @@ -0,0 +1,202 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Controller; + +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\ILogger; + +use OCP\AppFramework\IAppContainer; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\RedirectResponse; + +use OCA\Gallery\AppInfo\Application; +use OCA\Gallery\Http\ImageResponse; +use OCA\Gallery\Service\SearchFolderService; +use OCA\Gallery\Service\ConfigService; +use OCA\Gallery\Service\SearchMediaService; +use OCA\Gallery\Service\DownloadService; + +/** + * Class FilesControllerTest + * + * @package OCA\Gallery\Controller + */ +class FilesControllerTest extends \Test\TestCase { + + /** @var IAppContainer */ + protected $container; + /** @var string */ + protected $appName = 'gallery'; + /** @var IRequest */ + protected $request; + /** @var ConfigController */ + protected $controller; + /** @var IURLGenerator */ + protected $urlGenerator; + /** @var SearchFolderService */ + protected $searchFolderService; + /** @var ConfigService */ + protected $configService; + /** @var SearchMediaService */ + protected $searchMediaService; + /** @var DownloadService */ + protected $downloadService; + /** @var ILogger */ + protected $logger; + + /** + * Test set up + */ + public function setUp() { + parent::setUp(); + + $app = new Application; + $this->container = $app->getContainer(); + $this->container['UserFolder'] = $this->getMockBuilder('OCP\Files\Folder') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator') + ->disableOriginalConstructor() + ->getMock(); + $this->searchFolderService = + $this->getMockBuilder('\OCA\Gallery\Service\SearchFolderService') + ->disableOriginalConstructor() + ->getMock(); + $this->configService = $this->getMockBuilder('\OCA\Gallery\Service\ConfigService') + ->disableOriginalConstructor() + ->getMock(); + $this->searchMediaService = $this->getMockBuilder('\OCA\Gallery\Service\SearchMediaService') + ->disableOriginalConstructor() + ->getMock(); + $this->downloadService = $this->getMockBuilder('\OCA\Gallery\Service\DownloadService') + ->disableOriginalConstructor() + ->getMock(); + $this->logger = $this->getMockBuilder('\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new FilesController( + $this->appName, + $this->request, + $this->urlGenerator, + $this->searchFolderService, + $this->configService, + $this->searchMediaService, + $this->downloadService, + $this->logger + ); + } + + public function testDownload() { + $fileId = 1234; + $filename = null; + + $download = $this->mockGetDownload($fileId, $filename); + + /** @type ImageResponse $response */ + $response = $this->controller->download($fileId, $filename); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals( + $download['mimetype'] . '; charset=utf-8', $response->getHeaders()['Content-type'] + ); + $this->assertEquals($download['preview'], $response->render()); + } + + public function testDownloadWithWrongId() { + $fileId = 99999; + $filename = null; + + $this->mockGetDownloadWithWrongId($fileId); + + $redirect = new RedirectResponse( + $this->urlGenerator->linkToRoute($this->appName . '.page.error_page') + ); + + $response = $this->controller->download($fileId, $filename); + + $this->assertEquals($redirect->getRedirectURL(), $response->getRedirectURL()); + } + + + /** + * Mocks Files->getDownload + * + * @param int $fileId the ID of the file of which we need a large preview of + * @param string|null $filename + * + * @return array + */ + private function mockGetDownload($fileId, $filename) { + $file = $this->mockFile($fileId); + + $this->downloadService->expects($this->once()) + ->method('getResourceFromId') + ->with($this->equalTo($fileId)) + ->willReturn($file); + + $download = [ + 'preview' => $file->getContent(), + 'mimetype' => $file->getMimeType(), + ]; + + if ($download) { + if (is_null($filename)) { + $filename = $file->getName(); + } + $download['name'] = $filename; + } + + $this->downloadService->expects($this->once()) + ->method('downloadFile') + ->with($this->equalTo($file)) + ->willReturn($download); + + return $download; + } + + /** + * @param int $fileId + */ + private function mockGetDownloadWithWrongId($fileId) { + $this->downloadService->expects($this->once()) + ->method('getResourceFromId') + ->with($this->equalTo($fileId)) + ->willReturn(false); + } + + /** + * @param $fileId + * + * @return object|\PHPUnit_Framework_MockObject_MockObject + */ + private function mockFile($fileId) { + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $file->method('getId') + ->willReturn($fileId); + $file->method('getContent') + ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); + $file->method('getName') + ->willReturn('testimage.jpg'); + $file->method('getMimeType') + ->willReturn('image/jpeg'); + + return $file; + } + +} diff --git a/tests/unit/controller/FilesPublicControllerTest.php b/tests/unit/controller/FilesPublicControllerTest.php new file mode 100644 index 00000000..5af98480 --- /dev/null +++ b/tests/unit/controller/FilesPublicControllerTest.php @@ -0,0 +1,37 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Controller; + +require_once __DIR__ . '/FilesControllerTest.php'; +/** + * Class FilesPublicControllerTest + * + * @package OCA\Gallery\Controller + */ +class FilesPublicControllerTest extends FilesControllerTest { + + public function setUp() { + parent::setUp(); + $this->controller = new FilesController( + $this->appName, + $this->request, + $this->urlGenerator, + $this->searchFolderService, + $this->configService, + $this->searchMediaService, + $this->downloadService, + $this->logger + ); + } + +} |