diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-24 22:10:31 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-24 22:10:31 +0300 |
commit | be0c66adc70fbf372acfc08cced7e59a8861a351 (patch) | |
tree | ba1ab44a8d9c9f76894ca1a82650ecebbf74c1ae /tests/unit | |
parent | ff6897f56d1541e236247094c29ed7c8b7e4223a (diff) |
Some tests for the ConfigService class
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/service/ConfigServiceTest.php | 127 | ||||
-rw-r--r-- | tests/unit/service/SearchFolderServiceTest.php | 96 | ||||
-rw-r--r-- | tests/unit/service/ServiceTest.php | 27 |
3 files changed, 224 insertions, 26 deletions
diff --git a/tests/unit/service/ConfigServiceTest.php b/tests/unit/service/ConfigServiceTest.php new file mode 100644 index 00000000..d8aa7e7a --- /dev/null +++ b/tests/unit/service/ConfigServiceTest.php @@ -0,0 +1,127 @@ +<?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 ConfigServiceTest + * + * @package OCA\Gallery\Controller + */ +class ConfigServiceTest extends ServiceTest { + + /** @var ConfigService */ + protected $service; + /** @var ConfigParser */ + protected $configParser; + + /** + * Test set up + */ + public function setUp() { + parent::setUp(); + + $this->configParser = $this->getMockBuilder('\OCA\Gallery\Service\ConfigParser') + ->disableOriginalConstructor() + ->getMock(); + $this->service = new ConfigService ( + $this->appName, + $this->environment, + $this->configParser, + $this->logger + ); + } + + public function testBuildFolderConfigWithBrokenSetup() { + $nodeId = 65965; + $files = []; + $folder = $this->mockFolder('home::user', $nodeId, $files); + $configName = 'gallery.cnf'; + $config = []; + $configItems = ['information' => false, 'sorting' => false]; // Default in the class + $level = 0; + $configPath = 'Some/folder'; + $exception = new ServiceException('Boom'); + $result = + [[['error' => ['message' => 'Boom' . "</br></br>Config location: /$configPath"]]]]; + + $this->mockGetPathFromVirtualRoot($folder, $configPath); + $this->mockGetFolderConfigWithBrokenSetup( + $folder, $configName, $config, $configItems, $level, $exception + ); + + $response = self::invokePrivate( + $this->service, 'buildFolderConfig', [$folder, $configName, $config, $level] + ); + + $this->assertSame($result, $response); + } + + public function providesValidatesInfoConfigData() { + $description = 'My cute description'; + $copyright = 'Copyright 2004-2016 interfaSys sàrl'; + + $albumConfig = [ + 'information' => [ + 'description_link' => $description, + 'copyright_link' => $copyright, + ] + ]; + + $modifiedAlbumConfig = [ + 'information' => [ + 'description_link' => null, + 'copyright_link' => null, + ] + ]; + + return [ + [0, 0, $albumConfig, $albumConfig], + [1, 0, $albumConfig, $modifiedAlbumConfig], + [1, 2, $albumConfig, $albumConfig] + ]; + } + + /** + * @dataProvider providesValidatesInfoConfigData + * + * @param $level + * @param $virtualRootLevel + * @param $albumConfig + * @param $modifiedAlbumConfig + */ + public function testValidatesInfoConfig( + $level, $virtualRootLevel, $albumConfig, $modifiedAlbumConfig + ) { + + self::invokePrivate($this->service, 'virtualRootLevel', [$virtualRootLevel]); + $albumConfig['information']['level'] = $level; + $modifiedAlbumConfig['information']['level'] = $level; + + $response = self::invokePrivate($this->service, 'validatesInfoConfig', [$albumConfig]); + + $this->assertSame($modifiedAlbumConfig, $response); + } + + private function mockGetFolderConfigWithBrokenSetup( + $folder, $configName, $config, $configItems, $level, $exception + ) { + $this->configParser->expects($this->any()) + ->method('getFolderConfig') + ->with( + $folder, $configName, $config, $configItems, $level + ) + ->willThrowException($exception); + } + + +} diff --git a/tests/unit/service/SearchFolderServiceTest.php b/tests/unit/service/SearchFolderServiceTest.php index 7496aef8..0c388676 100644 --- a/tests/unit/service/SearchFolderServiceTest.php +++ b/tests/unit/service/SearchFolderServiceTest.php @@ -35,6 +35,24 @@ class SearchFolderServiceTest extends ServiceTest { ); } + + public function testGetNodeTypeWithBrokenFolder() { + $node = $this->mockBadFile(); + + $response = self::invokePrivate($this->service, 'getNodeType', [$node]); + + $this->assertSame('', $response); + } + + public function testGetAllowedSubFolderWithFile() { + $node = $this->mockFile(11335); + $nodeType = $node->getType(); + + $response = self::invokePrivate($this->service, 'getAllowedSubFolder', [$node, $nodeType]); + + $this->assertSame([], $response); + } + /** * @expectedException \OCA\Gallery\Service\NotFoundServiceException */ @@ -73,22 +91,37 @@ class SearchFolderServiceTest extends ServiceTest { $this->assertSame($folder, $response); } - public function testSendExternalFolder() { + public function providesSendExternalFolderData() { + return [ + ['shared::99999'], + ['home::user'] + ]; + } + + /** + * @dataProvider providesSendExternalFolderData + */ + public function testSendExternalFolder($storageId) { + $expectedException = + new \OCA\Gallery\Service\ForbiddenServiceException('Album is private or unavailable'); $path = ''; $nodeId = 94875; $files = []; $shared = $this->mockFolder('shared::12345', $nodeId, $files); $rootNodeId = 91919191; $rootFiles = [$shared]; - $sharedRoot = $this->mockFolder('shared::99999', $rootNodeId, $rootFiles); + $sharedRoot = $this->mockFolder($storageId, $rootNodeId, $rootFiles); $this->mockGetVirtualRootFolderOfSharedFolder($sharedRoot); $locationHasChanged = false; $folder = [$path, $shared, $locationHasChanged]; - - $response = self::invokePrivate($this->service, 'sendFolder', $folder); - - $this->assertSame($folder, $response); + try { + $response = self::invokePrivate($this->service, 'sendFolder', $folder); + $this->assertSame($folder, $response); + } catch (\Exception $exception) { + $this->assertInstanceOf('\OCA\Gallery\Service\ForbiddenServiceException', $exception); + $this->assertSame($expectedException->getMessage(), $exception->getMessage()); + } } public function providesNodesData() { @@ -121,6 +154,37 @@ class SearchFolderServiceTest extends ServiceTest { } } + public function providesRecoverFromGetNodesData() { + $caughtException = new \Exception('Nasty'); + $newException = new \OCA\Gallery\Service\NotFoundServiceException('Boom'); + + return [ + [0, $caughtException, $newException], + [1, $caughtException, []] + ]; + } + + /** + * @dataProvider providesRecoverFromGetNodesData + * + * @param $subDepth + * @param $exception + * @param $nodes + */ + public function testRecoverFromGetNodesError($subDepth, $caughtException, $nodes) { + try { + $response = self::invokePrivate( + $this->service, 'recoverFromGetNodesError', [$subDepth, $caughtException] + ); + $this->assertSame($nodes, $response); + } catch (\Exception $thisException) { + $this->assertInstanceOf( + '\OCA\Gallery\Service\NotFoundServiceException', $thisException + ); + $this->assertSame($caughtException->getMessage(), $thisException->getMessage()); + } + } + public function testIsAllowedAndAvailableWithNullFolder() { $node = null; $response = self::invokePrivate($this->service, 'isAllowedAndAvailable', [$node]); @@ -220,7 +284,7 @@ class SearchFolderServiceTest extends ServiceTest { ->willReturn($folder); $this->mockGetFileNodeFromVirtualRoot($location, $file); - $this->mockgetPathFromVirtualRoot($folder, $location); + $this->mockGetPathFromVirtualRoot($folder, $location); $locationHasChanged = false; $expectedResult = [$location, $folder, $locationHasChanged]; @@ -261,22 +325,4 @@ class SearchFolderServiceTest extends ServiceTest { return $mountPoint; } - private function mockGetFileNodeFromVirtualRoot($location, $file) { - $this->environment->expects($this->any()) - ->method('getNodeFromVirtualRoot') - ->with( - $location - ) - ->willReturn($file); - } - - private function mockgetPathFromVirtualRoot($node, $path) { - $this->environment->expects($this->any()) - ->method('getPathFromVirtualRoot') - ->with( - $node - ) - ->willReturn($path); - } - } diff --git a/tests/unit/service/ServiceTest.php b/tests/unit/service/ServiceTest.php index 5a9c10ea..ffa5f0ae 100644 --- a/tests/unit/service/ServiceTest.php +++ b/tests/unit/service/ServiceTest.php @@ -130,11 +130,18 @@ abstract class ServiceTest extends \Test\TestCase { } protected function mockBadFile() { + $exception = new ServiceException("Can't read file"); $file = $this->getMockBuilder('OCP\Files\File') ->disableOriginalConstructor() ->getMock(); + $file->method('getId') + ->willThrowException($exception); + $file->method('getType') + ->willThrowException($exception); + $file->method('getPath') + ->willThrowException($exception); $file->method('getContent') - ->willThrowException(new ServiceException("Can't read file")); + ->willThrowException($exception); return $file; } @@ -184,4 +191,22 @@ abstract class ServiceTest extends \Test\TestCase { return $storage; } + protected function mockGetFileNodeFromVirtualRoot($location, $file) { + $this->environment->expects($this->any()) + ->method('getNodeFromVirtualRoot') + ->with( + $location + ) + ->willReturn($file); + } + + protected function mockGetPathFromVirtualRoot($node, $path) { + $this->environment->expects($this->any()) + ->method('getPathFromVirtualRoot') + ->with( + $node + ) + ->willReturn($path); + } + } |