diff --git a/lib/Controller/BoardController.php b/lib/Controller/BoardController.php index d28f6fd65..005139cee 100644 --- a/lib/Controller/BoardController.php +++ b/lib/Controller/BoardController.php @@ -5,20 +5,20 @@ * @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; @@ -34,40 +34,43 @@ use OCP\IUserManager; use OCP\IGroupManager; class BoardController extends Controller { - private $userId; - private $boardService; - protected $userManager; - protected $groupManager; + private $userId; + private $boardService; + private $userManager; + private $groupManager; private $userInfo; - public function __construct($appName, - IRequest $request, - IUserManager $userManager, - IGroupManager $groupManager, - BoardService $cardService, - $userId) { - parent::__construct($appName, $request); - $this->userId = $userId; - $this->userManager = $userManager; - $this->groupManager = $groupManager; - $this->boardService = $cardService; - $this->userInfo = $this->getBoardPrerequisites(); - } - private function getBoardPrerequisites() { - $groups = $this->groupManager->getUserGroupIds($this->userManager->get($this->userId)); - return [ - 'user' => $this->userId, - 'groups' => $groups - ]; - } + public function __construct($appName, + IRequest $request, + IUserManager $userManager, + IGroupManager $groupManager, + BoardService $boardService, + $userId) { + parent::__construct($appName, $request); + $this->userId = $userId; + $this->userManager = $userManager; + $this->groupManager = $groupManager; + $this->boardService = $boardService; + $this->userInfo = $this->getBoardPrerequisites(); + } - /** - * @NoAdminRequired + private function getBoardPrerequisites() { + $groups = $this->groupManager->getUserGroupIds( + $this->userManager->get($this->userId) + ); + return [ + 'user' => $this->userId, + 'groups' => $groups + ]; + } + + /** + * @NoAdminRequired * @RequireNoPermission - */ - public function index() { - return $this->boardService->findAll($this->userInfo); - } + */ + public function index() { + return $this->boardService->findAll($this->userInfo); + } /** * @NoAdminRequired @@ -75,9 +78,9 @@ class BoardController extends Controller { * @param $boardId * @return \OCP\AppFramework\Db\Entity */ - public function read($boardId) { - return $this->boardService->find($boardId); - } + public function read($boardId) { + return $this->boardService->find($boardId); + } /** * @NoAdminRequired @@ -86,9 +89,9 @@ class BoardController extends Controller { * @param $color * @return \OCP\AppFramework\Db\Entity */ - public function create($title, $color) { - return $this->boardService->create($title, $this->userId, $color); - } + public function create($title, $color) { + return $this->boardService->create($title, $this->userId, $color); + } /** * @NoAdminRequired @@ -98,9 +101,9 @@ class BoardController extends Controller { * @param $color * @return \OCP\AppFramework\Db\Entity */ - public function update($id, $title, $color) { - return $this->boardService->update($id, $title, $color); - } + public function update($id, $title, $color) { + return $this->boardService->update($id, $title, $color); + } /** * @NoAdminRequired @@ -108,19 +111,9 @@ class BoardController extends Controller { * @param $boardId * @return \OCP\AppFramework\Db\Entity */ - public function delete($boardId) { - return $this->boardService->delete($boardId); - } - - /** - * @NoAdminRequired - * @RequireReadPermission - * @param $boardId - * @return - */ - public function labels($boardId) { - return $this->boardService->labels($boardId); - } + public function delete($boardId) { + return $this->boardService->delete($boardId); + } /** * @NoAdminRequired @@ -131,7 +124,7 @@ class BoardController extends Controller { */ public function getUserPermissions($boardId) { $board = $this->boardService->find($boardId); - if($this->userId === $board->getOwner()) { + if ($this->userId === $board->getOwner()) { return [ 'PERMISSION_READ' => true, 'PERMISSION_EDIT' => true, @@ -160,9 +153,9 @@ class BoardController extends Controller { * @param $manage * @return \OCP\AppFramework\Db\Entity */ - public function addAcl($boardId, $type, $participant, $write, $invite, $manage) { - return $this->boardService->addAcl($boardId, $type, $participant, $write, $invite, $manage); - } + public function addAcl($boardId, $type, $participant, $write, $invite, $manage) { + return $this->boardService->addAcl($boardId, $type, $participant, $write, $invite, $manage); + } /** * @NoAdminRequired @@ -173,9 +166,9 @@ class BoardController extends Controller { * @param $permissionManage * @return \OCP\AppFramework\Db\Entity */ - public function updateAcl($id, $permissionWrite, $permissionInvite, $permissionManage) { - return $this->boardService->updateAcl($id, $permissionWrite, $permissionInvite, $permissionManage); - } + public function updateAcl($id, $permissionWrite, $permissionInvite, $permissionManage) { + return $this->boardService->updateAcl($id, $permissionWrite, $permissionInvite, $permissionManage); + } /** * @NoAdminRequired @@ -183,8 +176,8 @@ class BoardController extends Controller { * @param $aclId * @return \OCP\AppFramework\Db\Entity */ - public function deleteAcl($aclId) { - return $this->boardService->deleteAcl($aclId); - } + public function deleteAcl($aclId) { + return $this->boardService->deleteAcl($aclId); + } -} \ No newline at end of file +} diff --git a/lib/Service/BoardService.php b/lib/Service/BoardService.php index e8496bc91..dc8237cf7 100644 --- a/lib/Service/BoardService.php +++ b/lib/Service/BoardService.php @@ -134,6 +134,12 @@ class BoardService { return $this->aclMapper->delete($acl); } + /** + * @param $boardId + * @param $user + * @param $permission + * @return bool + */ public function getPermission($boardId, $user, $permission) { $acls = $this->aclMapper->findAll($boardId); // check for users diff --git a/phpunit.integration.xml b/phpunit.integration.xml index c899f231b..cfc121cd5 100644 --- a/phpunit.integration.xml +++ b/phpunit.integration.xml @@ -1,7 +1,12 @@ - - - ./tests/integration - - + + + ./tests/integration + + + + + ./lib + + \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 2cbf73494..7e0ca06e9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,12 @@ - - - ./tests/unit - - - - - ./lib - - + + + ./tests/unit + + + + + ./lib + + diff --git a/tests/integration/AppTest.php b/tests/integration/AppTest.php index 1060edf88..f5a0d0fb7 100644 --- a/tests/integration/AppTest.php +++ b/tests/integration/AppTest.php @@ -33,11 +33,12 @@ use Test\TestCase; class AppTest extends TestCase { private $container; + private $app; public function setUp() { parent::setUp(); - $app = new App('deck'); - $this->container = $app->getContainer(); + $this->app = new \OCA\Deck\AppInfo\Application(); + $this->container = $this->app->getContainer(); } public function testAppInstalled() { @@ -45,4 +46,8 @@ class AppTest extends TestCase { $this->assertTrue($appManager->isInstalled('deck')); } + public function testNavigationEntry() { + $this->app->registerNavigationEntry(); + } + } \ No newline at end of file diff --git a/tests/unit/controller/BoardControllerTest.php b/tests/unit/controller/BoardControllerTest.php index 63656666a..dd863c3b3 100644 --- a/tests/unit/controller/BoardControllerTest.php +++ b/tests/unit/controller/BoardControllerTest.php @@ -5,32 +5,34 @@ * @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 PHPUnit_Framework_TestCase; +use OCA\Deck\Db\Acl; class BoardControllerTest extends \PHPUnit_Framework_TestCase { private $controller; private $request; - private $l10n; - private $userId = 'john'; + private $userManager; + private $groupManager; + private $boardService; + private $userId = 'user'; public function setUp() { $this->l10n = $this->request = $this->getMockBuilder( @@ -41,17 +43,144 @@ class BoardControllerTest extends \PHPUnit_Framework_TestCase { '\OCP\IRequest') ->disableOriginalConstructor() ->getMock(); + $this->userManager = $this->getMockBuilder( + '\OCP\IUserManager') + ->disableOriginalConstructor() + ->getMock(); + $this->groupManager = $this->getMockBuilder( + '\OCP\IGroupManager') + ->disableOriginalConstructor() + ->getMock(); + $this->boardService = $this->getMockBuilder( + '\OCA\Deck\Service\BoardService') + ->disableOriginalConstructor() + ->getMock(); - $this->controller = new PageController( - 'deck', $this->request, $this->l10n, $this->userId + $this->groupManager->method('getUserGroupIds') + ->willReturn(['admin', 'group1', 'group2']); + $this->userManager->method('get') + ->with($this->userId) + ->willReturn('user'); + + $this->controller = new BoardController( + 'deck', + $this->request, + $this->userManager, + $this->groupManager, + $this->boardService, + $this->userId ); } public function testIndex() { - $response = $this->controller->index(); - $this->assertEquals('main', $response->getTemplateName()); + $this->boardService->expects($this->once()) + ->method('findAll') + ->willReturn([1, 2, 3]); + + $actual = $this->controller->index(); + $this->assertEquals([1, 2, 3], $actual); } + public function testRead() { + $this->boardService->expects($this->once()) + ->method('find') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->read(123)); + } -} \ No newline at end of file + public function testCreate() { + $this->boardService->expects($this->once()) + ->method('create') + ->with(1, 'user', 3) + ->willReturn(1); + $this->assertEquals(1, $this->controller->create(1, 3)); + } + + public function testUpdate() { + $this->boardService->expects($this->once()) + ->method('update') + ->with(1, 2, 3) + ->willReturn(1); + $this->assertEquals(1, $this->controller->update(1, 2, 3)); + } + + public function testDelete() { + $this->boardService->expects($this->once()) + ->method('delete') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->delete(123)); + } + + public function testGetUserPermissions() { + $board = $this->getMockBuilder(\OCA\Deck\Db\Board::class) + ->disableOriginalConstructor() + ->setMethods(['getOwner']) + ->getMock(); + $this->boardService->expects($this->once()) + ->method('find') + ->with(123) + ->willReturn($board); + $board->expects($this->once()) + ->method('getOwner') + ->willReturn('user'); + $expected = [ + 'PERMISSION_READ' => true, + 'PERMISSION_EDIT' => true, + 'PERMISSION_MANAGE' => true, + 'PERMISSION_SHARE' => true, + ]; + $this->assertEquals($expected, $this->controller->getUserPermissions(123)); + } + + public function testGetUserPermissionsNotOwner() { + $board = $this->getMockBuilder(\OCA\Deck\Db\Board::class) + ->disableOriginalConstructor() + ->setMethods(['getOwner']) + ->getMock(); + $this->boardService->expects($this->once()) + ->method('find') + ->with(123) + ->willReturn($board); + $board->expects($this->once()) + ->method('getOwner') + ->willReturn('someoneelse'); + $this->boardService->expects($this->exactly(4)) + ->method('getPermission') + ->withConsecutive([123, 'user', Acl::PERMISSION_READ]) + ->will($this->onConsecutiveCalls(1, 2, 3, 4)); + $expected = [ + 'PERMISSION_READ' => 1, + 'PERMISSION_EDIT' => 2, + 'PERMISSION_MANAGE' => 3, + 'PERMISSION_SHARE' => 4, + ]; + $this->assertEquals($expected, $this->controller->getUserPermissions(123)); + } + + public function testAddAcl() { + $this->boardService->expects($this->once()) + ->method('addAcl') + ->with(1, 2, 3, 4, 5, 6) + ->willReturn(1); + $this->assertEquals(1, $this->controller->addAcl(1, 2, 3, 4, 5, 6)); + } + + public function testUpdateAcl() { + $this->boardService->expects($this->once()) + ->method('updateAcl') + ->with(1, 2, 3, 4) + ->willReturn(1); + $this->assertEquals(1, $this->controller->updateAcl(1, 2, 3, 4)); + } + + public function testDeleteAcl() { + $this->boardService->expects($this->once()) + ->method('deleteAcl') + ->with(1) + ->willReturn(1); + $this->assertEquals(1, $this->controller->deleteAcl(1)); + } +}