diff --git a/.gitignore b/.gitignore index 3aa1bffb30..16aa28585a 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ translations.js # JS coverage coverage/ + +# vim +.*.sw? diff --git a/lib/Service/AutoCompletion/AutoCompleteService.php b/lib/Service/AutoCompletion/AutoCompleteService.php index 0f8cbe6b64..e85915b3aa 100644 --- a/lib/Service/AutoCompletion/AutoCompleteService.php +++ b/lib/Service/AutoCompletion/AutoCompleteService.php @@ -2,6 +2,7 @@ /** * @author Christoph Wurst + * @author Matthias Rella * * Mail * @@ -23,22 +24,28 @@ use OCA\Mail\Db\CollectedAddress; use OCA\Mail\Service\ContactsIntegration; +use OCA\Mail\Service\GroupsIntegration; class AutoCompleteService { /** @var ContactsIntegration */ private $contactsIntegration; + /** @var GroupsIntegration */ + private $groupsIntegration; + /** @var AddressCollector */ private $addressCollector; - public function __construct(ContactsIntegration $ci, AddressCollector $ac) { + public function __construct(ContactsIntegration $ci, GroupsIntegration $gi, AddressCollector $ac) { $this->contactsIntegration = $ci; + $this->groupsIntegration = $gi; $this->addressCollector = $ac; } public function findMatches($term) { $recipientsFromContacts = $this->contactsIntegration->getMatchingRecipient($term); + $recipientGroups = $this->groupsIntegration->getMatchingGroups($term); $fromCollector = $this->addressCollector->searchAddress($term); // Convert collected addresses into same format as CI creates @@ -50,7 +57,7 @@ public function findMatches($term) { ]; }, $fromCollector); - return array_merge($recipientsFromContacts, $recipientsFromCollector); + return array_merge($recipientsFromContacts, $recipientsFromCollector, $recipientGroups); } } diff --git a/lib/Service/GroupsIntegration.php b/lib/Service/GroupsIntegration.php new file mode 100644 index 0000000000..137fb8ac21 --- /dev/null +++ b/lib/Service/GroupsIntegration.php @@ -0,0 +1,62 @@ + + * + * 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 + * + */ + +namespace OCA\Mail\Service; + +use OCP\IGroupManager; + +class GroupsIntegration { + + /** + * @var IGroupManager + */ + private $groupManager; + + /** + * @param IGroupManager $groupsManager + */ + public function __construct(IGroupManager $groupManager) { + $this->groupManager = $groupManager; + } + + /** + * Extracts all matching contacts with email address and name + * + * @param string $term + * @return array + */ + public function getMatchingGroups($term) { + $result = $this->groupManager->search($term); + $receivers = []; + foreach ($result as $g) { + $receivers[] = [ + 'id' => $g->getGID(), + 'label' => $g->getDisplayName(), + 'value' => $g->getDisplayName(), + 'photo' => null, + ]; + } + + return $receivers; + } + + +} diff --git a/tests/Service/Autocompletion/AutoCompleteServiceTest.php b/tests/Service/Autocompletion/AutoCompleteServiceTest.php index 256469b0b5..7da604f812 100644 --- a/tests/Service/Autocompletion/AutoCompleteServiceTest.php +++ b/tests/Service/Autocompletion/AutoCompleteServiceTest.php @@ -2,6 +2,7 @@ /** * @author Christoph Wurst + * @author Matthias Rella * * Mail * @@ -28,6 +29,7 @@ class AutoCompleteServiceTest extends TestCase { private $contactsIntegration; + private $groupsIntegration; private $addressCollector; private $service; @@ -37,11 +39,15 @@ protected function setUp() { $this->contactsIntegration = $this->getMockBuilder('\OCA\Mail\Service\ContactsIntegration') ->disableOriginalConstructor() ->getMock(); + $this->groupsIntegration = $this->getMockBuilder('\OCA\Mail\Service\GroupsIntegration') + ->disableOriginalConstructor() + ->getMock(); $this->addressCollector = $this->getMockBuilder('\OCA\Mail\Service\AutoCompletion\AddressCollector') ->disableOriginalConstructor() ->getMock(); $this->service = new AutoCompleteService($this->contactsIntegration, + $this->groupsIntegration, $this->addressCollector); } @@ -61,10 +67,18 @@ public function testFindMatches() { $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 +90,7 @@ public function testFindMatches() { ['id' => 12, 'label' => '"john doe" ', 'value' => '"john doe" '], ['id' => 13, 'label' => '"joe doe" ', 'value' => '"joe doe" '], ['id' => 1234, 'label' => 'John Doe', 'value' => '"John Doe" '], + ['id' => 20, 'label' => 'Journalists', 'value' => 'Journalists'], ]; $this->assertEquals($expected, $response); } diff --git a/tests/Service/GroupsIntegrationTest.php b/tests/Service/GroupsIntegrationTest.php new file mode 100644 index 0000000000..1e592c2850 --- /dev/null +++ b/tests/Service/GroupsIntegrationTest.php @@ -0,0 +1,84 @@ + + * + * 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 + * + */ + +namespace OCA\Mail\Tests\Service; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Service\GroupsIntegration; + +class GroupsIntegrationTest extends TestCase { + + private $groupsManager; + private $groupsIntegration; + + protected function setUp() { + parent::setUp(); + + $this->groupsManager = $this->getMockBuilder('OCP\IGroupManager') + ->disableOriginalConstructor() + ->getMock(); + $this->groupsIntegration = new GroupsIntegration($this->groupsManager); + } + + private function createTestGroup($id, $name) { + $mockGroup = $this->createMock('OCP\IGroup'); + $mockGroup->expects($this->any()) + ->method('getGID') + ->will($this->returnValue($id)); + $mockGroup->expects($this->any()) + ->method('getDisplayName') + ->will($this->returnValue($name)); + return $mockGroup; + } + + + public function testGetMatchingGroups() { + $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', + 'label' => 'first test group', + 'value' => 'first test group', + 'photo' => null, + ], + [ + 'id' => 'testgroup2', + 'label' => 'second test group', + 'value' => 'second test group', + 'photo' => null, + ] + ]; + $actual = $this->groupsIntegration->getMatchingGroups($term); + + $this->assertEquals($expected, $actual); + } + +}