Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ translations.js

# JS coverage
coverage/

# vim
.*.sw?
11 changes: 9 additions & 2 deletions lib/Service/AutoCompletion/AutoCompleteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Matthias Rella <mrella@pisys.eu>
*
* Mail
*
Expand All @@ -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
Expand All @@ -50,7 +57,7 @@ public function findMatches($term) {
];
}, $fromCollector);

return array_merge($recipientsFromContacts, $recipientsFromCollector);
return array_merge($recipientsFromContacts, $recipientsFromCollector, $recipientGroups);
}

}
62 changes: 62 additions & 0 deletions lib/Service/GroupsIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\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;
}


}
15 changes: 15 additions & 0 deletions tests/Service/Autocompletion/AutoCompleteServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Matthias Rella <mrella@pisys.eu>
*
* Mail
*
Expand All @@ -28,6 +29,7 @@
class AutoCompleteServiceTest extends TestCase {

private $contactsIntegration;
private $groupsIntegration;
private $addressCollector;
private $service;

Expand All @@ -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);
}

Expand All @@ -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)
Expand All @@ -76,6 +90,7 @@ public function testFindMatches() {
['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);
}
Expand Down
84 changes: 84 additions & 0 deletions tests/Service/GroupsIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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;

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 = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

[
'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);
}

}