From e3064fcdcfdb02332407b44535f315c66688f5f2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 21 Feb 2018 11:09:30 +0100 Subject: [PATCH 1/6] initial changes --- .../AutoCompletion/AutoCompleteService.php | 7 +- lib/Service/GroupsIntegration.php | 65 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 lib/Service/GroupsIntegration.php diff --git a/lib/Service/AutoCompletion/AutoCompleteService.php b/lib/Service/AutoCompletion/AutoCompleteService.php index 0f8cbe6b64..f4dd542a7f 100644 --- a/lib/Service/AutoCompletion/AutoCompleteService.php +++ b/lib/Service/AutoCompletion/AutoCompleteService.php @@ -23,6 +23,7 @@ use OCA\Mail\Db\CollectedAddress; use OCA\Mail\Service\ContactsIntegration; +use OCA\Mail\Service\GroupsIntegration; class AutoCompleteService { @@ -32,13 +33,15 @@ class AutoCompleteService { /** @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->getMatchingGroup($term); $fromCollector = $this->addressCollector->searchAddress($term); // Convert collected addresses into same format as CI creates @@ -50,7 +53,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..79c9f58a91 --- /dev/null +++ b/lib/Service/GroupsIntegration.php @@ -0,0 +1,65 @@ + + * @author Thomas Müller + * @author Matthias Rella + * + * 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\GroupInterface; + +class GroupsIntegration { + + /** + * @var GroupInterface + */ + private $groupInterface; + + /** + * @param GroupInterface $groupsInterface + */ + public function __construct(GroupInterface $groupInterface) { + $this->groupInterface = $groupInterface; + } + + /** + * Extracts all matching contacts with email address and name + * + * @param string $term + * @return array + */ + public function getMatchingGroups($term) { + $result = $this->groupInterface->getGroups($term); + $receivers = []; + foreach ($result as $id) { + $receivers[] = [ + 'id' => $id, + 'label' => $id, + 'value' => $id, + 'photo' => null, + ]; + } + } + + return $receivers; + } + + +} From d51ec493eed72085643f1971c595bccc571217e7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 21 Feb 2018 12:11:06 +0100 Subject: [PATCH 2/6] add vim to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 3aa1bffb30..1f67d4d147 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ translations.js # JS coverage coverage/ + +# vim +*.sw* From 8e254a9e599e4837c71366e3d892fe7e0544c8b9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 23 Feb 2018 08:02:08 +0100 Subject: [PATCH 3/6] use right interface, fix typos --- .../AutoCompletion/AutoCompleteService.php | 6 +++- lib/Service/GroupsIntegration.php | 29 +++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/Service/AutoCompletion/AutoCompleteService.php b/lib/Service/AutoCompletion/AutoCompleteService.php index f4dd542a7f..6785fa4e58 100644 --- a/lib/Service/AutoCompletion/AutoCompleteService.php +++ b/lib/Service/AutoCompletion/AutoCompleteService.php @@ -2,6 +2,7 @@ /** * @author Christoph Wurst + * @author Matthias Rella * * Mail * @@ -30,6 +31,9 @@ class AutoCompleteService { /** @var ContactsIntegration */ private $contactsIntegration; + /** @var GroupsIntegration */ + private $groupsIntegration; + /** @var AddressCollector */ private $addressCollector; @@ -41,7 +45,7 @@ public function __construct(ContactsIntegration $ci, GroupsIntegration $gi, Addr public function findMatches($term) { $recipientsFromContacts = $this->contactsIntegration->getMatchingRecipient($term); - $recipientGroups = $this->groupsIntegration->getMatchingGroup($term); + $recipientGroups = $this->groupsIntegration->getMatchingGroups($term); $fromCollector = $this->addressCollector->searchAddress($term); // Convert collected addresses into same format as CI creates diff --git a/lib/Service/GroupsIntegration.php b/lib/Service/GroupsIntegration.php index 79c9f58a91..c4ce2e8789 100644 --- a/lib/Service/GroupsIntegration.php +++ b/lib/Service/GroupsIntegration.php @@ -1,9 +1,7 @@ - * @author Thomas Müller - * @author Matthias Rella + * @author Matthias Rella * * Mail * @@ -23,20 +21,20 @@ namespace OCA\Mail\Service; -use OCP\GroupInterface; +use OCP\IGroupManager; class GroupsIntegration { /** - * @var GroupInterface + * @var IGroupManager */ - private $groupInterface; + private $groupManager; /** - * @param GroupInterface $groupsInterface + * @param IGroupManager $groupsManager */ - public function __construct(GroupInterface $groupInterface) { - $this->groupInterface = $groupInterface; + public function __construct(IGroupManager $groupManager) { + $this->groupManager = $groupManager; } /** @@ -46,17 +44,16 @@ public function __construct(GroupInterface $groupInterface) { * @return array */ public function getMatchingGroups($term) { - $result = $this->groupInterface->getGroups($term); + $result = $this->groupManager->search($term); $receivers = []; - foreach ($result as $id) { + foreach ($result as $g) { $receivers[] = [ - 'id' => $id, - 'label' => $id, - 'value' => $id, + 'id' => $g->getGID(), + 'label' => $g->getDisplayName(), + 'value' => $g->getDisplayName(), 'photo' => null, ]; - } - } + } return $receivers; } From cbbe4795fd730abfd90424b8ddd4560aa48b0ad7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 23 Feb 2018 09:02:42 +0100 Subject: [PATCH 4/6] add tests --- .../AutoCompleteServiceTest.php | 15 ++++ tests/Service/GroupsIntegrationTest.php | 84 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/Service/GroupsIntegrationTest.php diff --git a/tests/Service/Autocompletion/AutoCompleteServiceTest.php b/tests/Service/Autocompletion/AutoCompleteServiceTest.php index 256469b0b5..49eeb340e3 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..cbd41cbb1d --- /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); + } + +} From 09505dff9952d51b68210b876b3e1a3abe8b157e Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 23 Feb 2018 14:18:37 +0100 Subject: [PATCH 5/6] fix indentation --- lib/Service/AutoCompletion/AutoCompleteService.php | 2 +- lib/Service/GroupsIntegration.php | 2 +- tests/Service/Autocompletion/AutoCompleteServiceTest.php | 8 ++++---- tests/Service/GroupsIntegrationTest.php | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Service/AutoCompletion/AutoCompleteService.php b/lib/Service/AutoCompletion/AutoCompleteService.php index 6785fa4e58..e85915b3aa 100644 --- a/lib/Service/AutoCompletion/AutoCompleteService.php +++ b/lib/Service/AutoCompletion/AutoCompleteService.php @@ -45,7 +45,7 @@ public function __construct(ContactsIntegration $ci, GroupsIntegration $gi, Addr public function findMatches($term) { $recipientsFromContacts = $this->contactsIntegration->getMatchingRecipient($term); - $recipientGroups = $this->groupsIntegration->getMatchingGroups($term); + $recipientGroups = $this->groupsIntegration->getMatchingGroups($term); $fromCollector = $this->addressCollector->searchAddress($term); // Convert collected addresses into same format as CI creates diff --git a/lib/Service/GroupsIntegration.php b/lib/Service/GroupsIntegration.php index c4ce2e8789..137fb8ac21 100644 --- a/lib/Service/GroupsIntegration.php +++ b/lib/Service/GroupsIntegration.php @@ -53,7 +53,7 @@ public function getMatchingGroups($term) { 'value' => $g->getDisplayName(), 'photo' => null, ]; - } + } return $receivers; } diff --git a/tests/Service/Autocompletion/AutoCompleteServiceTest.php b/tests/Service/Autocompletion/AutoCompleteServiceTest.php index 49eeb340e3..7da604f812 100644 --- a/tests/Service/Autocompletion/AutoCompleteServiceTest.php +++ b/tests/Service/Autocompletion/AutoCompleteServiceTest.php @@ -47,7 +47,7 @@ protected function setUp() { ->getMock(); $this->service = new AutoCompleteService($this->contactsIntegration, - $this->groupsIntegration, + $this->groupsIntegration, $this->addressCollector); } @@ -67,9 +67,9 @@ public function testFindMatches() { $john, ]; - $groupsResult = [ - ['id' => 20, 'label' => 'Journalists', 'value' => 'Journalists'] - ]; + $groupsResult = [ + ['id' => 20, 'label' => 'Journalists', 'value' => 'Journalists'] + ]; $this->contactsIntegration->expects($this->once()) ->method('getMatchingRecipient') diff --git a/tests/Service/GroupsIntegrationTest.php b/tests/Service/GroupsIntegrationTest.php index cbd41cbb1d..1e592c2850 100644 --- a/tests/Service/GroupsIntegrationTest.php +++ b/tests/Service/GroupsIntegrationTest.php @@ -53,8 +53,8 @@ private function createTestGroup($id, $name) { public function testGetMatchingGroups() { $term = 'te'; // searching for: John Doe $searchResult = [ - $this->createTestGroup('testgroup', 'first test group'), - $this->createTestGroup('testgroup2', 'second test group'), + $this->createTestGroup('testgroup', 'first test group'), + $this->createTestGroup('testgroup2', 'second test group'), ]; $this->groupsManager->expects($this->once()) @@ -68,7 +68,7 @@ public function testGetMatchingGroups() { 'label' => 'first test group', 'value' => 'first test group', 'photo' => null, - ], + ], [ 'id' => 'testgroup2', 'label' => 'second test group', From e10a2a922794106ad4b0b8806febf4cc3d4df449 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 23 Feb 2018 14:22:02 +0100 Subject: [PATCH 6/6] fix gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1f67d4d147..16aa28585a 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,4 @@ translations.js coverage/ # vim -*.sw* +.*.sw?