Completed
Push — master ( b36223...1eab80 )
by
unknown
27:41 queued 14s
created
apps/files_sharing/tests/MountProviderTest.php 2 patches
Indentation   +354 added lines, -354 removed lines patch added patch discarded remove patch
@@ -28,380 +28,380 @@
 block discarded – undo
28 28
  */
29 29
 class MountProviderTest extends \Test\TestCase {
30 30
 
31
-	protected MountProvider $provider;
31
+    protected MountProvider $provider;
32 32
 
33
-	protected IUser&MockObject $user;
34
-	protected IConfig&MockObject $config;
35
-	protected IManager&MockObject $shareManager;
36
-	protected IStorageFactory&MockObject $loader;
37
-	protected LoggerInterface&MockObject $logger;
33
+    protected IUser&MockObject $user;
34
+    protected IConfig&MockObject $config;
35
+    protected IManager&MockObject $shareManager;
36
+    protected IStorageFactory&MockObject $loader;
37
+    protected LoggerInterface&MockObject $logger;
38 38
 
39
-	protected function setUp(): void {
40
-		parent::setUp();
39
+    protected function setUp(): void {
40
+        parent::setUp();
41 41
 
42
-		$this->config = $this->getMockBuilder(IConfig::class)->getMock();
43
-		$this->user = $this->getMockBuilder(IUser::class)->getMock();
44
-		$this->loader = $this->getMockBuilder('OCP\Files\Storage\IStorageFactory')->getMock();
45
-		$this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
46
-		$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
47
-		$eventDispatcher = $this->createMock(IEventDispatcher::class);
48
-		$cacheFactory = $this->createMock(ICacheFactory::class);
49
-		$cacheFactory->method('createLocal')
50
-			->willReturn(new NullCache());
51
-		$mountManager = $this->createMock(IMountManager::class);
42
+        $this->config = $this->getMockBuilder(IConfig::class)->getMock();
43
+        $this->user = $this->getMockBuilder(IUser::class)->getMock();
44
+        $this->loader = $this->getMockBuilder('OCP\Files\Storage\IStorageFactory')->getMock();
45
+        $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
46
+        $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
47
+        $eventDispatcher = $this->createMock(IEventDispatcher::class);
48
+        $cacheFactory = $this->createMock(ICacheFactory::class);
49
+        $cacheFactory->method('createLocal')
50
+            ->willReturn(new NullCache());
51
+        $mountManager = $this->createMock(IMountManager::class);
52 52
 
53
-		$this->provider = new MountProvider($this->config, $this->shareManager, $this->logger, $eventDispatcher, $cacheFactory, $mountManager);
54
-	}
53
+        $this->provider = new MountProvider($this->config, $this->shareManager, $this->logger, $eventDispatcher, $cacheFactory, $mountManager);
54
+    }
55 55
 
56
-	private function makeMockShareAttributes($attrs) {
57
-		if ($attrs === null) {
58
-			return null;
59
-		}
56
+    private function makeMockShareAttributes($attrs) {
57
+        if ($attrs === null) {
58
+            return null;
59
+        }
60 60
 
61
-		$shareAttributes = $this->createMock(IShareAttributes::class);
62
-		$shareAttributes->method('toArray')->willReturn($attrs);
63
-		$shareAttributes->method('getAttribute')->will(
64
-			$this->returnCallback(function ($scope, $key) use ($attrs) {
65
-				$result = null;
66
-				foreach ($attrs as $attr) {
67
-					if ($attr['key'] === $key && $attr['scope'] === $scope) {
68
-						$result = $attr['value'];
69
-					}
70
-				}
71
-				return $result;
72
-			})
73
-		);
74
-		return $shareAttributes;
75
-	}
61
+        $shareAttributes = $this->createMock(IShareAttributes::class);
62
+        $shareAttributes->method('toArray')->willReturn($attrs);
63
+        $shareAttributes->method('getAttribute')->will(
64
+            $this->returnCallback(function ($scope, $key) use ($attrs) {
65
+                $result = null;
66
+                foreach ($attrs as $attr) {
67
+                    if ($attr['key'] === $key && $attr['scope'] === $scope) {
68
+                        $result = $attr['value'];
69
+                    }
70
+                }
71
+                return $result;
72
+            })
73
+        );
74
+        return $shareAttributes;
75
+    }
76 76
 
77
-	private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31, $attributes = null) {
78
-		$share = $this->createMock(IShare::class);
79
-		$share->expects($this->any())
80
-			->method('getPermissions')
81
-			->willReturn($permissions);
82
-		$share->expects($this->any())
83
-			->method('getAttributes')
84
-			->will($this->returnValue($this->makeMockShareAttributes($attributes)));
85
-		$share->expects($this->any())
86
-			->method('getShareOwner')
87
-			->willReturn($owner);
88
-		$share->expects($this->any())
89
-			->method('getTarget')
90
-			->willReturn($target);
91
-		$share->expects($this->any())
92
-			->method('getId')
93
-			->willReturn($id);
94
-		$share->expects($this->any())
95
-			->method('getNodeId')
96
-			->willReturn($nodeId);
97
-		$share->expects($this->any())
98
-			->method('getShareTime')
99
-			->willReturn(
100
-				// compute share time based on id, simulating share order
101
-				new \DateTime('@' . (1469193980 + 1000 * $id))
102
-			);
103
-		return $share;
104
-	}
77
+    private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31, $attributes = null) {
78
+        $share = $this->createMock(IShare::class);
79
+        $share->expects($this->any())
80
+            ->method('getPermissions')
81
+            ->willReturn($permissions);
82
+        $share->expects($this->any())
83
+            ->method('getAttributes')
84
+            ->will($this->returnValue($this->makeMockShareAttributes($attributes)));
85
+        $share->expects($this->any())
86
+            ->method('getShareOwner')
87
+            ->willReturn($owner);
88
+        $share->expects($this->any())
89
+            ->method('getTarget')
90
+            ->willReturn($target);
91
+        $share->expects($this->any())
92
+            ->method('getId')
93
+            ->willReturn($id);
94
+        $share->expects($this->any())
95
+            ->method('getNodeId')
96
+            ->willReturn($nodeId);
97
+        $share->expects($this->any())
98
+            ->method('getShareTime')
99
+            ->willReturn(
100
+                // compute share time based on id, simulating share order
101
+                new \DateTime('@' . (1469193980 + 1000 * $id))
102
+            );
103
+        return $share;
104
+    }
105 105
 
106
-	/**
107
-	 * Tests excluding shares from the current view. This includes:
108
-	 * - shares that were opted out of (permissions === 0)
109
-	 * - shares with a group in which the owner is already in
110
-	 */
111
-	public function testExcludeShares(): void {
112
-		$rootFolder = $this->createMock(IRootFolder::class);
113
-		$userManager = $this->createMock(IUserManager::class);
114
-		$attr1 = [];
115
-		$attr2 = [['scope' => 'permission', 'key' => 'download', 'value' => true]];
116
-		$userShares = [
117
-			$this->makeMockShare(1, 100, 'user2', '/share2', 0, $attr1),
118
-			$this->makeMockShare(2, 100, 'user2', '/share2', 31, $attr2),
119
-		];
120
-		$groupShares = [
121
-			$this->makeMockShare(3, 100, 'user2', '/share2', 0, $attr1),
122
-			$this->makeMockShare(4, 101, 'user2', '/share4', 31, $attr2),
123
-			$this->makeMockShare(5, 100, 'user1', '/share4', 31, $attr2),
124
-		];
125
-		$roomShares = [
126
-			$this->makeMockShare(6, 102, 'user2', '/share6', 0),
127
-			$this->makeMockShare(7, 102, 'user1', '/share6', 31),
128
-			$this->makeMockShare(8, 102, 'user2', '/share6', 31),
129
-			$this->makeMockShare(9, 102, 'user2', '/share6', 31),
130
-		];
131
-		$deckShares = [
132
-			$this->makeMockShare(10, 103, 'user2', '/share7', 0),
133
-			$this->makeMockShare(11, 103, 'user1', '/share7', 31),
134
-			$this->makeMockShare(12, 103, 'user2', '/share7', 31),
135
-			$this->makeMockShare(13, 103, 'user2', '/share7', 31),
136
-		];
137
-		// tests regarding circles and sciencemesh are made in the apps themselves.
138
-		$circleShares = [];
139
-		$scienceMeshShares = [];
140
-		$this->user->expects($this->any())
141
-			->method('getUID')
142
-			->willReturn('user1');
143
-		$this->shareManager->expects($this->exactly(6))
144
-			->method('getSharedWith')
145
-			->willReturnMap([
146
-				['user1', IShare::TYPE_USER, null, -1, 0, $userShares],
147
-				['user1', IShare::TYPE_GROUP, null, -1, 0, $groupShares],
148
-				['user1', IShare::TYPE_CIRCLE, null, -1, 0, $circleShares],
149
-				['user1', IShare::TYPE_ROOM, null, -1, 0, $roomShares],
150
-				['user1', IShare::TYPE_DECK, null, -1, 0, $deckShares],
151
-				['user1', IShare::TYPE_SCIENCEMESH, null, -1, 0, $scienceMeshShares],
152
-			]);
106
+    /**
107
+     * Tests excluding shares from the current view. This includes:
108
+     * - shares that were opted out of (permissions === 0)
109
+     * - shares with a group in which the owner is already in
110
+     */
111
+    public function testExcludeShares(): void {
112
+        $rootFolder = $this->createMock(IRootFolder::class);
113
+        $userManager = $this->createMock(IUserManager::class);
114
+        $attr1 = [];
115
+        $attr2 = [['scope' => 'permission', 'key' => 'download', 'value' => true]];
116
+        $userShares = [
117
+            $this->makeMockShare(1, 100, 'user2', '/share2', 0, $attr1),
118
+            $this->makeMockShare(2, 100, 'user2', '/share2', 31, $attr2),
119
+        ];
120
+        $groupShares = [
121
+            $this->makeMockShare(3, 100, 'user2', '/share2', 0, $attr1),
122
+            $this->makeMockShare(4, 101, 'user2', '/share4', 31, $attr2),
123
+            $this->makeMockShare(5, 100, 'user1', '/share4', 31, $attr2),
124
+        ];
125
+        $roomShares = [
126
+            $this->makeMockShare(6, 102, 'user2', '/share6', 0),
127
+            $this->makeMockShare(7, 102, 'user1', '/share6', 31),
128
+            $this->makeMockShare(8, 102, 'user2', '/share6', 31),
129
+            $this->makeMockShare(9, 102, 'user2', '/share6', 31),
130
+        ];
131
+        $deckShares = [
132
+            $this->makeMockShare(10, 103, 'user2', '/share7', 0),
133
+            $this->makeMockShare(11, 103, 'user1', '/share7', 31),
134
+            $this->makeMockShare(12, 103, 'user2', '/share7', 31),
135
+            $this->makeMockShare(13, 103, 'user2', '/share7', 31),
136
+        ];
137
+        // tests regarding circles and sciencemesh are made in the apps themselves.
138
+        $circleShares = [];
139
+        $scienceMeshShares = [];
140
+        $this->user->expects($this->any())
141
+            ->method('getUID')
142
+            ->willReturn('user1');
143
+        $this->shareManager->expects($this->exactly(6))
144
+            ->method('getSharedWith')
145
+            ->willReturnMap([
146
+                ['user1', IShare::TYPE_USER, null, -1, 0, $userShares],
147
+                ['user1', IShare::TYPE_GROUP, null, -1, 0, $groupShares],
148
+                ['user1', IShare::TYPE_CIRCLE, null, -1, 0, $circleShares],
149
+                ['user1', IShare::TYPE_ROOM, null, -1, 0, $roomShares],
150
+                ['user1', IShare::TYPE_DECK, null, -1, 0, $deckShares],
151
+                ['user1', IShare::TYPE_SCIENCEMESH, null, -1, 0, $scienceMeshShares],
152
+            ]);
153 153
 
154
-		$this->shareManager->expects($this->any())
155
-			->method('newShare')
156
-			->willReturnCallback(function () use ($rootFolder, $userManager) {
157
-				return new Share($rootFolder, $userManager);
158
-			});
154
+        $this->shareManager->expects($this->any())
155
+            ->method('newShare')
156
+            ->willReturnCallback(function () use ($rootFolder, $userManager) {
157
+                return new Share($rootFolder, $userManager);
158
+            });
159 159
 
160
-		$mounts = $this->provider->getMountsForUser($this->user, $this->loader);
161
-		$this->assertCount(4, $mounts);
162
-		$this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[0]);
163
-		$this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[1]);
164
-		$this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[2]);
165
-		$this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[3]);
166
-		/** @var OCA\Files_Sharing\SharedMount[] $mounts */
167
-		$mountedShare1 = $mounts[0]->getShare();
168
-		$this->assertEquals('2', $mountedShare1->getId());
169
-		$this->assertEquals('user2', $mountedShare1->getShareOwner());
170
-		$this->assertEquals(100, $mountedShare1->getNodeId());
171
-		$this->assertEquals('/share2', $mountedShare1->getTarget());
172
-		$this->assertEquals(31, $mountedShare1->getPermissions());
173
-		$this->assertEquals(true, $mountedShare1->getAttributes()->getAttribute('permission', 'download'));
174
-		$mountedShare2 = $mounts[1]->getShare();
175
-		$this->assertEquals('4', $mountedShare2->getId());
176
-		$this->assertEquals('user2', $mountedShare2->getShareOwner());
177
-		$this->assertEquals(101, $mountedShare2->getNodeId());
178
-		$this->assertEquals('/share4', $mountedShare2->getTarget());
179
-		$this->assertEquals(31, $mountedShare2->getPermissions());
180
-		$this->assertEquals(true, $mountedShare2->getAttributes()->getAttribute('permission', 'download'));
181
-		$mountedShare3 = $mounts[2]->getShare();
182
-		$this->assertEquals('8', $mountedShare3->getId());
183
-		$this->assertEquals('user2', $mountedShare3->getShareOwner());
184
-		$this->assertEquals(102, $mountedShare3->getNodeId());
185
-		$this->assertEquals('/share6', $mountedShare3->getTarget());
186
-		$this->assertEquals(31, $mountedShare3->getPermissions());
187
-		$mountedShare4 = $mounts[3]->getShare();
188
-		$this->assertEquals('12', $mountedShare4->getId());
189
-		$this->assertEquals('user2', $mountedShare4->getShareOwner());
190
-		$this->assertEquals(103, $mountedShare4->getNodeId());
191
-		$this->assertEquals('/share7', $mountedShare4->getTarget());
192
-		$this->assertEquals(31, $mountedShare4->getPermissions());
193
-	}
160
+        $mounts = $this->provider->getMountsForUser($this->user, $this->loader);
161
+        $this->assertCount(4, $mounts);
162
+        $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[0]);
163
+        $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[1]);
164
+        $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[2]);
165
+        $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[3]);
166
+        /** @var OCA\Files_Sharing\SharedMount[] $mounts */
167
+        $mountedShare1 = $mounts[0]->getShare();
168
+        $this->assertEquals('2', $mountedShare1->getId());
169
+        $this->assertEquals('user2', $mountedShare1->getShareOwner());
170
+        $this->assertEquals(100, $mountedShare1->getNodeId());
171
+        $this->assertEquals('/share2', $mountedShare1->getTarget());
172
+        $this->assertEquals(31, $mountedShare1->getPermissions());
173
+        $this->assertEquals(true, $mountedShare1->getAttributes()->getAttribute('permission', 'download'));
174
+        $mountedShare2 = $mounts[1]->getShare();
175
+        $this->assertEquals('4', $mountedShare2->getId());
176
+        $this->assertEquals('user2', $mountedShare2->getShareOwner());
177
+        $this->assertEquals(101, $mountedShare2->getNodeId());
178
+        $this->assertEquals('/share4', $mountedShare2->getTarget());
179
+        $this->assertEquals(31, $mountedShare2->getPermissions());
180
+        $this->assertEquals(true, $mountedShare2->getAttributes()->getAttribute('permission', 'download'));
181
+        $mountedShare3 = $mounts[2]->getShare();
182
+        $this->assertEquals('8', $mountedShare3->getId());
183
+        $this->assertEquals('user2', $mountedShare3->getShareOwner());
184
+        $this->assertEquals(102, $mountedShare3->getNodeId());
185
+        $this->assertEquals('/share6', $mountedShare3->getTarget());
186
+        $this->assertEquals(31, $mountedShare3->getPermissions());
187
+        $mountedShare4 = $mounts[3]->getShare();
188
+        $this->assertEquals('12', $mountedShare4->getId());
189
+        $this->assertEquals('user2', $mountedShare4->getShareOwner());
190
+        $this->assertEquals(103, $mountedShare4->getNodeId());
191
+        $this->assertEquals('/share7', $mountedShare4->getTarget());
192
+        $this->assertEquals(31, $mountedShare4->getPermissions());
193
+    }
194 194
 
195
-	public static function mergeSharesDataProvider(): array {
196
-		// note: the user in the specs here is the shareOwner not recipient
197
-		// the recipient is always "user1"
198
-		return [
199
-			// #0: share as outsider with "group1" and "user1" with same permissions
200
-			[
201
-				[
202
-					[1, 100, 'user2', '/share2', 31, null],
203
-				],
204
-				[
205
-					[2, 100, 'user2', '/share2', 31, null],
206
-				],
207
-				[
208
-					// combined, user share has higher priority
209
-					['1', 100, 'user2', '/share2', 31, []],
210
-				],
211
-			],
212
-			// #1: share as outsider with "group1" and "user1" with different permissions
213
-			[
214
-				[
215
-					[1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true], ['scope' => 'app', 'key' => 'attribute1', 'value' => true]]],
216
-				],
217
-				[
218
-					[2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => false], ['scope' => 'app', 'key' => 'attribute2', 'value' => false]]],
219
-				],
220
-				[
221
-					// use highest permissions
222
-					['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true], ['scope' => 'app', 'key' => 'attribute1', 'value' => true], ['scope' => 'app', 'key' => 'attribute2', 'value' => false]]],
223
-				],
224
-			],
225
-			// #2: share as outsider with "group1" and "group2" with same permissions
226
-			[
227
-				[
228
-				],
229
-				[
230
-					[1, 100, 'user2', '/share', 31, null],
231
-					[2, 100, 'user2', '/share', 31, []],
232
-				],
233
-				[
234
-					// combined, first group share has higher priority
235
-					['1', 100, 'user2', '/share', 31, []],
236
-				],
237
-			],
238
-			// #3: share as outsider with "group1" and "group2" with different permissions
239
-			[
240
-				[
241
-				],
242
-				[
243
-					[1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => false]]],
244
-					[2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => true]]],
245
-				],
246
-				[
247
-					// use higher permissions (most permissive)
248
-					['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true]]],
249
-				],
250
-			],
251
-			// #4: share as insider with "group1"
252
-			[
253
-				[
254
-				],
255
-				[
256
-					[1, 100, 'user1', '/share', 31, []],
257
-				],
258
-				[
259
-					// no received share since "user1" is the sharer/owner
260
-				],
261
-			],
262
-			// #5: share as insider with "group1" and "group2" with different permissions
263
-			[
264
-				[
265
-				],
266
-				[
267
-					[1, 100, 'user1', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true]]],
268
-					[2, 100, 'user1', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => false]]],
269
-				],
270
-				[
271
-					// no received share since "user1" is the sharer/owner
272
-				],
273
-			],
274
-			// #6: share as outside with "group1", recipient opted out
275
-			[
276
-				[
277
-				],
278
-				[
279
-					[1, 100, 'user2', '/share', 0, []],
280
-				],
281
-				[
282
-					// no received share since "user1" opted out
283
-				],
284
-			],
285
-			// #7: share as outsider with "group1" and "user1" where recipient renamed in between
286
-			[
287
-				[
288
-					[1, 100, 'user2', '/share2-renamed', 31, []],
289
-				],
290
-				[
291
-					[2, 100, 'user2', '/share2', 31, []],
292
-				],
293
-				[
294
-					// use target of least recent share
295
-					['1', 100, 'user2', '/share2-renamed', 31, []],
296
-				],
297
-			],
298
-			// #8: share as outsider with "group1" and "user1" where recipient renamed in between
299
-			[
300
-				[
301
-					[2, 100, 'user2', '/share2', 31, []],
302
-				],
303
-				[
304
-					[1, 100, 'user2', '/share2-renamed', 31, []],
305
-				],
306
-				[
307
-					// use target of least recent share
308
-					['1', 100, 'user2', '/share2-renamed', 31, []],
309
-				],
310
-			],
311
-			// #9: share as outsider with "nullgroup" and "user1" where recipient renamed in between
312
-			[
313
-				[
314
-					[2, 100, 'user2', '/share2', 31, []],
315
-				],
316
-				[
317
-					[1, 100, 'nullgroup', '/share2-renamed', 31, []],
318
-				],
319
-				[
320
-					// use target of least recent share
321
-					['1', 100, 'nullgroup', '/share2-renamed', 31, []],
322
-				],
323
-				true
324
-			],
325
-		];
326
-	}
195
+    public static function mergeSharesDataProvider(): array {
196
+        // note: the user in the specs here is the shareOwner not recipient
197
+        // the recipient is always "user1"
198
+        return [
199
+            // #0: share as outsider with "group1" and "user1" with same permissions
200
+            [
201
+                [
202
+                    [1, 100, 'user2', '/share2', 31, null],
203
+                ],
204
+                [
205
+                    [2, 100, 'user2', '/share2', 31, null],
206
+                ],
207
+                [
208
+                    // combined, user share has higher priority
209
+                    ['1', 100, 'user2', '/share2', 31, []],
210
+                ],
211
+            ],
212
+            // #1: share as outsider with "group1" and "user1" with different permissions
213
+            [
214
+                [
215
+                    [1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true], ['scope' => 'app', 'key' => 'attribute1', 'value' => true]]],
216
+                ],
217
+                [
218
+                    [2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => false], ['scope' => 'app', 'key' => 'attribute2', 'value' => false]]],
219
+                ],
220
+                [
221
+                    // use highest permissions
222
+                    ['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true], ['scope' => 'app', 'key' => 'attribute1', 'value' => true], ['scope' => 'app', 'key' => 'attribute2', 'value' => false]]],
223
+                ],
224
+            ],
225
+            // #2: share as outsider with "group1" and "group2" with same permissions
226
+            [
227
+                [
228
+                ],
229
+                [
230
+                    [1, 100, 'user2', '/share', 31, null],
231
+                    [2, 100, 'user2', '/share', 31, []],
232
+                ],
233
+                [
234
+                    // combined, first group share has higher priority
235
+                    ['1', 100, 'user2', '/share', 31, []],
236
+                ],
237
+            ],
238
+            // #3: share as outsider with "group1" and "group2" with different permissions
239
+            [
240
+                [
241
+                ],
242
+                [
243
+                    [1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => false]]],
244
+                    [2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => true]]],
245
+                ],
246
+                [
247
+                    // use higher permissions (most permissive)
248
+                    ['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true]]],
249
+                ],
250
+            ],
251
+            // #4: share as insider with "group1"
252
+            [
253
+                [
254
+                ],
255
+                [
256
+                    [1, 100, 'user1', '/share', 31, []],
257
+                ],
258
+                [
259
+                    // no received share since "user1" is the sharer/owner
260
+                ],
261
+            ],
262
+            // #5: share as insider with "group1" and "group2" with different permissions
263
+            [
264
+                [
265
+                ],
266
+                [
267
+                    [1, 100, 'user1', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true]]],
268
+                    [2, 100, 'user1', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => false]]],
269
+                ],
270
+                [
271
+                    // no received share since "user1" is the sharer/owner
272
+                ],
273
+            ],
274
+            // #6: share as outside with "group1", recipient opted out
275
+            [
276
+                [
277
+                ],
278
+                [
279
+                    [1, 100, 'user2', '/share', 0, []],
280
+                ],
281
+                [
282
+                    // no received share since "user1" opted out
283
+                ],
284
+            ],
285
+            // #7: share as outsider with "group1" and "user1" where recipient renamed in between
286
+            [
287
+                [
288
+                    [1, 100, 'user2', '/share2-renamed', 31, []],
289
+                ],
290
+                [
291
+                    [2, 100, 'user2', '/share2', 31, []],
292
+                ],
293
+                [
294
+                    // use target of least recent share
295
+                    ['1', 100, 'user2', '/share2-renamed', 31, []],
296
+                ],
297
+            ],
298
+            // #8: share as outsider with "group1" and "user1" where recipient renamed in between
299
+            [
300
+                [
301
+                    [2, 100, 'user2', '/share2', 31, []],
302
+                ],
303
+                [
304
+                    [1, 100, 'user2', '/share2-renamed', 31, []],
305
+                ],
306
+                [
307
+                    // use target of least recent share
308
+                    ['1', 100, 'user2', '/share2-renamed', 31, []],
309
+                ],
310
+            ],
311
+            // #9: share as outsider with "nullgroup" and "user1" where recipient renamed in between
312
+            [
313
+                [
314
+                    [2, 100, 'user2', '/share2', 31, []],
315
+                ],
316
+                [
317
+                    [1, 100, 'nullgroup', '/share2-renamed', 31, []],
318
+                ],
319
+                [
320
+                    // use target of least recent share
321
+                    ['1', 100, 'nullgroup', '/share2-renamed', 31, []],
322
+                ],
323
+                true
324
+            ],
325
+        ];
326
+    }
327 327
 
328
-	/**
329
-	 * Tests merging shares.
330
-	 *
331
-	 * Happens when sharing the same entry to a user through multiple ways,
332
-	 * like several groups and also direct shares at the same time.
333
-	 *
334
-	 * @dataProvider mergeSharesDataProvider
335
-	 *
336
-	 * @param array $userShares array of user share specs
337
-	 * @param array $groupShares array of group share specs
338
-	 * @param array $expectedShares array of expected supershare specs
339
-	 */
340
-	public function testMergeShares($userShares, $groupShares, $expectedShares, $moveFails = false): void {
341
-		$rootFolder = $this->createMock(IRootFolder::class);
342
-		$userManager = $this->createMock(IUserManager::class);
328
+    /**
329
+     * Tests merging shares.
330
+     *
331
+     * Happens when sharing the same entry to a user through multiple ways,
332
+     * like several groups and also direct shares at the same time.
333
+     *
334
+     * @dataProvider mergeSharesDataProvider
335
+     *
336
+     * @param array $userShares array of user share specs
337
+     * @param array $groupShares array of group share specs
338
+     * @param array $expectedShares array of expected supershare specs
339
+     */
340
+    public function testMergeShares($userShares, $groupShares, $expectedShares, $moveFails = false): void {
341
+        $rootFolder = $this->createMock(IRootFolder::class);
342
+        $userManager = $this->createMock(IUserManager::class);
343 343
 
344
-		$userShares = array_map(function ($shareSpec) {
345
-			return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
346
-		}, $userShares);
347
-		$groupShares = array_map(function ($shareSpec) {
348
-			return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
349
-		}, $groupShares);
344
+        $userShares = array_map(function ($shareSpec) {
345
+            return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
346
+        }, $userShares);
347
+        $groupShares = array_map(function ($shareSpec) {
348
+            return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
349
+        }, $groupShares);
350 350
 
351
-		$this->user->expects($this->any())
352
-			->method('getUID')
353
-			->willReturn('user1');
351
+        $this->user->expects($this->any())
352
+            ->method('getUID')
353
+            ->willReturn('user1');
354 354
 
355
-		// tests regarding circles are made in the app itself.
356
-		$circleShares = [];
357
-		$roomShares = [];
358
-		$deckShares = [];
359
-		$scienceMeshShares = [];
360
-		$this->shareManager->expects($this->exactly(6))
361
-			->method('getSharedWith')
362
-			->willReturnMap([
363
-				['user1', IShare::TYPE_USER, null, -1, 0, $userShares],
364
-				['user1', IShare::TYPE_GROUP, null, -1, 0, $groupShares],
365
-				['user1', IShare::TYPE_CIRCLE, null, -1, 0, $circleShares],
366
-				['user1', IShare::TYPE_ROOM, null, -1, 0, $roomShares],
367
-				['user1', IShare::TYPE_DECK, null, -1, 0, $deckShares],
368
-				['user1', IShare::TYPE_SCIENCEMESH, null, -1, 0, $scienceMeshShares],
369
-			]);
355
+        // tests regarding circles are made in the app itself.
356
+        $circleShares = [];
357
+        $roomShares = [];
358
+        $deckShares = [];
359
+        $scienceMeshShares = [];
360
+        $this->shareManager->expects($this->exactly(6))
361
+            ->method('getSharedWith')
362
+            ->willReturnMap([
363
+                ['user1', IShare::TYPE_USER, null, -1, 0, $userShares],
364
+                ['user1', IShare::TYPE_GROUP, null, -1, 0, $groupShares],
365
+                ['user1', IShare::TYPE_CIRCLE, null, -1, 0, $circleShares],
366
+                ['user1', IShare::TYPE_ROOM, null, -1, 0, $roomShares],
367
+                ['user1', IShare::TYPE_DECK, null, -1, 0, $deckShares],
368
+                ['user1', IShare::TYPE_SCIENCEMESH, null, -1, 0, $scienceMeshShares],
369
+            ]);
370 370
 
371
-		$this->shareManager->expects($this->any())
372
-			->method('newShare')
373
-			->willReturnCallback(function () use ($rootFolder, $userManager) {
374
-				return new Share($rootFolder, $userManager);
375
-			});
371
+        $this->shareManager->expects($this->any())
372
+            ->method('newShare')
373
+            ->willReturnCallback(function () use ($rootFolder, $userManager) {
374
+                return new Share($rootFolder, $userManager);
375
+            });
376 376
 
377
-		if ($moveFails) {
378
-			$this->shareManager->expects($this->any())
379
-				->method('moveShare')
380
-				->will($this->throwException(new \InvalidArgumentException()));
381
-		}
377
+        if ($moveFails) {
378
+            $this->shareManager->expects($this->any())
379
+                ->method('moveShare')
380
+                ->will($this->throwException(new \InvalidArgumentException()));
381
+        }
382 382
 
383
-		$mounts = $this->provider->getMountsForUser($this->user, $this->loader);
383
+        $mounts = $this->provider->getMountsForUser($this->user, $this->loader);
384 384
 
385
-		$this->assertCount(count($expectedShares), $mounts);
385
+        $this->assertCount(count($expectedShares), $mounts);
386 386
 
387
-		foreach ($mounts as $index => $mount) {
388
-			$expectedShare = $expectedShares[$index];
389
-			$this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mount);
387
+        foreach ($mounts as $index => $mount) {
388
+            $expectedShare = $expectedShares[$index];
389
+            $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mount);
390 390
 
391
-			// supershare
392
-			/** @var OCA\Files_Sharing\SharedMount $mount */
393
-			$share = $mount->getShare();
391
+            // supershare
392
+            /** @var OCA\Files_Sharing\SharedMount $mount */
393
+            $share = $mount->getShare();
394 394
 
395
-			$this->assertEquals($expectedShare[0], $share->getId());
396
-			$this->assertEquals($expectedShare[1], $share->getNodeId());
397
-			$this->assertEquals($expectedShare[2], $share->getShareOwner());
398
-			$this->assertEquals($expectedShare[3], $share->getTarget());
399
-			$this->assertEquals($expectedShare[4], $share->getPermissions());
400
-			if ($expectedShare[5] === null) {
401
-				$this->assertNull($share->getAttributes());
402
-			} else {
403
-				$this->assertEquals($expectedShare[5], $share->getAttributes()->toArray());
404
-			}
405
-		}
406
-	}
395
+            $this->assertEquals($expectedShare[0], $share->getId());
396
+            $this->assertEquals($expectedShare[1], $share->getNodeId());
397
+            $this->assertEquals($expectedShare[2], $share->getShareOwner());
398
+            $this->assertEquals($expectedShare[3], $share->getTarget());
399
+            $this->assertEquals($expectedShare[4], $share->getPermissions());
400
+            if ($expectedShare[5] === null) {
401
+                $this->assertNull($share->getAttributes());
402
+            } else {
403
+                $this->assertEquals($expectedShare[5], $share->getAttributes()->toArray());
404
+            }
405
+        }
406
+    }
407 407
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 		$shareAttributes = $this->createMock(IShareAttributes::class);
62 62
 		$shareAttributes->method('toArray')->willReturn($attrs);
63 63
 		$shareAttributes->method('getAttribute')->will(
64
-			$this->returnCallback(function ($scope, $key) use ($attrs) {
64
+			$this->returnCallback(function($scope, $key) use ($attrs) {
65 65
 				$result = null;
66 66
 				foreach ($attrs as $attr) {
67 67
 					if ($attr['key'] === $key && $attr['scope'] === $scope) {
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
 			->method('getShareTime')
99 99
 			->willReturn(
100 100
 				// compute share time based on id, simulating share order
101
-				new \DateTime('@' . (1469193980 + 1000 * $id))
101
+				new \DateTime('@'.(1469193980 + 1000 * $id))
102 102
 			);
103 103
 		return $share;
104 104
 	}
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 
154 154
 		$this->shareManager->expects($this->any())
155 155
 			->method('newShare')
156
-			->willReturnCallback(function () use ($rootFolder, $userManager) {
156
+			->willReturnCallback(function() use ($rootFolder, $userManager) {
157 157
 				return new Share($rootFolder, $userManager);
158 158
 			});
159 159
 
@@ -341,10 +341,10 @@  discard block
 block discarded – undo
341 341
 		$rootFolder = $this->createMock(IRootFolder::class);
342 342
 		$userManager = $this->createMock(IUserManager::class);
343 343
 
344
-		$userShares = array_map(function ($shareSpec) {
344
+		$userShares = array_map(function($shareSpec) {
345 345
 			return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
346 346
 		}, $userShares);
347
-		$groupShares = array_map(function ($shareSpec) {
347
+		$groupShares = array_map(function($shareSpec) {
348 348
 			return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
349 349
 		}, $groupShares);
350 350
 
@@ -370,7 +370,7 @@  discard block
 block discarded – undo
370 370
 
371 371
 		$this->shareManager->expects($this->any())
372 372
 			->method('newShare')
373
-			->willReturnCallback(function () use ($rootFolder, $userManager) {
373
+			->willReturnCallback(function() use ($rootFolder, $userManager) {
374 374
 				return new Share($rootFolder, $userManager);
375 375
 			});
376 376
 
Please login to merge, or discard this patch.
apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php 2 patches
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -25,180 +25,180 @@
 block discarded – undo
25 25
  */
26 26
 class CleanupRemoteStoragesTest extends TestCase {
27 27
 
28
-	protected IDBConnection $connection;
29
-	protected CleanupRemoteStorages $command;
30
-	private ICloudIdManager&MockObject $cloudIdManager;
31
-
32
-	private $storages = [
33
-		['id' => 'shared::7b4a322b22f9d0047c38d77d471ce3cf', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e1', 'remote' => 'https://hostname.tld/owncloud1', 'user' => 'user1'],
34
-		['id' => 'shared::efe3b456112c3780da6155d3a9b9141c', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e2', 'remote' => 'https://hostname.tld/owncloud2', 'user' => 'user2'],
35
-		['notExistingId' => 'shared::33323d9f4ca416a9e3525b435354bc6f', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e3', 'remote' => 'https://hostname.tld/owncloud3', 'user' => 'user3'],
36
-		['id' => 'shared::7fe41a07d3f517a923f4b2b599e72cbb', 'files_count' => 2],
37
-		['id' => 'shared::de4aeb2f378d222b6d2c5fd8f4e42f8e', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e5', 'remote' => 'https://hostname.tld/owncloud5', 'user' => 'user5'],
38
-		['id' => 'shared::af712293ab5eb9e6a1745a13818b99fe', 'files_count' => 3],
39
-		['notExistingId' => 'shared::c34568c143cdac7d2f06e0800b5280f9', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e7', 'remote' => 'https://hostname.tld/owncloud7', 'user' => 'user7'],
40
-	];
41
-
42
-	protected function setUp(): void {
43
-		parent::setUp();
44
-
45
-		$this->connection = Server::get(IDBConnection::class);
46
-
47
-		$storageQuery = Server::get(IDBConnection::class)->getQueryBuilder();
48
-		$storageQuery->insert('storages')
49
-			->setValue('id', $storageQuery->createParameter('id'));
50
-
51
-		$shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder();
52
-		$shareExternalQuery->insert('share_external')
53
-			->setValue('share_token', $shareExternalQuery->createParameter('share_token'))
54
-			->setValue('remote', $shareExternalQuery->createParameter('remote'))
55
-			->setValue('name', $shareExternalQuery->createParameter('name'))
56
-			->setValue('owner', $shareExternalQuery->createParameter('owner'))
57
-			->setValue('user', $shareExternalQuery->createParameter('user'))
58
-			->setValue('mountpoint', $shareExternalQuery->createParameter('mountpoint'))
59
-			->setValue('mountpoint_hash', $shareExternalQuery->createParameter('mountpoint_hash'));
60
-
61
-		$filesQuery = Server::get(IDBConnection::class)->getQueryBuilder();
62
-		$filesQuery->insert('filecache')
63
-			->setValue('storage', $filesQuery->createParameter('storage'))
64
-			->setValue('path', $filesQuery->createParameter('path'))
65
-			->setValue('path_hash', $filesQuery->createParameter('path_hash'));
66
-
67
-		foreach ($this->storages as &$storage) {
68
-			if (isset($storage['id'])) {
69
-				$storageQuery->setParameter('id', $storage['id']);
70
-				$storageQuery->executeStatement();
71
-				$storage['numeric_id'] = $storageQuery->getLastInsertId();
72
-			}
73
-
74
-			if (isset($storage['share_token'])) {
75
-				$shareExternalQuery
76
-					->setParameter('share_token', $storage['share_token'])
77
-					->setParameter('remote', $storage['remote'])
78
-					->setParameter('name', 'irrelevant')
79
-					->setParameter('owner', 'irrelevant')
80
-					->setParameter('user', $storage['user'])
81
-					->setParameter('mountpoint', 'irrelevant')
82
-					->setParameter('mountpoint_hash', 'irrelevant');
83
-				$shareExternalQuery->executeStatement();
84
-			}
85
-
86
-			if (isset($storage['files_count'])) {
87
-				for ($i = 0; $i < $storage['files_count']; $i++) {
88
-					$filesQuery->setParameter('storage', $storage['numeric_id']);
89
-					$filesQuery->setParameter('path', 'file' . $i);
90
-					$filesQuery->setParameter('path_hash', md5('file' . $i));
91
-					$filesQuery->executeStatement();
92
-				}
93
-			}
94
-		}
95
-
96
-		$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
97
-
98
-		$this->command = new CleanupRemoteStorages($this->connection, $this->cloudIdManager);
99
-	}
100
-
101
-	protected function tearDown(): void {
102
-		$storageQuery = Server::get(IDBConnection::class)->getQueryBuilder();
103
-		$storageQuery->delete('storages')
104
-			->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id')));
105
-
106
-		$shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder();
107
-		$shareExternalQuery->delete('share_external')
108
-			->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token')))
109
-			->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote')));
110
-
111
-		foreach ($this->storages as $storage) {
112
-			if (isset($storage['id'])) {
113
-				$storageQuery->setParameter('id', $storage['id']);
114
-				$storageQuery->executeStatement();
115
-			}
116
-
117
-			if (isset($storage['share_token'])) {
118
-				$shareExternalQuery->setParameter('share_token', $storage['share_token']);
119
-				$shareExternalQuery->setParameter('remote', $storage['remote']);
120
-				$shareExternalQuery->executeStatement();
121
-			}
122
-		}
123
-
124
-		parent::tearDown();
125
-	}
126
-
127
-	private function doesStorageExist($numericId) {
128
-		$qb = Server::get(IDBConnection::class)->getQueryBuilder();
129
-		$qb->select('*')
130
-			->from('storages')
131
-			->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId)));
132
-
133
-		$qResult = $qb->executeQuery();
134
-		$result = $qResult->fetch();
135
-		$qResult->closeCursor();
136
-		if (!empty($result)) {
137
-			return true;
138
-		}
139
-
140
-		$qb = Server::get(IDBConnection::class)->getQueryBuilder();
141
-		$qb->select('*')
142
-			->from('filecache')
143
-			->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId)));
144
-
145
-		$qResult = $qb->executeQuery();
146
-		$result = $qResult->fetch();
147
-		$qResult->closeCursor();
148
-		if (!empty($result)) {
149
-			return true;
150
-		}
151
-
152
-		return false;
153
-	}
154
-
155
-	/**
156
-	 * Test cleanup of orphaned storages
157
-	 */
158
-	public function testCleanup(): void {
159
-		$input = $this->getMockBuilder(InputInterface::class)
160
-			->disableOriginalConstructor()
161
-			->getMock();
162
-		$output = $this->getMockBuilder(OutputInterface::class)
163
-			->disableOriginalConstructor()
164
-			->getMock();
165
-
166
-		// parent folder, `files`, ´test` and `welcome.txt` => 4 elements
167
-		$outputCalls = [];
168
-		$output
169
-			->expects($this->any())
170
-			->method('writeln')
171
-			->willReturnCallback(function (string $text) use (&$outputCalls) {
172
-				$outputCalls[] = $text;
173
-			});
174
-
175
-		$this->cloudIdManager
176
-			->expects($this->any())
177
-			->method('getCloudId')
178
-			->will($this->returnCallback(function (string $user, string $remote) {
179
-				$cloudIdMock = $this->createMock(ICloudId::class);
180
-
181
-				// The remotes are already sanitized in the original data, so
182
-				// they can be directly returned.
183
-				$cloudIdMock
184
-					->expects($this->any())
185
-					->method('getRemote')
186
-					->willReturn($remote);
187
-
188
-				return $cloudIdMock;
189
-			}));
190
-
191
-		$this->command->execute($input, $output);
192
-
193
-		$this->assertTrue($this->doesStorageExist($this->storages[0]['numeric_id']));
194
-		$this->assertTrue($this->doesStorageExist($this->storages[1]['numeric_id']));
195
-		$this->assertFalse($this->doesStorageExist($this->storages[3]['numeric_id']));
196
-		$this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id']));
197
-		$this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id']));
198
-
199
-		$this->assertEquals([
200
-			'5 remote storage(s) need(s) to be checked',
201
-			'5 remote share(s) exist',
202
-		], array_slice($outputCalls, 0, 2));
203
-	}
28
+    protected IDBConnection $connection;
29
+    protected CleanupRemoteStorages $command;
30
+    private ICloudIdManager&MockObject $cloudIdManager;
31
+
32
+    private $storages = [
33
+        ['id' => 'shared::7b4a322b22f9d0047c38d77d471ce3cf', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e1', 'remote' => 'https://hostname.tld/owncloud1', 'user' => 'user1'],
34
+        ['id' => 'shared::efe3b456112c3780da6155d3a9b9141c', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e2', 'remote' => 'https://hostname.tld/owncloud2', 'user' => 'user2'],
35
+        ['notExistingId' => 'shared::33323d9f4ca416a9e3525b435354bc6f', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e3', 'remote' => 'https://hostname.tld/owncloud3', 'user' => 'user3'],
36
+        ['id' => 'shared::7fe41a07d3f517a923f4b2b599e72cbb', 'files_count' => 2],
37
+        ['id' => 'shared::de4aeb2f378d222b6d2c5fd8f4e42f8e', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e5', 'remote' => 'https://hostname.tld/owncloud5', 'user' => 'user5'],
38
+        ['id' => 'shared::af712293ab5eb9e6a1745a13818b99fe', 'files_count' => 3],
39
+        ['notExistingId' => 'shared::c34568c143cdac7d2f06e0800b5280f9', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e7', 'remote' => 'https://hostname.tld/owncloud7', 'user' => 'user7'],
40
+    ];
41
+
42
+    protected function setUp(): void {
43
+        parent::setUp();
44
+
45
+        $this->connection = Server::get(IDBConnection::class);
46
+
47
+        $storageQuery = Server::get(IDBConnection::class)->getQueryBuilder();
48
+        $storageQuery->insert('storages')
49
+            ->setValue('id', $storageQuery->createParameter('id'));
50
+
51
+        $shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder();
52
+        $shareExternalQuery->insert('share_external')
53
+            ->setValue('share_token', $shareExternalQuery->createParameter('share_token'))
54
+            ->setValue('remote', $shareExternalQuery->createParameter('remote'))
55
+            ->setValue('name', $shareExternalQuery->createParameter('name'))
56
+            ->setValue('owner', $shareExternalQuery->createParameter('owner'))
57
+            ->setValue('user', $shareExternalQuery->createParameter('user'))
58
+            ->setValue('mountpoint', $shareExternalQuery->createParameter('mountpoint'))
59
+            ->setValue('mountpoint_hash', $shareExternalQuery->createParameter('mountpoint_hash'));
60
+
61
+        $filesQuery = Server::get(IDBConnection::class)->getQueryBuilder();
62
+        $filesQuery->insert('filecache')
63
+            ->setValue('storage', $filesQuery->createParameter('storage'))
64
+            ->setValue('path', $filesQuery->createParameter('path'))
65
+            ->setValue('path_hash', $filesQuery->createParameter('path_hash'));
66
+
67
+        foreach ($this->storages as &$storage) {
68
+            if (isset($storage['id'])) {
69
+                $storageQuery->setParameter('id', $storage['id']);
70
+                $storageQuery->executeStatement();
71
+                $storage['numeric_id'] = $storageQuery->getLastInsertId();
72
+            }
73
+
74
+            if (isset($storage['share_token'])) {
75
+                $shareExternalQuery
76
+                    ->setParameter('share_token', $storage['share_token'])
77
+                    ->setParameter('remote', $storage['remote'])
78
+                    ->setParameter('name', 'irrelevant')
79
+                    ->setParameter('owner', 'irrelevant')
80
+                    ->setParameter('user', $storage['user'])
81
+                    ->setParameter('mountpoint', 'irrelevant')
82
+                    ->setParameter('mountpoint_hash', 'irrelevant');
83
+                $shareExternalQuery->executeStatement();
84
+            }
85
+
86
+            if (isset($storage['files_count'])) {
87
+                for ($i = 0; $i < $storage['files_count']; $i++) {
88
+                    $filesQuery->setParameter('storage', $storage['numeric_id']);
89
+                    $filesQuery->setParameter('path', 'file' . $i);
90
+                    $filesQuery->setParameter('path_hash', md5('file' . $i));
91
+                    $filesQuery->executeStatement();
92
+                }
93
+            }
94
+        }
95
+
96
+        $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
97
+
98
+        $this->command = new CleanupRemoteStorages($this->connection, $this->cloudIdManager);
99
+    }
100
+
101
+    protected function tearDown(): void {
102
+        $storageQuery = Server::get(IDBConnection::class)->getQueryBuilder();
103
+        $storageQuery->delete('storages')
104
+            ->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id')));
105
+
106
+        $shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder();
107
+        $shareExternalQuery->delete('share_external')
108
+            ->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token')))
109
+            ->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote')));
110
+
111
+        foreach ($this->storages as $storage) {
112
+            if (isset($storage['id'])) {
113
+                $storageQuery->setParameter('id', $storage['id']);
114
+                $storageQuery->executeStatement();
115
+            }
116
+
117
+            if (isset($storage['share_token'])) {
118
+                $shareExternalQuery->setParameter('share_token', $storage['share_token']);
119
+                $shareExternalQuery->setParameter('remote', $storage['remote']);
120
+                $shareExternalQuery->executeStatement();
121
+            }
122
+        }
123
+
124
+        parent::tearDown();
125
+    }
126
+
127
+    private function doesStorageExist($numericId) {
128
+        $qb = Server::get(IDBConnection::class)->getQueryBuilder();
129
+        $qb->select('*')
130
+            ->from('storages')
131
+            ->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId)));
132
+
133
+        $qResult = $qb->executeQuery();
134
+        $result = $qResult->fetch();
135
+        $qResult->closeCursor();
136
+        if (!empty($result)) {
137
+            return true;
138
+        }
139
+
140
+        $qb = Server::get(IDBConnection::class)->getQueryBuilder();
141
+        $qb->select('*')
142
+            ->from('filecache')
143
+            ->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId)));
144
+
145
+        $qResult = $qb->executeQuery();
146
+        $result = $qResult->fetch();
147
+        $qResult->closeCursor();
148
+        if (!empty($result)) {
149
+            return true;
150
+        }
151
+
152
+        return false;
153
+    }
154
+
155
+    /**
156
+     * Test cleanup of orphaned storages
157
+     */
158
+    public function testCleanup(): void {
159
+        $input = $this->getMockBuilder(InputInterface::class)
160
+            ->disableOriginalConstructor()
161
+            ->getMock();
162
+        $output = $this->getMockBuilder(OutputInterface::class)
163
+            ->disableOriginalConstructor()
164
+            ->getMock();
165
+
166
+        // parent folder, `files`, ´test` and `welcome.txt` => 4 elements
167
+        $outputCalls = [];
168
+        $output
169
+            ->expects($this->any())
170
+            ->method('writeln')
171
+            ->willReturnCallback(function (string $text) use (&$outputCalls) {
172
+                $outputCalls[] = $text;
173
+            });
174
+
175
+        $this->cloudIdManager
176
+            ->expects($this->any())
177
+            ->method('getCloudId')
178
+            ->will($this->returnCallback(function (string $user, string $remote) {
179
+                $cloudIdMock = $this->createMock(ICloudId::class);
180
+
181
+                // The remotes are already sanitized in the original data, so
182
+                // they can be directly returned.
183
+                $cloudIdMock
184
+                    ->expects($this->any())
185
+                    ->method('getRemote')
186
+                    ->willReturn($remote);
187
+
188
+                return $cloudIdMock;
189
+            }));
190
+
191
+        $this->command->execute($input, $output);
192
+
193
+        $this->assertTrue($this->doesStorageExist($this->storages[0]['numeric_id']));
194
+        $this->assertTrue($this->doesStorageExist($this->storages[1]['numeric_id']));
195
+        $this->assertFalse($this->doesStorageExist($this->storages[3]['numeric_id']));
196
+        $this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id']));
197
+        $this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id']));
198
+
199
+        $this->assertEquals([
200
+            '5 remote storage(s) need(s) to be checked',
201
+            '5 remote share(s) exist',
202
+        ], array_slice($outputCalls, 0, 2));
203
+    }
204 204
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -86,8 +86,8 @@  discard block
 block discarded – undo
86 86
 			if (isset($storage['files_count'])) {
87 87
 				for ($i = 0; $i < $storage['files_count']; $i++) {
88 88
 					$filesQuery->setParameter('storage', $storage['numeric_id']);
89
-					$filesQuery->setParameter('path', 'file' . $i);
90
-					$filesQuery->setParameter('path_hash', md5('file' . $i));
89
+					$filesQuery->setParameter('path', 'file'.$i);
90
+					$filesQuery->setParameter('path_hash', md5('file'.$i));
91 91
 					$filesQuery->executeStatement();
92 92
 				}
93 93
 			}
@@ -168,14 +168,14 @@  discard block
 block discarded – undo
168 168
 		$output
169 169
 			->expects($this->any())
170 170
 			->method('writeln')
171
-			->willReturnCallback(function (string $text) use (&$outputCalls) {
171
+			->willReturnCallback(function(string $text) use (&$outputCalls) {
172 172
 				$outputCalls[] = $text;
173 173
 			});
174 174
 
175 175
 		$this->cloudIdManager
176 176
 			->expects($this->any())
177 177
 			->method('getCloudId')
178
-			->will($this->returnCallback(function (string $user, string $remote) {
178
+			->will($this->returnCallback(function(string $user, string $remote) {
179 179
 				$cloudIdMock = $this->createMock(ICloudId::class);
180 180
 
181 181
 				// The remotes are already sanitized in the original data, so
Please login to merge, or discard this patch.
apps/files_sharing/tests/Controller/ShareInfoControllerTest.php 1 patch
Indentation   +253 added lines, -253 removed lines patch added patch discarded remove patch
@@ -20,257 +20,257 @@
 block discarded – undo
20 20
 
21 21
 class ShareInfoControllerTest extends TestCase {
22 22
 
23
-	protected ShareInfoController $controller;
24
-	protected ShareManager&MockObject $shareManager;
25
-
26
-
27
-	protected function setUp(): void {
28
-		parent::setUp();
29
-
30
-		$this->shareManager = $this->createMock(ShareManager::class);
31
-
32
-		$this->controller = new ShareInfoController(
33
-			'files_sharing',
34
-			$this->createMock(IRequest::class),
35
-			$this->shareManager
36
-		);
37
-	}
38
-
39
-	public function testNoShare(): void {
40
-		$this->shareManager->method('getShareByToken')
41
-			->with('token')
42
-			->willThrowException(new ShareNotFound());
43
-
44
-		$expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
45
-		$expected->throttle(['token' => 'token']);
46
-		$this->assertEquals($expected, $this->controller->info('token'));
47
-	}
48
-
49
-	public function testWrongPassword(): void {
50
-		$share = $this->createMock(IShare::class);
51
-		$share->method('getPassword')
52
-			->willReturn('sharePass');
53
-
54
-		$this->shareManager->method('getShareByToken')
55
-			->with('token')
56
-			->willReturn($share);
57
-		$this->shareManager->method('checkPassword')
58
-			->with($share, 'pass')
59
-			->willReturn(false);
60
-
61
-		$expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
62
-		$expected->throttle(['token' => 'token']);
63
-		$this->assertEquals($expected, $this->controller->info('token', 'pass'));
64
-	}
65
-
66
-	public function testNoReadPermissions(): void {
67
-		$share = $this->createMock(IShare::class);
68
-		$share->method('getPassword')
69
-			->willReturn('sharePass');
70
-		$share->method('getPermissions')
71
-			->willReturn(Constants::PERMISSION_CREATE);
72
-
73
-		$this->shareManager->method('getShareByToken')
74
-			->with('token')
75
-			->willReturn($share);
76
-		$this->shareManager->method('checkPassword')
77
-			->with($share, 'pass')
78
-			->willReturn(true);
79
-
80
-		$expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
81
-		$expected->throttle(['token' => 'token']);
82
-		$this->assertEquals($expected, $this->controller->info('token', 'pass'));
83
-	}
84
-
85
-	private function prepareFile() {
86
-		$file = $this->createMock(File::class);
87
-
88
-		$file->method('getId')->willReturn(42);
89
-
90
-		$parent = $this->createMock(Folder::class);
91
-		$parent->method('getId')->willReturn(41);
92
-		$file->method('getParent')->willReturn($parent);
93
-
94
-		$file->method('getMTime')->willReturn(1337);
95
-		$file->method('getName')->willReturn('file');
96
-		$file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
97
-		$file->method('getMimeType')->willReturn('mime/type');
98
-		$file->method('getSize')->willReturn(1);
99
-		$file->method('getType')->willReturn('file');
100
-		$file->method('getEtag')->willReturn('etag');
101
-
102
-		return $file;
103
-	}
104
-
105
-	public function testInfoFile(): void {
106
-		$file = $this->prepareFile();
107
-
108
-		$share = $this->createMock(IShare::class);
109
-		$share->method('getPassword')
110
-			->willReturn('sharePass');
111
-		$share->method('getPermissions')
112
-			->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
113
-		$share->method('getNode')
114
-			->willReturn($file);
115
-
116
-		$this->shareManager->method('getShareByToken')
117
-			->with('token')
118
-			->willReturn($share);
119
-		$this->shareManager->method('checkPassword')
120
-			->with($share, 'pass')
121
-			->willReturn(true);
122
-
123
-		$expected = new JSONResponse([
124
-			'id' => 42,
125
-			'parentId' => 41,
126
-			'mtime' => 1337	,
127
-			'name' => 'file',
128
-			'permissions' => 1,
129
-			'mimetype' => 'mime/type',
130
-			'size' => 1,
131
-			'type' => 'file',
132
-			'etag' => 'etag',
133
-		]);
134
-		$this->assertEquals($expected, $this->controller->info('token', 'pass'));
135
-	}
136
-
137
-	public function testInfoFileRO(): void {
138
-		$file = $this->prepareFile();
139
-
140
-		$share = $this->createMock(IShare::class);
141
-		$share->method('getPassword')
142
-			->willReturn('sharePass');
143
-		$share->method('getPermissions')
144
-			->willReturn(Constants::PERMISSION_READ);
145
-		$share->method('getNode')
146
-			->willReturn($file);
147
-
148
-		$this->shareManager->method('getShareByToken')
149
-			->with('token')
150
-			->willReturn($share);
151
-		$this->shareManager->method('checkPassword')
152
-			->with($share, 'pass')
153
-			->willReturn(true);
154
-
155
-		$expected = new JSONResponse([
156
-			'id' => 42,
157
-			'parentId' => 41,
158
-			'mtime' => 1337	,
159
-			'name' => 'file',
160
-			'permissions' => 1,
161
-			'mimetype' => 'mime/type',
162
-			'size' => 1,
163
-			'type' => 'file',
164
-			'etag' => 'etag',
165
-		]);
166
-		$this->assertEquals($expected, $this->controller->info('token', 'pass'));
167
-	}
168
-
169
-	private function prepareFolder() {
170
-		$root = $this->createMock(Folder::class);
171
-
172
-		$root->method('getId')->willReturn(42);
173
-
174
-		$parent = $this->createMock(Folder::class);
175
-		$parent->method('getId')->willReturn(41);
176
-		$root->method('getParent')->willReturn($parent);
177
-
178
-		$root->method('getMTime')->willReturn(1337);
179
-		$root->method('getName')->willReturn('root');
180
-		$root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
181
-		$root->method('getMimeType')->willReturn('mime/type');
182
-		$root->method('getSize')->willReturn(1);
183
-		$root->method('getType')->willReturn('folder');
184
-		$root->method('getEtag')->willReturn('etag');
185
-
186
-
187
-		//Subfolder
188
-		$sub = $this->createMock(Folder::class);
189
-
190
-		$sub->method('getId')->willReturn(43);
191
-		$sub->method('getParent')->willReturn($root);
192
-		$sub->method('getMTime')->willReturn(1338);
193
-		$sub->method('getName')->willReturn('sub');
194
-		$sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
195
-		$sub->method('getMimeType')->willReturn('mime/type');
196
-		$sub->method('getSize')->willReturn(2);
197
-		$sub->method('getType')->willReturn('folder');
198
-		$sub->method('getEtag')->willReturn('etag2');
199
-
200
-		$root->method('getDirectoryListing')->willReturn([$sub]);
201
-
202
-		//Subfile
203
-		$file = $this->createMock(File::class);
204
-		$file->method('getId')->willReturn(88);
205
-		$file->method('getParent')->willReturn($sub);
206
-		$file->method('getMTime')->willReturn(1339);
207
-		$file->method('getName')->willReturn('file');
208
-		$file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
209
-		$file->method('getMimeType')->willReturn('mime/type');
210
-		$file->method('getSize')->willReturn(3);
211
-		$file->method('getType')->willReturn('file');
212
-		$file->method('getEtag')->willReturn('etag3');
213
-
214
-		$sub->method('getDirectoryListing')->willReturn([$file]);
215
-
216
-		return $root;
217
-	}
218
-
219
-	public function testInfoFolder(): void {
220
-		$file = $this->prepareFolder();
221
-
222
-		$share = $this->createMock(IShare::class);
223
-		$share->method('getPassword')
224
-			->willReturn('sharePass');
225
-		$share->method('getPermissions')
226
-			->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
227
-		$share->method('getNode')
228
-			->willReturn($file);
229
-
230
-		$this->shareManager->method('getShareByToken')
231
-			->with('token')
232
-			->willReturn($share);
233
-		$this->shareManager->method('checkPassword')
234
-			->with($share, 'pass')
235
-			->willReturn(true);
236
-
237
-		$expected = new JSONResponse([
238
-			'id' => 42,
239
-			'parentId' => 41,
240
-			'mtime' => 1337,
241
-			'name' => 'root',
242
-			'permissions' => 1,
243
-			'mimetype' => 'mime/type',
244
-			'size' => 1,
245
-			'type' => 'folder',
246
-			'etag' => 'etag',
247
-			'children' => [
248
-				[
249
-					'id' => 43,
250
-					'parentId' => 42,
251
-					'mtime' => 1338,
252
-					'name' => 'sub',
253
-					'permissions' => 3,
254
-					'mimetype' => 'mime/type',
255
-					'size' => 2,
256
-					'type' => 'folder',
257
-					'etag' => 'etag2',
258
-					'children' => [
259
-						[
260
-							'id' => 88,
261
-							'parentId' => 43,
262
-							'mtime' => 1339,
263
-							'name' => 'file',
264
-							'permissions' => 1,
265
-							'mimetype' => 'mime/type',
266
-							'size' => 3,
267
-							'type' => 'file',
268
-							'etag' => 'etag3',
269
-						]
270
-					],
271
-				]
272
-			],
273
-		]);
274
-		$this->assertEquals($expected, $this->controller->info('token', 'pass'));
275
-	}
23
+    protected ShareInfoController $controller;
24
+    protected ShareManager&MockObject $shareManager;
25
+
26
+
27
+    protected function setUp(): void {
28
+        parent::setUp();
29
+
30
+        $this->shareManager = $this->createMock(ShareManager::class);
31
+
32
+        $this->controller = new ShareInfoController(
33
+            'files_sharing',
34
+            $this->createMock(IRequest::class),
35
+            $this->shareManager
36
+        );
37
+    }
38
+
39
+    public function testNoShare(): void {
40
+        $this->shareManager->method('getShareByToken')
41
+            ->with('token')
42
+            ->willThrowException(new ShareNotFound());
43
+
44
+        $expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
45
+        $expected->throttle(['token' => 'token']);
46
+        $this->assertEquals($expected, $this->controller->info('token'));
47
+    }
48
+
49
+    public function testWrongPassword(): void {
50
+        $share = $this->createMock(IShare::class);
51
+        $share->method('getPassword')
52
+            ->willReturn('sharePass');
53
+
54
+        $this->shareManager->method('getShareByToken')
55
+            ->with('token')
56
+            ->willReturn($share);
57
+        $this->shareManager->method('checkPassword')
58
+            ->with($share, 'pass')
59
+            ->willReturn(false);
60
+
61
+        $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
62
+        $expected->throttle(['token' => 'token']);
63
+        $this->assertEquals($expected, $this->controller->info('token', 'pass'));
64
+    }
65
+
66
+    public function testNoReadPermissions(): void {
67
+        $share = $this->createMock(IShare::class);
68
+        $share->method('getPassword')
69
+            ->willReturn('sharePass');
70
+        $share->method('getPermissions')
71
+            ->willReturn(Constants::PERMISSION_CREATE);
72
+
73
+        $this->shareManager->method('getShareByToken')
74
+            ->with('token')
75
+            ->willReturn($share);
76
+        $this->shareManager->method('checkPassword')
77
+            ->with($share, 'pass')
78
+            ->willReturn(true);
79
+
80
+        $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
81
+        $expected->throttle(['token' => 'token']);
82
+        $this->assertEquals($expected, $this->controller->info('token', 'pass'));
83
+    }
84
+
85
+    private function prepareFile() {
86
+        $file = $this->createMock(File::class);
87
+
88
+        $file->method('getId')->willReturn(42);
89
+
90
+        $parent = $this->createMock(Folder::class);
91
+        $parent->method('getId')->willReturn(41);
92
+        $file->method('getParent')->willReturn($parent);
93
+
94
+        $file->method('getMTime')->willReturn(1337);
95
+        $file->method('getName')->willReturn('file');
96
+        $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
97
+        $file->method('getMimeType')->willReturn('mime/type');
98
+        $file->method('getSize')->willReturn(1);
99
+        $file->method('getType')->willReturn('file');
100
+        $file->method('getEtag')->willReturn('etag');
101
+
102
+        return $file;
103
+    }
104
+
105
+    public function testInfoFile(): void {
106
+        $file = $this->prepareFile();
107
+
108
+        $share = $this->createMock(IShare::class);
109
+        $share->method('getPassword')
110
+            ->willReturn('sharePass');
111
+        $share->method('getPermissions')
112
+            ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
113
+        $share->method('getNode')
114
+            ->willReturn($file);
115
+
116
+        $this->shareManager->method('getShareByToken')
117
+            ->with('token')
118
+            ->willReturn($share);
119
+        $this->shareManager->method('checkPassword')
120
+            ->with($share, 'pass')
121
+            ->willReturn(true);
122
+
123
+        $expected = new JSONResponse([
124
+            'id' => 42,
125
+            'parentId' => 41,
126
+            'mtime' => 1337	,
127
+            'name' => 'file',
128
+            'permissions' => 1,
129
+            'mimetype' => 'mime/type',
130
+            'size' => 1,
131
+            'type' => 'file',
132
+            'etag' => 'etag',
133
+        ]);
134
+        $this->assertEquals($expected, $this->controller->info('token', 'pass'));
135
+    }
136
+
137
+    public function testInfoFileRO(): void {
138
+        $file = $this->prepareFile();
139
+
140
+        $share = $this->createMock(IShare::class);
141
+        $share->method('getPassword')
142
+            ->willReturn('sharePass');
143
+        $share->method('getPermissions')
144
+            ->willReturn(Constants::PERMISSION_READ);
145
+        $share->method('getNode')
146
+            ->willReturn($file);
147
+
148
+        $this->shareManager->method('getShareByToken')
149
+            ->with('token')
150
+            ->willReturn($share);
151
+        $this->shareManager->method('checkPassword')
152
+            ->with($share, 'pass')
153
+            ->willReturn(true);
154
+
155
+        $expected = new JSONResponse([
156
+            'id' => 42,
157
+            'parentId' => 41,
158
+            'mtime' => 1337	,
159
+            'name' => 'file',
160
+            'permissions' => 1,
161
+            'mimetype' => 'mime/type',
162
+            'size' => 1,
163
+            'type' => 'file',
164
+            'etag' => 'etag',
165
+        ]);
166
+        $this->assertEquals($expected, $this->controller->info('token', 'pass'));
167
+    }
168
+
169
+    private function prepareFolder() {
170
+        $root = $this->createMock(Folder::class);
171
+
172
+        $root->method('getId')->willReturn(42);
173
+
174
+        $parent = $this->createMock(Folder::class);
175
+        $parent->method('getId')->willReturn(41);
176
+        $root->method('getParent')->willReturn($parent);
177
+
178
+        $root->method('getMTime')->willReturn(1337);
179
+        $root->method('getName')->willReturn('root');
180
+        $root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
181
+        $root->method('getMimeType')->willReturn('mime/type');
182
+        $root->method('getSize')->willReturn(1);
183
+        $root->method('getType')->willReturn('folder');
184
+        $root->method('getEtag')->willReturn('etag');
185
+
186
+
187
+        //Subfolder
188
+        $sub = $this->createMock(Folder::class);
189
+
190
+        $sub->method('getId')->willReturn(43);
191
+        $sub->method('getParent')->willReturn($root);
192
+        $sub->method('getMTime')->willReturn(1338);
193
+        $sub->method('getName')->willReturn('sub');
194
+        $sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
195
+        $sub->method('getMimeType')->willReturn('mime/type');
196
+        $sub->method('getSize')->willReturn(2);
197
+        $sub->method('getType')->willReturn('folder');
198
+        $sub->method('getEtag')->willReturn('etag2');
199
+
200
+        $root->method('getDirectoryListing')->willReturn([$sub]);
201
+
202
+        //Subfile
203
+        $file = $this->createMock(File::class);
204
+        $file->method('getId')->willReturn(88);
205
+        $file->method('getParent')->willReturn($sub);
206
+        $file->method('getMTime')->willReturn(1339);
207
+        $file->method('getName')->willReturn('file');
208
+        $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
209
+        $file->method('getMimeType')->willReturn('mime/type');
210
+        $file->method('getSize')->willReturn(3);
211
+        $file->method('getType')->willReturn('file');
212
+        $file->method('getEtag')->willReturn('etag3');
213
+
214
+        $sub->method('getDirectoryListing')->willReturn([$file]);
215
+
216
+        return $root;
217
+    }
218
+
219
+    public function testInfoFolder(): void {
220
+        $file = $this->prepareFolder();
221
+
222
+        $share = $this->createMock(IShare::class);
223
+        $share->method('getPassword')
224
+            ->willReturn('sharePass');
225
+        $share->method('getPermissions')
226
+            ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
227
+        $share->method('getNode')
228
+            ->willReturn($file);
229
+
230
+        $this->shareManager->method('getShareByToken')
231
+            ->with('token')
232
+            ->willReturn($share);
233
+        $this->shareManager->method('checkPassword')
234
+            ->with($share, 'pass')
235
+            ->willReturn(true);
236
+
237
+        $expected = new JSONResponse([
238
+            'id' => 42,
239
+            'parentId' => 41,
240
+            'mtime' => 1337,
241
+            'name' => 'root',
242
+            'permissions' => 1,
243
+            'mimetype' => 'mime/type',
244
+            'size' => 1,
245
+            'type' => 'folder',
246
+            'etag' => 'etag',
247
+            'children' => [
248
+                [
249
+                    'id' => 43,
250
+                    'parentId' => 42,
251
+                    'mtime' => 1338,
252
+                    'name' => 'sub',
253
+                    'permissions' => 3,
254
+                    'mimetype' => 'mime/type',
255
+                    'size' => 2,
256
+                    'type' => 'folder',
257
+                    'etag' => 'etag2',
258
+                    'children' => [
259
+                        [
260
+                            'id' => 88,
261
+                            'parentId' => 43,
262
+                            'mtime' => 1339,
263
+                            'name' => 'file',
264
+                            'permissions' => 1,
265
+                            'mimetype' => 'mime/type',
266
+                            'size' => 3,
267
+                            'type' => 'file',
268
+                            'etag' => 'etag3',
269
+                        ]
270
+                    ],
271
+                ]
272
+            ],
273
+        ]);
274
+        $this->assertEquals($expected, $this->controller->info('token', 'pass'));
275
+    }
276 276
 }
Please login to merge, or discard this patch.
apps/files_sharing/tests/Controller/ShareAPIControllerTest.php 1 patch
Indentation   +5116 added lines, -5116 removed lines patch added patch discarded remove patch
@@ -57,504 +57,504 @@  discard block
 block discarded – undo
57 57
  */
58 58
 class ShareAPIControllerTest extends TestCase {
59 59
 
60
-	private string $appName = 'files_sharing';
61
-	private string $currentUser;
62
-
63
-	private ShareAPIController $ocs;
64
-
65
-	private IManager&MockObject $shareManager;
66
-	private IGroupManager&MockObject $groupManager;
67
-	private IUserManager&MockObject $userManager;
68
-	private IRequest&MockObject $request;
69
-	private IRootFolder&MockObject $rootFolder;
70
-	private IURLGenerator&MockObject $urlGenerator;
71
-	private IL10N&MockObject $l;
72
-	private IConfig&MockObject $config;
73
-	private IAppManager&MockObject $appManager;
74
-	private ContainerInterface&MockObject $serverContainer;
75
-	private IUserStatusManager&MockObject $userStatusManager;
76
-	private IPreview&MockObject $previewManager;
77
-	private IDateTimeZone&MockObject $dateTimeZone;
78
-	private LoggerInterface&MockObject $logger;
79
-	private IProviderFactory&MockObject $factory;
80
-	private IMailer&MockObject $mailer;
81
-	private ITagManager&MockObject $tagManager;
82
-
83
-	protected function setUp(): void {
84
-		$this->shareManager = $this->createMock(IManager::class);
85
-		$this->shareManager
86
-			->expects($this->any())
87
-			->method('shareApiEnabled')
88
-			->willReturn(true);
89
-		$this->shareManager
90
-			->expects($this->any())
91
-			->method('shareProviderExists')->willReturn(true);
92
-		$this->groupManager = $this->createMock(IGroupManager::class);
93
-		$this->userManager = $this->createMock(IUserManager::class);
94
-		$this->request = $this->createMock(IRequest::class);
95
-		$this->rootFolder = $this->createMock(IRootFolder::class);
96
-		$this->urlGenerator = $this->createMock(IURLGenerator::class);
97
-		$this->currentUser = 'currentUser';
98
-
99
-		$this->l = $this->createMock(IL10N::class);
100
-		$this->l->method('t')
101
-			->willReturnCallback(function ($text, $parameters = []) {
102
-				return vsprintf($text, $parameters);
103
-			});
104
-		$this->config = $this->createMock(IConfig::class);
105
-		$this->appManager = $this->createMock(IAppManager::class);
106
-		$this->serverContainer = $this->createMock(ContainerInterface::class);
107
-		$this->userStatusManager = $this->createMock(IUserStatusManager::class);
108
-		$this->previewManager = $this->createMock(IPreview::class);
109
-		$this->previewManager->method('isAvailable')
110
-			->willReturnCallback(function ($fileInfo) {
111
-				return $fileInfo->getMimeType() === 'mimeWithPreview';
112
-			});
113
-		$this->dateTimeZone = $this->createMock(IDateTimeZone::class);
114
-		$this->logger = $this->createMock(LoggerInterface::class);
115
-		$this->factory = $this->createMock(IProviderFactory::class);
116
-		$this->mailer = $this->createMock(IMailer::class);
117
-		$this->tagManager = $this->createMock(ITagManager::class);
118
-
119
-		$this->ocs = new ShareAPIController(
120
-			$this->appName,
121
-			$this->request,
122
-			$this->shareManager,
123
-			$this->groupManager,
124
-			$this->userManager,
125
-			$this->rootFolder,
126
-			$this->urlGenerator,
127
-			$this->l,
128
-			$this->config,
129
-			$this->appManager,
130
-			$this->serverContainer,
131
-			$this->userStatusManager,
132
-			$this->previewManager,
133
-			$this->dateTimeZone,
134
-			$this->logger,
135
-			$this->factory,
136
-			$this->mailer,
137
-			$this->tagManager,
138
-			$this->currentUser,
139
-		);
140
-	}
141
-
142
-	/**
143
-	 * @return ShareAPIController&MockObject
144
-	 */
145
-	private function mockFormatShare() {
146
-		return $this->getMockBuilder(ShareAPIController::class)
147
-			->setConstructorArgs([
148
-				$this->appName,
149
-				$this->request,
150
-				$this->shareManager,
151
-				$this->groupManager,
152
-				$this->userManager,
153
-				$this->rootFolder,
154
-				$this->urlGenerator,
155
-				$this->l,
156
-				$this->config,
157
-				$this->appManager,
158
-				$this->serverContainer,
159
-				$this->userStatusManager,
160
-				$this->previewManager,
161
-				$this->dateTimeZone,
162
-				$this->logger,
163
-				$this->factory,
164
-				$this->mailer,
165
-				$this->tagManager,
166
-				$this->currentUser,
167
-			])->onlyMethods(['formatShare'])
168
-			->getMock();
169
-	}
170
-
171
-	private function newShare() {
172
-		return Server::get(IManager::class)->newShare();
173
-	}
174
-
175
-
176
-	private function mockShareAttributes() {
177
-		$formattedShareAttributes = [
178
-			[
179
-				'scope' => 'permissions',
180
-				'key' => 'download',
181
-				'value' => true
182
-			]
183
-		];
184
-
185
-		$shareAttributes = $this->createMock(IShareAttributes::class);
186
-		$shareAttributes->method('toArray')->willReturn($formattedShareAttributes);
187
-		$shareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true);
188
-
189
-		// send both IShare attributes class and expected json string
190
-		return [$shareAttributes, \json_encode($formattedShareAttributes)];
191
-	}
192
-
193
-	public function testDeleteShareShareNotFound(): void {
194
-		$this->expectException(OCSNotFoundException::class);
195
-		$this->expectExceptionMessage('Wrong share ID, share does not exist');
196
-
197
-		$this->shareManager
198
-			->expects($this->exactly(7))
199
-			->method('getShareById')
200
-			->willReturnCallback(function ($id): void {
201
-				if ($id === 'ocinternal:42' || $id === 'ocRoomShare:42' || $id === 'ocFederatedSharing:42' || $id === 'ocCircleShare:42' || $id === 'ocMailShare:42' || $id === 'deck:42' || $id === 'sciencemesh:42') {
202
-					throw new ShareNotFound();
203
-				} else {
204
-					throw new \Exception();
205
-				}
206
-			});
207
-
208
-		$this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
209
-
210
-		$this->ocs->deleteShare(42);
211
-	}
212
-
213
-	public function testDeleteShare(): void {
214
-		$node = $this->getMockBuilder(File::class)->getMock();
215
-
216
-		$share = $this->newShare();
217
-		$share->setSharedBy($this->currentUser)
218
-			->setNode($node);
219
-		$this->shareManager
220
-			->expects($this->once())
221
-			->method('getShareById')
222
-			->with('ocinternal:42')
223
-			->willReturn($share);
224
-		$this->shareManager
225
-			->expects($this->once())
226
-			->method('deleteShare')
227
-			->with($share);
228
-
229
-		$node->expects($this->once())
230
-			->method('lock')
231
-			->with(ILockingProvider::LOCK_SHARED);
232
-
233
-		$expected = new DataResponse();
234
-		$result = $this->ocs->deleteShare(42);
235
-
236
-		$this->assertInstanceOf(get_class($expected), $result);
237
-		$this->assertEquals($expected->getData(), $result->getData());
238
-	}
239
-
240
-
241
-	public function testDeleteShareLocked(): void {
242
-		$this->expectException(OCSNotFoundException::class);
243
-		$this->expectExceptionMessage('Could not delete share');
244
-
245
-		$node = $this->getMockBuilder(File::class)->getMock();
246
-		$node->method('getId')->willReturn(1);
247
-
248
-		$share = $this->newShare();
249
-		$share->setNode($node);
250
-
251
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
252
-		$this->rootFolder->method('getUserFolder')
253
-			->with($this->currentUser)
254
-			->willReturn($userFolder);
255
-
256
-		$userFolder->method('getById')
257
-			->with($share->getNodeId())
258
-			->willReturn([$node]);
259
-
260
-		$this->shareManager
261
-			->expects($this->once())
262
-			->method('getShareById')
263
-			->with('ocinternal:42')
264
-			->willReturn($share);
265
-
266
-		$this->shareManager
267
-			->expects($this->never())
268
-			->method('deleteShare')
269
-			->with($share);
270
-
271
-		$node->expects($this->once())
272
-			->method('lock')
273
-			->with(ILockingProvider::LOCK_SHARED)
274
-			->will($this->throwException(new LockedException('mypath')));
275
-
276
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
277
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
278
-
279
-		$this->ocs->deleteShare(42);
280
-	}
281
-
282
-	/**
283
-	 * You can always remove a share that was shared with you
284
-	 */
285
-	public function testDeleteShareWithMe(): void {
286
-		$node = $this->getMockBuilder(File::class)->getMock();
287
-
288
-		$share = $this->newShare();
289
-		$share->setSharedWith($this->currentUser)
290
-			->setShareType(IShare::TYPE_USER)
291
-			->setNode($node);
292
-
293
-		$this->shareManager
294
-			->expects($this->once())
295
-			->method('getShareById')
296
-			->with('ocinternal:42')
297
-			->willReturn($share);
298
-
299
-		$this->shareManager
300
-			->expects($this->once())
301
-			->method('deleteShare')
302
-			->with($share);
303
-
304
-		$node->expects($this->once())
305
-			->method('lock')
306
-			->with(ILockingProvider::LOCK_SHARED);
307
-
308
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
309
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
310
-
311
-		$this->ocs->deleteShare(42);
312
-	}
313
-
314
-	/**
315
-	 * You can always delete a share you own
316
-	 */
317
-	public function testDeleteShareOwner(): void {
318
-		$node = $this->getMockBuilder(File::class)->getMock();
319
-
320
-		$share = $this->newShare();
321
-		$share->setSharedBy($this->currentUser)
322
-			->setNode($node);
323
-
324
-		$this->shareManager
325
-			->expects($this->once())
326
-			->method('getShareById')
327
-			->with('ocinternal:42')
328
-			->willReturn($share);
329
-
330
-		$this->shareManager
331
-			->expects($this->once())
332
-			->method('deleteShare')
333
-			->with($share);
334
-
335
-		$node->expects($this->once())
336
-			->method('lock')
337
-			->with(ILockingProvider::LOCK_SHARED);
338
-
339
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
340
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
341
-
342
-		$this->ocs->deleteShare(42);
343
-	}
344
-
345
-	/**
346
-	 * You can always delete a share when you own
347
-	 * the file path it belong to
348
-	 */
349
-	public function testDeleteShareFileOwner(): void {
350
-		$node = $this->getMockBuilder(File::class)->getMock();
351
-		$node->method('getId')->willReturn(1);
352
-
353
-		$share = $this->newShare();
354
-		$share->setShareOwner($this->currentUser)
355
-			->setNode($node);
356
-
357
-		$this->shareManager
358
-			->expects($this->once())
359
-			->method('getShareById')
360
-			->with('ocinternal:42')
361
-			->willReturn($share);
362
-
363
-		$this->shareManager
364
-			->expects($this->once())
365
-			->method('deleteShare')
366
-			->with($share);
367
-
368
-		$node->expects($this->once())
369
-			->method('lock')
370
-			->with(ILockingProvider::LOCK_SHARED);
371
-
372
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
373
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
374
-
375
-		$this->ocs->deleteShare(42);
376
-	}
377
-
378
-	/**
379
-	 * You can remove (the mountpoint, not the share)
380
-	 * a share if you're in the group the share is shared with
381
-	 */
382
-	public function testDeleteSharedWithMyGroup(): void {
383
-		$node = $this->getMockBuilder(File::class)->getMock();
384
-		$node->method('getId')->willReturn(1);
385
-
386
-		$share = $this->newShare();
387
-		$share->setShareType(IShare::TYPE_GROUP)
388
-			->setSharedWith('group')
389
-			->setNode($node);
390
-
391
-		$this->shareManager
392
-			->expects($this->once())
393
-			->method('getShareById')
394
-			->with('ocinternal:42')
395
-			->willReturn($share);
396
-
397
-		// canDeleteShareFromSelf
398
-		$user = $this->createMock(IUser::class);
399
-		$group = $this->getMockBuilder(IGroup::class)->getMock();
400
-		$this->groupManager
401
-			->method('get')
402
-			->with('group')
403
-			->willReturn($group);
404
-		$this->userManager
405
-			->method('get')
406
-			->with($this->currentUser)
407
-			->willReturn($user);
408
-		$group->method('inGroup')
409
-			->with($user)
410
-			->willReturn(true);
411
-
412
-		$node->expects($this->once())
413
-			->method('lock')
414
-			->with(ILockingProvider::LOCK_SHARED);
415
-
416
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
417
-		$this->rootFolder->method('getUserFolder')
418
-			->with($this->currentUser)
419
-			->willReturn($userFolder);
420
-
421
-		$userFolder->method('getById')
422
-			->with($share->getNodeId())
423
-			->willReturn([$share->getNode()]);
424
-
425
-		$this->shareManager->expects($this->once())
426
-			->method('deleteFromSelf')
427
-			->with($share, $this->currentUser);
428
-
429
-		$this->shareManager->expects($this->never())
430
-			->method('deleteShare');
431
-
432
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share]));
433
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
434
-
435
-		$this->ocs->deleteShare(42);
436
-	}
437
-
438
-	/**
439
-	 * You cannot remove a share if you're not
440
-	 * in the group the share is shared with
441
-	 */
442
-	public function testDeleteSharedWithGroupIDontBelongTo(): void {
443
-		$this->expectException(OCSNotFoundException::class);
444
-		$this->expectExceptionMessage('Wrong share ID, share does not exist');
445
-
446
-		$node = $this->getMockBuilder(File::class)->getMock();
447
-		$node->method('getId')->willReturn(42);
448
-
449
-		$share = $this->newShare();
450
-		$share->setShareType(IShare::TYPE_GROUP)
451
-			->setSharedWith('group')
452
-			->setNode($node);
453
-
454
-		$this->shareManager
455
-			->expects($this->once())
456
-			->method('getShareById')
457
-			->with('ocinternal:42')
458
-			->willReturn($share);
459
-
460
-		// canDeleteShareFromSelf
461
-		$user = $this->createMock(IUser::class);
462
-		$group = $this->getMockBuilder(IGroup::class)->getMock();
463
-		$this->groupManager
464
-			->method('get')
465
-			->with('group')
466
-			->willReturn($group);
467
-		$this->userManager
468
-			->method('get')
469
-			->with($this->currentUser)
470
-			->willReturn($user);
471
-		$group->method('inGroup')
472
-			->with($user)
473
-			->willReturn(false);
474
-
475
-		$node->expects($this->once())
476
-			->method('lock')
477
-			->with(ILockingProvider::LOCK_SHARED);
478
-
479
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
480
-		$this->rootFolder->method('getUserFolder')
481
-			->with($this->currentUser)
482
-			->willReturn($userFolder);
483
-
484
-		$userFolder->method('getById')
485
-			->with($share->getNodeId())
486
-			->willReturn([$share->getNode()]);
487
-
488
-		$this->shareManager->expects($this->never())
489
-			->method('deleteFromSelf');
490
-
491
-		$this->shareManager->expects($this->never())
492
-			->method('deleteShare');
493
-
494
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share]));
495
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
496
-
497
-		$this->ocs->deleteShare(42);
498
-	}
499
-
500
-	public function testDeleteShareOwnerless(): void {
501
-		$ocs = $this->mockFormatShare();
502
-
503
-		$mount = $this->createMock(IShareOwnerlessMount::class);
504
-
505
-		$file = $this->createMock(File::class);
506
-		$file
507
-			->expects($this->exactly(2))
508
-			->method('getPermissions')
509
-			->willReturn(Constants::PERMISSION_SHARE);
510
-		$file
511
-			->expects($this->once())
512
-			->method('getMountPoint')
513
-			->willReturn($mount);
514
-
515
-		$userFolder = $this->createMock(Folder::class);
516
-		$userFolder->method('getById')
517
-			->with(2)
518
-			->willReturn([$file]);
519
-		$userFolder->method('getFirstNodeById')
520
-			->with(2)
521
-			->willReturn($file);
522
-
523
-		$this->rootFolder
524
-			->method('getUserFolder')
525
-			->with($this->currentUser)
526
-			->willReturn($userFolder);
527
-
528
-		$share = $this->createMock(IShare::class);
529
-		$share
530
-			->expects($this->once())
531
-			->method('getNode')
532
-			->willReturn($file);
533
-		$share
534
-			->expects($this->exactly(2))
535
-			->method('getNodeId')
536
-			->willReturn(2);
537
-		$share
538
-			->expects($this->exactly(2))
539
-			->method('getPermissions')
540
-			->willReturn(Constants::PERMISSION_SHARE);
541
-
542
-		$this->shareManager
543
-			->expects($this->once())
544
-			->method('getShareById')
545
-			->with('ocinternal:1', $this->currentUser)
546
-			->willReturn($share);
547
-
548
-		$this->shareManager
549
-			->expects($this->once())
550
-			->method('deleteShare')
551
-			->with($share);
552
-
553
-		$result = $ocs->deleteShare(1);
554
-		$this->assertInstanceOf(DataResponse::class, $result);
555
-	}
556
-
557
-	/*
60
+    private string $appName = 'files_sharing';
61
+    private string $currentUser;
62
+
63
+    private ShareAPIController $ocs;
64
+
65
+    private IManager&MockObject $shareManager;
66
+    private IGroupManager&MockObject $groupManager;
67
+    private IUserManager&MockObject $userManager;
68
+    private IRequest&MockObject $request;
69
+    private IRootFolder&MockObject $rootFolder;
70
+    private IURLGenerator&MockObject $urlGenerator;
71
+    private IL10N&MockObject $l;
72
+    private IConfig&MockObject $config;
73
+    private IAppManager&MockObject $appManager;
74
+    private ContainerInterface&MockObject $serverContainer;
75
+    private IUserStatusManager&MockObject $userStatusManager;
76
+    private IPreview&MockObject $previewManager;
77
+    private IDateTimeZone&MockObject $dateTimeZone;
78
+    private LoggerInterface&MockObject $logger;
79
+    private IProviderFactory&MockObject $factory;
80
+    private IMailer&MockObject $mailer;
81
+    private ITagManager&MockObject $tagManager;
82
+
83
+    protected function setUp(): void {
84
+        $this->shareManager = $this->createMock(IManager::class);
85
+        $this->shareManager
86
+            ->expects($this->any())
87
+            ->method('shareApiEnabled')
88
+            ->willReturn(true);
89
+        $this->shareManager
90
+            ->expects($this->any())
91
+            ->method('shareProviderExists')->willReturn(true);
92
+        $this->groupManager = $this->createMock(IGroupManager::class);
93
+        $this->userManager = $this->createMock(IUserManager::class);
94
+        $this->request = $this->createMock(IRequest::class);
95
+        $this->rootFolder = $this->createMock(IRootFolder::class);
96
+        $this->urlGenerator = $this->createMock(IURLGenerator::class);
97
+        $this->currentUser = 'currentUser';
98
+
99
+        $this->l = $this->createMock(IL10N::class);
100
+        $this->l->method('t')
101
+            ->willReturnCallback(function ($text, $parameters = []) {
102
+                return vsprintf($text, $parameters);
103
+            });
104
+        $this->config = $this->createMock(IConfig::class);
105
+        $this->appManager = $this->createMock(IAppManager::class);
106
+        $this->serverContainer = $this->createMock(ContainerInterface::class);
107
+        $this->userStatusManager = $this->createMock(IUserStatusManager::class);
108
+        $this->previewManager = $this->createMock(IPreview::class);
109
+        $this->previewManager->method('isAvailable')
110
+            ->willReturnCallback(function ($fileInfo) {
111
+                return $fileInfo->getMimeType() === 'mimeWithPreview';
112
+            });
113
+        $this->dateTimeZone = $this->createMock(IDateTimeZone::class);
114
+        $this->logger = $this->createMock(LoggerInterface::class);
115
+        $this->factory = $this->createMock(IProviderFactory::class);
116
+        $this->mailer = $this->createMock(IMailer::class);
117
+        $this->tagManager = $this->createMock(ITagManager::class);
118
+
119
+        $this->ocs = new ShareAPIController(
120
+            $this->appName,
121
+            $this->request,
122
+            $this->shareManager,
123
+            $this->groupManager,
124
+            $this->userManager,
125
+            $this->rootFolder,
126
+            $this->urlGenerator,
127
+            $this->l,
128
+            $this->config,
129
+            $this->appManager,
130
+            $this->serverContainer,
131
+            $this->userStatusManager,
132
+            $this->previewManager,
133
+            $this->dateTimeZone,
134
+            $this->logger,
135
+            $this->factory,
136
+            $this->mailer,
137
+            $this->tagManager,
138
+            $this->currentUser,
139
+        );
140
+    }
141
+
142
+    /**
143
+     * @return ShareAPIController&MockObject
144
+     */
145
+    private function mockFormatShare() {
146
+        return $this->getMockBuilder(ShareAPIController::class)
147
+            ->setConstructorArgs([
148
+                $this->appName,
149
+                $this->request,
150
+                $this->shareManager,
151
+                $this->groupManager,
152
+                $this->userManager,
153
+                $this->rootFolder,
154
+                $this->urlGenerator,
155
+                $this->l,
156
+                $this->config,
157
+                $this->appManager,
158
+                $this->serverContainer,
159
+                $this->userStatusManager,
160
+                $this->previewManager,
161
+                $this->dateTimeZone,
162
+                $this->logger,
163
+                $this->factory,
164
+                $this->mailer,
165
+                $this->tagManager,
166
+                $this->currentUser,
167
+            ])->onlyMethods(['formatShare'])
168
+            ->getMock();
169
+    }
170
+
171
+    private function newShare() {
172
+        return Server::get(IManager::class)->newShare();
173
+    }
174
+
175
+
176
+    private function mockShareAttributes() {
177
+        $formattedShareAttributes = [
178
+            [
179
+                'scope' => 'permissions',
180
+                'key' => 'download',
181
+                'value' => true
182
+            ]
183
+        ];
184
+
185
+        $shareAttributes = $this->createMock(IShareAttributes::class);
186
+        $shareAttributes->method('toArray')->willReturn($formattedShareAttributes);
187
+        $shareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true);
188
+
189
+        // send both IShare attributes class and expected json string
190
+        return [$shareAttributes, \json_encode($formattedShareAttributes)];
191
+    }
192
+
193
+    public function testDeleteShareShareNotFound(): void {
194
+        $this->expectException(OCSNotFoundException::class);
195
+        $this->expectExceptionMessage('Wrong share ID, share does not exist');
196
+
197
+        $this->shareManager
198
+            ->expects($this->exactly(7))
199
+            ->method('getShareById')
200
+            ->willReturnCallback(function ($id): void {
201
+                if ($id === 'ocinternal:42' || $id === 'ocRoomShare:42' || $id === 'ocFederatedSharing:42' || $id === 'ocCircleShare:42' || $id === 'ocMailShare:42' || $id === 'deck:42' || $id === 'sciencemesh:42') {
202
+                    throw new ShareNotFound();
203
+                } else {
204
+                    throw new \Exception();
205
+                }
206
+            });
207
+
208
+        $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
209
+
210
+        $this->ocs->deleteShare(42);
211
+    }
212
+
213
+    public function testDeleteShare(): void {
214
+        $node = $this->getMockBuilder(File::class)->getMock();
215
+
216
+        $share = $this->newShare();
217
+        $share->setSharedBy($this->currentUser)
218
+            ->setNode($node);
219
+        $this->shareManager
220
+            ->expects($this->once())
221
+            ->method('getShareById')
222
+            ->with('ocinternal:42')
223
+            ->willReturn($share);
224
+        $this->shareManager
225
+            ->expects($this->once())
226
+            ->method('deleteShare')
227
+            ->with($share);
228
+
229
+        $node->expects($this->once())
230
+            ->method('lock')
231
+            ->with(ILockingProvider::LOCK_SHARED);
232
+
233
+        $expected = new DataResponse();
234
+        $result = $this->ocs->deleteShare(42);
235
+
236
+        $this->assertInstanceOf(get_class($expected), $result);
237
+        $this->assertEquals($expected->getData(), $result->getData());
238
+    }
239
+
240
+
241
+    public function testDeleteShareLocked(): void {
242
+        $this->expectException(OCSNotFoundException::class);
243
+        $this->expectExceptionMessage('Could not delete share');
244
+
245
+        $node = $this->getMockBuilder(File::class)->getMock();
246
+        $node->method('getId')->willReturn(1);
247
+
248
+        $share = $this->newShare();
249
+        $share->setNode($node);
250
+
251
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
252
+        $this->rootFolder->method('getUserFolder')
253
+            ->with($this->currentUser)
254
+            ->willReturn($userFolder);
255
+
256
+        $userFolder->method('getById')
257
+            ->with($share->getNodeId())
258
+            ->willReturn([$node]);
259
+
260
+        $this->shareManager
261
+            ->expects($this->once())
262
+            ->method('getShareById')
263
+            ->with('ocinternal:42')
264
+            ->willReturn($share);
265
+
266
+        $this->shareManager
267
+            ->expects($this->never())
268
+            ->method('deleteShare')
269
+            ->with($share);
270
+
271
+        $node->expects($this->once())
272
+            ->method('lock')
273
+            ->with(ILockingProvider::LOCK_SHARED)
274
+            ->will($this->throwException(new LockedException('mypath')));
275
+
276
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
277
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
278
+
279
+        $this->ocs->deleteShare(42);
280
+    }
281
+
282
+    /**
283
+     * You can always remove a share that was shared with you
284
+     */
285
+    public function testDeleteShareWithMe(): void {
286
+        $node = $this->getMockBuilder(File::class)->getMock();
287
+
288
+        $share = $this->newShare();
289
+        $share->setSharedWith($this->currentUser)
290
+            ->setShareType(IShare::TYPE_USER)
291
+            ->setNode($node);
292
+
293
+        $this->shareManager
294
+            ->expects($this->once())
295
+            ->method('getShareById')
296
+            ->with('ocinternal:42')
297
+            ->willReturn($share);
298
+
299
+        $this->shareManager
300
+            ->expects($this->once())
301
+            ->method('deleteShare')
302
+            ->with($share);
303
+
304
+        $node->expects($this->once())
305
+            ->method('lock')
306
+            ->with(ILockingProvider::LOCK_SHARED);
307
+
308
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
309
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
310
+
311
+        $this->ocs->deleteShare(42);
312
+    }
313
+
314
+    /**
315
+     * You can always delete a share you own
316
+     */
317
+    public function testDeleteShareOwner(): void {
318
+        $node = $this->getMockBuilder(File::class)->getMock();
319
+
320
+        $share = $this->newShare();
321
+        $share->setSharedBy($this->currentUser)
322
+            ->setNode($node);
323
+
324
+        $this->shareManager
325
+            ->expects($this->once())
326
+            ->method('getShareById')
327
+            ->with('ocinternal:42')
328
+            ->willReturn($share);
329
+
330
+        $this->shareManager
331
+            ->expects($this->once())
332
+            ->method('deleteShare')
333
+            ->with($share);
334
+
335
+        $node->expects($this->once())
336
+            ->method('lock')
337
+            ->with(ILockingProvider::LOCK_SHARED);
338
+
339
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
340
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
341
+
342
+        $this->ocs->deleteShare(42);
343
+    }
344
+
345
+    /**
346
+     * You can always delete a share when you own
347
+     * the file path it belong to
348
+     */
349
+    public function testDeleteShareFileOwner(): void {
350
+        $node = $this->getMockBuilder(File::class)->getMock();
351
+        $node->method('getId')->willReturn(1);
352
+
353
+        $share = $this->newShare();
354
+        $share->setShareOwner($this->currentUser)
355
+            ->setNode($node);
356
+
357
+        $this->shareManager
358
+            ->expects($this->once())
359
+            ->method('getShareById')
360
+            ->with('ocinternal:42')
361
+            ->willReturn($share);
362
+
363
+        $this->shareManager
364
+            ->expects($this->once())
365
+            ->method('deleteShare')
366
+            ->with($share);
367
+
368
+        $node->expects($this->once())
369
+            ->method('lock')
370
+            ->with(ILockingProvider::LOCK_SHARED);
371
+
372
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
373
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
374
+
375
+        $this->ocs->deleteShare(42);
376
+    }
377
+
378
+    /**
379
+     * You can remove (the mountpoint, not the share)
380
+     * a share if you're in the group the share is shared with
381
+     */
382
+    public function testDeleteSharedWithMyGroup(): void {
383
+        $node = $this->getMockBuilder(File::class)->getMock();
384
+        $node->method('getId')->willReturn(1);
385
+
386
+        $share = $this->newShare();
387
+        $share->setShareType(IShare::TYPE_GROUP)
388
+            ->setSharedWith('group')
389
+            ->setNode($node);
390
+
391
+        $this->shareManager
392
+            ->expects($this->once())
393
+            ->method('getShareById')
394
+            ->with('ocinternal:42')
395
+            ->willReturn($share);
396
+
397
+        // canDeleteShareFromSelf
398
+        $user = $this->createMock(IUser::class);
399
+        $group = $this->getMockBuilder(IGroup::class)->getMock();
400
+        $this->groupManager
401
+            ->method('get')
402
+            ->with('group')
403
+            ->willReturn($group);
404
+        $this->userManager
405
+            ->method('get')
406
+            ->with($this->currentUser)
407
+            ->willReturn($user);
408
+        $group->method('inGroup')
409
+            ->with($user)
410
+            ->willReturn(true);
411
+
412
+        $node->expects($this->once())
413
+            ->method('lock')
414
+            ->with(ILockingProvider::LOCK_SHARED);
415
+
416
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
417
+        $this->rootFolder->method('getUserFolder')
418
+            ->with($this->currentUser)
419
+            ->willReturn($userFolder);
420
+
421
+        $userFolder->method('getById')
422
+            ->with($share->getNodeId())
423
+            ->willReturn([$share->getNode()]);
424
+
425
+        $this->shareManager->expects($this->once())
426
+            ->method('deleteFromSelf')
427
+            ->with($share, $this->currentUser);
428
+
429
+        $this->shareManager->expects($this->never())
430
+            ->method('deleteShare');
431
+
432
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share]));
433
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
434
+
435
+        $this->ocs->deleteShare(42);
436
+    }
437
+
438
+    /**
439
+     * You cannot remove a share if you're not
440
+     * in the group the share is shared with
441
+     */
442
+    public function testDeleteSharedWithGroupIDontBelongTo(): void {
443
+        $this->expectException(OCSNotFoundException::class);
444
+        $this->expectExceptionMessage('Wrong share ID, share does not exist');
445
+
446
+        $node = $this->getMockBuilder(File::class)->getMock();
447
+        $node->method('getId')->willReturn(42);
448
+
449
+        $share = $this->newShare();
450
+        $share->setShareType(IShare::TYPE_GROUP)
451
+            ->setSharedWith('group')
452
+            ->setNode($node);
453
+
454
+        $this->shareManager
455
+            ->expects($this->once())
456
+            ->method('getShareById')
457
+            ->with('ocinternal:42')
458
+            ->willReturn($share);
459
+
460
+        // canDeleteShareFromSelf
461
+        $user = $this->createMock(IUser::class);
462
+        $group = $this->getMockBuilder(IGroup::class)->getMock();
463
+        $this->groupManager
464
+            ->method('get')
465
+            ->with('group')
466
+            ->willReturn($group);
467
+        $this->userManager
468
+            ->method('get')
469
+            ->with($this->currentUser)
470
+            ->willReturn($user);
471
+        $group->method('inGroup')
472
+            ->with($user)
473
+            ->willReturn(false);
474
+
475
+        $node->expects($this->once())
476
+            ->method('lock')
477
+            ->with(ILockingProvider::LOCK_SHARED);
478
+
479
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
480
+        $this->rootFolder->method('getUserFolder')
481
+            ->with($this->currentUser)
482
+            ->willReturn($userFolder);
483
+
484
+        $userFolder->method('getById')
485
+            ->with($share->getNodeId())
486
+            ->willReturn([$share->getNode()]);
487
+
488
+        $this->shareManager->expects($this->never())
489
+            ->method('deleteFromSelf');
490
+
491
+        $this->shareManager->expects($this->never())
492
+            ->method('deleteShare');
493
+
494
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share]));
495
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
496
+
497
+        $this->ocs->deleteShare(42);
498
+    }
499
+
500
+    public function testDeleteShareOwnerless(): void {
501
+        $ocs = $this->mockFormatShare();
502
+
503
+        $mount = $this->createMock(IShareOwnerlessMount::class);
504
+
505
+        $file = $this->createMock(File::class);
506
+        $file
507
+            ->expects($this->exactly(2))
508
+            ->method('getPermissions')
509
+            ->willReturn(Constants::PERMISSION_SHARE);
510
+        $file
511
+            ->expects($this->once())
512
+            ->method('getMountPoint')
513
+            ->willReturn($mount);
514
+
515
+        $userFolder = $this->createMock(Folder::class);
516
+        $userFolder->method('getById')
517
+            ->with(2)
518
+            ->willReturn([$file]);
519
+        $userFolder->method('getFirstNodeById')
520
+            ->with(2)
521
+            ->willReturn($file);
522
+
523
+        $this->rootFolder
524
+            ->method('getUserFolder')
525
+            ->with($this->currentUser)
526
+            ->willReturn($userFolder);
527
+
528
+        $share = $this->createMock(IShare::class);
529
+        $share
530
+            ->expects($this->once())
531
+            ->method('getNode')
532
+            ->willReturn($file);
533
+        $share
534
+            ->expects($this->exactly(2))
535
+            ->method('getNodeId')
536
+            ->willReturn(2);
537
+        $share
538
+            ->expects($this->exactly(2))
539
+            ->method('getPermissions')
540
+            ->willReturn(Constants::PERMISSION_SHARE);
541
+
542
+        $this->shareManager
543
+            ->expects($this->once())
544
+            ->method('getShareById')
545
+            ->with('ocinternal:1', $this->currentUser)
546
+            ->willReturn($share);
547
+
548
+        $this->shareManager
549
+            ->expects($this->once())
550
+            ->method('deleteShare')
551
+            ->with($share);
552
+
553
+        $result = $ocs->deleteShare(1);
554
+        $this->assertInstanceOf(DataResponse::class, $result);
555
+    }
556
+
557
+    /*
558 558
 	 * FIXME: Enable once we have a federated Share Provider
559 559
 
560 560
 	public function testGetGetShareNotExists() {
@@ -569,4623 +569,4623 @@  discard block
 block discarded – undo
569 569
 	}
570 570
 	*/
571 571
 
572
-	public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions,
573
-		$shareTime, $expiration, $parent, $target, $mail_send, $note = '', $token = null,
574
-		$password = null, $label = '', $attributes = null) {
575
-		$share = $this->getMockBuilder(IShare::class)->getMock();
576
-		$share->method('getId')->willReturn($id);
577
-		$share->method('getShareType')->willReturn($shareType);
578
-		$share->method('getSharedWith')->willReturn($sharedWith);
579
-		$share->method('getSharedBy')->willReturn($sharedBy);
580
-		$share->method('getShareOwner')->willReturn($shareOwner);
581
-		$share->method('getNode')->willReturn($path);
582
-		$share->method('getPermissions')->willReturn($permissions);
583
-		$share->method('getNote')->willReturn($note);
584
-		$share->method('getLabel')->willReturn($label);
585
-		$share->method('getAttributes')->willReturn($attributes);
586
-		$time = new \DateTime();
587
-		$time->setTimestamp($shareTime);
588
-		$share->method('getShareTime')->willReturn($time);
589
-		$share->method('getExpirationDate')->willReturn($expiration);
590
-		$share->method('getTarget')->willReturn($target);
591
-		$share->method('getMailSend')->willReturn($mail_send);
592
-		$share->method('getToken')->willReturn($token);
593
-		$share->method('getPassword')->willReturn($password);
594
-
595
-		if ($shareType === IShare::TYPE_USER ||
596
-			$shareType === IShare::TYPE_GROUP ||
597
-			$shareType === IShare::TYPE_LINK) {
598
-			$share->method('getFullId')->willReturn('ocinternal:' . $id);
599
-		}
600
-
601
-		return $share;
602
-	}
603
-
604
-	public function dataGetShare() {
605
-		$data = [];
606
-
607
-		$cache = $this->getMockBuilder('OC\Files\Cache\Cache')
608
-			->disableOriginalConstructor()
609
-			->getMock();
610
-		$cache->method('getNumericStorageId')->willReturn(101);
611
-
612
-		$storage = $this->getMockBuilder(IStorage::class)
613
-			->disableOriginalConstructor()
614
-			->getMock();
615
-		$storage->method('getId')->willReturn('STORAGE');
616
-		$storage->method('getCache')->willReturn($cache);
617
-
618
-		$parentFolder = $this->getMockBuilder(Folder::class)->getMock();
619
-		$parentFolder->method('getId')->willReturn(3);
620
-		$mountPoint = $this->createMock(IMountPoint::class);
621
-		$mountPoint->method('getMountType')->willReturn('');
622
-
623
-		$file = $this->getMockBuilder('OCP\Files\File')->getMock();
624
-		$file->method('getId')->willReturn(1);
625
-		$file->method('getPath')->willReturn('file');
626
-		$file->method('getStorage')->willReturn($storage);
627
-		$file->method('getParent')->willReturn($parentFolder);
628
-		$file->method('getSize')->willReturn(123465);
629
-		$file->method('getMTime')->willReturn(1234567890);
630
-		$file->method('getMimeType')->willReturn('myMimeType');
631
-		$file->method('getMountPoint')->willReturn($mountPoint);
632
-
633
-		$folder = $this->getMockBuilder(Folder::class)->getMock();
634
-		$folder->method('getId')->willReturn(2);
635
-		$folder->method('getPath')->willReturn('folder');
636
-		$folder->method('getStorage')->willReturn($storage);
637
-		$folder->method('getParent')->willReturn($parentFolder);
638
-		$folder->method('getSize')->willReturn(123465);
639
-		$folder->method('getMTime')->willReturn(1234567890);
640
-		$folder->method('getMimeType')->willReturn('myFolderMimeType');
641
-		$folder->method('getMountPoint')->willReturn($mountPoint);
642
-
643
-		[$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
644
-
645
-		// File shared with user
646
-		$share = $this->createShare(
647
-			100,
648
-			IShare::TYPE_USER,
649
-			'userId',
650
-			'initiatorId',
651
-			'ownerId',
652
-			$file,
653
-			4,
654
-			5,
655
-			null,
656
-			6,
657
-			'target',
658
-			0,
659
-			'personal note',
660
-			$shareAttributes,
661
-		);
662
-		$expected = [
663
-			'id' => 100,
664
-			'share_type' => IShare::TYPE_USER,
665
-			'share_with' => 'userId',
666
-			'share_with_displayname' => 'userDisplay',
667
-			'share_with_displayname_unique' => '[email protected]',
668
-			'uid_owner' => 'initiatorId',
669
-			'displayname_owner' => 'initiatorDisplay',
670
-			'item_type' => 'file',
671
-			'item_source' => 1,
672
-			'file_source' => 1,
673
-			'file_target' => 'target',
674
-			'file_parent' => 3,
675
-			'token' => null,
676
-			'expiration' => null,
677
-			'permissions' => 4,
678
-			'attributes' => $shareAttributesReturnJson,
679
-			'stime' => 5,
680
-			'parent' => null,
681
-			'storage_id' => 'STORAGE',
682
-			'path' => 'file',
683
-			'storage' => 101,
684
-			'mail_send' => 0,
685
-			'uid_file_owner' => 'ownerId',
686
-			'note' => 'personal note',
687
-			'label' => '',
688
-			'displayname_file_owner' => 'ownerDisplay',
689
-			'mimetype' => 'myMimeType',
690
-			'has_preview' => false,
691
-			'hide_download' => 0,
692
-			'can_edit' => false,
693
-			'can_delete' => false,
694
-			'item_size' => 123465,
695
-			'item_mtime' => 1234567890,
696
-			'attributes' => null,
697
-			'item_permissions' => 4,
698
-			'is-mount-root' => false,
699
-			'mount-type' => '',
700
-		];
701
-		$data[] = [$share, $expected];
702
-
703
-		// Folder shared with group
704
-		$share = $this->createShare(
705
-			101,
706
-			IShare::TYPE_GROUP,
707
-			'groupId',
708
-			'initiatorId',
709
-			'ownerId',
710
-			$folder,
711
-			4,
712
-			5,
713
-			null,
714
-			6,
715
-			'target',
716
-			0,
717
-			'personal note',
718
-			$shareAttributes,
719
-		);
720
-		$expected = [
721
-			'id' => 101,
722
-			'share_type' => IShare::TYPE_GROUP,
723
-			'share_with' => 'groupId',
724
-			'share_with_displayname' => 'groupId',
725
-			'uid_owner' => 'initiatorId',
726
-			'displayname_owner' => 'initiatorDisplay',
727
-			'item_type' => 'folder',
728
-			'item_source' => 2,
729
-			'file_source' => 2,
730
-			'file_target' => 'target',
731
-			'file_parent' => 3,
732
-			'token' => null,
733
-			'expiration' => null,
734
-			'permissions' => 4,
735
-			'attributes' => $shareAttributesReturnJson,
736
-			'stime' => 5,
737
-			'parent' => null,
738
-			'storage_id' => 'STORAGE',
739
-			'path' => 'folder',
740
-			'storage' => 101,
741
-			'mail_send' => 0,
742
-			'uid_file_owner' => 'ownerId',
743
-			'note' => 'personal note',
744
-			'label' => '',
745
-			'displayname_file_owner' => 'ownerDisplay',
746
-			'mimetype' => 'myFolderMimeType',
747
-			'has_preview' => false,
748
-			'hide_download' => 0,
749
-			'can_edit' => false,
750
-			'can_delete' => false,
751
-			'item_size' => 123465,
752
-			'item_mtime' => 1234567890,
753
-			'attributes' => null,
754
-			'item_permissions' => 4,
755
-			'is-mount-root' => false,
756
-			'mount-type' => '',
757
-		];
758
-		$data[] = [$share, $expected];
759
-
760
-		// File shared by link with Expire
761
-		$expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
762
-		$share = $this->createShare(
763
-			101,
764
-			IShare::TYPE_LINK,
765
-			null,
766
-			'initiatorId',
767
-			'ownerId',
768
-			$folder,
769
-			4,
770
-			5,
771
-			$expire,
772
-			6,
773
-			'target',
774
-			0,
775
-			'personal note',
776
-			'token',
777
-			'password',
778
-			'first link share'
779
-		);
780
-		$expected = [
781
-			'id' => 101,
782
-			'share_type' => IShare::TYPE_LINK,
783
-			'password' => 'password',
784
-			'share_with' => 'password',
785
-			'share_with_displayname' => '(Shared link)',
786
-			'send_password_by_talk' => false,
787
-			'uid_owner' => 'initiatorId',
788
-			'displayname_owner' => 'initiatorDisplay',
789
-			'item_type' => 'folder',
790
-			'item_source' => 2,
791
-			'file_source' => 2,
792
-			'file_target' => 'target',
793
-			'file_parent' => 3,
794
-			'token' => 'token',
795
-			'expiration' => '2000-01-02 00:00:00',
796
-			'permissions' => 4,
797
-			'attributes' => null,
798
-			'stime' => 5,
799
-			'parent' => null,
800
-			'storage_id' => 'STORAGE',
801
-			'path' => 'folder',
802
-			'storage' => 101,
803
-			'mail_send' => 0,
804
-			'url' => 'url',
805
-			'uid_file_owner' => 'ownerId',
806
-			'note' => 'personal note',
807
-			'label' => 'first link share',
808
-			'displayname_file_owner' => 'ownerDisplay',
809
-			'mimetype' => 'myFolderMimeType',
810
-			'has_preview' => false,
811
-			'hide_download' => 0,
812
-			'can_edit' => false,
813
-			'can_delete' => false,
814
-			'item_size' => 123465,
815
-			'item_mtime' => 1234567890,
816
-			'attributes' => null,
817
-			'item_permissions' => 4,
818
-			'is-mount-root' => false,
819
-			'mount-type' => '',
820
-		];
821
-		$data[] = [$share, $expected];
822
-
823
-		return $data;
824
-	}
825
-
826
-	/**
827
-	 * @dataProvider dataGetShare
828
-	 */
829
-	public function testGetShare(IShare $share, array $result): void {
830
-		/** @var ShareAPIController&MockObject $ocs */
831
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
832
-			->setConstructorArgs([
833
-				$this->appName,
834
-				$this->request,
835
-				$this->shareManager,
836
-				$this->groupManager,
837
-				$this->userManager,
838
-				$this->rootFolder,
839
-				$this->urlGenerator,
840
-				$this->l,
841
-				$this->config,
842
-				$this->appManager,
843
-				$this->serverContainer,
844
-				$this->userStatusManager,
845
-				$this->previewManager,
846
-				$this->dateTimeZone,
847
-				$this->logger,
848
-				$this->factory,
849
-				$this->mailer,
850
-				$this->tagManager,
851
-				$this->currentUser,
852
-			])
853
-			->onlyMethods(['canAccessShare'])
854
-			->getMock();
855
-
856
-		$ocs->expects($this->any())
857
-			->method('canAccessShare')
858
-			->willReturn(true);
859
-
860
-		$this->shareManager
861
-			->expects($this->any())
862
-			->method('getShareById')
863
-			->with($share->getFullId(), 'currentUser')
864
-			->willReturn($share);
865
-
866
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
867
-		$userFolder
868
-			->method('getRelativePath')
869
-			->willReturnArgument(0);
870
-
871
-		$userFolder->method('getById')
872
-			->with($share->getNodeId())
873
-			->willReturn([$share->getNode()]);
874
-		$userFolder->method('getFirstNodeById')
875
-			->with($share->getNodeId())
876
-			->willReturn($share->getNode());
877
-
878
-		$this->rootFolder->method('getUserFolder')
879
-			->with($this->currentUser)
880
-			->willReturn($userFolder);
881
-
882
-		$this->urlGenerator
883
-			->method('linkToRouteAbsolute')
884
-			->willReturn('url');
885
-
886
-		$initiator = $this->getMockBuilder(IUser::class)->getMock();
887
-		$initiator->method('getUID')->willReturn('initiatorId');
888
-		$initiator->method('getDisplayName')->willReturn('initiatorDisplay');
889
-
890
-		$owner = $this->getMockBuilder(IUser::class)->getMock();
891
-		$owner->method('getUID')->willReturn('ownerId');
892
-		$owner->method('getDisplayName')->willReturn('ownerDisplay');
893
-
894
-		$user = $this->getMockBuilder(IUser::class)->getMock();
895
-		$user->method('getUID')->willReturn('userId');
896
-		$user->method('getDisplayName')->willReturn('userDisplay');
897
-		$user->method('getSystemEMailAddress')->willReturn('[email protected]');
898
-
899
-		$group = $this->getMockBuilder(IGroup::class)->getMock();
900
-		$group->method('getGID')->willReturn('groupId');
901
-
902
-		$this->userManager->method('get')->willReturnMap([
903
-			['userId', $user],
904
-			['initiatorId', $initiator],
905
-			['ownerId', $owner],
906
-		]);
907
-		$this->groupManager->method('get')->willReturnMap([
908
-			['group', $group],
909
-		]);
910
-		$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
911
-
912
-		$data = $ocs->getShare($share->getId())->getData()[0];
913
-		$this->assertEquals($result, $data);
914
-	}
915
-
916
-
917
-	public function testGetShareInvalidNode(): void {
918
-		$this->expectException(OCSNotFoundException::class);
919
-		$this->expectExceptionMessage('Wrong share ID, share does not exist');
920
-
921
-		$share = Server::get(IManager::class)->newShare();
922
-		$share->setSharedBy('initiator')
923
-			->setSharedWith('recipient')
924
-			->setShareOwner('owner');
925
-
926
-		$this->shareManager
927
-			->expects($this->once())
928
-			->method('getShareById')
929
-			->with('ocinternal:42', 'currentUser')
930
-			->willReturn($share);
931
-
932
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
933
-		$this->rootFolder->method('getUserFolder')
934
-			->with($this->currentUser)
935
-			->willReturn($userFolder);
936
-
937
-		$this->ocs->getShare(42);
938
-	}
939
-
940
-	public function dataGetShares() {
941
-		$folder = $this->getMockBuilder(Folder::class)->getMock();
942
-		$file1 = $this->getMockBuilder(File::class)->getMock();
943
-		$file1->method('getName')
944
-			->willReturn('file1');
945
-		$file2 = $this->getMockBuilder(File::class)->getMock();
946
-		$file2->method('getName')
947
-			->willReturn('file2');
948
-
949
-		$folder->method('getDirectoryListing')
950
-			->willReturn([$file1, $file2]);
951
-
952
-		$file1UserShareOwner = Server::get(IManager::class)->newShare();
953
-		$file1UserShareOwner->setShareType(IShare::TYPE_USER)
954
-			->setSharedWith('recipient')
955
-			->setSharedBy('initiator')
956
-			->setShareOwner('currentUser')
957
-			->setPermissions(Constants::PERMISSION_READ)
958
-			->setNode($file1)
959
-			->setId(4);
960
-
961
-		$file1UserShareOwnerExpected = [
962
-			'id' => 4,
963
-			'share_type' => IShare::TYPE_USER,
964
-		];
965
-
966
-		$file1UserShareInitiator = Server::get(IManager::class)->newShare();
967
-		$file1UserShareInitiator->setShareType(IShare::TYPE_USER)
968
-			->setSharedWith('recipient')
969
-			->setSharedBy('currentUser')
970
-			->setShareOwner('owner')
971
-			->setPermissions(Constants::PERMISSION_READ)
972
-			->setNode($file1)
973
-			->setId(8);
974
-
975
-		$file1UserShareInitiatorExpected = [
976
-			'id' => 8,
977
-			'share_type' => IShare::TYPE_USER,
978
-		];
979
-
980
-		$file1UserShareRecipient = Server::get(IManager::class)->newShare();
981
-		$file1UserShareRecipient->setShareType(IShare::TYPE_USER)
982
-			->setSharedWith('currentUser')
983
-			->setSharedBy('initiator')
984
-			->setShareOwner('owner')
985
-			->setPermissions(Constants::PERMISSION_READ)
986
-			->setNode($file1)
987
-			->setId(15);
988
-
989
-		$file1UserShareRecipientExpected = [
990
-			'id' => 15,
991
-			'share_type' => IShare::TYPE_USER,
992
-		];
993
-
994
-		$file1UserShareOther = Server::get(IManager::class)->newShare();
995
-		$file1UserShareOther->setShareType(IShare::TYPE_USER)
996
-			->setSharedWith('recipient')
997
-			->setSharedBy('initiator')
998
-			->setShareOwner('owner')
999
-			->setPermissions(Constants::PERMISSION_READ)
1000
-			->setNode($file1)
1001
-			->setId(16);
1002
-
1003
-		$file1UserShareOtherExpected = [
1004
-			'id' => 16,
1005
-			'share_type' => IShare::TYPE_USER,
1006
-		];
1007
-
1008
-		$file1GroupShareOwner = Server::get(IManager::class)->newShare();
1009
-		$file1GroupShareOwner->setShareType(IShare::TYPE_GROUP)
1010
-			->setSharedWith('recipient')
1011
-			->setSharedBy('initiator')
1012
-			->setShareOwner('currentUser')
1013
-			->setPermissions(Constants::PERMISSION_READ)
1014
-			->setNode($file1)
1015
-			->setId(23);
1016
-
1017
-		$file1GroupShareOwnerExpected = [
1018
-			'id' => 23,
1019
-			'share_type' => IShare::TYPE_GROUP,
1020
-		];
1021
-
1022
-		$file1GroupShareRecipient = Server::get(IManager::class)->newShare();
1023
-		$file1GroupShareRecipient->setShareType(IShare::TYPE_GROUP)
1024
-			->setSharedWith('currentUserGroup')
1025
-			->setSharedBy('initiator')
1026
-			->setShareOwner('owner')
1027
-			->setPermissions(Constants::PERMISSION_READ)
1028
-			->setNode($file1)
1029
-			->setId(42);
1030
-
1031
-		$file1GroupShareRecipientExpected = [
1032
-			'id' => 42,
1033
-			'share_type' => IShare::TYPE_GROUP,
1034
-		];
1035
-
1036
-		$file1GroupShareOther = Server::get(IManager::class)->newShare();
1037
-		$file1GroupShareOther->setShareType(IShare::TYPE_GROUP)
1038
-			->setSharedWith('recipient')
1039
-			->setSharedBy('initiator')
1040
-			->setShareOwner('owner')
1041
-			->setPermissions(Constants::PERMISSION_READ)
1042
-			->setNode($file1)
1043
-			->setId(108);
1044
-
1045
-		$file1LinkShareOwner = Server::get(IManager::class)->newShare();
1046
-		$file1LinkShareOwner->setShareType(IShare::TYPE_LINK)
1047
-			->setSharedWith('recipient')
1048
-			->setSharedBy('initiator')
1049
-			->setShareOwner('currentUser')
1050
-			->setPermissions(Constants::PERMISSION_READ)
1051
-			->setNode($file1)
1052
-			->setId(415);
1053
-
1054
-		$file1LinkShareOwnerExpected = [
1055
-			'id' => 415,
1056
-			'share_type' => IShare::TYPE_LINK,
1057
-		];
1058
-
1059
-		$file1EmailShareOwner = Server::get(IManager::class)->newShare();
1060
-		$file1EmailShareOwner->setShareType(IShare::TYPE_EMAIL)
1061
-			->setSharedWith('recipient')
1062
-			->setSharedBy('initiator')
1063
-			->setShareOwner('currentUser')
1064
-			->setPermissions(Constants::PERMISSION_READ)
1065
-			->setNode($file1)
1066
-			->setId(416);
1067
-
1068
-		$file1EmailShareOwnerExpected = [
1069
-			'id' => 416,
1070
-			'share_type' => IShare::TYPE_EMAIL,
1071
-		];
1072
-
1073
-		$file1CircleShareOwner = Server::get(IManager::class)->newShare();
1074
-		$file1CircleShareOwner->setShareType(IShare::TYPE_CIRCLE)
1075
-			->setSharedWith('recipient')
1076
-			->setSharedBy('initiator')
1077
-			->setShareOwner('currentUser')
1078
-			->setPermissions(Constants::PERMISSION_READ)
1079
-			->setNode($file1)
1080
-			->setId(423);
1081
-
1082
-		$file1CircleShareOwnerExpected = [
1083
-			'id' => 423,
1084
-			'share_type' => IShare::TYPE_CIRCLE,
1085
-		];
1086
-
1087
-		$file1RoomShareOwner = Server::get(IManager::class)->newShare();
1088
-		$file1RoomShareOwner->setShareType(IShare::TYPE_ROOM)
1089
-			->setSharedWith('recipient')
1090
-			->setSharedBy('initiator')
1091
-			->setShareOwner('currentUser')
1092
-			->setPermissions(Constants::PERMISSION_READ)
1093
-			->setNode($file1)
1094
-			->setId(442);
1095
-
1096
-		$file1RoomShareOwnerExpected = [
1097
-			'id' => 442,
1098
-			'share_type' => IShare::TYPE_ROOM,
1099
-		];
1100
-
1101
-		$file1RemoteShareOwner = Server::get(IManager::class)->newShare();
1102
-		$file1RemoteShareOwner->setShareType(IShare::TYPE_REMOTE)
1103
-			->setSharedWith('recipient')
1104
-			->setSharedBy('initiator')
1105
-			->setShareOwner('currentUser')
1106
-			->setPermissions(Constants::PERMISSION_READ)
1107
-			->setExpirationDate(new \DateTime('2000-01-01T01:02:03'))
1108
-			->setNode($file1)
1109
-			->setId(815);
1110
-
1111
-		$file1RemoteShareOwnerExpected = [
1112
-			'id' => 815,
1113
-			'share_type' => IShare::TYPE_REMOTE,
1114
-		];
1115
-
1116
-		$file1RemoteGroupShareOwner = Server::get(IManager::class)->newShare();
1117
-		$file1RemoteGroupShareOwner->setShareType(IShare::TYPE_REMOTE_GROUP)
1118
-			->setSharedWith('recipient')
1119
-			->setSharedBy('initiator')
1120
-			->setShareOwner('currentUser')
1121
-			->setPermissions(Constants::PERMISSION_READ)
1122
-			->setExpirationDate(new \DateTime('2000-01-02T01:02:03'))
1123
-			->setNode($file1)
1124
-			->setId(816);
1125
-
1126
-		$file1RemoteGroupShareOwnerExpected = [
1127
-			'id' => 816,
1128
-			'share_type' => IShare::TYPE_REMOTE_GROUP,
1129
-		];
1130
-
1131
-		$file2UserShareOwner = Server::get(IManager::class)->newShare();
1132
-		$file2UserShareOwner->setShareType(IShare::TYPE_USER)
1133
-			->setSharedWith('recipient')
1134
-			->setSharedBy('initiator')
1135
-			->setShareOwner('currentUser')
1136
-			->setPermissions(Constants::PERMISSION_READ)
1137
-			->setNode($file2)
1138
-			->setId(823);
1139
-
1140
-		$file2UserShareOwnerExpected = [
1141
-			'id' => 823,
1142
-			'share_type' => IShare::TYPE_USER,
1143
-		];
1144
-
1145
-		$data = [
1146
-			[
1147
-				[
1148
-					'path' => $file1,
1149
-				],
1150
-				[
1151
-					'file1' => [
1152
-						IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner],
1153
-					],
1154
-				],
1155
-				[
1156
-				],
1157
-				[
1158
-					$file1UserShareOwnerExpected
1159
-				]
1160
-			],
1161
-			[
1162
-				[
1163
-					'path' => $file1,
1164
-				],
1165
-				[
1166
-					'file1' => [
1167
-						IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient],
1168
-					],
1169
-				],
1170
-				[
1171
-				],
1172
-				[
1173
-					$file1UserShareOwnerExpected,
1174
-				]
1175
-			],
1176
-			[
1177
-				[
1178
-					'path' => $file1,
1179
-				],
1180
-				[
1181
-					'file1' => [
1182
-						IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1183
-					],
1184
-				],
1185
-				[
1186
-				],
1187
-				[
1188
-					$file1UserShareOwnerExpected,
1189
-					$file1UserShareInitiatorExpected,
1190
-					$file1UserShareOtherExpected,
1191
-				]
1192
-			],
1193
-			[
1194
-				[
1195
-					'path' => $file1,
1196
-				],
1197
-				[
1198
-					'file1' => [
1199
-						IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1200
-					],
1201
-				],
1202
-				[
1203
-				],
1204
-				[
1205
-					$file1UserShareInitiatorExpected,
1206
-				]
1207
-			],
1208
-			[
1209
-				[
1210
-					'path' => $file1,
1211
-				],
1212
-				[
1213
-					'file1' => [
1214
-						IShare::TYPE_USER => [$file1UserShareOwner],
1215
-						IShare::TYPE_GROUP => [$file1GroupShareRecipient],
1216
-					],
1217
-				],
1218
-				[
1219
-				],
1220
-				[
1221
-					$file1UserShareOwnerExpected,
1222
-					$file1GroupShareRecipientExpected,
1223
-				]
1224
-			],
1225
-			[
1226
-				[
1227
-					'path' => $file1,
1228
-				],
1229
-				[
1230
-					'file1' => [
1231
-						IShare::TYPE_USER => [$file1UserShareOwner],
1232
-						IShare::TYPE_GROUP => [$file1GroupShareOwner],
1233
-						IShare::TYPE_LINK => [$file1LinkShareOwner],
1234
-						IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1235
-						IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1236
-						IShare::TYPE_ROOM => [$file1RoomShareOwner],
1237
-						IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1238
-						IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1239
-					],
1240
-				],
1241
-				[
1242
-				],
1243
-				[
1244
-					$file1UserShareOwnerExpected,
1245
-					$file1GroupShareOwnerExpected,
1246
-					$file1LinkShareOwnerExpected,
1247
-					$file1EmailShareOwnerExpected,
1248
-					$file1CircleShareOwnerExpected,
1249
-					$file1RoomShareOwnerExpected,
1250
-				]
1251
-			],
1252
-			[
1253
-				[
1254
-					'path' => $file1,
1255
-				],
1256
-				[
1257
-					'file1' => [
1258
-						IShare::TYPE_USER => [$file1UserShareOwner],
1259
-						IShare::TYPE_GROUP => [$file1GroupShareOwner],
1260
-						IShare::TYPE_LINK => [$file1LinkShareOwner],
1261
-						IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1262
-						IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1263
-						IShare::TYPE_ROOM => [$file1RoomShareOwner],
1264
-						IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1265
-						IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1266
-					],
1267
-				],
1268
-				[
1269
-					IShare::TYPE_REMOTE => true,
1270
-					IShare::TYPE_REMOTE_GROUP => true,
1271
-				],
1272
-				[
1273
-					$file1UserShareOwnerExpected,
1274
-					$file1GroupShareOwnerExpected,
1275
-					$file1LinkShareOwnerExpected,
1276
-					$file1EmailShareOwnerExpected,
1277
-					$file1CircleShareOwnerExpected,
1278
-					$file1RoomShareOwnerExpected,
1279
-					$file1RemoteShareOwnerExpected,
1280
-					$file1RemoteGroupShareOwnerExpected,
1281
-				]
1282
-			],
1283
-			[
1284
-				[
1285
-					'path' => $folder,
1286
-					'subfiles' => 'true',
1287
-				],
1288
-				[
1289
-					'file1' => [
1290
-						IShare::TYPE_USER => [$file1UserShareOwner],
1291
-					],
1292
-					'file2' => [
1293
-						IShare::TYPE_USER => [$file2UserShareOwner],
1294
-					],
1295
-				],
1296
-				[
1297
-				],
1298
-				[
1299
-					$file1UserShareOwnerExpected,
1300
-					$file2UserShareOwnerExpected,
1301
-				]
1302
-			],
1303
-			[
1304
-				[
1305
-					'path' => $folder,
1306
-					'subfiles' => 'true',
1307
-				],
1308
-				[
1309
-					'file1' => [
1310
-						IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner],
1311
-					],
1312
-				],
1313
-				[
1314
-				],
1315
-				[
1316
-					$file1UserShareOwnerExpected,
1317
-				]
1318
-			],
1319
-			[
1320
-				[
1321
-					'path' => $folder,
1322
-					'subfiles' => 'true',
1323
-				],
1324
-				[
1325
-					'file1' => [
1326
-						IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient],
1327
-					],
1328
-				],
1329
-				[
1330
-				],
1331
-				[
1332
-					$file1UserShareOwnerExpected
1333
-				]
1334
-			],
1335
-			[
1336
-				[
1337
-					'path' => $folder,
1338
-					'subfiles' => 'true',
1339
-				],
1340
-				[
1341
-					'file1' => [
1342
-						IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1343
-					],
1344
-					'file2' => [
1345
-						IShare::TYPE_USER => [$file2UserShareOwner],
1346
-					],
1347
-				],
1348
-				[
1349
-				],
1350
-				[
1351
-					$file1UserShareInitiatorExpected,
1352
-					$file1UserShareOtherExpected,
1353
-					$file2UserShareOwnerExpected,
1354
-				]
1355
-			],
1356
-			// This might not happen in a real environment, as the combination
1357
-			// of shares does not seem to be possible on a folder without
1358
-			// resharing rights; if the folder has resharing rights then the
1359
-			// share with others would be included too in the results.
1360
-			[
1361
-				[
1362
-					'path' => $folder,
1363
-					'subfiles' => 'true',
1364
-				],
1365
-				[
1366
-					'file1' => [
1367
-						IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1368
-					],
1369
-				],
1370
-				[
1371
-				],
1372
-				[
1373
-					$file1UserShareInitiatorExpected,
1374
-				]
1375
-			],
1376
-			[
1377
-				[
1378
-					'path' => $folder,
1379
-					'subfiles' => 'true',
1380
-				],
1381
-				[
1382
-					'file1' => [
1383
-						IShare::TYPE_USER => [$file1UserShareOwner],
1384
-						IShare::TYPE_GROUP => [$file1GroupShareRecipient],
1385
-					],
1386
-				],
1387
-				[
1388
-				],
1389
-				[
1390
-					$file1UserShareOwnerExpected,
1391
-					$file1GroupShareRecipientExpected,
1392
-				]
1393
-			],
1394
-			[
1395
-				[
1396
-					'path' => $folder,
1397
-					'subfiles' => 'true',
1398
-				],
1399
-				[
1400
-					'file1' => [
1401
-						IShare::TYPE_USER => [$file1UserShareOwner],
1402
-						IShare::TYPE_GROUP => [$file1GroupShareOwner],
1403
-						IShare::TYPE_LINK => [$file1LinkShareOwner],
1404
-						IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1405
-						IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1406
-						IShare::TYPE_ROOM => [$file1RoomShareOwner],
1407
-						IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1408
-						IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1409
-					],
1410
-				],
1411
-				[
1412
-				],
1413
-				[
1414
-					$file1UserShareOwnerExpected,
1415
-					$file1GroupShareOwnerExpected,
1416
-					$file1LinkShareOwnerExpected,
1417
-					$file1EmailShareOwnerExpected,
1418
-					$file1CircleShareOwnerExpected,
1419
-					$file1RoomShareOwnerExpected,
1420
-				]
1421
-			],
1422
-			[
1423
-				[
1424
-					'path' => $folder,
1425
-					'subfiles' => 'true',
1426
-				],
1427
-				[
1428
-					'file1' => [
1429
-						IShare::TYPE_USER => [$file1UserShareOwner],
1430
-						IShare::TYPE_GROUP => [$file1GroupShareOwner],
1431
-						IShare::TYPE_LINK => [$file1LinkShareOwner],
1432
-						IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1433
-						IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1434
-						IShare::TYPE_ROOM => [$file1RoomShareOwner],
1435
-						IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1436
-						IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1437
-					],
1438
-				],
1439
-				[
1440
-					IShare::TYPE_REMOTE => true,
1441
-					IShare::TYPE_REMOTE_GROUP => true,
1442
-				],
1443
-				[
1444
-					$file1UserShareOwnerExpected,
1445
-					$file1GroupShareOwnerExpected,
1446
-					$file1LinkShareOwnerExpected,
1447
-					$file1EmailShareOwnerExpected,
1448
-					$file1CircleShareOwnerExpected,
1449
-					$file1RoomShareOwnerExpected,
1450
-					$file1RemoteShareOwnerExpected,
1451
-					$file1RemoteGroupShareOwnerExpected,
1452
-				]
1453
-			],
1454
-		];
1455
-
1456
-		return $data;
1457
-	}
1458
-
1459
-	/**
1460
-	 * @dataProvider dataGetShares
1461
-	 */
1462
-	public function testGetShares(array $getSharesParameters, array $shares, array $extraShareTypes, array $expected): void {
1463
-		/** @var ShareAPIController&MockObject $ocs */
1464
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
1465
-			->setConstructorArgs([
1466
-				$this->appName,
1467
-				$this->request,
1468
-				$this->shareManager,
1469
-				$this->groupManager,
1470
-				$this->userManager,
1471
-				$this->rootFolder,
1472
-				$this->urlGenerator,
1473
-				$this->l,
1474
-				$this->config,
1475
-				$this->appManager,
1476
-				$this->serverContainer,
1477
-				$this->userStatusManager,
1478
-				$this->previewManager,
1479
-				$this->dateTimeZone,
1480
-				$this->logger,
1481
-				$this->factory,
1482
-				$this->mailer,
1483
-				$this->tagManager,
1484
-				$this->currentUser,
1485
-			])
1486
-			->onlyMethods(['formatShare'])
1487
-			->getMock();
1488
-
1489
-		$ocs->method('formatShare')
1490
-			->willReturnCallback(
1491
-				function ($share) {
1492
-					return [
1493
-						'id' => $share->getId(),
1494
-						'share_type' => $share->getShareType()
1495
-					];
1496
-				}
1497
-			);
1498
-
1499
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1500
-		$userFolder->method('get')
1501
-			->with('path')
1502
-			->willReturn($getSharesParameters['path']);
1503
-
1504
-		$this->rootFolder->method('getUserFolder')
1505
-			->with($this->currentUser)
1506
-			->willReturn($userFolder);
1507
-
1508
-		$this->shareManager
1509
-			->method('getSharesBy')
1510
-			->willReturnCallback(
1511
-				function ($user, $shareType, $node) use ($shares) {
1512
-					if (!isset($shares[$node->getName()]) || !isset($shares[$node->getName()][$shareType])) {
1513
-						return [];
1514
-					}
1515
-					return $shares[$node->getName()][$shareType];
1516
-				}
1517
-			);
1518
-
1519
-		$this->shareManager
1520
-			->method('outgoingServer2ServerSharesAllowed')
1521
-			->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE] ?? false);
1522
-
1523
-		$this->shareManager
1524
-			->method('outgoingServer2ServerGroupSharesAllowed')
1525
-			->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE_GROUP] ?? false);
1526
-
1527
-		$this->groupManager
1528
-			->method('isInGroup')
1529
-			->willReturnCallback(
1530
-				function ($user, $group) {
1531
-					return $group === 'currentUserGroup';
1532
-				}
1533
-			);
1534
-
1535
-		$result = $ocs->getShares(
1536
-			$getSharesParameters['sharedWithMe'] ?? 'false',
1537
-			$getSharesParameters['reshares'] ?? 'false',
1538
-			$getSharesParameters['subfiles'] ?? 'false',
1539
-			'path'
1540
-		);
1541
-
1542
-		$this->assertEquals($expected, $result->getData());
1543
-	}
1544
-
1545
-	public function testCanAccessShare(): void {
1546
-		$share = $this->getMockBuilder(IShare::class)->getMock();
1547
-		$share->method('getShareOwner')->willReturn($this->currentUser);
1548
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1549
-
1550
-		$share = $this->getMockBuilder(IShare::class)->getMock();
1551
-		$share->method('getSharedBy')->willReturn($this->currentUser);
1552
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1553
-
1554
-		$share = $this->getMockBuilder(IShare::class)->getMock();
1555
-		$share->method('getShareType')->willReturn(IShare::TYPE_USER);
1556
-		$share->method('getSharedWith')->willReturn($this->currentUser);
1557
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1558
-
1559
-		$file = $this->getMockBuilder(File::class)->getMock();
1560
-
1561
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1562
-		$this->rootFolder->method('getUserFolder')
1563
-			->with($this->currentUser)
1564
-			->willReturn($userFolder);
1565
-
1566
-		$userFolder->method('getFirstNodeById')
1567
-			->with($share->getNodeId())
1568
-			->willReturn($file);
1569
-		$userFolder->method('getById')
1570
-			->with($share->getNodeId())
1571
-			->willReturn([$file]);
1572
-
1573
-		$file->method('getPermissions')
1574
-			->will($this->onConsecutiveCalls(Constants::PERMISSION_SHARE, Constants::PERMISSION_READ));
1575
-
1576
-		// getPermissions -> share
1577
-		$share = $this->getMockBuilder(IShare::class)->getMock();
1578
-		$share->method('getShareType')->willReturn(IShare::TYPE_USER);
1579
-		$share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
1580
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1581
-
1582
-		// getPermissions -> read
1583
-		$share = $this->getMockBuilder(IShare::class)->getMock();
1584
-		$share->method('getShareType')->willReturn(IShare::TYPE_USER);
1585
-		$share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
1586
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1587
-
1588
-		$share = $this->getMockBuilder(IShare::class)->getMock();
1589
-		$share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1590
-		$share->method('getSharedWith')->willReturn('group');
1591
-
1592
-		$user = $this->createMock(IUser::class);
1593
-		$this->userManager->method('get')
1594
-			->with($this->currentUser)
1595
-			->willReturn($user);
1596
-
1597
-		$group = $this->getMockBuilder(IGroup::class)->getMock();
1598
-		$group->method('inGroup')->with($user)->willReturn(true);
1599
-		$group2 = $this->getMockBuilder(IGroup::class)->getMock();
1600
-		$group2->method('inGroup')->with($user)->willReturn(false);
1601
-
1602
-		$this->groupManager->method('get')->willReturnMap([
1603
-			['group', $group],
1604
-			['group2', $group2],
1605
-			['groupnull', null],
1606
-		]);
1607
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1608
-
1609
-		$share = $this->createMock(IShare::class);
1610
-		$share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1611
-		$share->method('getSharedWith')->willReturn('group2');
1612
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1613
-
1614
-		// null group
1615
-		$share = $this->createMock(IShare::class);
1616
-		$share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1617
-		$share->method('getSharedWith')->willReturn('groupnull');
1618
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1619
-
1620
-		$share = $this->createMock(IShare::class);
1621
-		$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
1622
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1623
-	}
1624
-
1625
-	public function dataCanAccessRoomShare() {
1626
-		$result = [];
1627
-
1628
-		$share = $this->createMock(IShare::class);
1629
-		$share->method('getShareType')->willReturn(IShare::TYPE_ROOM);
1630
-		$share->method('getSharedWith')->willReturn('recipientRoom');
1631
-
1632
-		$result[] = [
1633
-			false, $share, false, false
1634
-		];
1635
-
1636
-		$result[] = [
1637
-			false, $share, false, true
1638
-		];
1639
-
1640
-		$result[] = [
1641
-			true, $share, true, true
1642
-		];
1643
-
1644
-		$result[] = [
1645
-			false, $share, true, false
1646
-		];
1647
-
1648
-		return $result;
1649
-	}
1650
-
1651
-	/**
1652
-	 * @dataProvider dataCanAccessRoomShare
1653
-	 *
1654
-	 * @param bool $expects
1655
-	 * @param IShare $share
1656
-	 * @param bool helperAvailable
1657
-	 * @param bool canAccessShareByHelper
1658
-	 */
1659
-	public function testCanAccessRoomShare(bool $expected, IShare $share, bool $helperAvailable, bool $canAccessShareByHelper): void {
1660
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1661
-		$this->rootFolder->method('getUserFolder')
1662
-			->with($this->currentUser)
1663
-			->willReturn($userFolder);
1664
-
1665
-		$userFolder->method('getById')
1666
-			->with($share->getNodeId())
1667
-			->willReturn([$share->getNode()]);
1668
-
1669
-		if (!$helperAvailable) {
1670
-			$this->appManager->method('isEnabledForUser')
1671
-				->with('spreed')
1672
-				->willReturn(false);
1673
-		} else {
1674
-			$this->appManager->method('isEnabledForUser')
1675
-				->with('spreed')
1676
-				->willReturn(true);
1677
-
1678
-			// This is not possible anymore with PHPUnit 10+
1679
-			// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
1680
-			// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
1681
-			$helper = $this->getMockBuilder(\stdClass::class)
1682
-				->addMethods(['canAccessShare'])
1683
-				->getMock();
1684
-			$helper->method('canAccessShare')
1685
-				->with($share, $this->currentUser)
1686
-				->willReturn($canAccessShareByHelper);
1687
-
1688
-			$this->serverContainer->method('get')
1689
-				->with('\OCA\Talk\Share\Helper\ShareAPIController')
1690
-				->willReturn($helper);
1691
-		}
1692
-
1693
-		$this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1694
-	}
1695
-
1696
-
1697
-	public function testCreateShareNoPath(): void {
1698
-		$this->expectException(OCSNotFoundException::class);
1699
-		$this->expectExceptionMessage('Please specify a file or folder path');
1700
-
1701
-		$this->ocs->createShare();
1702
-	}
1703
-
1704
-
1705
-	public function testCreateShareInvalidPath(): void {
1706
-		$this->expectException(OCSNotFoundException::class);
1707
-		$this->expectExceptionMessage('Wrong path, file/folder does not exist');
1708
-
1709
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1710
-		$this->rootFolder->expects($this->once())
1711
-			->method('getUserFolder')
1712
-			->with('currentUser')
1713
-			->willReturn($userFolder);
1714
-
1715
-		$userFolder->expects($this->once())
1716
-			->method('get')
1717
-			->with('invalid-path')
1718
-			->will($this->throwException(new NotFoundException()));
1719
-
1720
-		$this->ocs->createShare('invalid-path');
1721
-	}
1722
-
1723
-	public function testCreateShareInvalidShareType(): void {
1724
-		$this->expectException(OCSBadRequestException::class);
1725
-		$this->expectExceptionMessage('Unknown share type');
1726
-
1727
-		$share = $this->newShare();
1728
-		$this->shareManager->method('newShare')->willReturn($share);
1729
-
1730
-		[$userFolder, $file] = $this->getNonSharedUserFile();
1731
-		$this->rootFolder->expects($this->atLeastOnce())
1732
-			->method('getUserFolder')
1733
-			->with('currentUser')
1734
-			->willReturn($userFolder);
1735
-
1736
-		$userFolder->expects($this->atLeastOnce())
1737
-			->method('get')
1738
-			->with('valid-path')
1739
-			->willReturn($file);
1740
-		$userFolder->method('getById')
1741
-			->willReturn([]);
1742
-
1743
-		$file->expects($this->once())
1744
-			->method('lock')
1745
-			->with(ILockingProvider::LOCK_SHARED);
1746
-
1747
-		$this->ocs->createShare('valid-path', 31);
1748
-	}
1749
-
1750
-	public function testCreateShareUserNoShareWith(): void {
1751
-		$this->expectException(OCSNotFoundException::class);
1752
-		$this->expectExceptionMessage('Please specify a valid account to share with');
1753
-
1754
-		$share = $this->newShare();
1755
-		$this->shareManager->method('newShare')->willReturn($share);
1756
-
1757
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1758
-		$this->rootFolder->method('getUserFolder')
1759
-			->with('currentUser')
1760
-			->willReturn($userFolder);
1761
-
1762
-		$userFolder->expects($this->once())
1763
-			->method('get')
1764
-			->with('valid-path')
1765
-			->willReturn($path);
1766
-		$userFolder->method('getById')
1767
-			->willReturn([]);
1768
-
1769
-		$path->expects($this->once())
1770
-			->method('lock')
1771
-			->with(ILockingProvider::LOCK_SHARED);
1772
-
1773
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER);
1774
-	}
1775
-
1776
-
1777
-	public function testCreateShareUserNoValidShareWith(): void {
1778
-		$this->expectException(OCSNotFoundException::class);
1779
-		$this->expectExceptionMessage('Please specify a valid account to share with');
1780
-
1781
-		$share = $this->newShare();
1782
-		$this->shareManager->method('newShare')->willReturn($share);
1783
-
1784
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1785
-		$this->rootFolder->method('getUserFolder')
1786
-			->with('currentUser')
1787
-			->willReturn($userFolder);
1788
-
1789
-		$userFolder->expects($this->once())
1790
-			->method('get')
1791
-			->with('valid-path')
1792
-			->willReturn($path);
1793
-		$userFolder->method('getById')
1794
-			->willReturn([]);
1795
-		$path->expects($this->once())
1796
-			->method('lock')
1797
-			->with(ILockingProvider::LOCK_SHARED);
1798
-		$this->userManager->method('userExists')
1799
-			->with('invalidUser')
1800
-			->willReturn(false);
1801
-
1802
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'invalidUser');
1803
-	}
1804
-
1805
-	public function testCreateShareUser(): void {
1806
-		$share = $this->newShare();
1807
-		$this->shareManager->method('newShare')->willReturn($share);
1808
-
1809
-		/** @var ShareAPIController $ocs */
1810
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
1811
-			->setConstructorArgs([
1812
-				$this->appName,
1813
-				$this->request,
1814
-				$this->shareManager,
1815
-				$this->groupManager,
1816
-				$this->userManager,
1817
-				$this->rootFolder,
1818
-				$this->urlGenerator,
1819
-				$this->l,
1820
-				$this->config,
1821
-				$this->appManager,
1822
-				$this->serverContainer,
1823
-				$this->userStatusManager,
1824
-				$this->previewManager,
1825
-				$this->dateTimeZone,
1826
-				$this->logger,
1827
-				$this->factory,
1828
-				$this->mailer,
1829
-				$this->tagManager,
1830
-				$this->currentUser,
1831
-			])->onlyMethods(['formatShare'])
1832
-			->getMock();
1833
-
1834
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1835
-		$this->rootFolder->expects($this->exactly(2))
1836
-			->method('getUserFolder')
1837
-			->with('currentUser')
1838
-			->willReturn($userFolder);
1839
-
1840
-		$userFolder->expects($this->once())
1841
-			->method('get')
1842
-			->with('valid-path')
1843
-			->willReturn($path);
1844
-		$userFolder->method('getById')
1845
-			->willReturn([]);
1846
-
1847
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
1848
-
1849
-		$path->expects($this->once())
1850
-			->method('lock')
1851
-			->with(ILockingProvider::LOCK_SHARED);
1852
-
1853
-		$this->shareManager->method('createShare')
1854
-			->with($this->callback(function (IShare $share) use ($path) {
1855
-				return $share->getNode() === $path &&
1856
-					$share->getPermissions() === (
1857
-						Constants::PERMISSION_ALL &
1858
-						~Constants::PERMISSION_DELETE &
1859
-						~Constants::PERMISSION_CREATE
1860
-					) &&
1861
-					$share->getShareType() === IShare::TYPE_USER &&
1862
-					$share->getSharedWith() === 'validUser' &&
1863
-					$share->getSharedBy() === 'currentUser';
1864
-			}))
1865
-			->willReturnArgument(0);
1866
-
1867
-		$expected = new DataResponse([]);
1868
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
1869
-
1870
-		$this->assertInstanceOf(get_class($expected), $result);
1871
-		$this->assertEquals($expected->getData(), $result->getData());
1872
-	}
1873
-
1874
-
1875
-	public function testCreateShareGroupNoValidShareWith(): void {
1876
-		$this->expectException(OCSNotFoundException::class);
1877
-		$this->expectExceptionMessage('Please specify a valid group');
1878
-
1879
-		$share = $this->newShare();
1880
-		$this->shareManager->method('newShare')->willReturn($share);
1881
-		$this->shareManager->method('createShare')->willReturnArgument(0);
1882
-		$this->shareManager->method('allowGroupSharing')->willReturn(true);
1883
-
1884
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1885
-		$this->rootFolder->method('getUserFolder')
1886
-			->with('currentUser')
1887
-			->willReturn($userFolder);
1888
-
1889
-		$userFolder->expects($this->once())
1890
-			->method('get')
1891
-			->with('valid-path')
1892
-			->willReturn($path);
1893
-		$userFolder->method('getById')
1894
-			->willReturn([]);
1895
-
1896
-		$path->expects($this->once())
1897
-			->method('lock')
1898
-			->with(ILockingProvider::LOCK_SHARED);
1899
-
1900
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
1901
-	}
1902
-
1903
-	public function testCreateShareGroup(): void {
1904
-		$share = $this->newShare();
1905
-		$this->shareManager->method('newShare')->willReturn($share);
1906
-
1907
-		/** @var ShareAPIController&MockObject $ocs */
1908
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
1909
-			->setConstructorArgs([
1910
-				$this->appName,
1911
-				$this->request,
1912
-				$this->shareManager,
1913
-				$this->groupManager,
1914
-				$this->userManager,
1915
-				$this->rootFolder,
1916
-				$this->urlGenerator,
1917
-				$this->l,
1918
-				$this->config,
1919
-				$this->appManager,
1920
-				$this->serverContainer,
1921
-				$this->userStatusManager,
1922
-				$this->previewManager,
1923
-				$this->dateTimeZone,
1924
-				$this->logger,
1925
-				$this->factory,
1926
-				$this->mailer,
1927
-				$this->tagManager,
1928
-				$this->currentUser,
1929
-			])->onlyMethods(['formatShare'])
1930
-			->getMock();
1931
-
1932
-		$this->request
1933
-			->method('getParam')
1934
-			->willReturnMap([
1935
-				['path', null, 'valid-path'],
1936
-				['permissions', null, Constants::PERMISSION_ALL],
1937
-				['shareType', '-1', IShare::TYPE_GROUP],
1938
-				['shareWith', null, 'validGroup'],
1939
-			]);
1940
-
1941
-		[$userFolder, $path] = $this->getNonSharedUserFolder();
1942
-		$this->rootFolder->expects($this->exactly(2))
1943
-			->method('getUserFolder')
1944
-			->with('currentUser')
1945
-			->willReturn($userFolder);
1946
-
1947
-		$userFolder->expects($this->once())
1948
-			->method('get')
1949
-			->with('valid-path')
1950
-			->willReturn($path);
1951
-		$userFolder->method('getById')
1952
-			->willReturn([]);
1953
-
1954
-		$this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
1955
-
1956
-		$this->shareManager->expects($this->once())
1957
-			->method('allowGroupSharing')
1958
-			->willReturn(true);
1959
-
1960
-		$path->expects($this->once())
1961
-			->method('lock')
1962
-			->with(ILockingProvider::LOCK_SHARED);
1963
-
1964
-		$this->shareManager->method('createShare')
1965
-			->with($this->callback(function (IShare $share) use ($path) {
1966
-				return $share->getNode() === $path &&
1967
-				$share->getPermissions() === Constants::PERMISSION_ALL &&
1968
-				$share->getShareType() === IShare::TYPE_GROUP &&
1969
-				$share->getSharedWith() === 'validGroup' &&
1970
-				$share->getSharedBy() === 'currentUser';
1971
-			}))
1972
-			->willReturnArgument(0);
1973
-
1974
-		$expected = new DataResponse([]);
1975
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'validGroup');
1976
-
1977
-		$this->assertInstanceOf(get_class($expected), $result);
1978
-		$this->assertEquals($expected->getData(), $result->getData());
1979
-	}
1980
-
1981
-
1982
-	public function testCreateShareGroupNotAllowed(): void {
1983
-		$this->expectException(OCSNotFoundException::class);
1984
-		$this->expectExceptionMessage('Group sharing is disabled by the administrator');
1985
-
1986
-		$share = $this->newShare();
1987
-		$this->shareManager->method('newShare')->willReturn($share);
1988
-
1989
-		[$userFolder, $path] = $this->getNonSharedUserFolder();
1990
-		$this->rootFolder->method('getUserFolder')
1991
-			->with('currentUser')
1992
-			->willReturn($userFolder);
1993
-
1994
-		$userFolder->expects($this->once())
1995
-			->method('get')
1996
-			->with('valid-path')
1997
-			->willReturn($path);
1998
-		$userFolder->method('getById')
1999
-			->willReturn([]);
2000
-
2001
-		$this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
2002
-
2003
-		$this->shareManager->expects($this->once())
2004
-			->method('allowGroupSharing')
2005
-			->willReturn(false);
2006
-
2007
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
2008
-	}
2009
-
2010
-
2011
-	public function testCreateShareLinkNoLinksAllowed(): void {
2012
-		$this->expectException(OCSNotFoundException::class);
2013
-		$this->expectExceptionMessage('Public link sharing is disabled by the administrator');
2014
-
2015
-		$this->request
2016
-			->method('getParam')
2017
-			->willReturnMap([
2018
-				['path', null, 'valid-path'],
2019
-				['shareType', '-1', IShare::TYPE_LINK],
2020
-			]);
2021
-
2022
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2023
-		$path->method('getId')->willReturn(42);
2024
-		$storage = $this->createMock(IStorage::class);
2025
-		$storage->method('instanceOfStorage')
2026
-			->willReturnMap([
2027
-				['OCA\Files_Sharing\External\Storage', false],
2028
-				['OCA\Files_Sharing\SharedStorage', false],
2029
-			]);
2030
-		$path->method('getStorage')->willReturn($storage);
2031
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2032
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2033
-		$this->rootFolder->method('getById')
2034
-			->willReturn([]);
2035
-
2036
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2037
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2038
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(false);
2039
-
2040
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK);
2041
-	}
2042
-
2043
-
2044
-	public function testCreateShareLinkNoPublicUpload(): void {
2045
-		$this->expectException(OCSForbiddenException::class);
2046
-		$this->expectExceptionMessage('Public upload disabled by the administrator');
2047
-
2048
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2049
-		$path->method('getId')->willReturn(42);
2050
-		$storage = $this->createMock(IStorage::class);
2051
-		$storage->method('instanceOfStorage')
2052
-			->willReturnMap([
2053
-				['OCA\Files_Sharing\External\Storage', false],
2054
-				['OCA\Files_Sharing\SharedStorage', false],
2055
-			]);
2056
-		$path->method('getStorage')->willReturn($storage);
2057
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2058
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2059
-		$this->rootFolder->method('getById')
2060
-			->willReturn([]);
2061
-
2062
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2063
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2064
-
2065
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2066
-	}
2067
-
2068
-
2069
-	public function testCreateShareLinkPublicUploadFile(): void {
2070
-		$this->expectException(OCSBadRequestException::class);
2071
-		$this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
2072
-
2073
-		$storage = $this->createMock(IStorage::class);
2074
-		$storage->method('instanceOfStorage')
2075
-			->willReturnMap([
2076
-				['OCA\Files_Sharing\External\Storage', false],
2077
-				['OCA\Files_Sharing\SharedStorage', false],
2078
-			]);
2079
-
2080
-		$file = $this->createMock(File::class);
2081
-		$file->method('getId')->willReturn(42);
2082
-		$file->method('getStorage')->willReturn($storage);
572
+    public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions,
573
+        $shareTime, $expiration, $parent, $target, $mail_send, $note = '', $token = null,
574
+        $password = null, $label = '', $attributes = null) {
575
+        $share = $this->getMockBuilder(IShare::class)->getMock();
576
+        $share->method('getId')->willReturn($id);
577
+        $share->method('getShareType')->willReturn($shareType);
578
+        $share->method('getSharedWith')->willReturn($sharedWith);
579
+        $share->method('getSharedBy')->willReturn($sharedBy);
580
+        $share->method('getShareOwner')->willReturn($shareOwner);
581
+        $share->method('getNode')->willReturn($path);
582
+        $share->method('getPermissions')->willReturn($permissions);
583
+        $share->method('getNote')->willReturn($note);
584
+        $share->method('getLabel')->willReturn($label);
585
+        $share->method('getAttributes')->willReturn($attributes);
586
+        $time = new \DateTime();
587
+        $time->setTimestamp($shareTime);
588
+        $share->method('getShareTime')->willReturn($time);
589
+        $share->method('getExpirationDate')->willReturn($expiration);
590
+        $share->method('getTarget')->willReturn($target);
591
+        $share->method('getMailSend')->willReturn($mail_send);
592
+        $share->method('getToken')->willReturn($token);
593
+        $share->method('getPassword')->willReturn($password);
594
+
595
+        if ($shareType === IShare::TYPE_USER ||
596
+            $shareType === IShare::TYPE_GROUP ||
597
+            $shareType === IShare::TYPE_LINK) {
598
+            $share->method('getFullId')->willReturn('ocinternal:' . $id);
599
+        }
600
+
601
+        return $share;
602
+    }
603
+
604
+    public function dataGetShare() {
605
+        $data = [];
606
+
607
+        $cache = $this->getMockBuilder('OC\Files\Cache\Cache')
608
+            ->disableOriginalConstructor()
609
+            ->getMock();
610
+        $cache->method('getNumericStorageId')->willReturn(101);
611
+
612
+        $storage = $this->getMockBuilder(IStorage::class)
613
+            ->disableOriginalConstructor()
614
+            ->getMock();
615
+        $storage->method('getId')->willReturn('STORAGE');
616
+        $storage->method('getCache')->willReturn($cache);
617
+
618
+        $parentFolder = $this->getMockBuilder(Folder::class)->getMock();
619
+        $parentFolder->method('getId')->willReturn(3);
620
+        $mountPoint = $this->createMock(IMountPoint::class);
621
+        $mountPoint->method('getMountType')->willReturn('');
622
+
623
+        $file = $this->getMockBuilder('OCP\Files\File')->getMock();
624
+        $file->method('getId')->willReturn(1);
625
+        $file->method('getPath')->willReturn('file');
626
+        $file->method('getStorage')->willReturn($storage);
627
+        $file->method('getParent')->willReturn($parentFolder);
628
+        $file->method('getSize')->willReturn(123465);
629
+        $file->method('getMTime')->willReturn(1234567890);
630
+        $file->method('getMimeType')->willReturn('myMimeType');
631
+        $file->method('getMountPoint')->willReturn($mountPoint);
632
+
633
+        $folder = $this->getMockBuilder(Folder::class)->getMock();
634
+        $folder->method('getId')->willReturn(2);
635
+        $folder->method('getPath')->willReturn('folder');
636
+        $folder->method('getStorage')->willReturn($storage);
637
+        $folder->method('getParent')->willReturn($parentFolder);
638
+        $folder->method('getSize')->willReturn(123465);
639
+        $folder->method('getMTime')->willReturn(1234567890);
640
+        $folder->method('getMimeType')->willReturn('myFolderMimeType');
641
+        $folder->method('getMountPoint')->willReturn($mountPoint);
642
+
643
+        [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
644
+
645
+        // File shared with user
646
+        $share = $this->createShare(
647
+            100,
648
+            IShare::TYPE_USER,
649
+            'userId',
650
+            'initiatorId',
651
+            'ownerId',
652
+            $file,
653
+            4,
654
+            5,
655
+            null,
656
+            6,
657
+            'target',
658
+            0,
659
+            'personal note',
660
+            $shareAttributes,
661
+        );
662
+        $expected = [
663
+            'id' => 100,
664
+            'share_type' => IShare::TYPE_USER,
665
+            'share_with' => 'userId',
666
+            'share_with_displayname' => 'userDisplay',
667
+            'share_with_displayname_unique' => '[email protected]',
668
+            'uid_owner' => 'initiatorId',
669
+            'displayname_owner' => 'initiatorDisplay',
670
+            'item_type' => 'file',
671
+            'item_source' => 1,
672
+            'file_source' => 1,
673
+            'file_target' => 'target',
674
+            'file_parent' => 3,
675
+            'token' => null,
676
+            'expiration' => null,
677
+            'permissions' => 4,
678
+            'attributes' => $shareAttributesReturnJson,
679
+            'stime' => 5,
680
+            'parent' => null,
681
+            'storage_id' => 'STORAGE',
682
+            'path' => 'file',
683
+            'storage' => 101,
684
+            'mail_send' => 0,
685
+            'uid_file_owner' => 'ownerId',
686
+            'note' => 'personal note',
687
+            'label' => '',
688
+            'displayname_file_owner' => 'ownerDisplay',
689
+            'mimetype' => 'myMimeType',
690
+            'has_preview' => false,
691
+            'hide_download' => 0,
692
+            'can_edit' => false,
693
+            'can_delete' => false,
694
+            'item_size' => 123465,
695
+            'item_mtime' => 1234567890,
696
+            'attributes' => null,
697
+            'item_permissions' => 4,
698
+            'is-mount-root' => false,
699
+            'mount-type' => '',
700
+        ];
701
+        $data[] = [$share, $expected];
702
+
703
+        // Folder shared with group
704
+        $share = $this->createShare(
705
+            101,
706
+            IShare::TYPE_GROUP,
707
+            'groupId',
708
+            'initiatorId',
709
+            'ownerId',
710
+            $folder,
711
+            4,
712
+            5,
713
+            null,
714
+            6,
715
+            'target',
716
+            0,
717
+            'personal note',
718
+            $shareAttributes,
719
+        );
720
+        $expected = [
721
+            'id' => 101,
722
+            'share_type' => IShare::TYPE_GROUP,
723
+            'share_with' => 'groupId',
724
+            'share_with_displayname' => 'groupId',
725
+            'uid_owner' => 'initiatorId',
726
+            'displayname_owner' => 'initiatorDisplay',
727
+            'item_type' => 'folder',
728
+            'item_source' => 2,
729
+            'file_source' => 2,
730
+            'file_target' => 'target',
731
+            'file_parent' => 3,
732
+            'token' => null,
733
+            'expiration' => null,
734
+            'permissions' => 4,
735
+            'attributes' => $shareAttributesReturnJson,
736
+            'stime' => 5,
737
+            'parent' => null,
738
+            'storage_id' => 'STORAGE',
739
+            'path' => 'folder',
740
+            'storage' => 101,
741
+            'mail_send' => 0,
742
+            'uid_file_owner' => 'ownerId',
743
+            'note' => 'personal note',
744
+            'label' => '',
745
+            'displayname_file_owner' => 'ownerDisplay',
746
+            'mimetype' => 'myFolderMimeType',
747
+            'has_preview' => false,
748
+            'hide_download' => 0,
749
+            'can_edit' => false,
750
+            'can_delete' => false,
751
+            'item_size' => 123465,
752
+            'item_mtime' => 1234567890,
753
+            'attributes' => null,
754
+            'item_permissions' => 4,
755
+            'is-mount-root' => false,
756
+            'mount-type' => '',
757
+        ];
758
+        $data[] = [$share, $expected];
759
+
760
+        // File shared by link with Expire
761
+        $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
762
+        $share = $this->createShare(
763
+            101,
764
+            IShare::TYPE_LINK,
765
+            null,
766
+            'initiatorId',
767
+            'ownerId',
768
+            $folder,
769
+            4,
770
+            5,
771
+            $expire,
772
+            6,
773
+            'target',
774
+            0,
775
+            'personal note',
776
+            'token',
777
+            'password',
778
+            'first link share'
779
+        );
780
+        $expected = [
781
+            'id' => 101,
782
+            'share_type' => IShare::TYPE_LINK,
783
+            'password' => 'password',
784
+            'share_with' => 'password',
785
+            'share_with_displayname' => '(Shared link)',
786
+            'send_password_by_talk' => false,
787
+            'uid_owner' => 'initiatorId',
788
+            'displayname_owner' => 'initiatorDisplay',
789
+            'item_type' => 'folder',
790
+            'item_source' => 2,
791
+            'file_source' => 2,
792
+            'file_target' => 'target',
793
+            'file_parent' => 3,
794
+            'token' => 'token',
795
+            'expiration' => '2000-01-02 00:00:00',
796
+            'permissions' => 4,
797
+            'attributes' => null,
798
+            'stime' => 5,
799
+            'parent' => null,
800
+            'storage_id' => 'STORAGE',
801
+            'path' => 'folder',
802
+            'storage' => 101,
803
+            'mail_send' => 0,
804
+            'url' => 'url',
805
+            'uid_file_owner' => 'ownerId',
806
+            'note' => 'personal note',
807
+            'label' => 'first link share',
808
+            'displayname_file_owner' => 'ownerDisplay',
809
+            'mimetype' => 'myFolderMimeType',
810
+            'has_preview' => false,
811
+            'hide_download' => 0,
812
+            'can_edit' => false,
813
+            'can_delete' => false,
814
+            'item_size' => 123465,
815
+            'item_mtime' => 1234567890,
816
+            'attributes' => null,
817
+            'item_permissions' => 4,
818
+            'is-mount-root' => false,
819
+            'mount-type' => '',
820
+        ];
821
+        $data[] = [$share, $expected];
822
+
823
+        return $data;
824
+    }
825
+
826
+    /**
827
+     * @dataProvider dataGetShare
828
+     */
829
+    public function testGetShare(IShare $share, array $result): void {
830
+        /** @var ShareAPIController&MockObject $ocs */
831
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
832
+            ->setConstructorArgs([
833
+                $this->appName,
834
+                $this->request,
835
+                $this->shareManager,
836
+                $this->groupManager,
837
+                $this->userManager,
838
+                $this->rootFolder,
839
+                $this->urlGenerator,
840
+                $this->l,
841
+                $this->config,
842
+                $this->appManager,
843
+                $this->serverContainer,
844
+                $this->userStatusManager,
845
+                $this->previewManager,
846
+                $this->dateTimeZone,
847
+                $this->logger,
848
+                $this->factory,
849
+                $this->mailer,
850
+                $this->tagManager,
851
+                $this->currentUser,
852
+            ])
853
+            ->onlyMethods(['canAccessShare'])
854
+            ->getMock();
855
+
856
+        $ocs->expects($this->any())
857
+            ->method('canAccessShare')
858
+            ->willReturn(true);
859
+
860
+        $this->shareManager
861
+            ->expects($this->any())
862
+            ->method('getShareById')
863
+            ->with($share->getFullId(), 'currentUser')
864
+            ->willReturn($share);
865
+
866
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
867
+        $userFolder
868
+            ->method('getRelativePath')
869
+            ->willReturnArgument(0);
870
+
871
+        $userFolder->method('getById')
872
+            ->with($share->getNodeId())
873
+            ->willReturn([$share->getNode()]);
874
+        $userFolder->method('getFirstNodeById')
875
+            ->with($share->getNodeId())
876
+            ->willReturn($share->getNode());
877
+
878
+        $this->rootFolder->method('getUserFolder')
879
+            ->with($this->currentUser)
880
+            ->willReturn($userFolder);
881
+
882
+        $this->urlGenerator
883
+            ->method('linkToRouteAbsolute')
884
+            ->willReturn('url');
885
+
886
+        $initiator = $this->getMockBuilder(IUser::class)->getMock();
887
+        $initiator->method('getUID')->willReturn('initiatorId');
888
+        $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
889
+
890
+        $owner = $this->getMockBuilder(IUser::class)->getMock();
891
+        $owner->method('getUID')->willReturn('ownerId');
892
+        $owner->method('getDisplayName')->willReturn('ownerDisplay');
893
+
894
+        $user = $this->getMockBuilder(IUser::class)->getMock();
895
+        $user->method('getUID')->willReturn('userId');
896
+        $user->method('getDisplayName')->willReturn('userDisplay');
897
+        $user->method('getSystemEMailAddress')->willReturn('[email protected]');
898
+
899
+        $group = $this->getMockBuilder(IGroup::class)->getMock();
900
+        $group->method('getGID')->willReturn('groupId');
901
+
902
+        $this->userManager->method('get')->willReturnMap([
903
+            ['userId', $user],
904
+            ['initiatorId', $initiator],
905
+            ['ownerId', $owner],
906
+        ]);
907
+        $this->groupManager->method('get')->willReturnMap([
908
+            ['group', $group],
909
+        ]);
910
+        $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
911
+
912
+        $data = $ocs->getShare($share->getId())->getData()[0];
913
+        $this->assertEquals($result, $data);
914
+    }
915
+
916
+
917
+    public function testGetShareInvalidNode(): void {
918
+        $this->expectException(OCSNotFoundException::class);
919
+        $this->expectExceptionMessage('Wrong share ID, share does not exist');
920
+
921
+        $share = Server::get(IManager::class)->newShare();
922
+        $share->setSharedBy('initiator')
923
+            ->setSharedWith('recipient')
924
+            ->setShareOwner('owner');
925
+
926
+        $this->shareManager
927
+            ->expects($this->once())
928
+            ->method('getShareById')
929
+            ->with('ocinternal:42', 'currentUser')
930
+            ->willReturn($share);
931
+
932
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
933
+        $this->rootFolder->method('getUserFolder')
934
+            ->with($this->currentUser)
935
+            ->willReturn($userFolder);
936
+
937
+        $this->ocs->getShare(42);
938
+    }
939
+
940
+    public function dataGetShares() {
941
+        $folder = $this->getMockBuilder(Folder::class)->getMock();
942
+        $file1 = $this->getMockBuilder(File::class)->getMock();
943
+        $file1->method('getName')
944
+            ->willReturn('file1');
945
+        $file2 = $this->getMockBuilder(File::class)->getMock();
946
+        $file2->method('getName')
947
+            ->willReturn('file2');
948
+
949
+        $folder->method('getDirectoryListing')
950
+            ->willReturn([$file1, $file2]);
951
+
952
+        $file1UserShareOwner = Server::get(IManager::class)->newShare();
953
+        $file1UserShareOwner->setShareType(IShare::TYPE_USER)
954
+            ->setSharedWith('recipient')
955
+            ->setSharedBy('initiator')
956
+            ->setShareOwner('currentUser')
957
+            ->setPermissions(Constants::PERMISSION_READ)
958
+            ->setNode($file1)
959
+            ->setId(4);
960
+
961
+        $file1UserShareOwnerExpected = [
962
+            'id' => 4,
963
+            'share_type' => IShare::TYPE_USER,
964
+        ];
965
+
966
+        $file1UserShareInitiator = Server::get(IManager::class)->newShare();
967
+        $file1UserShareInitiator->setShareType(IShare::TYPE_USER)
968
+            ->setSharedWith('recipient')
969
+            ->setSharedBy('currentUser')
970
+            ->setShareOwner('owner')
971
+            ->setPermissions(Constants::PERMISSION_READ)
972
+            ->setNode($file1)
973
+            ->setId(8);
974
+
975
+        $file1UserShareInitiatorExpected = [
976
+            'id' => 8,
977
+            'share_type' => IShare::TYPE_USER,
978
+        ];
979
+
980
+        $file1UserShareRecipient = Server::get(IManager::class)->newShare();
981
+        $file1UserShareRecipient->setShareType(IShare::TYPE_USER)
982
+            ->setSharedWith('currentUser')
983
+            ->setSharedBy('initiator')
984
+            ->setShareOwner('owner')
985
+            ->setPermissions(Constants::PERMISSION_READ)
986
+            ->setNode($file1)
987
+            ->setId(15);
988
+
989
+        $file1UserShareRecipientExpected = [
990
+            'id' => 15,
991
+            'share_type' => IShare::TYPE_USER,
992
+        ];
993
+
994
+        $file1UserShareOther = Server::get(IManager::class)->newShare();
995
+        $file1UserShareOther->setShareType(IShare::TYPE_USER)
996
+            ->setSharedWith('recipient')
997
+            ->setSharedBy('initiator')
998
+            ->setShareOwner('owner')
999
+            ->setPermissions(Constants::PERMISSION_READ)
1000
+            ->setNode($file1)
1001
+            ->setId(16);
1002
+
1003
+        $file1UserShareOtherExpected = [
1004
+            'id' => 16,
1005
+            'share_type' => IShare::TYPE_USER,
1006
+        ];
1007
+
1008
+        $file1GroupShareOwner = Server::get(IManager::class)->newShare();
1009
+        $file1GroupShareOwner->setShareType(IShare::TYPE_GROUP)
1010
+            ->setSharedWith('recipient')
1011
+            ->setSharedBy('initiator')
1012
+            ->setShareOwner('currentUser')
1013
+            ->setPermissions(Constants::PERMISSION_READ)
1014
+            ->setNode($file1)
1015
+            ->setId(23);
1016
+
1017
+        $file1GroupShareOwnerExpected = [
1018
+            'id' => 23,
1019
+            'share_type' => IShare::TYPE_GROUP,
1020
+        ];
1021
+
1022
+        $file1GroupShareRecipient = Server::get(IManager::class)->newShare();
1023
+        $file1GroupShareRecipient->setShareType(IShare::TYPE_GROUP)
1024
+            ->setSharedWith('currentUserGroup')
1025
+            ->setSharedBy('initiator')
1026
+            ->setShareOwner('owner')
1027
+            ->setPermissions(Constants::PERMISSION_READ)
1028
+            ->setNode($file1)
1029
+            ->setId(42);
1030
+
1031
+        $file1GroupShareRecipientExpected = [
1032
+            'id' => 42,
1033
+            'share_type' => IShare::TYPE_GROUP,
1034
+        ];
1035
+
1036
+        $file1GroupShareOther = Server::get(IManager::class)->newShare();
1037
+        $file1GroupShareOther->setShareType(IShare::TYPE_GROUP)
1038
+            ->setSharedWith('recipient')
1039
+            ->setSharedBy('initiator')
1040
+            ->setShareOwner('owner')
1041
+            ->setPermissions(Constants::PERMISSION_READ)
1042
+            ->setNode($file1)
1043
+            ->setId(108);
1044
+
1045
+        $file1LinkShareOwner = Server::get(IManager::class)->newShare();
1046
+        $file1LinkShareOwner->setShareType(IShare::TYPE_LINK)
1047
+            ->setSharedWith('recipient')
1048
+            ->setSharedBy('initiator')
1049
+            ->setShareOwner('currentUser')
1050
+            ->setPermissions(Constants::PERMISSION_READ)
1051
+            ->setNode($file1)
1052
+            ->setId(415);
1053
+
1054
+        $file1LinkShareOwnerExpected = [
1055
+            'id' => 415,
1056
+            'share_type' => IShare::TYPE_LINK,
1057
+        ];
1058
+
1059
+        $file1EmailShareOwner = Server::get(IManager::class)->newShare();
1060
+        $file1EmailShareOwner->setShareType(IShare::TYPE_EMAIL)
1061
+            ->setSharedWith('recipient')
1062
+            ->setSharedBy('initiator')
1063
+            ->setShareOwner('currentUser')
1064
+            ->setPermissions(Constants::PERMISSION_READ)
1065
+            ->setNode($file1)
1066
+            ->setId(416);
1067
+
1068
+        $file1EmailShareOwnerExpected = [
1069
+            'id' => 416,
1070
+            'share_type' => IShare::TYPE_EMAIL,
1071
+        ];
1072
+
1073
+        $file1CircleShareOwner = Server::get(IManager::class)->newShare();
1074
+        $file1CircleShareOwner->setShareType(IShare::TYPE_CIRCLE)
1075
+            ->setSharedWith('recipient')
1076
+            ->setSharedBy('initiator')
1077
+            ->setShareOwner('currentUser')
1078
+            ->setPermissions(Constants::PERMISSION_READ)
1079
+            ->setNode($file1)
1080
+            ->setId(423);
1081
+
1082
+        $file1CircleShareOwnerExpected = [
1083
+            'id' => 423,
1084
+            'share_type' => IShare::TYPE_CIRCLE,
1085
+        ];
1086
+
1087
+        $file1RoomShareOwner = Server::get(IManager::class)->newShare();
1088
+        $file1RoomShareOwner->setShareType(IShare::TYPE_ROOM)
1089
+            ->setSharedWith('recipient')
1090
+            ->setSharedBy('initiator')
1091
+            ->setShareOwner('currentUser')
1092
+            ->setPermissions(Constants::PERMISSION_READ)
1093
+            ->setNode($file1)
1094
+            ->setId(442);
1095
+
1096
+        $file1RoomShareOwnerExpected = [
1097
+            'id' => 442,
1098
+            'share_type' => IShare::TYPE_ROOM,
1099
+        ];
1100
+
1101
+        $file1RemoteShareOwner = Server::get(IManager::class)->newShare();
1102
+        $file1RemoteShareOwner->setShareType(IShare::TYPE_REMOTE)
1103
+            ->setSharedWith('recipient')
1104
+            ->setSharedBy('initiator')
1105
+            ->setShareOwner('currentUser')
1106
+            ->setPermissions(Constants::PERMISSION_READ)
1107
+            ->setExpirationDate(new \DateTime('2000-01-01T01:02:03'))
1108
+            ->setNode($file1)
1109
+            ->setId(815);
1110
+
1111
+        $file1RemoteShareOwnerExpected = [
1112
+            'id' => 815,
1113
+            'share_type' => IShare::TYPE_REMOTE,
1114
+        ];
1115
+
1116
+        $file1RemoteGroupShareOwner = Server::get(IManager::class)->newShare();
1117
+        $file1RemoteGroupShareOwner->setShareType(IShare::TYPE_REMOTE_GROUP)
1118
+            ->setSharedWith('recipient')
1119
+            ->setSharedBy('initiator')
1120
+            ->setShareOwner('currentUser')
1121
+            ->setPermissions(Constants::PERMISSION_READ)
1122
+            ->setExpirationDate(new \DateTime('2000-01-02T01:02:03'))
1123
+            ->setNode($file1)
1124
+            ->setId(816);
1125
+
1126
+        $file1RemoteGroupShareOwnerExpected = [
1127
+            'id' => 816,
1128
+            'share_type' => IShare::TYPE_REMOTE_GROUP,
1129
+        ];
1130
+
1131
+        $file2UserShareOwner = Server::get(IManager::class)->newShare();
1132
+        $file2UserShareOwner->setShareType(IShare::TYPE_USER)
1133
+            ->setSharedWith('recipient')
1134
+            ->setSharedBy('initiator')
1135
+            ->setShareOwner('currentUser')
1136
+            ->setPermissions(Constants::PERMISSION_READ)
1137
+            ->setNode($file2)
1138
+            ->setId(823);
1139
+
1140
+        $file2UserShareOwnerExpected = [
1141
+            'id' => 823,
1142
+            'share_type' => IShare::TYPE_USER,
1143
+        ];
1144
+
1145
+        $data = [
1146
+            [
1147
+                [
1148
+                    'path' => $file1,
1149
+                ],
1150
+                [
1151
+                    'file1' => [
1152
+                        IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner],
1153
+                    ],
1154
+                ],
1155
+                [
1156
+                ],
1157
+                [
1158
+                    $file1UserShareOwnerExpected
1159
+                ]
1160
+            ],
1161
+            [
1162
+                [
1163
+                    'path' => $file1,
1164
+                ],
1165
+                [
1166
+                    'file1' => [
1167
+                        IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient],
1168
+                    ],
1169
+                ],
1170
+                [
1171
+                ],
1172
+                [
1173
+                    $file1UserShareOwnerExpected,
1174
+                ]
1175
+            ],
1176
+            [
1177
+                [
1178
+                    'path' => $file1,
1179
+                ],
1180
+                [
1181
+                    'file1' => [
1182
+                        IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1183
+                    ],
1184
+                ],
1185
+                [
1186
+                ],
1187
+                [
1188
+                    $file1UserShareOwnerExpected,
1189
+                    $file1UserShareInitiatorExpected,
1190
+                    $file1UserShareOtherExpected,
1191
+                ]
1192
+            ],
1193
+            [
1194
+                [
1195
+                    'path' => $file1,
1196
+                ],
1197
+                [
1198
+                    'file1' => [
1199
+                        IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1200
+                    ],
1201
+                ],
1202
+                [
1203
+                ],
1204
+                [
1205
+                    $file1UserShareInitiatorExpected,
1206
+                ]
1207
+            ],
1208
+            [
1209
+                [
1210
+                    'path' => $file1,
1211
+                ],
1212
+                [
1213
+                    'file1' => [
1214
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1215
+                        IShare::TYPE_GROUP => [$file1GroupShareRecipient],
1216
+                    ],
1217
+                ],
1218
+                [
1219
+                ],
1220
+                [
1221
+                    $file1UserShareOwnerExpected,
1222
+                    $file1GroupShareRecipientExpected,
1223
+                ]
1224
+            ],
1225
+            [
1226
+                [
1227
+                    'path' => $file1,
1228
+                ],
1229
+                [
1230
+                    'file1' => [
1231
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1232
+                        IShare::TYPE_GROUP => [$file1GroupShareOwner],
1233
+                        IShare::TYPE_LINK => [$file1LinkShareOwner],
1234
+                        IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1235
+                        IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1236
+                        IShare::TYPE_ROOM => [$file1RoomShareOwner],
1237
+                        IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1238
+                        IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1239
+                    ],
1240
+                ],
1241
+                [
1242
+                ],
1243
+                [
1244
+                    $file1UserShareOwnerExpected,
1245
+                    $file1GroupShareOwnerExpected,
1246
+                    $file1LinkShareOwnerExpected,
1247
+                    $file1EmailShareOwnerExpected,
1248
+                    $file1CircleShareOwnerExpected,
1249
+                    $file1RoomShareOwnerExpected,
1250
+                ]
1251
+            ],
1252
+            [
1253
+                [
1254
+                    'path' => $file1,
1255
+                ],
1256
+                [
1257
+                    'file1' => [
1258
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1259
+                        IShare::TYPE_GROUP => [$file1GroupShareOwner],
1260
+                        IShare::TYPE_LINK => [$file1LinkShareOwner],
1261
+                        IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1262
+                        IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1263
+                        IShare::TYPE_ROOM => [$file1RoomShareOwner],
1264
+                        IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1265
+                        IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1266
+                    ],
1267
+                ],
1268
+                [
1269
+                    IShare::TYPE_REMOTE => true,
1270
+                    IShare::TYPE_REMOTE_GROUP => true,
1271
+                ],
1272
+                [
1273
+                    $file1UserShareOwnerExpected,
1274
+                    $file1GroupShareOwnerExpected,
1275
+                    $file1LinkShareOwnerExpected,
1276
+                    $file1EmailShareOwnerExpected,
1277
+                    $file1CircleShareOwnerExpected,
1278
+                    $file1RoomShareOwnerExpected,
1279
+                    $file1RemoteShareOwnerExpected,
1280
+                    $file1RemoteGroupShareOwnerExpected,
1281
+                ]
1282
+            ],
1283
+            [
1284
+                [
1285
+                    'path' => $folder,
1286
+                    'subfiles' => 'true',
1287
+                ],
1288
+                [
1289
+                    'file1' => [
1290
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1291
+                    ],
1292
+                    'file2' => [
1293
+                        IShare::TYPE_USER => [$file2UserShareOwner],
1294
+                    ],
1295
+                ],
1296
+                [
1297
+                ],
1298
+                [
1299
+                    $file1UserShareOwnerExpected,
1300
+                    $file2UserShareOwnerExpected,
1301
+                ]
1302
+            ],
1303
+            [
1304
+                [
1305
+                    'path' => $folder,
1306
+                    'subfiles' => 'true',
1307
+                ],
1308
+                [
1309
+                    'file1' => [
1310
+                        IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner],
1311
+                    ],
1312
+                ],
1313
+                [
1314
+                ],
1315
+                [
1316
+                    $file1UserShareOwnerExpected,
1317
+                ]
1318
+            ],
1319
+            [
1320
+                [
1321
+                    'path' => $folder,
1322
+                    'subfiles' => 'true',
1323
+                ],
1324
+                [
1325
+                    'file1' => [
1326
+                        IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient],
1327
+                    ],
1328
+                ],
1329
+                [
1330
+                ],
1331
+                [
1332
+                    $file1UserShareOwnerExpected
1333
+                ]
1334
+            ],
1335
+            [
1336
+                [
1337
+                    'path' => $folder,
1338
+                    'subfiles' => 'true',
1339
+                ],
1340
+                [
1341
+                    'file1' => [
1342
+                        IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1343
+                    ],
1344
+                    'file2' => [
1345
+                        IShare::TYPE_USER => [$file2UserShareOwner],
1346
+                    ],
1347
+                ],
1348
+                [
1349
+                ],
1350
+                [
1351
+                    $file1UserShareInitiatorExpected,
1352
+                    $file1UserShareOtherExpected,
1353
+                    $file2UserShareOwnerExpected,
1354
+                ]
1355
+            ],
1356
+            // This might not happen in a real environment, as the combination
1357
+            // of shares does not seem to be possible on a folder without
1358
+            // resharing rights; if the folder has resharing rights then the
1359
+            // share with others would be included too in the results.
1360
+            [
1361
+                [
1362
+                    'path' => $folder,
1363
+                    'subfiles' => 'true',
1364
+                ],
1365
+                [
1366
+                    'file1' => [
1367
+                        IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
1368
+                    ],
1369
+                ],
1370
+                [
1371
+                ],
1372
+                [
1373
+                    $file1UserShareInitiatorExpected,
1374
+                ]
1375
+            ],
1376
+            [
1377
+                [
1378
+                    'path' => $folder,
1379
+                    'subfiles' => 'true',
1380
+                ],
1381
+                [
1382
+                    'file1' => [
1383
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1384
+                        IShare::TYPE_GROUP => [$file1GroupShareRecipient],
1385
+                    ],
1386
+                ],
1387
+                [
1388
+                ],
1389
+                [
1390
+                    $file1UserShareOwnerExpected,
1391
+                    $file1GroupShareRecipientExpected,
1392
+                ]
1393
+            ],
1394
+            [
1395
+                [
1396
+                    'path' => $folder,
1397
+                    'subfiles' => 'true',
1398
+                ],
1399
+                [
1400
+                    'file1' => [
1401
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1402
+                        IShare::TYPE_GROUP => [$file1GroupShareOwner],
1403
+                        IShare::TYPE_LINK => [$file1LinkShareOwner],
1404
+                        IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1405
+                        IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1406
+                        IShare::TYPE_ROOM => [$file1RoomShareOwner],
1407
+                        IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1408
+                        IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1409
+                    ],
1410
+                ],
1411
+                [
1412
+                ],
1413
+                [
1414
+                    $file1UserShareOwnerExpected,
1415
+                    $file1GroupShareOwnerExpected,
1416
+                    $file1LinkShareOwnerExpected,
1417
+                    $file1EmailShareOwnerExpected,
1418
+                    $file1CircleShareOwnerExpected,
1419
+                    $file1RoomShareOwnerExpected,
1420
+                ]
1421
+            ],
1422
+            [
1423
+                [
1424
+                    'path' => $folder,
1425
+                    'subfiles' => 'true',
1426
+                ],
1427
+                [
1428
+                    'file1' => [
1429
+                        IShare::TYPE_USER => [$file1UserShareOwner],
1430
+                        IShare::TYPE_GROUP => [$file1GroupShareOwner],
1431
+                        IShare::TYPE_LINK => [$file1LinkShareOwner],
1432
+                        IShare::TYPE_EMAIL => [$file1EmailShareOwner],
1433
+                        IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
1434
+                        IShare::TYPE_ROOM => [$file1RoomShareOwner],
1435
+                        IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
1436
+                        IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
1437
+                    ],
1438
+                ],
1439
+                [
1440
+                    IShare::TYPE_REMOTE => true,
1441
+                    IShare::TYPE_REMOTE_GROUP => true,
1442
+                ],
1443
+                [
1444
+                    $file1UserShareOwnerExpected,
1445
+                    $file1GroupShareOwnerExpected,
1446
+                    $file1LinkShareOwnerExpected,
1447
+                    $file1EmailShareOwnerExpected,
1448
+                    $file1CircleShareOwnerExpected,
1449
+                    $file1RoomShareOwnerExpected,
1450
+                    $file1RemoteShareOwnerExpected,
1451
+                    $file1RemoteGroupShareOwnerExpected,
1452
+                ]
1453
+            ],
1454
+        ];
1455
+
1456
+        return $data;
1457
+    }
1458
+
1459
+    /**
1460
+     * @dataProvider dataGetShares
1461
+     */
1462
+    public function testGetShares(array $getSharesParameters, array $shares, array $extraShareTypes, array $expected): void {
1463
+        /** @var ShareAPIController&MockObject $ocs */
1464
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
1465
+            ->setConstructorArgs([
1466
+                $this->appName,
1467
+                $this->request,
1468
+                $this->shareManager,
1469
+                $this->groupManager,
1470
+                $this->userManager,
1471
+                $this->rootFolder,
1472
+                $this->urlGenerator,
1473
+                $this->l,
1474
+                $this->config,
1475
+                $this->appManager,
1476
+                $this->serverContainer,
1477
+                $this->userStatusManager,
1478
+                $this->previewManager,
1479
+                $this->dateTimeZone,
1480
+                $this->logger,
1481
+                $this->factory,
1482
+                $this->mailer,
1483
+                $this->tagManager,
1484
+                $this->currentUser,
1485
+            ])
1486
+            ->onlyMethods(['formatShare'])
1487
+            ->getMock();
1488
+
1489
+        $ocs->method('formatShare')
1490
+            ->willReturnCallback(
1491
+                function ($share) {
1492
+                    return [
1493
+                        'id' => $share->getId(),
1494
+                        'share_type' => $share->getShareType()
1495
+                    ];
1496
+                }
1497
+            );
1498
+
1499
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1500
+        $userFolder->method('get')
1501
+            ->with('path')
1502
+            ->willReturn($getSharesParameters['path']);
1503
+
1504
+        $this->rootFolder->method('getUserFolder')
1505
+            ->with($this->currentUser)
1506
+            ->willReturn($userFolder);
1507
+
1508
+        $this->shareManager
1509
+            ->method('getSharesBy')
1510
+            ->willReturnCallback(
1511
+                function ($user, $shareType, $node) use ($shares) {
1512
+                    if (!isset($shares[$node->getName()]) || !isset($shares[$node->getName()][$shareType])) {
1513
+                        return [];
1514
+                    }
1515
+                    return $shares[$node->getName()][$shareType];
1516
+                }
1517
+            );
1518
+
1519
+        $this->shareManager
1520
+            ->method('outgoingServer2ServerSharesAllowed')
1521
+            ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE] ?? false);
1522
+
1523
+        $this->shareManager
1524
+            ->method('outgoingServer2ServerGroupSharesAllowed')
1525
+            ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE_GROUP] ?? false);
1526
+
1527
+        $this->groupManager
1528
+            ->method('isInGroup')
1529
+            ->willReturnCallback(
1530
+                function ($user, $group) {
1531
+                    return $group === 'currentUserGroup';
1532
+                }
1533
+            );
1534
+
1535
+        $result = $ocs->getShares(
1536
+            $getSharesParameters['sharedWithMe'] ?? 'false',
1537
+            $getSharesParameters['reshares'] ?? 'false',
1538
+            $getSharesParameters['subfiles'] ?? 'false',
1539
+            'path'
1540
+        );
1541
+
1542
+        $this->assertEquals($expected, $result->getData());
1543
+    }
1544
+
1545
+    public function testCanAccessShare(): void {
1546
+        $share = $this->getMockBuilder(IShare::class)->getMock();
1547
+        $share->method('getShareOwner')->willReturn($this->currentUser);
1548
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1549
+
1550
+        $share = $this->getMockBuilder(IShare::class)->getMock();
1551
+        $share->method('getSharedBy')->willReturn($this->currentUser);
1552
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1553
+
1554
+        $share = $this->getMockBuilder(IShare::class)->getMock();
1555
+        $share->method('getShareType')->willReturn(IShare::TYPE_USER);
1556
+        $share->method('getSharedWith')->willReturn($this->currentUser);
1557
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1558
+
1559
+        $file = $this->getMockBuilder(File::class)->getMock();
1560
+
1561
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1562
+        $this->rootFolder->method('getUserFolder')
1563
+            ->with($this->currentUser)
1564
+            ->willReturn($userFolder);
1565
+
1566
+        $userFolder->method('getFirstNodeById')
1567
+            ->with($share->getNodeId())
1568
+            ->willReturn($file);
1569
+        $userFolder->method('getById')
1570
+            ->with($share->getNodeId())
1571
+            ->willReturn([$file]);
1572
+
1573
+        $file->method('getPermissions')
1574
+            ->will($this->onConsecutiveCalls(Constants::PERMISSION_SHARE, Constants::PERMISSION_READ));
1575
+
1576
+        // getPermissions -> share
1577
+        $share = $this->getMockBuilder(IShare::class)->getMock();
1578
+        $share->method('getShareType')->willReturn(IShare::TYPE_USER);
1579
+        $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
1580
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1581
+
1582
+        // getPermissions -> read
1583
+        $share = $this->getMockBuilder(IShare::class)->getMock();
1584
+        $share->method('getShareType')->willReturn(IShare::TYPE_USER);
1585
+        $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
1586
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1587
+
1588
+        $share = $this->getMockBuilder(IShare::class)->getMock();
1589
+        $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1590
+        $share->method('getSharedWith')->willReturn('group');
1591
+
1592
+        $user = $this->createMock(IUser::class);
1593
+        $this->userManager->method('get')
1594
+            ->with($this->currentUser)
1595
+            ->willReturn($user);
1596
+
1597
+        $group = $this->getMockBuilder(IGroup::class)->getMock();
1598
+        $group->method('inGroup')->with($user)->willReturn(true);
1599
+        $group2 = $this->getMockBuilder(IGroup::class)->getMock();
1600
+        $group2->method('inGroup')->with($user)->willReturn(false);
1601
+
1602
+        $this->groupManager->method('get')->willReturnMap([
1603
+            ['group', $group],
1604
+            ['group2', $group2],
1605
+            ['groupnull', null],
1606
+        ]);
1607
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1608
+
1609
+        $share = $this->createMock(IShare::class);
1610
+        $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1611
+        $share->method('getSharedWith')->willReturn('group2');
1612
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1613
+
1614
+        // null group
1615
+        $share = $this->createMock(IShare::class);
1616
+        $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1617
+        $share->method('getSharedWith')->willReturn('groupnull');
1618
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1619
+
1620
+        $share = $this->createMock(IShare::class);
1621
+        $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
1622
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1623
+    }
1624
+
1625
+    public function dataCanAccessRoomShare() {
1626
+        $result = [];
1627
+
1628
+        $share = $this->createMock(IShare::class);
1629
+        $share->method('getShareType')->willReturn(IShare::TYPE_ROOM);
1630
+        $share->method('getSharedWith')->willReturn('recipientRoom');
1631
+
1632
+        $result[] = [
1633
+            false, $share, false, false
1634
+        ];
1635
+
1636
+        $result[] = [
1637
+            false, $share, false, true
1638
+        ];
1639
+
1640
+        $result[] = [
1641
+            true, $share, true, true
1642
+        ];
1643
+
1644
+        $result[] = [
1645
+            false, $share, true, false
1646
+        ];
1647
+
1648
+        return $result;
1649
+    }
1650
+
1651
+    /**
1652
+     * @dataProvider dataCanAccessRoomShare
1653
+     *
1654
+     * @param bool $expects
1655
+     * @param IShare $share
1656
+     * @param bool helperAvailable
1657
+     * @param bool canAccessShareByHelper
1658
+     */
1659
+    public function testCanAccessRoomShare(bool $expected, IShare $share, bool $helperAvailable, bool $canAccessShareByHelper): void {
1660
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1661
+        $this->rootFolder->method('getUserFolder')
1662
+            ->with($this->currentUser)
1663
+            ->willReturn($userFolder);
1664
+
1665
+        $userFolder->method('getById')
1666
+            ->with($share->getNodeId())
1667
+            ->willReturn([$share->getNode()]);
1668
+
1669
+        if (!$helperAvailable) {
1670
+            $this->appManager->method('isEnabledForUser')
1671
+                ->with('spreed')
1672
+                ->willReturn(false);
1673
+        } else {
1674
+            $this->appManager->method('isEnabledForUser')
1675
+                ->with('spreed')
1676
+                ->willReturn(true);
1677
+
1678
+            // This is not possible anymore with PHPUnit 10+
1679
+            // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
1680
+            // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
1681
+            $helper = $this->getMockBuilder(\stdClass::class)
1682
+                ->addMethods(['canAccessShare'])
1683
+                ->getMock();
1684
+            $helper->method('canAccessShare')
1685
+                ->with($share, $this->currentUser)
1686
+                ->willReturn($canAccessShareByHelper);
1687
+
1688
+            $this->serverContainer->method('get')
1689
+                ->with('\OCA\Talk\Share\Helper\ShareAPIController')
1690
+                ->willReturn($helper);
1691
+        }
1692
+
1693
+        $this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1694
+    }
1695
+
1696
+
1697
+    public function testCreateShareNoPath(): void {
1698
+        $this->expectException(OCSNotFoundException::class);
1699
+        $this->expectExceptionMessage('Please specify a file or folder path');
1700
+
1701
+        $this->ocs->createShare();
1702
+    }
1703
+
1704
+
1705
+    public function testCreateShareInvalidPath(): void {
1706
+        $this->expectException(OCSNotFoundException::class);
1707
+        $this->expectExceptionMessage('Wrong path, file/folder does not exist');
1708
+
1709
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1710
+        $this->rootFolder->expects($this->once())
1711
+            ->method('getUserFolder')
1712
+            ->with('currentUser')
1713
+            ->willReturn($userFolder);
1714
+
1715
+        $userFolder->expects($this->once())
1716
+            ->method('get')
1717
+            ->with('invalid-path')
1718
+            ->will($this->throwException(new NotFoundException()));
1719
+
1720
+        $this->ocs->createShare('invalid-path');
1721
+    }
1722
+
1723
+    public function testCreateShareInvalidShareType(): void {
1724
+        $this->expectException(OCSBadRequestException::class);
1725
+        $this->expectExceptionMessage('Unknown share type');
1726
+
1727
+        $share = $this->newShare();
1728
+        $this->shareManager->method('newShare')->willReturn($share);
1729
+
1730
+        [$userFolder, $file] = $this->getNonSharedUserFile();
1731
+        $this->rootFolder->expects($this->atLeastOnce())
1732
+            ->method('getUserFolder')
1733
+            ->with('currentUser')
1734
+            ->willReturn($userFolder);
1735
+
1736
+        $userFolder->expects($this->atLeastOnce())
1737
+            ->method('get')
1738
+            ->with('valid-path')
1739
+            ->willReturn($file);
1740
+        $userFolder->method('getById')
1741
+            ->willReturn([]);
1742
+
1743
+        $file->expects($this->once())
1744
+            ->method('lock')
1745
+            ->with(ILockingProvider::LOCK_SHARED);
1746
+
1747
+        $this->ocs->createShare('valid-path', 31);
1748
+    }
1749
+
1750
+    public function testCreateShareUserNoShareWith(): void {
1751
+        $this->expectException(OCSNotFoundException::class);
1752
+        $this->expectExceptionMessage('Please specify a valid account to share with');
1753
+
1754
+        $share = $this->newShare();
1755
+        $this->shareManager->method('newShare')->willReturn($share);
1756
+
1757
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1758
+        $this->rootFolder->method('getUserFolder')
1759
+            ->with('currentUser')
1760
+            ->willReturn($userFolder);
1761
+
1762
+        $userFolder->expects($this->once())
1763
+            ->method('get')
1764
+            ->with('valid-path')
1765
+            ->willReturn($path);
1766
+        $userFolder->method('getById')
1767
+            ->willReturn([]);
1768
+
1769
+        $path->expects($this->once())
1770
+            ->method('lock')
1771
+            ->with(ILockingProvider::LOCK_SHARED);
1772
+
1773
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER);
1774
+    }
1775
+
1776
+
1777
+    public function testCreateShareUserNoValidShareWith(): void {
1778
+        $this->expectException(OCSNotFoundException::class);
1779
+        $this->expectExceptionMessage('Please specify a valid account to share with');
1780
+
1781
+        $share = $this->newShare();
1782
+        $this->shareManager->method('newShare')->willReturn($share);
1783
+
1784
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1785
+        $this->rootFolder->method('getUserFolder')
1786
+            ->with('currentUser')
1787
+            ->willReturn($userFolder);
1788
+
1789
+        $userFolder->expects($this->once())
1790
+            ->method('get')
1791
+            ->with('valid-path')
1792
+            ->willReturn($path);
1793
+        $userFolder->method('getById')
1794
+            ->willReturn([]);
1795
+        $path->expects($this->once())
1796
+            ->method('lock')
1797
+            ->with(ILockingProvider::LOCK_SHARED);
1798
+        $this->userManager->method('userExists')
1799
+            ->with('invalidUser')
1800
+            ->willReturn(false);
1801
+
1802
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'invalidUser');
1803
+    }
1804
+
1805
+    public function testCreateShareUser(): void {
1806
+        $share = $this->newShare();
1807
+        $this->shareManager->method('newShare')->willReturn($share);
1808
+
1809
+        /** @var ShareAPIController $ocs */
1810
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
1811
+            ->setConstructorArgs([
1812
+                $this->appName,
1813
+                $this->request,
1814
+                $this->shareManager,
1815
+                $this->groupManager,
1816
+                $this->userManager,
1817
+                $this->rootFolder,
1818
+                $this->urlGenerator,
1819
+                $this->l,
1820
+                $this->config,
1821
+                $this->appManager,
1822
+                $this->serverContainer,
1823
+                $this->userStatusManager,
1824
+                $this->previewManager,
1825
+                $this->dateTimeZone,
1826
+                $this->logger,
1827
+                $this->factory,
1828
+                $this->mailer,
1829
+                $this->tagManager,
1830
+                $this->currentUser,
1831
+            ])->onlyMethods(['formatShare'])
1832
+            ->getMock();
1833
+
1834
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1835
+        $this->rootFolder->expects($this->exactly(2))
1836
+            ->method('getUserFolder')
1837
+            ->with('currentUser')
1838
+            ->willReturn($userFolder);
1839
+
1840
+        $userFolder->expects($this->once())
1841
+            ->method('get')
1842
+            ->with('valid-path')
1843
+            ->willReturn($path);
1844
+        $userFolder->method('getById')
1845
+            ->willReturn([]);
1846
+
1847
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
1848
+
1849
+        $path->expects($this->once())
1850
+            ->method('lock')
1851
+            ->with(ILockingProvider::LOCK_SHARED);
1852
+
1853
+        $this->shareManager->method('createShare')
1854
+            ->with($this->callback(function (IShare $share) use ($path) {
1855
+                return $share->getNode() === $path &&
1856
+                    $share->getPermissions() === (
1857
+                        Constants::PERMISSION_ALL &
1858
+                        ~Constants::PERMISSION_DELETE &
1859
+                        ~Constants::PERMISSION_CREATE
1860
+                    ) &&
1861
+                    $share->getShareType() === IShare::TYPE_USER &&
1862
+                    $share->getSharedWith() === 'validUser' &&
1863
+                    $share->getSharedBy() === 'currentUser';
1864
+            }))
1865
+            ->willReturnArgument(0);
1866
+
1867
+        $expected = new DataResponse([]);
1868
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
1869
+
1870
+        $this->assertInstanceOf(get_class($expected), $result);
1871
+        $this->assertEquals($expected->getData(), $result->getData());
1872
+    }
1873
+
1874
+
1875
+    public function testCreateShareGroupNoValidShareWith(): void {
1876
+        $this->expectException(OCSNotFoundException::class);
1877
+        $this->expectExceptionMessage('Please specify a valid group');
1878
+
1879
+        $share = $this->newShare();
1880
+        $this->shareManager->method('newShare')->willReturn($share);
1881
+        $this->shareManager->method('createShare')->willReturnArgument(0);
1882
+        $this->shareManager->method('allowGroupSharing')->willReturn(true);
1883
+
1884
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1885
+        $this->rootFolder->method('getUserFolder')
1886
+            ->with('currentUser')
1887
+            ->willReturn($userFolder);
1888
+
1889
+        $userFolder->expects($this->once())
1890
+            ->method('get')
1891
+            ->with('valid-path')
1892
+            ->willReturn($path);
1893
+        $userFolder->method('getById')
1894
+            ->willReturn([]);
1895
+
1896
+        $path->expects($this->once())
1897
+            ->method('lock')
1898
+            ->with(ILockingProvider::LOCK_SHARED);
1899
+
1900
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
1901
+    }
1902
+
1903
+    public function testCreateShareGroup(): void {
1904
+        $share = $this->newShare();
1905
+        $this->shareManager->method('newShare')->willReturn($share);
1906
+
1907
+        /** @var ShareAPIController&MockObject $ocs */
1908
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
1909
+            ->setConstructorArgs([
1910
+                $this->appName,
1911
+                $this->request,
1912
+                $this->shareManager,
1913
+                $this->groupManager,
1914
+                $this->userManager,
1915
+                $this->rootFolder,
1916
+                $this->urlGenerator,
1917
+                $this->l,
1918
+                $this->config,
1919
+                $this->appManager,
1920
+                $this->serverContainer,
1921
+                $this->userStatusManager,
1922
+                $this->previewManager,
1923
+                $this->dateTimeZone,
1924
+                $this->logger,
1925
+                $this->factory,
1926
+                $this->mailer,
1927
+                $this->tagManager,
1928
+                $this->currentUser,
1929
+            ])->onlyMethods(['formatShare'])
1930
+            ->getMock();
1931
+
1932
+        $this->request
1933
+            ->method('getParam')
1934
+            ->willReturnMap([
1935
+                ['path', null, 'valid-path'],
1936
+                ['permissions', null, Constants::PERMISSION_ALL],
1937
+                ['shareType', '-1', IShare::TYPE_GROUP],
1938
+                ['shareWith', null, 'validGroup'],
1939
+            ]);
1940
+
1941
+        [$userFolder, $path] = $this->getNonSharedUserFolder();
1942
+        $this->rootFolder->expects($this->exactly(2))
1943
+            ->method('getUserFolder')
1944
+            ->with('currentUser')
1945
+            ->willReturn($userFolder);
1946
+
1947
+        $userFolder->expects($this->once())
1948
+            ->method('get')
1949
+            ->with('valid-path')
1950
+            ->willReturn($path);
1951
+        $userFolder->method('getById')
1952
+            ->willReturn([]);
1953
+
1954
+        $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
1955
+
1956
+        $this->shareManager->expects($this->once())
1957
+            ->method('allowGroupSharing')
1958
+            ->willReturn(true);
1959
+
1960
+        $path->expects($this->once())
1961
+            ->method('lock')
1962
+            ->with(ILockingProvider::LOCK_SHARED);
1963
+
1964
+        $this->shareManager->method('createShare')
1965
+            ->with($this->callback(function (IShare $share) use ($path) {
1966
+                return $share->getNode() === $path &&
1967
+                $share->getPermissions() === Constants::PERMISSION_ALL &&
1968
+                $share->getShareType() === IShare::TYPE_GROUP &&
1969
+                $share->getSharedWith() === 'validGroup' &&
1970
+                $share->getSharedBy() === 'currentUser';
1971
+            }))
1972
+            ->willReturnArgument(0);
1973
+
1974
+        $expected = new DataResponse([]);
1975
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'validGroup');
1976
+
1977
+        $this->assertInstanceOf(get_class($expected), $result);
1978
+        $this->assertEquals($expected->getData(), $result->getData());
1979
+    }
1980
+
1981
+
1982
+    public function testCreateShareGroupNotAllowed(): void {
1983
+        $this->expectException(OCSNotFoundException::class);
1984
+        $this->expectExceptionMessage('Group sharing is disabled by the administrator');
1985
+
1986
+        $share = $this->newShare();
1987
+        $this->shareManager->method('newShare')->willReturn($share);
1988
+
1989
+        [$userFolder, $path] = $this->getNonSharedUserFolder();
1990
+        $this->rootFolder->method('getUserFolder')
1991
+            ->with('currentUser')
1992
+            ->willReturn($userFolder);
1993
+
1994
+        $userFolder->expects($this->once())
1995
+            ->method('get')
1996
+            ->with('valid-path')
1997
+            ->willReturn($path);
1998
+        $userFolder->method('getById')
1999
+            ->willReturn([]);
2000
+
2001
+        $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
2002
+
2003
+        $this->shareManager->expects($this->once())
2004
+            ->method('allowGroupSharing')
2005
+            ->willReturn(false);
2006
+
2007
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
2008
+    }
2009
+
2010
+
2011
+    public function testCreateShareLinkNoLinksAllowed(): void {
2012
+        $this->expectException(OCSNotFoundException::class);
2013
+        $this->expectExceptionMessage('Public link sharing is disabled by the administrator');
2014
+
2015
+        $this->request
2016
+            ->method('getParam')
2017
+            ->willReturnMap([
2018
+                ['path', null, 'valid-path'],
2019
+                ['shareType', '-1', IShare::TYPE_LINK],
2020
+            ]);
2021
+
2022
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2023
+        $path->method('getId')->willReturn(42);
2024
+        $storage = $this->createMock(IStorage::class);
2025
+        $storage->method('instanceOfStorage')
2026
+            ->willReturnMap([
2027
+                ['OCA\Files_Sharing\External\Storage', false],
2028
+                ['OCA\Files_Sharing\SharedStorage', false],
2029
+            ]);
2030
+        $path->method('getStorage')->willReturn($storage);
2031
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2032
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2033
+        $this->rootFolder->method('getById')
2034
+            ->willReturn([]);
2035
+
2036
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2037
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2038
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(false);
2039
+
2040
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK);
2041
+    }
2042
+
2043
+
2044
+    public function testCreateShareLinkNoPublicUpload(): void {
2045
+        $this->expectException(OCSForbiddenException::class);
2046
+        $this->expectExceptionMessage('Public upload disabled by the administrator');
2047
+
2048
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2049
+        $path->method('getId')->willReturn(42);
2050
+        $storage = $this->createMock(IStorage::class);
2051
+        $storage->method('instanceOfStorage')
2052
+            ->willReturnMap([
2053
+                ['OCA\Files_Sharing\External\Storage', false],
2054
+                ['OCA\Files_Sharing\SharedStorage', false],
2055
+            ]);
2056
+        $path->method('getStorage')->willReturn($storage);
2057
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2058
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2059
+        $this->rootFolder->method('getById')
2060
+            ->willReturn([]);
2061
+
2062
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2063
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2064
+
2065
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2066
+    }
2067
+
2068
+
2069
+    public function testCreateShareLinkPublicUploadFile(): void {
2070
+        $this->expectException(OCSBadRequestException::class);
2071
+        $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
2072
+
2073
+        $storage = $this->createMock(IStorage::class);
2074
+        $storage->method('instanceOfStorage')
2075
+            ->willReturnMap([
2076
+                ['OCA\Files_Sharing\External\Storage', false],
2077
+                ['OCA\Files_Sharing\SharedStorage', false],
2078
+            ]);
2079
+
2080
+        $file = $this->createMock(File::class);
2081
+        $file->method('getId')->willReturn(42);
2082
+        $file->method('getStorage')->willReturn($storage);
2083 2083
 	
2084
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2085
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($file);
2086
-		$this->rootFolder->method('getById')
2087
-			->willReturn([]);
2088
-
2089
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2090
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2091
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2092
-
2093
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2094
-	}
2095
-
2096
-	public function testCreateShareLinkPublicUploadFolder(): void {
2097
-		$ocs = $this->mockFormatShare();
2098
-
2099
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2100
-		$path->method('getId')->willReturn(1);
2101
-		$storage = $this->createMock(IStorage::class);
2102
-		$storage->method('instanceOfStorage')
2103
-			->willReturnMap([
2104
-				['OCA\Files_Sharing\External\Storage', false],
2105
-				['OCA\Files_Sharing\SharedStorage', false],
2106
-			]);
2107
-		$path->method('getStorage')->willReturn($storage);
2108
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2109
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2110
-		$this->rootFolder->method('getById')
2111
-			->willReturn([]);
2112
-
2113
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2114
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2115
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2116
-
2117
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2118
-			$this->callback(function (IShare $share) use ($path) {
2119
-				return $share->getNode() === $path &&
2120
-					$share->getShareType() === IShare::TYPE_LINK &&
2121
-					$share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2122
-					$share->getSharedBy() === 'currentUser' &&
2123
-					$share->getPassword() === null &&
2124
-					$share->getExpirationDate() === null;
2125
-			})
2126
-		)->willReturnArgument(0);
2127
-
2128
-		$expected = new DataResponse([]);
2129
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', '', null, '');
2130
-
2131
-		$this->assertInstanceOf(get_class($expected), $result);
2132
-		$this->assertEquals($expected->getData(), $result->getData());
2133
-	}
2134
-
2135
-	public function testCreateShareLinkPassword(): void {
2136
-		$ocs = $this->mockFormatShare();
2137
-
2138
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2139
-		$path->method('getId')->willReturn(42);
2140
-		$storage = $this->createMock(IStorage::class);
2141
-		$storage->method('instanceOfStorage')
2142
-			->willReturnMap([
2143
-				['OCA\Files_Sharing\External\Storage', false],
2144
-				['OCA\Files_Sharing\SharedStorage', false],
2145
-			]);
2146
-		$path->method('getStorage')->willReturn($storage);
2147
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2148
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2149
-		$this->rootFolder->method('getById')
2150
-			->willReturn([]);
2151
-
2152
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2153
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2154
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2155
-
2156
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2157
-			$this->callback(function (IShare $share) use ($path) {
2158
-				return $share->getNode() === $path
2159
-				&& $share->getShareType() === IShare::TYPE_LINK
2160
-				&& $share->getPermissions() === Constants::PERMISSION_READ // publicUpload was set to false
2161
-				&& $share->getSharedBy() === 'currentUser'
2162
-				&& $share->getPassword() === 'password'
2163
-				&& $share->getExpirationDate() === null;
2164
-			})
2165
-		)->willReturnArgument(0);
2166
-
2167
-		$expected = new DataResponse([]);
2168
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_READ, IShare::TYPE_LINK, null, 'false', 'password', null, '');
2169
-
2170
-		$this->assertInstanceOf(get_class($expected), $result);
2171
-		$this->assertEquals($expected->getData(), $result->getData());
2172
-	}
2173
-
2174
-	public function testCreateShareLinkSendPasswordByTalk(): void {
2175
-		$ocs = $this->mockFormatShare();
2176
-
2177
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2178
-		$path->method('getId')->willReturn(42);
2179
-		$storage = $this->createMock(IStorage::class);
2180
-		$storage->method('instanceOfStorage')
2181
-			->willReturnMap([
2182
-				['OCA\Files_Sharing\External\Storage', false],
2183
-				['OCA\Files_Sharing\SharedStorage', false],
2184
-			]);
2185
-		$path->method('getStorage')->willReturn($storage);
2186
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2187
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2188
-		$this->rootFolder->method('getById')
2189
-			->willReturn([]);
2190
-
2191
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2192
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2193
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2194
-
2195
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
2196
-
2197
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2198
-			$this->callback(function (IShare $share) use ($path) {
2199
-				return $share->getNode() === $path &&
2200
-				$share->getShareType() === IShare::TYPE_LINK &&
2201
-				$share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) &&
2202
-				$share->getSharedBy() === 'currentUser' &&
2203
-				$share->getPassword() === 'password' &&
2204
-				$share->getSendPasswordByTalk() === true &&
2205
-				$share->getExpirationDate() === null;
2206
-			})
2207
-		)->willReturnArgument(0);
2208
-
2209
-		$expected = new DataResponse([]);
2210
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', 'password', 'true', '');
2211
-
2212
-		$this->assertInstanceOf(get_class($expected), $result);
2213
-		$this->assertEquals($expected->getData(), $result->getData());
2214
-	}
2215
-
2216
-
2217
-	public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled(): void {
2218
-		$this->expectException(OCSForbiddenException::class);
2219
-		$this->expectExceptionMessage('Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled');
2220
-
2221
-		$ocs = $this->mockFormatShare();
2222
-
2223
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2224
-		$path->method('getId')->willReturn(42);
2225
-		$storage = $this->createMock(IStorage::class);
2226
-		$storage->method('instanceOfStorage')
2227
-			->willReturnMap([
2228
-				['OCA\Files_Sharing\External\Storage', false],
2229
-				['OCA\Files_Sharing\SharedStorage', false],
2230
-			]);
2231
-		$path->method('getStorage')->willReturn($storage);
2232
-		$path->method('getPath')->willReturn('valid-path');
2233
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2234
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2235
-		$this->rootFolder->method('getById')
2236
-			->willReturn([]);
2237
-
2238
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2239
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2240
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2241
-
2242
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
2243
-
2244
-		$this->shareManager->expects($this->never())->method('createShare');
2245
-
2246
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', '');
2247
-	}
2248
-
2249
-	public function testCreateShareValidExpireDate(): void {
2250
-		$ocs = $this->mockFormatShare();
2251
-
2252
-		$this->request
2253
-			->method('getParam')
2254
-			->willReturnMap([
2255
-				['path', null, 'valid-path'],
2256
-				['shareType', '-1', IShare::TYPE_LINK],
2257
-				['publicUpload', null, 'false'],
2258
-				['expireDate', '', '2000-01-01'],
2259
-				['password', '', ''],
2260
-			]);
2261
-
2262
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2263
-		$path->method('getId')->willReturn(42);
2264
-		$storage = $this->createMock(IStorage::class);
2265
-		$storage->method('instanceOfStorage')
2266
-			->willReturnMap([
2267
-				['OCA\Files_Sharing\External\Storage', false],
2268
-				['OCA\Files_Sharing\SharedStorage', false],
2269
-			]);
2270
-		$path->method('getStorage')->willReturn($storage);
2271
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2272
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2273
-		$this->rootFolder->method('getById')
2274
-			->willReturn([]);
2275
-
2276
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2277
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2278
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2279
-
2280
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2281
-			$this->callback(function (IShare $share) use ($path) {
2282
-				$date = new \DateTime('2000-01-01');
2283
-				$date->setTime(0, 0, 0);
2284
-
2285
-				return $share->getNode() === $path &&
2286
-				$share->getShareType() === IShare::TYPE_LINK &&
2287
-				$share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE &&
2288
-				$share->getSharedBy() === 'currentUser' &&
2289
-				$share->getPassword() === null &&
2290
-				$share->getExpirationDate() == $date;
2291
-			})
2292
-		)->willReturnArgument(0);
2293
-
2294
-		$expected = new DataResponse([]);
2295
-		$result = $ocs->createShare('valid-path', null, IShare::TYPE_LINK, null, 'false', '', null, '2000-01-01');
2296
-
2297
-		$this->assertInstanceOf(get_class($expected), $result);
2298
-		$this->assertEquals($expected->getData(), $result->getData());
2299
-	}
2300
-
2301
-
2302
-	public function testCreateShareInvalidExpireDate(): void {
2303
-		$this->expectException(OCSNotFoundException::class);
2304
-		$this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
2305
-
2306
-		$ocs = $this->mockFormatShare();
2307
-
2308
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2309
-		$path->method('getId')->willReturn(42);
2310
-		$storage = $this->createMock(IStorage::class);
2311
-		$storage->method('instanceOfStorage')
2312
-			->willReturnMap([
2313
-				['OCA\Files_Sharing\External\Storage', false],
2314
-				['OCA\Files_Sharing\SharedStorage', false],
2315
-			]);
2316
-		$path->method('getStorage')->willReturn($storage);
2317
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2318
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2319
-		$this->rootFolder->method('getById')
2320
-			->willReturn([]);
2321
-
2322
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2323
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2324
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2325
-
2326
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3');
2327
-	}
2328
-
2329
-	public function testCreateShareRemote(): void {
2330
-		$share = $this->newShare();
2331
-		$this->shareManager->method('newShare')->willReturn($share);
2332
-
2333
-		/** @var ShareAPIController $ocs */
2334
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
2335
-			->setConstructorArgs([
2336
-				$this->appName,
2337
-				$this->request,
2338
-				$this->shareManager,
2339
-				$this->groupManager,
2340
-				$this->userManager,
2341
-				$this->rootFolder,
2342
-				$this->urlGenerator,
2343
-				$this->l,
2344
-				$this->config,
2345
-				$this->appManager,
2346
-				$this->serverContainer,
2347
-				$this->userStatusManager,
2348
-				$this->previewManager,
2349
-				$this->dateTimeZone,
2350
-				$this->logger,
2351
-				$this->factory,
2352
-				$this->mailer,
2353
-				$this->tagManager,
2354
-				$this->currentUser,
2355
-			])->onlyMethods(['formatShare'])
2356
-			->getMock();
2357
-
2358
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2359
-		$this->rootFolder->expects($this->exactly(2))
2360
-			->method('getUserFolder')
2361
-			->with('currentUser')
2362
-			->willReturn($userFolder);
2363
-
2364
-		$userFolder->expects($this->once())
2365
-			->method('get')
2366
-			->with('valid-path')
2367
-			->willReturn($path);
2368
-		$userFolder->method('getById')
2369
-			->willReturn([]);
2370
-
2371
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
2372
-
2373
-		$path->expects($this->once())
2374
-			->method('lock')
2375
-			->with(ILockingProvider::LOCK_SHARED);
2376
-
2377
-		$this->shareManager->method('createShare')
2378
-			->with($this->callback(function (IShare $share) use ($path) {
2379
-				return $share->getNode() === $path &&
2380
-					$share->getPermissions() === (
2381
-						Constants::PERMISSION_ALL &
2382
-						~Constants::PERMISSION_DELETE &
2383
-						~Constants::PERMISSION_CREATE
2384
-					) &&
2385
-					$share->getShareType() === IShare::TYPE_REMOTE &&
2386
-					$share->getSharedWith() === '[email protected]' &&
2387
-					$share->getSharedBy() === 'currentUser';
2388
-			}))
2389
-			->willReturnArgument(0);
2390
-
2391
-		$this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
2392
-
2393
-		$expected = new DataResponse([]);
2394
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, '[email protected]');
2395
-
2396
-		$this->assertInstanceOf(get_class($expected), $result);
2397
-		$this->assertEquals($expected->getData(), $result->getData());
2398
-	}
2399
-
2400
-	public function testCreateShareRemoteGroup(): void {
2401
-		$share = $this->newShare();
2402
-		$this->shareManager->method('newShare')->willReturn($share);
2403
-
2404
-		/** @var ShareAPIController $ocs */
2405
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
2406
-			->setConstructorArgs([
2407
-				$this->appName,
2408
-				$this->request,
2409
-				$this->shareManager,
2410
-				$this->groupManager,
2411
-				$this->userManager,
2412
-				$this->rootFolder,
2413
-				$this->urlGenerator,
2414
-				$this->l,
2415
-				$this->config,
2416
-				$this->appManager,
2417
-				$this->serverContainer,
2418
-				$this->userStatusManager,
2419
-				$this->previewManager,
2420
-				$this->dateTimeZone,
2421
-				$this->logger,
2422
-				$this->factory,
2423
-				$this->mailer,
2424
-				$this->tagManager,
2425
-				$this->currentUser,
2426
-			])->onlyMethods(['formatShare'])
2427
-			->getMock();
2428
-
2429
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2430
-		$this->rootFolder->expects($this->exactly(2))
2431
-			->method('getUserFolder')
2432
-			->with('currentUser')
2433
-			->willReturn($userFolder);
2434
-
2435
-		$userFolder->expects($this->once())
2436
-			->method('get')
2437
-			->with('valid-path')
2438
-			->willReturn($path);
2439
-		$userFolder->method('getById')
2440
-			->willReturn([]);
2441
-
2442
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
2443
-
2444
-		$path->expects($this->once())
2445
-			->method('lock')
2446
-			->with(ILockingProvider::LOCK_SHARED);
2447
-
2448
-		$this->shareManager->method('createShare')
2449
-			->with($this->callback(function (IShare $share) use ($path) {
2450
-				return $share->getNode() === $path &&
2451
-					$share->getPermissions() === (
2452
-						Constants::PERMISSION_ALL &
2453
-						~Constants::PERMISSION_DELETE &
2454
-						~Constants::PERMISSION_CREATE
2455
-					) &&
2456
-					$share->getShareType() === IShare::TYPE_REMOTE_GROUP &&
2457
-					$share->getSharedWith() === '[email protected]' &&
2458
-					$share->getSharedBy() === 'currentUser';
2459
-			}))
2460
-			->willReturnArgument(0);
2461
-
2462
-		$this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true);
2463
-
2464
-		$expected = new DataResponse([]);
2465
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, '[email protected]');
2466
-
2467
-		$this->assertInstanceOf(get_class($expected), $result);
2468
-		$this->assertEquals($expected->getData(), $result->getData());
2469
-	}
2470
-
2471
-	public function testCreateShareRoom(): void {
2472
-		$ocs = $this->mockFormatShare();
2473
-
2474
-		$share = $this->newShare();
2475
-		$this->shareManager->method('newShare')->willReturn($share);
2476
-
2477
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2478
-		$this->rootFolder->expects($this->exactly(2))
2479
-			->method('getUserFolder')
2480
-			->with('currentUser')
2481
-			->willReturn($userFolder);
2482
-
2483
-		$userFolder->expects($this->once())
2484
-			->method('get')
2485
-			->with('valid-path')
2486
-			->willReturn($path);
2487
-		$userFolder->method('getById')
2488
-			->willReturn([]);
2489
-
2490
-		$path->expects($this->once())
2491
-			->method('lock')
2492
-			->with(ILockingProvider::LOCK_SHARED);
2493
-
2494
-		$this->appManager->method('isEnabledForUser')
2495
-			->with('spreed')
2496
-			->willReturn(true);
2497
-
2498
-		// This is not possible anymore with PHPUnit 10+
2499
-		// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2500
-		// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2501
-		$helper = $this->getMockBuilder(\stdClass::class)
2502
-			->addMethods(['createShare'])
2503
-			->getMock();
2504
-		$helper->method('createShare')
2505
-			->with(
2506
-				$share,
2507
-				'recipientRoom',
2508
-				Constants::PERMISSION_ALL &
2509
-				~Constants::PERMISSION_DELETE &
2510
-				~Constants::PERMISSION_CREATE,
2511
-				''
2512
-			)->willReturnCallback(
2513
-				function ($share): void {
2514
-					$share->setSharedWith('recipientRoom');
2515
-					$share->setPermissions(Constants::PERMISSION_ALL);
2516
-				}
2517
-			);
2518
-
2519
-		$this->serverContainer->method('get')
2520
-			->with('\OCA\Talk\Share\Helper\ShareAPIController')
2521
-			->willReturn($helper);
2522
-
2523
-		$this->shareManager->method('createShare')
2524
-			->with($this->callback(function (IShare $share) use ($path) {
2525
-				return $share->getNode() === $path
2526
-					&& $share->getPermissions() === Constants::PERMISSION_ALL
2527
-					&& $share->getShareType() === IShare::TYPE_ROOM
2528
-					&& $share->getSharedWith() === 'recipientRoom'
2529
-					&& $share->getSharedBy() === 'currentUser';
2530
-			}))
2531
-			->willReturnArgument(0);
2532
-
2533
-		$expected = new DataResponse([]);
2534
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2535
-
2536
-		$this->assertInstanceOf(get_class($expected), $result);
2537
-		$this->assertEquals($expected->getData(), $result->getData());
2538
-	}
2539
-
2540
-
2541
-	public function testCreateShareRoomHelperNotAvailable(): void {
2542
-		$this->expectException(OCSForbiddenException::class);
2543
-		$this->expectExceptionMessage('Sharing valid-path failed because the back end does not support room shares');
2544
-
2545
-		$ocs = $this->mockFormatShare();
2546
-
2547
-		$share = $this->newShare();
2548
-		$this->shareManager->method('newShare')->willReturn($share);
2549
-
2550
-		[$userFolder, $path] = $this->getNonSharedUserFolder();
2551
-		$this->rootFolder->method('getUserFolder')
2552
-			->with('currentUser')
2553
-			->willReturn($userFolder);
2554
-
2555
-		$path->method('getPath')->willReturn('valid-path');
2556
-		$userFolder->expects($this->once())
2557
-			->method('get')
2558
-			->with('valid-path')
2559
-			->willReturn($path);
2560
-		$userFolder->method('getById')
2561
-			->willReturn([]);
2562
-
2563
-		$path->expects($this->once())
2564
-			->method('lock')
2565
-			->with(ILockingProvider::LOCK_SHARED);
2566
-
2567
-		$this->appManager->method('isEnabledForUser')
2568
-			->with('spreed')
2569
-			->willReturn(false);
2570
-
2571
-		$this->shareManager->expects($this->never())->method('createShare');
2572
-
2573
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2574
-	}
2575
-
2576
-
2577
-	public function testCreateShareRoomHelperThrowException(): void {
2578
-		$this->expectException(OCSNotFoundException::class);
2579
-		$this->expectExceptionMessage('Exception thrown by the helper');
2580
-
2581
-		$ocs = $this->mockFormatShare();
2582
-
2583
-		$share = $this->newShare();
2584
-		$share->setSharedBy('currentUser');
2585
-		$this->shareManager->method('newShare')->willReturn($share);
2586
-
2587
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2588
-		$this->rootFolder->method('getUserFolder')
2589
-			->with('currentUser')
2590
-			->willReturn($userFolder);
2591
-
2592
-		$userFolder->expects($this->once())
2593
-			->method('get')
2594
-			->with('valid-path')
2595
-			->willReturn($path);
2596
-		$userFolder->method('getById')
2597
-			->willReturn([]);
2598
-
2599
-		$path->expects($this->once())
2600
-			->method('lock')
2601
-			->with(ILockingProvider::LOCK_SHARED);
2602
-
2603
-		$this->appManager->method('isEnabledForUser')
2604
-			->with('spreed')
2605
-			->willReturn(true);
2606
-
2607
-		// This is not possible anymore with PHPUnit 10+
2608
-		// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2609
-		// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2610
-		$helper = $this->getMockBuilder(\stdClass::class)
2611
-			->addMethods(['createShare'])
2612
-			->getMock();
2613
-		$helper->method('createShare')
2614
-			->with(
2615
-				$share,
2616
-				'recipientRoom',
2617
-				Constants::PERMISSION_ALL & ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE),
2618
-				''
2619
-			)->willReturnCallback(
2620
-				function ($share): void {
2621
-					throw new OCSNotFoundException('Exception thrown by the helper');
2622
-				}
2623
-			);
2624
-
2625
-		$this->serverContainer->method('get')
2626
-			->with('\OCA\Talk\Share\Helper\ShareAPIController')
2627
-			->willReturn($helper);
2628
-
2629
-		$this->shareManager->expects($this->never())->method('createShare');
2630
-
2631
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2632
-	}
2633
-
2634
-	/**
2635
-	 * Test for https://github.com/owncloud/core/issues/22587
2636
-	 * TODO: Remove once proper solution is in place
2637
-	 */
2638
-	public function testCreateReshareOfFederatedMountNoDeletePermissions(): void {
2639
-		$share = Server::get(IManager::class)->newShare();
2640
-		$this->shareManager->method('newShare')->willReturn($share);
2641
-
2642
-		/** @var ShareAPIController&MockObject $ocs */
2643
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
2644
-			->setConstructorArgs([
2645
-				$this->appName,
2646
-				$this->request,
2647
-				$this->shareManager,
2648
-				$this->groupManager,
2649
-				$this->userManager,
2650
-				$this->rootFolder,
2651
-				$this->urlGenerator,
2652
-				$this->l,
2653
-				$this->config,
2654
-				$this->appManager,
2655
-				$this->serverContainer,
2656
-				$this->userStatusManager,
2657
-				$this->previewManager,
2658
-				$this->dateTimeZone,
2659
-				$this->logger,
2660
-				$this->factory,
2661
-				$this->mailer,
2662
-				$this->tagManager,
2663
-				$this->currentUser,
2664
-			])->onlyMethods(['formatShare'])
2665
-			->getMock();
2666
-
2667
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
2668
-		$this->rootFolder->expects($this->exactly(2))
2669
-			->method('getUserFolder')
2670
-			->with('currentUser')
2671
-			->willReturn($userFolder);
2672
-
2673
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2674
-		$path->method('getId')->willReturn(42);
2675
-
2676
-		$storage = $this->createMock(IStorage::class);
2677
-		$storage->method('instanceOfStorage')
2678
-			->willReturnMap([
2679
-				['OCA\Files_Sharing\External\Storage', true],
2680
-				['OCA\Files_Sharing\SharedStorage', false],
2681
-			]);
2682
-		$userFolder->method('getStorage')->willReturn($storage);
2683
-		$path->method('getStorage')->willReturn($storage);
2684
-
2685
-		$path->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
2686
-		$userFolder->expects($this->once())
2687
-			->method('get')
2688
-			->with('valid-path')
2689
-			->willReturn($path);
2690
-		$userFolder->method('getById')
2691
-			->willReturn([]);
2692
-
2693
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
2694
-
2695
-		$this->shareManager
2696
-			->expects($this->once())
2697
-			->method('createShare')
2698
-			->with($this->callback(function (IShare $share) {
2699
-				return $share->getPermissions() === Constants::PERMISSION_READ;
2700
-			}))
2701
-			->willReturnArgument(0);
2702
-
2703
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
2704
-	}
2705
-
2706
-
2707
-	public function testUpdateShareCantAccess(): void {
2708
-		$this->expectException(OCSNotFoundException::class);
2709
-		$this->expectExceptionMessage('Wrong share ID, share does not exist');
2710
-
2711
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
2712
-		$share = $this->newShare();
2713
-		$share->setNode($node);
2714
-
2715
-		$node->expects($this->once())
2716
-			->method('lock')
2717
-			->with(ILockingProvider::LOCK_SHARED);
2718
-
2719
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2720
-
2721
-		$this->rootFolder->method('getUserFolder')
2722
-			->with($this->currentUser)
2723
-			->willReturn($userFolder);
2724
-
2725
-		$userFolder->method('getById')
2726
-			->with($share->getNodeId())
2727
-			->willReturn([$share->getNode()]);
2728
-
2729
-		$this->ocs->updateShare(42);
2730
-	}
2731
-
2732
-
2733
-	public function testUpdateNoParametersLink(): void {
2734
-		$this->expectException(OCSBadRequestException::class);
2735
-		$this->expectExceptionMessage('Wrong or no update parameter given');
2736
-
2737
-		$node = $this->getMockBuilder(Folder::class)->getMock();
2738
-		$share = $this->newShare();
2739
-		$share->setPermissions(Constants::PERMISSION_ALL)
2740
-			->setSharedBy($this->currentUser)
2741
-			->setShareType(IShare::TYPE_LINK)
2742
-			->setNode($node);
2743
-
2744
-		$node->expects($this->once())
2745
-			->method('lock')
2746
-			->with(ILockingProvider::LOCK_SHARED);
2747
-
2748
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2749
-
2750
-		$this->ocs->updateShare(42);
2751
-	}
2752
-
2753
-
2754
-	public function testUpdateNoParametersOther(): void {
2755
-		$this->expectException(OCSBadRequestException::class);
2756
-		$this->expectExceptionMessage('Wrong or no update parameter given');
2757
-
2758
-		$node = $this->getMockBuilder(Folder::class)->getMock();
2759
-		$share = $this->newShare();
2760
-		$share->setPermissions(Constants::PERMISSION_ALL)
2761
-			->setSharedBy($this->currentUser)
2762
-			->setShareType(IShare::TYPE_GROUP)
2763
-			->setNode($node);
2764
-
2765
-		$node->expects($this->once())
2766
-			->method('lock')
2767
-			->with(ILockingProvider::LOCK_SHARED);
2768
-
2769
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2770
-
2771
-		$this->ocs->updateShare(42);
2772
-	}
2773
-
2774
-	public function testUpdateLinkShareClear(): void {
2775
-		$ocs = $this->mockFormatShare();
2776
-
2777
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
2778
-		$node->method('getId')
2779
-			->willReturn(42);
2780
-		$share = $this->newShare();
2781
-		$share->setPermissions(Constants::PERMISSION_ALL)
2782
-			->setSharedBy($this->currentUser)
2783
-			->setShareType(IShare::TYPE_LINK)
2784
-			->setPassword('password')
2785
-			->setExpirationDate(new \DateTime())
2786
-			->setNote('note')
2787
-			->setLabel('label')
2788
-			->setHideDownload(true)
2789
-			->setPermissions(Constants::PERMISSION_ALL)
2790
-			->setNode($node);
2791
-
2792
-		$node->expects($this->once())
2793
-			->method('lock')
2794
-			->with(ILockingProvider::LOCK_SHARED);
2795
-
2796
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2797
-
2798
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
2799
-			$this->callback(function (IShare $share) {
2800
-				return $share->getPermissions() === Constants::PERMISSION_READ &&
2801
-				$share->getPassword() === null &&
2802
-				$share->getExpirationDate() === null &&
2803
-				// Once set a note or a label are never back to null, only to an
2804
-				// empty string.
2805
-				$share->getNote() === '' &&
2806
-				$share->getLabel() === '' &&
2807
-				$share->getHideDownload() === false;
2808
-			})
2809
-		)->willReturnArgument(0);
2810
-
2811
-		$this->shareManager->method('getSharedWith')
2812
-			->willReturn([]);
2813
-
2814
-		$this->rootFolder->method('getUserFolder')
2815
-			->with($this->currentUser)
2816
-			->willReturn($userFolder);
2817
-
2818
-		$userFolder->method('getById')
2819
-			->with(42)
2820
-			->willReturn([$node]);
2821
-		$userFolder->method('getFirstNodeById')
2822
-			->with(42)
2823
-			->willReturn($node);
2824
-
2825
-		$mountPoint = $this->createMock(IMountPoint::class);
2826
-		$node->method('getMountPoint')
2827
-			->willReturn($mountPoint);
2828
-		$mountPoint->method('getStorageRootId')
2829
-			->willReturn(42);
2830
-
2831
-		$expected = new DataResponse([]);
2832
-		$result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false');
2833
-
2834
-		$this->assertInstanceOf(get_class($expected), $result);
2835
-		$this->assertEquals($expected->getData(), $result->getData());
2836
-	}
2837
-
2838
-	public function testUpdateLinkShareSet(): void {
2839
-		$ocs = $this->mockFormatShare();
2840
-
2841
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
2842
-		$folder->method('getId')
2843
-			->willReturn(42);
2844
-
2845
-		$share = Server::get(IManager::class)->newShare();
2846
-		$share->setPermissions(Constants::PERMISSION_ALL)
2847
-			->setSharedBy($this->currentUser)
2848
-			->setShareType(IShare::TYPE_LINK)
2849
-			->setNode($folder);
2850
-
2851
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2852
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2853
-
2854
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
2855
-			$this->callback(function (IShare $share) {
2856
-				$date = new \DateTime('2000-01-01');
2857
-				$date->setTime(0, 0, 0);
2858
-
2859
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2860
-				$share->getPassword() === 'password' &&
2861
-				$share->getExpirationDate() == $date &&
2862
-				$share->getNote() === 'note' &&
2863
-				$share->getLabel() === 'label' &&
2864
-				$share->getHideDownload() === true;
2865
-			})
2866
-		)->willReturnArgument(0);
2867
-
2868
-		$this->shareManager->method('getSharedWith')
2869
-			->willReturn([]);
2870
-
2871
-		$this->rootFolder->method('getUserFolder')
2872
-			->with($this->currentUser)
2873
-			->willReturn($userFolder);
2874
-
2875
-		$userFolder->method('getById')
2876
-			->with(42)
2877
-			->willReturn([$folder]);
2878
-
2879
-		$mountPoint = $this->createMock(IMountPoint::class);
2880
-		$folder->method('getMountPoint')
2881
-			->willReturn($mountPoint);
2882
-		$mountPoint->method('getStorageRootId')
2883
-			->willReturn(42);
2884
-
2885
-		$expected = new DataResponse([]);
2886
-		$result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true');
2887
-
2888
-		$this->assertInstanceOf(get_class($expected), $result);
2889
-		$this->assertEquals($expected->getData(), $result->getData());
2890
-	}
2891
-
2892
-	/**
2893
-	 * @dataProvider publicUploadParamsProvider
2894
-	 */
2895
-	public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password): void {
2896
-		$ocs = $this->mockFormatShare();
2897
-
2898
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
2899
-		$folder->method('getId')
2900
-			->willReturn(42);
2901
-
2902
-		$share = Server::get(IManager::class)->newShare();
2903
-		$share->setPermissions(Constants::PERMISSION_ALL)
2904
-			->setSharedBy($this->currentUser)
2905
-			->setShareType(IShare::TYPE_LINK)
2906
-			->setPassword('password')
2907
-			->setNode($folder);
2908
-
2909
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2910
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2911
-		$this->shareManager->method('getSharedWith')->willReturn([]);
2912
-
2913
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
2914
-			$this->callback(function (IShare $share) {
2915
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2916
-				$share->getPassword() === 'password' &&
2917
-				$share->getExpirationDate() === null;
2918
-			})
2919
-		)->willReturnArgument(0);
2920
-
2921
-		$this->rootFolder->method('getUserFolder')
2922
-			->with($this->currentUser)
2923
-			->willReturn($userFolder);
2924
-
2925
-		$userFolder->method('getById')
2926
-			->with(42)
2927
-			->willReturn([$folder]);
2928
-
2929
-		$mountPoint = $this->createMock(IMountPoint::class);
2930
-		$folder->method('getMountPoint')
2931
-			->willReturn($mountPoint);
2932
-		$mountPoint->method('getStorageRootId')
2933
-			->willReturn(42);
2934
-
2935
-		$expected = new DataResponse([]);
2936
-		$result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
2937
-
2938
-		$this->assertInstanceOf(get_class($expected), $result);
2939
-		$this->assertEquals($expected->getData(), $result->getData());
2940
-	}
2941
-
2942
-
2943
-	public function publicLinkValidPermissionsProvider() {
2944
-		return [
2945
-			[Constants::PERMISSION_CREATE],
2946
-			[Constants::PERMISSION_READ],
2947
-			[Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE],
2948
-			[Constants::PERMISSION_READ | Constants::PERMISSION_DELETE],
2949
-			[Constants::PERMISSION_READ | Constants::PERMISSION_CREATE],
2950
-		];
2951
-	}
2952
-
2953
-	/**
2954
-	 * @dataProvider publicLinkValidPermissionsProvider
2955
-	 */
2956
-	public function testUpdateLinkShareSetCRUDPermissions($permissions): void {
2957
-		$ocs = $this->mockFormatShare();
2958
-
2959
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
2960
-		$folder->method('getId')
2961
-			->willReturn(42);
2962
-
2963
-		$share = Server::get(IManager::class)->newShare();
2964
-		$share->setPermissions(Constants::PERMISSION_ALL)
2965
-			->setSharedBy($this->currentUser)
2966
-			->setShareType(IShare::TYPE_LINK)
2967
-			->setPassword('password')
2968
-			->setNode($folder);
2969
-
2970
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2971
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2972
-		$this->shareManager->method('getSharedWith')->willReturn([]);
2973
-
2974
-		$this->shareManager
2975
-			->expects($this->any())
2976
-			->method('updateShare')
2977
-			->willReturnArgument(0);
2978
-
2979
-		$this->rootFolder->method('getUserFolder')
2980
-			->with($this->currentUser)
2981
-			->willReturn($userFolder);
2982
-
2983
-		$userFolder->method('getById')
2984
-			->with(42)
2985
-			->willReturn([$folder]);
2986
-
2987
-		$mountPoint = $this->createMock(IMountPoint::class);
2988
-		$folder->method('getMountPoint')
2989
-			->willReturn($mountPoint);
2990
-		$mountPoint->method('getStorageRootId')
2991
-			->willReturn(42);
2992
-
2993
-		$expected = new DataResponse([]);
2994
-		$result = $ocs->updateShare(42, $permissions, 'password', null, null, null);
2995
-
2996
-		$this->assertInstanceOf(get_class($expected), $result);
2997
-		$this->assertEquals($expected->getData(), $result->getData());
2998
-	}
2999
-
3000
-	public function publicLinkInvalidPermissionsProvider1() {
3001
-		return [
3002
-			[Constants::PERMISSION_DELETE],
3003
-			[Constants::PERMISSION_UPDATE],
3004
-			[Constants::PERMISSION_SHARE],
3005
-		];
3006
-	}
3007
-
3008
-	/**
3009
-	 * @dataProvider publicLinkInvalidPermissionsProvider1
3010
-	 */
3011
-	public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions): void {
3012
-		$this->expectException(OCSBadRequestException::class);
3013
-		$this->expectExceptionMessage('Share must at least have READ or CREATE permissions');
3014
-
3015
-		$this->testUpdateLinkShareSetCRUDPermissions($permissions, null);
3016
-	}
3017
-
3018
-	public function publicLinkInvalidPermissionsProvider2() {
3019
-		return [
3020
-			[Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE],
3021
-			[Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE],
3022
-		];
3023
-	}
3024
-
3025
-	/**
3026
-	 * @dataProvider publicLinkInvalidPermissionsProvider2
3027
-	 */
3028
-	public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions): void {
3029
-		$this->expectException(OCSBadRequestException::class);
3030
-		$this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set');
3031
-
3032
-		$this->testUpdateLinkShareSetCRUDPermissions($permissions);
3033
-	}
3034
-
3035
-	public function testUpdateLinkShareInvalidDate(): void {
3036
-		$this->expectException(OCSBadRequestException::class);
3037
-		$this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
3038
-
3039
-		$ocs = $this->mockFormatShare();
3040
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3041
-		$userFolder->method('getById')
3042
-			->with(42)
3043
-			->willReturn([$folder]);
3044
-		$this->rootFolder->method('getUserFolder')
3045
-			->with($this->currentUser)
3046
-			->willReturn($userFolder);
3047
-
3048
-		$folder->method('getId')
3049
-			->willReturn(42);
3050
-
3051
-		$share = Server::get(IManager::class)->newShare();
3052
-		$share->setPermissions(Constants::PERMISSION_ALL)
3053
-			->setSharedBy($this->currentUser)
3054
-			->setShareType(IShare::TYPE_LINK)
3055
-			->setNode($folder);
3056
-
3057
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3058
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3059
-
3060
-		$ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a');
3061
-	}
3062
-
3063
-	public function publicUploadParamsProvider() {
3064
-		return [
3065
-			[null, 'true', null, 'password'],
3066
-			// legacy had no delete
3067
-			[
3068
-				Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE,
3069
-				'true', null, 'password'
3070
-			],
3071
-			// correct
3072
-			[
3073
-				Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE,
3074
-				null, null, 'password'
3075
-			],
3076
-		];
3077
-	}
3078
-
3079
-	/**
3080
-	 * @dataProvider publicUploadParamsProvider
3081
-	 */
3082
-	public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password): void {
3083
-		$this->expectException(OCSForbiddenException::class);
3084
-		$this->expectExceptionMessage('Public upload disabled by the administrator');
3085
-
3086
-		$ocs = $this->mockFormatShare();
3087
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3088
-		$userFolder->method('getById')
3089
-			->with(42)
3090
-			->willReturn([$folder]);
3091
-		$this->rootFolder->method('getUserFolder')
3092
-			->with($this->currentUser)
3093
-			->willReturn($userFolder);
3094
-
3095
-		$folder->method('getId')->willReturn(42);
3096
-
3097
-		$share = Server::get(IManager::class)->newShare();
3098
-		$share->setPermissions(Constants::PERMISSION_ALL)
3099
-			->setSharedBy($this->currentUser)
3100
-			->setShareType(IShare::TYPE_LINK)
3101
-			->setNode($folder);
3102
-
3103
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3104
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
3105
-
3106
-		$ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
3107
-	}
3108
-
3109
-
3110
-	public function testUpdateLinkSharePublicUploadOnFile(): void {
3111
-		$this->expectException(OCSBadRequestException::class);
3112
-		$this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
3113
-
3114
-		$ocs = $this->mockFormatShare();
3115
-
3116
-		$file = $this->getMockBuilder(File::class)->getMock();
3117
-		$file->method('getId')
3118
-			->willReturn(42);
3119
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3120
-		$userFolder->method('getById')
3121
-			->with(42)
3122
-			->willReturn([$folder]);
3123
-		$this->rootFolder->method('getUserFolder')
3124
-			->with($this->currentUser)
3125
-			->willReturn($userFolder);
3126
-
3127
-		$share = Server::get(IManager::class)->newShare();
3128
-		$share->setPermissions(Constants::PERMISSION_ALL)
3129
-			->setSharedBy($this->currentUser)
3130
-			->setShareType(IShare::TYPE_LINK)
3131
-			->setNode($file);
3132
-
3133
-		$this->shareManager
3134
-			->method('getShareById')
3135
-			->with('ocinternal:42')
3136
-			->willReturn($share);
3137
-		$this->shareManager
3138
-			->method('shareApiLinkAllowPublicUpload')
3139
-			->willReturn(true);
3140
-		$this->shareManager
3141
-			->method('updateShare')
3142
-			->with($share)
3143
-			->willThrowException(new \InvalidArgumentException('File shares cannot have create or delete permissions'));
3144
-
3145
-		$ocs->updateShare(42, null, 'password', null, 'true', '');
3146
-	}
3147
-
3148
-	public function testUpdateLinkSharePasswordDoesNotChangeOther(): void {
3149
-		$ocs = $this->mockFormatShare();
3150
-
3151
-		$date = new \DateTime('2000-01-01');
3152
-		$date->setTime(0, 0, 0);
3153
-
3154
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3155
-		$node->method('getId')->willReturn(42);
3156
-		$userFolder->method('getById')
3157
-			->with(42)
3158
-			->willReturn([$node]);
3159
-		$this->rootFolder->method('getUserFolder')
3160
-			->with($this->currentUser)
3161
-			->willReturn($userFolder);
3162
-		$share = $this->newShare();
3163
-		$share->setPermissions(Constants::PERMISSION_ALL)
3164
-			->setSharedBy($this->currentUser)
3165
-			->setShareType(IShare::TYPE_LINK)
3166
-			->setPassword('password')
3167
-			->setSendPasswordByTalk(true)
3168
-			->setExpirationDate($date)
3169
-			->setNote('note')
3170
-			->setLabel('label')
3171
-			->setHideDownload(true)
3172
-			->setPermissions(Constants::PERMISSION_ALL)
3173
-			->setNode($node);
3174
-
3175
-		$node->expects($this->once())
3176
-			->method('lock')
3177
-			->with(ILockingProvider::LOCK_SHARED);
3178
-
3179
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3180
-
3181
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3182
-			$this->callback(function (IShare $share) use ($date) {
3183
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3184
-				$share->getPassword() === 'newpassword' &&
3185
-				$share->getSendPasswordByTalk() === true &&
3186
-				$share->getExpirationDate() === $date &&
3187
-				$share->getNote() === 'note' &&
3188
-				$share->getLabel() === 'label' &&
3189
-				$share->getHideDownload() === true;
3190
-			})
3191
-		)->willReturnArgument(0);
3192
-
3193
-		$expected = new DataResponse([]);
3194
-		$result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null);
3195
-
3196
-		$this->assertInstanceOf(get_class($expected), $result);
3197
-		$this->assertEquals($expected->getData(), $result->getData());
3198
-	}
3199
-
3200
-	public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther(): void {
3201
-		$ocs = $this->mockFormatShare();
3202
-
3203
-		$date = new \DateTime('2000-01-01');
3204
-		$date->setTime(0, 0, 0);
3205
-
3206
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3207
-		$userFolder->method('getById')
3208
-			->with(42)
3209
-			->willReturn([$node]);
3210
-		$this->rootFolder->method('getUserFolder')
3211
-			->with($this->currentUser)
3212
-			->willReturn($userFolder);
3213
-		$node->method('getId')->willReturn(42);
3214
-		$share = $this->newShare();
3215
-		$share->setPermissions(Constants::PERMISSION_ALL)
3216
-			->setSharedBy($this->currentUser)
3217
-			->setShareType(IShare::TYPE_LINK)
3218
-			->setPassword('password')
3219
-			->setSendPasswordByTalk(false)
3220
-			->setExpirationDate($date)
3221
-			->setNote('note')
3222
-			->setLabel('label')
3223
-			->setHideDownload(true)
3224
-			->setPermissions(Constants::PERMISSION_ALL)
3225
-			->setNode($node);
3226
-
3227
-		$node->expects($this->once())
3228
-			->method('lock')
3229
-			->with(ILockingProvider::LOCK_SHARED);
3230
-
3231
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3232
-
3233
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3234
-
3235
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3236
-			$this->callback(function (IShare $share) use ($date) {
3237
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3238
-				$share->getPassword() === 'password' &&
3239
-				$share->getSendPasswordByTalk() === true &&
3240
-				$share->getExpirationDate() === $date &&
3241
-				$share->getNote() === 'note' &&
3242
-				$share->getLabel() === 'label' &&
3243
-				$share->getHideDownload() === true;
3244
-			})
3245
-		)->willReturnArgument(0);
3246
-
3247
-		$expected = new DataResponse([]);
3248
-		$result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3249
-
3250
-		$this->assertInstanceOf(get_class($expected), $result);
3251
-		$this->assertEquals($expected->getData(), $result->getData());
3252
-	}
3253
-
3254
-
3255
-	public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3256
-		$this->expectException(OCSForbiddenException::class);
3257
-		$this->expectExceptionMessage('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.');
3258
-
3259
-		$ocs = $this->mockFormatShare();
3260
-
3261
-		$date = new \DateTime('2000-01-01');
3262
-		$date->setTime(0, 0, 0);
3263
-
3264
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3265
-		$userFolder->method('getById')
3266
-			->with(42)
3267
-			->willReturn([$node]);
3268
-		$this->rootFolder->method('getUserFolder')
3269
-			->with($this->currentUser)
3270
-			->willReturn($userFolder);
3271
-		$node->method('getId')->willReturn(42);
3272
-		$share = $this->newShare();
3273
-		$share->setPermissions(Constants::PERMISSION_ALL)
3274
-			->setSharedBy($this->currentUser)
3275
-			->setShareType(IShare::TYPE_LINK)
3276
-			->setPassword('password')
3277
-			->setSendPasswordByTalk(false)
3278
-			->setExpirationDate($date)
3279
-			->setNote('note')
3280
-			->setLabel('label')
3281
-			->setHideDownload(true)
3282
-			->setPermissions(Constants::PERMISSION_ALL)
3283
-			->setNode($node);
3284
-
3285
-		$node->expects($this->once())
3286
-			->method('lock')
3287
-			->with(ILockingProvider::LOCK_SHARED);
3288
-
3289
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3290
-
3291
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3292
-
3293
-		$this->shareManager->expects($this->never())->method('updateShare');
3294
-
3295
-		$ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3296
-	}
3297
-
3298
-	public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther(): void {
3299
-		$ocs = $this->mockFormatShare();
3300
-
3301
-		$date = new \DateTime('2000-01-01');
3302
-		$date->setTime(0, 0, 0);
3303
-
3304
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3305
-		$userFolder->method('getById')
3306
-			->with(42)
3307
-			->willReturn([$node]);
3308
-		$this->rootFolder->method('getUserFolder')
3309
-			->with($this->currentUser)
3310
-			->willReturn($userFolder);
3311
-		$node->method('getId')->willReturn(42);
3312
-		$share = $this->newShare();
3313
-		$share->setPermissions(Constants::PERMISSION_ALL)
3314
-			->setSharedBy($this->currentUser)
3315
-			->setShareType(IShare::TYPE_LINK)
3316
-			->setPassword('password')
3317
-			->setSendPasswordByTalk(true)
3318
-			->setExpirationDate($date)
3319
-			->setNote('note')
3320
-			->setLabel('label')
3321
-			->setHideDownload(true)
3322
-			->setPermissions(Constants::PERMISSION_ALL)
3323
-			->setNode($node);
3324
-
3325
-		$node->expects($this->once())
3326
-			->method('lock')
3327
-			->with(ILockingProvider::LOCK_SHARED);
3328
-
3329
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3330
-
3331
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3332
-
3333
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3334
-			$this->callback(function (IShare $share) use ($date) {
3335
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3336
-				$share->getPassword() === 'password' &&
3337
-				$share->getSendPasswordByTalk() === false &&
3338
-				$share->getExpirationDate() === $date &&
3339
-				$share->getNote() === 'note' &&
3340
-				$share->getLabel() === 'label' &&
3341
-				$share->getHideDownload() === true;
3342
-			})
3343
-		)->willReturnArgument(0);
3344
-
3345
-		$expected = new DataResponse([]);
3346
-		$result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3347
-
3348
-		$this->assertInstanceOf(get_class($expected), $result);
3349
-		$this->assertEquals($expected->getData(), $result->getData());
3350
-	}
3351
-
3352
-	public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3353
-		$ocs = $this->mockFormatShare();
3354
-
3355
-		$date = new \DateTime('2000-01-01');
3356
-		$date->setTime(0, 0, 0);
3357
-
3358
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3359
-		$node->method('getId')
3360
-			->willReturn(42);
3361
-
3362
-		$share = $this->newShare();
3363
-		$share->setPermissions(Constants::PERMISSION_ALL)
3364
-			->setSharedBy($this->currentUser)
3365
-			->setShareType(IShare::TYPE_LINK)
3366
-			->setPassword('password')
3367
-			->setSendPasswordByTalk(true)
3368
-			->setExpirationDate($date)
3369
-			->setNote('note')
3370
-			->setLabel('label')
3371
-			->setHideDownload(true)
3372
-			->setPermissions(Constants::PERMISSION_ALL)
3373
-			->setNode($node);
3374
-
3375
-		$node->expects($this->once())
3376
-			->method('lock')
3377
-			->with(ILockingProvider::LOCK_SHARED);
3378
-
3379
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3380
-
3381
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3382
-
3383
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3384
-			$this->callback(function (IShare $share) use ($date) {
3385
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3386
-				$share->getPassword() === 'password' &&
3387
-				$share->getSendPasswordByTalk() === false &&
3388
-				$share->getExpirationDate() === $date &&
3389
-				$share->getNote() === 'note' &&
3390
-				$share->getLabel() === 'label' &&
3391
-				$share->getHideDownload() === true;
3392
-			})
3393
-		)->willReturnArgument(0);
3394
-
3395
-		$this->rootFolder->method('getUserFolder')
3396
-			->with($this->currentUser)
3397
-			->willReturn($userFolder);
3398
-
3399
-		$userFolder->method('getById')
3400
-			->with(42)
3401
-			->willReturn([$node]);
3402
-
3403
-		$mountPoint = $this->createMock(IMountPoint::class);
3404
-		$node->method('getMountPoint')
3405
-			->willReturn($mountPoint);
3406
-		$mountPoint->method('getStorageRootId')
3407
-			->willReturn(42);
3408
-
3409
-		$mountPoint = $this->createMock(IMountPoint::class);
3410
-		$node->method('getMountPoint')
3411
-			->willReturn($mountPoint);
3412
-		$mountPoint->method('getStorageRootId')
3413
-			->willReturn(42);
3414
-
3415
-		$expected = new DataResponse([]);
3416
-		$result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3417
-
3418
-		$this->assertInstanceOf(get_class($expected), $result);
3419
-		$this->assertEquals($expected->getData(), $result->getData());
3420
-	}
3421
-
3422
-	public function testUpdateLinkShareExpireDateDoesNotChangeOther(): void {
3423
-		$ocs = $this->mockFormatShare();
3424
-
3425
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3426
-		$node->method('getId')
3427
-			->willReturn(42);
3428
-
3429
-		$share = $this->newShare();
3430
-		$share->setPermissions(Constants::PERMISSION_ALL)
3431
-			->setSharedBy($this->currentUser)
3432
-			->setShareType(IShare::TYPE_LINK)
3433
-			->setPassword('password')
3434
-			->setSendPasswordByTalk(true)
3435
-			->setExpirationDate(new \DateTime())
3436
-			->setNote('note')
3437
-			->setLabel('label')
3438
-			->setHideDownload(true)
3439
-			->setPermissions(Constants::PERMISSION_ALL)
3440
-			->setNode($node);
3441
-
3442
-		$node->expects($this->once())
3443
-			->method('lock')
3444
-			->with(ILockingProvider::LOCK_SHARED);
3445
-
3446
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3447
-
3448
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3449
-			$this->callback(function (IShare $share) {
3450
-				$date = new \DateTime('2010-12-23');
3451
-				$date->setTime(0, 0, 0);
3452
-
3453
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3454
-				$share->getPassword() === 'password' &&
3455
-				$share->getSendPasswordByTalk() === true &&
3456
-				$share->getExpirationDate() == $date &&
3457
-				$share->getNote() === 'note' &&
3458
-				$share->getLabel() === 'label' &&
3459
-				$share->getHideDownload() === true;
3460
-			})
3461
-		)->willReturnArgument(0);
3462
-
3463
-		$this->rootFolder->method('getUserFolder')
3464
-			->with($this->currentUser)
3465
-			->willReturn($userFolder);
3466
-
3467
-		$userFolder->method('getById')
3468
-			->with(42)
3469
-			->willReturn([$node]);
3470
-
3471
-		$mountPoint = $this->createMock(IMountPoint::class);
3472
-		$node->method('getMountPoint')
3473
-			->willReturn($mountPoint);
3474
-		$mountPoint->method('getStorageRootId')
3475
-			->willReturn(42);
3476
-
3477
-		$expected = new DataResponse([]);
3478
-		$result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null);
3479
-
3480
-		$this->assertInstanceOf(get_class($expected), $result);
3481
-		$this->assertEquals($expected->getData(), $result->getData());
3482
-	}
3483
-
3484
-	public function testUpdateLinkSharePublicUploadDoesNotChangeOther(): void {
3485
-		$ocs = $this->mockFormatShare();
3486
-
3487
-		$date = new \DateTime('2000-01-01');
3488
-
3489
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3490
-		$folder->method('getId')
3491
-			->willReturn(42);
3492
-
3493
-		$share = Server::get(IManager::class)->newShare();
3494
-		$share->setPermissions(Constants::PERMISSION_ALL)
3495
-			->setSharedBy($this->currentUser)
3496
-			->setShareType(IShare::TYPE_LINK)
3497
-			->setPassword('password')
3498
-			->setSendPasswordByTalk(true)
3499
-			->setExpirationDate($date)
3500
-			->setNote('note')
3501
-			->setLabel('label')
3502
-			->setHideDownload(true)
3503
-			->setPermissions(Constants::PERMISSION_ALL)
3504
-			->setNode($folder);
3505
-
3506
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3507
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3508
-
3509
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3510
-			$this->callback(function (IShare $share) use ($date) {
3511
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3512
-				$share->getPassword() === 'password' &&
3513
-				$share->getSendPasswordByTalk() === true &&
3514
-				$share->getExpirationDate() === $date &&
3515
-				$share->getNote() === 'note' &&
3516
-				$share->getLabel() === 'label' &&
3517
-				$share->getHideDownload() === true;
3518
-			})
3519
-		)->willReturnArgument(0);
3520
-
3521
-		$this->shareManager->method('getSharedWith')
3522
-			->willReturn([]);
3523
-
3524
-		$this->rootFolder->method('getUserFolder')
3525
-			->with($this->currentUser)
3526
-			->willReturn($userFolder);
3527
-
3528
-		$userFolder->method('getById')
3529
-			->with(42)
3530
-			->willReturn([$folder]);
3531
-
3532
-		$mountPoint = $this->createMock(IMountPoint::class);
3533
-		$folder->method('getMountPoint')
3534
-			->willReturn($mountPoint);
3535
-		$mountPoint->method('getStorageRootId')
3536
-			->willReturn(42);
3537
-
3538
-		$expected = new DataResponse([]);
3539
-		$result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null);
3540
-
3541
-		$this->assertInstanceOf(get_class($expected), $result);
3542
-		$this->assertEquals($expected->getData(), $result->getData());
3543
-	}
3544
-
3545
-	public function testUpdateLinkSharePermissions(): void {
3546
-		$ocs = $this->mockFormatShare();
3547
-
3548
-		$date = new \DateTime('2000-01-01');
3549
-
3550
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3551
-		$folder->method('getId')
3552
-			->willReturn(42);
3553
-
3554
-		$share = Server::get(IManager::class)->newShare();
3555
-		$share->setPermissions(Constants::PERMISSION_ALL)
3556
-			->setSharedBy($this->currentUser)
3557
-			->setShareType(IShare::TYPE_LINK)
3558
-			->setPassword('password')
3559
-			->setSendPasswordByTalk(true)
3560
-			->setExpirationDate($date)
3561
-			->setNote('note')
3562
-			->setLabel('label')
3563
-			->setHideDownload(true)
3564
-			->setPermissions(Constants::PERMISSION_ALL)
3565
-			->setNode($folder);
3566
-
3567
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3568
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3569
-
3570
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3571
-			$this->callback(function (IShare $share) use ($date): bool {
3572
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3573
-				$share->getPassword() === 'password' &&
3574
-				$share->getSendPasswordByTalk() === true &&
3575
-				$share->getExpirationDate() === $date &&
3576
-				$share->getNote() === 'note' &&
3577
-				$share->getLabel() === 'label' &&
3578
-				$share->getHideDownload() === true;
3579
-			})
3580
-		)->willReturnArgument(0);
3581
-
3582
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3583
-
3584
-		$this->rootFolder->method('getUserFolder')
3585
-			->with($this->currentUser)
3586
-			->willReturn($userFolder);
3587
-
3588
-		$userFolder->method('getById')
3589
-			->with(42)
3590
-			->willReturn([$folder]);
3591
-
3592
-		$mountPoint = $this->createMock(IMountPoint::class);
3593
-		$folder->method('getMountPoint')
3594
-			->willReturn($mountPoint);
3595
-		$mountPoint->method('getStorageRootId')
3596
-			->willReturn(42);
3597
-
3598
-		$expected = new DataResponse([]);
3599
-		$result = $ocs->updateShare(42, 7, null, null, 'true', null, null, null, null);
3600
-
3601
-		$this->assertInstanceOf(get_class($expected), $result);
3602
-		$this->assertEquals($expected->getData(), $result->getData());
3603
-	}
3604
-
3605
-	public function testUpdateLinkSharePermissionsShare(): void {
3606
-		$ocs = $this->mockFormatShare();
3607
-
3608
-		$date = new \DateTime('2000-01-01');
3609
-
3610
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3611
-		$folder->method('getId')
3612
-			->willReturn(42);
3613
-
3614
-		$share = Server::get(IManager::class)->newShare();
3615
-		$share->setPermissions(Constants::PERMISSION_ALL)
3616
-			->setSharedBy($this->currentUser)
3617
-			->setShareType(IShare::TYPE_LINK)
3618
-			->setPassword('password')
3619
-			->setSendPasswordByTalk(true)
3620
-			->setExpirationDate($date)
3621
-			->setNote('note')
3622
-			->setLabel('label')
3623
-			->setHideDownload(true)
3624
-			->setPermissions(Constants::PERMISSION_READ)
3625
-			->setNode($folder);
3626
-
3627
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3628
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3629
-
3630
-		$this->shareManager->expects($this->once())
3631
-			->method('updateShare')
3632
-			->with(
3633
-				$this->callback(function (IShare $share) use ($date) {
3634
-					return $share->getPermissions() === Constants::PERMISSION_ALL &&
3635
-						$share->getPassword() === 'password' &&
3636
-						$share->getSendPasswordByTalk() === true &&
3637
-						$share->getExpirationDate() === $date &&
3638
-						$share->getNote() === 'note' &&
3639
-						$share->getLabel() === 'label' &&
3640
-						$share->getHideDownload() === true;
3641
-				})
3642
-			)->willReturnArgument(0);
3643
-
3644
-		$this->rootFolder->method('getUserFolder')
3645
-			->with($this->currentUser)
3646
-			->willReturn($userFolder);
3647
-
3648
-		$userFolder->method('getById')
3649
-			->with(42)
3650
-			->willReturn([$folder]);
3651
-
3652
-		$mountPoint = $this->createMock(IMountPoint::class);
3653
-		$folder->method('getMountPoint')
3654
-			->willReturn($mountPoint);
3655
-		$mountPoint->method('getStorageRootId')
3656
-			->willReturn(42);
3657
-
3658
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3659
-
3660
-		$expected = new DataResponse([]);
3661
-		$result = $ocs->updateShare(42, Constants::PERMISSION_ALL, null, null, null, null, null, null, null);
3662
-
3663
-		$this->assertInstanceOf(get_class($expected), $result);
3664
-		$this->assertEquals($expected->getData(), $result->getData());
3665
-	}
3666
-
3667
-	public function testUpdateOtherPermissions(): void {
3668
-		$ocs = $this->mockFormatShare();
3669
-
3670
-		[$userFolder, $file] = $this->getNonSharedUserFolder();
3671
-		$file->method('getId')
3672
-			->willReturn(42);
3673
-
3674
-		$share = Server::get(IManager::class)->newShare();
3675
-		$share->setPermissions(Constants::PERMISSION_ALL)
3676
-			->setSharedBy($this->currentUser)
3677
-			->setShareType(IShare::TYPE_USER)
3678
-			->setNode($file);
3679
-
3680
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3681
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3682
-
3683
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3684
-			$this->callback(function (IShare $share) {
3685
-				return $share->getPermissions() === Constants::PERMISSION_ALL;
3686
-			})
3687
-		)->willReturnArgument(0);
3688
-
3689
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3690
-
3691
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3692
-		$this->rootFolder->method('getUserFolder')
3693
-			->with($this->currentUser)
3694
-			->willReturn($userFolder);
3695
-
3696
-		$userFolder->method('getById')
3697
-			->with(42)
3698
-			->willReturn([$file]);
3699
-
3700
-		$mountPoint = $this->createMock(IMountPoint::class);
3701
-		$file->method('getMountPoint')
3702
-			->willReturn($mountPoint);
3703
-		$mountPoint->method('getStorageRootId')
3704
-			->willReturn(42);
3705
-
3706
-		$expected = new DataResponse([]);
3707
-		$result = $ocs->updateShare(42, 31, null, null, null, null);
3708
-
3709
-		$this->assertInstanceOf(get_class($expected), $result);
3710
-		$this->assertEquals($expected->getData(), $result->getData());
3711
-	}
3712
-
3713
-	public function testUpdateShareCannotIncreasePermissions(): void {
3714
-		$ocs = $this->mockFormatShare();
3715
-
3716
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3717
-		$folder->method('getId')
3718
-			->willReturn(42);
3719
-
3720
-		$share = Server::get(IManager::class)->newShare();
3721
-		$share
3722
-			->setId(42)
3723
-			->setSharedBy($this->currentUser)
3724
-			->setShareOwner('anotheruser')
3725
-			->setShareType(IShare::TYPE_GROUP)
3726
-			->setSharedWith('group1')
3727
-			->setPermissions(Constants::PERMISSION_READ)
3728
-			->setNode($folder);
3729
-
3730
-		// note: updateShare will modify the received instance but getSharedWith will reread from the database,
3731
-		// so their values will be different
3732
-		$incomingShare = Server::get(IManager::class)->newShare();
3733
-		$incomingShare
3734
-			->setId(42)
3735
-			->setSharedBy($this->currentUser)
3736
-			->setShareOwner('anotheruser')
3737
-			->setShareType(IShare::TYPE_GROUP)
3738
-			->setSharedWith('group1')
3739
-			->setPermissions(Constants::PERMISSION_READ)
3740
-			->setNode($folder);
3741
-
3742
-		$this->request
3743
-			->method('getParam')
3744
-			->willReturnMap([
3745
-				['permissions', null, '31'],
3746
-			]);
3747
-
3748
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3749
-
3750
-		$this->shareManager->expects($this->any())
3751
-			->method('getSharedWith')
3752
-			->willReturnMap([
3753
-				['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3754
-				['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]],
3755
-				['currentUser', IShare::TYPE_ROOM, $share->getNode(), -1, 0, []]
3756
-			]);
3757
-
3758
-		$this->rootFolder->method('getUserFolder')
3759
-			->with($this->currentUser)
3760
-			->willReturn($userFolder);
3761
-
3762
-		$userFolder->method('getById')
3763
-			->with(42)
3764
-			->willReturn([$folder]);
3765
-		$userFolder->method('getFirstNodeById')
3766
-			->with(42)
3767
-			->willReturn($folder);
3768
-
3769
-		$mountPoint = $this->createMock(IMountPoint::class);
3770
-		$folder->method('getMountPoint')
3771
-			->willReturn($mountPoint);
3772
-		$mountPoint->method('getStorageRootId')
3773
-			->willReturn(42);
3774
-
3775
-		$this->shareManager->expects($this->once())
3776
-			->method('updateShare')
3777
-			->with($share)
3778
-			->willThrowException(new GenericShareException('Cannot increase permissions of path/file', 'Cannot increase permissions of path/file', 404));
3779
-
3780
-		try {
3781
-			$ocs->updateShare(42, 31);
3782
-			$this->fail();
3783
-		} catch (OCSException $e) {
3784
-			$this->assertEquals('Cannot increase permissions of path/file', $e->getMessage());
3785
-		}
3786
-	}
3787
-
3788
-	public function testUpdateShareCanIncreasePermissionsIfOwner(): void {
3789
-		$ocs = $this->mockFormatShare();
3790
-
3791
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3792
-		$folder->method('getId')
3793
-			->willReturn(42);
3794
-
3795
-		$share = Server::get(IManager::class)->newShare();
3796
-		$share
3797
-			->setId(42)
3798
-			->setSharedBy($this->currentUser)
3799
-			->setShareOwner($this->currentUser)
3800
-			->setShareType(IShare::TYPE_GROUP)
3801
-			->setSharedWith('group1')
3802
-			->setPermissions(Constants::PERMISSION_READ)
3803
-			->setNode($folder);
3804
-
3805
-		// note: updateShare will modify the received instance but getSharedWith will reread from the database,
3806
-		// so their values will be different
3807
-		$incomingShare = Server::get(IManager::class)->newShare();
3808
-		$incomingShare
3809
-			->setId(42)
3810
-			->setSharedBy($this->currentUser)
3811
-			->setShareOwner($this->currentUser)
3812
-			->setShareType(IShare::TYPE_GROUP)
3813
-			->setSharedWith('group1')
3814
-			->setPermissions(Constants::PERMISSION_READ)
3815
-			->setNode($folder);
3816
-
3817
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3818
-
3819
-		$this->shareManager->expects($this->any())
3820
-			->method('getSharedWith')
3821
-			->willReturnMap([
3822
-				['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3823
-				['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
3824
-			]);
3825
-
3826
-		$this->shareManager->expects($this->once())
3827
-			->method('updateShare')
3828
-			->with($share)
3829
-			->willReturn($share);
3830
-
3831
-		$this->rootFolder->method('getUserFolder')
3832
-			->with($this->currentUser)
3833
-			->willReturn($userFolder);
3834
-
3835
-		$userFolder->method('getById')
3836
-			->with(42)
3837
-			->willReturn([$folder]);
3838
-
3839
-		$mountPoint = $this->createMock(IMountPoint::class);
3840
-		$folder->method('getMountPoint')
3841
-			->willReturn($mountPoint);
3842
-		$mountPoint->method('getStorageRootId')
3843
-			->willReturn(42);
3844
-
3845
-		$result = $ocs->updateShare(42, 31);
3846
-		$this->assertInstanceOf(DataResponse::class, $result);
3847
-	}
3848
-
3849
-	public function testUpdateShareOwnerless(): void {
3850
-		$ocs = $this->mockFormatShare();
3851
-
3852
-		$mount = $this->createMock(IShareOwnerlessMount::class);
3853
-
3854
-		$file = $this->createMock(File::class);
3855
-		$file
3856
-			->expects($this->exactly(2))
3857
-			->method('getPermissions')
3858
-			->willReturn(Constants::PERMISSION_SHARE);
3859
-		$file
3860
-			->expects($this->once())
3861
-			->method('getMountPoint')
3862
-			->willReturn($mount);
3863
-
3864
-		$userFolder = $this->createMock(Folder::class);
3865
-		$userFolder->method('getById')
3866
-			->with(2)
3867
-			->willReturn([$file]);
3868
-		$userFolder->method('getFirstNodeById')
3869
-			->with(2)
3870
-			->willReturn($file);
3871
-
3872
-		$this->rootFolder
3873
-			->method('getUserFolder')
3874
-			->with($this->currentUser)
3875
-			->willReturn($userFolder);
3876
-
3877
-		$share = $this->createMock(IShare::class);
3878
-		$share
3879
-			->expects($this->once())
3880
-			->method('getNode')
3881
-			->willReturn($file);
3882
-		$share
3883
-			->expects($this->exactly(2))
3884
-			->method('getNodeId')
3885
-			->willReturn(2);
3886
-		$share
3887
-			->expects($this->exactly(2))
3888
-			->method('getPermissions')
3889
-			->willReturn(Constants::PERMISSION_SHARE);
3890
-
3891
-		$this->shareManager
3892
-			->expects($this->once())
3893
-			->method('getShareById')
3894
-			->with('ocinternal:1', $this->currentUser)
3895
-			->willReturn($share);
3896
-
3897
-		$this->shareManager
3898
-			->expects($this->once())
3899
-			->method('updateShare')
3900
-			->with($share)
3901
-			->willReturn($share);
3902
-
3903
-		$result = $ocs->updateShare(1, Constants::PERMISSION_ALL);
3904
-		$this->assertInstanceOf(DataResponse::class, $result);
3905
-	}
3906
-
3907
-	public function dataFormatShare() {
3908
-		$file = $this->getMockBuilder(File::class)->getMock();
3909
-		$folder = $this->getMockBuilder(Folder::class)->getMock();
3910
-		$parent = $this->getMockBuilder(Folder::class)->getMock();
3911
-		$fileWithPreview = $this->getMockBuilder(File::class)->getMock();
3912
-
3913
-		$file->method('getMimeType')->willReturn('myMimeType');
3914
-		$folder->method('getMimeType')->willReturn('myFolderMimeType');
3915
-		$fileWithPreview->method('getMimeType')->willReturn('mimeWithPreview');
3916
-
3917
-		$mountPoint = $this->createMock(IMountPoint::class);
3918
-		$mountPoint->method('getMountType')->willReturn('');
3919
-		$file->method('getMountPoint')->willReturn($mountPoint);
3920
-		$folder->method('getMountPoint')->willReturn($mountPoint);
3921
-		$fileWithPreview->method('getMountPoint')->willReturn($mountPoint);
3922
-
3923
-		$file->method('getPath')->willReturn('file');
3924
-		$folder->method('getPath')->willReturn('folder');
3925
-		$fileWithPreview->method('getPath')->willReturn('fileWithPreview');
3926
-
3927
-		$parent->method('getId')->willReturn(1);
3928
-		$folder->method('getId')->willReturn(2);
3929
-		$file->method('getId')->willReturn(3);
3930
-		$fileWithPreview->method('getId')->willReturn(4);
3931
-
3932
-		$file->method('getParent')->willReturn($parent);
3933
-		$folder->method('getParent')->willReturn($parent);
3934
-		$fileWithPreview->method('getParent')->willReturn($parent);
3935
-
3936
-		$file->method('getSize')->willReturn(123456);
3937
-		$folder->method('getSize')->willReturn(123456);
3938
-		$fileWithPreview->method('getSize')->willReturn(123456);
3939
-		$file->method('getMTime')->willReturn(1234567890);
3940
-		$folder->method('getMTime')->willReturn(1234567890);
3941
-		$fileWithPreview->method('getMTime')->willReturn(1234567890);
3942
-
3943
-		$cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
3944
-		$cache->method('getNumericStorageId')->willReturn(100);
3945
-		$storage = $this->createMock(IStorage::class);
3946
-		$storage->method('getId')->willReturn('storageId');
3947
-		$storage->method('getCache')->willReturn($cache);
3948
-
3949
-		$file->method('getStorage')->willReturn($storage);
3950
-		$folder->method('getStorage')->willReturn($storage);
3951
-		$fileWithPreview->method('getStorage')->willReturn($storage);
3952
-
3953
-
3954
-		$mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
3955
-		$mountPoint->method('getMountType')->willReturn('');
3956
-		$file->method('getMountPoint')->willReturn($mountPoint);
3957
-		$folder->method('getMountPoint')->willReturn($mountPoint);
3958
-
3959
-		$owner = $this->getMockBuilder(IUser::class)->getMock();
3960
-		$owner->method('getDisplayName')->willReturn('ownerDN');
3961
-		$initiator = $this->getMockBuilder(IUser::class)->getMock();
3962
-		$initiator->method('getDisplayName')->willReturn('initiatorDN');
3963
-		$recipient = $this->getMockBuilder(IUser::class)->getMock();
3964
-		$recipient->method('getDisplayName')->willReturn('recipientDN');
3965
-		$recipient->method('getSystemEMailAddress')->willReturn('recipient');
3966
-		[$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
3967
-
3968
-		$result = [];
3969
-
3970
-		$share = Server::get(IManager::class)->newShare();
3971
-		$share->setShareType(IShare::TYPE_USER)
3972
-			->setSharedWith('recipient')
3973
-			->setSharedBy('initiator')
3974
-			->setShareOwner('owner')
3975
-			->setPermissions(Constants::PERMISSION_READ)
3976
-			->setAttributes($shareAttributes)
3977
-			->setNode($file)
3978
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
3979
-			->setTarget('myTarget')
3980
-			->setNote('personal note')
3981
-			->setId(42);
3982
-
3983
-		// User backend down
3984
-		$result[] = [
3985
-			[
3986
-				'id' => '42',
3987
-				'share_type' => IShare::TYPE_USER,
3988
-				'uid_owner' => 'initiator',
3989
-				'displayname_owner' => 'initiator',
3990
-				'permissions' => 1,
3991
-				'attributes' => $shareAttributesReturnJson,
3992
-				'stime' => 946684862,
3993
-				'parent' => null,
3994
-				'expiration' => null,
3995
-				'token' => null,
3996
-				'uid_file_owner' => 'owner',
3997
-				'displayname_file_owner' => 'owner',
3998
-				'path' => 'file',
3999
-				'item_type' => 'file',
4000
-				'storage_id' => 'storageId',
4001
-				'storage' => 100,
4002
-				'item_source' => 3,
4003
-				'file_source' => 3,
4004
-				'file_parent' => 1,
4005
-				'file_target' => 'myTarget',
4006
-				'share_with' => 'recipient',
4007
-				'share_with_displayname' => 'recipient',
4008
-				'share_with_displayname_unique' => 'recipient',
4009
-				'note' => 'personal note',
4010
-				'label' => '',
4011
-				'mail_send' => 0,
4012
-				'mimetype' => 'myMimeType',
4013
-				'has_preview' => false,
4014
-				'hide_download' => 0,
4015
-				'can_edit' => false,
4016
-				'can_delete' => false,
4017
-				'item_size' => 123456,
4018
-				'item_mtime' => 1234567890,
4019
-				'is-mount-root' => false,
4020
-				'mount-type' => '',
4021
-				'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4022
-				'item_permissions' => 1,
4023
-			], $share, [], false
4024
-		];
4025
-		// User backend up
4026
-		$result[] = [
4027
-			[
4028
-				'id' => '42',
4029
-				'share_type' => IShare::TYPE_USER,
4030
-				'uid_owner' => 'initiator',
4031
-				'displayname_owner' => 'initiatorDN',
4032
-				'permissions' => 1,
4033
-				'attributes' => $shareAttributesReturnJson,
4034
-				'stime' => 946684862,
4035
-				'parent' => null,
4036
-				'expiration' => null,
4037
-				'token' => null,
4038
-				'uid_file_owner' => 'owner',
4039
-				'displayname_file_owner' => 'ownerDN',
4040
-				'note' => 'personal note',
4041
-				'label' => '',
4042
-				'path' => 'file',
4043
-				'item_type' => 'file',
4044
-				'storage_id' => 'storageId',
4045
-				'storage' => 100,
4046
-				'item_source' => 3,
4047
-				'file_source' => 3,
4048
-				'file_parent' => 1,
4049
-				'file_target' => 'myTarget',
4050
-				'share_with' => 'recipient',
4051
-				'share_with_displayname' => 'recipientDN',
4052
-				'share_with_displayname_unique' => 'recipient',
4053
-				'mail_send' => 0,
4054
-				'mimetype' => 'myMimeType',
4055
-				'has_preview' => false,
4056
-				'hide_download' => 0,
4057
-				'can_edit' => false,
4058
-				'can_delete' => false,
4059
-				'item_size' => 123456,
4060
-				'item_mtime' => 1234567890,
4061
-				'is-mount-root' => false,
4062
-				'mount-type' => '',
4063
-				'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4064
-				'item_permissions' => 1,
4065
-			], $share, [
4066
-				['owner', $owner],
4067
-				['initiator', $initiator],
4068
-				['recipient', $recipient],
4069
-			], false
4070
-		];
4071
-
4072
-		$share = Server::get(IManager::class)->newShare();
4073
-		$share->setShareType(IShare::TYPE_USER)
4074
-			->setSharedWith('recipient')
4075
-			->setSharedBy('initiator')
4076
-			->setShareOwner('owner')
4077
-			->setPermissions(Constants::PERMISSION_READ)
4078
-			->setNode($file)
4079
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4080
-			->setTarget('myTarget')
4081
-			->setNote('personal note')
4082
-			->setId(42);
4083
-		// User backend down
4084
-		$result[] = [
4085
-			[
4086
-				'id' => '42',
4087
-				'share_type' => IShare::TYPE_USER,
4088
-				'uid_owner' => 'initiator',
4089
-				'displayname_owner' => 'initiator',
4090
-				'permissions' => 1,
4091
-				'attributes' => null,
4092
-				'stime' => 946684862,
4093
-				'parent' => null,
4094
-				'expiration' => null,
4095
-				'token' => null,
4096
-				'uid_file_owner' => 'owner',
4097
-				'displayname_file_owner' => 'owner',
4098
-				'note' => 'personal note',
4099
-				'label' => '',
4100
-				'path' => 'file',
4101
-				'item_type' => 'file',
4102
-				'storage_id' => 'storageId',
4103
-				'storage' => 100,
4104
-				'item_source' => 3,
4105
-				'file_source' => 3,
4106
-				'file_parent' => 1,
4107
-				'file_target' => 'myTarget',
4108
-				'share_with' => 'recipient',
4109
-				'share_with_displayname' => 'recipient',
4110
-				'share_with_displayname_unique' => 'recipient',
4111
-				'mail_send' => 0,
4112
-				'mimetype' => 'myMimeType',
4113
-				'has_preview' => false,
4114
-				'hide_download' => 0,
4115
-				'can_edit' => false,
4116
-				'can_delete' => false,
4117
-				'item_size' => 123456,
4118
-				'item_mtime' => 1234567890,
4119
-				'is-mount-root' => false,
4120
-				'mount-type' => '',
4121
-				'attributes' => null,
4122
-				'item_permissions' => 1,
4123
-			], $share, [], false
4124
-		];
4125
-
4126
-		$share = Server::get(IManager::class)->newShare();
4127
-		$share->setShareType(IShare::TYPE_USER)
4128
-			->setSharedWith('recipient')
4129
-			->setSharedBy('initiator')
4130
-			->setShareOwner('currentUser')
4131
-			->setPermissions(Constants::PERMISSION_READ)
4132
-			->setNode($file)
4133
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4134
-			->setTarget('myTarget')
4135
-			->setNote('personal note')
4136
-			->setId(42);
4137
-		// User backend down
4138
-		$result[] = [
4139
-			[
4140
-				'id' => '42',
4141
-				'share_type' => IShare::TYPE_USER,
4142
-				'uid_owner' => 'initiator',
4143
-				'displayname_owner' => 'initiator',
4144
-				'permissions' => 1,
4145
-				'attributes' => null,
4146
-				'stime' => 946684862,
4147
-				'parent' => null,
4148
-				'expiration' => null,
4149
-				'token' => null,
4150
-				'uid_file_owner' => 'currentUser',
4151
-				'displayname_file_owner' => 'currentUser',
4152
-				'note' => 'personal note',
4153
-				'label' => '',
4154
-				'path' => 'file',
4155
-				'item_type' => 'file',
4156
-				'storage_id' => 'storageId',
4157
-				'storage' => 100,
4158
-				'item_source' => 3,
4159
-				'file_source' => 3,
4160
-				'file_parent' => 1,
4161
-				'file_target' => 'myTarget',
4162
-				'share_with' => 'recipient',
4163
-				'share_with_displayname' => 'recipient',
4164
-				'share_with_displayname_unique' => 'recipient',
4165
-				'mail_send' => 0,
4166
-				'mimetype' => 'myMimeType',
4167
-				'has_preview' => false,
4168
-				'hide_download' => 0,
4169
-				'can_edit' => true,
4170
-				'can_delete' => true,
4171
-				'item_size' => 123456,
4172
-				'item_mtime' => 1234567890,
4173
-				'is-mount-root' => false,
4174
-				'mount-type' => '',
4175
-				'attributes' => null,
4176
-				'item_permissions' => 11,
4177
-			], $share, [], false
4178
-		];
4179
-
4180
-		// with existing group
4181
-
4182
-		$share = Server::get(IManager::class)->newShare();
4183
-		$share->setShareType(IShare::TYPE_GROUP)
4184
-			->setSharedWith('recipientGroup')
4185
-			->setSharedBy('initiator')
4186
-			->setShareOwner('owner')
4187
-			->setPermissions(Constants::PERMISSION_READ)
4188
-			->setNode($file)
4189
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4190
-			->setTarget('myTarget')
4191
-			->setNote('personal note')
4192
-			->setId(42);
4193
-
4194
-		$result[] = [
4195
-			[
4196
-				'id' => '42',
4197
-				'share_type' => IShare::TYPE_GROUP,
4198
-				'uid_owner' => 'initiator',
4199
-				'displayname_owner' => 'initiator',
4200
-				'permissions' => 1,
4201
-				'attributes' => null,
4202
-				'stime' => 946684862,
4203
-				'parent' => null,
4204
-				'expiration' => null,
4205
-				'token' => null,
4206
-				'uid_file_owner' => 'owner',
4207
-				'displayname_file_owner' => 'owner',
4208
-				'note' => 'personal note',
4209
-				'label' => '',
4210
-				'path' => 'file',
4211
-				'item_type' => 'file',
4212
-				'storage_id' => 'storageId',
4213
-				'storage' => 100,
4214
-				'item_source' => 3,
4215
-				'file_source' => 3,
4216
-				'file_parent' => 1,
4217
-				'file_target' => 'myTarget',
4218
-				'share_with' => 'recipientGroup',
4219
-				'share_with_displayname' => 'recipientGroupDisplayName',
4220
-				'mail_send' => 0,
4221
-				'mimetype' => 'myMimeType',
4222
-				'has_preview' => false,
4223
-				'hide_download' => 0,
4224
-				'can_edit' => false,
4225
-				'can_delete' => false,
4226
-				'item_size' => 123456,
4227
-				'item_mtime' => 1234567890,
4228
-				'is-mount-root' => false,
4229
-				'mount-type' => '',
4230
-				'attributes' => null,
4231
-				'item_permissions' => 1,
4232
-			], $share, [], false
4233
-		];
4234
-
4235
-		// with unknown group / no group backend
4236
-		$share = Server::get(IManager::class)->newShare();
4237
-		$share->setShareType(IShare::TYPE_GROUP)
4238
-			->setSharedWith('recipientGroup2')
4239
-			->setSharedBy('initiator')
4240
-			->setShareOwner('owner')
4241
-			->setPermissions(Constants::PERMISSION_READ)
4242
-			->setNode($file)
4243
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4244
-			->setTarget('myTarget')
4245
-			->setNote('personal note')
4246
-			->setId(42);
4247
-		$result[] = [
4248
-			[
4249
-				'id' => '42',
4250
-				'share_type' => IShare::TYPE_GROUP,
4251
-				'uid_owner' => 'initiator',
4252
-				'displayname_owner' => 'initiator',
4253
-				'permissions' => 1,
4254
-				'stime' => 946684862,
4255
-				'parent' => null,
4256
-				'expiration' => null,
4257
-				'token' => null,
4258
-				'uid_file_owner' => 'owner',
4259
-				'displayname_file_owner' => 'owner',
4260
-				'note' => 'personal note',
4261
-				'label' => '',
4262
-				'path' => 'file',
4263
-				'item_type' => 'file',
4264
-				'storage_id' => 'storageId',
4265
-				'storage' => 100,
4266
-				'item_source' => 3,
4267
-				'file_source' => 3,
4268
-				'file_parent' => 1,
4269
-				'file_target' => 'myTarget',
4270
-				'share_with' => 'recipientGroup2',
4271
-				'share_with_displayname' => 'recipientGroup2',
4272
-				'mail_send' => 0,
4273
-				'mimetype' => 'myMimeType',
4274
-				'has_preview' => false,
4275
-				'hide_download' => 0,
4276
-				'can_edit' => false,
4277
-				'can_delete' => false,
4278
-				'item_size' => 123456,
4279
-				'item_mtime' => 1234567890,
4280
-				'is-mount-root' => false,
4281
-				'mount-type' => '',
4282
-				'attributes' => null,
4283
-				'item_permissions' => 1,
4284
-			], $share, [], false
4285
-		];
4286
-
4287
-		$share = Server::get(IManager::class)->newShare();
4288
-		$share->setShareType(IShare::TYPE_LINK)
4289
-			->setSharedBy('initiator')
4290
-			->setShareOwner('owner')
4291
-			->setPermissions(Constants::PERMISSION_READ)
4292
-			->setNode($file)
4293
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4294
-			->setTarget('myTarget')
4295
-			->setPassword('mypassword')
4296
-			->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4297
-			->setToken('myToken')
4298
-			->setNote('personal note')
4299
-			->setLabel('new link share')
4300
-			->setId(42);
4301
-
4302
-		$result[] = [
4303
-			[
4304
-				'id' => '42',
4305
-				'share_type' => IShare::TYPE_LINK,
4306
-				'uid_owner' => 'initiator',
4307
-				'displayname_owner' => 'initiator',
4308
-				'permissions' => 1,
4309
-				'attributes' => null,
4310
-				'stime' => 946684862,
4311
-				'parent' => null,
4312
-				'expiration' => '2001-01-02 00:00:00',
4313
-				'token' => 'myToken',
4314
-				'uid_file_owner' => 'owner',
4315
-				'displayname_file_owner' => 'owner',
4316
-				'note' => 'personal note',
4317
-				'label' => 'new link share',
4318
-				'path' => 'file',
4319
-				'item_type' => 'file',
4320
-				'storage_id' => 'storageId',
4321
-				'storage' => 100,
4322
-				'item_source' => 3,
4323
-				'file_source' => 3,
4324
-				'file_parent' => 1,
4325
-				'file_target' => 'myTarget',
4326
-				'password' => 'mypassword',
4327
-				'share_with' => 'mypassword',
4328
-				'share_with_displayname' => '(Shared link)',
4329
-				'send_password_by_talk' => false,
4330
-				'mail_send' => 0,
4331
-				'url' => 'myLink',
4332
-				'mimetype' => 'myMimeType',
4333
-				'has_preview' => false,
4334
-				'hide_download' => 0,
4335
-				'can_edit' => false,
4336
-				'can_delete' => false,
4337
-				'item_size' => 123456,
4338
-				'item_mtime' => 1234567890,
4339
-				'is-mount-root' => false,
4340
-				'mount-type' => '',
4341
-				'attributes' => null,
4342
-				'item_permissions' => 1,
4343
-			], $share, [], false
4344
-		];
4345
-
4346
-		$share = Server::get(IManager::class)->newShare();
4347
-		$share->setShareType(IShare::TYPE_LINK)
4348
-			->setSharedBy('initiator')
4349
-			->setShareOwner('owner')
4350
-			->setPermissions(Constants::PERMISSION_READ)
4351
-			->setNode($file)
4352
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4353
-			->setTarget('myTarget')
4354
-			->setPassword('mypassword')
4355
-			->setSendPasswordByTalk(true)
4356
-			->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4357
-			->setToken('myToken')
4358
-			->setNote('personal note')
4359
-			->setLabel('new link share')
4360
-			->setId(42);
4361
-
4362
-		$result[] = [
4363
-			[
4364
-				'id' => '42',
4365
-				'share_type' => IShare::TYPE_LINK,
4366
-				'uid_owner' => 'initiator',
4367
-				'displayname_owner' => 'initiator',
4368
-				'permissions' => 1,
4369
-				'stime' => 946684862,
4370
-				'parent' => null,
4371
-				'expiration' => '2001-01-02 00:00:00',
4372
-				'token' => 'myToken',
4373
-				'uid_file_owner' => 'owner',
4374
-				'displayname_file_owner' => 'owner',
4375
-				'note' => 'personal note',
4376
-				'label' => 'new link share',
4377
-				'path' => 'file',
4378
-				'item_type' => 'file',
4379
-				'storage_id' => 'storageId',
4380
-				'storage' => 100,
4381
-				'item_source' => 3,
4382
-				'file_source' => 3,
4383
-				'file_parent' => 1,
4384
-				'file_target' => 'myTarget',
4385
-				'password' => 'mypassword',
4386
-				'share_with' => 'mypassword',
4387
-				'share_with_displayname' => '(Shared link)',
4388
-				'send_password_by_talk' => true,
4389
-				'mail_send' => 0,
4390
-				'url' => 'myLink',
4391
-				'mimetype' => 'myMimeType',
4392
-				'has_preview' => false,
4393
-				'hide_download' => 0,
4394
-				'can_edit' => false,
4395
-				'can_delete' => false,
4396
-				'item_size' => 123456,
4397
-				'item_mtime' => 1234567890,
4398
-				'is-mount-root' => false,
4399
-				'mount-type' => '',
4400
-				'attributes' => null,
4401
-				'item_permissions' => 1,
4402
-			], $share, [], false
4403
-		];
4404
-
4405
-		$share = Server::get(IManager::class)->newShare();
4406
-		$share->setShareType(IShare::TYPE_REMOTE)
4407
-			->setSharedBy('initiator')
4408
-			->setSharedWith('[email protected]')
4409
-			->setShareOwner('owner')
4410
-			->setPermissions(Constants::PERMISSION_READ)
4411
-			->setNode($folder)
4412
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4413
-			->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4414
-			->setTarget('myTarget')
4415
-			->setNote('personal note')
4416
-			->setId(42);
4417
-
4418
-		$result[] = [
4419
-			[
4420
-				'id' => '42',
4421
-				'share_type' => IShare::TYPE_REMOTE,
4422
-				'uid_owner' => 'initiator',
4423
-				'displayname_owner' => 'initiator',
4424
-				'permissions' => 1,
4425
-				'stime' => 946684862,
4426
-				'parent' => null,
4427
-				'expiration' => '2001-02-03 00:00:00',
4428
-				'token' => null,
4429
-				'uid_file_owner' => 'owner',
4430
-				'displayname_file_owner' => 'owner',
4431
-				'note' => 'personal note',
4432
-				'label' => '',
4433
-				'path' => 'folder',
4434
-				'item_type' => 'folder',
4435
-				'storage_id' => 'storageId',
4436
-				'storage' => 100,
4437
-				'item_source' => 2,
4438
-				'file_source' => 2,
4439
-				'file_parent' => 1,
4440
-				'file_target' => 'myTarget',
4441
-				'share_with' => '[email protected]',
4442
-				'share_with_displayname' => 'foobar',
4443
-				'mail_send' => 0,
4444
-				'mimetype' => 'myFolderMimeType',
4445
-				'has_preview' => false,
4446
-				'hide_download' => 0,
4447
-				'can_edit' => false,
4448
-				'can_delete' => false,
4449
-				'item_size' => 123456,
4450
-				'item_mtime' => 1234567890,
4451
-				'is-mount-root' => false,
4452
-				'mount-type' => '',
4453
-				'attributes' => null,
4454
-				'item_permissions' => 1,
4455
-			], $share, [], false
4456
-		];
4457
-
4458
-		$share = Server::get(IManager::class)->newShare();
4459
-		$share->setShareType(IShare::TYPE_REMOTE_GROUP)
4460
-			->setSharedBy('initiator')
4461
-			->setSharedWith('[email protected]')
4462
-			->setShareOwner('owner')
4463
-			->setPermissions(Constants::PERMISSION_READ)
4464
-			->setNode($folder)
4465
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4466
-			->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4467
-			->setTarget('myTarget')
4468
-			->setNote('personal note')
4469
-			->setId(42);
4470
-
4471
-		$result[] = [
4472
-			[
4473
-				'id' => '42',
4474
-				'share_type' => IShare::TYPE_REMOTE_GROUP,
4475
-				'uid_owner' => 'initiator',
4476
-				'displayname_owner' => 'initiator',
4477
-				'permissions' => 1,
4478
-				'stime' => 946684862,
4479
-				'parent' => null,
4480
-				'expiration' => '2001-02-03 00:00:00',
4481
-				'token' => null,
4482
-				'uid_file_owner' => 'owner',
4483
-				'displayname_file_owner' => 'owner',
4484
-				'note' => 'personal note',
4485
-				'label' => '',
4486
-				'path' => 'folder',
4487
-				'item_type' => 'folder',
4488
-				'storage_id' => 'storageId',
4489
-				'storage' => 100,
4490
-				'item_source' => 2,
4491
-				'file_source' => 2,
4492
-				'file_parent' => 1,
4493
-				'file_target' => 'myTarget',
4494
-				'share_with' => '[email protected]',
4495
-				'share_with_displayname' => 'foobar',
4496
-				'mail_send' => 0,
4497
-				'mimetype' => 'myFolderMimeType',
4498
-				'has_preview' => false,
4499
-				'hide_download' => 0,
4500
-				'can_edit' => false,
4501
-				'can_delete' => false,
4502
-				'item_size' => 123456,
4503
-				'item_mtime' => 1234567890,
4504
-				'is-mount-root' => false,
4505
-				'mount-type' => '',
4506
-				'attributes' => null,
4507
-				'item_permissions' => 1,
4508
-			], $share, [], false
4509
-		];
4510
-
4511
-		// Circle with id, display name and avatar set by the Circles app
4512
-		$share = Server::get(IManager::class)->newShare();
4513
-		$share->setShareType(IShare::TYPE_CIRCLE)
4514
-			->setSharedBy('initiator')
4515
-			->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4516
-			->setSharedWithDisplayName('The display name')
4517
-			->setSharedWithAvatar('path/to/the/avatar')
4518
-			->setShareOwner('owner')
4519
-			->setPermissions(Constants::PERMISSION_READ)
4520
-			->setNode($folder)
4521
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4522
-			->setTarget('myTarget')
4523
-			->setId(42);
4524
-
4525
-		$result[] = [
4526
-			[
4527
-				'id' => '42',
4528
-				'share_type' => IShare::TYPE_CIRCLE,
4529
-				'uid_owner' => 'initiator',
4530
-				'displayname_owner' => 'initiator',
4531
-				'permissions' => 1,
4532
-				'attributes' => null,
4533
-				'stime' => 946684862,
4534
-				'parent' => null,
4535
-				'expiration' => null,
4536
-				'token' => null,
4537
-				'uid_file_owner' => 'owner',
4538
-				'displayname_file_owner' => 'owner',
4539
-				'note' => '',
4540
-				'label' => '',
4541
-				'path' => 'folder',
4542
-				'item_type' => 'folder',
4543
-				'storage_id' => 'storageId',
4544
-				'storage' => 100,
4545
-				'item_source' => 2,
4546
-				'file_source' => 2,
4547
-				'file_parent' => 1,
4548
-				'file_target' => 'myTarget',
4549
-				'share_with' => '4815162342',
4550
-				'share_with_displayname' => 'The display name',
4551
-				'share_with_avatar' => 'path/to/the/avatar',
4552
-				'mail_send' => 0,
4553
-				'mimetype' => 'myFolderMimeType',
4554
-				'has_preview' => false,
4555
-				'hide_download' => 0,
4556
-				'can_edit' => false,
4557
-				'can_delete' => false,
4558
-				'item_size' => 123456,
4559
-				'item_mtime' => 1234567890,
4560
-				'is-mount-root' => false,
4561
-				'mount-type' => '',
4562
-				'attributes' => null,
4563
-				'item_permissions' => 1,
4564
-			], $share, [], false
4565
-		];
4566
-
4567
-		// Circle with id set by the Circles app
4568
-		$share = Server::get(IManager::class)->newShare();
4569
-		$share->setShareType(IShare::TYPE_CIRCLE)
4570
-			->setSharedBy('initiator')
4571
-			->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4572
-			->setShareOwner('owner')
4573
-			->setPermissions(Constants::PERMISSION_READ)
4574
-			->setNode($folder)
4575
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4576
-			->setTarget('myTarget')
4577
-			->setId(42);
4578
-
4579
-		$result[] = [
4580
-			[
4581
-				'id' => '42',
4582
-				'share_type' => IShare::TYPE_CIRCLE,
4583
-				'uid_owner' => 'initiator',
4584
-				'displayname_owner' => 'initiator',
4585
-				'permissions' => 1,
4586
-				'stime' => 946684862,
4587
-				'parent' => null,
4588
-				'expiration' => null,
4589
-				'token' => null,
4590
-				'uid_file_owner' => 'owner',
4591
-				'displayname_file_owner' => 'owner',
4592
-				'note' => '',
4593
-				'label' => '',
4594
-				'path' => 'folder',
4595
-				'item_type' => 'folder',
4596
-				'storage_id' => 'storageId',
4597
-				'storage' => 100,
4598
-				'item_source' => 2,
4599
-				'file_source' => 2,
4600
-				'file_parent' => 1,
4601
-				'file_target' => 'myTarget',
4602
-				'share_with' => '4815162342',
4603
-				'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4604
-				'share_with_avatar' => '',
4605
-				'mail_send' => 0,
4606
-				'mimetype' => 'myFolderMimeType',
4607
-				'has_preview' => false,
4608
-				'hide_download' => 0,
4609
-				'can_edit' => false,
4610
-				'can_delete' => false,
4611
-				'item_size' => 123456,
4612
-				'item_mtime' => 1234567890,
4613
-				'is-mount-root' => false,
4614
-				'mount-type' => '',
4615
-				'attributes' => null,
4616
-				'item_permissions' => 1,
4617
-			], $share, [], false
4618
-		];
4619
-
4620
-		// Circle with id not set by the Circles app
4621
-		$share = Server::get(IManager::class)->newShare();
4622
-		$share->setShareType(IShare::TYPE_CIRCLE)
4623
-			->setSharedBy('initiator')
4624
-			->setSharedWith('Circle (Public circle, circleOwner)')
4625
-			->setShareOwner('owner')
4626
-			->setPermissions(Constants::PERMISSION_READ)
4627
-			->setNode($folder)
4628
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4629
-			->setTarget('myTarget')
4630
-			->setId(42);
4631
-
4632
-		$result[] = [
4633
-			[
4634
-				'id' => '42',
4635
-				'share_type' => IShare::TYPE_CIRCLE,
4636
-				'uid_owner' => 'initiator',
4637
-				'displayname_owner' => 'initiator',
4638
-				'permissions' => 1,
4639
-				'stime' => 946684862,
4640
-				'parent' => null,
4641
-				'expiration' => null,
4642
-				'token' => null,
4643
-				'uid_file_owner' => 'owner',
4644
-				'displayname_file_owner' => 'owner',
4645
-				'note' => '',
4646
-				'label' => '',
4647
-				'path' => 'folder',
4648
-				'item_type' => 'folder',
4649
-				'storage_id' => 'storageId',
4650
-				'storage' => 100,
4651
-				'item_source' => 2,
4652
-				'file_source' => 2,
4653
-				'file_parent' => 1,
4654
-				'file_target' => 'myTarget',
4655
-				'share_with' => 'Circle',
4656
-				'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4657
-				'share_with_avatar' => '',
4658
-				'mail_send' => 0,
4659
-				'mimetype' => 'myFolderMimeType',
4660
-				'has_preview' => false,
4661
-				'hide_download' => 0,
4662
-				'can_edit' => false,
4663
-				'can_delete' => false,
4664
-				'item_size' => 123456,
4665
-				'item_mtime' => 1234567890,
4666
-				'is-mount-root' => false,
4667
-				'mount-type' => '',
4668
-				'attributes' => null,
4669
-				'item_permissions' => 1,
4670
-			], $share, [], false
4671
-		];
4672
-
4673
-		$share = Server::get(IManager::class)->newShare();
4674
-		$share->setShareType(IShare::TYPE_USER)
4675
-			->setSharedBy('initiator')
4676
-			->setSharedWith('recipient')
4677
-			->setShareOwner('owner')
4678
-			->setPermissions(Constants::PERMISSION_READ)
4679
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4680
-			->setTarget('myTarget')
4681
-			->setNote('personal note')
4682
-			->setId(42);
4683
-
4684
-		$result[] = [
4685
-			[], $share, [], true
4686
-		];
4687
-
4688
-		$share = Server::get(IManager::class)->newShare();
4689
-		$share->setShareType(IShare::TYPE_EMAIL)
4690
-			->setSharedBy('initiator')
4691
-			->setSharedWith('[email protected]')
4692
-			->setShareOwner('owner')
4693
-			->setPermissions(Constants::PERMISSION_READ)
4694
-			->setNode($folder)
4695
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4696
-			->setTarget('myTarget')
4697
-			->setId(42)
4698
-			->setPassword('password');
4699
-
4700
-		$result[] = [
4701
-			[
4702
-				'id' => '42',
4703
-				'share_type' => IShare::TYPE_EMAIL,
4704
-				'uid_owner' => 'initiator',
4705
-				'displayname_owner' => 'initiator',
4706
-				'permissions' => 1,
4707
-				'stime' => 946684862,
4708
-				'parent' => null,
4709
-				'expiration' => null,
4710
-				'token' => null,
4711
-				'uid_file_owner' => 'owner',
4712
-				'displayname_file_owner' => 'owner',
4713
-				'note' => '',
4714
-				'label' => '',
4715
-				'path' => 'folder',
4716
-				'item_type' => 'folder',
4717
-				'storage_id' => 'storageId',
4718
-				'storage' => 100,
4719
-				'item_source' => 2,
4720
-				'file_source' => 2,
4721
-				'file_parent' => 1,
4722
-				'file_target' => 'myTarget',
4723
-				'share_with' => '[email protected]',
4724
-				'share_with_displayname' => 'mail display name',
4725
-				'mail_send' => 0,
4726
-				'mimetype' => 'myFolderMimeType',
4727
-				'has_preview' => false,
4728
-				'password' => 'password',
4729
-				'send_password_by_talk' => false,
4730
-				'hide_download' => 0,
4731
-				'can_edit' => false,
4732
-				'can_delete' => false,
4733
-				'password_expiration_time' => null,
4734
-				'item_size' => 123456,
4735
-				'item_mtime' => 1234567890,
4736
-				'is-mount-root' => false,
4737
-				'mount-type' => '',
4738
-				'attributes' => null,
4739
-				'item_permissions' => 1,
4740
-			], $share, [], false
4741
-		];
4742
-
4743
-		$share = Server::get(IManager::class)->newShare();
4744
-		$share->setShareType(IShare::TYPE_EMAIL)
4745
-			->setSharedBy('initiator')
4746
-			->setSharedWith('[email protected]')
4747
-			->setShareOwner('owner')
4748
-			->setPermissions(Constants::PERMISSION_READ)
4749
-			->setNode($folder)
4750
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4751
-			->setTarget('myTarget')
4752
-			->setId(42)
4753
-			->setPassword('password')
4754
-			->setSendPasswordByTalk(true);
4755
-
4756
-		$result[] = [
4757
-			[
4758
-				'id' => '42',
4759
-				'share_type' => IShare::TYPE_EMAIL,
4760
-				'uid_owner' => 'initiator',
4761
-				'displayname_owner' => 'initiator',
4762
-				'permissions' => 1,
4763
-				'stime' => 946684862,
4764
-				'parent' => null,
4765
-				'expiration' => null,
4766
-				'token' => null,
4767
-				'uid_file_owner' => 'owner',
4768
-				'displayname_file_owner' => 'owner',
4769
-				'note' => '',
4770
-				'label' => '',
4771
-				'path' => 'folder',
4772
-				'item_type' => 'folder',
4773
-				'storage_id' => 'storageId',
4774
-				'storage' => 100,
4775
-				'item_source' => 2,
4776
-				'file_source' => 2,
4777
-				'file_parent' => 1,
4778
-				'file_target' => 'myTarget',
4779
-				'share_with' => '[email protected]',
4780
-				'share_with_displayname' => 'mail display name',
4781
-				'mail_send' => 0,
4782
-				'mimetype' => 'myFolderMimeType',
4783
-				'has_preview' => false,
4784
-				'password' => 'password',
4785
-				'send_password_by_talk' => true,
4786
-				'hide_download' => 0,
4787
-				'can_edit' => false,
4788
-				'can_delete' => false,
4789
-				'password_expiration_time' => null,
4790
-				'item_size' => 123456,
4791
-				'item_mtime' => 1234567890,
4792
-				'is-mount-root' => false,
4793
-				'mount-type' => '',
4794
-				'attributes' => null,
4795
-				'item_permissions' => 1,
4796
-			], $share, [], false
4797
-		];
4798
-
4799
-		// Preview is available
4800
-		$share = Server::get(IManager::class)->newShare();
4801
-		$share->setShareType(IShare::TYPE_USER)
4802
-			->setSharedWith('recipient')
4803
-			->setSharedBy('initiator')
4804
-			->setShareOwner('currentUser')
4805
-			->setPermissions(Constants::PERMISSION_READ)
4806
-			->setNode($fileWithPreview)
4807
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4808
-			->setTarget('myTarget')
4809
-			->setNote('personal note')
4810
-			->setId(42);
4811
-
4812
-		$result[] = [
4813
-			[
4814
-				'id' => '42',
4815
-				'share_type' => IShare::TYPE_USER,
4816
-				'uid_owner' => 'initiator',
4817
-				'displayname_owner' => 'initiator',
4818
-				'permissions' => 1,
4819
-				'stime' => 946684862,
4820
-				'parent' => null,
4821
-				'expiration' => null,
4822
-				'token' => null,
4823
-				'uid_file_owner' => 'currentUser',
4824
-				'displayname_file_owner' => 'currentUser',
4825
-				'note' => 'personal note',
4826
-				'label' => '',
4827
-				'path' => 'fileWithPreview',
4828
-				'item_type' => 'file',
4829
-				'storage_id' => 'storageId',
4830
-				'storage' => 100,
4831
-				'item_source' => 4,
4832
-				'file_source' => 4,
4833
-				'file_parent' => 1,
4834
-				'file_target' => 'myTarget',
4835
-				'share_with' => 'recipient',
4836
-				'share_with_displayname' => 'recipient',
4837
-				'share_with_displayname_unique' => 'recipient',
4838
-				'mail_send' => 0,
4839
-				'mimetype' => 'mimeWithPreview',
4840
-				'has_preview' => true,
4841
-				'hide_download' => 0,
4842
-				'can_edit' => true,
4843
-				'can_delete' => true,
4844
-				'item_size' => 123456,
4845
-				'item_mtime' => 1234567890,
4846
-				'is-mount-root' => false,
4847
-				'mount-type' => '',
4848
-				'attributes' => null,
4849
-				'item_permissions' => 11,
4850
-			], $share, [], false
4851
-		];
4852
-
4853
-		return $result;
4854
-	}
4855
-
4856
-	/**
4857
-	 * @dataProvider dataFormatShare
4858
-	 *
4859
-	 * @param array $expects
4860
-	 * @param IShare $share
4861
-	 * @param array $users
4862
-	 * @param $exception
4863
-	 */
4864
-	public function testFormatShare(array $expects, IShare $share, array $users, $exception): void {
4865
-		$this->userManager->method('get')->willReturnMap($users);
4866
-
4867
-		$recipientGroup = $this->createMock(IGroup::class);
4868
-		$recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
4869
-		$this->groupManager->method('get')->willReturnMap([
4870
-			['recipientGroup', $recipientGroup],
4871
-		]);
4872
-
4873
-		$this->urlGenerator->method('linkToRouteAbsolute')
4874
-			->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
4875
-			->willReturn('myLink');
4876
-
4877
-		$this->rootFolder->method('getUserFolder')
4878
-			->with($this->currentUser)
4879
-			->willReturnSelf();
4880
-		$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
4881
-
4882
-		if (!$exception) {
4883
-			$this->rootFolder->method('getFirstNodeById')
4884
-				->with($share->getNodeId())
4885
-				->willReturn($share->getNode());
4886
-
4887
-			$this->rootFolder->method('getRelativePath')
4888
-				->with($share->getNode()->getPath())
4889
-				->willReturnArgument(0);
4890
-		}
4891
-
4892
-		$cm = $this->createMock(\OCP\Contacts\IManager::class);
4893
-		$this->overwriteService(\OCP\Contacts\IManager::class, $cm);
4894
-
4895
-		$cm->method('search')
4896
-			->willReturnMap([
4897
-				['[email protected]', ['CLOUD'], [
4898
-					'limit' => 1,
4899
-					'enumeration' => false,
4900
-					'strict_search' => true,
4901
-				],
4902
-					[
4903
-						[
4904
-							'CLOUD' => [
4905
-								'[email protected]',
4906
-							],
4907
-							'FN' => 'foobar',
4908
-						],
4909
-					],
4910
-				],
4911
-				['[email protected]', ['EMAIL'], [
4912
-					'limit' => 1,
4913
-					'enumeration' => false,
4914
-					'strict_search' => true,
4915
-				],
4916
-					[
4917
-						[
4918
-							'EMAIL' => [
4919
-								'[email protected]',
4920
-							],
4921
-							'FN' => 'mail display name',
4922
-						],
4923
-					],
4924
-				],
4925
-			]);
4926
-
4927
-		try {
4928
-			$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
4929
-			$this->assertFalse($exception);
4930
-			$this->assertEquals($expects, $result);
4931
-		} catch (NotFoundException $e) {
4932
-			$this->assertTrue($exception);
4933
-		}
4934
-	}
4935
-
4936
-	public function dataFormatRoomShare() {
4937
-		$file = $this->getMockBuilder(File::class)->getMock();
4938
-		$parent = $this->getMockBuilder(Folder::class)->getMock();
4939
-
4940
-		$file->method('getMimeType')->willReturn('myMimeType');
4941
-
4942
-		$file->method('getPath')->willReturn('file');
4943
-
4944
-		$parent->method('getId')->willReturn(1);
4945
-		$file->method('getId')->willReturn(3);
4946
-
4947
-		$file->method('getParent')->willReturn($parent);
4948
-
4949
-		$file->method('getSize')->willReturn(123456);
4950
-		$file->method('getMTime')->willReturn(1234567890);
4951
-
4952
-		$mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
4953
-		$mountPoint->method('getMountType')->willReturn('');
4954
-		$file->method('getMountPoint')->willReturn($mountPoint);
4955
-
4956
-		$cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
4957
-		$cache->method('getNumericStorageId')->willReturn(100);
4958
-		$storage = $this->createMock(IStorage::class);
4959
-		$storage->method('getId')->willReturn('storageId');
4960
-		$storage->method('getCache')->willReturn($cache);
4961
-
4962
-		$file->method('getStorage')->willReturn($storage);
4963
-
4964
-		$result = [];
4965
-
4966
-		$share = Server::get(IManager::class)->newShare();
4967
-		$share->setShareType(IShare::TYPE_ROOM)
4968
-			->setSharedWith('recipientRoom')
4969
-			->setSharedBy('initiator')
4970
-			->setShareOwner('owner')
4971
-			->setPermissions(Constants::PERMISSION_READ)
4972
-			->setNode($file)
4973
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4974
-			->setTarget('myTarget')
4975
-			->setNote('personal note')
4976
-			->setId(42);
4977
-
4978
-		$result[] = [
4979
-			[
4980
-				'id' => '42',
4981
-				'share_type' => IShare::TYPE_ROOM,
4982
-				'uid_owner' => 'initiator',
4983
-				'displayname_owner' => 'initiator',
4984
-				'permissions' => 1,
4985
-				'stime' => 946684862,
4986
-				'parent' => null,
4987
-				'expiration' => null,
4988
-				'token' => null,
4989
-				'uid_file_owner' => 'owner',
4990
-				'displayname_file_owner' => 'owner',
4991
-				'note' => 'personal note',
4992
-				'path' => 'file',
4993
-				'item_type' => 'file',
4994
-				'storage_id' => 'storageId',
4995
-				'storage' => 100,
4996
-				'item_source' => 3,
4997
-				'file_source' => 3,
4998
-				'file_parent' => 1,
4999
-				'file_target' => 'myTarget',
5000
-				'share_with' => 'recipientRoom',
5001
-				'share_with_displayname' => '',
5002
-				'mail_send' => 0,
5003
-				'mimetype' => 'myMimeType',
5004
-				'has_preview' => false,
5005
-				'hide_download' => 0,
5006
-				'label' => '',
5007
-				'can_edit' => false,
5008
-				'can_delete' => false,
5009
-				'item_size' => 123456,
5010
-				'item_mtime' => 1234567890,
5011
-				'is-mount-root' => false,
5012
-				'mount-type' => '',
5013
-				'attributes' => null,
5014
-				'item_permissions' => 1,
5015
-			], $share, false, []
5016
-		];
5017
-
5018
-		$share = Server::get(IManager::class)->newShare();
5019
-		$share->setShareType(IShare::TYPE_ROOM)
5020
-			->setSharedWith('recipientRoom')
5021
-			->setSharedBy('initiator')
5022
-			->setShareOwner('owner')
5023
-			->setPermissions(Constants::PERMISSION_READ)
5024
-			->setNode($file)
5025
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
5026
-			->setTarget('myTarget')
5027
-			->setNote('personal note')
5028
-			->setId(42);
5029
-
5030
-		$result[] = [
5031
-			[
5032
-				'id' => '42',
5033
-				'share_type' => IShare::TYPE_ROOM,
5034
-				'uid_owner' => 'initiator',
5035
-				'displayname_owner' => 'initiator',
5036
-				'permissions' => 1,
5037
-				'stime' => 946684862,
5038
-				'parent' => null,
5039
-				'expiration' => null,
5040
-				'token' => null,
5041
-				'uid_file_owner' => 'owner',
5042
-				'displayname_file_owner' => 'owner',
5043
-				'note' => 'personal note',
5044
-				'path' => 'file',
5045
-				'item_type' => 'file',
5046
-				'storage_id' => 'storageId',
5047
-				'storage' => 100,
5048
-				'item_source' => 3,
5049
-				'file_source' => 3,
5050
-				'file_parent' => 1,
5051
-				'file_target' => 'myTarget',
5052
-				'share_with' => 'recipientRoom',
5053
-				'share_with_displayname' => 'recipientRoomName',
5054
-				'mail_send' => 0,
5055
-				'mimetype' => 'myMimeType',
5056
-				'has_preview' => false,
5057
-				'hide_download' => 0,
5058
-				'label' => '',
5059
-				'can_edit' => false,
5060
-				'can_delete' => false,
5061
-				'item_size' => 123456,
5062
-				'item_mtime' => 1234567890,
5063
-				'is-mount-root' => false,
5064
-				'mount-type' => '',
5065
-				'attributes' => null,
5066
-				'item_permissions' => 9,
5067
-			], $share, true, [
5068
-				'share_with_displayname' => 'recipientRoomName'
5069
-			]
5070
-		];
5071
-
5072
-		return $result;
5073
-	}
5074
-
5075
-	/**
5076
-	 * @dataProvider dataFormatRoomShare
5077
-	 *
5078
-	 * @param array $expects
5079
-	 * @param IShare $share
5080
-	 * @param bool $helperAvailable
5081
-	 * @param array $formatShareByHelper
5082
-	 */
5083
-	public function testFormatRoomShare(array $expects, IShare $share, bool $helperAvailable, array $formatShareByHelper): void {
5084
-		$this->rootFolder->method('getUserFolder')
5085
-			->with($this->currentUser)
5086
-			->willReturnSelf();
5087
-
5088
-		$this->rootFolder->method('getFirstNodeById')
5089
-			->with($share->getNodeId())
5090
-			->willReturn($share->getNode());
5091
-
5092
-		$this->rootFolder->method('getRelativePath')
5093
-			->with($share->getNode()->getPath())
5094
-			->willReturnArgument(0);
5095
-
5096
-		if (!$helperAvailable) {
5097
-			$this->appManager->method('isEnabledForUser')
5098
-				->with('spreed')
5099
-				->willReturn(false);
5100
-		} else {
5101
-			$this->appManager->method('isEnabledForUser')
5102
-				->with('spreed')
5103
-				->willReturn(true);
5104
-
5105
-			// This is not possible anymore with PHPUnit 10+
5106
-			// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
5107
-			// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
5108
-			$helper = $this->getMockBuilder(\stdClass::class)
5109
-				->addMethods(['formatShare', 'canAccessShare'])
5110
-				->getMock();
5111
-			$helper->method('formatShare')
5112
-				->with($share)
5113
-				->willReturn($formatShareByHelper);
5114
-			$helper->method('canAccessShare')
5115
-				->with($share)
5116
-				->willReturn(true);
5117
-
5118
-			$this->serverContainer->method('get')
5119
-				->with('\OCA\Talk\Share\Helper\ShareAPIController')
5120
-				->willReturn($helper);
5121
-		}
5122
-
5123
-		$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
5124
-		$this->assertEquals($expects, $result);
5125
-	}
5126
-
5127
-	/**
5128
-	 * @return list{Folder, Folder}
5129
-	 */
5130
-	private function getNonSharedUserFolder(): array {
5131
-		$node = $this->getMockBuilder(Folder::class)->getMock();
5132
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
5133
-		$storage = $this->createMock(IStorage::class);
5134
-		$storage->method('instanceOfStorage')
5135
-			->willReturnMap([
5136
-				['OCA\Files_Sharing\External\Storage', false],
5137
-				['OCA\Files_Sharing\SharedStorage', false],
5138
-			]);
5139
-		$userFolder->method('getStorage')->willReturn($storage);
5140
-		$node->method('getStorage')->willReturn($storage);
5141
-		$node->method('getId')->willReturn(42);
5142
-		$user = $this->createMock(IUser::class);
5143
-		$user->method('getUID')->willReturn($this->currentUser);
5144
-		$node->method('getOwner')->willReturn($user);
5145
-		return [$userFolder, $node];
5146
-	}
5147
-
5148
-	/**
5149
-	 * @return list{Folder, File}
5150
-	 */
5151
-	private function getNonSharedUserFile(): array {
5152
-		$node = $this->getMockBuilder(File::class)->getMock();
5153
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
5154
-		$storage = $this->createMock(IStorage::class);
5155
-		$storage->method('instanceOfStorage')
5156
-			->willReturnMap([
5157
-				['OCA\Files_Sharing\External\Storage', false],
5158
-				['OCA\Files_Sharing\SharedStorage', false],
5159
-			]);
5160
-		$userFolder->method('getStorage')->willReturn($storage);
5161
-		$node->method('getStorage')->willReturn($storage);
5162
-		$node->method('getId')->willReturn(42);
5163
-		return [$userFolder, $node];
5164
-	}
5165
-
5166
-	public function testPopulateTags(): void {
5167
-		$tagger = $this->createMock(ITags::class);
5168
-		$this->tagManager->method('load')
5169
-			->with('files')
5170
-			->willReturn($tagger);
5171
-		$data = [
5172
-			['file_source' => 10],
5173
-			['file_source' => 22, 'foo' => 'bar'],
5174
-			['file_source' => 42, 'x' => 'y'],
5175
-		];
5176
-		$tags = [
5177
-			10 => ['tag3'],
5178
-			42 => ['tag1', 'tag2'],
5179
-		];
5180
-		$tagger->method('getTagsForObjects')
5181
-			->with([10, 22, 42])
5182
-			->willReturn($tags);
5183
-
5184
-		$result = self::invokePrivate($this->ocs, 'populateTags', [$data]);
5185
-		$this->assertSame([
5186
-			['file_source' => 10, 'tags' => ['tag3']],
5187
-			['file_source' => 22, 'foo' => 'bar', 'tags' => []],
5188
-			['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
5189
-		], $result);
5190
-	}
2084
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2085
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($file);
2086
+        $this->rootFolder->method('getById')
2087
+            ->willReturn([]);
2088
+
2089
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2090
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2091
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2092
+
2093
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2094
+    }
2095
+
2096
+    public function testCreateShareLinkPublicUploadFolder(): void {
2097
+        $ocs = $this->mockFormatShare();
2098
+
2099
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2100
+        $path->method('getId')->willReturn(1);
2101
+        $storage = $this->createMock(IStorage::class);
2102
+        $storage->method('instanceOfStorage')
2103
+            ->willReturnMap([
2104
+                ['OCA\Files_Sharing\External\Storage', false],
2105
+                ['OCA\Files_Sharing\SharedStorage', false],
2106
+            ]);
2107
+        $path->method('getStorage')->willReturn($storage);
2108
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2109
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2110
+        $this->rootFolder->method('getById')
2111
+            ->willReturn([]);
2112
+
2113
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2114
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2115
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2116
+
2117
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2118
+            $this->callback(function (IShare $share) use ($path) {
2119
+                return $share->getNode() === $path &&
2120
+                    $share->getShareType() === IShare::TYPE_LINK &&
2121
+                    $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2122
+                    $share->getSharedBy() === 'currentUser' &&
2123
+                    $share->getPassword() === null &&
2124
+                    $share->getExpirationDate() === null;
2125
+            })
2126
+        )->willReturnArgument(0);
2127
+
2128
+        $expected = new DataResponse([]);
2129
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', '', null, '');
2130
+
2131
+        $this->assertInstanceOf(get_class($expected), $result);
2132
+        $this->assertEquals($expected->getData(), $result->getData());
2133
+    }
2134
+
2135
+    public function testCreateShareLinkPassword(): void {
2136
+        $ocs = $this->mockFormatShare();
2137
+
2138
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2139
+        $path->method('getId')->willReturn(42);
2140
+        $storage = $this->createMock(IStorage::class);
2141
+        $storage->method('instanceOfStorage')
2142
+            ->willReturnMap([
2143
+                ['OCA\Files_Sharing\External\Storage', false],
2144
+                ['OCA\Files_Sharing\SharedStorage', false],
2145
+            ]);
2146
+        $path->method('getStorage')->willReturn($storage);
2147
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2148
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2149
+        $this->rootFolder->method('getById')
2150
+            ->willReturn([]);
2151
+
2152
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2153
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2154
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2155
+
2156
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2157
+            $this->callback(function (IShare $share) use ($path) {
2158
+                return $share->getNode() === $path
2159
+                && $share->getShareType() === IShare::TYPE_LINK
2160
+                && $share->getPermissions() === Constants::PERMISSION_READ // publicUpload was set to false
2161
+                && $share->getSharedBy() === 'currentUser'
2162
+                && $share->getPassword() === 'password'
2163
+                && $share->getExpirationDate() === null;
2164
+            })
2165
+        )->willReturnArgument(0);
2166
+
2167
+        $expected = new DataResponse([]);
2168
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_READ, IShare::TYPE_LINK, null, 'false', 'password', null, '');
2169
+
2170
+        $this->assertInstanceOf(get_class($expected), $result);
2171
+        $this->assertEquals($expected->getData(), $result->getData());
2172
+    }
2173
+
2174
+    public function testCreateShareLinkSendPasswordByTalk(): void {
2175
+        $ocs = $this->mockFormatShare();
2176
+
2177
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2178
+        $path->method('getId')->willReturn(42);
2179
+        $storage = $this->createMock(IStorage::class);
2180
+        $storage->method('instanceOfStorage')
2181
+            ->willReturnMap([
2182
+                ['OCA\Files_Sharing\External\Storage', false],
2183
+                ['OCA\Files_Sharing\SharedStorage', false],
2184
+            ]);
2185
+        $path->method('getStorage')->willReturn($storage);
2186
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2187
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2188
+        $this->rootFolder->method('getById')
2189
+            ->willReturn([]);
2190
+
2191
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2192
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2193
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2194
+
2195
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
2196
+
2197
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2198
+            $this->callback(function (IShare $share) use ($path) {
2199
+                return $share->getNode() === $path &&
2200
+                $share->getShareType() === IShare::TYPE_LINK &&
2201
+                $share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) &&
2202
+                $share->getSharedBy() === 'currentUser' &&
2203
+                $share->getPassword() === 'password' &&
2204
+                $share->getSendPasswordByTalk() === true &&
2205
+                $share->getExpirationDate() === null;
2206
+            })
2207
+        )->willReturnArgument(0);
2208
+
2209
+        $expected = new DataResponse([]);
2210
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', 'password', 'true', '');
2211
+
2212
+        $this->assertInstanceOf(get_class($expected), $result);
2213
+        $this->assertEquals($expected->getData(), $result->getData());
2214
+    }
2215
+
2216
+
2217
+    public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled(): void {
2218
+        $this->expectException(OCSForbiddenException::class);
2219
+        $this->expectExceptionMessage('Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled');
2220
+
2221
+        $ocs = $this->mockFormatShare();
2222
+
2223
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2224
+        $path->method('getId')->willReturn(42);
2225
+        $storage = $this->createMock(IStorage::class);
2226
+        $storage->method('instanceOfStorage')
2227
+            ->willReturnMap([
2228
+                ['OCA\Files_Sharing\External\Storage', false],
2229
+                ['OCA\Files_Sharing\SharedStorage', false],
2230
+            ]);
2231
+        $path->method('getStorage')->willReturn($storage);
2232
+        $path->method('getPath')->willReturn('valid-path');
2233
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2234
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2235
+        $this->rootFolder->method('getById')
2236
+            ->willReturn([]);
2237
+
2238
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2239
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2240
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2241
+
2242
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
2243
+
2244
+        $this->shareManager->expects($this->never())->method('createShare');
2245
+
2246
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', '');
2247
+    }
2248
+
2249
+    public function testCreateShareValidExpireDate(): void {
2250
+        $ocs = $this->mockFormatShare();
2251
+
2252
+        $this->request
2253
+            ->method('getParam')
2254
+            ->willReturnMap([
2255
+                ['path', null, 'valid-path'],
2256
+                ['shareType', '-1', IShare::TYPE_LINK],
2257
+                ['publicUpload', null, 'false'],
2258
+                ['expireDate', '', '2000-01-01'],
2259
+                ['password', '', ''],
2260
+            ]);
2261
+
2262
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2263
+        $path->method('getId')->willReturn(42);
2264
+        $storage = $this->createMock(IStorage::class);
2265
+        $storage->method('instanceOfStorage')
2266
+            ->willReturnMap([
2267
+                ['OCA\Files_Sharing\External\Storage', false],
2268
+                ['OCA\Files_Sharing\SharedStorage', false],
2269
+            ]);
2270
+        $path->method('getStorage')->willReturn($storage);
2271
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2272
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2273
+        $this->rootFolder->method('getById')
2274
+            ->willReturn([]);
2275
+
2276
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2277
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2278
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2279
+
2280
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2281
+            $this->callback(function (IShare $share) use ($path) {
2282
+                $date = new \DateTime('2000-01-01');
2283
+                $date->setTime(0, 0, 0);
2284
+
2285
+                return $share->getNode() === $path &&
2286
+                $share->getShareType() === IShare::TYPE_LINK &&
2287
+                $share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE &&
2288
+                $share->getSharedBy() === 'currentUser' &&
2289
+                $share->getPassword() === null &&
2290
+                $share->getExpirationDate() == $date;
2291
+            })
2292
+        )->willReturnArgument(0);
2293
+
2294
+        $expected = new DataResponse([]);
2295
+        $result = $ocs->createShare('valid-path', null, IShare::TYPE_LINK, null, 'false', '', null, '2000-01-01');
2296
+
2297
+        $this->assertInstanceOf(get_class($expected), $result);
2298
+        $this->assertEquals($expected->getData(), $result->getData());
2299
+    }
2300
+
2301
+
2302
+    public function testCreateShareInvalidExpireDate(): void {
2303
+        $this->expectException(OCSNotFoundException::class);
2304
+        $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
2305
+
2306
+        $ocs = $this->mockFormatShare();
2307
+
2308
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2309
+        $path->method('getId')->willReturn(42);
2310
+        $storage = $this->createMock(IStorage::class);
2311
+        $storage->method('instanceOfStorage')
2312
+            ->willReturnMap([
2313
+                ['OCA\Files_Sharing\External\Storage', false],
2314
+                ['OCA\Files_Sharing\SharedStorage', false],
2315
+            ]);
2316
+        $path->method('getStorage')->willReturn($storage);
2317
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2318
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2319
+        $this->rootFolder->method('getById')
2320
+            ->willReturn([]);
2321
+
2322
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2323
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2324
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2325
+
2326
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3');
2327
+    }
2328
+
2329
+    public function testCreateShareRemote(): void {
2330
+        $share = $this->newShare();
2331
+        $this->shareManager->method('newShare')->willReturn($share);
2332
+
2333
+        /** @var ShareAPIController $ocs */
2334
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
2335
+            ->setConstructorArgs([
2336
+                $this->appName,
2337
+                $this->request,
2338
+                $this->shareManager,
2339
+                $this->groupManager,
2340
+                $this->userManager,
2341
+                $this->rootFolder,
2342
+                $this->urlGenerator,
2343
+                $this->l,
2344
+                $this->config,
2345
+                $this->appManager,
2346
+                $this->serverContainer,
2347
+                $this->userStatusManager,
2348
+                $this->previewManager,
2349
+                $this->dateTimeZone,
2350
+                $this->logger,
2351
+                $this->factory,
2352
+                $this->mailer,
2353
+                $this->tagManager,
2354
+                $this->currentUser,
2355
+            ])->onlyMethods(['formatShare'])
2356
+            ->getMock();
2357
+
2358
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2359
+        $this->rootFolder->expects($this->exactly(2))
2360
+            ->method('getUserFolder')
2361
+            ->with('currentUser')
2362
+            ->willReturn($userFolder);
2363
+
2364
+        $userFolder->expects($this->once())
2365
+            ->method('get')
2366
+            ->with('valid-path')
2367
+            ->willReturn($path);
2368
+        $userFolder->method('getById')
2369
+            ->willReturn([]);
2370
+
2371
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
2372
+
2373
+        $path->expects($this->once())
2374
+            ->method('lock')
2375
+            ->with(ILockingProvider::LOCK_SHARED);
2376
+
2377
+        $this->shareManager->method('createShare')
2378
+            ->with($this->callback(function (IShare $share) use ($path) {
2379
+                return $share->getNode() === $path &&
2380
+                    $share->getPermissions() === (
2381
+                        Constants::PERMISSION_ALL &
2382
+                        ~Constants::PERMISSION_DELETE &
2383
+                        ~Constants::PERMISSION_CREATE
2384
+                    ) &&
2385
+                    $share->getShareType() === IShare::TYPE_REMOTE &&
2386
+                    $share->getSharedWith() === '[email protected]' &&
2387
+                    $share->getSharedBy() === 'currentUser';
2388
+            }))
2389
+            ->willReturnArgument(0);
2390
+
2391
+        $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
2392
+
2393
+        $expected = new DataResponse([]);
2394
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, '[email protected]');
2395
+
2396
+        $this->assertInstanceOf(get_class($expected), $result);
2397
+        $this->assertEquals($expected->getData(), $result->getData());
2398
+    }
2399
+
2400
+    public function testCreateShareRemoteGroup(): void {
2401
+        $share = $this->newShare();
2402
+        $this->shareManager->method('newShare')->willReturn($share);
2403
+
2404
+        /** @var ShareAPIController $ocs */
2405
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
2406
+            ->setConstructorArgs([
2407
+                $this->appName,
2408
+                $this->request,
2409
+                $this->shareManager,
2410
+                $this->groupManager,
2411
+                $this->userManager,
2412
+                $this->rootFolder,
2413
+                $this->urlGenerator,
2414
+                $this->l,
2415
+                $this->config,
2416
+                $this->appManager,
2417
+                $this->serverContainer,
2418
+                $this->userStatusManager,
2419
+                $this->previewManager,
2420
+                $this->dateTimeZone,
2421
+                $this->logger,
2422
+                $this->factory,
2423
+                $this->mailer,
2424
+                $this->tagManager,
2425
+                $this->currentUser,
2426
+            ])->onlyMethods(['formatShare'])
2427
+            ->getMock();
2428
+
2429
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2430
+        $this->rootFolder->expects($this->exactly(2))
2431
+            ->method('getUserFolder')
2432
+            ->with('currentUser')
2433
+            ->willReturn($userFolder);
2434
+
2435
+        $userFolder->expects($this->once())
2436
+            ->method('get')
2437
+            ->with('valid-path')
2438
+            ->willReturn($path);
2439
+        $userFolder->method('getById')
2440
+            ->willReturn([]);
2441
+
2442
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
2443
+
2444
+        $path->expects($this->once())
2445
+            ->method('lock')
2446
+            ->with(ILockingProvider::LOCK_SHARED);
2447
+
2448
+        $this->shareManager->method('createShare')
2449
+            ->with($this->callback(function (IShare $share) use ($path) {
2450
+                return $share->getNode() === $path &&
2451
+                    $share->getPermissions() === (
2452
+                        Constants::PERMISSION_ALL &
2453
+                        ~Constants::PERMISSION_DELETE &
2454
+                        ~Constants::PERMISSION_CREATE
2455
+                    ) &&
2456
+                    $share->getShareType() === IShare::TYPE_REMOTE_GROUP &&
2457
+                    $share->getSharedWith() === '[email protected]' &&
2458
+                    $share->getSharedBy() === 'currentUser';
2459
+            }))
2460
+            ->willReturnArgument(0);
2461
+
2462
+        $this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true);
2463
+
2464
+        $expected = new DataResponse([]);
2465
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, '[email protected]');
2466
+
2467
+        $this->assertInstanceOf(get_class($expected), $result);
2468
+        $this->assertEquals($expected->getData(), $result->getData());
2469
+    }
2470
+
2471
+    public function testCreateShareRoom(): void {
2472
+        $ocs = $this->mockFormatShare();
2473
+
2474
+        $share = $this->newShare();
2475
+        $this->shareManager->method('newShare')->willReturn($share);
2476
+
2477
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2478
+        $this->rootFolder->expects($this->exactly(2))
2479
+            ->method('getUserFolder')
2480
+            ->with('currentUser')
2481
+            ->willReturn($userFolder);
2482
+
2483
+        $userFolder->expects($this->once())
2484
+            ->method('get')
2485
+            ->with('valid-path')
2486
+            ->willReturn($path);
2487
+        $userFolder->method('getById')
2488
+            ->willReturn([]);
2489
+
2490
+        $path->expects($this->once())
2491
+            ->method('lock')
2492
+            ->with(ILockingProvider::LOCK_SHARED);
2493
+
2494
+        $this->appManager->method('isEnabledForUser')
2495
+            ->with('spreed')
2496
+            ->willReturn(true);
2497
+
2498
+        // This is not possible anymore with PHPUnit 10+
2499
+        // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2500
+        // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2501
+        $helper = $this->getMockBuilder(\stdClass::class)
2502
+            ->addMethods(['createShare'])
2503
+            ->getMock();
2504
+        $helper->method('createShare')
2505
+            ->with(
2506
+                $share,
2507
+                'recipientRoom',
2508
+                Constants::PERMISSION_ALL &
2509
+                ~Constants::PERMISSION_DELETE &
2510
+                ~Constants::PERMISSION_CREATE,
2511
+                ''
2512
+            )->willReturnCallback(
2513
+                function ($share): void {
2514
+                    $share->setSharedWith('recipientRoom');
2515
+                    $share->setPermissions(Constants::PERMISSION_ALL);
2516
+                }
2517
+            );
2518
+
2519
+        $this->serverContainer->method('get')
2520
+            ->with('\OCA\Talk\Share\Helper\ShareAPIController')
2521
+            ->willReturn($helper);
2522
+
2523
+        $this->shareManager->method('createShare')
2524
+            ->with($this->callback(function (IShare $share) use ($path) {
2525
+                return $share->getNode() === $path
2526
+                    && $share->getPermissions() === Constants::PERMISSION_ALL
2527
+                    && $share->getShareType() === IShare::TYPE_ROOM
2528
+                    && $share->getSharedWith() === 'recipientRoom'
2529
+                    && $share->getSharedBy() === 'currentUser';
2530
+            }))
2531
+            ->willReturnArgument(0);
2532
+
2533
+        $expected = new DataResponse([]);
2534
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2535
+
2536
+        $this->assertInstanceOf(get_class($expected), $result);
2537
+        $this->assertEquals($expected->getData(), $result->getData());
2538
+    }
2539
+
2540
+
2541
+    public function testCreateShareRoomHelperNotAvailable(): void {
2542
+        $this->expectException(OCSForbiddenException::class);
2543
+        $this->expectExceptionMessage('Sharing valid-path failed because the back end does not support room shares');
2544
+
2545
+        $ocs = $this->mockFormatShare();
2546
+
2547
+        $share = $this->newShare();
2548
+        $this->shareManager->method('newShare')->willReturn($share);
2549
+
2550
+        [$userFolder, $path] = $this->getNonSharedUserFolder();
2551
+        $this->rootFolder->method('getUserFolder')
2552
+            ->with('currentUser')
2553
+            ->willReturn($userFolder);
2554
+
2555
+        $path->method('getPath')->willReturn('valid-path');
2556
+        $userFolder->expects($this->once())
2557
+            ->method('get')
2558
+            ->with('valid-path')
2559
+            ->willReturn($path);
2560
+        $userFolder->method('getById')
2561
+            ->willReturn([]);
2562
+
2563
+        $path->expects($this->once())
2564
+            ->method('lock')
2565
+            ->with(ILockingProvider::LOCK_SHARED);
2566
+
2567
+        $this->appManager->method('isEnabledForUser')
2568
+            ->with('spreed')
2569
+            ->willReturn(false);
2570
+
2571
+        $this->shareManager->expects($this->never())->method('createShare');
2572
+
2573
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2574
+    }
2575
+
2576
+
2577
+    public function testCreateShareRoomHelperThrowException(): void {
2578
+        $this->expectException(OCSNotFoundException::class);
2579
+        $this->expectExceptionMessage('Exception thrown by the helper');
2580
+
2581
+        $ocs = $this->mockFormatShare();
2582
+
2583
+        $share = $this->newShare();
2584
+        $share->setSharedBy('currentUser');
2585
+        $this->shareManager->method('newShare')->willReturn($share);
2586
+
2587
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2588
+        $this->rootFolder->method('getUserFolder')
2589
+            ->with('currentUser')
2590
+            ->willReturn($userFolder);
2591
+
2592
+        $userFolder->expects($this->once())
2593
+            ->method('get')
2594
+            ->with('valid-path')
2595
+            ->willReturn($path);
2596
+        $userFolder->method('getById')
2597
+            ->willReturn([]);
2598
+
2599
+        $path->expects($this->once())
2600
+            ->method('lock')
2601
+            ->with(ILockingProvider::LOCK_SHARED);
2602
+
2603
+        $this->appManager->method('isEnabledForUser')
2604
+            ->with('spreed')
2605
+            ->willReturn(true);
2606
+
2607
+        // This is not possible anymore with PHPUnit 10+
2608
+        // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2609
+        // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2610
+        $helper = $this->getMockBuilder(\stdClass::class)
2611
+            ->addMethods(['createShare'])
2612
+            ->getMock();
2613
+        $helper->method('createShare')
2614
+            ->with(
2615
+                $share,
2616
+                'recipientRoom',
2617
+                Constants::PERMISSION_ALL & ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE),
2618
+                ''
2619
+            )->willReturnCallback(
2620
+                function ($share): void {
2621
+                    throw new OCSNotFoundException('Exception thrown by the helper');
2622
+                }
2623
+            );
2624
+
2625
+        $this->serverContainer->method('get')
2626
+            ->with('\OCA\Talk\Share\Helper\ShareAPIController')
2627
+            ->willReturn($helper);
2628
+
2629
+        $this->shareManager->expects($this->never())->method('createShare');
2630
+
2631
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2632
+    }
2633
+
2634
+    /**
2635
+     * Test for https://github.com/owncloud/core/issues/22587
2636
+     * TODO: Remove once proper solution is in place
2637
+     */
2638
+    public function testCreateReshareOfFederatedMountNoDeletePermissions(): void {
2639
+        $share = Server::get(IManager::class)->newShare();
2640
+        $this->shareManager->method('newShare')->willReturn($share);
2641
+
2642
+        /** @var ShareAPIController&MockObject $ocs */
2643
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
2644
+            ->setConstructorArgs([
2645
+                $this->appName,
2646
+                $this->request,
2647
+                $this->shareManager,
2648
+                $this->groupManager,
2649
+                $this->userManager,
2650
+                $this->rootFolder,
2651
+                $this->urlGenerator,
2652
+                $this->l,
2653
+                $this->config,
2654
+                $this->appManager,
2655
+                $this->serverContainer,
2656
+                $this->userStatusManager,
2657
+                $this->previewManager,
2658
+                $this->dateTimeZone,
2659
+                $this->logger,
2660
+                $this->factory,
2661
+                $this->mailer,
2662
+                $this->tagManager,
2663
+                $this->currentUser,
2664
+            ])->onlyMethods(['formatShare'])
2665
+            ->getMock();
2666
+
2667
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
2668
+        $this->rootFolder->expects($this->exactly(2))
2669
+            ->method('getUserFolder')
2670
+            ->with('currentUser')
2671
+            ->willReturn($userFolder);
2672
+
2673
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2674
+        $path->method('getId')->willReturn(42);
2675
+
2676
+        $storage = $this->createMock(IStorage::class);
2677
+        $storage->method('instanceOfStorage')
2678
+            ->willReturnMap([
2679
+                ['OCA\Files_Sharing\External\Storage', true],
2680
+                ['OCA\Files_Sharing\SharedStorage', false],
2681
+            ]);
2682
+        $userFolder->method('getStorage')->willReturn($storage);
2683
+        $path->method('getStorage')->willReturn($storage);
2684
+
2685
+        $path->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
2686
+        $userFolder->expects($this->once())
2687
+            ->method('get')
2688
+            ->with('valid-path')
2689
+            ->willReturn($path);
2690
+        $userFolder->method('getById')
2691
+            ->willReturn([]);
2692
+
2693
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
2694
+
2695
+        $this->shareManager
2696
+            ->expects($this->once())
2697
+            ->method('createShare')
2698
+            ->with($this->callback(function (IShare $share) {
2699
+                return $share->getPermissions() === Constants::PERMISSION_READ;
2700
+            }))
2701
+            ->willReturnArgument(0);
2702
+
2703
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
2704
+    }
2705
+
2706
+
2707
+    public function testUpdateShareCantAccess(): void {
2708
+        $this->expectException(OCSNotFoundException::class);
2709
+        $this->expectExceptionMessage('Wrong share ID, share does not exist');
2710
+
2711
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
2712
+        $share = $this->newShare();
2713
+        $share->setNode($node);
2714
+
2715
+        $node->expects($this->once())
2716
+            ->method('lock')
2717
+            ->with(ILockingProvider::LOCK_SHARED);
2718
+
2719
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2720
+
2721
+        $this->rootFolder->method('getUserFolder')
2722
+            ->with($this->currentUser)
2723
+            ->willReturn($userFolder);
2724
+
2725
+        $userFolder->method('getById')
2726
+            ->with($share->getNodeId())
2727
+            ->willReturn([$share->getNode()]);
2728
+
2729
+        $this->ocs->updateShare(42);
2730
+    }
2731
+
2732
+
2733
+    public function testUpdateNoParametersLink(): void {
2734
+        $this->expectException(OCSBadRequestException::class);
2735
+        $this->expectExceptionMessage('Wrong or no update parameter given');
2736
+
2737
+        $node = $this->getMockBuilder(Folder::class)->getMock();
2738
+        $share = $this->newShare();
2739
+        $share->setPermissions(Constants::PERMISSION_ALL)
2740
+            ->setSharedBy($this->currentUser)
2741
+            ->setShareType(IShare::TYPE_LINK)
2742
+            ->setNode($node);
2743
+
2744
+        $node->expects($this->once())
2745
+            ->method('lock')
2746
+            ->with(ILockingProvider::LOCK_SHARED);
2747
+
2748
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2749
+
2750
+        $this->ocs->updateShare(42);
2751
+    }
2752
+
2753
+
2754
+    public function testUpdateNoParametersOther(): void {
2755
+        $this->expectException(OCSBadRequestException::class);
2756
+        $this->expectExceptionMessage('Wrong or no update parameter given');
2757
+
2758
+        $node = $this->getMockBuilder(Folder::class)->getMock();
2759
+        $share = $this->newShare();
2760
+        $share->setPermissions(Constants::PERMISSION_ALL)
2761
+            ->setSharedBy($this->currentUser)
2762
+            ->setShareType(IShare::TYPE_GROUP)
2763
+            ->setNode($node);
2764
+
2765
+        $node->expects($this->once())
2766
+            ->method('lock')
2767
+            ->with(ILockingProvider::LOCK_SHARED);
2768
+
2769
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2770
+
2771
+        $this->ocs->updateShare(42);
2772
+    }
2773
+
2774
+    public function testUpdateLinkShareClear(): void {
2775
+        $ocs = $this->mockFormatShare();
2776
+
2777
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
2778
+        $node->method('getId')
2779
+            ->willReturn(42);
2780
+        $share = $this->newShare();
2781
+        $share->setPermissions(Constants::PERMISSION_ALL)
2782
+            ->setSharedBy($this->currentUser)
2783
+            ->setShareType(IShare::TYPE_LINK)
2784
+            ->setPassword('password')
2785
+            ->setExpirationDate(new \DateTime())
2786
+            ->setNote('note')
2787
+            ->setLabel('label')
2788
+            ->setHideDownload(true)
2789
+            ->setPermissions(Constants::PERMISSION_ALL)
2790
+            ->setNode($node);
2791
+
2792
+        $node->expects($this->once())
2793
+            ->method('lock')
2794
+            ->with(ILockingProvider::LOCK_SHARED);
2795
+
2796
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2797
+
2798
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
2799
+            $this->callback(function (IShare $share) {
2800
+                return $share->getPermissions() === Constants::PERMISSION_READ &&
2801
+                $share->getPassword() === null &&
2802
+                $share->getExpirationDate() === null &&
2803
+                // Once set a note or a label are never back to null, only to an
2804
+                // empty string.
2805
+                $share->getNote() === '' &&
2806
+                $share->getLabel() === '' &&
2807
+                $share->getHideDownload() === false;
2808
+            })
2809
+        )->willReturnArgument(0);
2810
+
2811
+        $this->shareManager->method('getSharedWith')
2812
+            ->willReturn([]);
2813
+
2814
+        $this->rootFolder->method('getUserFolder')
2815
+            ->with($this->currentUser)
2816
+            ->willReturn($userFolder);
2817
+
2818
+        $userFolder->method('getById')
2819
+            ->with(42)
2820
+            ->willReturn([$node]);
2821
+        $userFolder->method('getFirstNodeById')
2822
+            ->with(42)
2823
+            ->willReturn($node);
2824
+
2825
+        $mountPoint = $this->createMock(IMountPoint::class);
2826
+        $node->method('getMountPoint')
2827
+            ->willReturn($mountPoint);
2828
+        $mountPoint->method('getStorageRootId')
2829
+            ->willReturn(42);
2830
+
2831
+        $expected = new DataResponse([]);
2832
+        $result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false');
2833
+
2834
+        $this->assertInstanceOf(get_class($expected), $result);
2835
+        $this->assertEquals($expected->getData(), $result->getData());
2836
+    }
2837
+
2838
+    public function testUpdateLinkShareSet(): void {
2839
+        $ocs = $this->mockFormatShare();
2840
+
2841
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
2842
+        $folder->method('getId')
2843
+            ->willReturn(42);
2844
+
2845
+        $share = Server::get(IManager::class)->newShare();
2846
+        $share->setPermissions(Constants::PERMISSION_ALL)
2847
+            ->setSharedBy($this->currentUser)
2848
+            ->setShareType(IShare::TYPE_LINK)
2849
+            ->setNode($folder);
2850
+
2851
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2852
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2853
+
2854
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
2855
+            $this->callback(function (IShare $share) {
2856
+                $date = new \DateTime('2000-01-01');
2857
+                $date->setTime(0, 0, 0);
2858
+
2859
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2860
+                $share->getPassword() === 'password' &&
2861
+                $share->getExpirationDate() == $date &&
2862
+                $share->getNote() === 'note' &&
2863
+                $share->getLabel() === 'label' &&
2864
+                $share->getHideDownload() === true;
2865
+            })
2866
+        )->willReturnArgument(0);
2867
+
2868
+        $this->shareManager->method('getSharedWith')
2869
+            ->willReturn([]);
2870
+
2871
+        $this->rootFolder->method('getUserFolder')
2872
+            ->with($this->currentUser)
2873
+            ->willReturn($userFolder);
2874
+
2875
+        $userFolder->method('getById')
2876
+            ->with(42)
2877
+            ->willReturn([$folder]);
2878
+
2879
+        $mountPoint = $this->createMock(IMountPoint::class);
2880
+        $folder->method('getMountPoint')
2881
+            ->willReturn($mountPoint);
2882
+        $mountPoint->method('getStorageRootId')
2883
+            ->willReturn(42);
2884
+
2885
+        $expected = new DataResponse([]);
2886
+        $result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true');
2887
+
2888
+        $this->assertInstanceOf(get_class($expected), $result);
2889
+        $this->assertEquals($expected->getData(), $result->getData());
2890
+    }
2891
+
2892
+    /**
2893
+     * @dataProvider publicUploadParamsProvider
2894
+     */
2895
+    public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password): void {
2896
+        $ocs = $this->mockFormatShare();
2897
+
2898
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
2899
+        $folder->method('getId')
2900
+            ->willReturn(42);
2901
+
2902
+        $share = Server::get(IManager::class)->newShare();
2903
+        $share->setPermissions(Constants::PERMISSION_ALL)
2904
+            ->setSharedBy($this->currentUser)
2905
+            ->setShareType(IShare::TYPE_LINK)
2906
+            ->setPassword('password')
2907
+            ->setNode($folder);
2908
+
2909
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2910
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2911
+        $this->shareManager->method('getSharedWith')->willReturn([]);
2912
+
2913
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
2914
+            $this->callback(function (IShare $share) {
2915
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2916
+                $share->getPassword() === 'password' &&
2917
+                $share->getExpirationDate() === null;
2918
+            })
2919
+        )->willReturnArgument(0);
2920
+
2921
+        $this->rootFolder->method('getUserFolder')
2922
+            ->with($this->currentUser)
2923
+            ->willReturn($userFolder);
2924
+
2925
+        $userFolder->method('getById')
2926
+            ->with(42)
2927
+            ->willReturn([$folder]);
2928
+
2929
+        $mountPoint = $this->createMock(IMountPoint::class);
2930
+        $folder->method('getMountPoint')
2931
+            ->willReturn($mountPoint);
2932
+        $mountPoint->method('getStorageRootId')
2933
+            ->willReturn(42);
2934
+
2935
+        $expected = new DataResponse([]);
2936
+        $result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
2937
+
2938
+        $this->assertInstanceOf(get_class($expected), $result);
2939
+        $this->assertEquals($expected->getData(), $result->getData());
2940
+    }
2941
+
2942
+
2943
+    public function publicLinkValidPermissionsProvider() {
2944
+        return [
2945
+            [Constants::PERMISSION_CREATE],
2946
+            [Constants::PERMISSION_READ],
2947
+            [Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE],
2948
+            [Constants::PERMISSION_READ | Constants::PERMISSION_DELETE],
2949
+            [Constants::PERMISSION_READ | Constants::PERMISSION_CREATE],
2950
+        ];
2951
+    }
2952
+
2953
+    /**
2954
+     * @dataProvider publicLinkValidPermissionsProvider
2955
+     */
2956
+    public function testUpdateLinkShareSetCRUDPermissions($permissions): void {
2957
+        $ocs = $this->mockFormatShare();
2958
+
2959
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
2960
+        $folder->method('getId')
2961
+            ->willReturn(42);
2962
+
2963
+        $share = Server::get(IManager::class)->newShare();
2964
+        $share->setPermissions(Constants::PERMISSION_ALL)
2965
+            ->setSharedBy($this->currentUser)
2966
+            ->setShareType(IShare::TYPE_LINK)
2967
+            ->setPassword('password')
2968
+            ->setNode($folder);
2969
+
2970
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2971
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2972
+        $this->shareManager->method('getSharedWith')->willReturn([]);
2973
+
2974
+        $this->shareManager
2975
+            ->expects($this->any())
2976
+            ->method('updateShare')
2977
+            ->willReturnArgument(0);
2978
+
2979
+        $this->rootFolder->method('getUserFolder')
2980
+            ->with($this->currentUser)
2981
+            ->willReturn($userFolder);
2982
+
2983
+        $userFolder->method('getById')
2984
+            ->with(42)
2985
+            ->willReturn([$folder]);
2986
+
2987
+        $mountPoint = $this->createMock(IMountPoint::class);
2988
+        $folder->method('getMountPoint')
2989
+            ->willReturn($mountPoint);
2990
+        $mountPoint->method('getStorageRootId')
2991
+            ->willReturn(42);
2992
+
2993
+        $expected = new DataResponse([]);
2994
+        $result = $ocs->updateShare(42, $permissions, 'password', null, null, null);
2995
+
2996
+        $this->assertInstanceOf(get_class($expected), $result);
2997
+        $this->assertEquals($expected->getData(), $result->getData());
2998
+    }
2999
+
3000
+    public function publicLinkInvalidPermissionsProvider1() {
3001
+        return [
3002
+            [Constants::PERMISSION_DELETE],
3003
+            [Constants::PERMISSION_UPDATE],
3004
+            [Constants::PERMISSION_SHARE],
3005
+        ];
3006
+    }
3007
+
3008
+    /**
3009
+     * @dataProvider publicLinkInvalidPermissionsProvider1
3010
+     */
3011
+    public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions): void {
3012
+        $this->expectException(OCSBadRequestException::class);
3013
+        $this->expectExceptionMessage('Share must at least have READ or CREATE permissions');
3014
+
3015
+        $this->testUpdateLinkShareSetCRUDPermissions($permissions, null);
3016
+    }
3017
+
3018
+    public function publicLinkInvalidPermissionsProvider2() {
3019
+        return [
3020
+            [Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE],
3021
+            [Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE],
3022
+        ];
3023
+    }
3024
+
3025
+    /**
3026
+     * @dataProvider publicLinkInvalidPermissionsProvider2
3027
+     */
3028
+    public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions): void {
3029
+        $this->expectException(OCSBadRequestException::class);
3030
+        $this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set');
3031
+
3032
+        $this->testUpdateLinkShareSetCRUDPermissions($permissions);
3033
+    }
3034
+
3035
+    public function testUpdateLinkShareInvalidDate(): void {
3036
+        $this->expectException(OCSBadRequestException::class);
3037
+        $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
3038
+
3039
+        $ocs = $this->mockFormatShare();
3040
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3041
+        $userFolder->method('getById')
3042
+            ->with(42)
3043
+            ->willReturn([$folder]);
3044
+        $this->rootFolder->method('getUserFolder')
3045
+            ->with($this->currentUser)
3046
+            ->willReturn($userFolder);
3047
+
3048
+        $folder->method('getId')
3049
+            ->willReturn(42);
3050
+
3051
+        $share = Server::get(IManager::class)->newShare();
3052
+        $share->setPermissions(Constants::PERMISSION_ALL)
3053
+            ->setSharedBy($this->currentUser)
3054
+            ->setShareType(IShare::TYPE_LINK)
3055
+            ->setNode($folder);
3056
+
3057
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3058
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3059
+
3060
+        $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a');
3061
+    }
3062
+
3063
+    public function publicUploadParamsProvider() {
3064
+        return [
3065
+            [null, 'true', null, 'password'],
3066
+            // legacy had no delete
3067
+            [
3068
+                Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE,
3069
+                'true', null, 'password'
3070
+            ],
3071
+            // correct
3072
+            [
3073
+                Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE,
3074
+                null, null, 'password'
3075
+            ],
3076
+        ];
3077
+    }
3078
+
3079
+    /**
3080
+     * @dataProvider publicUploadParamsProvider
3081
+     */
3082
+    public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password): void {
3083
+        $this->expectException(OCSForbiddenException::class);
3084
+        $this->expectExceptionMessage('Public upload disabled by the administrator');
3085
+
3086
+        $ocs = $this->mockFormatShare();
3087
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3088
+        $userFolder->method('getById')
3089
+            ->with(42)
3090
+            ->willReturn([$folder]);
3091
+        $this->rootFolder->method('getUserFolder')
3092
+            ->with($this->currentUser)
3093
+            ->willReturn($userFolder);
3094
+
3095
+        $folder->method('getId')->willReturn(42);
3096
+
3097
+        $share = Server::get(IManager::class)->newShare();
3098
+        $share->setPermissions(Constants::PERMISSION_ALL)
3099
+            ->setSharedBy($this->currentUser)
3100
+            ->setShareType(IShare::TYPE_LINK)
3101
+            ->setNode($folder);
3102
+
3103
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3104
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
3105
+
3106
+        $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
3107
+    }
3108
+
3109
+
3110
+    public function testUpdateLinkSharePublicUploadOnFile(): void {
3111
+        $this->expectException(OCSBadRequestException::class);
3112
+        $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
3113
+
3114
+        $ocs = $this->mockFormatShare();
3115
+
3116
+        $file = $this->getMockBuilder(File::class)->getMock();
3117
+        $file->method('getId')
3118
+            ->willReturn(42);
3119
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3120
+        $userFolder->method('getById')
3121
+            ->with(42)
3122
+            ->willReturn([$folder]);
3123
+        $this->rootFolder->method('getUserFolder')
3124
+            ->with($this->currentUser)
3125
+            ->willReturn($userFolder);
3126
+
3127
+        $share = Server::get(IManager::class)->newShare();
3128
+        $share->setPermissions(Constants::PERMISSION_ALL)
3129
+            ->setSharedBy($this->currentUser)
3130
+            ->setShareType(IShare::TYPE_LINK)
3131
+            ->setNode($file);
3132
+
3133
+        $this->shareManager
3134
+            ->method('getShareById')
3135
+            ->with('ocinternal:42')
3136
+            ->willReturn($share);
3137
+        $this->shareManager
3138
+            ->method('shareApiLinkAllowPublicUpload')
3139
+            ->willReturn(true);
3140
+        $this->shareManager
3141
+            ->method('updateShare')
3142
+            ->with($share)
3143
+            ->willThrowException(new \InvalidArgumentException('File shares cannot have create or delete permissions'));
3144
+
3145
+        $ocs->updateShare(42, null, 'password', null, 'true', '');
3146
+    }
3147
+
3148
+    public function testUpdateLinkSharePasswordDoesNotChangeOther(): void {
3149
+        $ocs = $this->mockFormatShare();
3150
+
3151
+        $date = new \DateTime('2000-01-01');
3152
+        $date->setTime(0, 0, 0);
3153
+
3154
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3155
+        $node->method('getId')->willReturn(42);
3156
+        $userFolder->method('getById')
3157
+            ->with(42)
3158
+            ->willReturn([$node]);
3159
+        $this->rootFolder->method('getUserFolder')
3160
+            ->with($this->currentUser)
3161
+            ->willReturn($userFolder);
3162
+        $share = $this->newShare();
3163
+        $share->setPermissions(Constants::PERMISSION_ALL)
3164
+            ->setSharedBy($this->currentUser)
3165
+            ->setShareType(IShare::TYPE_LINK)
3166
+            ->setPassword('password')
3167
+            ->setSendPasswordByTalk(true)
3168
+            ->setExpirationDate($date)
3169
+            ->setNote('note')
3170
+            ->setLabel('label')
3171
+            ->setHideDownload(true)
3172
+            ->setPermissions(Constants::PERMISSION_ALL)
3173
+            ->setNode($node);
3174
+
3175
+        $node->expects($this->once())
3176
+            ->method('lock')
3177
+            ->with(ILockingProvider::LOCK_SHARED);
3178
+
3179
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3180
+
3181
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3182
+            $this->callback(function (IShare $share) use ($date) {
3183
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3184
+                $share->getPassword() === 'newpassword' &&
3185
+                $share->getSendPasswordByTalk() === true &&
3186
+                $share->getExpirationDate() === $date &&
3187
+                $share->getNote() === 'note' &&
3188
+                $share->getLabel() === 'label' &&
3189
+                $share->getHideDownload() === true;
3190
+            })
3191
+        )->willReturnArgument(0);
3192
+
3193
+        $expected = new DataResponse([]);
3194
+        $result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null);
3195
+
3196
+        $this->assertInstanceOf(get_class($expected), $result);
3197
+        $this->assertEquals($expected->getData(), $result->getData());
3198
+    }
3199
+
3200
+    public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther(): void {
3201
+        $ocs = $this->mockFormatShare();
3202
+
3203
+        $date = new \DateTime('2000-01-01');
3204
+        $date->setTime(0, 0, 0);
3205
+
3206
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3207
+        $userFolder->method('getById')
3208
+            ->with(42)
3209
+            ->willReturn([$node]);
3210
+        $this->rootFolder->method('getUserFolder')
3211
+            ->with($this->currentUser)
3212
+            ->willReturn($userFolder);
3213
+        $node->method('getId')->willReturn(42);
3214
+        $share = $this->newShare();
3215
+        $share->setPermissions(Constants::PERMISSION_ALL)
3216
+            ->setSharedBy($this->currentUser)
3217
+            ->setShareType(IShare::TYPE_LINK)
3218
+            ->setPassword('password')
3219
+            ->setSendPasswordByTalk(false)
3220
+            ->setExpirationDate($date)
3221
+            ->setNote('note')
3222
+            ->setLabel('label')
3223
+            ->setHideDownload(true)
3224
+            ->setPermissions(Constants::PERMISSION_ALL)
3225
+            ->setNode($node);
3226
+
3227
+        $node->expects($this->once())
3228
+            ->method('lock')
3229
+            ->with(ILockingProvider::LOCK_SHARED);
3230
+
3231
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3232
+
3233
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3234
+
3235
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3236
+            $this->callback(function (IShare $share) use ($date) {
3237
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3238
+                $share->getPassword() === 'password' &&
3239
+                $share->getSendPasswordByTalk() === true &&
3240
+                $share->getExpirationDate() === $date &&
3241
+                $share->getNote() === 'note' &&
3242
+                $share->getLabel() === 'label' &&
3243
+                $share->getHideDownload() === true;
3244
+            })
3245
+        )->willReturnArgument(0);
3246
+
3247
+        $expected = new DataResponse([]);
3248
+        $result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3249
+
3250
+        $this->assertInstanceOf(get_class($expected), $result);
3251
+        $this->assertEquals($expected->getData(), $result->getData());
3252
+    }
3253
+
3254
+
3255
+    public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3256
+        $this->expectException(OCSForbiddenException::class);
3257
+        $this->expectExceptionMessage('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.');
3258
+
3259
+        $ocs = $this->mockFormatShare();
3260
+
3261
+        $date = new \DateTime('2000-01-01');
3262
+        $date->setTime(0, 0, 0);
3263
+
3264
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3265
+        $userFolder->method('getById')
3266
+            ->with(42)
3267
+            ->willReturn([$node]);
3268
+        $this->rootFolder->method('getUserFolder')
3269
+            ->with($this->currentUser)
3270
+            ->willReturn($userFolder);
3271
+        $node->method('getId')->willReturn(42);
3272
+        $share = $this->newShare();
3273
+        $share->setPermissions(Constants::PERMISSION_ALL)
3274
+            ->setSharedBy($this->currentUser)
3275
+            ->setShareType(IShare::TYPE_LINK)
3276
+            ->setPassword('password')
3277
+            ->setSendPasswordByTalk(false)
3278
+            ->setExpirationDate($date)
3279
+            ->setNote('note')
3280
+            ->setLabel('label')
3281
+            ->setHideDownload(true)
3282
+            ->setPermissions(Constants::PERMISSION_ALL)
3283
+            ->setNode($node);
3284
+
3285
+        $node->expects($this->once())
3286
+            ->method('lock')
3287
+            ->with(ILockingProvider::LOCK_SHARED);
3288
+
3289
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3290
+
3291
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3292
+
3293
+        $this->shareManager->expects($this->never())->method('updateShare');
3294
+
3295
+        $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3296
+    }
3297
+
3298
+    public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther(): void {
3299
+        $ocs = $this->mockFormatShare();
3300
+
3301
+        $date = new \DateTime('2000-01-01');
3302
+        $date->setTime(0, 0, 0);
3303
+
3304
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3305
+        $userFolder->method('getById')
3306
+            ->with(42)
3307
+            ->willReturn([$node]);
3308
+        $this->rootFolder->method('getUserFolder')
3309
+            ->with($this->currentUser)
3310
+            ->willReturn($userFolder);
3311
+        $node->method('getId')->willReturn(42);
3312
+        $share = $this->newShare();
3313
+        $share->setPermissions(Constants::PERMISSION_ALL)
3314
+            ->setSharedBy($this->currentUser)
3315
+            ->setShareType(IShare::TYPE_LINK)
3316
+            ->setPassword('password')
3317
+            ->setSendPasswordByTalk(true)
3318
+            ->setExpirationDate($date)
3319
+            ->setNote('note')
3320
+            ->setLabel('label')
3321
+            ->setHideDownload(true)
3322
+            ->setPermissions(Constants::PERMISSION_ALL)
3323
+            ->setNode($node);
3324
+
3325
+        $node->expects($this->once())
3326
+            ->method('lock')
3327
+            ->with(ILockingProvider::LOCK_SHARED);
3328
+
3329
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3330
+
3331
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3332
+
3333
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3334
+            $this->callback(function (IShare $share) use ($date) {
3335
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3336
+                $share->getPassword() === 'password' &&
3337
+                $share->getSendPasswordByTalk() === false &&
3338
+                $share->getExpirationDate() === $date &&
3339
+                $share->getNote() === 'note' &&
3340
+                $share->getLabel() === 'label' &&
3341
+                $share->getHideDownload() === true;
3342
+            })
3343
+        )->willReturnArgument(0);
3344
+
3345
+        $expected = new DataResponse([]);
3346
+        $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3347
+
3348
+        $this->assertInstanceOf(get_class($expected), $result);
3349
+        $this->assertEquals($expected->getData(), $result->getData());
3350
+    }
3351
+
3352
+    public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3353
+        $ocs = $this->mockFormatShare();
3354
+
3355
+        $date = new \DateTime('2000-01-01');
3356
+        $date->setTime(0, 0, 0);
3357
+
3358
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3359
+        $node->method('getId')
3360
+            ->willReturn(42);
3361
+
3362
+        $share = $this->newShare();
3363
+        $share->setPermissions(Constants::PERMISSION_ALL)
3364
+            ->setSharedBy($this->currentUser)
3365
+            ->setShareType(IShare::TYPE_LINK)
3366
+            ->setPassword('password')
3367
+            ->setSendPasswordByTalk(true)
3368
+            ->setExpirationDate($date)
3369
+            ->setNote('note')
3370
+            ->setLabel('label')
3371
+            ->setHideDownload(true)
3372
+            ->setPermissions(Constants::PERMISSION_ALL)
3373
+            ->setNode($node);
3374
+
3375
+        $node->expects($this->once())
3376
+            ->method('lock')
3377
+            ->with(ILockingProvider::LOCK_SHARED);
3378
+
3379
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3380
+
3381
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3382
+
3383
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3384
+            $this->callback(function (IShare $share) use ($date) {
3385
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3386
+                $share->getPassword() === 'password' &&
3387
+                $share->getSendPasswordByTalk() === false &&
3388
+                $share->getExpirationDate() === $date &&
3389
+                $share->getNote() === 'note' &&
3390
+                $share->getLabel() === 'label' &&
3391
+                $share->getHideDownload() === true;
3392
+            })
3393
+        )->willReturnArgument(0);
3394
+
3395
+        $this->rootFolder->method('getUserFolder')
3396
+            ->with($this->currentUser)
3397
+            ->willReturn($userFolder);
3398
+
3399
+        $userFolder->method('getById')
3400
+            ->with(42)
3401
+            ->willReturn([$node]);
3402
+
3403
+        $mountPoint = $this->createMock(IMountPoint::class);
3404
+        $node->method('getMountPoint')
3405
+            ->willReturn($mountPoint);
3406
+        $mountPoint->method('getStorageRootId')
3407
+            ->willReturn(42);
3408
+
3409
+        $mountPoint = $this->createMock(IMountPoint::class);
3410
+        $node->method('getMountPoint')
3411
+            ->willReturn($mountPoint);
3412
+        $mountPoint->method('getStorageRootId')
3413
+            ->willReturn(42);
3414
+
3415
+        $expected = new DataResponse([]);
3416
+        $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3417
+
3418
+        $this->assertInstanceOf(get_class($expected), $result);
3419
+        $this->assertEquals($expected->getData(), $result->getData());
3420
+    }
3421
+
3422
+    public function testUpdateLinkShareExpireDateDoesNotChangeOther(): void {
3423
+        $ocs = $this->mockFormatShare();
3424
+
3425
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3426
+        $node->method('getId')
3427
+            ->willReturn(42);
3428
+
3429
+        $share = $this->newShare();
3430
+        $share->setPermissions(Constants::PERMISSION_ALL)
3431
+            ->setSharedBy($this->currentUser)
3432
+            ->setShareType(IShare::TYPE_LINK)
3433
+            ->setPassword('password')
3434
+            ->setSendPasswordByTalk(true)
3435
+            ->setExpirationDate(new \DateTime())
3436
+            ->setNote('note')
3437
+            ->setLabel('label')
3438
+            ->setHideDownload(true)
3439
+            ->setPermissions(Constants::PERMISSION_ALL)
3440
+            ->setNode($node);
3441
+
3442
+        $node->expects($this->once())
3443
+            ->method('lock')
3444
+            ->with(ILockingProvider::LOCK_SHARED);
3445
+
3446
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3447
+
3448
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3449
+            $this->callback(function (IShare $share) {
3450
+                $date = new \DateTime('2010-12-23');
3451
+                $date->setTime(0, 0, 0);
3452
+
3453
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3454
+                $share->getPassword() === 'password' &&
3455
+                $share->getSendPasswordByTalk() === true &&
3456
+                $share->getExpirationDate() == $date &&
3457
+                $share->getNote() === 'note' &&
3458
+                $share->getLabel() === 'label' &&
3459
+                $share->getHideDownload() === true;
3460
+            })
3461
+        )->willReturnArgument(0);
3462
+
3463
+        $this->rootFolder->method('getUserFolder')
3464
+            ->with($this->currentUser)
3465
+            ->willReturn($userFolder);
3466
+
3467
+        $userFolder->method('getById')
3468
+            ->with(42)
3469
+            ->willReturn([$node]);
3470
+
3471
+        $mountPoint = $this->createMock(IMountPoint::class);
3472
+        $node->method('getMountPoint')
3473
+            ->willReturn($mountPoint);
3474
+        $mountPoint->method('getStorageRootId')
3475
+            ->willReturn(42);
3476
+
3477
+        $expected = new DataResponse([]);
3478
+        $result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null);
3479
+
3480
+        $this->assertInstanceOf(get_class($expected), $result);
3481
+        $this->assertEquals($expected->getData(), $result->getData());
3482
+    }
3483
+
3484
+    public function testUpdateLinkSharePublicUploadDoesNotChangeOther(): void {
3485
+        $ocs = $this->mockFormatShare();
3486
+
3487
+        $date = new \DateTime('2000-01-01');
3488
+
3489
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3490
+        $folder->method('getId')
3491
+            ->willReturn(42);
3492
+
3493
+        $share = Server::get(IManager::class)->newShare();
3494
+        $share->setPermissions(Constants::PERMISSION_ALL)
3495
+            ->setSharedBy($this->currentUser)
3496
+            ->setShareType(IShare::TYPE_LINK)
3497
+            ->setPassword('password')
3498
+            ->setSendPasswordByTalk(true)
3499
+            ->setExpirationDate($date)
3500
+            ->setNote('note')
3501
+            ->setLabel('label')
3502
+            ->setHideDownload(true)
3503
+            ->setPermissions(Constants::PERMISSION_ALL)
3504
+            ->setNode($folder);
3505
+
3506
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3507
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3508
+
3509
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3510
+            $this->callback(function (IShare $share) use ($date) {
3511
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3512
+                $share->getPassword() === 'password' &&
3513
+                $share->getSendPasswordByTalk() === true &&
3514
+                $share->getExpirationDate() === $date &&
3515
+                $share->getNote() === 'note' &&
3516
+                $share->getLabel() === 'label' &&
3517
+                $share->getHideDownload() === true;
3518
+            })
3519
+        )->willReturnArgument(0);
3520
+
3521
+        $this->shareManager->method('getSharedWith')
3522
+            ->willReturn([]);
3523
+
3524
+        $this->rootFolder->method('getUserFolder')
3525
+            ->with($this->currentUser)
3526
+            ->willReturn($userFolder);
3527
+
3528
+        $userFolder->method('getById')
3529
+            ->with(42)
3530
+            ->willReturn([$folder]);
3531
+
3532
+        $mountPoint = $this->createMock(IMountPoint::class);
3533
+        $folder->method('getMountPoint')
3534
+            ->willReturn($mountPoint);
3535
+        $mountPoint->method('getStorageRootId')
3536
+            ->willReturn(42);
3537
+
3538
+        $expected = new DataResponse([]);
3539
+        $result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null);
3540
+
3541
+        $this->assertInstanceOf(get_class($expected), $result);
3542
+        $this->assertEquals($expected->getData(), $result->getData());
3543
+    }
3544
+
3545
+    public function testUpdateLinkSharePermissions(): void {
3546
+        $ocs = $this->mockFormatShare();
3547
+
3548
+        $date = new \DateTime('2000-01-01');
3549
+
3550
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3551
+        $folder->method('getId')
3552
+            ->willReturn(42);
3553
+
3554
+        $share = Server::get(IManager::class)->newShare();
3555
+        $share->setPermissions(Constants::PERMISSION_ALL)
3556
+            ->setSharedBy($this->currentUser)
3557
+            ->setShareType(IShare::TYPE_LINK)
3558
+            ->setPassword('password')
3559
+            ->setSendPasswordByTalk(true)
3560
+            ->setExpirationDate($date)
3561
+            ->setNote('note')
3562
+            ->setLabel('label')
3563
+            ->setHideDownload(true)
3564
+            ->setPermissions(Constants::PERMISSION_ALL)
3565
+            ->setNode($folder);
3566
+
3567
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3568
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3569
+
3570
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3571
+            $this->callback(function (IShare $share) use ($date): bool {
3572
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3573
+                $share->getPassword() === 'password' &&
3574
+                $share->getSendPasswordByTalk() === true &&
3575
+                $share->getExpirationDate() === $date &&
3576
+                $share->getNote() === 'note' &&
3577
+                $share->getLabel() === 'label' &&
3578
+                $share->getHideDownload() === true;
3579
+            })
3580
+        )->willReturnArgument(0);
3581
+
3582
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3583
+
3584
+        $this->rootFolder->method('getUserFolder')
3585
+            ->with($this->currentUser)
3586
+            ->willReturn($userFolder);
3587
+
3588
+        $userFolder->method('getById')
3589
+            ->with(42)
3590
+            ->willReturn([$folder]);
3591
+
3592
+        $mountPoint = $this->createMock(IMountPoint::class);
3593
+        $folder->method('getMountPoint')
3594
+            ->willReturn($mountPoint);
3595
+        $mountPoint->method('getStorageRootId')
3596
+            ->willReturn(42);
3597
+
3598
+        $expected = new DataResponse([]);
3599
+        $result = $ocs->updateShare(42, 7, null, null, 'true', null, null, null, null);
3600
+
3601
+        $this->assertInstanceOf(get_class($expected), $result);
3602
+        $this->assertEquals($expected->getData(), $result->getData());
3603
+    }
3604
+
3605
+    public function testUpdateLinkSharePermissionsShare(): void {
3606
+        $ocs = $this->mockFormatShare();
3607
+
3608
+        $date = new \DateTime('2000-01-01');
3609
+
3610
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3611
+        $folder->method('getId')
3612
+            ->willReturn(42);
3613
+
3614
+        $share = Server::get(IManager::class)->newShare();
3615
+        $share->setPermissions(Constants::PERMISSION_ALL)
3616
+            ->setSharedBy($this->currentUser)
3617
+            ->setShareType(IShare::TYPE_LINK)
3618
+            ->setPassword('password')
3619
+            ->setSendPasswordByTalk(true)
3620
+            ->setExpirationDate($date)
3621
+            ->setNote('note')
3622
+            ->setLabel('label')
3623
+            ->setHideDownload(true)
3624
+            ->setPermissions(Constants::PERMISSION_READ)
3625
+            ->setNode($folder);
3626
+
3627
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3628
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3629
+
3630
+        $this->shareManager->expects($this->once())
3631
+            ->method('updateShare')
3632
+            ->with(
3633
+                $this->callback(function (IShare $share) use ($date) {
3634
+                    return $share->getPermissions() === Constants::PERMISSION_ALL &&
3635
+                        $share->getPassword() === 'password' &&
3636
+                        $share->getSendPasswordByTalk() === true &&
3637
+                        $share->getExpirationDate() === $date &&
3638
+                        $share->getNote() === 'note' &&
3639
+                        $share->getLabel() === 'label' &&
3640
+                        $share->getHideDownload() === true;
3641
+                })
3642
+            )->willReturnArgument(0);
3643
+
3644
+        $this->rootFolder->method('getUserFolder')
3645
+            ->with($this->currentUser)
3646
+            ->willReturn($userFolder);
3647
+
3648
+        $userFolder->method('getById')
3649
+            ->with(42)
3650
+            ->willReturn([$folder]);
3651
+
3652
+        $mountPoint = $this->createMock(IMountPoint::class);
3653
+        $folder->method('getMountPoint')
3654
+            ->willReturn($mountPoint);
3655
+        $mountPoint->method('getStorageRootId')
3656
+            ->willReturn(42);
3657
+
3658
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3659
+
3660
+        $expected = new DataResponse([]);
3661
+        $result = $ocs->updateShare(42, Constants::PERMISSION_ALL, null, null, null, null, null, null, null);
3662
+
3663
+        $this->assertInstanceOf(get_class($expected), $result);
3664
+        $this->assertEquals($expected->getData(), $result->getData());
3665
+    }
3666
+
3667
+    public function testUpdateOtherPermissions(): void {
3668
+        $ocs = $this->mockFormatShare();
3669
+
3670
+        [$userFolder, $file] = $this->getNonSharedUserFolder();
3671
+        $file->method('getId')
3672
+            ->willReturn(42);
3673
+
3674
+        $share = Server::get(IManager::class)->newShare();
3675
+        $share->setPermissions(Constants::PERMISSION_ALL)
3676
+            ->setSharedBy($this->currentUser)
3677
+            ->setShareType(IShare::TYPE_USER)
3678
+            ->setNode($file);
3679
+
3680
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3681
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3682
+
3683
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3684
+            $this->callback(function (IShare $share) {
3685
+                return $share->getPermissions() === Constants::PERMISSION_ALL;
3686
+            })
3687
+        )->willReturnArgument(0);
3688
+
3689
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3690
+
3691
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3692
+        $this->rootFolder->method('getUserFolder')
3693
+            ->with($this->currentUser)
3694
+            ->willReturn($userFolder);
3695
+
3696
+        $userFolder->method('getById')
3697
+            ->with(42)
3698
+            ->willReturn([$file]);
3699
+
3700
+        $mountPoint = $this->createMock(IMountPoint::class);
3701
+        $file->method('getMountPoint')
3702
+            ->willReturn($mountPoint);
3703
+        $mountPoint->method('getStorageRootId')
3704
+            ->willReturn(42);
3705
+
3706
+        $expected = new DataResponse([]);
3707
+        $result = $ocs->updateShare(42, 31, null, null, null, null);
3708
+
3709
+        $this->assertInstanceOf(get_class($expected), $result);
3710
+        $this->assertEquals($expected->getData(), $result->getData());
3711
+    }
3712
+
3713
+    public function testUpdateShareCannotIncreasePermissions(): void {
3714
+        $ocs = $this->mockFormatShare();
3715
+
3716
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3717
+        $folder->method('getId')
3718
+            ->willReturn(42);
3719
+
3720
+        $share = Server::get(IManager::class)->newShare();
3721
+        $share
3722
+            ->setId(42)
3723
+            ->setSharedBy($this->currentUser)
3724
+            ->setShareOwner('anotheruser')
3725
+            ->setShareType(IShare::TYPE_GROUP)
3726
+            ->setSharedWith('group1')
3727
+            ->setPermissions(Constants::PERMISSION_READ)
3728
+            ->setNode($folder);
3729
+
3730
+        // note: updateShare will modify the received instance but getSharedWith will reread from the database,
3731
+        // so their values will be different
3732
+        $incomingShare = Server::get(IManager::class)->newShare();
3733
+        $incomingShare
3734
+            ->setId(42)
3735
+            ->setSharedBy($this->currentUser)
3736
+            ->setShareOwner('anotheruser')
3737
+            ->setShareType(IShare::TYPE_GROUP)
3738
+            ->setSharedWith('group1')
3739
+            ->setPermissions(Constants::PERMISSION_READ)
3740
+            ->setNode($folder);
3741
+
3742
+        $this->request
3743
+            ->method('getParam')
3744
+            ->willReturnMap([
3745
+                ['permissions', null, '31'],
3746
+            ]);
3747
+
3748
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3749
+
3750
+        $this->shareManager->expects($this->any())
3751
+            ->method('getSharedWith')
3752
+            ->willReturnMap([
3753
+                ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3754
+                ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]],
3755
+                ['currentUser', IShare::TYPE_ROOM, $share->getNode(), -1, 0, []]
3756
+            ]);
3757
+
3758
+        $this->rootFolder->method('getUserFolder')
3759
+            ->with($this->currentUser)
3760
+            ->willReturn($userFolder);
3761
+
3762
+        $userFolder->method('getById')
3763
+            ->with(42)
3764
+            ->willReturn([$folder]);
3765
+        $userFolder->method('getFirstNodeById')
3766
+            ->with(42)
3767
+            ->willReturn($folder);
3768
+
3769
+        $mountPoint = $this->createMock(IMountPoint::class);
3770
+        $folder->method('getMountPoint')
3771
+            ->willReturn($mountPoint);
3772
+        $mountPoint->method('getStorageRootId')
3773
+            ->willReturn(42);
3774
+
3775
+        $this->shareManager->expects($this->once())
3776
+            ->method('updateShare')
3777
+            ->with($share)
3778
+            ->willThrowException(new GenericShareException('Cannot increase permissions of path/file', 'Cannot increase permissions of path/file', 404));
3779
+
3780
+        try {
3781
+            $ocs->updateShare(42, 31);
3782
+            $this->fail();
3783
+        } catch (OCSException $e) {
3784
+            $this->assertEquals('Cannot increase permissions of path/file', $e->getMessage());
3785
+        }
3786
+    }
3787
+
3788
+    public function testUpdateShareCanIncreasePermissionsIfOwner(): void {
3789
+        $ocs = $this->mockFormatShare();
3790
+
3791
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3792
+        $folder->method('getId')
3793
+            ->willReturn(42);
3794
+
3795
+        $share = Server::get(IManager::class)->newShare();
3796
+        $share
3797
+            ->setId(42)
3798
+            ->setSharedBy($this->currentUser)
3799
+            ->setShareOwner($this->currentUser)
3800
+            ->setShareType(IShare::TYPE_GROUP)
3801
+            ->setSharedWith('group1')
3802
+            ->setPermissions(Constants::PERMISSION_READ)
3803
+            ->setNode($folder);
3804
+
3805
+        // note: updateShare will modify the received instance but getSharedWith will reread from the database,
3806
+        // so their values will be different
3807
+        $incomingShare = Server::get(IManager::class)->newShare();
3808
+        $incomingShare
3809
+            ->setId(42)
3810
+            ->setSharedBy($this->currentUser)
3811
+            ->setShareOwner($this->currentUser)
3812
+            ->setShareType(IShare::TYPE_GROUP)
3813
+            ->setSharedWith('group1')
3814
+            ->setPermissions(Constants::PERMISSION_READ)
3815
+            ->setNode($folder);
3816
+
3817
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3818
+
3819
+        $this->shareManager->expects($this->any())
3820
+            ->method('getSharedWith')
3821
+            ->willReturnMap([
3822
+                ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3823
+                ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
3824
+            ]);
3825
+
3826
+        $this->shareManager->expects($this->once())
3827
+            ->method('updateShare')
3828
+            ->with($share)
3829
+            ->willReturn($share);
3830
+
3831
+        $this->rootFolder->method('getUserFolder')
3832
+            ->with($this->currentUser)
3833
+            ->willReturn($userFolder);
3834
+
3835
+        $userFolder->method('getById')
3836
+            ->with(42)
3837
+            ->willReturn([$folder]);
3838
+
3839
+        $mountPoint = $this->createMock(IMountPoint::class);
3840
+        $folder->method('getMountPoint')
3841
+            ->willReturn($mountPoint);
3842
+        $mountPoint->method('getStorageRootId')
3843
+            ->willReturn(42);
3844
+
3845
+        $result = $ocs->updateShare(42, 31);
3846
+        $this->assertInstanceOf(DataResponse::class, $result);
3847
+    }
3848
+
3849
+    public function testUpdateShareOwnerless(): void {
3850
+        $ocs = $this->mockFormatShare();
3851
+
3852
+        $mount = $this->createMock(IShareOwnerlessMount::class);
3853
+
3854
+        $file = $this->createMock(File::class);
3855
+        $file
3856
+            ->expects($this->exactly(2))
3857
+            ->method('getPermissions')
3858
+            ->willReturn(Constants::PERMISSION_SHARE);
3859
+        $file
3860
+            ->expects($this->once())
3861
+            ->method('getMountPoint')
3862
+            ->willReturn($mount);
3863
+
3864
+        $userFolder = $this->createMock(Folder::class);
3865
+        $userFolder->method('getById')
3866
+            ->with(2)
3867
+            ->willReturn([$file]);
3868
+        $userFolder->method('getFirstNodeById')
3869
+            ->with(2)
3870
+            ->willReturn($file);
3871
+
3872
+        $this->rootFolder
3873
+            ->method('getUserFolder')
3874
+            ->with($this->currentUser)
3875
+            ->willReturn($userFolder);
3876
+
3877
+        $share = $this->createMock(IShare::class);
3878
+        $share
3879
+            ->expects($this->once())
3880
+            ->method('getNode')
3881
+            ->willReturn($file);
3882
+        $share
3883
+            ->expects($this->exactly(2))
3884
+            ->method('getNodeId')
3885
+            ->willReturn(2);
3886
+        $share
3887
+            ->expects($this->exactly(2))
3888
+            ->method('getPermissions')
3889
+            ->willReturn(Constants::PERMISSION_SHARE);
3890
+
3891
+        $this->shareManager
3892
+            ->expects($this->once())
3893
+            ->method('getShareById')
3894
+            ->with('ocinternal:1', $this->currentUser)
3895
+            ->willReturn($share);
3896
+
3897
+        $this->shareManager
3898
+            ->expects($this->once())
3899
+            ->method('updateShare')
3900
+            ->with($share)
3901
+            ->willReturn($share);
3902
+
3903
+        $result = $ocs->updateShare(1, Constants::PERMISSION_ALL);
3904
+        $this->assertInstanceOf(DataResponse::class, $result);
3905
+    }
3906
+
3907
+    public function dataFormatShare() {
3908
+        $file = $this->getMockBuilder(File::class)->getMock();
3909
+        $folder = $this->getMockBuilder(Folder::class)->getMock();
3910
+        $parent = $this->getMockBuilder(Folder::class)->getMock();
3911
+        $fileWithPreview = $this->getMockBuilder(File::class)->getMock();
3912
+
3913
+        $file->method('getMimeType')->willReturn('myMimeType');
3914
+        $folder->method('getMimeType')->willReturn('myFolderMimeType');
3915
+        $fileWithPreview->method('getMimeType')->willReturn('mimeWithPreview');
3916
+
3917
+        $mountPoint = $this->createMock(IMountPoint::class);
3918
+        $mountPoint->method('getMountType')->willReturn('');
3919
+        $file->method('getMountPoint')->willReturn($mountPoint);
3920
+        $folder->method('getMountPoint')->willReturn($mountPoint);
3921
+        $fileWithPreview->method('getMountPoint')->willReturn($mountPoint);
3922
+
3923
+        $file->method('getPath')->willReturn('file');
3924
+        $folder->method('getPath')->willReturn('folder');
3925
+        $fileWithPreview->method('getPath')->willReturn('fileWithPreview');
3926
+
3927
+        $parent->method('getId')->willReturn(1);
3928
+        $folder->method('getId')->willReturn(2);
3929
+        $file->method('getId')->willReturn(3);
3930
+        $fileWithPreview->method('getId')->willReturn(4);
3931
+
3932
+        $file->method('getParent')->willReturn($parent);
3933
+        $folder->method('getParent')->willReturn($parent);
3934
+        $fileWithPreview->method('getParent')->willReturn($parent);
3935
+
3936
+        $file->method('getSize')->willReturn(123456);
3937
+        $folder->method('getSize')->willReturn(123456);
3938
+        $fileWithPreview->method('getSize')->willReturn(123456);
3939
+        $file->method('getMTime')->willReturn(1234567890);
3940
+        $folder->method('getMTime')->willReturn(1234567890);
3941
+        $fileWithPreview->method('getMTime')->willReturn(1234567890);
3942
+
3943
+        $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
3944
+        $cache->method('getNumericStorageId')->willReturn(100);
3945
+        $storage = $this->createMock(IStorage::class);
3946
+        $storage->method('getId')->willReturn('storageId');
3947
+        $storage->method('getCache')->willReturn($cache);
3948
+
3949
+        $file->method('getStorage')->willReturn($storage);
3950
+        $folder->method('getStorage')->willReturn($storage);
3951
+        $fileWithPreview->method('getStorage')->willReturn($storage);
3952
+
3953
+
3954
+        $mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
3955
+        $mountPoint->method('getMountType')->willReturn('');
3956
+        $file->method('getMountPoint')->willReturn($mountPoint);
3957
+        $folder->method('getMountPoint')->willReturn($mountPoint);
3958
+
3959
+        $owner = $this->getMockBuilder(IUser::class)->getMock();
3960
+        $owner->method('getDisplayName')->willReturn('ownerDN');
3961
+        $initiator = $this->getMockBuilder(IUser::class)->getMock();
3962
+        $initiator->method('getDisplayName')->willReturn('initiatorDN');
3963
+        $recipient = $this->getMockBuilder(IUser::class)->getMock();
3964
+        $recipient->method('getDisplayName')->willReturn('recipientDN');
3965
+        $recipient->method('getSystemEMailAddress')->willReturn('recipient');
3966
+        [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
3967
+
3968
+        $result = [];
3969
+
3970
+        $share = Server::get(IManager::class)->newShare();
3971
+        $share->setShareType(IShare::TYPE_USER)
3972
+            ->setSharedWith('recipient')
3973
+            ->setSharedBy('initiator')
3974
+            ->setShareOwner('owner')
3975
+            ->setPermissions(Constants::PERMISSION_READ)
3976
+            ->setAttributes($shareAttributes)
3977
+            ->setNode($file)
3978
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
3979
+            ->setTarget('myTarget')
3980
+            ->setNote('personal note')
3981
+            ->setId(42);
3982
+
3983
+        // User backend down
3984
+        $result[] = [
3985
+            [
3986
+                'id' => '42',
3987
+                'share_type' => IShare::TYPE_USER,
3988
+                'uid_owner' => 'initiator',
3989
+                'displayname_owner' => 'initiator',
3990
+                'permissions' => 1,
3991
+                'attributes' => $shareAttributesReturnJson,
3992
+                'stime' => 946684862,
3993
+                'parent' => null,
3994
+                'expiration' => null,
3995
+                'token' => null,
3996
+                'uid_file_owner' => 'owner',
3997
+                'displayname_file_owner' => 'owner',
3998
+                'path' => 'file',
3999
+                'item_type' => 'file',
4000
+                'storage_id' => 'storageId',
4001
+                'storage' => 100,
4002
+                'item_source' => 3,
4003
+                'file_source' => 3,
4004
+                'file_parent' => 1,
4005
+                'file_target' => 'myTarget',
4006
+                'share_with' => 'recipient',
4007
+                'share_with_displayname' => 'recipient',
4008
+                'share_with_displayname_unique' => 'recipient',
4009
+                'note' => 'personal note',
4010
+                'label' => '',
4011
+                'mail_send' => 0,
4012
+                'mimetype' => 'myMimeType',
4013
+                'has_preview' => false,
4014
+                'hide_download' => 0,
4015
+                'can_edit' => false,
4016
+                'can_delete' => false,
4017
+                'item_size' => 123456,
4018
+                'item_mtime' => 1234567890,
4019
+                'is-mount-root' => false,
4020
+                'mount-type' => '',
4021
+                'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4022
+                'item_permissions' => 1,
4023
+            ], $share, [], false
4024
+        ];
4025
+        // User backend up
4026
+        $result[] = [
4027
+            [
4028
+                'id' => '42',
4029
+                'share_type' => IShare::TYPE_USER,
4030
+                'uid_owner' => 'initiator',
4031
+                'displayname_owner' => 'initiatorDN',
4032
+                'permissions' => 1,
4033
+                'attributes' => $shareAttributesReturnJson,
4034
+                'stime' => 946684862,
4035
+                'parent' => null,
4036
+                'expiration' => null,
4037
+                'token' => null,
4038
+                'uid_file_owner' => 'owner',
4039
+                'displayname_file_owner' => 'ownerDN',
4040
+                'note' => 'personal note',
4041
+                'label' => '',
4042
+                'path' => 'file',
4043
+                'item_type' => 'file',
4044
+                'storage_id' => 'storageId',
4045
+                'storage' => 100,
4046
+                'item_source' => 3,
4047
+                'file_source' => 3,
4048
+                'file_parent' => 1,
4049
+                'file_target' => 'myTarget',
4050
+                'share_with' => 'recipient',
4051
+                'share_with_displayname' => 'recipientDN',
4052
+                'share_with_displayname_unique' => 'recipient',
4053
+                'mail_send' => 0,
4054
+                'mimetype' => 'myMimeType',
4055
+                'has_preview' => false,
4056
+                'hide_download' => 0,
4057
+                'can_edit' => false,
4058
+                'can_delete' => false,
4059
+                'item_size' => 123456,
4060
+                'item_mtime' => 1234567890,
4061
+                'is-mount-root' => false,
4062
+                'mount-type' => '',
4063
+                'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4064
+                'item_permissions' => 1,
4065
+            ], $share, [
4066
+                ['owner', $owner],
4067
+                ['initiator', $initiator],
4068
+                ['recipient', $recipient],
4069
+            ], false
4070
+        ];
4071
+
4072
+        $share = Server::get(IManager::class)->newShare();
4073
+        $share->setShareType(IShare::TYPE_USER)
4074
+            ->setSharedWith('recipient')
4075
+            ->setSharedBy('initiator')
4076
+            ->setShareOwner('owner')
4077
+            ->setPermissions(Constants::PERMISSION_READ)
4078
+            ->setNode($file)
4079
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4080
+            ->setTarget('myTarget')
4081
+            ->setNote('personal note')
4082
+            ->setId(42);
4083
+        // User backend down
4084
+        $result[] = [
4085
+            [
4086
+                'id' => '42',
4087
+                'share_type' => IShare::TYPE_USER,
4088
+                'uid_owner' => 'initiator',
4089
+                'displayname_owner' => 'initiator',
4090
+                'permissions' => 1,
4091
+                'attributes' => null,
4092
+                'stime' => 946684862,
4093
+                'parent' => null,
4094
+                'expiration' => null,
4095
+                'token' => null,
4096
+                'uid_file_owner' => 'owner',
4097
+                'displayname_file_owner' => 'owner',
4098
+                'note' => 'personal note',
4099
+                'label' => '',
4100
+                'path' => 'file',
4101
+                'item_type' => 'file',
4102
+                'storage_id' => 'storageId',
4103
+                'storage' => 100,
4104
+                'item_source' => 3,
4105
+                'file_source' => 3,
4106
+                'file_parent' => 1,
4107
+                'file_target' => 'myTarget',
4108
+                'share_with' => 'recipient',
4109
+                'share_with_displayname' => 'recipient',
4110
+                'share_with_displayname_unique' => 'recipient',
4111
+                'mail_send' => 0,
4112
+                'mimetype' => 'myMimeType',
4113
+                'has_preview' => false,
4114
+                'hide_download' => 0,
4115
+                'can_edit' => false,
4116
+                'can_delete' => false,
4117
+                'item_size' => 123456,
4118
+                'item_mtime' => 1234567890,
4119
+                'is-mount-root' => false,
4120
+                'mount-type' => '',
4121
+                'attributes' => null,
4122
+                'item_permissions' => 1,
4123
+            ], $share, [], false
4124
+        ];
4125
+
4126
+        $share = Server::get(IManager::class)->newShare();
4127
+        $share->setShareType(IShare::TYPE_USER)
4128
+            ->setSharedWith('recipient')
4129
+            ->setSharedBy('initiator')
4130
+            ->setShareOwner('currentUser')
4131
+            ->setPermissions(Constants::PERMISSION_READ)
4132
+            ->setNode($file)
4133
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4134
+            ->setTarget('myTarget')
4135
+            ->setNote('personal note')
4136
+            ->setId(42);
4137
+        // User backend down
4138
+        $result[] = [
4139
+            [
4140
+                'id' => '42',
4141
+                'share_type' => IShare::TYPE_USER,
4142
+                'uid_owner' => 'initiator',
4143
+                'displayname_owner' => 'initiator',
4144
+                'permissions' => 1,
4145
+                'attributes' => null,
4146
+                'stime' => 946684862,
4147
+                'parent' => null,
4148
+                'expiration' => null,
4149
+                'token' => null,
4150
+                'uid_file_owner' => 'currentUser',
4151
+                'displayname_file_owner' => 'currentUser',
4152
+                'note' => 'personal note',
4153
+                'label' => '',
4154
+                'path' => 'file',
4155
+                'item_type' => 'file',
4156
+                'storage_id' => 'storageId',
4157
+                'storage' => 100,
4158
+                'item_source' => 3,
4159
+                'file_source' => 3,
4160
+                'file_parent' => 1,
4161
+                'file_target' => 'myTarget',
4162
+                'share_with' => 'recipient',
4163
+                'share_with_displayname' => 'recipient',
4164
+                'share_with_displayname_unique' => 'recipient',
4165
+                'mail_send' => 0,
4166
+                'mimetype' => 'myMimeType',
4167
+                'has_preview' => false,
4168
+                'hide_download' => 0,
4169
+                'can_edit' => true,
4170
+                'can_delete' => true,
4171
+                'item_size' => 123456,
4172
+                'item_mtime' => 1234567890,
4173
+                'is-mount-root' => false,
4174
+                'mount-type' => '',
4175
+                'attributes' => null,
4176
+                'item_permissions' => 11,
4177
+            ], $share, [], false
4178
+        ];
4179
+
4180
+        // with existing group
4181
+
4182
+        $share = Server::get(IManager::class)->newShare();
4183
+        $share->setShareType(IShare::TYPE_GROUP)
4184
+            ->setSharedWith('recipientGroup')
4185
+            ->setSharedBy('initiator')
4186
+            ->setShareOwner('owner')
4187
+            ->setPermissions(Constants::PERMISSION_READ)
4188
+            ->setNode($file)
4189
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4190
+            ->setTarget('myTarget')
4191
+            ->setNote('personal note')
4192
+            ->setId(42);
4193
+
4194
+        $result[] = [
4195
+            [
4196
+                'id' => '42',
4197
+                'share_type' => IShare::TYPE_GROUP,
4198
+                'uid_owner' => 'initiator',
4199
+                'displayname_owner' => 'initiator',
4200
+                'permissions' => 1,
4201
+                'attributes' => null,
4202
+                'stime' => 946684862,
4203
+                'parent' => null,
4204
+                'expiration' => null,
4205
+                'token' => null,
4206
+                'uid_file_owner' => 'owner',
4207
+                'displayname_file_owner' => 'owner',
4208
+                'note' => 'personal note',
4209
+                'label' => '',
4210
+                'path' => 'file',
4211
+                'item_type' => 'file',
4212
+                'storage_id' => 'storageId',
4213
+                'storage' => 100,
4214
+                'item_source' => 3,
4215
+                'file_source' => 3,
4216
+                'file_parent' => 1,
4217
+                'file_target' => 'myTarget',
4218
+                'share_with' => 'recipientGroup',
4219
+                'share_with_displayname' => 'recipientGroupDisplayName',
4220
+                'mail_send' => 0,
4221
+                'mimetype' => 'myMimeType',
4222
+                'has_preview' => false,
4223
+                'hide_download' => 0,
4224
+                'can_edit' => false,
4225
+                'can_delete' => false,
4226
+                'item_size' => 123456,
4227
+                'item_mtime' => 1234567890,
4228
+                'is-mount-root' => false,
4229
+                'mount-type' => '',
4230
+                'attributes' => null,
4231
+                'item_permissions' => 1,
4232
+            ], $share, [], false
4233
+        ];
4234
+
4235
+        // with unknown group / no group backend
4236
+        $share = Server::get(IManager::class)->newShare();
4237
+        $share->setShareType(IShare::TYPE_GROUP)
4238
+            ->setSharedWith('recipientGroup2')
4239
+            ->setSharedBy('initiator')
4240
+            ->setShareOwner('owner')
4241
+            ->setPermissions(Constants::PERMISSION_READ)
4242
+            ->setNode($file)
4243
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4244
+            ->setTarget('myTarget')
4245
+            ->setNote('personal note')
4246
+            ->setId(42);
4247
+        $result[] = [
4248
+            [
4249
+                'id' => '42',
4250
+                'share_type' => IShare::TYPE_GROUP,
4251
+                'uid_owner' => 'initiator',
4252
+                'displayname_owner' => 'initiator',
4253
+                'permissions' => 1,
4254
+                'stime' => 946684862,
4255
+                'parent' => null,
4256
+                'expiration' => null,
4257
+                'token' => null,
4258
+                'uid_file_owner' => 'owner',
4259
+                'displayname_file_owner' => 'owner',
4260
+                'note' => 'personal note',
4261
+                'label' => '',
4262
+                'path' => 'file',
4263
+                'item_type' => 'file',
4264
+                'storage_id' => 'storageId',
4265
+                'storage' => 100,
4266
+                'item_source' => 3,
4267
+                'file_source' => 3,
4268
+                'file_parent' => 1,
4269
+                'file_target' => 'myTarget',
4270
+                'share_with' => 'recipientGroup2',
4271
+                'share_with_displayname' => 'recipientGroup2',
4272
+                'mail_send' => 0,
4273
+                'mimetype' => 'myMimeType',
4274
+                'has_preview' => false,
4275
+                'hide_download' => 0,
4276
+                'can_edit' => false,
4277
+                'can_delete' => false,
4278
+                'item_size' => 123456,
4279
+                'item_mtime' => 1234567890,
4280
+                'is-mount-root' => false,
4281
+                'mount-type' => '',
4282
+                'attributes' => null,
4283
+                'item_permissions' => 1,
4284
+            ], $share, [], false
4285
+        ];
4286
+
4287
+        $share = Server::get(IManager::class)->newShare();
4288
+        $share->setShareType(IShare::TYPE_LINK)
4289
+            ->setSharedBy('initiator')
4290
+            ->setShareOwner('owner')
4291
+            ->setPermissions(Constants::PERMISSION_READ)
4292
+            ->setNode($file)
4293
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4294
+            ->setTarget('myTarget')
4295
+            ->setPassword('mypassword')
4296
+            ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4297
+            ->setToken('myToken')
4298
+            ->setNote('personal note')
4299
+            ->setLabel('new link share')
4300
+            ->setId(42);
4301
+
4302
+        $result[] = [
4303
+            [
4304
+                'id' => '42',
4305
+                'share_type' => IShare::TYPE_LINK,
4306
+                'uid_owner' => 'initiator',
4307
+                'displayname_owner' => 'initiator',
4308
+                'permissions' => 1,
4309
+                'attributes' => null,
4310
+                'stime' => 946684862,
4311
+                'parent' => null,
4312
+                'expiration' => '2001-01-02 00:00:00',
4313
+                'token' => 'myToken',
4314
+                'uid_file_owner' => 'owner',
4315
+                'displayname_file_owner' => 'owner',
4316
+                'note' => 'personal note',
4317
+                'label' => 'new link share',
4318
+                'path' => 'file',
4319
+                'item_type' => 'file',
4320
+                'storage_id' => 'storageId',
4321
+                'storage' => 100,
4322
+                'item_source' => 3,
4323
+                'file_source' => 3,
4324
+                'file_parent' => 1,
4325
+                'file_target' => 'myTarget',
4326
+                'password' => 'mypassword',
4327
+                'share_with' => 'mypassword',
4328
+                'share_with_displayname' => '(Shared link)',
4329
+                'send_password_by_talk' => false,
4330
+                'mail_send' => 0,
4331
+                'url' => 'myLink',
4332
+                'mimetype' => 'myMimeType',
4333
+                'has_preview' => false,
4334
+                'hide_download' => 0,
4335
+                'can_edit' => false,
4336
+                'can_delete' => false,
4337
+                'item_size' => 123456,
4338
+                'item_mtime' => 1234567890,
4339
+                'is-mount-root' => false,
4340
+                'mount-type' => '',
4341
+                'attributes' => null,
4342
+                'item_permissions' => 1,
4343
+            ], $share, [], false
4344
+        ];
4345
+
4346
+        $share = Server::get(IManager::class)->newShare();
4347
+        $share->setShareType(IShare::TYPE_LINK)
4348
+            ->setSharedBy('initiator')
4349
+            ->setShareOwner('owner')
4350
+            ->setPermissions(Constants::PERMISSION_READ)
4351
+            ->setNode($file)
4352
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4353
+            ->setTarget('myTarget')
4354
+            ->setPassword('mypassword')
4355
+            ->setSendPasswordByTalk(true)
4356
+            ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4357
+            ->setToken('myToken')
4358
+            ->setNote('personal note')
4359
+            ->setLabel('new link share')
4360
+            ->setId(42);
4361
+
4362
+        $result[] = [
4363
+            [
4364
+                'id' => '42',
4365
+                'share_type' => IShare::TYPE_LINK,
4366
+                'uid_owner' => 'initiator',
4367
+                'displayname_owner' => 'initiator',
4368
+                'permissions' => 1,
4369
+                'stime' => 946684862,
4370
+                'parent' => null,
4371
+                'expiration' => '2001-01-02 00:00:00',
4372
+                'token' => 'myToken',
4373
+                'uid_file_owner' => 'owner',
4374
+                'displayname_file_owner' => 'owner',
4375
+                'note' => 'personal note',
4376
+                'label' => 'new link share',
4377
+                'path' => 'file',
4378
+                'item_type' => 'file',
4379
+                'storage_id' => 'storageId',
4380
+                'storage' => 100,
4381
+                'item_source' => 3,
4382
+                'file_source' => 3,
4383
+                'file_parent' => 1,
4384
+                'file_target' => 'myTarget',
4385
+                'password' => 'mypassword',
4386
+                'share_with' => 'mypassword',
4387
+                'share_with_displayname' => '(Shared link)',
4388
+                'send_password_by_talk' => true,
4389
+                'mail_send' => 0,
4390
+                'url' => 'myLink',
4391
+                'mimetype' => 'myMimeType',
4392
+                'has_preview' => false,
4393
+                'hide_download' => 0,
4394
+                'can_edit' => false,
4395
+                'can_delete' => false,
4396
+                'item_size' => 123456,
4397
+                'item_mtime' => 1234567890,
4398
+                'is-mount-root' => false,
4399
+                'mount-type' => '',
4400
+                'attributes' => null,
4401
+                'item_permissions' => 1,
4402
+            ], $share, [], false
4403
+        ];
4404
+
4405
+        $share = Server::get(IManager::class)->newShare();
4406
+        $share->setShareType(IShare::TYPE_REMOTE)
4407
+            ->setSharedBy('initiator')
4408
+            ->setSharedWith('[email protected]')
4409
+            ->setShareOwner('owner')
4410
+            ->setPermissions(Constants::PERMISSION_READ)
4411
+            ->setNode($folder)
4412
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4413
+            ->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4414
+            ->setTarget('myTarget')
4415
+            ->setNote('personal note')
4416
+            ->setId(42);
4417
+
4418
+        $result[] = [
4419
+            [
4420
+                'id' => '42',
4421
+                'share_type' => IShare::TYPE_REMOTE,
4422
+                'uid_owner' => 'initiator',
4423
+                'displayname_owner' => 'initiator',
4424
+                'permissions' => 1,
4425
+                'stime' => 946684862,
4426
+                'parent' => null,
4427
+                'expiration' => '2001-02-03 00:00:00',
4428
+                'token' => null,
4429
+                'uid_file_owner' => 'owner',
4430
+                'displayname_file_owner' => 'owner',
4431
+                'note' => 'personal note',
4432
+                'label' => '',
4433
+                'path' => 'folder',
4434
+                'item_type' => 'folder',
4435
+                'storage_id' => 'storageId',
4436
+                'storage' => 100,
4437
+                'item_source' => 2,
4438
+                'file_source' => 2,
4439
+                'file_parent' => 1,
4440
+                'file_target' => 'myTarget',
4441
+                'share_with' => '[email protected]',
4442
+                'share_with_displayname' => 'foobar',
4443
+                'mail_send' => 0,
4444
+                'mimetype' => 'myFolderMimeType',
4445
+                'has_preview' => false,
4446
+                'hide_download' => 0,
4447
+                'can_edit' => false,
4448
+                'can_delete' => false,
4449
+                'item_size' => 123456,
4450
+                'item_mtime' => 1234567890,
4451
+                'is-mount-root' => false,
4452
+                'mount-type' => '',
4453
+                'attributes' => null,
4454
+                'item_permissions' => 1,
4455
+            ], $share, [], false
4456
+        ];
4457
+
4458
+        $share = Server::get(IManager::class)->newShare();
4459
+        $share->setShareType(IShare::TYPE_REMOTE_GROUP)
4460
+            ->setSharedBy('initiator')
4461
+            ->setSharedWith('[email protected]')
4462
+            ->setShareOwner('owner')
4463
+            ->setPermissions(Constants::PERMISSION_READ)
4464
+            ->setNode($folder)
4465
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4466
+            ->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4467
+            ->setTarget('myTarget')
4468
+            ->setNote('personal note')
4469
+            ->setId(42);
4470
+
4471
+        $result[] = [
4472
+            [
4473
+                'id' => '42',
4474
+                'share_type' => IShare::TYPE_REMOTE_GROUP,
4475
+                'uid_owner' => 'initiator',
4476
+                'displayname_owner' => 'initiator',
4477
+                'permissions' => 1,
4478
+                'stime' => 946684862,
4479
+                'parent' => null,
4480
+                'expiration' => '2001-02-03 00:00:00',
4481
+                'token' => null,
4482
+                'uid_file_owner' => 'owner',
4483
+                'displayname_file_owner' => 'owner',
4484
+                'note' => 'personal note',
4485
+                'label' => '',
4486
+                'path' => 'folder',
4487
+                'item_type' => 'folder',
4488
+                'storage_id' => 'storageId',
4489
+                'storage' => 100,
4490
+                'item_source' => 2,
4491
+                'file_source' => 2,
4492
+                'file_parent' => 1,
4493
+                'file_target' => 'myTarget',
4494
+                'share_with' => '[email protected]',
4495
+                'share_with_displayname' => 'foobar',
4496
+                'mail_send' => 0,
4497
+                'mimetype' => 'myFolderMimeType',
4498
+                'has_preview' => false,
4499
+                'hide_download' => 0,
4500
+                'can_edit' => false,
4501
+                'can_delete' => false,
4502
+                'item_size' => 123456,
4503
+                'item_mtime' => 1234567890,
4504
+                'is-mount-root' => false,
4505
+                'mount-type' => '',
4506
+                'attributes' => null,
4507
+                'item_permissions' => 1,
4508
+            ], $share, [], false
4509
+        ];
4510
+
4511
+        // Circle with id, display name and avatar set by the Circles app
4512
+        $share = Server::get(IManager::class)->newShare();
4513
+        $share->setShareType(IShare::TYPE_CIRCLE)
4514
+            ->setSharedBy('initiator')
4515
+            ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4516
+            ->setSharedWithDisplayName('The display name')
4517
+            ->setSharedWithAvatar('path/to/the/avatar')
4518
+            ->setShareOwner('owner')
4519
+            ->setPermissions(Constants::PERMISSION_READ)
4520
+            ->setNode($folder)
4521
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4522
+            ->setTarget('myTarget')
4523
+            ->setId(42);
4524
+
4525
+        $result[] = [
4526
+            [
4527
+                'id' => '42',
4528
+                'share_type' => IShare::TYPE_CIRCLE,
4529
+                'uid_owner' => 'initiator',
4530
+                'displayname_owner' => 'initiator',
4531
+                'permissions' => 1,
4532
+                'attributes' => null,
4533
+                'stime' => 946684862,
4534
+                'parent' => null,
4535
+                'expiration' => null,
4536
+                'token' => null,
4537
+                'uid_file_owner' => 'owner',
4538
+                'displayname_file_owner' => 'owner',
4539
+                'note' => '',
4540
+                'label' => '',
4541
+                'path' => 'folder',
4542
+                'item_type' => 'folder',
4543
+                'storage_id' => 'storageId',
4544
+                'storage' => 100,
4545
+                'item_source' => 2,
4546
+                'file_source' => 2,
4547
+                'file_parent' => 1,
4548
+                'file_target' => 'myTarget',
4549
+                'share_with' => '4815162342',
4550
+                'share_with_displayname' => 'The display name',
4551
+                'share_with_avatar' => 'path/to/the/avatar',
4552
+                'mail_send' => 0,
4553
+                'mimetype' => 'myFolderMimeType',
4554
+                'has_preview' => false,
4555
+                'hide_download' => 0,
4556
+                'can_edit' => false,
4557
+                'can_delete' => false,
4558
+                'item_size' => 123456,
4559
+                'item_mtime' => 1234567890,
4560
+                'is-mount-root' => false,
4561
+                'mount-type' => '',
4562
+                'attributes' => null,
4563
+                'item_permissions' => 1,
4564
+            ], $share, [], false
4565
+        ];
4566
+
4567
+        // Circle with id set by the Circles app
4568
+        $share = Server::get(IManager::class)->newShare();
4569
+        $share->setShareType(IShare::TYPE_CIRCLE)
4570
+            ->setSharedBy('initiator')
4571
+            ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4572
+            ->setShareOwner('owner')
4573
+            ->setPermissions(Constants::PERMISSION_READ)
4574
+            ->setNode($folder)
4575
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4576
+            ->setTarget('myTarget')
4577
+            ->setId(42);
4578
+
4579
+        $result[] = [
4580
+            [
4581
+                'id' => '42',
4582
+                'share_type' => IShare::TYPE_CIRCLE,
4583
+                'uid_owner' => 'initiator',
4584
+                'displayname_owner' => 'initiator',
4585
+                'permissions' => 1,
4586
+                'stime' => 946684862,
4587
+                'parent' => null,
4588
+                'expiration' => null,
4589
+                'token' => null,
4590
+                'uid_file_owner' => 'owner',
4591
+                'displayname_file_owner' => 'owner',
4592
+                'note' => '',
4593
+                'label' => '',
4594
+                'path' => 'folder',
4595
+                'item_type' => 'folder',
4596
+                'storage_id' => 'storageId',
4597
+                'storage' => 100,
4598
+                'item_source' => 2,
4599
+                'file_source' => 2,
4600
+                'file_parent' => 1,
4601
+                'file_target' => 'myTarget',
4602
+                'share_with' => '4815162342',
4603
+                'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4604
+                'share_with_avatar' => '',
4605
+                'mail_send' => 0,
4606
+                'mimetype' => 'myFolderMimeType',
4607
+                'has_preview' => false,
4608
+                'hide_download' => 0,
4609
+                'can_edit' => false,
4610
+                'can_delete' => false,
4611
+                'item_size' => 123456,
4612
+                'item_mtime' => 1234567890,
4613
+                'is-mount-root' => false,
4614
+                'mount-type' => '',
4615
+                'attributes' => null,
4616
+                'item_permissions' => 1,
4617
+            ], $share, [], false
4618
+        ];
4619
+
4620
+        // Circle with id not set by the Circles app
4621
+        $share = Server::get(IManager::class)->newShare();
4622
+        $share->setShareType(IShare::TYPE_CIRCLE)
4623
+            ->setSharedBy('initiator')
4624
+            ->setSharedWith('Circle (Public circle, circleOwner)')
4625
+            ->setShareOwner('owner')
4626
+            ->setPermissions(Constants::PERMISSION_READ)
4627
+            ->setNode($folder)
4628
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4629
+            ->setTarget('myTarget')
4630
+            ->setId(42);
4631
+
4632
+        $result[] = [
4633
+            [
4634
+                'id' => '42',
4635
+                'share_type' => IShare::TYPE_CIRCLE,
4636
+                'uid_owner' => 'initiator',
4637
+                'displayname_owner' => 'initiator',
4638
+                'permissions' => 1,
4639
+                'stime' => 946684862,
4640
+                'parent' => null,
4641
+                'expiration' => null,
4642
+                'token' => null,
4643
+                'uid_file_owner' => 'owner',
4644
+                'displayname_file_owner' => 'owner',
4645
+                'note' => '',
4646
+                'label' => '',
4647
+                'path' => 'folder',
4648
+                'item_type' => 'folder',
4649
+                'storage_id' => 'storageId',
4650
+                'storage' => 100,
4651
+                'item_source' => 2,
4652
+                'file_source' => 2,
4653
+                'file_parent' => 1,
4654
+                'file_target' => 'myTarget',
4655
+                'share_with' => 'Circle',
4656
+                'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4657
+                'share_with_avatar' => '',
4658
+                'mail_send' => 0,
4659
+                'mimetype' => 'myFolderMimeType',
4660
+                'has_preview' => false,
4661
+                'hide_download' => 0,
4662
+                'can_edit' => false,
4663
+                'can_delete' => false,
4664
+                'item_size' => 123456,
4665
+                'item_mtime' => 1234567890,
4666
+                'is-mount-root' => false,
4667
+                'mount-type' => '',
4668
+                'attributes' => null,
4669
+                'item_permissions' => 1,
4670
+            ], $share, [], false
4671
+        ];
4672
+
4673
+        $share = Server::get(IManager::class)->newShare();
4674
+        $share->setShareType(IShare::TYPE_USER)
4675
+            ->setSharedBy('initiator')
4676
+            ->setSharedWith('recipient')
4677
+            ->setShareOwner('owner')
4678
+            ->setPermissions(Constants::PERMISSION_READ)
4679
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4680
+            ->setTarget('myTarget')
4681
+            ->setNote('personal note')
4682
+            ->setId(42);
4683
+
4684
+        $result[] = [
4685
+            [], $share, [], true
4686
+        ];
4687
+
4688
+        $share = Server::get(IManager::class)->newShare();
4689
+        $share->setShareType(IShare::TYPE_EMAIL)
4690
+            ->setSharedBy('initiator')
4691
+            ->setSharedWith('[email protected]')
4692
+            ->setShareOwner('owner')
4693
+            ->setPermissions(Constants::PERMISSION_READ)
4694
+            ->setNode($folder)
4695
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4696
+            ->setTarget('myTarget')
4697
+            ->setId(42)
4698
+            ->setPassword('password');
4699
+
4700
+        $result[] = [
4701
+            [
4702
+                'id' => '42',
4703
+                'share_type' => IShare::TYPE_EMAIL,
4704
+                'uid_owner' => 'initiator',
4705
+                'displayname_owner' => 'initiator',
4706
+                'permissions' => 1,
4707
+                'stime' => 946684862,
4708
+                'parent' => null,
4709
+                'expiration' => null,
4710
+                'token' => null,
4711
+                'uid_file_owner' => 'owner',
4712
+                'displayname_file_owner' => 'owner',
4713
+                'note' => '',
4714
+                'label' => '',
4715
+                'path' => 'folder',
4716
+                'item_type' => 'folder',
4717
+                'storage_id' => 'storageId',
4718
+                'storage' => 100,
4719
+                'item_source' => 2,
4720
+                'file_source' => 2,
4721
+                'file_parent' => 1,
4722
+                'file_target' => 'myTarget',
4723
+                'share_with' => '[email protected]',
4724
+                'share_with_displayname' => 'mail display name',
4725
+                'mail_send' => 0,
4726
+                'mimetype' => 'myFolderMimeType',
4727
+                'has_preview' => false,
4728
+                'password' => 'password',
4729
+                'send_password_by_talk' => false,
4730
+                'hide_download' => 0,
4731
+                'can_edit' => false,
4732
+                'can_delete' => false,
4733
+                'password_expiration_time' => null,
4734
+                'item_size' => 123456,
4735
+                'item_mtime' => 1234567890,
4736
+                'is-mount-root' => false,
4737
+                'mount-type' => '',
4738
+                'attributes' => null,
4739
+                'item_permissions' => 1,
4740
+            ], $share, [], false
4741
+        ];
4742
+
4743
+        $share = Server::get(IManager::class)->newShare();
4744
+        $share->setShareType(IShare::TYPE_EMAIL)
4745
+            ->setSharedBy('initiator')
4746
+            ->setSharedWith('[email protected]')
4747
+            ->setShareOwner('owner')
4748
+            ->setPermissions(Constants::PERMISSION_READ)
4749
+            ->setNode($folder)
4750
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4751
+            ->setTarget('myTarget')
4752
+            ->setId(42)
4753
+            ->setPassword('password')
4754
+            ->setSendPasswordByTalk(true);
4755
+
4756
+        $result[] = [
4757
+            [
4758
+                'id' => '42',
4759
+                'share_type' => IShare::TYPE_EMAIL,
4760
+                'uid_owner' => 'initiator',
4761
+                'displayname_owner' => 'initiator',
4762
+                'permissions' => 1,
4763
+                'stime' => 946684862,
4764
+                'parent' => null,
4765
+                'expiration' => null,
4766
+                'token' => null,
4767
+                'uid_file_owner' => 'owner',
4768
+                'displayname_file_owner' => 'owner',
4769
+                'note' => '',
4770
+                'label' => '',
4771
+                'path' => 'folder',
4772
+                'item_type' => 'folder',
4773
+                'storage_id' => 'storageId',
4774
+                'storage' => 100,
4775
+                'item_source' => 2,
4776
+                'file_source' => 2,
4777
+                'file_parent' => 1,
4778
+                'file_target' => 'myTarget',
4779
+                'share_with' => '[email protected]',
4780
+                'share_with_displayname' => 'mail display name',
4781
+                'mail_send' => 0,
4782
+                'mimetype' => 'myFolderMimeType',
4783
+                'has_preview' => false,
4784
+                'password' => 'password',
4785
+                'send_password_by_talk' => true,
4786
+                'hide_download' => 0,
4787
+                'can_edit' => false,
4788
+                'can_delete' => false,
4789
+                'password_expiration_time' => null,
4790
+                'item_size' => 123456,
4791
+                'item_mtime' => 1234567890,
4792
+                'is-mount-root' => false,
4793
+                'mount-type' => '',
4794
+                'attributes' => null,
4795
+                'item_permissions' => 1,
4796
+            ], $share, [], false
4797
+        ];
4798
+
4799
+        // Preview is available
4800
+        $share = Server::get(IManager::class)->newShare();
4801
+        $share->setShareType(IShare::TYPE_USER)
4802
+            ->setSharedWith('recipient')
4803
+            ->setSharedBy('initiator')
4804
+            ->setShareOwner('currentUser')
4805
+            ->setPermissions(Constants::PERMISSION_READ)
4806
+            ->setNode($fileWithPreview)
4807
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4808
+            ->setTarget('myTarget')
4809
+            ->setNote('personal note')
4810
+            ->setId(42);
4811
+
4812
+        $result[] = [
4813
+            [
4814
+                'id' => '42',
4815
+                'share_type' => IShare::TYPE_USER,
4816
+                'uid_owner' => 'initiator',
4817
+                'displayname_owner' => 'initiator',
4818
+                'permissions' => 1,
4819
+                'stime' => 946684862,
4820
+                'parent' => null,
4821
+                'expiration' => null,
4822
+                'token' => null,
4823
+                'uid_file_owner' => 'currentUser',
4824
+                'displayname_file_owner' => 'currentUser',
4825
+                'note' => 'personal note',
4826
+                'label' => '',
4827
+                'path' => 'fileWithPreview',
4828
+                'item_type' => 'file',
4829
+                'storage_id' => 'storageId',
4830
+                'storage' => 100,
4831
+                'item_source' => 4,
4832
+                'file_source' => 4,
4833
+                'file_parent' => 1,
4834
+                'file_target' => 'myTarget',
4835
+                'share_with' => 'recipient',
4836
+                'share_with_displayname' => 'recipient',
4837
+                'share_with_displayname_unique' => 'recipient',
4838
+                'mail_send' => 0,
4839
+                'mimetype' => 'mimeWithPreview',
4840
+                'has_preview' => true,
4841
+                'hide_download' => 0,
4842
+                'can_edit' => true,
4843
+                'can_delete' => true,
4844
+                'item_size' => 123456,
4845
+                'item_mtime' => 1234567890,
4846
+                'is-mount-root' => false,
4847
+                'mount-type' => '',
4848
+                'attributes' => null,
4849
+                'item_permissions' => 11,
4850
+            ], $share, [], false
4851
+        ];
4852
+
4853
+        return $result;
4854
+    }
4855
+
4856
+    /**
4857
+     * @dataProvider dataFormatShare
4858
+     *
4859
+     * @param array $expects
4860
+     * @param IShare $share
4861
+     * @param array $users
4862
+     * @param $exception
4863
+     */
4864
+    public function testFormatShare(array $expects, IShare $share, array $users, $exception): void {
4865
+        $this->userManager->method('get')->willReturnMap($users);
4866
+
4867
+        $recipientGroup = $this->createMock(IGroup::class);
4868
+        $recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
4869
+        $this->groupManager->method('get')->willReturnMap([
4870
+            ['recipientGroup', $recipientGroup],
4871
+        ]);
4872
+
4873
+        $this->urlGenerator->method('linkToRouteAbsolute')
4874
+            ->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
4875
+            ->willReturn('myLink');
4876
+
4877
+        $this->rootFolder->method('getUserFolder')
4878
+            ->with($this->currentUser)
4879
+            ->willReturnSelf();
4880
+        $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
4881
+
4882
+        if (!$exception) {
4883
+            $this->rootFolder->method('getFirstNodeById')
4884
+                ->with($share->getNodeId())
4885
+                ->willReturn($share->getNode());
4886
+
4887
+            $this->rootFolder->method('getRelativePath')
4888
+                ->with($share->getNode()->getPath())
4889
+                ->willReturnArgument(0);
4890
+        }
4891
+
4892
+        $cm = $this->createMock(\OCP\Contacts\IManager::class);
4893
+        $this->overwriteService(\OCP\Contacts\IManager::class, $cm);
4894
+
4895
+        $cm->method('search')
4896
+            ->willReturnMap([
4897
+                ['[email protected]', ['CLOUD'], [
4898
+                    'limit' => 1,
4899
+                    'enumeration' => false,
4900
+                    'strict_search' => true,
4901
+                ],
4902
+                    [
4903
+                        [
4904
+                            'CLOUD' => [
4905
+                                '[email protected]',
4906
+                            ],
4907
+                            'FN' => 'foobar',
4908
+                        ],
4909
+                    ],
4910
+                ],
4911
+                ['[email protected]', ['EMAIL'], [
4912
+                    'limit' => 1,
4913
+                    'enumeration' => false,
4914
+                    'strict_search' => true,
4915
+                ],
4916
+                    [
4917
+                        [
4918
+                            'EMAIL' => [
4919
+                                '[email protected]',
4920
+                            ],
4921
+                            'FN' => 'mail display name',
4922
+                        ],
4923
+                    ],
4924
+                ],
4925
+            ]);
4926
+
4927
+        try {
4928
+            $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
4929
+            $this->assertFalse($exception);
4930
+            $this->assertEquals($expects, $result);
4931
+        } catch (NotFoundException $e) {
4932
+            $this->assertTrue($exception);
4933
+        }
4934
+    }
4935
+
4936
+    public function dataFormatRoomShare() {
4937
+        $file = $this->getMockBuilder(File::class)->getMock();
4938
+        $parent = $this->getMockBuilder(Folder::class)->getMock();
4939
+
4940
+        $file->method('getMimeType')->willReturn('myMimeType');
4941
+
4942
+        $file->method('getPath')->willReturn('file');
4943
+
4944
+        $parent->method('getId')->willReturn(1);
4945
+        $file->method('getId')->willReturn(3);
4946
+
4947
+        $file->method('getParent')->willReturn($parent);
4948
+
4949
+        $file->method('getSize')->willReturn(123456);
4950
+        $file->method('getMTime')->willReturn(1234567890);
4951
+
4952
+        $mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
4953
+        $mountPoint->method('getMountType')->willReturn('');
4954
+        $file->method('getMountPoint')->willReturn($mountPoint);
4955
+
4956
+        $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
4957
+        $cache->method('getNumericStorageId')->willReturn(100);
4958
+        $storage = $this->createMock(IStorage::class);
4959
+        $storage->method('getId')->willReturn('storageId');
4960
+        $storage->method('getCache')->willReturn($cache);
4961
+
4962
+        $file->method('getStorage')->willReturn($storage);
4963
+
4964
+        $result = [];
4965
+
4966
+        $share = Server::get(IManager::class)->newShare();
4967
+        $share->setShareType(IShare::TYPE_ROOM)
4968
+            ->setSharedWith('recipientRoom')
4969
+            ->setSharedBy('initiator')
4970
+            ->setShareOwner('owner')
4971
+            ->setPermissions(Constants::PERMISSION_READ)
4972
+            ->setNode($file)
4973
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4974
+            ->setTarget('myTarget')
4975
+            ->setNote('personal note')
4976
+            ->setId(42);
4977
+
4978
+        $result[] = [
4979
+            [
4980
+                'id' => '42',
4981
+                'share_type' => IShare::TYPE_ROOM,
4982
+                'uid_owner' => 'initiator',
4983
+                'displayname_owner' => 'initiator',
4984
+                'permissions' => 1,
4985
+                'stime' => 946684862,
4986
+                'parent' => null,
4987
+                'expiration' => null,
4988
+                'token' => null,
4989
+                'uid_file_owner' => 'owner',
4990
+                'displayname_file_owner' => 'owner',
4991
+                'note' => 'personal note',
4992
+                'path' => 'file',
4993
+                'item_type' => 'file',
4994
+                'storage_id' => 'storageId',
4995
+                'storage' => 100,
4996
+                'item_source' => 3,
4997
+                'file_source' => 3,
4998
+                'file_parent' => 1,
4999
+                'file_target' => 'myTarget',
5000
+                'share_with' => 'recipientRoom',
5001
+                'share_with_displayname' => '',
5002
+                'mail_send' => 0,
5003
+                'mimetype' => 'myMimeType',
5004
+                'has_preview' => false,
5005
+                'hide_download' => 0,
5006
+                'label' => '',
5007
+                'can_edit' => false,
5008
+                'can_delete' => false,
5009
+                'item_size' => 123456,
5010
+                'item_mtime' => 1234567890,
5011
+                'is-mount-root' => false,
5012
+                'mount-type' => '',
5013
+                'attributes' => null,
5014
+                'item_permissions' => 1,
5015
+            ], $share, false, []
5016
+        ];
5017
+
5018
+        $share = Server::get(IManager::class)->newShare();
5019
+        $share->setShareType(IShare::TYPE_ROOM)
5020
+            ->setSharedWith('recipientRoom')
5021
+            ->setSharedBy('initiator')
5022
+            ->setShareOwner('owner')
5023
+            ->setPermissions(Constants::PERMISSION_READ)
5024
+            ->setNode($file)
5025
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
5026
+            ->setTarget('myTarget')
5027
+            ->setNote('personal note')
5028
+            ->setId(42);
5029
+
5030
+        $result[] = [
5031
+            [
5032
+                'id' => '42',
5033
+                'share_type' => IShare::TYPE_ROOM,
5034
+                'uid_owner' => 'initiator',
5035
+                'displayname_owner' => 'initiator',
5036
+                'permissions' => 1,
5037
+                'stime' => 946684862,
5038
+                'parent' => null,
5039
+                'expiration' => null,
5040
+                'token' => null,
5041
+                'uid_file_owner' => 'owner',
5042
+                'displayname_file_owner' => 'owner',
5043
+                'note' => 'personal note',
5044
+                'path' => 'file',
5045
+                'item_type' => 'file',
5046
+                'storage_id' => 'storageId',
5047
+                'storage' => 100,
5048
+                'item_source' => 3,
5049
+                'file_source' => 3,
5050
+                'file_parent' => 1,
5051
+                'file_target' => 'myTarget',
5052
+                'share_with' => 'recipientRoom',
5053
+                'share_with_displayname' => 'recipientRoomName',
5054
+                'mail_send' => 0,
5055
+                'mimetype' => 'myMimeType',
5056
+                'has_preview' => false,
5057
+                'hide_download' => 0,
5058
+                'label' => '',
5059
+                'can_edit' => false,
5060
+                'can_delete' => false,
5061
+                'item_size' => 123456,
5062
+                'item_mtime' => 1234567890,
5063
+                'is-mount-root' => false,
5064
+                'mount-type' => '',
5065
+                'attributes' => null,
5066
+                'item_permissions' => 9,
5067
+            ], $share, true, [
5068
+                'share_with_displayname' => 'recipientRoomName'
5069
+            ]
5070
+        ];
5071
+
5072
+        return $result;
5073
+    }
5074
+
5075
+    /**
5076
+     * @dataProvider dataFormatRoomShare
5077
+     *
5078
+     * @param array $expects
5079
+     * @param IShare $share
5080
+     * @param bool $helperAvailable
5081
+     * @param array $formatShareByHelper
5082
+     */
5083
+    public function testFormatRoomShare(array $expects, IShare $share, bool $helperAvailable, array $formatShareByHelper): void {
5084
+        $this->rootFolder->method('getUserFolder')
5085
+            ->with($this->currentUser)
5086
+            ->willReturnSelf();
5087
+
5088
+        $this->rootFolder->method('getFirstNodeById')
5089
+            ->with($share->getNodeId())
5090
+            ->willReturn($share->getNode());
5091
+
5092
+        $this->rootFolder->method('getRelativePath')
5093
+            ->with($share->getNode()->getPath())
5094
+            ->willReturnArgument(0);
5095
+
5096
+        if (!$helperAvailable) {
5097
+            $this->appManager->method('isEnabledForUser')
5098
+                ->with('spreed')
5099
+                ->willReturn(false);
5100
+        } else {
5101
+            $this->appManager->method('isEnabledForUser')
5102
+                ->with('spreed')
5103
+                ->willReturn(true);
5104
+
5105
+            // This is not possible anymore with PHPUnit 10+
5106
+            // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
5107
+            // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
5108
+            $helper = $this->getMockBuilder(\stdClass::class)
5109
+                ->addMethods(['formatShare', 'canAccessShare'])
5110
+                ->getMock();
5111
+            $helper->method('formatShare')
5112
+                ->with($share)
5113
+                ->willReturn($formatShareByHelper);
5114
+            $helper->method('canAccessShare')
5115
+                ->with($share)
5116
+                ->willReturn(true);
5117
+
5118
+            $this->serverContainer->method('get')
5119
+                ->with('\OCA\Talk\Share\Helper\ShareAPIController')
5120
+                ->willReturn($helper);
5121
+        }
5122
+
5123
+        $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
5124
+        $this->assertEquals($expects, $result);
5125
+    }
5126
+
5127
+    /**
5128
+     * @return list{Folder, Folder}
5129
+     */
5130
+    private function getNonSharedUserFolder(): array {
5131
+        $node = $this->getMockBuilder(Folder::class)->getMock();
5132
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
5133
+        $storage = $this->createMock(IStorage::class);
5134
+        $storage->method('instanceOfStorage')
5135
+            ->willReturnMap([
5136
+                ['OCA\Files_Sharing\External\Storage', false],
5137
+                ['OCA\Files_Sharing\SharedStorage', false],
5138
+            ]);
5139
+        $userFolder->method('getStorage')->willReturn($storage);
5140
+        $node->method('getStorage')->willReturn($storage);
5141
+        $node->method('getId')->willReturn(42);
5142
+        $user = $this->createMock(IUser::class);
5143
+        $user->method('getUID')->willReturn($this->currentUser);
5144
+        $node->method('getOwner')->willReturn($user);
5145
+        return [$userFolder, $node];
5146
+    }
5147
+
5148
+    /**
5149
+     * @return list{Folder, File}
5150
+     */
5151
+    private function getNonSharedUserFile(): array {
5152
+        $node = $this->getMockBuilder(File::class)->getMock();
5153
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
5154
+        $storage = $this->createMock(IStorage::class);
5155
+        $storage->method('instanceOfStorage')
5156
+            ->willReturnMap([
5157
+                ['OCA\Files_Sharing\External\Storage', false],
5158
+                ['OCA\Files_Sharing\SharedStorage', false],
5159
+            ]);
5160
+        $userFolder->method('getStorage')->willReturn($storage);
5161
+        $node->method('getStorage')->willReturn($storage);
5162
+        $node->method('getId')->willReturn(42);
5163
+        return [$userFolder, $node];
5164
+    }
5165
+
5166
+    public function testPopulateTags(): void {
5167
+        $tagger = $this->createMock(ITags::class);
5168
+        $this->tagManager->method('load')
5169
+            ->with('files')
5170
+            ->willReturn($tagger);
5171
+        $data = [
5172
+            ['file_source' => 10],
5173
+            ['file_source' => 22, 'foo' => 'bar'],
5174
+            ['file_source' => 42, 'x' => 'y'],
5175
+        ];
5176
+        $tags = [
5177
+            10 => ['tag3'],
5178
+            42 => ['tag1', 'tag2'],
5179
+        ];
5180
+        $tagger->method('getTagsForObjects')
5181
+            ->with([10, 22, 42])
5182
+            ->willReturn($tags);
5183
+
5184
+        $result = self::invokePrivate($this->ocs, 'populateTags', [$data]);
5185
+        $this->assertSame([
5186
+            ['file_source' => 10, 'tags' => ['tag3']],
5187
+            ['file_source' => 22, 'foo' => 'bar', 'tags' => []],
5188
+            ['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
5189
+        ], $result);
5190
+    }
5191 5191
 }
Please login to merge, or discard this patch.
apps/files_sharing/tests/External/ManagerTest.php 1 patch
Indentation   +681 added lines, -681 removed lines patch added patch discarded remove patch
@@ -45,685 +45,685 @@
 block discarded – undo
45 45
  * @package OCA\Files_Sharing\Tests\External
46 46
  */
47 47
 class ManagerTest extends TestCase {
48
-	use UserTrait;
49
-
50
-	protected string $uid;
51
-	protected IUser $user;
52
-	protected MountProvider $testMountProvider;
53
-	protected IEventDispatcher&MockObject $eventDispatcher;
54
-	protected LoggerInterface&MockObject $logger;
55
-	protected \OC\Files\Mount\Manager $mountManager;
56
-	protected IManager&MockObject $contactsManager;
57
-	protected Manager&MockObject $manager;
58
-	protected IClientService&MockObject $clientService;
59
-	protected ICloudFederationProviderManager&MockObject $cloudFederationProviderManager;
60
-	protected ICloudFederationFactory&MockObject $cloudFederationFactory;
61
-	protected IGroupManager&MockObject $groupManager;
62
-	protected IUserManager&MockObject $userManager;
63
-
64
-	protected function setUp(): void {
65
-		parent::setUp();
66
-
67
-		$this->uid = $this->getUniqueID('user');
68
-		$this->user = $this->createUser($this->uid, '');
69
-		$this->mountManager = new \OC\Files\Mount\Manager($this->createMock(SetupManagerFactory::class));
70
-		$this->clientService = $this->getMockBuilder(IClientService::class)
71
-			->disableOriginalConstructor()->getMock();
72
-		$this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
73
-		$this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
74
-		$this->groupManager = $this->createMock(IGroupManager::class);
75
-		$this->userManager = $this->createMock(IUserManager::class);
76
-		$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
77
-
78
-		$this->contactsManager = $this->createMock(IManager::class);
79
-		// needed for MountProvider() initialization
80
-		$this->contactsManager->expects($this->any())
81
-			->method('search')
82
-			->willReturn([]);
83
-
84
-		$this->logger = $this->createMock(LoggerInterface::class);
85
-		$this->logger->expects($this->never())->method('emergency');
86
-
87
-		$this->manager = $this->createManagerForUser($this->uid);
88
-
89
-		$this->testMountProvider = new MountProvider(Server::get(IDBConnection::class), function () {
90
-			return $this->manager;
91
-		}, new CloudIdManager(
92
-			$this->contactsManager,
93
-			$this->createMock(IURLGenerator::class),
94
-			$this->userManager,
95
-			$this->createMock(ICacheFactory::class),
96
-			$this->createMock(IEventDispatcher::class)
97
-		));
98
-
99
-		$group1 = $this->createMock(IGroup::class);
100
-		$group1->expects($this->any())->method('getGID')->willReturn('group1');
101
-		$group1->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true);
102
-
103
-		$group2 = $this->createMock(IGroup::class);
104
-		$group2->expects($this->any())->method('getGID')->willReturn('group2');
105
-		$group2->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true);
106
-
107
-		$this->userManager->expects($this->any())->method('get')->willReturn($this->user);
108
-		$this->groupManager->expects($this->any())->method(('getUserGroups'))->willReturn([$group1, $group2]);
109
-		$this->groupManager->expects($this->any())->method(('get'))
110
-			->will($this->returnValueMap([
111
-				['group1', $group1],
112
-				['group2', $group2],
113
-			]));
114
-	}
115
-
116
-	protected function tearDown(): void {
117
-		// clear the share external table to avoid side effects
118
-		$query = Server::get(IDBConnection::class)->prepare('DELETE FROM `*PREFIX*share_external`');
119
-		$result = $query->execute();
120
-		$result->closeCursor();
121
-
122
-		parent::tearDown();
123
-	}
124
-
125
-	private function createManagerForUser($userId) {
126
-		$user = $this->createMock(IUser::class);
127
-		$user->method('getUID')
128
-			->willReturn($userId);
129
-		$userSession = $this->createMock(IUserSession::class);
130
-		$userSession->method('getUser')
131
-			->willReturn($user);
132
-
133
-		return $this->getMockBuilder(Manager::class)
134
-			->setConstructorArgs(
135
-				[
136
-					Server::get(IDBConnection::class),
137
-					$this->mountManager,
138
-					new StorageFactory(),
139
-					$this->clientService,
140
-					Server::get(\OCP\Notification\IManager::class),
141
-					Server::get(IDiscoveryService::class),
142
-					$this->cloudFederationProviderManager,
143
-					$this->cloudFederationFactory,
144
-					$this->groupManager,
145
-					$this->userManager,
146
-					$userSession,
147
-					$this->eventDispatcher,
148
-					$this->logger,
149
-				]
150
-			)->onlyMethods(['tryOCMEndPoint'])->getMock();
151
-	}
152
-
153
-	private function setupMounts() {
154
-		$this->clearMounts();
155
-		$mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory());
156
-		foreach ($mounts as $mount) {
157
-			$this->mountManager->addMount($mount);
158
-		}
159
-	}
160
-
161
-	private function clearMounts() {
162
-		$this->mountManager->clear();
163
-		$this->mountManager->addMount(new MountPoint(Temporary::class, '', []));
164
-	}
165
-
166
-	public function testAddUserShare(): void {
167
-		$this->doTestAddShare([
168
-			'remote' => 'http://localhost',
169
-			'token' => 'token1',
170
-			'password' => '',
171
-			'name' => '/SharedFolder',
172
-			'owner' => 'foobar',
173
-			'shareType' => IShare::TYPE_USER,
174
-			'accepted' => false,
175
-			'user' => $this->uid,
176
-			'remoteId' => '2342'
177
-		], false);
178
-	}
179
-
180
-	public function testAddGroupShare(): void {
181
-		$this->doTestAddShare([
182
-			'remote' => 'http://localhost',
183
-			'token' => 'token1',
184
-			'password' => '',
185
-			'name' => '/SharedFolder',
186
-			'owner' => 'foobar',
187
-			'shareType' => IShare::TYPE_GROUP,
188
-			'accepted' => false,
189
-			'user' => 'group1',
190
-			'remoteId' => '2342'
191
-		], true);
192
-	}
193
-
194
-	public function doTestAddShare($shareData1, $isGroup = false) {
195
-		$shareData2 = $shareData1;
196
-		$shareData2['token'] = 'token2';
197
-		$shareData3 = $shareData1;
198
-		$shareData3['token'] = 'token3';
199
-
200
-		if ($isGroup) {
201
-			$this->manager->expects($this->never())->method('tryOCMEndPoint');
202
-		} else {
203
-			$this->manager->expects(self::atLeast(2))
204
-				->method('tryOCMEndPoint')
205
-				->willReturnMap([
206
-					['http://localhost', 'token1', '2342', 'accept', false],
207
-					['http://localhost', 'token3', '2342', 'decline', false],
208
-				]);
209
-		}
210
-
211
-		// Add a share for "user"
212
-		$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
213
-		$openShares = $this->manager->getOpenShares();
214
-		$this->assertCount(1, $openShares);
215
-		$this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']);
216
-
217
-		$this->setupMounts();
218
-		$this->assertNotMount('SharedFolder');
219
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
220
-
221
-		// Add a second share for "user" with the same name
222
-		$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
223
-		$openShares = $this->manager->getOpenShares();
224
-		$this->assertCount(2, $openShares);
225
-		$this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']);
226
-		// New share falls back to "-1" appendix, because the name is already taken
227
-		$this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
228
-
229
-		$this->setupMounts();
230
-		$this->assertNotMount('SharedFolder');
231
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
232
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
233
-
234
-		$newClientCalls = [];
235
-		$this->clientService
236
-			->method('newClient')
237
-			->willReturnCallback(function () use (&$newClientCalls): IClient {
238
-				if (!empty($newClientCalls)) {
239
-					return array_shift($newClientCalls);
240
-				}
241
-				return $this->createMock(IClient::class);
242
-			});
243
-		if (!$isGroup) {
244
-			$client = $this->createMock(IClient::class);
245
-			$newClientCalls[] = $client;
246
-			$response = $this->createMock(IResponse::class);
247
-			$response->method('getBody')
248
-				->willReturn(json_encode([
249
-					'ocs' => [
250
-						'meta' => [
251
-							'statuscode' => 200,
252
-						]
253
-					]
254
-				]));
255
-			$client->expects($this->once())
256
-				->method('post')
257
-				->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
258
-				->willReturn($response);
259
-		}
260
-
261
-		// Accept the first share
262
-		$this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
263
-
264
-		// Check remaining shares - Accepted
265
-		$acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
266
-		$this->assertCount(1, $acceptedShares);
267
-		$shareData1['accepted'] = true;
268
-		$this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid);
269
-		// Check remaining shares - Open
270
-		$openShares = $this->manager->getOpenShares();
271
-		$this->assertCount(1, $openShares);
272
-		$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
273
-
274
-		$this->setupMounts();
275
-		$this->assertMount($shareData1['name']);
276
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
277
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
278
-
279
-		// Add another share for "user" with the same name
280
-		$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
281
-		$openShares = $this->manager->getOpenShares();
282
-		$this->assertCount(2, $openShares);
283
-		$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
284
-		if (!$isGroup) {
285
-			// New share falls back to the original name (no "-\d", because the name is not taken)
286
-			$this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}', $shareData3['user']);
287
-		} else {
288
-			$this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $shareData3['user']);
289
-		}
290
-
291
-		$this->setupMounts();
292
-		$this->assertMount($shareData1['name']);
293
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
294
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
295
-
296
-		if (!$isGroup) {
297
-			$client = $this->createMock(IClient::class);
298
-			$newClientCalls[] = $client;
299
-			$response = $this->createMock(IResponse::class);
300
-			$response->method('getBody')
301
-				->willReturn(json_encode([
302
-					'ocs' => [
303
-						'meta' => [
304
-							'statuscode' => 200,
305
-						]
306
-					]
307
-				]));
308
-			$client->expects($this->once())
309
-				->method('post')
310
-				->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
311
-				->willReturn($response);
312
-		}
313
-
314
-		// Decline the third share
315
-		$this->assertTrue($this->manager->declineShare($openShares[1]['id']));
316
-
317
-		$this->setupMounts();
318
-		$this->assertMount($shareData1['name']);
319
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
320
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
321
-
322
-		// Check remaining shares - Accepted
323
-		$acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
324
-		$this->assertCount(1, $acceptedShares);
325
-		$shareData1['accepted'] = true;
326
-		$this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid);
327
-		// Check remaining shares - Open
328
-		$openShares = $this->manager->getOpenShares();
329
-		if ($isGroup) {
330
-			// declining a group share adds it back to pending instead of deleting it
331
-			$this->assertCount(2, $openShares);
332
-			// this is a group share that is still open
333
-			$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
334
-			// this is the user share sub-entry matching the group share which got declined
335
-			$this->assertExternalShareEntry($shareData3, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $this->uid);
336
-		} else {
337
-			$this->assertCount(1, $openShares);
338
-			$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $this->uid);
339
-		}
340
-
341
-		$this->setupMounts();
342
-		$this->assertMount($shareData1['name']);
343
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
344
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
345
-
346
-		if ($isGroup) {
347
-			// no http requests here
348
-			$this->manager->removeGroupShares('group1');
349
-		} else {
350
-			$client1 = $this->createMock(IClient::class);
351
-			$client2 = $this->createMock(IClient::class);
352
-			$newClientCalls[] = $client1;
353
-			$newClientCalls[] = $client2;
354
-			$response = $this->createMock(IResponse::class);
355
-			$response->method('getBody')
356
-				->willReturn(json_encode([
357
-					'ocs' => [
358
-						'meta' => [
359
-							'statuscode' => 200,
360
-						]
361
-					]
362
-				]));
363
-
364
-			$client1->expects($this->once())
365
-				->method('post')
366
-				->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
367
-				->willReturn($response);
368
-			$client2->expects($this->once())
369
-				->method('post')
370
-				->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
371
-				->willReturn($response);
372
-
373
-			$this->manager->removeUserShares($this->uid);
374
-		}
375
-
376
-		$this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
377
-
378
-		$this->clearMounts();
379
-		self::invokePrivate($this->manager, 'setupMounts');
380
-		$this->assertNotMount($shareData1['name']);
381
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
382
-		$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
383
-	}
384
-
385
-	private function verifyAcceptedGroupShare($shareData) {
386
-		$openShares = $this->manager->getOpenShares();
387
-		$this->assertCount(0, $openShares);
388
-		$acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
389
-		$this->assertCount(1, $acceptedShares);
390
-		$shareData['accepted'] = true;
391
-		$this->assertExternalShareEntry($shareData, $acceptedShares[0], 0, $shareData['name'], $this->uid);
392
-		$this->setupMounts();
393
-		$this->assertMount($shareData['name']);
394
-	}
395
-
396
-	private function verifyDeclinedGroupShare($shareData, $tempMount = null) {
397
-		if ($tempMount === null) {
398
-			$tempMount = '{{TemporaryMountPointName#/SharedFolder}}';
399
-		}
400
-		$openShares = $this->manager->getOpenShares();
401
-		$this->assertCount(1, $openShares);
402
-		$acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
403
-		$this->assertCount(0, $acceptedShares);
404
-		$this->assertExternalShareEntry($shareData, $openShares[0], 0, $tempMount, $this->uid);
405
-		$this->setupMounts();
406
-		$this->assertNotMount($shareData['name']);
407
-		$this->assertNotMount($tempMount);
408
-	}
409
-
410
-	private function createTestUserShare($userId = 'user1') {
411
-		$shareData = [
412
-			'remote' => 'http://localhost',
413
-			'token' => 'token1',
414
-			'password' => '',
415
-			'name' => '/SharedFolder',
416
-			'owner' => 'foobar',
417
-			'shareType' => IShare::TYPE_USER,
418
-			'accepted' => false,
419
-			'user' => $userId,
420
-			'remoteId' => '2342'
421
-		];
422
-
423
-		$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData));
424
-
425
-		return $shareData;
426
-	}
427
-	private function createTestGroupShare($groupId = 'group1') {
428
-		$shareData = [
429
-			'remote' => 'http://localhost',
430
-			'token' => 'token1',
431
-			'password' => '',
432
-			'name' => '/SharedFolder',
433
-			'owner' => 'foobar',
434
-			'shareType' => IShare::TYPE_GROUP,
435
-			'accepted' => false,
436
-			'user' => $groupId,
437
-			'remoteId' => '2342'
438
-		];
439
-
440
-		$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData));
441
-
442
-		$allShares = self::invokePrivate($this->manager, 'getShares', [null]);
443
-		foreach ($allShares as $share) {
444
-			if ($share['user'] === $groupId) {
445
-				// this will hold the main group entry
446
-				$groupShare = $share;
447
-				break;
448
-			}
449
-		}
450
-
451
-		return [$shareData, $groupShare];
452
-	}
453
-
454
-	public function testAcceptOriginalGroupShare(): void {
455
-		[$shareData, $groupShare] = $this->createTestGroupShare();
456
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
457
-		$this->verifyAcceptedGroupShare($shareData);
458
-
459
-		// a second time
460
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
461
-		$this->verifyAcceptedGroupShare($shareData);
462
-	}
463
-
464
-	public function testAcceptGroupShareAgainThroughGroupShare(): void {
465
-		[$shareData, $groupShare] = $this->createTestGroupShare();
466
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
467
-		$this->verifyAcceptedGroupShare($shareData);
468
-
469
-		// decline again, this keeps the sub-share
470
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
471
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
472
-
473
-		// this will return sub-entries
474
-		$openShares = $this->manager->getOpenShares();
475
-		$this->assertCount(1, $openShares);
476
-
477
-		// accept through group share
478
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
479
-		$this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
480
-
481
-		// accept a second time
482
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
483
-		$this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
484
-	}
485
-
486
-	public function testAcceptGroupShareAgainThroughSubShare(): void {
487
-		[$shareData, $groupShare] = $this->createTestGroupShare();
488
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
489
-		$this->verifyAcceptedGroupShare($shareData);
490
-
491
-		// decline again, this keeps the sub-share
492
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
493
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
494
-
495
-		// this will return sub-entries
496
-		$openShares = $this->manager->getOpenShares();
497
-		$this->assertCount(1, $openShares);
498
-
499
-		// accept through sub-share
500
-		$this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
501
-		$this->verifyAcceptedGroupShare($shareData);
502
-
503
-		// accept a second time
504
-		$this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
505
-		$this->verifyAcceptedGroupShare($shareData);
506
-	}
507
-
508
-	public function testDeclineOriginalGroupShare(): void {
509
-		[$shareData, $groupShare] = $this->createTestGroupShare();
510
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
511
-		$this->verifyDeclinedGroupShare($shareData);
512
-
513
-		// a second time
514
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
515
-		$this->verifyDeclinedGroupShare($shareData);
516
-	}
517
-
518
-	public function testDeclineGroupShareAgainThroughGroupShare(): void {
519
-		[$shareData, $groupShare] = $this->createTestGroupShare();
520
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
521
-		$this->verifyAcceptedGroupShare($shareData);
522
-
523
-		// decline again, this keeps the sub-share
524
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
525
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
526
-
527
-		// a second time
528
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
529
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
530
-	}
531
-
532
-	public function testDeclineGroupShareAgainThroughSubshare(): void {
533
-		[$shareData, $groupShare] = $this->createTestGroupShare();
534
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
535
-		$this->verifyAcceptedGroupShare($shareData);
536
-
537
-		// this will return sub-entries
538
-		$allShares = self::invokePrivate($this->manager, 'getShares', [null]);
539
-		$this->assertCount(1, $allShares);
540
-
541
-		// decline again through sub-share
542
-		$this->assertTrue($this->manager->declineShare($allShares[0]['id']));
543
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
544
-
545
-		// a second time
546
-		$this->assertTrue($this->manager->declineShare($allShares[0]['id']));
547
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
548
-	}
549
-
550
-	public function testDeclineGroupShareAgainThroughMountPoint(): void {
551
-		[$shareData, $groupShare] = $this->createTestGroupShare();
552
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
553
-		$this->verifyAcceptedGroupShare($shareData);
554
-
555
-		// decline through mount point name
556
-		$this->assertTrue($this->manager->removeShare($this->uid . '/files/' . $shareData['name']));
557
-		$this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
558
-
559
-		// second time must fail as the mount point is gone
560
-		$this->assertFalse($this->manager->removeShare($this->uid . '/files/' . $shareData['name']));
561
-	}
562
-
563
-	public function testDeclineThenAcceptGroupShareAgainThroughGroupShare(): void {
564
-		[$shareData, $groupShare] = $this->createTestGroupShare();
565
-		// decline, this creates a declined sub-share
566
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
567
-		$this->verifyDeclinedGroupShare($shareData);
568
-
569
-		// this will return sub-entries
570
-		$openShares = $this->manager->getOpenShares();
571
-
572
-		// accept through sub-share
573
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
574
-		$this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
575
-
576
-		// accept a second time
577
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
578
-		$this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
579
-	}
580
-
581
-	public function testDeclineThenAcceptGroupShareAgainThroughSubShare(): void {
582
-		[$shareData, $groupShare] = $this->createTestGroupShare();
583
-		// decline, this creates a declined sub-share
584
-		$this->assertTrue($this->manager->declineShare($groupShare['id']));
585
-		$this->verifyDeclinedGroupShare($shareData);
586
-
587
-		// this will return sub-entries
588
-		$openShares = $this->manager->getOpenShares();
589
-
590
-		// accept through sub-share
591
-		$this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
592
-		$this->verifyAcceptedGroupShare($shareData);
593
-
594
-		// accept a second time
595
-		$this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
596
-		$this->verifyAcceptedGroupShare($shareData);
597
-	}
598
-
599
-	public function testDeleteUserShares(): void {
600
-		// user 1 shares
601
-
602
-		$shareData = $this->createTestUserShare($this->uid);
603
-
604
-		[$shareData, $groupShare] = $this->createTestGroupShare();
605
-
606
-		$shares = $this->manager->getOpenShares();
607
-		$this->assertCount(2, $shares);
608
-
609
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
610
-
611
-		// user 2 shares
612
-		$manager2 = $this->createManagerForUser('user2');
613
-		$shareData2 = [
614
-			'remote' => 'http://localhost',
615
-			'token' => 'token1',
616
-			'password' => '',
617
-			'name' => '/SharedFolder',
618
-			'owner' => 'foobar',
619
-			'shareType' => IShare::TYPE_USER,
620
-			'accepted' => false,
621
-			'user' => 'user2',
622
-			'remoteId' => '2342'
623
-		];
624
-
625
-		$this->assertCount(1, $manager2->getOpenShares());
626
-		$this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2));
627
-		$this->assertCount(2, $manager2->getOpenShares());
628
-
629
-		$this->manager->expects($this->once())->method('tryOCMEndPoint')->with('http://localhost', 'token1', '2342', 'decline')->willReturn([]);
630
-		$this->manager->removeUserShares($this->uid);
631
-
632
-		$user1Shares = $this->manager->getOpenShares();
633
-		// user share is gone, group is still there
634
-		$this->assertCount(1, $user1Shares);
635
-		$this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_GROUP);
636
-
637
-		// user 2 shares untouched
638
-		$user2Shares = $manager2->getOpenShares();
639
-		$this->assertCount(2, $user2Shares);
640
-		$this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_GROUP);
641
-		$this->assertEquals($user2Shares[0]['user'], 'group1');
642
-		$this->assertEquals($user2Shares[1]['share_type'], IShare::TYPE_USER);
643
-		$this->assertEquals($user2Shares[1]['user'], 'user2');
644
-	}
645
-
646
-	public function testDeleteGroupShares(): void {
647
-		$shareData = $this->createTestUserShare($this->uid);
648
-
649
-		[$shareData, $groupShare] = $this->createTestGroupShare();
650
-
651
-		$shares = $this->manager->getOpenShares();
652
-		$this->assertCount(2, $shares);
653
-
654
-		$this->assertTrue($this->manager->acceptShare($groupShare['id']));
655
-
656
-		// user 2 shares
657
-		$manager2 = $this->createManagerForUser('user2');
658
-		$shareData2 = [
659
-			'remote' => 'http://localhost',
660
-			'token' => 'token1',
661
-			'password' => '',
662
-			'name' => '/SharedFolder',
663
-			'owner' => 'foobar',
664
-			'shareType' => IShare::TYPE_USER,
665
-			'accepted' => false,
666
-			'user' => 'user2',
667
-			'remoteId' => '2342'
668
-		];
669
-
670
-		$this->assertCount(1, $manager2->getOpenShares());
671
-		$this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2));
672
-		$this->assertCount(2, $manager2->getOpenShares());
673
-
674
-		$this->manager->expects($this->never())->method('tryOCMEndPoint');
675
-		$this->manager->removeGroupShares('group1');
676
-
677
-		$user1Shares = $this->manager->getOpenShares();
678
-		// user share is gone, group is still there
679
-		$this->assertCount(1, $user1Shares);
680
-		$this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_USER);
681
-
682
-		// user 2 shares untouched
683
-		$user2Shares = $manager2->getOpenShares();
684
-		$this->assertCount(1, $user2Shares);
685
-		$this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_USER);
686
-		$this->assertEquals($user2Shares[0]['user'], 'user2');
687
-	}
688
-
689
-	/**
690
-	 * @param array $expected
691
-	 * @param array $actual
692
-	 * @param int $share
693
-	 * @param string $mountPoint
694
-	 */
695
-	protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint, $targetEntity) {
696
-		$this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
697
-		$this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
698
-		$this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
699
-		$this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
700
-		$this->assertEquals($expected['accepted'], (int)$actual['accepted'], 'Asserting accept of a share #' . $share);
701
-		$this->assertEquals($targetEntity, $actual['user'], 'Asserting user of a share #' . $share);
702
-		$this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
703
-	}
704
-
705
-	private function assertMount($mountPoint) {
706
-		$mountPoint = rtrim($mountPoint, '/');
707
-		$mount = $this->mountManager->find($this->getFullPath($mountPoint));
708
-		$this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
709
-		$this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
710
-		$this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
711
-		$storage = $mount->getStorage();
712
-		$this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
713
-	}
714
-
715
-	private function assertNotMount($mountPoint) {
716
-		$mountPoint = rtrim($mountPoint, '/');
717
-		try {
718
-			$mount = $this->mountManager->find($this->getFullPath($mountPoint));
719
-			$this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
720
-			$this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
721
-		} catch (NotFoundException $e) {
722
-
723
-		}
724
-	}
725
-
726
-	private function getFullPath($path) {
727
-		return '/' . $this->uid . '/files' . $path;
728
-	}
48
+    use UserTrait;
49
+
50
+    protected string $uid;
51
+    protected IUser $user;
52
+    protected MountProvider $testMountProvider;
53
+    protected IEventDispatcher&MockObject $eventDispatcher;
54
+    protected LoggerInterface&MockObject $logger;
55
+    protected \OC\Files\Mount\Manager $mountManager;
56
+    protected IManager&MockObject $contactsManager;
57
+    protected Manager&MockObject $manager;
58
+    protected IClientService&MockObject $clientService;
59
+    protected ICloudFederationProviderManager&MockObject $cloudFederationProviderManager;
60
+    protected ICloudFederationFactory&MockObject $cloudFederationFactory;
61
+    protected IGroupManager&MockObject $groupManager;
62
+    protected IUserManager&MockObject $userManager;
63
+
64
+    protected function setUp(): void {
65
+        parent::setUp();
66
+
67
+        $this->uid = $this->getUniqueID('user');
68
+        $this->user = $this->createUser($this->uid, '');
69
+        $this->mountManager = new \OC\Files\Mount\Manager($this->createMock(SetupManagerFactory::class));
70
+        $this->clientService = $this->getMockBuilder(IClientService::class)
71
+            ->disableOriginalConstructor()->getMock();
72
+        $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
73
+        $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
74
+        $this->groupManager = $this->createMock(IGroupManager::class);
75
+        $this->userManager = $this->createMock(IUserManager::class);
76
+        $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
77
+
78
+        $this->contactsManager = $this->createMock(IManager::class);
79
+        // needed for MountProvider() initialization
80
+        $this->contactsManager->expects($this->any())
81
+            ->method('search')
82
+            ->willReturn([]);
83
+
84
+        $this->logger = $this->createMock(LoggerInterface::class);
85
+        $this->logger->expects($this->never())->method('emergency');
86
+
87
+        $this->manager = $this->createManagerForUser($this->uid);
88
+
89
+        $this->testMountProvider = new MountProvider(Server::get(IDBConnection::class), function () {
90
+            return $this->manager;
91
+        }, new CloudIdManager(
92
+            $this->contactsManager,
93
+            $this->createMock(IURLGenerator::class),
94
+            $this->userManager,
95
+            $this->createMock(ICacheFactory::class),
96
+            $this->createMock(IEventDispatcher::class)
97
+        ));
98
+
99
+        $group1 = $this->createMock(IGroup::class);
100
+        $group1->expects($this->any())->method('getGID')->willReturn('group1');
101
+        $group1->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true);
102
+
103
+        $group2 = $this->createMock(IGroup::class);
104
+        $group2->expects($this->any())->method('getGID')->willReturn('group2');
105
+        $group2->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true);
106
+
107
+        $this->userManager->expects($this->any())->method('get')->willReturn($this->user);
108
+        $this->groupManager->expects($this->any())->method(('getUserGroups'))->willReturn([$group1, $group2]);
109
+        $this->groupManager->expects($this->any())->method(('get'))
110
+            ->will($this->returnValueMap([
111
+                ['group1', $group1],
112
+                ['group2', $group2],
113
+            ]));
114
+    }
115
+
116
+    protected function tearDown(): void {
117
+        // clear the share external table to avoid side effects
118
+        $query = Server::get(IDBConnection::class)->prepare('DELETE FROM `*PREFIX*share_external`');
119
+        $result = $query->execute();
120
+        $result->closeCursor();
121
+
122
+        parent::tearDown();
123
+    }
124
+
125
+    private function createManagerForUser($userId) {
126
+        $user = $this->createMock(IUser::class);
127
+        $user->method('getUID')
128
+            ->willReturn($userId);
129
+        $userSession = $this->createMock(IUserSession::class);
130
+        $userSession->method('getUser')
131
+            ->willReturn($user);
132
+
133
+        return $this->getMockBuilder(Manager::class)
134
+            ->setConstructorArgs(
135
+                [
136
+                    Server::get(IDBConnection::class),
137
+                    $this->mountManager,
138
+                    new StorageFactory(),
139
+                    $this->clientService,
140
+                    Server::get(\OCP\Notification\IManager::class),
141
+                    Server::get(IDiscoveryService::class),
142
+                    $this->cloudFederationProviderManager,
143
+                    $this->cloudFederationFactory,
144
+                    $this->groupManager,
145
+                    $this->userManager,
146
+                    $userSession,
147
+                    $this->eventDispatcher,
148
+                    $this->logger,
149
+                ]
150
+            )->onlyMethods(['tryOCMEndPoint'])->getMock();
151
+    }
152
+
153
+    private function setupMounts() {
154
+        $this->clearMounts();
155
+        $mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory());
156
+        foreach ($mounts as $mount) {
157
+            $this->mountManager->addMount($mount);
158
+        }
159
+    }
160
+
161
+    private function clearMounts() {
162
+        $this->mountManager->clear();
163
+        $this->mountManager->addMount(new MountPoint(Temporary::class, '', []));
164
+    }
165
+
166
+    public function testAddUserShare(): void {
167
+        $this->doTestAddShare([
168
+            'remote' => 'http://localhost',
169
+            'token' => 'token1',
170
+            'password' => '',
171
+            'name' => '/SharedFolder',
172
+            'owner' => 'foobar',
173
+            'shareType' => IShare::TYPE_USER,
174
+            'accepted' => false,
175
+            'user' => $this->uid,
176
+            'remoteId' => '2342'
177
+        ], false);
178
+    }
179
+
180
+    public function testAddGroupShare(): void {
181
+        $this->doTestAddShare([
182
+            'remote' => 'http://localhost',
183
+            'token' => 'token1',
184
+            'password' => '',
185
+            'name' => '/SharedFolder',
186
+            'owner' => 'foobar',
187
+            'shareType' => IShare::TYPE_GROUP,
188
+            'accepted' => false,
189
+            'user' => 'group1',
190
+            'remoteId' => '2342'
191
+        ], true);
192
+    }
193
+
194
+    public function doTestAddShare($shareData1, $isGroup = false) {
195
+        $shareData2 = $shareData1;
196
+        $shareData2['token'] = 'token2';
197
+        $shareData3 = $shareData1;
198
+        $shareData3['token'] = 'token3';
199
+
200
+        if ($isGroup) {
201
+            $this->manager->expects($this->never())->method('tryOCMEndPoint');
202
+        } else {
203
+            $this->manager->expects(self::atLeast(2))
204
+                ->method('tryOCMEndPoint')
205
+                ->willReturnMap([
206
+                    ['http://localhost', 'token1', '2342', 'accept', false],
207
+                    ['http://localhost', 'token3', '2342', 'decline', false],
208
+                ]);
209
+        }
210
+
211
+        // Add a share for "user"
212
+        $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
213
+        $openShares = $this->manager->getOpenShares();
214
+        $this->assertCount(1, $openShares);
215
+        $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']);
216
+
217
+        $this->setupMounts();
218
+        $this->assertNotMount('SharedFolder');
219
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
220
+
221
+        // Add a second share for "user" with the same name
222
+        $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
223
+        $openShares = $this->manager->getOpenShares();
224
+        $this->assertCount(2, $openShares);
225
+        $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']);
226
+        // New share falls back to "-1" appendix, because the name is already taken
227
+        $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
228
+
229
+        $this->setupMounts();
230
+        $this->assertNotMount('SharedFolder');
231
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
232
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
233
+
234
+        $newClientCalls = [];
235
+        $this->clientService
236
+            ->method('newClient')
237
+            ->willReturnCallback(function () use (&$newClientCalls): IClient {
238
+                if (!empty($newClientCalls)) {
239
+                    return array_shift($newClientCalls);
240
+                }
241
+                return $this->createMock(IClient::class);
242
+            });
243
+        if (!$isGroup) {
244
+            $client = $this->createMock(IClient::class);
245
+            $newClientCalls[] = $client;
246
+            $response = $this->createMock(IResponse::class);
247
+            $response->method('getBody')
248
+                ->willReturn(json_encode([
249
+                    'ocs' => [
250
+                        'meta' => [
251
+                            'statuscode' => 200,
252
+                        ]
253
+                    ]
254
+                ]));
255
+            $client->expects($this->once())
256
+                ->method('post')
257
+                ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
258
+                ->willReturn($response);
259
+        }
260
+
261
+        // Accept the first share
262
+        $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
263
+
264
+        // Check remaining shares - Accepted
265
+        $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
266
+        $this->assertCount(1, $acceptedShares);
267
+        $shareData1['accepted'] = true;
268
+        $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid);
269
+        // Check remaining shares - Open
270
+        $openShares = $this->manager->getOpenShares();
271
+        $this->assertCount(1, $openShares);
272
+        $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
273
+
274
+        $this->setupMounts();
275
+        $this->assertMount($shareData1['name']);
276
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
277
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
278
+
279
+        // Add another share for "user" with the same name
280
+        $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
281
+        $openShares = $this->manager->getOpenShares();
282
+        $this->assertCount(2, $openShares);
283
+        $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
284
+        if (!$isGroup) {
285
+            // New share falls back to the original name (no "-\d", because the name is not taken)
286
+            $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}', $shareData3['user']);
287
+        } else {
288
+            $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $shareData3['user']);
289
+        }
290
+
291
+        $this->setupMounts();
292
+        $this->assertMount($shareData1['name']);
293
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
294
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
295
+
296
+        if (!$isGroup) {
297
+            $client = $this->createMock(IClient::class);
298
+            $newClientCalls[] = $client;
299
+            $response = $this->createMock(IResponse::class);
300
+            $response->method('getBody')
301
+                ->willReturn(json_encode([
302
+                    'ocs' => [
303
+                        'meta' => [
304
+                            'statuscode' => 200,
305
+                        ]
306
+                    ]
307
+                ]));
308
+            $client->expects($this->once())
309
+                ->method('post')
310
+                ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
311
+                ->willReturn($response);
312
+        }
313
+
314
+        // Decline the third share
315
+        $this->assertTrue($this->manager->declineShare($openShares[1]['id']));
316
+
317
+        $this->setupMounts();
318
+        $this->assertMount($shareData1['name']);
319
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
320
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
321
+
322
+        // Check remaining shares - Accepted
323
+        $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
324
+        $this->assertCount(1, $acceptedShares);
325
+        $shareData1['accepted'] = true;
326
+        $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid);
327
+        // Check remaining shares - Open
328
+        $openShares = $this->manager->getOpenShares();
329
+        if ($isGroup) {
330
+            // declining a group share adds it back to pending instead of deleting it
331
+            $this->assertCount(2, $openShares);
332
+            // this is a group share that is still open
333
+            $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']);
334
+            // this is the user share sub-entry matching the group share which got declined
335
+            $this->assertExternalShareEntry($shareData3, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $this->uid);
336
+        } else {
337
+            $this->assertCount(1, $openShares);
338
+            $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $this->uid);
339
+        }
340
+
341
+        $this->setupMounts();
342
+        $this->assertMount($shareData1['name']);
343
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
344
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
345
+
346
+        if ($isGroup) {
347
+            // no http requests here
348
+            $this->manager->removeGroupShares('group1');
349
+        } else {
350
+            $client1 = $this->createMock(IClient::class);
351
+            $client2 = $this->createMock(IClient::class);
352
+            $newClientCalls[] = $client1;
353
+            $newClientCalls[] = $client2;
354
+            $response = $this->createMock(IResponse::class);
355
+            $response->method('getBody')
356
+                ->willReturn(json_encode([
357
+                    'ocs' => [
358
+                        'meta' => [
359
+                            'statuscode' => 200,
360
+                        ]
361
+                    ]
362
+                ]));
363
+
364
+            $client1->expects($this->once())
365
+                ->method('post')
366
+                ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
367
+                ->willReturn($response);
368
+            $client2->expects($this->once())
369
+                ->method('post')
370
+                ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
371
+                ->willReturn($response);
372
+
373
+            $this->manager->removeUserShares($this->uid);
374
+        }
375
+
376
+        $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
377
+
378
+        $this->clearMounts();
379
+        self::invokePrivate($this->manager, 'setupMounts');
380
+        $this->assertNotMount($shareData1['name']);
381
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
382
+        $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
383
+    }
384
+
385
+    private function verifyAcceptedGroupShare($shareData) {
386
+        $openShares = $this->manager->getOpenShares();
387
+        $this->assertCount(0, $openShares);
388
+        $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
389
+        $this->assertCount(1, $acceptedShares);
390
+        $shareData['accepted'] = true;
391
+        $this->assertExternalShareEntry($shareData, $acceptedShares[0], 0, $shareData['name'], $this->uid);
392
+        $this->setupMounts();
393
+        $this->assertMount($shareData['name']);
394
+    }
395
+
396
+    private function verifyDeclinedGroupShare($shareData, $tempMount = null) {
397
+        if ($tempMount === null) {
398
+            $tempMount = '{{TemporaryMountPointName#/SharedFolder}}';
399
+        }
400
+        $openShares = $this->manager->getOpenShares();
401
+        $this->assertCount(1, $openShares);
402
+        $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
403
+        $this->assertCount(0, $acceptedShares);
404
+        $this->assertExternalShareEntry($shareData, $openShares[0], 0, $tempMount, $this->uid);
405
+        $this->setupMounts();
406
+        $this->assertNotMount($shareData['name']);
407
+        $this->assertNotMount($tempMount);
408
+    }
409
+
410
+    private function createTestUserShare($userId = 'user1') {
411
+        $shareData = [
412
+            'remote' => 'http://localhost',
413
+            'token' => 'token1',
414
+            'password' => '',
415
+            'name' => '/SharedFolder',
416
+            'owner' => 'foobar',
417
+            'shareType' => IShare::TYPE_USER,
418
+            'accepted' => false,
419
+            'user' => $userId,
420
+            'remoteId' => '2342'
421
+        ];
422
+
423
+        $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData));
424
+
425
+        return $shareData;
426
+    }
427
+    private function createTestGroupShare($groupId = 'group1') {
428
+        $shareData = [
429
+            'remote' => 'http://localhost',
430
+            'token' => 'token1',
431
+            'password' => '',
432
+            'name' => '/SharedFolder',
433
+            'owner' => 'foobar',
434
+            'shareType' => IShare::TYPE_GROUP,
435
+            'accepted' => false,
436
+            'user' => $groupId,
437
+            'remoteId' => '2342'
438
+        ];
439
+
440
+        $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData));
441
+
442
+        $allShares = self::invokePrivate($this->manager, 'getShares', [null]);
443
+        foreach ($allShares as $share) {
444
+            if ($share['user'] === $groupId) {
445
+                // this will hold the main group entry
446
+                $groupShare = $share;
447
+                break;
448
+            }
449
+        }
450
+
451
+        return [$shareData, $groupShare];
452
+    }
453
+
454
+    public function testAcceptOriginalGroupShare(): void {
455
+        [$shareData, $groupShare] = $this->createTestGroupShare();
456
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
457
+        $this->verifyAcceptedGroupShare($shareData);
458
+
459
+        // a second time
460
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
461
+        $this->verifyAcceptedGroupShare($shareData);
462
+    }
463
+
464
+    public function testAcceptGroupShareAgainThroughGroupShare(): void {
465
+        [$shareData, $groupShare] = $this->createTestGroupShare();
466
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
467
+        $this->verifyAcceptedGroupShare($shareData);
468
+
469
+        // decline again, this keeps the sub-share
470
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
471
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
472
+
473
+        // this will return sub-entries
474
+        $openShares = $this->manager->getOpenShares();
475
+        $this->assertCount(1, $openShares);
476
+
477
+        // accept through group share
478
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
479
+        $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
480
+
481
+        // accept a second time
482
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
483
+        $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
484
+    }
485
+
486
+    public function testAcceptGroupShareAgainThroughSubShare(): void {
487
+        [$shareData, $groupShare] = $this->createTestGroupShare();
488
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
489
+        $this->verifyAcceptedGroupShare($shareData);
490
+
491
+        // decline again, this keeps the sub-share
492
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
493
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
494
+
495
+        // this will return sub-entries
496
+        $openShares = $this->manager->getOpenShares();
497
+        $this->assertCount(1, $openShares);
498
+
499
+        // accept through sub-share
500
+        $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
501
+        $this->verifyAcceptedGroupShare($shareData);
502
+
503
+        // accept a second time
504
+        $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
505
+        $this->verifyAcceptedGroupShare($shareData);
506
+    }
507
+
508
+    public function testDeclineOriginalGroupShare(): void {
509
+        [$shareData, $groupShare] = $this->createTestGroupShare();
510
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
511
+        $this->verifyDeclinedGroupShare($shareData);
512
+
513
+        // a second time
514
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
515
+        $this->verifyDeclinedGroupShare($shareData);
516
+    }
517
+
518
+    public function testDeclineGroupShareAgainThroughGroupShare(): void {
519
+        [$shareData, $groupShare] = $this->createTestGroupShare();
520
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
521
+        $this->verifyAcceptedGroupShare($shareData);
522
+
523
+        // decline again, this keeps the sub-share
524
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
525
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
526
+
527
+        // a second time
528
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
529
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
530
+    }
531
+
532
+    public function testDeclineGroupShareAgainThroughSubshare(): void {
533
+        [$shareData, $groupShare] = $this->createTestGroupShare();
534
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
535
+        $this->verifyAcceptedGroupShare($shareData);
536
+
537
+        // this will return sub-entries
538
+        $allShares = self::invokePrivate($this->manager, 'getShares', [null]);
539
+        $this->assertCount(1, $allShares);
540
+
541
+        // decline again through sub-share
542
+        $this->assertTrue($this->manager->declineShare($allShares[0]['id']));
543
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
544
+
545
+        // a second time
546
+        $this->assertTrue($this->manager->declineShare($allShares[0]['id']));
547
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
548
+    }
549
+
550
+    public function testDeclineGroupShareAgainThroughMountPoint(): void {
551
+        [$shareData, $groupShare] = $this->createTestGroupShare();
552
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
553
+        $this->verifyAcceptedGroupShare($shareData);
554
+
555
+        // decline through mount point name
556
+        $this->assertTrue($this->manager->removeShare($this->uid . '/files/' . $shareData['name']));
557
+        $this->verifyDeclinedGroupShare($shareData, '/SharedFolder');
558
+
559
+        // second time must fail as the mount point is gone
560
+        $this->assertFalse($this->manager->removeShare($this->uid . '/files/' . $shareData['name']));
561
+    }
562
+
563
+    public function testDeclineThenAcceptGroupShareAgainThroughGroupShare(): void {
564
+        [$shareData, $groupShare] = $this->createTestGroupShare();
565
+        // decline, this creates a declined sub-share
566
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
567
+        $this->verifyDeclinedGroupShare($shareData);
568
+
569
+        // this will return sub-entries
570
+        $openShares = $this->manager->getOpenShares();
571
+
572
+        // accept through sub-share
573
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
574
+        $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
575
+
576
+        // accept a second time
577
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
578
+        $this->verifyAcceptedGroupShare($shareData, '/SharedFolder');
579
+    }
580
+
581
+    public function testDeclineThenAcceptGroupShareAgainThroughSubShare(): void {
582
+        [$shareData, $groupShare] = $this->createTestGroupShare();
583
+        // decline, this creates a declined sub-share
584
+        $this->assertTrue($this->manager->declineShare($groupShare['id']));
585
+        $this->verifyDeclinedGroupShare($shareData);
586
+
587
+        // this will return sub-entries
588
+        $openShares = $this->manager->getOpenShares();
589
+
590
+        // accept through sub-share
591
+        $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
592
+        $this->verifyAcceptedGroupShare($shareData);
593
+
594
+        // accept a second time
595
+        $this->assertTrue($this->manager->acceptShare($openShares[0]['id']));
596
+        $this->verifyAcceptedGroupShare($shareData);
597
+    }
598
+
599
+    public function testDeleteUserShares(): void {
600
+        // user 1 shares
601
+
602
+        $shareData = $this->createTestUserShare($this->uid);
603
+
604
+        [$shareData, $groupShare] = $this->createTestGroupShare();
605
+
606
+        $shares = $this->manager->getOpenShares();
607
+        $this->assertCount(2, $shares);
608
+
609
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
610
+
611
+        // user 2 shares
612
+        $manager2 = $this->createManagerForUser('user2');
613
+        $shareData2 = [
614
+            'remote' => 'http://localhost',
615
+            'token' => 'token1',
616
+            'password' => '',
617
+            'name' => '/SharedFolder',
618
+            'owner' => 'foobar',
619
+            'shareType' => IShare::TYPE_USER,
620
+            'accepted' => false,
621
+            'user' => 'user2',
622
+            'remoteId' => '2342'
623
+        ];
624
+
625
+        $this->assertCount(1, $manager2->getOpenShares());
626
+        $this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2));
627
+        $this->assertCount(2, $manager2->getOpenShares());
628
+
629
+        $this->manager->expects($this->once())->method('tryOCMEndPoint')->with('http://localhost', 'token1', '2342', 'decline')->willReturn([]);
630
+        $this->manager->removeUserShares($this->uid);
631
+
632
+        $user1Shares = $this->manager->getOpenShares();
633
+        // user share is gone, group is still there
634
+        $this->assertCount(1, $user1Shares);
635
+        $this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_GROUP);
636
+
637
+        // user 2 shares untouched
638
+        $user2Shares = $manager2->getOpenShares();
639
+        $this->assertCount(2, $user2Shares);
640
+        $this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_GROUP);
641
+        $this->assertEquals($user2Shares[0]['user'], 'group1');
642
+        $this->assertEquals($user2Shares[1]['share_type'], IShare::TYPE_USER);
643
+        $this->assertEquals($user2Shares[1]['user'], 'user2');
644
+    }
645
+
646
+    public function testDeleteGroupShares(): void {
647
+        $shareData = $this->createTestUserShare($this->uid);
648
+
649
+        [$shareData, $groupShare] = $this->createTestGroupShare();
650
+
651
+        $shares = $this->manager->getOpenShares();
652
+        $this->assertCount(2, $shares);
653
+
654
+        $this->assertTrue($this->manager->acceptShare($groupShare['id']));
655
+
656
+        // user 2 shares
657
+        $manager2 = $this->createManagerForUser('user2');
658
+        $shareData2 = [
659
+            'remote' => 'http://localhost',
660
+            'token' => 'token1',
661
+            'password' => '',
662
+            'name' => '/SharedFolder',
663
+            'owner' => 'foobar',
664
+            'shareType' => IShare::TYPE_USER,
665
+            'accepted' => false,
666
+            'user' => 'user2',
667
+            'remoteId' => '2342'
668
+        ];
669
+
670
+        $this->assertCount(1, $manager2->getOpenShares());
671
+        $this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2));
672
+        $this->assertCount(2, $manager2->getOpenShares());
673
+
674
+        $this->manager->expects($this->never())->method('tryOCMEndPoint');
675
+        $this->manager->removeGroupShares('group1');
676
+
677
+        $user1Shares = $this->manager->getOpenShares();
678
+        // user share is gone, group is still there
679
+        $this->assertCount(1, $user1Shares);
680
+        $this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_USER);
681
+
682
+        // user 2 shares untouched
683
+        $user2Shares = $manager2->getOpenShares();
684
+        $this->assertCount(1, $user2Shares);
685
+        $this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_USER);
686
+        $this->assertEquals($user2Shares[0]['user'], 'user2');
687
+    }
688
+
689
+    /**
690
+     * @param array $expected
691
+     * @param array $actual
692
+     * @param int $share
693
+     * @param string $mountPoint
694
+     */
695
+    protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint, $targetEntity) {
696
+        $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
697
+        $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
698
+        $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
699
+        $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
700
+        $this->assertEquals($expected['accepted'], (int)$actual['accepted'], 'Asserting accept of a share #' . $share);
701
+        $this->assertEquals($targetEntity, $actual['user'], 'Asserting user of a share #' . $share);
702
+        $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
703
+    }
704
+
705
+    private function assertMount($mountPoint) {
706
+        $mountPoint = rtrim($mountPoint, '/');
707
+        $mount = $this->mountManager->find($this->getFullPath($mountPoint));
708
+        $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
709
+        $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
710
+        $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
711
+        $storage = $mount->getStorage();
712
+        $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
713
+    }
714
+
715
+    private function assertNotMount($mountPoint) {
716
+        $mountPoint = rtrim($mountPoint, '/');
717
+        try {
718
+            $mount = $this->mountManager->find($this->getFullPath($mountPoint));
719
+            $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
720
+            $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
721
+        } catch (NotFoundException $e) {
722
+
723
+        }
724
+    }
725
+
726
+    private function getFullPath($path) {
727
+        return '/' . $this->uid . '/files' . $path;
728
+    }
729 729
 }
Please login to merge, or discard this patch.