Skip to content

Commit dbe1b92

Browse files
committed
Fix unit tests
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent d0e3305 commit dbe1b92

File tree

1 file changed

+75
-56
lines changed

1 file changed

+75
-56
lines changed

apps/files/tests/Activity/ProviderTest.php

Lines changed: 75 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
use OCP\Activity\IEvent;
3030
use OCP\Activity\IEventMerger;
3131
use OCP\Activity\IManager;
32+
use OCP\Contacts\IManager as IContactsManager;
33+
use OCP\Federation\ICloudId;
34+
use OCP\Federation\ICloudIdManager;
3235
use OCP\Files\IRootFolder;
3336
use OCP\IURLGenerator;
3437
use OCP\IUser;
3538
use OCP\IUserManager;
3639
use OCP\L10N\IFactory;
40+
use PHPUnit\Framework\MockObject\MockObject;
3741
use Test\TestCase;
3842

3943
/**
@@ -43,17 +47,21 @@
4347
*/
4448
class ProviderTest extends TestCase {
4549

46-
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
50+
/** @var IFactory|MockObject */
4751
protected $l10nFactory;
48-
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
52+
/** @var IURLGenerator|MockObject */
4953
protected $url;
50-
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
54+
/** @var IManager|MockObject */
5155
protected $activityManager;
52-
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
56+
/** @var IUserManager|MockObject */
5357
protected $userManager;
54-
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
58+
/** @var IRootFolder|MockObject */
5559
protected $rootFolder;
56-
/** @var IEventMerger|\PHPUnit_Framework_MockObject_MockObject */
60+
/** @var ICloudIdManager|MockObject */
61+
protected $cloudIdManager;
62+
/** @var IContactsManager|MockObject */
63+
protected $contactsManager;
64+
/** @var IEventMerger|MockObject */
5765
protected $eventMerger;
5866

5967
protected function setUp(): void {
@@ -64,12 +72,14 @@ protected function setUp(): void {
6472
$this->activityManager = $this->createMock(IManager::class);
6573
$this->userManager = $this->createMock(IUserManager::class);
6674
$this->rootFolder = $this->createMock(IRootFolder::class);
75+
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
76+
$this->contactsManager = $this->createMock(IContactsManager::class);
6777
$this->eventMerger = $this->createMock(IEventMerger::class);
6878
}
6979

7080
/**
7181
* @param string[] $methods
72-
* @return Provider|\PHPUnit_Framework_MockObject_MockObject
82+
* @return Provider|MockObject
7383
*/
7484
protected function getProvider(array $methods = []) {
7585
if (!empty($methods)) {
@@ -80,6 +90,8 @@ protected function getProvider(array $methods = []) {
8090
$this->activityManager,
8191
$this->userManager,
8292
$this->rootFolder,
93+
$this->cloudIdManager,
94+
$this->contactsManager,
8395
$this->eventMerger,
8496
])
8597
->setMethods($methods)
@@ -91,6 +103,8 @@ protected function getProvider(array $methods = []) {
91103
$this->activityManager,
92104
$this->userManager,
93105
$this->rootFolder,
106+
$this->cloudIdManager,
107+
$this->contactsManager,
94108
$this->eventMerger
95109
);
96110
}
@@ -99,7 +113,7 @@ public function dataGetFile() {
99113
return [
100114
[[42 => '/FortyTwo.txt'], null, '42', 'FortyTwo.txt', 'FortyTwo.txt'],
101115
[['23' => '/Twenty/Three.txt'], null, '23', 'Three.txt', 'Twenty/Three.txt'],
102-
['/Foo/Bar.txt', '128', 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before
116+
['/Foo/Bar.txt', 128, 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before
103117
];
104118
}
105119

@@ -137,7 +151,7 @@ public function testGetFile($parameter, $eventId, $id, $name, $path) {
137151
$this->assertSame('link-' . $id, $result['link']);
138152
}
139153

140-
154+
141155
public function testGetFileThrows() {
142156
$this->expectException(\InvalidArgumentException::class);
143157

@@ -147,71 +161,76 @@ public function testGetFileThrows() {
147161

148162
public function dataGetUser() {
149163
return [
150-
['test', [], false, 'Test'],
151-
['foo', ['admin' => 'Admin'], false, 'Bar'],
152-
['admin', ['admin' => 'Administrator'], true, 'Administrator'],
164+
['test', 'Test user', null, ['type' => 'user', 'id' => 'test', 'name' => 'Test user']],
165+
['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => null], ['type' => 'user', 'id' => 'test', 'name' => 'test@localhost', 'server' => 'localhost']],
166+
['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => 'Remote user'], ['type' => 'user', 'id' => 'test', 'name' => 'Remote user (test@localhost)', 'server' => 'localhost']],
167+
['test', null, null, ['type' => 'user', 'id' => 'test', 'name' => 'test']],
153168
];
154169
}
155170

156171
/**
157172
* @dataProvider dataGetUser
158173
* @param string $uid
159-
* @param array $cache
160-
* @param bool $cacheHit
161-
* @param string $name
174+
* @param string|null $userDisplayName
175+
* @param array|null $cloudIdData
176+
* @param array $expected
162177
*/
163-
public function testGetUser($uid, $cache, $cacheHit, $name) {
164-
$provider = $this->getProvider(['getDisplayName']);
165-
166-
self::invokePrivate($provider, 'displayNames', [$cache]);
167-
168-
if (!$cacheHit) {
169-
$provider->expects($this->once())
170-
->method('getDisplayName')
171-
->with($uid)
172-
->willReturn($name);
173-
} else {
174-
$provider->expects($this->never())
175-
->method('getDisplayName');
176-
}
177-
178-
$result = self::invokePrivate($provider, 'getUser', [$uid]);
179-
$this->assertSame('user', $result['type']);
180-
$this->assertSame($uid, $result['id']);
181-
$this->assertSame($name, $result['name']);
182-
}
183-
184-
public function dataGetDisplayName() {
185-
return [
186-
['test', true, 'Test'],
187-
['foo', false, 'foo'],
188-
];
189-
}
190-
191-
/**
192-
* @dataProvider dataGetDisplayName
193-
* @param string $uid
194-
* @param string $name
195-
*/
196-
public function testGetDisplayNamer($uid, $validUser, $name) {
178+
public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void {
197179
$provider = $this->getProvider();
198180

199-
if ($validUser) {
181+
if ($userDisplayName !== null) {
200182
$user = $this->createMock(IUser::class);
183+
$user->expects($this->once())
184+
->method('getUID')
185+
->willReturn($uid);
201186
$user->expects($this->once())
202187
->method('getDisplayName')
203-
->willReturn($name);
188+
->willReturn($userDisplayName);
204189
$this->userManager->expects($this->once())
205190
->method('get')
206191
->with($uid)
207192
->willReturn($user);
208-
} else {
209-
$this->userManager->expects($this->once())
210-
->method('get')
193+
}
194+
if ($cloudIdData !== null) {
195+
$this->cloudIdManager->expects($this->once())
196+
->method('isValidCloudId')
197+
->willReturn(true);
198+
199+
$cloudId = $this->createMock(ICloudId::class);
200+
$cloudId->expects($this->once())
201+
->method('getUser')
202+
->willReturn($cloudIdData['user']);
203+
$cloudId->expects($this->once())
204+
->method('getDisplayId')
205+
->willReturn($cloudIdData['displayId']);
206+
$cloudId->expects($this->once())
207+
->method('getRemote')
208+
->willReturn($cloudIdData['remote']);
209+
210+
$this->cloudIdManager->expects($this->once())
211+
->method('resolveCloudId')
211212
->with($uid)
212-
->willReturn(null);
213+
->willReturn($cloudId);
214+
215+
if ($cloudIdData['name'] !== null) {
216+
$this->contactsManager->expects($this->once())
217+
->method('search')
218+
->with($cloudIdData['displayId'], ['CLOUD'])
219+
->willReturn([
220+
[
221+
'CLOUD' => $cloudIdData['displayId'],
222+
'FN' => $cloudIdData['name'],
223+
]
224+
]);
225+
} else {
226+
$this->contactsManager->expects($this->once())
227+
->method('search')
228+
->with($cloudIdData['displayId'], ['CLOUD'])
229+
->willReturn([]);
230+
}
213231
}
214232

215-
$this->assertSame($name, self::invokePrivate($provider, 'getDisplayName', [$uid]));
233+
$result = self::invokePrivate($provider, 'getUser', [$uid]);
234+
$this->assertEquals($expected, $result);
216235
}
217236
}

0 commit comments

Comments
 (0)