Skip to content
Merged
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
13 changes: 12 additions & 1 deletion lib/Service/Group/NextcloudGroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* @author Matthias Rella <mrella@pisys.eu>
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* Mail
*
Expand All @@ -23,6 +24,7 @@

namespace OCA\Mail\Service\Group;

use OCP\IConfig;
use OCP\IGroupManager;
use OCA\Mail\Exception\ServiceException;

Expand All @@ -35,22 +37,31 @@ class NextcloudGroupService implements IGroupService {
*/
private $groupManager;

/**
* @var IConfig
*/
private $config;

/**
* Group's namespace
*
* @var string
*/
private $namespace = "Nextcloud";

public function __construct(IGroupManager $groupManager) {
public function __construct(IGroupManager $groupManager, IConfig $config) {
$this->groupManager = $groupManager;
$this->config = $config;
}

public function getNamespace(): string {
return $this->namespace;
}

public function search(string $term): array {
if ($this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') !== 'yes') {
return [];
}
$groups = $this->groupManager->search($term);

return array_map(
Expand Down
61 changes: 46 additions & 15 deletions tests/Unit/Service/Group/NextcloudGroupServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* @author Matthias Rella <mrella@pisys.eu>
* @author Thomas Citharel <nextcloud@tcit.fr>
*
* Mail
*
Expand All @@ -24,19 +25,32 @@
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Service\Group\NextcloudGroupService;
use OCA\Mail\Exception\ServiceException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IGroup;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;

class NextcloudGroupServiceTest extends TestCase {
/**
* @var IGroupManager|MockObject
*/
private $groupsManager;
/**
* @var IGroupManager|MockObject
*/
private $config;
/**
* @var NextcloudGroupService
*/
private $groupService;

protected function setUp(): void {
parent::setUp();

$this->groupsManager = $this->createMock(IGroupManager::class);
$this->groupService = new NextcloudGroupService($this->groupsManager);
$this->config = $this->createMock(IConfig::class);
$this->groupService = new NextcloudGroupService($this->groupsManager, $this->config);
}

private function createTestGroup($id, $name, $users = []) {
Expand Down Expand Up @@ -67,32 +81,49 @@ private function createTestUser($id, $name, $email) {
return $mockUser;
}

public function dataForTestSearch(): array {
return [
['yes', [
[
'id' => 'testgroup',
'name' => 'first test group',
],
[
'id' => 'testgroup2',
'name' => 'second test group',
]
]],
['no', []]
];
}

public function testSearch() {

/**
* @dataProvider dataForTestSearch
* @param string $allowGroupSharing
* @param array $expected
*/
public function testSearch(string $allowGroupSharing, array $expected): void {
$term = 'te'; // searching for: John Doe
$searchResult = [
$this->createTestGroup('testgroup', 'first test group'),
$this->createTestGroup('testgroup2', 'second test group'),
];

$this->groupsManager->expects($this->once())
$this->groupsManager->expects($allowGroupSharing === 'yes' ? self::once() : self::never())
->method('search')
->with($term)
->will($this->returnValue($searchResult));
->willReturn($searchResult);

$this->config->expects(self::once())
->method('getAppValue')
->with('core', 'shareapi_allow_group_sharing', 'yes')
->willReturn($allowGroupSharing);


$expected = [
[
'id' => 'testgroup',
'name' => 'first test group',
],
[
'id' => 'testgroup2',
'name' => 'second test group',
]
];
$actual = $this->groupService->search($term);

$this->assertEquals($expected, $actual);
self::assertEquals($expected, $actual);
}

public function testGetUsers() {
Expand Down