1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files\Tests\Activity;
use OCA\Files\Activity\Provider;
use OCP\Activity\Exceptions\UnknownActivityException;
use OCP\Activity\IEvent;
use OCP\Activity\IEventMerger;
use OCP\Activity\IManager;
use OCP\Contacts\IManager as IContactsManager;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\Files\IRootFolder;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
/**
* Class ProviderTest
*
* @package OCA\Files\Tests\Activity
*/
class ProviderTest extends TestCase {
protected IFactory&MockObject $l10nFactory;
protected IURLGenerator&MockObject $url;
protected IManager&MockObject $activityManager;
protected IUserManager&MockObject $userManager;
protected IRootFolder&MockObject $rootFolder;
protected ICloudIdManager&MockObject $cloudIdManager;
protected IContactsManager&MockObject $contactsManager;
protected IEventMerger&MockObject $eventMerger;
protected function setUp(): void {
parent::setUp();
$this->l10nFactory = $this->createMock(IFactory::class);
$this->url = $this->createMock(IURLGenerator::class);
$this->activityManager = $this->createMock(IManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
$this->contactsManager = $this->createMock(IContactsManager::class);
$this->eventMerger = $this->createMock(IEventMerger::class);
}
/**
* @param string[] $methods
* @return Provider|MockObject
*/
protected function getProvider(array $methods = []) {
if (!empty($methods)) {
return $this->getMockBuilder(Provider::class)
->setConstructorArgs([
$this->l10nFactory,
$this->url,
$this->activityManager,
$this->userManager,
$this->rootFolder,
$this->cloudIdManager,
$this->contactsManager,
$this->eventMerger,
])
->onlyMethods($methods)
->getMock();
}
return new Provider(
$this->l10nFactory,
$this->url,
$this->activityManager,
$this->userManager,
$this->rootFolder,
$this->cloudIdManager,
$this->contactsManager,
$this->eventMerger
);
}
public static function dataGetFile(): array {
return [
[[42 => '/FortyTwo.txt'], null, '42', 'FortyTwo.txt', 'FortyTwo.txt'],
[['23' => '/Twenty/Three.txt'], null, '23', 'Three.txt', 'Twenty/Three.txt'],
['/Foo/Bar.txt', 128, '128', 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before
];
}
/**
* @dataProvider dataGetFile
*/
public function testGetFile(array|string $parameter, ?int $eventId, string $id, string $name, string $path): void {
$provider = $this->getProvider();
if ($eventId !== null) {
$event = $this->createMock(IEvent::class);
$event->expects($this->once())
->method('getObjectId')
->willReturn($eventId);
} else {
$event = null;
}
$this->url->expects($this->once())
->method('linkToRouteAbsolute')
->with('files.viewcontroller.showFile', ['fileid' => $id])
->willReturn('link-' . $id);
$result = self::invokePrivate($provider, 'getFile', [$parameter, $event]);
$this->assertSame('file', $result['type']);
$this->assertSame($id, $result['id']);
$this->assertSame($name, $result['name']);
$this->assertSame($path, $result['path']);
$this->assertSame('link-' . $id, $result['link']);
}
public function testGetFileThrows(): void {
$this->expectException(UnknownActivityException::class);
$provider = $this->getProvider();
self::invokePrivate($provider, 'getFile', ['/Foo/Bar.txt', null]);
}
public static function dataGetUser(): array {
return [
['test', 'Test user', null, ['type' => 'user', 'id' => 'test', 'name' => 'Test user']],
['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => null], ['type' => 'user', 'id' => 'test', 'name' => 'test@localhost', 'server' => 'localhost']],
['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']],
['test', null, null, ['type' => 'user', 'id' => 'test', 'name' => 'test']],
];
}
/**
* @dataProvider dataGetUser
*/
public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void {
$provider = $this->getProvider();
if ($userDisplayName !== null) {
$this->userManager->expects($this->once())
->method('getDisplayName')
->with($uid)
->willReturn($userDisplayName);
}
if ($cloudIdData !== null) {
$this->cloudIdManager->expects($this->once())
->method('isValidCloudId')
->willReturn(true);
$cloudId = $this->createMock(ICloudId::class);
$cloudId->expects($this->once())
->method('getUser')
->willReturn($cloudIdData['user']);
$cloudId->expects($this->once())
->method('getDisplayId')
->willReturn($cloudIdData['displayId']);
$cloudId->expects($this->once())
->method('getRemote')
->willReturn($cloudIdData['remote']);
$this->cloudIdManager->expects($this->once())
->method('resolveCloudId')
->with($uid)
->willReturn($cloudId);
if ($cloudIdData['name'] !== null) {
$this->contactsManager->expects($this->once())
->method('search')
->with($cloudIdData['displayId'], ['CLOUD'])
->willReturn([
[
'CLOUD' => $cloudIdData['displayId'],
'FN' => $cloudIdData['name'],
]
]);
} else {
$this->contactsManager->expects($this->once())
->method('search')
->with($cloudIdData['displayId'], ['CLOUD'])
->willReturn([]);
}
}
$result = self::invokePrivate($provider, 'getUser', [$uid]);
$this->assertEquals($expected, $result);
}
}
|