Merge pull request #24354 from nextcloud/backport/24162/stable19

[stable19] set the display name of federated sharees from addressbook
This commit is contained in:
Roeland Jago Douma 2020-11-25 16:18:12 +01:00 committed by GitHub
commit 6d6382a55d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 195 additions and 29 deletions

View File

@ -29,10 +29,13 @@ namespace OCA\FederatedFileSharing\Tests;
use OC\Federation\CloudIdManager; use OC\Federation\CloudIdManager;
use OCA\FederatedFileSharing\AddressHandler; use OCA\FederatedFileSharing\AddressHandler;
use OCP\Contacts\IManager;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
class AddressHandlerTest extends \Test\TestCase { class AddressHandlerTest extends \Test\TestCase {
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $contactsManager;
/** @var AddressHandler */ /** @var AddressHandler */
private $addressHandler; private $addressHandler;
@ -54,7 +57,9 @@ class AddressHandlerTest extends \Test\TestCase {
$this->il10n = $this->getMockBuilder(IL10N::class) $this->il10n = $this->getMockBuilder(IL10N::class)
->getMock(); ->getMock();
$this->cloudIdManager = new CloudIdManager(); $this->contactsManager = $this->createMock(IManager::class);
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
$this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager); $this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
} }
@ -98,6 +103,10 @@ class AddressHandlerTest extends \Test\TestCase {
* @param string $expectedUrl * @param string $expectedUrl
*/ */
public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) { public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
list($remoteUser, $remoteUrl) = $this->addressHandler->splitUserRemote($remote); list($remoteUser, $remoteUrl) = $this->addressHandler->splitUserRemote($remote);
$this->assertSame($expectedUser, $remoteUser); $this->assertSame($expectedUser, $remoteUser);
$this->assertSame($expectedUrl, $remoteUrl); $this->assertSame($expectedUrl, $remoteUrl);

View File

@ -34,6 +34,7 @@ use OCA\FederatedFileSharing\AddressHandler;
use OCA\FederatedFileSharing\Controller\MountPublicLinkController; use OCA\FederatedFileSharing\Controller\MountPublicLinkController;
use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FederatedShareProvider;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\Contacts\IManager as IContactsManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -46,6 +47,8 @@ use OCP\Share\IManager;
use OCP\Share\IShare; use OCP\Share\IShare;
class MountPublicLinkControllerTest extends \Test\TestCase { class MountPublicLinkControllerTest extends \Test\TestCase {
/** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
protected $contactsManager;
/** @var MountPublicLinkController */ /** @var MountPublicLinkController */
private $controller; private $controller;
@ -102,7 +105,8 @@ class MountPublicLinkControllerTest extends \Test\TestCase {
$this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock(); $this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
$this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock(); $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
$this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock(); $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
$this->cloudIdManager = new CloudIdManager(); $this->contactsManager = $this->createMock(IContactsManager::class);
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
$this->controller = new MountPublicLinkController( $this->controller = new MountPublicLinkController(
'federatedfilesharing', $this->request, 'federatedfilesharing', $this->request,

View File

@ -35,6 +35,7 @@ use OCA\FederatedFileSharing\AddressHandler;
use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\FederatedFileSharing\Notifications; use OCA\FederatedFileSharing\Notifications;
use OCA\FederatedFileSharing\TokenHandler; use OCA\FederatedFileSharing\TokenHandler;
use OCP\Contacts\IManager as IContactsManager;
use OCP\Federation\ICloudFederationProviderManager; use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\Files\File; use OCP\Files\File;
@ -79,6 +80,8 @@ class FederatedShareProviderTest extends \Test\TestCase {
protected $shareManager; protected $shareManager;
/** @var FederatedShareProvider */ /** @var FederatedShareProvider */
protected $provider; protected $provider;
/** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
protected $contactsManager;
/** @var ICloudIdManager */ /** @var ICloudIdManager */
private $cloudIdManager; private $cloudIdManager;
@ -107,7 +110,8 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock(); $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
//$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l); //$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l);
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock(); $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock();
$this->cloudIdManager = new CloudIdManager(); $this->contactsManager = $this->createMock(IContactsManager::class);
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
$this->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class); $this->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class);
$this->userManager->expects($this->any())->method('userExists')->willReturn(true); $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
@ -141,6 +145,7 @@ class FederatedShareProviderTest extends \Test\TestCase {
public function testCreate() { public function testCreate() {
$share = $this->shareManager->newShare(); $share = $this->shareManager->newShare();
/** @var File|MockObject $node */
$node = $this->getMockBuilder(File::class)->getMock(); $node = $this->getMockBuilder(File::class)->getMock();
$node->method('getId')->willReturn(42); $node->method('getId')->willReturn(42);
$node->method('getName')->willReturn('myFile'); $node->method('getName')->willReturn('myFile');
@ -170,10 +175,15 @@ class FederatedShareProviderTest extends \Test\TestCase {
'shareOwner@http://localhost/', 'shareOwner@http://localhost/',
'sharedBy', 'sharedBy',
'sharedBy@http://localhost/' 'sharedBy@http://localhost/'
)->willReturn(true); )
->willReturn(true);
$this->rootFolder->expects($this->never())->method($this->anything()); $this->rootFolder->expects($this->never())->method($this->anything());
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share = $this->provider->create($share); $share = $this->provider->create($share);
$qb = $this->connection->getQueryBuilder(); $qb = $this->connection->getQueryBuilder();
@ -250,6 +260,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
->with('42') ->with('42')
->willReturn([$node]); ->willReturn([$node]);
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
try { try {
$share = $this->provider->create($share); $share = $this->provider->create($share);
$this->fail(); $this->fail();
@ -307,6 +321,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
->with('42') ->with('42')
->willReturn([$node]); ->willReturn([$node]);
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
try { try {
$share = $this->provider->create($share); $share = $this->provider->create($share);
$this->fail(); $this->fail();
@ -344,6 +362,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
->setPermissions(19) ->setPermissions(19)
->setNode($node); ->setNode($node);
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$this->rootFolder->expects($this->never())->method($this->anything()); $this->rootFolder->expects($this->never())->method($this->anything());
try { try {
@ -403,6 +425,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->rootFolder->expects($this->never())->method($this->anything()); $this->rootFolder->expects($this->never())->method($this->anything());
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$this->provider->create($share); $this->provider->create($share);
try { try {
@ -441,7 +467,6 @@ class FederatedShareProviderTest extends \Test\TestCase {
$node->method('getId')->willReturn(42); $node->method('getId')->willReturn(42);
$node->method('getName')->willReturn('myFile'); $node->method('getName')->willReturn('myFile');
$this->addressHandler->expects($this->any())->method('splitUserRemote') $this->addressHandler->expects($this->any())->method('splitUserRemote')
->willReturn(['user', 'server.com']); ->willReturn(['user', 'server.com']);
@ -477,6 +502,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->rootFolder->expects($this->never())->method($this->anything()); $this->rootFolder->expects($this->never())->method($this->anything());
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share = $this->provider->create($share); $share = $this->provider->create($share);
$share->setPermissions(1); $share->setPermissions(1);
@ -515,6 +544,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->rootFolder->expects($this->never())->method($this->anything()); $this->rootFolder->expects($this->never())->method($this->anything());
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share = $this->shareManager->newShare(); $share = $this->shareManager->newShare();
$share->setSharedWith('user@server.com') $share->setSharedWith('user@server.com')
->setSharedBy('sharedBy') ->setSharedBy('sharedBy')
@ -555,6 +588,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->addressHandler->method('generateRemoteURL') $this->addressHandler->method('generateRemoteURL')
->willReturn('remoteurl.com'); ->willReturn('remoteurl.com');
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share = $this->shareManager->newShare(); $share = $this->shareManager->newShare();
$share->setSharedWith('user@server.com') $share->setSharedWith('user@server.com')
->setSharedBy('sharedBy') ->setSharedBy('sharedBy')
@ -598,6 +635,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->addressHandler->method('generateRemoteURL') $this->addressHandler->method('generateRemoteURL')
->willReturn('remoteurl.com'); ->willReturn('remoteurl.com');
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share = $this->shareManager->newShare(); $share = $this->shareManager->newShare();
$share->setSharedWith('user@server.com') $share->setSharedWith('user@server.com')
->setSharedBy('shareOwner') ->setSharedBy('shareOwner')
@ -644,6 +685,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->addressHandler->method('generateRemoteURL') $this->addressHandler->method('generateRemoteURL')
->willReturn('remoteurl.com'); ->willReturn('remoteurl.com');
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share = $this->shareManager->newShare(); $share = $this->shareManager->newShare();
$share->setSharedWith('user@server.com') $share->setSharedWith('user@server.com')
->setSharedBy('sharedBy') ->setSharedBy('sharedBy')
@ -844,6 +889,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
$this->addressHandler->method('generateRemoteURL') $this->addressHandler->method('generateRemoteURL')
->willReturn('remoteurl.com'); ->willReturn('remoteurl.com');
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$share1 = $this->shareManager->newShare(); $share1 = $this->shareManager->newShare();
$share1->setSharedWith('user@server.com') $share1->setSharedWith('user@server.com')
->setSharedBy($u1->getUID()) ->setSharedBy($u1->getUID())
@ -891,6 +940,10 @@ class FederatedShareProviderTest extends \Test\TestCase {
->method('sendRemoteShare') ->method('sendRemoteShare')
->willReturn(true); ->willReturn(true);
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$result = $this->provider->getAccessList([$file1], true); $result = $this->provider->getAccessList([$file1], true);
$this->assertEquals(['remote' => []], $result); $this->assertEquals(['remote' => []], $result);

View File

@ -29,6 +29,7 @@ namespace OCA\Files_Sharing\Tests\External;
use OC\Federation\CloudIdManager; use OC\Federation\CloudIdManager;
use OCA\Files_Sharing\Tests\TestCase; use OCA\Files_Sharing\Tests\TestCase;
use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
/** /**
@ -39,6 +40,8 @@ use OCP\Federation\ICloudIdManager;
* @package OCA\Files_Sharing\Tests\External * @package OCA\Files_Sharing\Tests\External
*/ */
class CacheTest extends TestCase { class CacheTest extends TestCase {
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $contactsManager;
/** /**
* @var \OC\Files\Storage\Storage * @var \OC\Files\Storage\Storage
@ -61,7 +64,9 @@ class CacheTest extends TestCase {
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->cloudIdManager = new CloudIdManager(); $this->contactsManager = $this->createMock(IManager::class);
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
$this->remoteUser = $this->getUniqueID('remoteuser'); $this->remoteUser = $this->getUniqueID('remoteuser');
$this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage') $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage')
@ -71,6 +76,11 @@ class CacheTest extends TestCase {
->expects($this->any()) ->expects($this->any())
->method('getId') ->method('getId')
->willReturn('dummystorage::'); ->willReturn('dummystorage::');
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$this->cache = new \OCA\Files_Sharing\External\Cache( $this->cache = new \OCA\Files_Sharing\External\Cache(
$this->storage, $this->storage,
$this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud') $this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud')

View File

@ -94,6 +94,12 @@ class ManagerTest extends TestCase {
$this->groupManager = $this->createMock(IGroupManager::class); $this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class); $this->userManager = $this->createMock(IUserManager::class);
$this->contactsManager = $this->createMock(IManager::class);
// needed for MountProvider() initialization
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
$this->manager = $this->getMockBuilder(Manager::class) $this->manager = $this->getMockBuilder(Manager::class)
->setConstructorArgs( ->setConstructorArgs(
[ [
@ -113,7 +119,7 @@ class ManagerTest extends TestCase {
$this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () { $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
return $this->manager; return $this->manager;
}, new CloudIdManager()); }, new CloudIdManager($this->contactsManager));
} }
private function setupMounts() { private function setupMounts() {

View File

@ -36,6 +36,8 @@ class CloudId implements ICloudId {
private $user; private $user;
/** @var string */ /** @var string */
private $remote; private $remote;
/** @var string|null */
private $displayName;
/** /**
* CloudId constructor. * CloudId constructor.
@ -44,10 +46,11 @@ class CloudId implements ICloudId {
* @param string $user * @param string $user
* @param string $remote * @param string $remote
*/ */
public function __construct(string $id, string $user, string $remote) { public function __construct(string $id, string $user, string $remote, ?string $displayName = null) {
$this->id = $id; $this->id = $id;
$this->user = $user; $this->user = $user;
$this->remote = $remote; $this->remote = $remote;
$this->displayName = $displayName;
} }
/** /**
@ -60,6 +63,11 @@ class CloudId implements ICloudId {
} }
public function getDisplayId(): string { public function getDisplayId(): string {
if ($this->displayName) {
$atPos = strrpos($this->getId(), '@');
$atHost = substr($this->getId(), $atPos);
return $this->displayName . $atHost;
}
return str_replace('https://', '', str_replace('http://', '', $this->getId())); return str_replace('https://', '', str_replace('http://', '', $this->getId()));
} }

View File

@ -29,10 +29,18 @@ declare(strict_types=1);
namespace OC\Federation; namespace OC\Federation;
use OCP\Contacts\IManager;
use OCP\Federation\ICloudId; use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
class CloudIdManager implements ICloudIdManager { class CloudIdManager implements ICloudIdManager {
/** @var IManager */
private $contactsManager;
public function __construct(IManager $contactsManager) {
$this->contactsManager = $contactsManager;
}
/** /**
* @param string $cloudId * @param string $cloudId
* @return ICloudId * @return ICloudId
@ -60,23 +68,32 @@ class CloudIdManager implements ICloudIdManager {
$invalidPos = min($posSlash, $posColon); $invalidPos = min($posSlash, $posColon);
} }
// Find the last @ before $invalidPos $lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id));
$pos = $lastAtPos = 0;
while ($lastAtPos !== false && $lastAtPos <= $invalidPos) {
$pos = $lastAtPos;
$lastAtPos = strpos($id, '@', $pos + 1);
}
if ($pos !== false) { if ($lastValidAtPos !== false) {
$user = substr($id, 0, $pos); $user = substr($id, 0, $lastValidAtPos);
$remote = substr($id, $pos + 1); $remote = substr($id, $lastValidAtPos + 1);
if (!empty($user) && !empty($remote)) { if (!empty($user) && !empty($remote)) {
return new CloudId($id, $user, $remote); return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
} }
} }
throw new \InvalidArgumentException('Invalid cloud id'); throw new \InvalidArgumentException('Invalid cloud id');
} }
protected function getDisplayNameFromContact(string $cloudId): ?string {
$addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD']);
foreach ($addressBookEntries as $entry) {
if (isset($entry['CLOUD'])) {
foreach ($entry['CLOUD'] as $cloudID) {
if ($cloudID === $cloudId) {
return $entry['FN'];
}
}
}
}
return null;
}
/** /**
* @param string $user * @param string $user
* @param string $remote * @param string $remote
@ -84,7 +101,17 @@ class CloudIdManager implements ICloudIdManager {
*/ */
public function getCloudId(string $user, string $remote): ICloudId { public function getCloudId(string $user, string $remote): ICloudId {
// TODO check what the correct url is for remote (asking the remote) // TODO check what the correct url is for remote (asking the remote)
return new CloudId($user. '@' . $remote, $user, $remote); $fixedRemote = $this->fixRemoteURL($remote);
if (strpos($fixedRemote, 'http://') === 0) {
$host = substr($fixedRemote, strlen('http://'));
} elseif (strpos($fixedRemote, 'https://') === 0) {
$host = substr($fixedRemote, strlen('https://'));
} else {
$host = $fixedRemote;
}
$id = $user . '@' . $remote;
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
return new CloudId($id, $user, $fixedRemote, $displayName);
} }
/** /**

View File

@ -1278,7 +1278,7 @@ class Server extends ServerContainer implements IServerContainer {
}); });
$this->registerService(ICloudIdManager::class, function (Server $c) { $this->registerService(ICloudIdManager::class, function (Server $c) {
return new CloudIdManager(); return new CloudIdManager($c->getContactsManager());
}); });
$this->registerService(IConfig::class, function (Server $c) { $this->registerService(IConfig::class, function (Server $c) {

View File

@ -65,7 +65,8 @@ class MailPluginTest extends TestCase {
$this->contactsManager = $this->createMock(IManager::class); $this->contactsManager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class); $this->groupManager = $this->createMock(IGroupManager::class);
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->cloudIdManager = new CloudIdManager(); $this->cloudIdManager = new CloudIdManager($this->contactsManager);
$this->searchResult = new SearchResult(); $this->searchResult = new SearchResult();
} }
@ -104,8 +105,12 @@ class MailPluginTest extends TestCase {
$this->contactsManager->expects($this->any()) $this->contactsManager->expects($this->any())
->method('search') ->method('search')
->with($searchTerm, ['EMAIL', 'FN']) ->willReturnCallback(function ($search, $searchAttributes) use ($searchTerm, $contacts) {
->willReturn($contacts); if ($search === $searchTerm) {
return $contacts;
}
return [];
});
$moreResults = $this->plugin->search($searchTerm, 2, 0, $this->searchResult); $moreResults = $this->plugin->search($searchTerm, 2, 0, $this->searchResult);
$result = $this->searchResult->asArray(); $result = $this->searchResult->asArray();
@ -553,8 +558,12 @@ class MailPluginTest extends TestCase {
$this->contactsManager->expects($this->any()) $this->contactsManager->expects($this->any())
->method('search') ->method('search')
->with($searchTerm, ['EMAIL', 'FN']) ->willReturnCallback(function ($search, $searchAttributes) use ($searchTerm, $contacts) {
->willReturn($contacts); if ($search === $searchTerm) {
return $contacts;
}
return [];
});
$this->userSession->expects($this->any()) $this->userSession->expects($this->any())
->method('getUser') ->method('getUser')

View File

@ -62,7 +62,7 @@ class RemotePluginTest extends TestCase {
$this->userManager = $this->createMock(IUserManager::class); $this->userManager = $this->createMock(IUserManager::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->contactsManager = $this->createMock(IManager::class); $this->contactsManager = $this->createMock(IManager::class);
$this->cloudIdManager = new CloudIdManager(); $this->cloudIdManager = new CloudIdManager($this->contactsManager);
$this->searchResult = new SearchResult(); $this->searchResult = new SearchResult();
} }
@ -104,8 +104,12 @@ class RemotePluginTest extends TestCase {
$this->contactsManager->expects($this->any()) $this->contactsManager->expects($this->any())
->method('search') ->method('search')
->with($searchTerm, ['CLOUD', 'FN']) ->willReturnCallback(function ($search, $searchAttributes) use ($searchTerm, $contacts) {
->willReturn($contacts); if ($search === $searchTerm) {
return $contacts;
}
return [];
});
$moreResults = $this->plugin->search($searchTerm, 2, 0, $this->searchResult); $moreResults = $this->plugin->search($searchTerm, 2, 0, $this->searchResult);
$result = $this->searchResult->asArray(); $result = $this->searchResult->asArray();
@ -125,6 +129,10 @@ class RemotePluginTest extends TestCase {
public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) { public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
$this->instantiatePlugin(); $this->instantiatePlugin();
$this->contactsManager->expects($this->any())
->method('search')
->willReturn([]);
list($remoteUser, $remoteUrl) = $this->plugin->splitUserRemote($remote); list($remoteUser, $remoteUrl) = $this->plugin->splitUserRemote($remote);
$this->assertSame($expectedUser, $remoteUser); $this->assertSame($expectedUser, $remoteUser);
$this->assertSame($expectedUrl, $remoteUrl); $this->assertSame($expectedUrl, $remoteUrl);

View File

@ -22,15 +22,21 @@
namespace Test\Federation; namespace Test\Federation;
use OC\Federation\CloudIdManager; use OC\Federation\CloudIdManager;
use OCP\Contacts\IManager;
use Test\TestCase; use Test\TestCase;
class CloudIdManagerTest extends TestCase { class CloudIdManagerTest extends TestCase {
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $contactsManager;
/** @var CloudIdManager */ /** @var CloudIdManager */
private $cloudIdManager; private $cloudIdManager;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->cloudIdManager = new CloudIdManager();
$this->contactsManager = $this->createMock(IManager::class);
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
} }
public function cloudIdProvider() { public function cloudIdProvider() {
@ -51,11 +57,24 @@ class CloudIdManagerTest extends TestCase {
* @param string $remote * @param string $remote
*/ */
public function testResolveCloudId($cloudId, $user, $remote, $cleanId) { public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
$displayName = 'Ample Ex';
$this->contactsManager->expects($this->any())
->method('search')
->with($cleanId, ['CLOUD'])
->willReturn([
[
'CLOUD' => [$cleanId],
'FN' => 'Ample Ex',
]
]);
$cloudId = $this->cloudIdManager->resolveCloudId($cloudId); $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
$this->assertEquals($user, $cloudId->getUser()); $this->assertEquals($user, $cloudId->getUser());
$this->assertEquals($remote, $cloudId->getRemote()); $this->assertEquals($remote, $cloudId->getRemote());
$this->assertEquals($cleanId, $cloudId->getId()); $this->assertEquals($cleanId, $cloudId->getId());
$this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
} }
public function invalidCloudIdProvider() { public function invalidCloudIdProvider() {
@ -75,6 +94,9 @@ class CloudIdManagerTest extends TestCase {
public function testInvalidCloudId($cloudId) { public function testInvalidCloudId($cloudId) {
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
$this->contactsManager->expects($this->never())
->method('search');
$this->cloudIdManager->resolveCloudId($cloudId); $this->cloudIdManager->resolveCloudId($cloudId);
} }
@ -93,6 +115,16 @@ class CloudIdManagerTest extends TestCase {
* @param string $id * @param string $id
*/ */
public function testGetCloudId($user, $remote, $id) { public function testGetCloudId($user, $remote, $id) {
$this->contactsManager->expects($this->any())
->method('search')
->with($id, ['CLOUD'])
->willReturn([
[
'CLOUD' => [$id],
'FN' => 'Ample Ex',
]
]);
$cloudId = $this->cloudIdManager->getCloudId($user, $remote); $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
$this->assertEquals($id, $cloudId->getId()); $this->assertEquals($id, $cloudId->getId());