diff options
author | Matthias <git@myrho.net> | 2018-02-21 13:09:30 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2018-03-19 11:55:29 +0300 |
commit | 1aa8a4d2d537d9e5639c92cb6d221b86903eb10a (patch) | |
tree | 5b0db32de05b1c91f1c4c4c04b4c33e504dbf918 /tests | |
parent | a4bca34d5ad5d7ca384096179b6d1555dd2f7bee (diff) |
Add ability to send to groups
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Controller/AccountsControllerTest.php | 10 | ||||
-rw-r--r-- | tests/Service/Autocompletion/AutoCompleteServiceTest.php | 24 | ||||
-rw-r--r-- | tests/Service/Group/NextcloudGroupServiceTest.php | 146 | ||||
-rw-r--r-- | tests/Service/GroupsIntegrationTest.php | 178 |
4 files changed, 351 insertions, 7 deletions
diff --git a/tests/Controller/AccountsControllerTest.php b/tests/Controller/AccountsControllerTest.php index 752473969..ed0fb5dd9 100644 --- a/tests/Controller/AccountsControllerTest.php +++ b/tests/Controller/AccountsControllerTest.php @@ -37,6 +37,7 @@ use OCA\Mail\Service\AliasesService; use OCA\Mail\Service\AutoConfig\AutoConfig; use OCA\Mail\Service\Logger; use OCA\Mail\Service\SetupService; +use OCA\Mail\Service\GroupsIntegration; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; use OCP\IL10N; @@ -55,6 +56,9 @@ class AccountsControllerTest extends TestCase { /** @var AccountService|PHPUnit_Framework_MockObject_MockObject */ private $accountService; + /** @var GroupsIntegration|PHPUnit_Framework_MockObject_MockObject */ + private $groupsIntegration; + /** @var string */ private $userId; @@ -94,6 +98,10 @@ class AccountsControllerTest extends TestCase { $this->appName = 'mail'; $this->request = $this->createMock(IRequest::class); $this->accountService = $this->createMock(AccountService::class); + $this->groupsIntegration = $this->createMock(GroupsIntegration::class); + $this->groupsIntegration->expects($this->any()) + ->method('expand') + ->will($this->returnArgument(0)); $this->userId = 'manfred'; $this->autoConfig = $this->createMock(AutoConfig::class); $this->logger = $this->createMock(Logger::class); @@ -103,7 +111,7 @@ class AccountsControllerTest extends TestCase { $this->transmission = $this->createMock(IMailTransmission::class); $this->setupService = $this->createMock(SetupService::class); - $this->controller = new AccountsController($this->appName, $this->request, $this->accountService, $this->userId, + $this->controller = new AccountsController($this->appName, $this->request, $this->accountService, $this->groupsIntegration, $this->userId, $this->logger, $this->l10n, $this->crypto, $this->aliasesService, $this->transmission, $this->setupService); $this->account = $this->createMock(Account::class); $this->accountId = 123; diff --git a/tests/Service/Autocompletion/AutoCompleteServiceTest.php b/tests/Service/Autocompletion/AutoCompleteServiceTest.php index 256469b0b..c75b504b0 100644 --- a/tests/Service/Autocompletion/AutoCompleteServiceTest.php +++ b/tests/Service/Autocompletion/AutoCompleteServiceTest.php @@ -2,6 +2,7 @@ /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * @author Matthias Rella <mrella@pisys.eu> * * Mail * @@ -24,24 +25,26 @@ namespace OCA\Mail\Tests\Service\Autocompletion; use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Db\CollectedAddress; use OCA\Mail\Service\AutoCompletion\AutoCompleteService; +use OCA\Mail\Service\AutoCompletion\AddressCollector; +use OCA\Mail\Service\ContactsIntegration; +use OCA\Mail\Service\GroupsIntegration; class AutoCompleteServiceTest extends TestCase { private $contactsIntegration; + private $groupsIntegration; private $addressCollector; private $service; protected function setUp() { parent::setUp(); - $this->contactsIntegration = $this->getMockBuilder('\OCA\Mail\Service\ContactsIntegration') - ->disableOriginalConstructor() - ->getMock(); - $this->addressCollector = $this->getMockBuilder('\OCA\Mail\Service\AutoCompletion\AddressCollector') - ->disableOriginalConstructor() - ->getMock(); + $this->contactsIntegration = $this->createMock(ContactsIntegration::class); + $this->groupsIntegration = $this->createMock(GroupsIntegration::class); + $this->addressCollector = $this->createMock(AddressCollector::class); $this->service = new AutoCompleteService($this->contactsIntegration, + $this->groupsIntegration, $this->addressCollector); } @@ -61,10 +64,18 @@ class AutoCompleteServiceTest extends TestCase { $john, ]; + $groupsResult = [ + ['id' => 20, 'label' => 'Journalists', 'value' => 'Journalists'] + ]; + $this->contactsIntegration->expects($this->once()) ->method('getMatchingRecipient') ->with($term) ->will($this->returnValue($contactsResult)); + $this->groupsIntegration->expects($this->once()) + ->method('getMatchingGroups') + ->with($term) + ->will($this->returnValue($groupsResult)); $this->addressCollector->expects($this->once()) ->method('searchAddress') ->with($term) @@ -76,6 +87,7 @@ class AutoCompleteServiceTest extends TestCase { ['id' => 12, 'label' => '"john doe" <john@doe.cz>', 'value' => '"john doe" <john@doe.cz>'], ['id' => 13, 'label' => '"joe doe" <joe@doe.se>', 'value' => '"joe doe" <joe@doe.se>'], ['id' => 1234, 'label' => 'John Doe', 'value' => '"John Doe" <john@doe.com>'], + ['id' => 20, 'label' => 'Journalists', 'value' => 'Journalists'], ]; $this->assertEquals($expected, $response); } diff --git a/tests/Service/Group/NextcloudGroupServiceTest.php b/tests/Service/Group/NextcloudGroupServiceTest.php new file mode 100644 index 000000000..18461e342 --- /dev/null +++ b/tests/Service/Group/NextcloudGroupServiceTest.php @@ -0,0 +1,146 @@ +<?php + +/** + * @author Matthias Rella <mrella@pisys.eu> + * + * Mail + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Mail\Tests\Service\Group; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Service\Group\NextcloudGroupService; +use OCA\Mail\Exception\ServiceException; +use OCP\IGroupManager; +use OCP\IGroup; +use OCP\IUser; + +class NextcloudGroupServiceTest extends TestCase { + + private $groupsManager; + private $groupService; + + protected function setUp() { + parent::setUp(); + + $this->groupsManager = $this->createMock(IGroupManager::class); + $this->groupService = new NextcloudGroupService($this->groupsManager); + } + + private function createTestGroup($id, $name, $users = []) { + $mockGroup = $this->createMock(IGroup::class); + $mockGroup->expects($this->any()) + ->method('getGID') + ->will($this->returnValue($id)); + $mockGroup->expects($this->any()) + ->method('getDisplayName') + ->will($this->returnValue($name)); + $mockGroup->expects($this->any()) + ->method('getUsers') + ->willReturn($users); + return $mockGroup; + } + + private function createTestUser($id, $name, $email) { + $mockUser = $this->createMock(IUser::class); + $mockUser->expects($this->any()) + ->method('getUID') + ->will($this->returnValue($id)); + $mockUser->expects($this->any()) + ->method('getDisplayName') + ->will($this->returnValue($name)); + $mockUser->expects($this->any()) + ->method('getEMailAddress') + ->willReturn($email); + return $mockUser; + } + + + public function testSearch() { + $term = 'te'; // searching for: John Doe + $searchResult = [ + $this->createTestGroup('testgroup', 'first test group'), + $this->createTestGroup('testgroup2', 'second test group'), + ]; + + $this->groupsManager->expects($this->once()) + ->method('search') + ->with($term) + ->will($this->returnValue($searchResult)); + + $expected = [ + [ + 'id' => 'testgroup', + 'name' => 'first test group', + ], + [ + 'id' => 'testgroup2', + 'name' => 'second test group', + ] + ]; + $actual = $this->groupService->search($term); + + $this->assertEquals($expected, $actual); + } + + public function testGetUsers() { + $users = [ + $this->createTestUser('bob', 'Bobby', 'bob@smith.net'), + $this->createTestUser('alice', 'Alice', 'alice@smith.net') + ]; + $group = + $this->createTestGroup('testgroup', 'first test group', $users); + + $this->groupsManager->expects($this->once()) + ->method('groupExists') + ->willReturn(true); + + $this->groupsManager->expects($this->once()) + ->method('get') + ->with('testgroup') + ->willReturn($group); + + $actual = $this->groupService->getUsers('testgroup'); + + $expected = [ + [ + 'id' => 'bob', + 'name' => 'Bobby', + 'email' => 'bob@smith.net' + ], + [ + 'id' => 'alice', + 'name' => 'Alice', + 'email' => 'alice@smith.net' + ] + ]; + + $this->assertEquals($expected, $actual); + + } + + public function testGetUsersWrong() { + $this->expectException(ServiceException::class); + + $this->groupsManager->expects($this->once()) + ->method('groupExists') + ->willReturn(false); + + $this->groupService->getUsers('nogroup'); + } + +} + diff --git a/tests/Service/GroupsIntegrationTest.php b/tests/Service/GroupsIntegrationTest.php new file mode 100644 index 000000000..ca0aa282b --- /dev/null +++ b/tests/Service/GroupsIntegrationTest.php @@ -0,0 +1,178 @@ +<?php + +/** + * @author Matthias Rella <mrella@pisys.eu> + * + * Mail + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Mail\Tests\Service; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Service\GroupsIntegration; +use OCA\Mail\Service\Group\NextcloudGroupService; +use OCA\Mail\Exception\ServiceException; + +class GroupsIntegrationTest extends TestCase { + + private $groupService1; + private $groupsIntegration; + + protected function setUp() { + parent::setUp(); + + $this->groupService1 = $this->createMock(NextcloudGroupService::class); + $this->groupService1->expects($this->any()) + ->method('getNamespace') + ->willReturn('Namespace1'); + $this->groupsIntegration = new GroupsIntegration($this->groupService1); + } + + public function testGetMatchingGroups() { + $term = 'te'; // searching for: John Doe + $searchResult1 = [ + [ + 'id' => 'testgroup', + 'name' => "first test group" + ] + ]; + + $this->groupService1->expects($this->once()) + ->method('search') + ->with($term) + ->will($this->returnValue($searchResult1)); + + $expected = [ + [ + 'id' => 'namespace1:testgroup', + 'label' => 'first test group (Namespace1)', + 'value' => 'namespace1:testgroup', + 'photo' => null, + ] + ]; + $actual = $this->groupsIntegration->getMatchingGroups($term); + + $this->assertEquals($expected, $actual); + } + + public function testExpandNone() { + $recipients = "john@doe.com,alice@smith.net"; + $members = [ + [ + 'id' => 'bob', + 'name' => "Bobby", + 'email' => "bob@smith.net" + ], + [ + 'id' => 'mary', + 'name' => 'Mary', + 'email' => 'mary@smith.net' + ] + ]; + $this->groupService1->expects($this->never()) + ->method('getUsers') + ->willReturn($members); + + $expected = $recipients; + + $actual = $this->groupsIntegration->expand($recipients); + + $this->assertEquals($expected, $actual); + + } + + public function testExpand() { + $recipients = "john@doe.com,namespace1:testgroup,alice@smith.net"; + $members = [ + [ + 'id' => 'bob', + 'name' => "Bobby", + 'email' => "bob@smith.net" + ], + [ + 'id' => 'mary', + 'name' => 'Mary', + 'email' => 'mary@smith.net' + ] + ]; + $this->groupService1->expects($this->once()) + ->method('getUsers') + ->willReturn($members); + + $expected = "john@doe.com,bob@smith.net,mary@smith.net,alice@smith.net"; + + $actual = $this->groupsIntegration->expand($recipients); + + $this->assertEquals($expected, $actual); + + } + + public function testExpand2() { + $recipients = "john@doe.com,namespace1:testgroup,alice@smith.net"; + $members = [ + [ + 'id' => 'bob', + 'name' => "Bobby", + 'email' => "bob@smith.net" + ], + [ + 'id' => 'mary', + 'name' => 'Mary', + 'email' => 'mary@smith.net' + ] + ]; + $this->groupService1->expects($this->once()) + ->method('getUsers') + ->willReturn($members); + + $expected = "john@doe.com,bob@smith.net,mary@smith.net,alice@smith.net"; + + $actual = $this->groupsIntegration->expand($recipients); + + $this->assertEquals($expected, $actual); + + } + + public function testExpandEmpty() { + $this->expectException(ServiceException::class); + $recipients = "john@doe.com,namespace1:testgroup,alice@smith.net"; + $members = [ + ]; + $this->groupService1->expects($this->once()) + ->method('getUsers') + ->willReturn($members); + $this->groupsIntegration->expand($recipients); + } + + public function testExpandWrong() { + $recipients = "john@doe.com,nons:testgroup,alice@smith.net"; + $expected = "john@doe.com,nons:testgroup,alice@smith.net"; + + $actual = $this->groupsIntegration->expand($recipients); + + $this->assertEquals($expected, $actual); + + } + + public function testExpandWrong2() { + $this->expectException(ServiceException::class); + $recipients = "john@doe.com,namespace1:nogroup,alice@smith.net"; + + $this->groupsIntegration->expand($recipients); + + } + +} |