diff --git a/lib/Service/Group/NextcloudGroupService.php b/lib/Service/Group/NextcloudGroupService.php index 2c3dd1a72d..ca3dce3cd9 100644 --- a/lib/Service/Group/NextcloudGroupService.php +++ b/lib/Service/Group/NextcloudGroupService.php @@ -4,6 +4,7 @@ /** * @author Matthias Rella + * @author Thomas Citharel * * Mail * @@ -23,6 +24,7 @@ namespace OCA\Mail\Service\Group; +use OCP\IConfig; use OCP\IGroupManager; use OCA\Mail\Exception\ServiceException; @@ -35,6 +37,11 @@ class NextcloudGroupService implements IGroupService { */ private $groupManager; + /** + * @var IConfig + */ + private $config; + /** * Group's namespace * @@ -42,8 +49,9 @@ class NextcloudGroupService implements IGroupService { */ 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 { @@ -51,6 +59,9 @@ public function getNamespace(): string { } 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( diff --git a/tests/Unit/Service/Group/NextcloudGroupServiceTest.php b/tests/Unit/Service/Group/NextcloudGroupServiceTest.php index dac3fdb7d0..23bae1fe0e 100644 --- a/tests/Unit/Service/Group/NextcloudGroupServiceTest.php +++ b/tests/Unit/Service/Group/NextcloudGroupServiceTest.php @@ -2,6 +2,7 @@ /** * @author Matthias Rella + * @author Thomas Citharel * * Mail * @@ -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 = []) { @@ -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() {