From 19a90809f97c4c4fdb9c0ff82808b3a2239ba7a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 29 May 2017 00:08:22 +0200 Subject: [PATCH] Add more unit testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- tests/unit/Cron/DeleteCronTest.php | 30 ++-- tests/unit/Db/BoardMapperTest.php | 27 +++- tests/unit/Db/BoardTest.php | 4 +- tests/unit/Db/GroupTest.php | 58 ++++++++ tests/unit/Db/UserTest.php | 64 +++++++++ tests/unit/Migration/UnknownUserTest.php | 128 ++++++++++++++++++ tests/unit/Service/BoardServiceTest.php | 1 + tests/unit/Service/CardServiceTest.php | 1 + tests/unit/Service/LabelServiceTest.php | 1 + tests/unit/Service/PermissionServiceTest.php | 1 + tests/unit/Service/StackServiceTest.php | 4 + tests/unit/controller/BoardControllerTest.php | 8 ++ 12 files changed, 317 insertions(+), 10 deletions(-) create mode 100644 tests/unit/Db/GroupTest.php create mode 100644 tests/unit/Db/UserTest.php create mode 100644 tests/unit/Migration/UnknownUserTest.php diff --git a/tests/unit/Cron/DeleteCronTest.php b/tests/unit/Cron/DeleteCronTest.php index 72b733c91..ff3aba488 100644 --- a/tests/unit/Cron/DeleteCronTest.php +++ b/tests/unit/Cron/DeleteCronTest.php @@ -23,6 +23,7 @@ namespace OCA\Deck\Cron; +use OCA\Deck\Db\Board; use OCA\Deck\Db\BoardMapper; class DeleteCronTest extends \Test\TestCase { @@ -33,26 +34,39 @@ class DeleteCronTest extends \Test\TestCase { protected $deleteCron; public function setUp() { + parent::setUp(); $this->boardMapper = $this->createMock(BoardMapper::class); $this->deleteCron = new DeleteCron($this->boardMapper); } + protected function getBoard($id) { + $board = new Board(); + $board->setId($id); + return $board; + } + public function testDeleteCron() { + $boards = [ + $this->getBoard(1), + $this->getBoard(2), + $this->getBoard(3), + $this->getBoard(4), + ]; $this->boardMapper->expects($this->once()) ->method('findToDelete') - ->willReturn([1, 2, 3, 4]); - $this->boardMapper->expects($this->at(0)) - ->method('delete') - ->with(1); + ->willReturn($boards); $this->boardMapper->expects($this->at(1)) ->method('delete') - ->with(2); + ->with($boards[0]); $this->boardMapper->expects($this->at(2)) ->method('delete') - ->with(3); + ->with($boards[1]); $this->boardMapper->expects($this->at(3)) ->method('delete') - ->with(4); - $this->invokePrivate($this->deleteCron, 'run', null); + ->with($boards[2]); + $this->boardMapper->expects($this->at(4)) + ->method('delete') + ->with($boards[3]); + $this->invokePrivate($this->deleteCron, 'run', [null]); } } \ No newline at end of file diff --git a/tests/unit/Db/BoardMapperTest.php b/tests/unit/Db/BoardMapperTest.php index 310265e97..d7b06a609 100644 --- a/tests/unit/Db/BoardMapperTest.php +++ b/tests/unit/Db/BoardMapperTest.php @@ -23,6 +23,7 @@ namespace OCA\Deck\Db; +use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IUserManager; use Test\AppFramework\Db\MapperTestUtility; @@ -32,10 +33,15 @@ use Test\AppFramework\Db\MapperTestUtility; */ class BoardMapperTest extends MapperTestUtility { + /** @var IDBConnection */ private $dbConnection; + /** @var AclMapper|\PHPUnit_Framework_MockObject_MockObject */ private $aclMapper; + /** @var BoardMapper */ private $boardMapper; + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ private $userManager; + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ private $groupManager; // Data @@ -96,7 +102,6 @@ class BoardMapperTest extends MapperTestUtility { $board = new Board(); $board->setTitle($title); $board->setOwner($owner); - $board->setShared(1); return $board; } @@ -122,11 +127,31 @@ class BoardMapperTest extends MapperTestUtility { } } + public function testFindAll() { + $actual = $this->boardMapper->findAll(); + $this->assertAttributeEquals($this->boards[0]->getId(), 'id', $actual[0]); + $this->assertAttributeEquals($this->boards[1]->getId(), 'id', $actual[1]); + $this->assertAttributeEquals($this->boards[2]->getId(), 'id', $actual[2]); + } + + public function testFindAllToDelete() { + $this->boards[0]->setDeletedAt(1); + $this->boards[0] = $this->boardMapper->update($this->boards[0]); + + $actual = $this->boardMapper->findToDelete(); + $this->boards[0]->resetUpdatedFields(); + $this->assertEquals([$this->boards[0]], $actual); + + $this->boards[0]->setDeletedAt(0); + $this->boardMapper->update($this->boards[0]); + } + public function testFindWithLabels() { $actual = $this->boardMapper->find($this->boards[0]->getId(), true, false); $expected = $this->boards[0]; $this->assertEquals($expected, $actual); } + public function testFindWithAcl() { $actual = $this->boardMapper->find($this->boards[0]->getId(), false, true); $expected = [ diff --git a/tests/unit/Db/BoardTest.php b/tests/unit/Db/BoardTest.php index 3874003d5..99b703976 100644 --- a/tests/unit/Db/BoardTest.php +++ b/tests/unit/Db/BoardTest.php @@ -2,7 +2,9 @@ namespace OCA\Deck\Db; -class BoardTest extends \PHPUnit_Framework_TestCase { +use PHPUnit\Framework\TestCase; + +class BoardTest extends TestCase { private function createBoard() { $board = new Board(); $board->setId(1); diff --git a/tests/unit/Db/GroupTest.php b/tests/unit/Db/GroupTest.php new file mode 100644 index 000000000..cb98d6f0f --- /dev/null +++ b/tests/unit/Db/GroupTest.php @@ -0,0 +1,58 @@ + + * + * @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\IGroup; + +class GroupTest extends \Test\TestCase { + + public function testGroupObjectSerialize() { + /** @var IGroup $group */ + $group = $this->createMock(IGroup::class); + $group->expects($this->any()) + ->method('getGID') + ->willReturn('mygroup'); + $groupRelationalObject = new Group($group); + $expected = [ + 'uid' => 'mygroup', + 'displayname' => 'mygroup' + ]; + $this->assertEquals($expected, $groupRelationalObject->getObjectSerialization()); + } + + public function testGroupJSONSerialize() { + /** @var IGroup $group */ + $group = $this->createMock(IGroup::class); + $group->expects($this->any()) + ->method('getGID') + ->willReturn('mygroup'); + $groupRelationalObject = new Group($group); + $expected = [ + 'uid' => 'mygroup', + 'displayname' => 'mygroup', + 'primaryKey' => 'mygroup' + ]; + $this->assertEquals($expected, $groupRelationalObject->jsonSerialize()); + } +} \ No newline at end of file diff --git a/tests/unit/Db/UserTest.php b/tests/unit/Db/UserTest.php new file mode 100644 index 000000000..43b15dda1 --- /dev/null +++ b/tests/unit/Db/UserTest.php @@ -0,0 +1,64 @@ + + * + * @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\IUser; + +class UserTest extends \Test\TestCase { + + public function testGroupObjectSerialize() { + /** @var IUser $user */ + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('myuser'); + $user->expects($this->any()) + ->method('getDisplayName') + ->willReturn('myuser displayname'); + $userRelationalObject = new User($user); + $expected = [ + 'uid' => 'myuser', + 'displayname' => 'myuser displayname' + ]; + $this->assertEquals($expected, $userRelationalObject->getObjectSerialization()); + } + + public function testGroupJSONSerialize() { + /** @var IUser $user */ + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('myuser'); + $user->expects($this->any()) + ->method('getDisplayName') + ->willReturn('myuser displayname'); + $userRelationalObject = new User($user); + $expected = [ + 'uid' => 'myuser', + 'displayname' => 'myuser displayname', + 'primaryKey' => 'myuser' + ]; + $this->assertEquals($expected, $userRelationalObject->jsonSerialize()); + } +} \ No newline at end of file diff --git a/tests/unit/Migration/UnknownUserTest.php b/tests/unit/Migration/UnknownUserTest.php new file mode 100644 index 000000000..5c61e6963 --- /dev/null +++ b/tests/unit/Migration/UnknownUserTest.php @@ -0,0 +1,128 @@ + + * + * @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\Migration; + +use OCA\Deck\Db\Acl; +use OCA\Deck\Db\AclMapper; +use OCA\Deck\Db\Board; +use OCA\Deck\Db\BoardMapper; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\Migration\IOutput; + +class UnknownUserTest extends \Test\TestCase { + + /** @var IUserManager */ + private $userManager; + /** @var IGroupManager */ + private $groupManager; + /** @var AclMapper */ + private $aclMapper; + /** @var BoardMapper */ + private $boardMapper; + /** @var UnknownUsers */ + private $unknownUsers; + + public function setUp() { + parent::setUp(); + $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->aclMapper = $this->createMock(AclMapper::class); + $this->boardMapper = $this->createMock(BoardMapper::class); + $this->unknownUsers = new UnknownUsers($this->userManager, $this->groupManager, $this->aclMapper, $this->boardMapper); + } + + + + public function testGetName() { + $this->assertEquals('Delete orphaned ACL rules', $this->unknownUsers->getName()); + } + + public function testRun() { + /** @var IOutput $output */ + $output = $this->createMock(IOutput::class); + $boards = [ + $this->getBoard(1,'Test', 'admin'), + ]; + $acl = [ + $this->getAcl('user', 'existing', 1), + $this->getAcl('user', 'not existing', 1), + $this->getAcl('group', 'existing', 1), + $this->getAcl('group', 'not existing', 1), + ]; + $this->aclMapper->expects($this->at(0)) + ->method('findAll') + ->with(1) + ->willReturn($acl); + + $this->userManager->expects($this->at(0)) + ->method('get') + ->with('existing') + ->willReturn(true); + $this->userManager->expects($this->at(1)) + ->method('get') + ->with('not existing') + ->willReturn(null); + $this->groupManager->expects($this->at(0)) + ->method('get') + ->with('existing') + ->willReturn(true); + $this->groupManager->expects($this->at(1)) + ->method('get') + ->with('not existing') + ->willReturn(null); + + $this->boardMapper->expects($this->once()) + ->method('findAll') + ->willReturn($boards); + + $this->aclMapper->expects($this->at(1)) + ->method('delete') + ->with($acl[1]); + $this->aclMapper->expects($this->at(2)) + ->method('delete') + ->with($acl[3]); + + $this->unknownUsers->run($output); + } + + + /** @return Acl */ + public function getAcl($type='user', $participant='admin', $boardId=123) { + $acl = new Acl(); + $acl->setParticipant($participant); + $acl->setType($type); + $acl->setBoardId($boardId); + return $acl; + } + + /** @return Board */ + public function getBoard($id, $title, $owner) { + $board = new Board(); + $board->setId($id); + $board->setTitle($title); + $board->setOwner($owner); + return $board; + } +} \ No newline at end of file diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index 86922344c..d8fb6080e 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -53,6 +53,7 @@ class BoardServiceTest extends \Test\TestCase { private $userId = 'admin'; public function setUp() { + parent::setUp(); $this->l10n = $this->createMock(L10N::class); $this->aclMapper = $this->createMock(AclMapper::class); $this->boardMapper = $this->createMock(BoardMapper::class); diff --git a/tests/unit/Service/CardServiceTest.php b/tests/unit/Service/CardServiceTest.php index f7f583e0a..c673ccbee 100644 --- a/tests/unit/Service/CardServiceTest.php +++ b/tests/unit/Service/CardServiceTest.php @@ -44,6 +44,7 @@ class CardServiceTest extends TestCase { private $boardService; public function setUp() { + parent::setUp(); $this->cardMapper = $this->getMockBuilder(CardMapper::class) ->disableOriginalConstructor()->getMock(); $this->stackMapper = $this->getMockBuilder(StackMapper::class) diff --git a/tests/unit/Service/LabelServiceTest.php b/tests/unit/Service/LabelServiceTest.php index 152bccdb2..8f266218c 100644 --- a/tests/unit/Service/LabelServiceTest.php +++ b/tests/unit/Service/LabelServiceTest.php @@ -40,6 +40,7 @@ class LabelServiceTest extends TestCase { private $boardService; public function setUp() { + parent::setUp(); $this->labelMapper = $this->getMockBuilder(LabelMapper::class) ->disableOriginalConstructor()->getMock(); $this->permissionService = $this->getMockBuilder(PermissionService::class) diff --git a/tests/unit/Service/PermissionServiceTest.php b/tests/unit/Service/PermissionServiceTest.php index 15ce8d6cd..43466c9c9 100644 --- a/tests/unit/Service/PermissionServiceTest.php +++ b/tests/unit/Service/PermissionServiceTest.php @@ -46,6 +46,7 @@ class PermissionServiceTest extends \PHPUnit_Framework_TestCase { private $userId = 'admin'; public function setUp() { + parent::setUp(); $this->logger = $this->request = $this->getMockBuilder(ILogger::class) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/unit/Service/StackServiceTest.php b/tests/unit/Service/StackServiceTest.php index 7ffff8236..fc15e3b7e 100644 --- a/tests/unit/Service/StackServiceTest.php +++ b/tests/unit/Service/StackServiceTest.php @@ -49,6 +49,7 @@ class StackServiceTest extends TestCase { private $boardService; public function setUp() { + parent::setUp(); $this->stackMapper = $this->getMockBuilder(StackMapper::class) ->disableOriginalConstructor()->getMock(); $this->cardMapper = $this->getMockBuilder(CardMapper::class) @@ -165,6 +166,9 @@ class StackServiceTest extends TestCase { $this->assertEquals($stack, $result); } + /** + * @group DB + */ public function testReorder() { $this->permissionService->expects($this->once())->method('checkPermission'); $a = $this->createStack(1, 0); diff --git a/tests/unit/controller/BoardControllerTest.php b/tests/unit/controller/BoardControllerTest.php index 439521206..d26e9288c 100644 --- a/tests/unit/controller/BoardControllerTest.php +++ b/tests/unit/controller/BoardControllerTest.php @@ -120,6 +120,14 @@ class BoardControllerTest extends \PHPUnit_Framework_TestCase { $this->assertEquals(1, $this->controller->delete(123)); } + public function testDeleteUndo() { + $this->boardService->expects($this->once()) + ->method('deleteUndo') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->deleteUndo(123)); + } + public function testGetUserPermissions() { $acl = [ Acl::PERMISSION_READ => true,