diff options
-rw-r--r-- | tests/unit/service/DownloadServiceTest.php | 8 | ||||
-rw-r--r-- | tests/unit/service/FilesServiceTest.php | 116 | ||||
-rw-r--r-- | tests/unit/service/PreviewServiceTest.php | 198 | ||||
-rw-r--r-- | tests/unit/service/SearchFolderServiceTest.php | 18 | ||||
-rw-r--r-- | tests/unit/service/SearchMediaServiceTest.php | 50 | ||||
-rw-r--r-- | tests/unit/service/ServiceTest.php | 134 |
6 files changed, 366 insertions, 158 deletions
diff --git a/tests/unit/service/DownloadServiceTest.php b/tests/unit/service/DownloadServiceTest.php index 99dcf164..2050a588 100644 --- a/tests/unit/service/DownloadServiceTest.php +++ b/tests/unit/service/DownloadServiceTest.php @@ -10,7 +10,7 @@ * @copyright Olivier Paroz 2015 */ namespace OCA\Gallery\Service; -include_once 'FilesServiceTest.php'; +include_once 'ServiceTest.php'; use OCP\Files\File; @@ -19,7 +19,7 @@ use OCP\Files\File; * * @package OCA\Gallery\Controller */ -class DownloadServiceTest extends FilesServiceTest { +class DownloadServiceTest extends ServiceTest { use Base64Encode; @@ -41,7 +41,7 @@ class DownloadServiceTest extends FilesServiceTest { public function testDownloadRawFile() { /** @type File $file */ - $file = $this->mockFile(12345); + $file = $this->mockJpgFile(12345); $download = [ 'preview' => $file->getContent(), @@ -56,7 +56,7 @@ class DownloadServiceTest extends FilesServiceTest { public function testDownloadBase64EncodedFile() { /** @type File $file */ - $file = $this->mockFile(12345); + $file = $this->mockJpgFile(12345); $download = [ 'preview' => $this->encode($file->getContent()), diff --git a/tests/unit/service/FilesServiceTest.php b/tests/unit/service/FilesServiceTest.php deleted file mode 100644 index 18bbe6c7..00000000 --- a/tests/unit/service/FilesServiceTest.php +++ /dev/null @@ -1,116 +0,0 @@ -<?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\Service; -include_once 'ServiceTest.php'; - -/** - * Class FilesServiceTest - * - * @package OCA\Gallery\Controller - */ -abstract class FilesServiceTest extends ServiceTest { - - /** - * Mocks OCP\Files\File - * - * Duplicate of PreviewControllerTest->mockFile - * - * Contains a JPG - * - * @param int $fileId - * @param string $storageId - * - * @return object|\PHPUnit_Framework_MockObject_MockObject - */ - protected function mockFile($fileId, $storageId = 'home::user', $isReadable = true, $path = '' - ) { - $filename = 'testimage.jpg'; - $storage = $this->mockGetStorage($storageId); - $file = $this->getMockBuilder('OCP\Files\File') - ->disableOriginalConstructor() - ->getMock(); - $file->method('getId') - ->willReturn($fileId); - $file->method('getType') - ->willReturn('file'); - $file->method('getContent') - ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $filename)); - $file->method('getName') - ->willReturn('testimage.jpg'); - $file->method('getMimeType') - ->willReturn('image/jpeg'); - $file->method('getStorage') - ->willReturn($storage); - $file->method('isReadable') - ->willReturn($isReadable); - $file->method('getPath') - ->willReturn($path); - - return $file; - } - - protected function mockBadFile() { - $file = $this->getMockBuilder('OCP\Files\File') - ->disableOriginalConstructor() - ->getMock(); - $file->method('getContent') - ->willThrowException(new ServiceException("Can't read file")); - - return $file; - } - - protected function mockGetFolder( - $storageId, - $nodeId, - $files, - $isReadable = true, - $mounted = false, - $mount = null, - $query = '', - $queryResult = false - ) { - $storage = $this->mockGetStorage($storageId); - $folder = $this->getMockBuilder('OCP\Files\Folder') - ->disableOriginalConstructor() - ->getMock(); - $folder->method('getType') - ->willReturn('dir'); - $folder->method('getId') - ->willReturn($nodeId); - $folder->method('getDirectoryListing') - ->willReturn($files); - $folder->method('getStorage') - ->willReturn($storage); - $folder->method('isReadable') - ->willReturn($isReadable); - $folder->method('isMounted') - ->willReturn($mounted); - $folder->method('getMountPoint') - ->willReturn($mount); - $folder->method('nodeExists') - ->with($query) - ->willReturn($queryResult); - - return $folder; - } - - protected function mockGetStorage($storageId) { - $storage = $this->getMockBuilder('OCP\Files\Storage') - ->disableOriginalConstructor() - ->getMock(); - $storage->method('getId') - ->willReturn($storageId); - - return $storage; - } - -} diff --git a/tests/unit/service/PreviewServiceTest.php b/tests/unit/service/PreviewServiceTest.php new file mode 100644 index 00000000..4df9ebbc --- /dev/null +++ b/tests/unit/service/PreviewServiceTest.php @@ -0,0 +1,198 @@ +<?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\Service; +include_once 'ServiceTest.php'; + +use OCP\Files\File; + +use OCA\Gallery\Preview\Preview; + +/** + * Class PreviewServiceTest + * + * @package OCA\Gallery\Controller + */ +class PreviewServiceTest extends ServiceTest { + + use Base64Encode; + + /** @var PreviewService */ + protected $service; + /** @var Preview */ + protected $previewManager; + + /** + * Test set up + */ + public function setUp() { + parent::setUp(); + + $this->previewManager = $this->getMockBuilder('\OCA\Gallery\Preview\Preview') + ->disableOriginalConstructor() + ->getMock(); + + $this->service = new PreviewService ( + $this->appName, + $this->environment, + $this->previewManager, + $this->logger + ); + } + + public function providesGetSupportedMediaTypesData() { + $baseMimeTypes = [ + 'image/jpeg', + ]; + + $slideshowMimes = array_merge( + $baseMimeTypes, + [ + 'application/font-sfnt', + 'application/x-font', + ] + ); + + $baseMimeTypesWithSvg = array_merge( + $baseMimeTypes, + [ + 'image/svg+xml', + ] + ); + + $slideshowMimesWithSvg = array_merge( + $slideshowMimes, + [ + 'image/svg+xml', + ] + ); + + return [ + [$baseMimeTypes, false, false, $baseMimeTypes], + [$baseMimeTypes, false, true, $baseMimeTypesWithSvg], + [$baseMimeTypes, true, true, $slideshowMimesWithSvg], + [$baseMimeTypes, true, false, $slideshowMimes], + ]; + } + + /** + * @dataProvider providesGetSupportedMediaTypesData + * + */ + public function testGetSupportedMediaTypes( + $baseMimeTypes, $extraMediaTypes, $nativeSvgSupport, $expectedResult + ) { + + $this->assertSame( + $baseMimeTypes, self::invokePrivate($this->service, 'baseMimeTypes', [$baseMimeTypes]) + ); + + $this->mockIsMimeSupported($nativeSvgSupport); + + $response = $this->service->getSupportedMediaTypes($extraMediaTypes, $nativeSvgSupport); + + $this->assertSame($expectedResult, array_keys($response)); + } + + public function providesIsPreviewRequiredData() { + return [ + [true], + [false], + ]; + } + + /** + * @dataProvider providesIsPreviewRequiredData + * + * @param $isMimeSupported + */ + public function testIsPreviewRequiredWithSvg($isMimeSupported) { + /** @type File $file */ + $file = $this->mockSvgFile(12345); + $animatedPreview = true; // Has no effect + + $this->mockIsMimeSupported($isMimeSupported); + + $response = $this->service->isPreviewRequired($file, $animatedPreview); + + $this->assertSame($isMimeSupported, $response); + + } + + public function testIsMimeSupportedWithBrokenSystem() { + $mimeType = 'secret/mime'; + $this->mockIsMimeSupportedWithBrokenSystem($mimeType); + + $response = self::invokePrivate($this->service, 'isMimeSupported', [$mimeType]); + + $this->assertFalse($response); + } + + public function providesAddSvgSupportData() { + $supportedMimes = [ + 'image/png', + 'image/jpeg', + 'image/gif' + ]; + + $supportedMimesWithSvg = array_merge( + $supportedMimes, + [ + // The method returns the path, but only checks for the key + 'image/svg+xml' => '/core/img/filetypes/image-vector.png', + ] + ); + + return [ + [$supportedMimes, true, $supportedMimesWithSvg], + [$supportedMimes, false, $supportedMimes], + [$supportedMimesWithSvg, true, $supportedMimesWithSvg], + [$supportedMimesWithSvg, false, $supportedMimesWithSvg], + ]; + } + + /** + * @dataProvider providesAddSvgSupportData + * + * @param array $supportedMimes + * @param bool $nativeSvgSupport + * @param array $expectedResult + */ + public function testAddSvgSupport($supportedMimes, $nativeSvgSupport, $expectedResult) { + $response = self::invokePrivate( + $this->service, 'addSvgSupport', [$supportedMimes, $nativeSvgSupport] + ); + + $this->assertSame($expectedResult, $response); + } + + + private function mockIsMimeSupported($svgSupport) { + $map = [ + ['image/jpeg', true], + ['application/font-sfnt', true], + ['application/x-font', true], + ['image/svg+xml', $svgSupport] + ]; + $this->previewManager->method('isMimeSupported') + ->will( + $this->returnValueMap($map) + ); + } + + private function mockIsMimeSupportedWithBrokenSystem($mimeType) { + $this->previewManager->expects($this->once()) + ->method('isMimeSupported') + ->with($mimeType) + ->willThrowException(new \Exception('Boom')); + } + +} diff --git a/tests/unit/service/SearchFolderServiceTest.php b/tests/unit/service/SearchFolderServiceTest.php index 26ba6c5b..7496aef8 100644 --- a/tests/unit/service/SearchFolderServiceTest.php +++ b/tests/unit/service/SearchFolderServiceTest.php @@ -17,7 +17,7 @@ include_once 'ServiceTest.php'; * * @package OCA\Gallery\Controller */ -class SearchFolderServiceTest extends FilesServiceTest { +class SearchFolderServiceTest extends ServiceTest { /** @var SearchFolderService */ protected $service; @@ -53,7 +53,7 @@ class SearchFolderServiceTest extends FilesServiceTest { $path = ''; $nodeId = 94875; $isReadable = false; - $node = $this->mockGetFolder('home::user', $nodeId, [], $isReadable); + $node = $this->mockFolder('home::user', $nodeId, [], $isReadable); $locationHasChanged = false; self::invokePrivate($this->service, 'sendFolder', [$path, $node, $locationHasChanged]); @@ -63,7 +63,7 @@ class SearchFolderServiceTest extends FilesServiceTest { $path = ''; $nodeId = 94875; $files = []; - $node = $this->mockGetFolder('home::user', $nodeId, $files); + $node = $this->mockFolder('home::user', $nodeId, $files); $locationHasChanged = false; $folder = [$path, $node, $locationHasChanged]; @@ -77,10 +77,10 @@ class SearchFolderServiceTest extends FilesServiceTest { $path = ''; $nodeId = 94875; $files = []; - $shared = $this->mockGetFolder('shared::12345', $nodeId, $files); + $shared = $this->mockFolder('shared::12345', $nodeId, $files); $rootNodeId = 91919191; $rootFiles = [$shared]; - $sharedRoot = $this->mockGetFolder('shared::99999', $rootNodeId, $rootFiles); + $sharedRoot = $this->mockFolder('shared::99999', $rootNodeId, $rootFiles); $this->mockGetVirtualRootFolderOfSharedFolder($sharedRoot); $locationHasChanged = false; @@ -129,7 +129,7 @@ class SearchFolderServiceTest extends FilesServiceTest { } public function testIsAllowedAndAvailableWithBrokenSetup() { - $node = $this->mockGetFolder('home::user', 909090, []); + $node = $this->mockFolder('home::user', 909090, []); $node->method('isReadable') ->willThrowException(new \Exception('Boom')); @@ -163,7 +163,7 @@ class SearchFolderServiceTest extends FilesServiceTest { $files = []; $isReadable = true; $mount = $this->mockMountPoint($previewsAllowedOnMountedShare); - $node = $this->mockGetFolder( + $node = $this->mockFolder( 'webdav::user@domain.com/dav', $nodeId, $files, $isReadable, $mounted, $mount ); @@ -214,8 +214,8 @@ class SearchFolderServiceTest extends FilesServiceTest { public function testFindFolderWithFileLocation() { $location = 'folder/file1.jpg'; $fileId = 99999; - $file = $this->mockFile($fileId); - $folder = $this->mockGetFolder('home::user', 10101, [$file]); + $file = $this->mockJpgFile($fileId); + $folder = $this->mockFolder('home::user', 10101, [$file]); $file->method('getParent') ->willReturn($folder); diff --git a/tests/unit/service/SearchMediaServiceTest.php b/tests/unit/service/SearchMediaServiceTest.php index 2beff115..e25432d6 100644 --- a/tests/unit/service/SearchMediaServiceTest.php +++ b/tests/unit/service/SearchMediaServiceTest.php @@ -10,14 +10,14 @@ * @copyright Olivier Paroz 2015 */ namespace OCA\Gallery\Service; -include_once 'FilesServiceTest.php'; +include_once 'ServiceTest.php'; /** * Class SearchMediaServiceTest * * @package OCA\Gallery\Controller */ -class SearchMediaServiceTest extends FilesServiceTest { +class SearchMediaServiceTest extends ServiceTest { /** @var SearchMediaService */ protected $service; @@ -46,7 +46,7 @@ class SearchMediaServiceTest extends FilesServiceTest { public function testGetMediaFilesWithUnavailableFolder() { $isReadable = false; $files = []; - $topFolder = $this->mockGetFolder( + $topFolder = $this->mockFolder( 'home::user', 545454, $files, $isReadable ); $supportedMediaTypes = []; @@ -63,24 +63,24 @@ class SearchMediaServiceTest extends FilesServiceTest { $query = '.nomedia'; $queryResult = false; - $folder1 = $this->mockGetFolder( + $folder1 = $this->mockFolder( 'home::user', 545454, [ - $this->mockFile(11111), - $this->mockFile(22222), - $this->mockFile(33333) + $this->mockJpgFile(11111), + $this->mockJpgFile(22222), + $this->mockJpgFile(33333) ], $isReadable, $mounted, $mount, $query, $queryResult ); - $folder2 = $this->mockGetFolder( + $folder2 = $this->mockFolder( 'home::user', 767676, [ - $this->mockFile(44444), - $this->mockFile(55555), - $this->mockFile(66666) + $this->mockJpgFile(44444), + $this->mockJpgFile(55555), + $this->mockJpgFile(66666) ], $isReadable, $mounted, $mount, $query, $queryResult ); - $folder3 = $this->mockGetFolder( + $folder3 = $this->mockFolder( 'home::user', 10101, [$folder1], $isReadable, $mounted, $mount, $query, $queryResult ); - $folder4 = $this->mockGetFolder( + $folder4 = $this->mockFolder( 'home::user', 10101, [$folder1, $folder2], $isReadable, $mounted, $mount, $query, $queryResult ); @@ -89,34 +89,34 @@ class SearchMediaServiceTest extends FilesServiceTest { $config1 = [ $folder1, $folder2, - $this->mockFile(77777), - $this->mockFile(88888), - $this->mockFile(99999) + $this->mockJpgFile(77777), + $this->mockJpgFile(88888), + $this->mockJpgFile(99999) ]; // 2 deepfolder and 3 files. Should return the all the files $config2 = [ $folder3, $folder3, - $this->mockFile(77777), - $this->mockFile(88888), - $this->mockFile(99999) + $this->mockJpgFile(77777), + $this->mockJpgFile(88888), + $this->mockJpgFile(99999) ]; // 1 deepfolder (with 2 sub-folders) and 3 files. Should return the files and the content of 1 folder $config3 = [ $folder4, - $this->mockFile(77777), - $this->mockFile(88888), - $this->mockFile(99999) + $this->mockJpgFile(77777), + $this->mockJpgFile(88888), + $this->mockJpgFile(99999) ]; - $topFolder1 = $this->mockGetFolder( + $topFolder1 = $this->mockFolder( 'home::user', 909090, $config1, $isReadable, $mounted, $mount, $query, $queryResult ); - $topFolder2 = $this->mockGetFolder( + $topFolder2 = $this->mockFolder( 'home::user', 909090, $config2, $isReadable, $mounted, $mount, $query, $queryResult ); - $topFolder3 = $this->mockGetFolder( + $topFolder3 = $this->mockFolder( 'home::user', 909090, $config3, $isReadable, $mounted, $mount, $query, $queryResult ); diff --git a/tests/unit/service/ServiceTest.php b/tests/unit/service/ServiceTest.php index 742fbb1f..5a9c10ea 100644 --- a/tests/unit/service/ServiceTest.php +++ b/tests/unit/service/ServiceTest.php @@ -16,7 +16,7 @@ use OCP\ILogger; use OCA\Gallery\Environment\Environment; /** - * Class FilesServiceTest + * Class ServiceTest * * @package OCA\Gallery\Controller */ @@ -53,9 +53,135 @@ abstract class ServiceTest extends \Test\TestCase { */ protected function mockGetResourceFromId($fileId, $answer) { $this->environment->expects($this->once()) - ->method('getResourceFromId') - ->with($this->equalTo($fileId)) - ->willReturn($answer); + ->method('getResourceFromId') + ->with($this->equalTo($fileId)) + ->willReturn($answer); + } + + /** + * Mocks OCP\Files\File + * + * Duplicate of PreviewControllerTest->mockFile + * + * Contains a JPG + * + * @param int $fileId + * @param string $storageId + * + * @return object|\PHPUnit_Framework_MockObject_MockObject + */ + protected function mockFile($fileId, $storageId = 'home::user', $isReadable = true, $path = '' + ) { + $storage = $this->mockGetStorage($storageId); + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $file->method('getId') + ->willReturn($fileId); + $file->method('getType') + ->willReturn('file'); + $file->method('getStorage') + ->willReturn($storage); + $file->method('isReadable') + ->willReturn($isReadable); + $file->method('getPath') + ->willReturn($path); + + return $file; + } + + public function mockJpgFile($fileId) { + $file = $this->mockFile($fileId); + $this->mockJpgFileMethods($file); + + return $file; + } + + public function mockSvgFile($fileId) { + $file = $this->mockFile($fileId); + $this->mockSvgFileMethods($file); + + return $file; + } + + + private function mockJpgFileMethods($file) { + $filename = 'testimage.jpg'; + $file->method('getContent') + ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $filename)); + $file->method('getName') + ->willReturn($filename); + $file->method('getMimeType') + ->willReturn('image/jpeg'); + + return $file; + } + + private function mockSvgFileMethods($file) { + $filename = 'testimagelarge.svg'; + $file->method('getContent') + ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $filename)); + $file->method('getName') + ->willReturn($filename); + $file->method('getMimeType') + ->willReturn('image/svg+xml'); + + return $file; + } + + protected function mockBadFile() { + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $file->method('getContent') + ->willThrowException(new ServiceException("Can't read file")); + + return $file; + } + + protected function mockFolder( + $storageId, + $nodeId, + $files, + $isReadable = true, + $mounted = false, + $mount = null, + $query = '', + $queryResult = false + ) { + $storage = $this->mockGetStorage($storageId); + $folder = $this->getMockBuilder('OCP\Files\Folder') + ->disableOriginalConstructor() + ->getMock(); + $folder->method('getType') + ->willReturn('dir'); + $folder->method('getId') + ->willReturn($nodeId); + $folder->method('getDirectoryListing') + ->willReturn($files); + $folder->method('getStorage') + ->willReturn($storage); + $folder->method('isReadable') + ->willReturn($isReadable); + $folder->method('isMounted') + ->willReturn($mounted); + $folder->method('getMountPoint') + ->willReturn($mount); + $folder->method('nodeExists') + ->with($query) + ->willReturn($queryResult); + + return $folder; + } + + protected function mockGetStorage($storageId) { + $storage = $this->getMockBuilder('OCP\Files\Storage') + ->disableOriginalConstructor() + ->getMock(); + $storage->method('getId') + ->willReturn($storageId); + + return $storage; } } |