diff options
author | Olivier Paroz <github@oparoz.com> | 2015-09-21 00:38:37 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-09-21 00:38:37 +0300 |
commit | f67d0c2d78b4f28347727ca5384948bdb4e88f55 (patch) | |
tree | a7969d06e12776ceb709b62c00743357fabd205b /tests | |
parent | 6ee8b3c62c0543832ff3ba4ef90e9413dd4bf279 (diff) |
Added test for text/plain SVGs in getPreview
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/GalleryUnitTest.php | 4 | ||||
-rw-r--r-- | tests/unit/controller/PreviewApiControllerTest.php | 50 | ||||
-rw-r--r-- | tests/unit/controller/PreviewControllerTest.php | 58 |
3 files changed, 96 insertions, 16 deletions
diff --git a/tests/unit/GalleryUnitTest.php b/tests/unit/GalleryUnitTest.php index b506e3f5..44b75913 100644 --- a/tests/unit/GalleryUnitTest.php +++ b/tests/unit/GalleryUnitTest.php @@ -13,6 +13,8 @@ namespace Test; use OCP\ILogger; +use OCP\Files\File; +use OCP\Files\Folder; use OCA\Gallery\Environment\Environment; use OCA\Gallery\Service\ServiceException; @@ -52,7 +54,7 @@ abstract class GalleryUnitTest extends \Test\TestCase { * * @param object $mockedObject * @param int $fileId - * @param \PHPUnit_Framework_MockObject_MockObject $answer + * @param File|Folder $answer */ protected function mockGetResourceFromId($mockedObject, $fileId, $answer) { $mockedObject->expects($this->once()) diff --git a/tests/unit/controller/PreviewApiControllerTest.php b/tests/unit/controller/PreviewApiControllerTest.php index 958020ea..914889d8 100644 --- a/tests/unit/controller/PreviewApiControllerTest.php +++ b/tests/unit/controller/PreviewApiControllerTest.php @@ -14,6 +14,12 @@ namespace OCA\Gallery\Controller; require_once __DIR__ . '/PreviewControllerTest.php'; +use OCP\Files\File; + +use OCP\AppFramework\Http; + +use OCA\Gallery\Http\ImageResponse; + /** * Class PreviewApiControllerTest * @@ -21,6 +27,9 @@ require_once __DIR__ . '/PreviewControllerTest.php'; */ class PreviewApiControllerTest extends PreviewControllerTest { + /** @var PreviewApiController */ + protected $controller; + public function setUp() { parent::setUp(); $this->controller = new PreviewApiController( @@ -36,4 +45,45 @@ class PreviewApiControllerTest extends PreviewControllerTest { ); } + /** + * @return array + */ + public function providesTestDownloadData() { + return [ + [1234, $this->mockSvgFile(1234), true, 'image/svg+xml'], + [4567, $this->mockSvgFile(4567), false, 'text/plain'] + ]; + } + + /** + * @dataProvider providesTestDownloadData + * + * @param int $fileId + * @param File $file + * @param string $nativeSvg + * @param string $expectedMimeType + * + * @internal param string $type + */ + public function testGetPreviewOfSvg($fileId, $file, $nativeSvg, $expectedMimeType) { + $width = 1024; + $height = 768; + + /** @type File $file */ + $preview = $this->mockGetData( + $fileId, $file, $width, $height, $keepAspect = true, $animatedPreview = true, + $base64Encode = false, $previewRequired = false + ); + $preview['name'] = $file->getName(); + + /** @type ImageResponse $response */ + $response = $this->controller->getPreview($fileId, $width, $height, $nativeSvg); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + + $this->assertEquals( + $expectedMimeType . '; charset=utf-8', $response->getHeaders()['Content-type'] + ); + } + } diff --git a/tests/unit/controller/PreviewControllerTest.php b/tests/unit/controller/PreviewControllerTest.php index 59b16bd7..d8f9f44b 100644 --- a/tests/unit/controller/PreviewControllerTest.php +++ b/tests/unit/controller/PreviewControllerTest.php @@ -157,10 +157,11 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $base64Encode ]; $this->mockGetThumbnailSpecs($square, $scale, $thumbnailSpecs); - - list($file, $mockedPreview) = + /** @type File $file */ + $file = $this->mockJpgFile($thumbnailId); + $mockedPreview = $this->mockGetData( - $thumbnailId, $width, $height, $aspect, $animatedPreview, $base64Encode + $thumbnailId, $file, $width, $height, $aspect, $animatedPreview, $base64Encode ); $this->mockPreviewValidator($square, $base64Encode, $mockedPreview['preview']); @@ -231,7 +232,8 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $height = 768; /** @type File $file */ - list($file, $preview) = $this->mockGetData($fileId, $width, $height); + $file = $this->mockJpgFile($fileId); + $preview = $this->mockGetData($fileId, $file, $width, $height); $preview['name'] = $file->getName(); /** @type ImageResponse $response */ @@ -282,26 +284,34 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { * Mocks Preview->getData * * @param int $fileId the ID of the file of which we need a large preview of + * @param File $file * @param int $width * @param int $height * @param bool $keepAspect * @param bool $animatedPreview * @param bool $base64Encode + * @param bool $previewRequired * * @return array */ - private function mockGetData( - $fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false + protected function mockGetData( + $fileId, $file, $width, $height, $keepAspect = true, $animatedPreview = true, + $base64Encode = false, $previewRequired = true ) { - $file = $this->mockJpgFile($fileId); $this->mockGetResourceFromId($this->previewService, $fileId, $file); - $this->mockIsPreviewRequired($file, $animatedPreview, true); - $previewData = $this->mockPreviewData($file); + $this->mockIsPreviewRequired($file, $animatedPreview, $previewRequired); + $previewData = $this->mockPreviewData($file, $previewRequired); - $this->mockCreatePreview($file, $width, $height, $keepAspect, $base64Encode, $previewData); + if ($previewRequired) { + $this->mockCreatePreview( + $file, $width, $height, $keepAspect, $base64Encode, $previewData + ); + } else { + $this->mockDownloadFile($file, $base64Encode, $previewData); + } - return [$file, $previewData]; + return $previewData; } /** @@ -399,14 +409,17 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { } /** - * @param object|\PHPUnit_Framework_MockObject_MockObject $file + * @param File $file + * @param bool $previewRequired * - * @return array<string,mixed> + * @return array <string,mixed> */ - private function mockPreviewData($file) { + private function mockPreviewData($file, $previewRequired) { + $mimeType = $previewRequired ? 'image/png' : $file->getMimeType(); + $preview = [ 'preview' => $file->getContent(), // Not a real preview, but it's not important - 'mimetype' => 'image/png', //Most previews are PNGs + 'mimetype' => $mimeType, ]; return $preview; @@ -473,6 +486,21 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { } /** + * @param $file + * @param $base64Encode + * @param $preview + */ + private function mockDownloadFile($file, $base64Encode, $preview) { + $this->downloadService->expects($this->once()) + ->method('downloadFile') + ->with( + $this->equalTo($file), + $this->equalTo($base64Encode) + ) + ->willReturn($preview); + } + + /** * @param $event * @param $data * @param $message |