Skip to content

Commit 8c94752

Browse files
blizzzbackportbot[bot]
authored andcommitted
set the display name of federated sharees from addressbook
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent e99d6b4 commit 8c94752

10 files changed

Lines changed: 193 additions & 28 deletions

File tree

apps/federatedfilesharing/tests/AddressHandlerTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929

3030
use OC\Federation\CloudIdManager;
3131
use OCA\FederatedFileSharing\AddressHandler;
32+
use OCP\Contacts\IManager;
3233
use OCP\IL10N;
3334
use OCP\IURLGenerator;
3435

3536
class AddressHandlerTest extends \Test\TestCase {
37+
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
38+
protected $contactsManager;
3639

3740
/** @var AddressHandler */
3841
private $addressHandler;
@@ -54,7 +57,9 @@ protected function setUp(): void {
5457
$this->il10n = $this->getMockBuilder(IL10N::class)
5558
->getMock();
5659

57-
$this->cloudIdManager = new CloudIdManager();
60+
$this->contactsManager = $this->createMock(IManager::class);
61+
62+
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
5863

5964
$this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
6065
}
@@ -98,6 +103,10 @@ public function dataTestSplitUserRemote() {
98103
* @param string $expectedUrl
99104
*/
100105
public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
106+
$this->contactsManager->expects($this->any())
107+
->method('search')
108+
->willReturn([]);
109+
101110
list($remoteUser, $remoteUrl) = $this->addressHandler->splitUserRemote($remote);
102111
$this->assertSame($expectedUser, $remoteUser);
103112
$this->assertSame($expectedUrl, $remoteUrl);

apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCA\FederatedFileSharing\Controller\MountPublicLinkController;
3535
use OCA\FederatedFileSharing\FederatedShareProvider;
3636
use OCP\AppFramework\Http;
37+
use OCP\Contacts\IManager as IContactsManager;
3738
use OCP\Federation\ICloudIdManager;
3839
use OCP\Files\IRootFolder;
3940
use OCP\Http\Client\IClientService;
@@ -46,6 +47,8 @@
4647
use OCP\Share\IShare;
4748

4849
class MountPublicLinkControllerTest extends \Test\TestCase {
50+
/** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
51+
protected $contactsManager;
4952

5053
/** @var MountPublicLinkController */
5154
private $controller;
@@ -102,7 +105,8 @@ protected function setUp(): void {
102105
$this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
103106
$this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
104107
$this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
105-
$this->cloudIdManager = new CloudIdManager();
108+
$this->contactsManager = $this->createMock(IContactsManager::class);
109+
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
106110

107111
$this->controller = new MountPublicLinkController(
108112
'federatedfilesharing', $this->request,

apps/federatedfilesharing/tests/FederatedShareProviderTest.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCA\FederatedFileSharing\FederatedShareProvider;
3636
use OCA\FederatedFileSharing\Notifications;
3737
use OCA\FederatedFileSharing\TokenHandler;
38+
use OCP\Contacts\IManager as IContactsManager;
3839
use OCP\Federation\ICloudFederationProviderManager;
3940
use OCP\Federation\ICloudIdManager;
4041
use OCP\Files\File;
@@ -79,6 +80,8 @@ class FederatedShareProviderTest extends \Test\TestCase {
7980
protected $shareManager;
8081
/** @var FederatedShareProvider */
8182
protected $provider;
83+
/** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
84+
protected $contactsManager;
8285

8386
/** @var ICloudIdManager */
8487
private $cloudIdManager;
@@ -107,7 +110,8 @@ protected function setUp(): void {
107110
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
108111
//$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l);
109112
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock();
110-
$this->cloudIdManager = new CloudIdManager();
113+
$this->contactsManager = $this->createMock(IContactsManager::class);
114+
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
111115
$this->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class);
112116

113117
$this->userManager->expects($this->any())->method('userExists')->willReturn(true);
@@ -141,6 +145,7 @@ protected function tearDown(): void {
141145
public function testCreate() {
142146
$share = $this->shareManager->newShare();
143147

148+
/** @var File|MockObject $node */
144149
$node = $this->getMockBuilder(File::class)->getMock();
145150
$node->method('getId')->willReturn(42);
146151
$node->method('getName')->willReturn('myFile');
@@ -170,10 +175,15 @@ public function testCreate() {
170175
'shareOwner@http://localhost/',
171176
'sharedBy',
172177
'sharedBy@http://localhost/'
173-
)->willReturn(true);
178+
)
179+
->willReturn(true);
174180

175181
$this->rootFolder->expects($this->never())->method($this->anything());
176182

183+
$this->contactsManager->expects($this->any())
184+
->method('search')
185+
->willReturn([]);
186+
177187
$share = $this->provider->create($share);
178188

179189
$qb = $this->connection->getQueryBuilder();
@@ -250,6 +260,10 @@ public function testCreateCouldNotFindServer() {
250260
->with('42')
251261
->willReturn([$node]);
252262

263+
$this->contactsManager->expects($this->any())
264+
->method('search')
265+
->willReturn([]);
266+
253267
try {
254268
$share = $this->provider->create($share);
255269
$this->fail();
@@ -307,6 +321,10 @@ public function testCreateException() {
307321
->with('42')
308322
->willReturn([$node]);
309323

324+
$this->contactsManager->expects($this->any())
325+
->method('search')
326+
->willReturn([]);
327+
310328
try {
311329
$share = $this->provider->create($share);
312330
$this->fail();
@@ -344,6 +362,10 @@ public function testCreateShareWithSelf() {
344362
->setPermissions(19)
345363
->setNode($node);
346364

365+
$this->contactsManager->expects($this->any())
366+
->method('search')
367+
->willReturn([]);
368+
347369
$this->rootFolder->expects($this->never())->method($this->anything());
348370

349371
try {
@@ -403,6 +425,10 @@ public function testCreateAlreadyShared() {
403425

404426
$this->rootFolder->expects($this->never())->method($this->anything());
405427

428+
$this->contactsManager->expects($this->any())
429+
->method('search')
430+
->willReturn([]);
431+
406432
$this->provider->create($share);
407433

408434
try {
@@ -441,7 +467,6 @@ public function testUpdate($owner, $sharedBy) {
441467
$node->method('getId')->willReturn(42);
442468
$node->method('getName')->willReturn('myFile');
443469

444-
445470
$this->addressHandler->expects($this->any())->method('splitUserRemote')
446471
->willReturn(['user', 'server.com']);
447472

@@ -477,6 +502,10 @@ public function testUpdate($owner, $sharedBy) {
477502

478503
$this->rootFolder->expects($this->never())->method($this->anything());
479504

505+
$this->contactsManager->expects($this->any())
506+
->method('search')
507+
->willReturn([]);
508+
480509
$share = $this->provider->create($share);
481510

482511
$share->setPermissions(1);
@@ -515,6 +544,10 @@ public function testGetSharedBy() {
515544

516545
$this->rootFolder->expects($this->never())->method($this->anything());
517546

547+
$this->contactsManager->expects($this->any())
548+
->method('search')
549+
->willReturn([]);
550+
518551
$share = $this->shareManager->newShare();
519552
$share->setSharedWith('user@server.com')
520553
->setSharedBy('sharedBy')
@@ -555,6 +588,10 @@ public function testGetSharedByWithNode() {
555588
$this->addressHandler->method('generateRemoteURL')
556589
->willReturn('remoteurl.com');
557590

591+
$this->contactsManager->expects($this->any())
592+
->method('search')
593+
->willReturn([]);
594+
558595
$share = $this->shareManager->newShare();
559596
$share->setSharedWith('user@server.com')
560597
->setSharedBy('sharedBy')
@@ -598,6 +635,10 @@ public function testGetSharedByWithReshares() {
598635
$this->addressHandler->method('generateRemoteURL')
599636
->willReturn('remoteurl.com');
600637

638+
$this->contactsManager->expects($this->any())
639+
->method('search')
640+
->willReturn([]);
641+
601642
$share = $this->shareManager->newShare();
602643
$share->setSharedWith('user@server.com')
603644
->setSharedBy('shareOwner')
@@ -644,6 +685,10 @@ public function testGetSharedByWithLimit() {
644685
$this->addressHandler->method('generateRemoteURL')
645686
->willReturn('remoteurl.com');
646687

688+
$this->contactsManager->expects($this->any())
689+
->method('search')
690+
->willReturn([]);
691+
647692
$share = $this->shareManager->newShare();
648693
$share->setSharedWith('user@server.com')
649694
->setSharedBy('sharedBy')
@@ -844,6 +889,10 @@ public function testGetSharesInFolder() {
844889
$this->addressHandler->method('generateRemoteURL')
845890
->willReturn('remoteurl.com');
846891

892+
$this->contactsManager->expects($this->any())
893+
->method('search')
894+
->willReturn([]);
895+
847896
$share1 = $this->shareManager->newShare();
848897
$share1->setSharedWith('user@server.com')
849898
->setSharedBy($u1->getUID())
@@ -891,6 +940,10 @@ public function testGetAccessList() {
891940
->method('sendRemoteShare')
892941
->willReturn(true);
893942

943+
$this->contactsManager->expects($this->any())
944+
->method('search')
945+
->willReturn([]);
946+
894947
$result = $this->provider->getAccessList([$file1], true);
895948
$this->assertEquals(['remote' => []], $result);
896949

apps/files_sharing/tests/External/CacheTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
use OC\Federation\CloudIdManager;
3131
use OCA\Files_Sharing\Tests\TestCase;
32+
use OCP\Contacts\IManager;
3233
use OCP\Federation\ICloudIdManager;
3334

3435
/**
@@ -39,6 +40,8 @@
3940
* @package OCA\Files_Sharing\Tests\External
4041
*/
4142
class CacheTest extends TestCase {
43+
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
44+
protected $contactsManager;
4245

4346
/**
4447
* @var \OC\Files\Storage\Storage
@@ -61,7 +64,9 @@ class CacheTest extends TestCase {
6164
protected function setUp(): void {
6265
parent::setUp();
6366

64-
$this->cloudIdManager = new CloudIdManager();
67+
$this->contactsManager = $this->createMock(IManager::class);
68+
69+
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
6570
$this->remoteUser = $this->getUniqueID('remoteuser');
6671

6772
$this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage')
@@ -71,6 +76,11 @@ protected function setUp(): void {
7176
->expects($this->any())
7277
->method('getId')
7378
->willReturn('dummystorage::');
79+
80+
$this->contactsManager->expects($this->any())
81+
->method('search')
82+
->willReturn([]);
83+
7484
$this->cache = new \OCA\Files_Sharing\External\Cache(
7585
$this->storage,
7686
$this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud')

apps/files_sharing/tests/External/ManagerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ protected function setUp(): void {
9494
$this->groupManager = $this->createMock(IGroupManager::class);
9595
$this->userManager = $this->createMock(IUserManager::class);
9696

97+
$this->contactsManager = $this->createMock(IManager::class);
98+
// needed for MountProvider() initialization
99+
$this->contactsManager->expects($this->any())
100+
->method('search')
101+
->willReturn([]);
102+
97103
$this->manager = $this->getMockBuilder(Manager::class)
98104
->setConstructorArgs(
99105
[
@@ -113,7 +119,7 @@ protected function setUp(): void {
113119

114120
$this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
115121
return $this->manager;
116-
}, new CloudIdManager());
122+
}, new CloudIdManager($this->contactsManager));
117123
}
118124

119125
private function setupMounts() {

lib/private/Federation/CloudId.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class CloudId implements ICloudId {
3636
private $user;
3737
/** @var string */
3838
private $remote;
39+
/** @var string|null */
40+
private $displayName;
3941

4042
/**
4143
* CloudId constructor.
@@ -44,10 +46,11 @@ class CloudId implements ICloudId {
4446
* @param string $user
4547
* @param string $remote
4648
*/
47-
public function __construct(string $id, string $user, string $remote) {
49+
public function __construct(string $id, string $user, string $remote, ?string $displayName = null) {
4850
$this->id = $id;
4951
$this->user = $user;
5052
$this->remote = $remote;
53+
$this->displayName = $displayName;
5154
}
5255

5356
/**
@@ -60,6 +63,11 @@ public function getId(): string {
6063
}
6164

6265
public function getDisplayId(): string {
66+
if ($this->displayName) {
67+
$atPos = strrpos($this->getId(), '@');
68+
$atHost = substr($this->getId(), $atPos);
69+
return $this->displayName . $atHost;
70+
}
6371
return str_replace('https://', '', str_replace('http://', '', $this->getId()));
6472
}
6573

0 commit comments

Comments
 (0)