From c720f964bb0487813e1b116a10358b73a5814fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 19 Jun 2018 18:41:14 +0200 Subject: [PATCH] Add AttachmentMapperTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/AttachmentMapper.php | 12 +- tests/unit/Db/AttachmentMapperTest.php | 148 +++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 tests/unit/Db/AttachmentMapperTest.php diff --git a/lib/Db/AttachmentMapper.php b/lib/Db/AttachmentMapper.php index cfbc75a0a..371122f2c 100644 --- a/lib/Db/AttachmentMapper.php +++ b/lib/Db/AttachmentMapper.php @@ -24,7 +24,9 @@ namespace OCA\Deck\Db; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\IUserManager; @@ -133,7 +135,13 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper { * @return boolean */ public function isOwner($userId, $id) { - // TODO: Implement isOwner() method. + try { + $attachment = $this->find($id); + return $this->cardMapper->isOwner($userId, $attachment->getCardId()); + } catch (DoesNotExistException $e) { + } catch (MultipleObjectsReturnedException $e) { + } + return false; } /** @@ -148,6 +156,6 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper { } catch (\Exception $e) { return null; } - $this->cardMapper->findBoardId($attachment->getCardId()); + return $this->cardMapper->findBoardId($attachment->getCardId()); } } \ No newline at end of file diff --git a/tests/unit/Db/AttachmentMapperTest.php b/tests/unit/Db/AttachmentMapperTest.php new file mode 100644 index 000000000..159731c7c --- /dev/null +++ b/tests/unit/Db/AttachmentMapperTest.php @@ -0,0 +1,148 @@ + + * + * @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; + +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\IUserManager; +use Test\AppFramework\Db\MapperTestUtility; + +/** + * @group DB + */ +class AttachmentMapperTest extends MapperTestUtility { + + /** @var IDBConnection */ + private $dbConnection; + /** @var AttachmentMapper */ + private $attachmentMapper; + /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ + private $userManager; + /** @var CardMapper|\PHPUnit\Framework\MockObject\MockObject */ + private $cardMapper; + + // Data + private $attachments; + private $attachmentsById = []; + + public function setup(){ + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->cardMapper = $this->createMock(CardMapper::class); + + $this->dbConnection = \OC::$server->getDatabaseConnection(); + $this->attachmentMapper = new AttachmentMapper( + $this->dbConnection, + $this->cardMapper, + $this->userManager + ); + $this->attachments = [ + $this->createAttachmentEntity(1, 'deck_file', 'file1.pdf'), + $this->createAttachmentEntity(1, 'deck_file', 'file2.pdf'), + $this->createAttachmentEntity(2, 'deck_file', 'file3.pdf'), + $this->createAttachmentEntity(3, 'deck_file', 'file4.pdf') + ]; + foreach ($this->attachments as $attachment) { + $entry = $this->attachmentMapper->insert($attachment); + $entry->resetUpdatedFields(); + $this->attachmentsById[$entry->getId()] = $entry; + } + } + + private function createAttachmentEntity($cardId, $type, $data) { + $attachment = new Attachment(); + $attachment->setCardId($cardId); + $attachment->setType($type); + $attachment->setData($data); + $attachment->setCreatedBy('admin'); + return $attachment; + } + + public function testFind() { + foreach ($this->attachmentsById as $id => $attachment) { + $this->assertEquals($attachment, $this->attachmentMapper->find($id)); + } + } + + public function testFindAll() { + $attachmentsByCard = [ + $this->attachmentMapper->findAll(1), + $this->attachmentMapper->findAll(2), + $this->attachmentMapper->findAll(3) + ]; + $this->assertEquals($attachmentsByCard[0], $this->attachmentMapper->findAll(1)); + $this->assertEquals($attachmentsByCard[1], $this->attachmentMapper->findAll(2)); + $this->assertEquals($attachmentsByCard[2], $this->attachmentMapper->findAll(3)); + $this->assertEquals([], $this->attachmentMapper->findAll(5)); + } + + public function testFindToDelete() { + $attachmentsToDelete = $this->attachments; + $attachmentsToDelete[0]->setDeletedAt(1); + $attachmentsToDelete[2]->setDeletedAt(1); + $this->attachmentMapper->update($attachmentsToDelete[0]); + $this->attachmentMapper->update($attachmentsToDelete[2]); + foreach ($attachmentsToDelete as $attachment) { + $attachment->resetUpdatedFields(); + } + + $this->assertEquals([$attachmentsToDelete[0]], $this->attachmentMapper->findToDelete(1)); + $this->assertEquals([$attachmentsToDelete[2]], $this->attachmentMapper->findToDelete(2)); + } + + public function testIsOwner() { + $this->cardMapper->expects($this->once()) + ->method('isOwner') + ->with('admin', 1) + ->willReturn(true); + $this->assertTrue($this->attachmentMapper->isOwner('admin', (string)$this->attachments[0]->getId())); + } + + public function testIsOwnerInvalid() { + $this->cardMapper->expects($this->once()) + ->method('isOwner') + ->with('admin', 1) + ->will($this->throwException(new DoesNotExistException('does not exist'))); + $this->assertFalse($this->attachmentMapper->isOwner('admin', (string)$this->attachments[0]->getId())); + } + + public function testFindBoardId() { + $this->cardMapper->expects($this->any()) + ->method('findBoardId') + ->willReturn(123); + foreach ($this->attachmentsById as $attachment) { + $this->assertEquals(123, $this->attachmentMapper->findBoardId($attachment->getId())); + } + } + + public function tearDown() { + parent::tearDown(); + foreach ($this->attachments as $attachment) { + $this->attachmentMapper->delete($attachment); + } + } + +} \ No newline at end of file