diff --git a/lib/Controller/AttachmentController.php b/lib/Controller/AttachmentController.php index cb6136ed3..103888781 100644 --- a/lib/Controller/AttachmentController.php +++ b/lib/Controller/AttachmentController.php @@ -39,7 +39,7 @@ class AttachmentController extends Controller { } public function getAll($cardId) { - return $this->attachmentService->getAll($cardId); + return $this->attachmentService->findAll($cardId); } /** diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index 34207d516..1836249e1 100644 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -55,6 +55,8 @@ class AttachmentService { * @param AttachmentMapper $attachmentMapper * @param CardMapper $cardMapper * @param PermissionService $permissionService + * @param Application $application + * @param ICacheFactory $cacheFactory * @param $userId * @throws \OCP\AppFramework\QueryException */ @@ -257,6 +259,6 @@ class AttachmentService { } } catch (InvalidAttachmentType $e) { } - throw new NoPermissionException(); + throw new NoPermissionException('Restore is not allowed.'); } } \ No newline at end of file diff --git a/tests/unit/Cron/DeleteCronTest.php b/tests/unit/Cron/DeleteCronTest.php index bee1662e4..e037cb1a4 100644 --- a/tests/unit/Cron/DeleteCronTest.php +++ b/tests/unit/Cron/DeleteCronTest.php @@ -32,11 +32,11 @@ use OCA\Deck\Service\IAttachmentService; class DeleteCronTest extends \Test\TestCase { - /** @var BoardMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var BoardMapper|\PHPUnit\Framework\MockObject\MockObject */ protected $boardMapper; - /** @var AttachmentService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var AttachmentService|\PHPUnit\Framework\MockObject\MockObject */ private $attachmentService; - /** @var AttachmentMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var AttachmentMapper|\PHPUnit\Framework\MockObject\MockObject */ private $attachmentMapper; /** @var DeleteCron */ protected $deleteCron; diff --git a/tests/unit/Cron/ScheduledNoificationsTest.php b/tests/unit/Cron/ScheduledNoificationsTest.php index 7e31a464d..85d9f1341 100644 --- a/tests/unit/Cron/ScheduledNoificationsTest.php +++ b/tests/unit/Cron/ScheduledNoificationsTest.php @@ -31,9 +31,9 @@ use OCA\Deck\Notification\NotificationHelper; class ScheduledNoificationsTest extends \Test\TestCase { - /** @var CardMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var CardMapper|\PHPUnit\Framework\MockObject\MockObject */ protected $cardMapper; - /** @var NotificationHelper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var NotificationHelper|\PHPUnit\Framework\MockObject\MockObject */ protected $notificationHelper; /** @var ScheduledNotifications */ protected $scheduledNotifications; diff --git a/tests/unit/Db/AttachmentTest.php b/tests/unit/Db/AttachmentTest.php new file mode 100644 index 000000000..cc71b92ec --- /dev/null +++ b/tests/unit/Db/AttachmentTest.php @@ -0,0 +1,51 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Db; + +class AttachmentTest extends \Test\TestCase { + private function createAttachment() { + $attachment = new Attachment(); + $attachment->setId(1); + $attachment->setCardId(123); + $attachment->setData("blob"); + $attachment->setCreatedBy('admin'); + $attachment->setType('deck_file'); + return $attachment; + } + public function testJsonSerialize() { + $board = $this->createAttachment(); + $this->assertEquals([ + 'id' => 1, + 'cardId' => 123, + 'lastModified' => 0, + 'data' => 'blob', + 'type' => 'deck_file', + 'createdAt' => 0, + 'createdBy' => 'admin', + 'deletedAt' => 0, + 'extendedData' => [] + + ], $board->jsonSerialize()); + } +} \ No newline at end of file diff --git a/tests/unit/Db/BoardMapperTest.php b/tests/unit/Db/BoardMapperTest.php index ab865b1fb..64c878893 100644 --- a/tests/unit/Db/BoardMapperTest.php +++ b/tests/unit/Db/BoardMapperTest.php @@ -35,13 +35,13 @@ class BoardMapperTest extends MapperTestUtility { /** @var IDBConnection */ private $dbConnection; - /** @var AclMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var AclMapper|\PHPUnit\Framework\MockObject\MockObject */ private $aclMapper; /** @var BoardMapper */ private $boardMapper; - /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ private $userManager; - /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ private $groupManager; // Data diff --git a/tests/unit/Db/CardTest.php b/tests/unit/Db/CardTest.php index 9f57565a9..be6472b88 100644 --- a/tests/unit/Db/CardTest.php +++ b/tests/unit/Db/CardTest.php @@ -25,8 +25,9 @@ namespace OCA\Deck\Db; use DateInterval; use DateTime; +use Test\TestCase; -class CardTest extends \PHPUnit_Framework_TestCase { +class CardTest extends TestCase { private function createCard() { $card = new Card(); $card->setId(1); diff --git a/tests/unit/Db/LabelTest.php b/tests/unit/Db/LabelTest.php index 0057a1e3c..15c67f216 100644 --- a/tests/unit/Db/LabelTest.php +++ b/tests/unit/Db/LabelTest.php @@ -23,7 +23,9 @@ namespace OCA\Deck\Db; -class LabelTest extends \PHPUnit_Framework_TestCase { +use Test\TestCase; + +class LabelTest extends TestCase { private function createLabel() { $label = new Label(); $label->setId(1); diff --git a/tests/unit/Db/StackTest.php b/tests/unit/Db/StackTest.php index aa2e02ab8..a28a732c6 100644 --- a/tests/unit/Db/StackTest.php +++ b/tests/unit/Db/StackTest.php @@ -23,7 +23,7 @@ namespace OCA\Deck\Db; -class StackTest extends \PHPUnit_Framework_TestCase { +class StackTest extends \Test\TestCase { private function createStack() { $board = new Stack(); $board->setId(1); diff --git a/tests/unit/ExceptionsTest.php b/tests/unit/ExceptionsTest.php index 732e31ca6..7af24c765 100644 --- a/tests/unit/ExceptionsTest.php +++ b/tests/unit/ExceptionsTest.php @@ -25,11 +25,12 @@ namespace OCA\Deck\Db; use OCA\Deck\ArchivedItemException; use OCA\Deck\Controller\PageController; +use OCA\Deck\InvalidAttachmentType; use OCA\Deck\NoPermissionException; use OCA\Deck\NotFoundException; use OCA\Deck\StatusException; -class ExceptionsTest extends \PHPUnit_Framework_TestCase { +class ExceptionsTest extends \Test\TestCase { public function testNoPermissionException() { $c = new \stdClass(); @@ -49,6 +50,11 @@ class ExceptionsTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('foo', $e->getMessage()); } + public function testInvalidAttachmentType() { + $e = new InvalidAttachmentType('foo'); + $this->assertEquals('No matching IAttachmentService implementation found for type foo', $e->getMessage()); + } + public function testStatusException() { $e = new StatusException('foo'); $this->assertEquals('foo', $e->getMessage()); diff --git a/tests/unit/Service/AttachmentServiceTest.php b/tests/unit/Service/AttachmentServiceTest.php new file mode 100644 index 000000000..9a0c86292 --- /dev/null +++ b/tests/unit/Service/AttachmentServiceTest.php @@ -0,0 +1,335 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Service; + + +use OCA\Deck\AppInfo\Application; +use OCA\Deck\Db\Acl; +use OCA\Deck\Db\Attachment; +use OCA\Deck\Db\AttachmentMapper; +use OCA\Deck\Db\CardMapper; +use OCA\Deck\InvalidAttachmentType; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\IAppContainer; +use OCP\ICache; +use OCP\ICacheFactory; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +/** @internal Just for testing the service registration */ +class MyAttachmentService { + public function extendData(Attachment $attachment) {} + public function display(Attachment $attachment) {} + public function create(Attachment $attachment) {} + public function update(Attachment $attachment) {} + public function delete(Attachment $attachment) {} + public function allowUndo() {} + public function markAsDeleted(Attachment $attachment) {} +} + +class AttachmentServiceTest extends TestCase { + + /** @var AttachmentMapper|MockObject */ + private $attachmentMapper; + /** @var CardMapper|MockObject */ + private $cardMapper; + /** @var PermissionService|MockObject */ + private $permissionService; + private $userId = 'admin'; + /** @var Application|MockObject */ + private $application; + private $cacheFactory; + /** @var AttachmentService */ + private $attachmentService; + /** @var MockObject */ + private $attachmentServiceImpl; + private $appContainer; + /** ICache */ + private $cache; + + /** + * @throws \OCP\AppFramework\QueryException + */ + public function setUp() { + parent::setUp(); + + $this->attachmentServiceImpl = $this->createMock(IAttachmentService::class); + $this->appContainer = $this->createMock(IAppContainer::class); + + $this->attachmentMapper = $this->createMock(AttachmentMapper::class); + $this->cardMapper = $this->createMock(CardMapper::class); + $this->permissionService = $this->createMock(PermissionService::class); + $this->application = $this->createMock(Application::class); + $this->cacheFactory = $this->createMock(ICacheFactory::class); + + $this->cache = $this->createMock(ICache::class); + $this->cacheFactory->expects($this->any())->method('createDistributed')->willReturn($this->cache); + + $this->appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($this->attachmentServiceImpl); + $this->application->expects($this->any()) + ->method('getContainer') + ->willReturn($this->appContainer); + + $this->attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $this->application, $this->cacheFactory, $this->userId); + } + + public function testRegisterAttachmentService() { + $application = $this->createMock(Application::class); + $appContainer = $this->createMock(IAppContainer::class); + $fileServiceMock = $this->createMock(FileService::class); + $appContainer->expects($this->at(1))->method('query')->with(MyAttachmentService::class)->willReturn(new MyAttachmentService()); + $appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($fileServiceMock); + $application->expects($this->any()) + ->method('getContainer') + ->willReturn($appContainer); + $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $application, $this->cacheFactory, $this->userId); + $attachmentService->registerAttachmentService('custom', MyAttachmentService::class); + $this->assertEquals($fileServiceMock, $attachmentService->getService('deck_file')); + $this->assertEquals(MyAttachmentService::class, get_class($attachmentService->getService('custom'))); + } + + /** + * @expectedException \OCA\Deck\InvalidAttachmentType + */ + public function testRegisterAttachmentServiceNotExisting() { + $application = $this->createMock(Application::class); + $appContainer = $this->createMock(IAppContainer::class); + $fileServiceMock = $this->createMock(FileService::class); + $appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($fileServiceMock); + $appContainer->expects($this->at(1))->method('query')->with(MyAttachmentService::class)->willReturn(new MyAttachmentService()); + $application->expects($this->any()) + ->method('getContainer') + ->willReturn($appContainer); + $attachmentService = new AttachmentService($this->attachmentMapper, $this->cardMapper, $this->permissionService, $application, $this->cacheFactory, $this->userId); + $attachmentService->registerAttachmentService('custom', MyAttachmentService::class); + $attachmentService->getService('deck_file_invalid'); + } + + private function mockPermission($permission) { + $this->permissionService->expects($this->once()) + ->method('checkPermission') + ->with($this->cardMapper, 123, $permission); + } + + private function createAttachment($type, $data) { + $attachment = new Attachment(); + $attachment->setType($type); + $attachment->setData($data); + return $attachment; + } + + public function testFindAll() { + $this->mockPermission(Acl::PERMISSION_READ); + $attachments = [ + $this->createAttachment('deck_file','file1'), + $this->createAttachment('deck_file','file2'), + $this->createAttachment('deck_file_invalid','file3'), + ]; + $this->attachmentMapper->expects($this->once()) + ->method('findAll') + ->with(123) + ->willReturn($attachments); + + $this->attachmentServiceImpl->expects($this->at(0)) + ->method('extendData') + ->with($attachments[0]); + $this->attachmentServiceImpl->expects($this->at(1)) + ->method('extendData') + ->with($attachments[1]); + $this->assertEquals($attachments, $this->attachmentService->findAll(123, false)); + } + + public function testCount() { + $this->cache->expects($this->once())->method('get')->with('card-123')->willReturn(null); + $this->attachmentMapper->expects($this->once())->method('findAll')->willReturn([1,2,3,4]); + $this->cache->expects($this->once())->method('set')->with('card-123', 4)->willReturn(null); + $this->assertEquals(4, $this->attachmentService->count(123)); + } + + public function testCountCacheHit() { + $this->cache->expects($this->once())->method('get')->with('card-123')->willReturn(4); + $this->assertEquals(4, $this->attachmentService->count(123)); + } + + public function testCreate() { + $attachment = $this->createAttachment('deck_file', 'file_name.jpg'); + $expected = $this->createAttachment('deck_file', 'file_name.jpg'); + $this->mockPermission(Acl::PERMISSION_EDIT); + $this->cache->expects($this->once())->method('clear')->with('card-123'); + $this->attachmentServiceImpl->expects($this->once()) + ->method('create'); + $this->attachmentMapper->expects($this->once()) + ->method('insert') + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('extendData') + ->willReturnCallback(function($a) { $a->setExtendedData(['mime' => 'image/jpeg']); }); + + $actual = $this->attachmentService->create(123, 'deck_file', 'file_name.jpg'); + + $expected->setExtendedData(['mime' => 'image/jpeg']); + $this->assertEquals($expected, $actual); + } + + public function testDisplay() { + $attachment = $this->createAttachment('deck_file', 'filename'); + $response = new Response(); + $this->mockPermission(Acl::PERMISSION_READ); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('display') + ->with($attachment) + ->willReturn($response); + $actual = $this->attachmentService->display(123, 1); + $this->assertEquals($response, $actual); + } + + /** + * @expectedException \OCA\Deck\NotFoundException + */ + public function testDisplayInvalid() { + $attachment = $this->createAttachment('deck_file', 'filename'); + $response = new Response(); + $this->mockPermission(Acl::PERMISSION_READ); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('display') + ->with($attachment) + ->will($this->throwException(new InvalidAttachmentType('deck_file'))); + $this->attachmentService->display(123, 1); + } + public function testUpdate() { + $attachment = $this->createAttachment('deck_file', 'file_name.jpg'); + $expected = $this->createAttachment('deck_file', 'file_name.jpg'); + $this->mockPermission(Acl::PERMISSION_EDIT); + $this->cache->expects($this->once())->method('clear')->with('card-123'); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('update'); + $this->attachmentMapper->expects($this->once()) + ->method('update') + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('extendData') + ->willReturnCallback(function($a) { $a->setExtendedData(['mime' => 'image/jpeg']); }); + + $actual = $this->attachmentService->update(123, 1, 'file_name.jpg'); + + $expected->setExtendedData(['mime' => 'image/jpeg']); + $expected->setLastModified($attachment->getLastModified()); + $this->assertEquals($expected, $actual); + } + + public function testDelete() { + $attachment = $this->createAttachment('deck_file', 'file_name.jpg'); + $expected = $this->createAttachment('deck_file', 'file_name.jpg'); + $this->mockPermission(Acl::PERMISSION_EDIT); + $this->cache->expects($this->once())->method('clear')->with('card-123'); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('allowUndo') + ->willReturn(false); + $this->attachmentServiceImpl->expects($this->once()) + ->method('delete'); + $this->attachmentMapper->expects($this->once()) + ->method('delete') + ->willReturn($attachment); + $actual = $this->attachmentService->delete(123, 1); + $this->assertEquals($expected, $actual); + } + + public function testDeleteWithUndo() { + $attachment = $this->createAttachment('deck_file', 'file_name.jpg'); + $expected = $this->createAttachment('deck_file', 'file_name.jpg'); + $this->mockPermission(Acl::PERMISSION_EDIT); + $this->cache->expects($this->once())->method('clear')->with('card-123'); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('allowUndo') + ->willReturn(true); + $this->attachmentServiceImpl->expects($this->once()) + ->method('markAsDeleted') + ->willReturnCallback(function($a) { $a->setDeletedAt(23); }); + $this->attachmentMapper->expects($this->once()) + ->method('update') + ->willReturn($attachment); + $expected->setDeletedAt(23); + $actual = $this->attachmentService->delete(123, 1); + $this->assertEquals($expected, $actual); + } + + public function testRestore() { + $attachment = $this->createAttachment('deck_file', 'file_name.jpg'); + $expected = $this->createAttachment('deck_file', 'file_name.jpg'); + $this->mockPermission(Acl::PERMISSION_EDIT); + $this->cache->expects($this->once())->method('clear')->with('card-123'); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('allowUndo') + ->willReturn(true); + $this->attachmentMapper->expects($this->once()) + ->method('update') + ->willReturn($attachment); + $expected->setDeletedAt(0); + $actual = $this->attachmentService->restore(123, 1); + $this->assertEquals($expected, $actual); + } + + /** + * @expectedException \OCA\Deck\NoPermissionException + */ + public function testRestoreNotAllowed() { + $attachment = $this->createAttachment('deck_file', 'file_name.jpg'); + $expected = $this->createAttachment('deck_file', 'file_name.jpg'); + $this->mockPermission(Acl::PERMISSION_EDIT); + $this->cache->expects($this->once())->method('clear')->with('card-123'); + $this->attachmentMapper->expects($this->once()) + ->method('find') + ->with(1) + ->willReturn($attachment); + $this->attachmentServiceImpl->expects($this->once()) + ->method('allowUndo') + ->willReturn(false); + $actual = $this->attachmentService->restore(123, 1); + } + +} \ No newline at end of file diff --git a/tests/unit/Service/CardServiceTest.php b/tests/unit/Service/CardServiceTest.php index 1c8f711c1..0822bab3d 100644 --- a/tests/unit/Service/CardServiceTest.php +++ b/tests/unit/Service/CardServiceTest.php @@ -35,17 +35,17 @@ use Test\TestCase; class CardServiceTest extends TestCase { - /** @var CardService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var CardService|\PHPUnit\Framework\MockObject\MockObject */ private $cardService; - /** @var CardMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var CardMapper|\PHPUnit\Framework\MockObject\MockObject */ private $cardMapper; - /** @var StackMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var StackMapper|\PHPUnit\Framework\MockObject\MockObject */ private $stackMapper; - /** @var PermissionService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var PermissionService|\PHPUnit\Framework\MockObject\MockObject */ private $permissionService; - /** @var AssignedUsersMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var AssignedUsersMapper|\PHPUnit\Framework\MockObject\MockObject */ private $assignedUsersMapper; - /** @var BoardService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var BoardService|\PHPUnit\Framework\MockObject\MockObject */ private $boardService; public function setUp() { diff --git a/tests/unit/Service/FileServiceTest.php b/tests/unit/Service/FileServiceTest.php new file mode 100644 index 000000000..ee0fe80ce --- /dev/null +++ b/tests/unit/Service/FileServiceTest.php @@ -0,0 +1,229 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Service; + + +use OCA\Deck\AppInfo\Application; +use OCA\Deck\Db\AssignedUsers; +use OCA\Deck\Db\AssignedUsersMapper; +use OCA\Deck\Db\Attachment; +use OCA\Deck\Db\AttachmentMapper; +use OCA\Deck\Db\Card; +use OCA\Deck\Db\CardMapper; +use OCA\Deck\Db\StackMapper; +use OCA\Deck\InvalidAttachmentType; +use OCA\Deck\NotFoundException; +use OCA\Deck\StatusException; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\IAppContainer; +use OCP\Files\IAppData; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; +use OCP\ICacheFactory; +use OCP\IL10N; +use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class FileServiceTest extends TestCase { + + /** @var IL10N|MockObject */ + private $l10n; + /** @var IAppData|MockObject */ + private $appData; + /** @var IRequest|MockObject */ + private $request; + /** @var FileService */ + private $fileService; + + public function setUp() { + parent::setUp(); + $this->request = $this->createMock(IRequest::class); + $this->appData = $this->createMock(IAppData::class); + $this->l10n = $this->createMock(IL10N::class); + $this->fileService = new FileService($this->l10n, $this->appData, $this->request); + } + + public function mockGetFolder($cardId) { + $folder = $this->createMock(ISimpleFolder::class); + $this->appData->expects($this->once()) + ->method('getFolder') + ->with('file-card-' . $cardId) + ->willReturn($folder); + return $folder; + } + + private function getAttachment() { + $attachment = new Attachment(); + $attachment->setId(1); + $attachment->setCardId(123); + return $attachment; + } + + private function mockGetUploadedFileEmpty() { + $this->request->expects($this->once()) + ->method('getUploadedFile') + ->willReturn(null); + } + private function mockGetUploadedFileError($error) { + $this->request->expects($this->once()) + ->method('getUploadedFile') + ->willReturn(['error' => $error]); + } + private function mockGetUploadedFile() { + $this->request->expects($this->once()) + ->method('getUploadedFile') + ->willReturn([ + 'name' => 'file.jpg', + 'tmp_name' => __FILE__, + ]); + } + + /** + * @expectedException \RuntimeException + */ + public function testCreateEmpty() { + $attachment = $this->getAttachment(); + $this->mockGetUploadedFileEmpty(); + $this->fileService->create($attachment); + } + + /** + * @expectedException \RuntimeException + */ + public function testCreateError() { + $attachment = $this->getAttachment(); + $this->mockGetUploadedFileError(UPLOAD_ERR_INI_SIZE); + $this->fileService->create($attachment); + } + + public function testCreate() { + $attachment = $this->getAttachment(); + $this->mockGetUploadedFile(); + $folder = $this->mockGetFolder(123); + $folder->expects($this->once()) + ->method('fileExists') + ->willReturn(false); + $file = $this->createMock(ISimpleFile::class); + $file->expects($this->once()) + ->method('putContent') + ->with(file_get_contents(__FILE__, 'r')); + $folder->expects($this->once()) + ->method('newFile') + ->willReturn($file); + + $this->fileService->create($attachment); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage File already exists. + */ + public function testCreateExists() { + $attachment = $this->getAttachment(); + $this->mockGetUploadedFile(); + $folder = $this->mockGetFolder(123); + $folder->expects($this->once()) + ->method('fileExists') + ->willReturn(true); + $this->fileService->create($attachment); + } + + public function testUpdate() { + $attachment = $this->getAttachment(); + $this->mockGetUploadedFile(); + $folder = $this->mockGetFolder(123); + $file = $this->createMock(ISimpleFile::class); + $file->expects($this->once()) + ->method('putContent') + ->with(file_get_contents(__FILE__, 'r')); + $folder->expects($this->once()) + ->method('getFile') + ->willReturn($file); + + $this->fileService->update($attachment); + } + + public function testDelete() { + $attachment = $this->getAttachment(); + $file = $this->createMock(ISimpleFile::class); + $folder = $this->mockGetFolder('123'); + $folder->expects($this->once()) + ->method('getFile') + ->willReturn($file); + $file->expects($this->once()) + ->method('delete'); + $this->fileService->delete($attachment); + } + + public function testDisplay() { + $attachment = $this->getAttachment(); + $file = $this->createMock(ISimpleFile::class); + $folder = $this->mockGetFolder('123'); + $folder->expects($this->once()) + ->method('getFile') + ->willReturn($file); + $file->expects($this->exactly(2)) + ->method('getMimeType') + ->willReturn('image/jpeg'); + $actual = $this->fileService->display($attachment); + $expected = new FileDisplayResponse($file); + $expected->addHeader('Content-Type', 'image/jpeg'); + $this->assertEquals($expected, $actual); + } + + public function testDisplayPdf() { + $attachment = $this->getAttachment(); + $file = $this->createMock(ISimpleFile::class); + $folder = $this->mockGetFolder('123'); + $folder->expects($this->once()) + ->method('getFile') + ->willReturn($file); + $file->expects($this->exactly(2)) + ->method('getMimeType') + ->willReturn('application/pdf'); + $actual = $this->fileService->display($attachment); + $expected = new FileDisplayResponse($file); + $expected->addHeader('Content-Type', 'application/pdf'); + $policy = new ContentSecurityPolicy(); + $policy->addAllowedObjectDomain('\'self\''); + $policy->addAllowedObjectDomain('blob:'); + $expected->setContentSecurityPolicy($policy); + $this->assertEquals($expected, $actual); + } + + public function testAllowUndo() { + $this->assertTrue($this->fileService->allowUndo()); + } + + public function testMarkAsDeleted() { + // TODO: use proper ITimeFactory in the service so we can mock the call to time + $attachment = $this->getAttachment(); + $this->assertEquals(0, $attachment->getDeletedAt()); + $this->fileService->markAsDeleted($attachment); + $this->assertGreaterThan(0, $attachment->getDeletedAt()); + } +} \ No newline at end of file diff --git a/tests/unit/Service/LabelServiceTest.php b/tests/unit/Service/LabelServiceTest.php index 8f266218c..e3ec245db 100644 --- a/tests/unit/Service/LabelServiceTest.php +++ b/tests/unit/Service/LabelServiceTest.php @@ -30,13 +30,13 @@ use Test\TestCase; class LabelServiceTest extends TestCase { - /** @var LabelMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var LabelMapper|\PHPUnit\Framework\MockObject\MockObject */ private $labelMapper; - /** @var PermissionService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var PermissionService|\PHPUnit\Framework\MockObject\MockObject */ private $permissionService; /** @var LabelService */ private $labelService; - /** @var BoardService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var BoardService|\PHPUnit\Framework\MockObject\MockObject */ private $boardService; public function setUp() { diff --git a/tests/unit/Service/PermissionServiceTest.php b/tests/unit/Service/PermissionServiceTest.php index 0dcaf77b0..f4c2f41ec 100644 --- a/tests/unit/Service/PermissionServiceTest.php +++ b/tests/unit/Service/PermissionServiceTest.php @@ -37,7 +37,7 @@ use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; -class PermissionServiceTest extends \PHPUnit_Framework_TestCase { +class PermissionServiceTest extends \Test\TestCase { /** @var PermissionService*/ private $service; diff --git a/tests/unit/Service/StackServiceTest.php b/tests/unit/Service/StackServiceTest.php index 03d6203da..d9cc908fc 100644 --- a/tests/unit/Service/StackServiceTest.php +++ b/tests/unit/Service/StackServiceTest.php @@ -44,19 +44,19 @@ class StackServiceTest extends TestCase { /** @var StackService */ private $stackService; - /** @var \PHPUnit_Framework_MockObject_MockObject|StackMapper */ + /** @var \PHPUnit\Framework\MockObject\MockObject|StackMapper */ private $stackMapper; - /** @var \PHPUnit_Framework_MockObject_MockObject|CardMapper */ + /** @var \PHPUnit\Framework\MockObject\MockObject|CardMapper */ private $cardMapper; - /** @var \PHPUnit_Framework_MockObject_MockObject|LabelMapper */ + /** @var \PHPUnit\Framework\MockObject\MockObject|LabelMapper */ private $labelMapper; - /** @var \PHPUnit_Framework_MockObject_MockObject|PermissionService */ + /** @var \PHPUnit\Framework\MockObject\MockObject|PermissionService */ private $permissionService; - /** @var AssignedUsersMapper|\PHPUnit_Framework_MockObject_MockObject */ + /** @var AssignedUsersMapper|\PHPUnit\Framework\MockObject\MockObject */ private $assignedUsersMapper; - /** @var AttachmentService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var AttachmentService|\PHPUnit\Framework\MockObject\MockObject */ private $attachmentService; - /** @var BoardService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var BoardService|\PHPUnit\Framework\MockObject\MockObject */ private $boardService; public function setUp() { diff --git a/tests/unit/controller/AttachmentControllerTest.php b/tests/unit/controller/AttachmentControllerTest.php new file mode 100644 index 000000000..0d1505032 --- /dev/null +++ b/tests/unit/controller/AttachmentControllerTest.php @@ -0,0 +1,105 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Controller; + +use OCA\Deck\Db\Acl; +use OCA\Deck\Service\AttachmentService; +use OCA\Deck\Service\CardService; +use OCA\Deck\Service\LabelService; +use OCA\Deck\Service\StackService; +use OCP\AppFramework\Controller; +use OCP\IRequest; + +class AttachmentControllerTest extends \Test\TestCase { + + /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */ + private $controller; + /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ + private $request; + /** @var AttachmentService|\PHPUnit\Framework\MockObject\MockObject */ + private $attachmentService; + /** @var string */ + private $userId = 'user'; + + public function setUp() { + $this->request = $this->createMock(IRequest::class); + $this->attachmentService = $this->createMock(AttachmentService::class); + $this->controller = new AttachmentController( + 'deck', + $this->request, + $this->attachmentService, + $this->userId + ); + } + + public function testGetAll() { + $this->attachmentService->expects($this->once())->method('findAll')->with(1); + $this->controller->getAll(1); + } + + public function testDisplay() { + $this->attachmentService->expects($this->once())->method('display')->with(1, 2); + $this->controller->display(1, 2); + } + + public function testCreate() { + $this->request->expects($this->exactly(2)) + ->method('getParam') + ->will($this->onConsecutiveCalls('type', 'data')); + $this->attachmentService->expects($this->once()) + ->method('create') + ->with(1, 'type', 'data') + ->willReturn(1); + $this->assertEquals(1, $this->controller->create(1)); + } + + public function testUpdate() { + $this->request->expects($this->exactly(1)) + ->method('getParam') + ->will($this->onConsecutiveCalls('data')); + $this->attachmentService->expects($this->once()) + ->method('update') + ->with(1, 2, 'data') + ->willReturn(1); + $this->assertEquals(1, $this->controller->update(1, 2)); + } + + + public function testDelete() { + $this->attachmentService->expects($this->once()) + ->method('delete') + ->with(123, 234) + ->willReturn(1); + $this->assertEquals(1, $this->controller->delete(123, 234)); + } + + public function testRestore() { + $this->attachmentService->expects($this->once()) + ->method('restore') + ->with(123, 234) + ->willReturn(1); + $this->assertEquals(1, $this->controller->restore(123, 234)); + } + +} diff --git a/tests/unit/controller/BoardControllerTest.php b/tests/unit/controller/BoardControllerTest.php index 9973c7410..660677125 100644 --- a/tests/unit/controller/BoardControllerTest.php +++ b/tests/unit/controller/BoardControllerTest.php @@ -26,7 +26,7 @@ namespace OCA\Deck\Controller; use OCA\Deck\Db\Acl; use OCP\IUser; -class BoardControllerTest extends \PHPUnit_Framework_TestCase { +class BoardControllerTest extends \Test\TestCase { private $controller; private $request; diff --git a/tests/unit/controller/CardControllerTest.php b/tests/unit/controller/CardControllerTest.php index 233aecadc..6f19db775 100644 --- a/tests/unit/controller/CardControllerTest.php +++ b/tests/unit/controller/CardControllerTest.php @@ -27,13 +27,13 @@ use OCA\Deck\Service\CardService; use OCP\AppFramework\Controller; use OCP\IRequest; -class CardControllerTest extends \PHPUnit_Framework_TestCase { +class CardControllerTest extends \Test\TestCase { - /** @var CardController|\PHPUnit_Framework_MockObject_MockObject */ + /** @var CardController|\PHPUnit\Framework\MockObject\MockObject */ private $controller; - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ private $request; - /** @var CardService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var CardService|\PHPUnit\Framework\MockObject\MockObject */ private $cardService; /** @var string */ private $userId = 'user'; diff --git a/tests/unit/controller/LabelControllerTest.php b/tests/unit/controller/LabelControllerTest.php index ac97e9b3d..8c1bb308e 100644 --- a/tests/unit/controller/LabelControllerTest.php +++ b/tests/unit/controller/LabelControllerTest.php @@ -29,13 +29,13 @@ use OCA\Deck\Service\LabelService; use OCP\AppFramework\Controller; use OCP\IRequest; -class LabelControllerTest extends \PHPUnit_Framework_TestCase { +class LabelControllerTest extends \Test\TestCase { - /** @var Controller|\PHPUnit_Framework_MockObject_MockObject */ + /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */ private $controller; - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ private $request; - /** @var LabelService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var LabelService|\PHPUnit\Framework\MockObject\MockObject */ private $labelService; /** @var string */ private $userId = 'user'; diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php index 7ea6ac23c..ff0d444e3 100644 --- a/tests/unit/controller/PageControllerTest.php +++ b/tests/unit/controller/PageControllerTest.php @@ -25,7 +25,7 @@ namespace OCA\Deck\Controller; use PHPUnit_Framework_TestCase; -class PageControllerTest extends \PHPUnit_Framework_TestCase { +class PageControllerTest extends \Test\TestCase { private $controller; private $request; diff --git a/tests/unit/controller/StackControllerTest.php b/tests/unit/controller/StackControllerTest.php index e4e88eb66..01f0f963e 100644 --- a/tests/unit/controller/StackControllerTest.php +++ b/tests/unit/controller/StackControllerTest.php @@ -30,13 +30,13 @@ use OCA\Deck\Service\StackService; use OCP\AppFramework\Controller; use OCP\IRequest; -class StackControllerTest extends \PHPUnit_Framework_TestCase { +class StackControllerTest extends \Test\TestCase { - /** @var Controller|\PHPUnit_Framework_MockObject_MockObject */ + /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */ private $controller; - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ private $request; - /** @var StackService|\PHPUnit_Framework_MockObject_MockObject */ + /** @var StackService|\PHPUnit\Framework\MockObject\MockObject */ private $stackService; /** @var string */ private $userId = 'user';