Completed
Push — master ( f4cb78...6b68ca )
by
unknown
33:09 queued 16s
created
apps/files_sharing/tests/Controller/ShareAPIControllerTest.php 1 patch
Indentation   +5161 added lines, -5161 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,4668 +569,4668 @@  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 testCanAccessShareAsOwner(): void {
1546
-		$share = $this->createMock(IShare::class);
1547
-		$share->method('getShareOwner')->willReturn($this->currentUser);
1548
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1549
-	}
1550
-
1551
-	public function testCanAccessShareAsSharer(): void {
1552
-		$share = $this->createMock(IShare::class);
1553
-		$share->method('getSharedBy')->willReturn($this->currentUser);
1554
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1555
-	}
1556
-
1557
-	public function testCanAccessShareAsSharee(): void {
1558
-		$share = $this->createMock(IShare::class);
1559
-		$share->method('getShareType')->willReturn(IShare::TYPE_USER);
1560
-		$share->method('getSharedWith')->willReturn($this->currentUser);
1561
-		$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1562
-	}
1563
-
1564
-	public function testCannotAccessLinkShare(): void {
1565
-		$share = $this->createMock(IShare::class);
1566
-		$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
1567
-		$share->method('getNodeId')->willReturn(42);
1568
-
1569
-		$userFolder = $this->createMock(Folder::class);
1570
-		$this->rootFolder->method('getUserFolder')
1571
-			->with($this->currentUser)
1572
-			->willReturn($userFolder);
1573
-
1574
-		$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1575
-	}
1576
-
1577
-	/**
1578
-	 * @dataProvider dataCanAccessShareWithPermissions
1579
-	 */
1580
-	public function testCanAccessShareWithPermissions(int $permissions, bool $expected): void {
1581
-		$share = $this->createMock(IShare::class);
1582
-		$share->method('getShareType')->willReturn(IShare::TYPE_USER);
1583
-		$share->method('getSharedWith')->willReturn($this->createMock(IUser::class));
1584
-		$share->method('getNodeId')->willReturn(42);
1585
-
1586
-		$file = $this->createMock(File::class);
1587
-
1588
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1589
-		$userFolder->method('getFirstNodeById')
1590
-			->with($share->getNodeId())
1591
-			->willReturn($file);
1592
-		$userFolder->method('getById')
1593
-			->with($share->getNodeId())
1594
-			->willReturn([$file]);
1595
-		$this->rootFolder->method('getUserFolder')
1596
-			->with($this->currentUser)
1597
-			->willReturn($userFolder);
1598
-
1599
-		$file->method('getPermissions')
1600
-			->willReturn($permissions);
1601
-
1602
-		if ($expected) {
1603
-			$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1604
-		} else {
1605
-			$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1606
-		}
1607
-	}
1608
-
1609
-	public static function dataCanAccessShareWithPermissions(): array {
1610
-		return [
1611
-			[Constants::PERMISSION_SHARE, true],
1612
-			[Constants::PERMISSION_READ, false],
1613
-			[Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, true],
1614
-		];
1615
-	}
1616
-
1617
-	/**
1618
-	 * @dataProvider dataCanAccessShareAsGroupMember
1619
-	 */
1620
-	public function testCanAccessShareAsGroupMember(string $group, bool $expected): void {
1621
-		$share = $this->createMock(IShare::class);
1622
-		$share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1623
-		$share->method('getSharedWith')->willReturn($group);
1624
-		$share->method('getNodeId')->willReturn(42);
1625
-
1626
-		$file = $this->createMock(File::class);
1627
-
1628
-		$userFolder = $this->createMock(Folder::class);
1629
-		$userFolder->method('getFirstNodeById')
1630
-			->with($share->getNodeId())
1631
-			->willReturn($file);
1632
-		$userFolder->method('getById')
1633
-			->with($share->getNodeId())
1634
-			->willReturn([$file]);
1635
-		$this->rootFolder->method('getUserFolder')
1636
-			->with($this->currentUser)
1637
-			->willReturn($userFolder);
1638
-
1639
-		$user = $this->createMock(IUser::class);
1640
-		$this->userManager->method('get')
1641
-			->with($this->currentUser)
1642
-			->willReturn($user);
1643
-
1644
-		$group = $this->createMock(IGroup::class);
1645
-		$group->method('inGroup')->with($user)->willReturn(true);
1646
-		$group2 = $this->createMock(IGroup::class);
1647
-		$group2->method('inGroup')->with($user)->willReturn(false);
1648
-
1649
-		$this->groupManager->method('get')->willReturnMap([
1650
-			['group', $group],
1651
-			['group2', $group2],
1652
-			['group-null', null],
1653
-		]);
1654
-
1655
-		if ($expected) {
1656
-			$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1657
-		} else {
1658
-			$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1659
-		}
1660
-	}
1661
-
1662
-	public static function dataCanAccessShareAsGroupMember(): array {
1663
-		return [
1664
-			['group', true],
1665
-			['group2', false],
1666
-			['group-null', false],
1667
-		];
1668
-	}
1669
-
1670
-	public function dataCanAccessRoomShare() {
1671
-		$result = [];
1672
-
1673
-		$share = $this->createMock(IShare::class);
1674
-		$share->method('getShareType')->willReturn(IShare::TYPE_ROOM);
1675
-		$share->method('getSharedWith')->willReturn('recipientRoom');
1676
-
1677
-		$result[] = [
1678
-			false, $share, false, false
1679
-		];
1680
-
1681
-		$result[] = [
1682
-			false, $share, false, true
1683
-		];
1684
-
1685
-		$result[] = [
1686
-			true, $share, true, true
1687
-		];
1688
-
1689
-		$result[] = [
1690
-			false, $share, true, false
1691
-		];
1692
-
1693
-		return $result;
1694
-	}
1695
-
1696
-	/**
1697
-	 * @dataProvider dataCanAccessRoomShare
1698
-	 *
1699
-	 * @param bool $expects
1700
-	 * @param IShare $share
1701
-	 * @param bool helperAvailable
1702
-	 * @param bool canAccessShareByHelper
1703
-	 */
1704
-	public function testCanAccessRoomShare(bool $expected, IShare $share, bool $helperAvailable, bool $canAccessShareByHelper): void {
1705
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1706
-		$this->rootFolder->method('getUserFolder')
1707
-			->with($this->currentUser)
1708
-			->willReturn($userFolder);
1709
-
1710
-		$userFolder->method('getById')
1711
-			->with($share->getNodeId())
1712
-			->willReturn([$share->getNode()]);
1713
-
1714
-		if (!$helperAvailable) {
1715
-			$this->appManager->method('isEnabledForUser')
1716
-				->with('spreed')
1717
-				->willReturn(false);
1718
-		} else {
1719
-			$this->appManager->method('isEnabledForUser')
1720
-				->with('spreed')
1721
-				->willReturn(true);
1722
-
1723
-			// This is not possible anymore with PHPUnit 10+
1724
-			// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
1725
-			// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
1726
-			$helper = $this->getMockBuilder(\stdClass::class)
1727
-				->addMethods(['canAccessShare'])
1728
-				->getMock();
1729
-			$helper->method('canAccessShare')
1730
-				->with($share, $this->currentUser)
1731
-				->willReturn($canAccessShareByHelper);
1732
-
1733
-			$this->serverContainer->method('get')
1734
-				->with('\OCA\Talk\Share\Helper\ShareAPIController')
1735
-				->willReturn($helper);
1736
-		}
1737
-
1738
-		$this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1739
-	}
1740
-
1741
-
1742
-	public function testCreateShareNoPath(): void {
1743
-		$this->expectException(OCSNotFoundException::class);
1744
-		$this->expectExceptionMessage('Please specify a file or folder path');
1745
-
1746
-		$this->ocs->createShare();
1747
-	}
1748
-
1749
-
1750
-	public function testCreateShareInvalidPath(): void {
1751
-		$this->expectException(OCSNotFoundException::class);
1752
-		$this->expectExceptionMessage('Wrong path, file/folder does not exist');
1753
-
1754
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
1755
-		$this->rootFolder->expects($this->once())
1756
-			->method('getUserFolder')
1757
-			->with('currentUser')
1758
-			->willReturn($userFolder);
1759
-
1760
-		$userFolder->expects($this->once())
1761
-			->method('get')
1762
-			->with('invalid-path')
1763
-			->will($this->throwException(new NotFoundException()));
1764
-
1765
-		$this->ocs->createShare('invalid-path');
1766
-	}
1767
-
1768
-	public function testCreateShareInvalidShareType(): void {
1769
-		$this->expectException(OCSBadRequestException::class);
1770
-		$this->expectExceptionMessage('Unknown share type');
1771
-
1772
-		$share = $this->newShare();
1773
-		$this->shareManager->method('newShare')->willReturn($share);
1774
-
1775
-		[$userFolder, $file] = $this->getNonSharedUserFile();
1776
-		$this->rootFolder->expects($this->atLeastOnce())
1777
-			->method('getUserFolder')
1778
-			->with('currentUser')
1779
-			->willReturn($userFolder);
1780
-
1781
-		$userFolder->expects($this->atLeastOnce())
1782
-			->method('get')
1783
-			->with('valid-path')
1784
-			->willReturn($file);
1785
-		$userFolder->method('getById')
1786
-			->willReturn([]);
1787
-
1788
-		$file->expects($this->once())
1789
-			->method('lock')
1790
-			->with(ILockingProvider::LOCK_SHARED);
1791
-
1792
-		$this->ocs->createShare('valid-path', 31);
1793
-	}
1794
-
1795
-	public function testCreateShareUserNoShareWith(): void {
1796
-		$this->expectException(OCSNotFoundException::class);
1797
-		$this->expectExceptionMessage('Please specify a valid account to share with');
1798
-
1799
-		$share = $this->newShare();
1800
-		$this->shareManager->method('newShare')->willReturn($share);
1801
-
1802
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1803
-		$this->rootFolder->method('getUserFolder')
1804
-			->with('currentUser')
1805
-			->willReturn($userFolder);
1806
-
1807
-		$userFolder->expects($this->once())
1808
-			->method('get')
1809
-			->with('valid-path')
1810
-			->willReturn($path);
1811
-		$userFolder->method('getById')
1812
-			->willReturn([]);
1813
-
1814
-		$path->expects($this->once())
1815
-			->method('lock')
1816
-			->with(ILockingProvider::LOCK_SHARED);
1817
-
1818
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER);
1819
-	}
1820
-
1821
-
1822
-	public function testCreateShareUserNoValidShareWith(): void {
1823
-		$this->expectException(OCSNotFoundException::class);
1824
-		$this->expectExceptionMessage('Please specify a valid account to share with');
1825
-
1826
-		$share = $this->newShare();
1827
-		$this->shareManager->method('newShare')->willReturn($share);
1828
-
1829
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1830
-		$this->rootFolder->method('getUserFolder')
1831
-			->with('currentUser')
1832
-			->willReturn($userFolder);
1833
-
1834
-		$userFolder->expects($this->once())
1835
-			->method('get')
1836
-			->with('valid-path')
1837
-			->willReturn($path);
1838
-		$userFolder->method('getById')
1839
-			->willReturn([]);
1840
-		$path->expects($this->once())
1841
-			->method('lock')
1842
-			->with(ILockingProvider::LOCK_SHARED);
1843
-		$this->userManager->method('userExists')
1844
-			->with('invalidUser')
1845
-			->willReturn(false);
1846
-
1847
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'invalidUser');
1848
-	}
1849
-
1850
-	public function testCreateShareUser(): void {
1851
-		$share = $this->newShare();
1852
-		$this->shareManager->method('newShare')->willReturn($share);
1853
-
1854
-		/** @var ShareAPIController $ocs */
1855
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
1856
-			->setConstructorArgs([
1857
-				$this->appName,
1858
-				$this->request,
1859
-				$this->shareManager,
1860
-				$this->groupManager,
1861
-				$this->userManager,
1862
-				$this->rootFolder,
1863
-				$this->urlGenerator,
1864
-				$this->l,
1865
-				$this->config,
1866
-				$this->appManager,
1867
-				$this->serverContainer,
1868
-				$this->userStatusManager,
1869
-				$this->previewManager,
1870
-				$this->dateTimeZone,
1871
-				$this->logger,
1872
-				$this->factory,
1873
-				$this->mailer,
1874
-				$this->tagManager,
1875
-				$this->currentUser,
1876
-			])->onlyMethods(['formatShare'])
1877
-			->getMock();
1878
-
1879
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1880
-		$this->rootFolder->expects($this->exactly(2))
1881
-			->method('getUserFolder')
1882
-			->with('currentUser')
1883
-			->willReturn($userFolder);
1884
-
1885
-		$userFolder->expects($this->once())
1886
-			->method('get')
1887
-			->with('valid-path')
1888
-			->willReturn($path);
1889
-		$userFolder->method('getById')
1890
-			->willReturn([]);
1891
-
1892
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
1893
-
1894
-		$path->expects($this->once())
1895
-			->method('lock')
1896
-			->with(ILockingProvider::LOCK_SHARED);
1897
-
1898
-		$this->shareManager->method('createShare')
1899
-			->with($this->callback(function (IShare $share) use ($path) {
1900
-				return $share->getNode() === $path &&
1901
-					$share->getPermissions() === (
1902
-						Constants::PERMISSION_ALL &
1903
-						~Constants::PERMISSION_DELETE &
1904
-						~Constants::PERMISSION_CREATE
1905
-					) &&
1906
-					$share->getShareType() === IShare::TYPE_USER &&
1907
-					$share->getSharedWith() === 'validUser' &&
1908
-					$share->getSharedBy() === 'currentUser';
1909
-			}))
1910
-			->willReturnArgument(0);
1911
-
1912
-		$expected = new DataResponse([]);
1913
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
1914
-
1915
-		$this->assertInstanceOf(get_class($expected), $result);
1916
-		$this->assertEquals($expected->getData(), $result->getData());
1917
-	}
1918
-
1919
-
1920
-	public function testCreateShareGroupNoValidShareWith(): void {
1921
-		$this->expectException(OCSNotFoundException::class);
1922
-		$this->expectExceptionMessage('Please specify a valid group');
1923
-
1924
-		$share = $this->newShare();
1925
-		$this->shareManager->method('newShare')->willReturn($share);
1926
-		$this->shareManager->method('createShare')->willReturnArgument(0);
1927
-		$this->shareManager->method('allowGroupSharing')->willReturn(true);
1928
-
1929
-		[$userFolder, $path] = $this->getNonSharedUserFile();
1930
-		$this->rootFolder->method('getUserFolder')
1931
-			->with('currentUser')
1932
-			->willReturn($userFolder);
1933
-
1934
-		$userFolder->expects($this->once())
1935
-			->method('get')
1936
-			->with('valid-path')
1937
-			->willReturn($path);
1938
-		$userFolder->method('getById')
1939
-			->willReturn([]);
1940
-
1941
-		$path->expects($this->once())
1942
-			->method('lock')
1943
-			->with(ILockingProvider::LOCK_SHARED);
1944
-
1945
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
1946
-	}
1947
-
1948
-	public function testCreateShareGroup(): void {
1949
-		$share = $this->newShare();
1950
-		$this->shareManager->method('newShare')->willReturn($share);
1951
-
1952
-		/** @var ShareAPIController&MockObject $ocs */
1953
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
1954
-			->setConstructorArgs([
1955
-				$this->appName,
1956
-				$this->request,
1957
-				$this->shareManager,
1958
-				$this->groupManager,
1959
-				$this->userManager,
1960
-				$this->rootFolder,
1961
-				$this->urlGenerator,
1962
-				$this->l,
1963
-				$this->config,
1964
-				$this->appManager,
1965
-				$this->serverContainer,
1966
-				$this->userStatusManager,
1967
-				$this->previewManager,
1968
-				$this->dateTimeZone,
1969
-				$this->logger,
1970
-				$this->factory,
1971
-				$this->mailer,
1972
-				$this->tagManager,
1973
-				$this->currentUser,
1974
-			])->onlyMethods(['formatShare'])
1975
-			->getMock();
1976
-
1977
-		$this->request
1978
-			->method('getParam')
1979
-			->willReturnMap([
1980
-				['path', null, 'valid-path'],
1981
-				['permissions', null, Constants::PERMISSION_ALL],
1982
-				['shareType', '-1', IShare::TYPE_GROUP],
1983
-				['shareWith', null, 'validGroup'],
1984
-			]);
1985
-
1986
-		[$userFolder, $path] = $this->getNonSharedUserFolder();
1987
-		$this->rootFolder->expects($this->exactly(2))
1988
-			->method('getUserFolder')
1989
-			->with('currentUser')
1990
-			->willReturn($userFolder);
1991
-
1992
-		$userFolder->expects($this->once())
1993
-			->method('get')
1994
-			->with('valid-path')
1995
-			->willReturn($path);
1996
-		$userFolder->method('getById')
1997
-			->willReturn([]);
1998
-
1999
-		$this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
2000
-
2001
-		$this->shareManager->expects($this->once())
2002
-			->method('allowGroupSharing')
2003
-			->willReturn(true);
2004
-
2005
-		$path->expects($this->once())
2006
-			->method('lock')
2007
-			->with(ILockingProvider::LOCK_SHARED);
2008
-
2009
-		$this->shareManager->method('createShare')
2010
-			->with($this->callback(function (IShare $share) use ($path) {
2011
-				return $share->getNode() === $path &&
2012
-				$share->getPermissions() === Constants::PERMISSION_ALL &&
2013
-				$share->getShareType() === IShare::TYPE_GROUP &&
2014
-				$share->getSharedWith() === 'validGroup' &&
2015
-				$share->getSharedBy() === 'currentUser';
2016
-			}))
2017
-			->willReturnArgument(0);
2018
-
2019
-		$expected = new DataResponse([]);
2020
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'validGroup');
2021
-
2022
-		$this->assertInstanceOf(get_class($expected), $result);
2023
-		$this->assertEquals($expected->getData(), $result->getData());
2024
-	}
2025
-
2026
-
2027
-	public function testCreateShareGroupNotAllowed(): void {
2028
-		$this->expectException(OCSNotFoundException::class);
2029
-		$this->expectExceptionMessage('Group sharing is disabled by the administrator');
2030
-
2031
-		$share = $this->newShare();
2032
-		$this->shareManager->method('newShare')->willReturn($share);
2033
-
2034
-		[$userFolder, $path] = $this->getNonSharedUserFolder();
2035
-		$this->rootFolder->method('getUserFolder')
2036
-			->with('currentUser')
2037
-			->willReturn($userFolder);
2038
-
2039
-		$userFolder->expects($this->once())
2040
-			->method('get')
2041
-			->with('valid-path')
2042
-			->willReturn($path);
2043
-		$userFolder->method('getById')
2044
-			->willReturn([]);
2045
-
2046
-		$this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
2047
-
2048
-		$this->shareManager->expects($this->once())
2049
-			->method('allowGroupSharing')
2050
-			->willReturn(false);
2051
-
2052
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
2053
-	}
2054
-
2055
-
2056
-	public function testCreateShareLinkNoLinksAllowed(): void {
2057
-		$this->expectException(OCSNotFoundException::class);
2058
-		$this->expectExceptionMessage('Public link sharing is disabled by the administrator');
2059
-
2060
-		$this->request
2061
-			->method('getParam')
2062
-			->willReturnMap([
2063
-				['path', null, 'valid-path'],
2064
-				['shareType', '-1', IShare::TYPE_LINK],
2065
-			]);
2066
-
2067
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2068
-		$path->method('getId')->willReturn(42);
2069
-		$storage = $this->createMock(IStorage::class);
2070
-		$storage->method('instanceOfStorage')
2071
-			->willReturnMap([
2072
-				['OCA\Files_Sharing\External\Storage', false],
2073
-				['OCA\Files_Sharing\SharedStorage', false],
2074
-			]);
2075
-		$path->method('getStorage')->willReturn($storage);
2076
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2077
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2078
-		$this->rootFolder->method('getById')
2079
-			->willReturn([]);
2080
-
2081
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2082
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2083
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(false);
2084
-
2085
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK);
2086
-	}
2087
-
2088
-
2089
-	public function testCreateShareLinkNoPublicUpload(): void {
2090
-		$this->expectException(OCSForbiddenException::class);
2091
-		$this->expectExceptionMessage('Public upload disabled by the administrator');
2092
-
2093
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2094
-		$path->method('getId')->willReturn(42);
2095
-		$storage = $this->createMock(IStorage::class);
2096
-		$storage->method('instanceOfStorage')
2097
-			->willReturnMap([
2098
-				['OCA\Files_Sharing\External\Storage', false],
2099
-				['OCA\Files_Sharing\SharedStorage', false],
2100
-			]);
2101
-		$path->method('getStorage')->willReturn($storage);
2102
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2103
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2104
-		$this->rootFolder->method('getById')
2105
-			->willReturn([]);
2106
-
2107
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2108
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2109
-
2110
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2111
-	}
2112
-
2113
-
2114
-	public function testCreateShareLinkPublicUploadFile(): void {
2115
-		$this->expectException(OCSBadRequestException::class);
2116
-		$this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
2117
-
2118
-		$storage = $this->createMock(IStorage::class);
2119
-		$storage->method('instanceOfStorage')
2120
-			->willReturnMap([
2121
-				['OCA\Files_Sharing\External\Storage', false],
2122
-				['OCA\Files_Sharing\SharedStorage', false],
2123
-			]);
2124
-
2125
-		$file = $this->createMock(File::class);
2126
-		$file->method('getId')->willReturn(42);
2127
-		$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 testCanAccessShareAsOwner(): void {
1546
+        $share = $this->createMock(IShare::class);
1547
+        $share->method('getShareOwner')->willReturn($this->currentUser);
1548
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1549
+    }
1550
+
1551
+    public function testCanAccessShareAsSharer(): void {
1552
+        $share = $this->createMock(IShare::class);
1553
+        $share->method('getSharedBy')->willReturn($this->currentUser);
1554
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1555
+    }
1556
+
1557
+    public function testCanAccessShareAsSharee(): void {
1558
+        $share = $this->createMock(IShare::class);
1559
+        $share->method('getShareType')->willReturn(IShare::TYPE_USER);
1560
+        $share->method('getSharedWith')->willReturn($this->currentUser);
1561
+        $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1562
+    }
1563
+
1564
+    public function testCannotAccessLinkShare(): void {
1565
+        $share = $this->createMock(IShare::class);
1566
+        $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
1567
+        $share->method('getNodeId')->willReturn(42);
1568
+
1569
+        $userFolder = $this->createMock(Folder::class);
1570
+        $this->rootFolder->method('getUserFolder')
1571
+            ->with($this->currentUser)
1572
+            ->willReturn($userFolder);
1573
+
1574
+        $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1575
+    }
1576
+
1577
+    /**
1578
+     * @dataProvider dataCanAccessShareWithPermissions
1579
+     */
1580
+    public function testCanAccessShareWithPermissions(int $permissions, bool $expected): void {
1581
+        $share = $this->createMock(IShare::class);
1582
+        $share->method('getShareType')->willReturn(IShare::TYPE_USER);
1583
+        $share->method('getSharedWith')->willReturn($this->createMock(IUser::class));
1584
+        $share->method('getNodeId')->willReturn(42);
1585
+
1586
+        $file = $this->createMock(File::class);
1587
+
1588
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1589
+        $userFolder->method('getFirstNodeById')
1590
+            ->with($share->getNodeId())
1591
+            ->willReturn($file);
1592
+        $userFolder->method('getById')
1593
+            ->with($share->getNodeId())
1594
+            ->willReturn([$file]);
1595
+        $this->rootFolder->method('getUserFolder')
1596
+            ->with($this->currentUser)
1597
+            ->willReturn($userFolder);
1598
+
1599
+        $file->method('getPermissions')
1600
+            ->willReturn($permissions);
1601
+
1602
+        if ($expected) {
1603
+            $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1604
+        } else {
1605
+            $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1606
+        }
1607
+    }
1608
+
1609
+    public static function dataCanAccessShareWithPermissions(): array {
1610
+        return [
1611
+            [Constants::PERMISSION_SHARE, true],
1612
+            [Constants::PERMISSION_READ, false],
1613
+            [Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, true],
1614
+        ];
1615
+    }
1616
+
1617
+    /**
1618
+     * @dataProvider dataCanAccessShareAsGroupMember
1619
+     */
1620
+    public function testCanAccessShareAsGroupMember(string $group, bool $expected): void {
1621
+        $share = $this->createMock(IShare::class);
1622
+        $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
1623
+        $share->method('getSharedWith')->willReturn($group);
1624
+        $share->method('getNodeId')->willReturn(42);
1625
+
1626
+        $file = $this->createMock(File::class);
1627
+
1628
+        $userFolder = $this->createMock(Folder::class);
1629
+        $userFolder->method('getFirstNodeById')
1630
+            ->with($share->getNodeId())
1631
+            ->willReturn($file);
1632
+        $userFolder->method('getById')
1633
+            ->with($share->getNodeId())
1634
+            ->willReturn([$file]);
1635
+        $this->rootFolder->method('getUserFolder')
1636
+            ->with($this->currentUser)
1637
+            ->willReturn($userFolder);
1638
+
1639
+        $user = $this->createMock(IUser::class);
1640
+        $this->userManager->method('get')
1641
+            ->with($this->currentUser)
1642
+            ->willReturn($user);
1643
+
1644
+        $group = $this->createMock(IGroup::class);
1645
+        $group->method('inGroup')->with($user)->willReturn(true);
1646
+        $group2 = $this->createMock(IGroup::class);
1647
+        $group2->method('inGroup')->with($user)->willReturn(false);
1648
+
1649
+        $this->groupManager->method('get')->willReturnMap([
1650
+            ['group', $group],
1651
+            ['group2', $group2],
1652
+            ['group-null', null],
1653
+        ]);
1654
+
1655
+        if ($expected) {
1656
+            $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1657
+        } else {
1658
+            $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1659
+        }
1660
+    }
1661
+
1662
+    public static function dataCanAccessShareAsGroupMember(): array {
1663
+        return [
1664
+            ['group', true],
1665
+            ['group2', false],
1666
+            ['group-null', false],
1667
+        ];
1668
+    }
1669
+
1670
+    public function dataCanAccessRoomShare() {
1671
+        $result = [];
1672
+
1673
+        $share = $this->createMock(IShare::class);
1674
+        $share->method('getShareType')->willReturn(IShare::TYPE_ROOM);
1675
+        $share->method('getSharedWith')->willReturn('recipientRoom');
1676
+
1677
+        $result[] = [
1678
+            false, $share, false, false
1679
+        ];
1680
+
1681
+        $result[] = [
1682
+            false, $share, false, true
1683
+        ];
1684
+
1685
+        $result[] = [
1686
+            true, $share, true, true
1687
+        ];
1688
+
1689
+        $result[] = [
1690
+            false, $share, true, false
1691
+        ];
1692
+
1693
+        return $result;
1694
+    }
1695
+
1696
+    /**
1697
+     * @dataProvider dataCanAccessRoomShare
1698
+     *
1699
+     * @param bool $expects
1700
+     * @param IShare $share
1701
+     * @param bool helperAvailable
1702
+     * @param bool canAccessShareByHelper
1703
+     */
1704
+    public function testCanAccessRoomShare(bool $expected, IShare $share, bool $helperAvailable, bool $canAccessShareByHelper): void {
1705
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1706
+        $this->rootFolder->method('getUserFolder')
1707
+            ->with($this->currentUser)
1708
+            ->willReturn($userFolder);
1709
+
1710
+        $userFolder->method('getById')
1711
+            ->with($share->getNodeId())
1712
+            ->willReturn([$share->getNode()]);
1713
+
1714
+        if (!$helperAvailable) {
1715
+            $this->appManager->method('isEnabledForUser')
1716
+                ->with('spreed')
1717
+                ->willReturn(false);
1718
+        } else {
1719
+            $this->appManager->method('isEnabledForUser')
1720
+                ->with('spreed')
1721
+                ->willReturn(true);
1722
+
1723
+            // This is not possible anymore with PHPUnit 10+
1724
+            // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
1725
+            // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
1726
+            $helper = $this->getMockBuilder(\stdClass::class)
1727
+                ->addMethods(['canAccessShare'])
1728
+                ->getMock();
1729
+            $helper->method('canAccessShare')
1730
+                ->with($share, $this->currentUser)
1731
+                ->willReturn($canAccessShareByHelper);
1732
+
1733
+            $this->serverContainer->method('get')
1734
+                ->with('\OCA\Talk\Share\Helper\ShareAPIController')
1735
+                ->willReturn($helper);
1736
+        }
1737
+
1738
+        $this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
1739
+    }
1740
+
1741
+
1742
+    public function testCreateShareNoPath(): void {
1743
+        $this->expectException(OCSNotFoundException::class);
1744
+        $this->expectExceptionMessage('Please specify a file or folder path');
1745
+
1746
+        $this->ocs->createShare();
1747
+    }
1748
+
1749
+
1750
+    public function testCreateShareInvalidPath(): void {
1751
+        $this->expectException(OCSNotFoundException::class);
1752
+        $this->expectExceptionMessage('Wrong path, file/folder does not exist');
1753
+
1754
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
1755
+        $this->rootFolder->expects($this->once())
1756
+            ->method('getUserFolder')
1757
+            ->with('currentUser')
1758
+            ->willReturn($userFolder);
1759
+
1760
+        $userFolder->expects($this->once())
1761
+            ->method('get')
1762
+            ->with('invalid-path')
1763
+            ->will($this->throwException(new NotFoundException()));
1764
+
1765
+        $this->ocs->createShare('invalid-path');
1766
+    }
1767
+
1768
+    public function testCreateShareInvalidShareType(): void {
1769
+        $this->expectException(OCSBadRequestException::class);
1770
+        $this->expectExceptionMessage('Unknown share type');
1771
+
1772
+        $share = $this->newShare();
1773
+        $this->shareManager->method('newShare')->willReturn($share);
1774
+
1775
+        [$userFolder, $file] = $this->getNonSharedUserFile();
1776
+        $this->rootFolder->expects($this->atLeastOnce())
1777
+            ->method('getUserFolder')
1778
+            ->with('currentUser')
1779
+            ->willReturn($userFolder);
1780
+
1781
+        $userFolder->expects($this->atLeastOnce())
1782
+            ->method('get')
1783
+            ->with('valid-path')
1784
+            ->willReturn($file);
1785
+        $userFolder->method('getById')
1786
+            ->willReturn([]);
1787
+
1788
+        $file->expects($this->once())
1789
+            ->method('lock')
1790
+            ->with(ILockingProvider::LOCK_SHARED);
1791
+
1792
+        $this->ocs->createShare('valid-path', 31);
1793
+    }
1794
+
1795
+    public function testCreateShareUserNoShareWith(): void {
1796
+        $this->expectException(OCSNotFoundException::class);
1797
+        $this->expectExceptionMessage('Please specify a valid account to share with');
1798
+
1799
+        $share = $this->newShare();
1800
+        $this->shareManager->method('newShare')->willReturn($share);
1801
+
1802
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1803
+        $this->rootFolder->method('getUserFolder')
1804
+            ->with('currentUser')
1805
+            ->willReturn($userFolder);
1806
+
1807
+        $userFolder->expects($this->once())
1808
+            ->method('get')
1809
+            ->with('valid-path')
1810
+            ->willReturn($path);
1811
+        $userFolder->method('getById')
1812
+            ->willReturn([]);
1813
+
1814
+        $path->expects($this->once())
1815
+            ->method('lock')
1816
+            ->with(ILockingProvider::LOCK_SHARED);
1817
+
1818
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER);
1819
+    }
1820
+
1821
+
1822
+    public function testCreateShareUserNoValidShareWith(): void {
1823
+        $this->expectException(OCSNotFoundException::class);
1824
+        $this->expectExceptionMessage('Please specify a valid account to share with');
1825
+
1826
+        $share = $this->newShare();
1827
+        $this->shareManager->method('newShare')->willReturn($share);
1828
+
1829
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1830
+        $this->rootFolder->method('getUserFolder')
1831
+            ->with('currentUser')
1832
+            ->willReturn($userFolder);
1833
+
1834
+        $userFolder->expects($this->once())
1835
+            ->method('get')
1836
+            ->with('valid-path')
1837
+            ->willReturn($path);
1838
+        $userFolder->method('getById')
1839
+            ->willReturn([]);
1840
+        $path->expects($this->once())
1841
+            ->method('lock')
1842
+            ->with(ILockingProvider::LOCK_SHARED);
1843
+        $this->userManager->method('userExists')
1844
+            ->with('invalidUser')
1845
+            ->willReturn(false);
1846
+
1847
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'invalidUser');
1848
+    }
1849
+
1850
+    public function testCreateShareUser(): void {
1851
+        $share = $this->newShare();
1852
+        $this->shareManager->method('newShare')->willReturn($share);
1853
+
1854
+        /** @var ShareAPIController $ocs */
1855
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
1856
+            ->setConstructorArgs([
1857
+                $this->appName,
1858
+                $this->request,
1859
+                $this->shareManager,
1860
+                $this->groupManager,
1861
+                $this->userManager,
1862
+                $this->rootFolder,
1863
+                $this->urlGenerator,
1864
+                $this->l,
1865
+                $this->config,
1866
+                $this->appManager,
1867
+                $this->serverContainer,
1868
+                $this->userStatusManager,
1869
+                $this->previewManager,
1870
+                $this->dateTimeZone,
1871
+                $this->logger,
1872
+                $this->factory,
1873
+                $this->mailer,
1874
+                $this->tagManager,
1875
+                $this->currentUser,
1876
+            ])->onlyMethods(['formatShare'])
1877
+            ->getMock();
1878
+
1879
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1880
+        $this->rootFolder->expects($this->exactly(2))
1881
+            ->method('getUserFolder')
1882
+            ->with('currentUser')
1883
+            ->willReturn($userFolder);
1884
+
1885
+        $userFolder->expects($this->once())
1886
+            ->method('get')
1887
+            ->with('valid-path')
1888
+            ->willReturn($path);
1889
+        $userFolder->method('getById')
1890
+            ->willReturn([]);
1891
+
1892
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
1893
+
1894
+        $path->expects($this->once())
1895
+            ->method('lock')
1896
+            ->with(ILockingProvider::LOCK_SHARED);
1897
+
1898
+        $this->shareManager->method('createShare')
1899
+            ->with($this->callback(function (IShare $share) use ($path) {
1900
+                return $share->getNode() === $path &&
1901
+                    $share->getPermissions() === (
1902
+                        Constants::PERMISSION_ALL &
1903
+                        ~Constants::PERMISSION_DELETE &
1904
+                        ~Constants::PERMISSION_CREATE
1905
+                    ) &&
1906
+                    $share->getShareType() === IShare::TYPE_USER &&
1907
+                    $share->getSharedWith() === 'validUser' &&
1908
+                    $share->getSharedBy() === 'currentUser';
1909
+            }))
1910
+            ->willReturnArgument(0);
1911
+
1912
+        $expected = new DataResponse([]);
1913
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
1914
+
1915
+        $this->assertInstanceOf(get_class($expected), $result);
1916
+        $this->assertEquals($expected->getData(), $result->getData());
1917
+    }
1918
+
1919
+
1920
+    public function testCreateShareGroupNoValidShareWith(): void {
1921
+        $this->expectException(OCSNotFoundException::class);
1922
+        $this->expectExceptionMessage('Please specify a valid group');
1923
+
1924
+        $share = $this->newShare();
1925
+        $this->shareManager->method('newShare')->willReturn($share);
1926
+        $this->shareManager->method('createShare')->willReturnArgument(0);
1927
+        $this->shareManager->method('allowGroupSharing')->willReturn(true);
1928
+
1929
+        [$userFolder, $path] = $this->getNonSharedUserFile();
1930
+        $this->rootFolder->method('getUserFolder')
1931
+            ->with('currentUser')
1932
+            ->willReturn($userFolder);
1933
+
1934
+        $userFolder->expects($this->once())
1935
+            ->method('get')
1936
+            ->with('valid-path')
1937
+            ->willReturn($path);
1938
+        $userFolder->method('getById')
1939
+            ->willReturn([]);
1940
+
1941
+        $path->expects($this->once())
1942
+            ->method('lock')
1943
+            ->with(ILockingProvider::LOCK_SHARED);
1944
+
1945
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
1946
+    }
1947
+
1948
+    public function testCreateShareGroup(): void {
1949
+        $share = $this->newShare();
1950
+        $this->shareManager->method('newShare')->willReturn($share);
1951
+
1952
+        /** @var ShareAPIController&MockObject $ocs */
1953
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
1954
+            ->setConstructorArgs([
1955
+                $this->appName,
1956
+                $this->request,
1957
+                $this->shareManager,
1958
+                $this->groupManager,
1959
+                $this->userManager,
1960
+                $this->rootFolder,
1961
+                $this->urlGenerator,
1962
+                $this->l,
1963
+                $this->config,
1964
+                $this->appManager,
1965
+                $this->serverContainer,
1966
+                $this->userStatusManager,
1967
+                $this->previewManager,
1968
+                $this->dateTimeZone,
1969
+                $this->logger,
1970
+                $this->factory,
1971
+                $this->mailer,
1972
+                $this->tagManager,
1973
+                $this->currentUser,
1974
+            ])->onlyMethods(['formatShare'])
1975
+            ->getMock();
1976
+
1977
+        $this->request
1978
+            ->method('getParam')
1979
+            ->willReturnMap([
1980
+                ['path', null, 'valid-path'],
1981
+                ['permissions', null, Constants::PERMISSION_ALL],
1982
+                ['shareType', '-1', IShare::TYPE_GROUP],
1983
+                ['shareWith', null, 'validGroup'],
1984
+            ]);
1985
+
1986
+        [$userFolder, $path] = $this->getNonSharedUserFolder();
1987
+        $this->rootFolder->expects($this->exactly(2))
1988
+            ->method('getUserFolder')
1989
+            ->with('currentUser')
1990
+            ->willReturn($userFolder);
1991
+
1992
+        $userFolder->expects($this->once())
1993
+            ->method('get')
1994
+            ->with('valid-path')
1995
+            ->willReturn($path);
1996
+        $userFolder->method('getById')
1997
+            ->willReturn([]);
1998
+
1999
+        $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
2000
+
2001
+        $this->shareManager->expects($this->once())
2002
+            ->method('allowGroupSharing')
2003
+            ->willReturn(true);
2004
+
2005
+        $path->expects($this->once())
2006
+            ->method('lock')
2007
+            ->with(ILockingProvider::LOCK_SHARED);
2008
+
2009
+        $this->shareManager->method('createShare')
2010
+            ->with($this->callback(function (IShare $share) use ($path) {
2011
+                return $share->getNode() === $path &&
2012
+                $share->getPermissions() === Constants::PERMISSION_ALL &&
2013
+                $share->getShareType() === IShare::TYPE_GROUP &&
2014
+                $share->getSharedWith() === 'validGroup' &&
2015
+                $share->getSharedBy() === 'currentUser';
2016
+            }))
2017
+            ->willReturnArgument(0);
2018
+
2019
+        $expected = new DataResponse([]);
2020
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'validGroup');
2021
+
2022
+        $this->assertInstanceOf(get_class($expected), $result);
2023
+        $this->assertEquals($expected->getData(), $result->getData());
2024
+    }
2025
+
2026
+
2027
+    public function testCreateShareGroupNotAllowed(): void {
2028
+        $this->expectException(OCSNotFoundException::class);
2029
+        $this->expectExceptionMessage('Group sharing is disabled by the administrator');
2030
+
2031
+        $share = $this->newShare();
2032
+        $this->shareManager->method('newShare')->willReturn($share);
2033
+
2034
+        [$userFolder, $path] = $this->getNonSharedUserFolder();
2035
+        $this->rootFolder->method('getUserFolder')
2036
+            ->with('currentUser')
2037
+            ->willReturn($userFolder);
2038
+
2039
+        $userFolder->expects($this->once())
2040
+            ->method('get')
2041
+            ->with('valid-path')
2042
+            ->willReturn($path);
2043
+        $userFolder->method('getById')
2044
+            ->willReturn([]);
2045
+
2046
+        $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
2047
+
2048
+        $this->shareManager->expects($this->once())
2049
+            ->method('allowGroupSharing')
2050
+            ->willReturn(false);
2051
+
2052
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
2053
+    }
2054
+
2055
+
2056
+    public function testCreateShareLinkNoLinksAllowed(): void {
2057
+        $this->expectException(OCSNotFoundException::class);
2058
+        $this->expectExceptionMessage('Public link sharing is disabled by the administrator');
2059
+
2060
+        $this->request
2061
+            ->method('getParam')
2062
+            ->willReturnMap([
2063
+                ['path', null, 'valid-path'],
2064
+                ['shareType', '-1', IShare::TYPE_LINK],
2065
+            ]);
2066
+
2067
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2068
+        $path->method('getId')->willReturn(42);
2069
+        $storage = $this->createMock(IStorage::class);
2070
+        $storage->method('instanceOfStorage')
2071
+            ->willReturnMap([
2072
+                ['OCA\Files_Sharing\External\Storage', false],
2073
+                ['OCA\Files_Sharing\SharedStorage', false],
2074
+            ]);
2075
+        $path->method('getStorage')->willReturn($storage);
2076
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2077
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2078
+        $this->rootFolder->method('getById')
2079
+            ->willReturn([]);
2080
+
2081
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2082
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2083
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(false);
2084
+
2085
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK);
2086
+    }
2087
+
2088
+
2089
+    public function testCreateShareLinkNoPublicUpload(): void {
2090
+        $this->expectException(OCSForbiddenException::class);
2091
+        $this->expectExceptionMessage('Public upload disabled by the administrator');
2092
+
2093
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2094
+        $path->method('getId')->willReturn(42);
2095
+        $storage = $this->createMock(IStorage::class);
2096
+        $storage->method('instanceOfStorage')
2097
+            ->willReturnMap([
2098
+                ['OCA\Files_Sharing\External\Storage', false],
2099
+                ['OCA\Files_Sharing\SharedStorage', false],
2100
+            ]);
2101
+        $path->method('getStorage')->willReturn($storage);
2102
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2103
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2104
+        $this->rootFolder->method('getById')
2105
+            ->willReturn([]);
2106
+
2107
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2108
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2109
+
2110
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2111
+    }
2112
+
2113
+
2114
+    public function testCreateShareLinkPublicUploadFile(): void {
2115
+        $this->expectException(OCSBadRequestException::class);
2116
+        $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
2117
+
2118
+        $storage = $this->createMock(IStorage::class);
2119
+        $storage->method('instanceOfStorage')
2120
+            ->willReturnMap([
2121
+                ['OCA\Files_Sharing\External\Storage', false],
2122
+                ['OCA\Files_Sharing\SharedStorage', false],
2123
+            ]);
2124
+
2125
+        $file = $this->createMock(File::class);
2126
+        $file->method('getId')->willReturn(42);
2127
+        $file->method('getStorage')->willReturn($storage);
2128 2128
 	
2129
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2130
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($file);
2131
-		$this->rootFolder->method('getById')
2132
-			->willReturn([]);
2133
-
2134
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2135
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2136
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2137
-
2138
-		$this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2139
-	}
2140
-
2141
-	public function testCreateShareLinkPublicUploadFolder(): void {
2142
-		$ocs = $this->mockFormatShare();
2143
-
2144
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2145
-		$path->method('getId')->willReturn(1);
2146
-		$storage = $this->createMock(IStorage::class);
2147
-		$storage->method('instanceOfStorage')
2148
-			->willReturnMap([
2149
-				['OCA\Files_Sharing\External\Storage', false],
2150
-				['OCA\Files_Sharing\SharedStorage', false],
2151
-			]);
2152
-		$path->method('getStorage')->willReturn($storage);
2153
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2154
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2155
-		$this->rootFolder->method('getById')
2156
-			->willReturn([]);
2157
-
2158
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2159
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2160
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2161
-
2162
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2163
-			$this->callback(function (IShare $share) use ($path) {
2164
-				return $share->getNode() === $path &&
2165
-					$share->getShareType() === IShare::TYPE_LINK &&
2166
-					$share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2167
-					$share->getSharedBy() === 'currentUser' &&
2168
-					$share->getPassword() === null &&
2169
-					$share->getExpirationDate() === null;
2170
-			})
2171
-		)->willReturnArgument(0);
2172
-
2173
-		$expected = new DataResponse([]);
2174
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', '', null, '');
2175
-
2176
-		$this->assertInstanceOf(get_class($expected), $result);
2177
-		$this->assertEquals($expected->getData(), $result->getData());
2178
-	}
2179
-
2180
-	public function testCreateShareLinkPassword(): void {
2181
-		$ocs = $this->mockFormatShare();
2182
-
2183
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2184
-		$path->method('getId')->willReturn(42);
2185
-		$storage = $this->createMock(IStorage::class);
2186
-		$storage->method('instanceOfStorage')
2187
-			->willReturnMap([
2188
-				['OCA\Files_Sharing\External\Storage', false],
2189
-				['OCA\Files_Sharing\SharedStorage', false],
2190
-			]);
2191
-		$path->method('getStorage')->willReturn($storage);
2192
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2193
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2194
-		$this->rootFolder->method('getById')
2195
-			->willReturn([]);
2196
-
2197
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2198
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2199
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2200
-
2201
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2202
-			$this->callback(function (IShare $share) use ($path) {
2203
-				return $share->getNode() === $path
2204
-				&& $share->getShareType() === IShare::TYPE_LINK
2205
-				&& $share->getPermissions() === Constants::PERMISSION_READ // publicUpload was set to false
2206
-				&& $share->getSharedBy() === 'currentUser'
2207
-				&& $share->getPassword() === 'password'
2208
-				&& $share->getExpirationDate() === null;
2209
-			})
2210
-		)->willReturnArgument(0);
2211
-
2212
-		$expected = new DataResponse([]);
2213
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_READ, IShare::TYPE_LINK, null, 'false', 'password', null, '');
2214
-
2215
-		$this->assertInstanceOf(get_class($expected), $result);
2216
-		$this->assertEquals($expected->getData(), $result->getData());
2217
-	}
2218
-
2219
-	public function testCreateShareLinkSendPasswordByTalk(): void {
2220
-		$ocs = $this->mockFormatShare();
2221
-
2222
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2223
-		$path->method('getId')->willReturn(42);
2224
-		$storage = $this->createMock(IStorage::class);
2225
-		$storage->method('instanceOfStorage')
2226
-			->willReturnMap([
2227
-				['OCA\Files_Sharing\External\Storage', false],
2228
-				['OCA\Files_Sharing\SharedStorage', false],
2229
-			]);
2230
-		$path->method('getStorage')->willReturn($storage);
2231
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2232
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2233
-		$this->rootFolder->method('getById')
2234
-			->willReturn([]);
2235
-
2236
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2237
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2238
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2239
-
2240
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
2241
-
2242
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2243
-			$this->callback(function (IShare $share) use ($path) {
2244
-				return $share->getNode() === $path &&
2245
-				$share->getShareType() === IShare::TYPE_LINK &&
2246
-				$share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) &&
2247
-				$share->getSharedBy() === 'currentUser' &&
2248
-				$share->getPassword() === 'password' &&
2249
-				$share->getSendPasswordByTalk() === true &&
2250
-				$share->getExpirationDate() === null;
2251
-			})
2252
-		)->willReturnArgument(0);
2253
-
2254
-		$expected = new DataResponse([]);
2255
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', 'password', 'true', '');
2256
-
2257
-		$this->assertInstanceOf(get_class($expected), $result);
2258
-		$this->assertEquals($expected->getData(), $result->getData());
2259
-	}
2260
-
2261
-
2262
-	public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled(): void {
2263
-		$this->expectException(OCSForbiddenException::class);
2264
-		$this->expectExceptionMessage('Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled');
2265
-
2266
-		$ocs = $this->mockFormatShare();
2267
-
2268
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2269
-		$path->method('getId')->willReturn(42);
2270
-		$storage = $this->createMock(IStorage::class);
2271
-		$storage->method('instanceOfStorage')
2272
-			->willReturnMap([
2273
-				['OCA\Files_Sharing\External\Storage', false],
2274
-				['OCA\Files_Sharing\SharedStorage', false],
2275
-			]);
2276
-		$path->method('getStorage')->willReturn($storage);
2277
-		$path->method('getPath')->willReturn('valid-path');
2278
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2279
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2280
-		$this->rootFolder->method('getById')
2281
-			->willReturn([]);
2282
-
2283
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2284
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2285
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2286
-
2287
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
2288
-
2289
-		$this->shareManager->expects($this->never())->method('createShare');
2290
-
2291
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', '');
2292
-	}
2293
-
2294
-	public function testCreateShareValidExpireDate(): void {
2295
-		$ocs = $this->mockFormatShare();
2296
-
2297
-		$this->request
2298
-			->method('getParam')
2299
-			->willReturnMap([
2300
-				['path', null, 'valid-path'],
2301
-				['shareType', '-1', IShare::TYPE_LINK],
2302
-				['publicUpload', null, 'false'],
2303
-				['expireDate', '', '2000-01-01'],
2304
-				['password', '', ''],
2305
-			]);
2306
-
2307
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2308
-		$path->method('getId')->willReturn(42);
2309
-		$storage = $this->createMock(IStorage::class);
2310
-		$storage->method('instanceOfStorage')
2311
-			->willReturnMap([
2312
-				['OCA\Files_Sharing\External\Storage', false],
2313
-				['OCA\Files_Sharing\SharedStorage', false],
2314
-			]);
2315
-		$path->method('getStorage')->willReturn($storage);
2316
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2317
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2318
-		$this->rootFolder->method('getById')
2319
-			->willReturn([]);
2320
-
2321
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2322
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2323
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2324
-
2325
-		$this->shareManager->expects($this->once())->method('createShare')->with(
2326
-			$this->callback(function (IShare $share) use ($path) {
2327
-				$date = new \DateTime('2000-01-01');
2328
-				$date->setTime(0, 0, 0);
2329
-
2330
-				return $share->getNode() === $path &&
2331
-				$share->getShareType() === IShare::TYPE_LINK &&
2332
-				$share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE &&
2333
-				$share->getSharedBy() === 'currentUser' &&
2334
-				$share->getPassword() === null &&
2335
-				$share->getExpirationDate() == $date;
2336
-			})
2337
-		)->willReturnArgument(0);
2338
-
2339
-		$expected = new DataResponse([]);
2340
-		$result = $ocs->createShare('valid-path', null, IShare::TYPE_LINK, null, 'false', '', null, '2000-01-01');
2341
-
2342
-		$this->assertInstanceOf(get_class($expected), $result);
2343
-		$this->assertEquals($expected->getData(), $result->getData());
2344
-	}
2345
-
2346
-
2347
-	public function testCreateShareInvalidExpireDate(): void {
2348
-		$this->expectException(OCSNotFoundException::class);
2349
-		$this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
2350
-
2351
-		$ocs = $this->mockFormatShare();
2352
-
2353
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2354
-		$path->method('getId')->willReturn(42);
2355
-		$storage = $this->createMock(IStorage::class);
2356
-		$storage->method('instanceOfStorage')
2357
-			->willReturnMap([
2358
-				['OCA\Files_Sharing\External\Storage', false],
2359
-				['OCA\Files_Sharing\SharedStorage', false],
2360
-			]);
2361
-		$path->method('getStorage')->willReturn($storage);
2362
-		$this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2363
-		$this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2364
-		$this->rootFolder->method('getById')
2365
-			->willReturn([]);
2366
-
2367
-		$this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2368
-		$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2369
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2370
-
2371
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3');
2372
-	}
2373
-
2374
-	public function testCreateShareRemote(): void {
2375
-		$share = $this->newShare();
2376
-		$this->shareManager->method('newShare')->willReturn($share);
2377
-
2378
-		/** @var ShareAPIController $ocs */
2379
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
2380
-			->setConstructorArgs([
2381
-				$this->appName,
2382
-				$this->request,
2383
-				$this->shareManager,
2384
-				$this->groupManager,
2385
-				$this->userManager,
2386
-				$this->rootFolder,
2387
-				$this->urlGenerator,
2388
-				$this->l,
2389
-				$this->config,
2390
-				$this->appManager,
2391
-				$this->serverContainer,
2392
-				$this->userStatusManager,
2393
-				$this->previewManager,
2394
-				$this->dateTimeZone,
2395
-				$this->logger,
2396
-				$this->factory,
2397
-				$this->mailer,
2398
-				$this->tagManager,
2399
-				$this->currentUser,
2400
-			])->onlyMethods(['formatShare'])
2401
-			->getMock();
2402
-
2403
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2404
-		$this->rootFolder->expects($this->exactly(2))
2405
-			->method('getUserFolder')
2406
-			->with('currentUser')
2407
-			->willReturn($userFolder);
2408
-
2409
-		$userFolder->expects($this->once())
2410
-			->method('get')
2411
-			->with('valid-path')
2412
-			->willReturn($path);
2413
-		$userFolder->method('getById')
2414
-			->willReturn([]);
2415
-
2416
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
2417
-
2418
-		$path->expects($this->once())
2419
-			->method('lock')
2420
-			->with(ILockingProvider::LOCK_SHARED);
2421
-
2422
-		$this->shareManager->method('createShare')
2423
-			->with($this->callback(function (IShare $share) use ($path) {
2424
-				return $share->getNode() === $path &&
2425
-					$share->getPermissions() === (
2426
-						Constants::PERMISSION_ALL &
2427
-						~Constants::PERMISSION_DELETE &
2428
-						~Constants::PERMISSION_CREATE
2429
-					) &&
2430
-					$share->getShareType() === IShare::TYPE_REMOTE &&
2431
-					$share->getSharedWith() === '[email protected]' &&
2432
-					$share->getSharedBy() === 'currentUser';
2433
-			}))
2434
-			->willReturnArgument(0);
2435
-
2436
-		$this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
2437
-
2438
-		$expected = new DataResponse([]);
2439
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, '[email protected]');
2440
-
2441
-		$this->assertInstanceOf(get_class($expected), $result);
2442
-		$this->assertEquals($expected->getData(), $result->getData());
2443
-	}
2444
-
2445
-	public function testCreateShareRemoteGroup(): void {
2446
-		$share = $this->newShare();
2447
-		$this->shareManager->method('newShare')->willReturn($share);
2448
-
2449
-		/** @var ShareAPIController $ocs */
2450
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
2451
-			->setConstructorArgs([
2452
-				$this->appName,
2453
-				$this->request,
2454
-				$this->shareManager,
2455
-				$this->groupManager,
2456
-				$this->userManager,
2457
-				$this->rootFolder,
2458
-				$this->urlGenerator,
2459
-				$this->l,
2460
-				$this->config,
2461
-				$this->appManager,
2462
-				$this->serverContainer,
2463
-				$this->userStatusManager,
2464
-				$this->previewManager,
2465
-				$this->dateTimeZone,
2466
-				$this->logger,
2467
-				$this->factory,
2468
-				$this->mailer,
2469
-				$this->tagManager,
2470
-				$this->currentUser,
2471
-			])->onlyMethods(['formatShare'])
2472
-			->getMock();
2473
-
2474
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2475
-		$this->rootFolder->expects($this->exactly(2))
2476
-			->method('getUserFolder')
2477
-			->with('currentUser')
2478
-			->willReturn($userFolder);
2479
-
2480
-		$userFolder->expects($this->once())
2481
-			->method('get')
2482
-			->with('valid-path')
2483
-			->willReturn($path);
2484
-		$userFolder->method('getById')
2485
-			->willReturn([]);
2486
-
2487
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
2488
-
2489
-		$path->expects($this->once())
2490
-			->method('lock')
2491
-			->with(ILockingProvider::LOCK_SHARED);
2492
-
2493
-		$this->shareManager->method('createShare')
2494
-			->with($this->callback(function (IShare $share) use ($path) {
2495
-				return $share->getNode() === $path &&
2496
-					$share->getPermissions() === (
2497
-						Constants::PERMISSION_ALL &
2498
-						~Constants::PERMISSION_DELETE &
2499
-						~Constants::PERMISSION_CREATE
2500
-					) &&
2501
-					$share->getShareType() === IShare::TYPE_REMOTE_GROUP &&
2502
-					$share->getSharedWith() === '[email protected]' &&
2503
-					$share->getSharedBy() === 'currentUser';
2504
-			}))
2505
-			->willReturnArgument(0);
2506
-
2507
-		$this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true);
2508
-
2509
-		$expected = new DataResponse([]);
2510
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, '[email protected]');
2511
-
2512
-		$this->assertInstanceOf(get_class($expected), $result);
2513
-		$this->assertEquals($expected->getData(), $result->getData());
2514
-	}
2515
-
2516
-	public function testCreateShareRoom(): void {
2517
-		$ocs = $this->mockFormatShare();
2518
-
2519
-		$share = $this->newShare();
2520
-		$this->shareManager->method('newShare')->willReturn($share);
2521
-
2522
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2523
-		$this->rootFolder->expects($this->exactly(2))
2524
-			->method('getUserFolder')
2525
-			->with('currentUser')
2526
-			->willReturn($userFolder);
2527
-
2528
-		$userFolder->expects($this->once())
2529
-			->method('get')
2530
-			->with('valid-path')
2531
-			->willReturn($path);
2532
-		$userFolder->method('getById')
2533
-			->willReturn([]);
2534
-
2535
-		$path->expects($this->once())
2536
-			->method('lock')
2537
-			->with(ILockingProvider::LOCK_SHARED);
2538
-
2539
-		$this->appManager->method('isEnabledForUser')
2540
-			->with('spreed')
2541
-			->willReturn(true);
2542
-
2543
-		// This is not possible anymore with PHPUnit 10+
2544
-		// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2545
-		// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2546
-		$helper = $this->getMockBuilder(\stdClass::class)
2547
-			->addMethods(['createShare'])
2548
-			->getMock();
2549
-		$helper->method('createShare')
2550
-			->with(
2551
-				$share,
2552
-				'recipientRoom',
2553
-				Constants::PERMISSION_ALL &
2554
-				~Constants::PERMISSION_DELETE &
2555
-				~Constants::PERMISSION_CREATE,
2556
-				''
2557
-			)->willReturnCallback(
2558
-				function ($share): void {
2559
-					$share->setSharedWith('recipientRoom');
2560
-					$share->setPermissions(Constants::PERMISSION_ALL);
2561
-				}
2562
-			);
2563
-
2564
-		$this->serverContainer->method('get')
2565
-			->with('\OCA\Talk\Share\Helper\ShareAPIController')
2566
-			->willReturn($helper);
2567
-
2568
-		$this->shareManager->method('createShare')
2569
-			->with($this->callback(function (IShare $share) use ($path) {
2570
-				return $share->getNode() === $path
2571
-					&& $share->getPermissions() === Constants::PERMISSION_ALL
2572
-					&& $share->getShareType() === IShare::TYPE_ROOM
2573
-					&& $share->getSharedWith() === 'recipientRoom'
2574
-					&& $share->getSharedBy() === 'currentUser';
2575
-			}))
2576
-			->willReturnArgument(0);
2577
-
2578
-		$expected = new DataResponse([]);
2579
-		$result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2580
-
2581
-		$this->assertInstanceOf(get_class($expected), $result);
2582
-		$this->assertEquals($expected->getData(), $result->getData());
2583
-	}
2584
-
2585
-
2586
-	public function testCreateShareRoomHelperNotAvailable(): void {
2587
-		$this->expectException(OCSForbiddenException::class);
2588
-		$this->expectExceptionMessage('Sharing valid-path failed because the back end does not support room shares');
2589
-
2590
-		$ocs = $this->mockFormatShare();
2591
-
2592
-		$share = $this->newShare();
2593
-		$this->shareManager->method('newShare')->willReturn($share);
2594
-
2595
-		[$userFolder, $path] = $this->getNonSharedUserFolder();
2596
-		$this->rootFolder->method('getUserFolder')
2597
-			->with('currentUser')
2598
-			->willReturn($userFolder);
2599
-
2600
-		$path->method('getPath')->willReturn('valid-path');
2601
-		$userFolder->expects($this->once())
2602
-			->method('get')
2603
-			->with('valid-path')
2604
-			->willReturn($path);
2605
-		$userFolder->method('getById')
2606
-			->willReturn([]);
2607
-
2608
-		$path->expects($this->once())
2609
-			->method('lock')
2610
-			->with(ILockingProvider::LOCK_SHARED);
2611
-
2612
-		$this->appManager->method('isEnabledForUser')
2613
-			->with('spreed')
2614
-			->willReturn(false);
2615
-
2616
-		$this->shareManager->expects($this->never())->method('createShare');
2617
-
2618
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2619
-	}
2620
-
2621
-
2622
-	public function testCreateShareRoomHelperThrowException(): void {
2623
-		$this->expectException(OCSNotFoundException::class);
2624
-		$this->expectExceptionMessage('Exception thrown by the helper');
2625
-
2626
-		$ocs = $this->mockFormatShare();
2627
-
2628
-		$share = $this->newShare();
2629
-		$share->setSharedBy('currentUser');
2630
-		$this->shareManager->method('newShare')->willReturn($share);
2631
-
2632
-		[$userFolder, $path] = $this->getNonSharedUserFile();
2633
-		$this->rootFolder->method('getUserFolder')
2634
-			->with('currentUser')
2635
-			->willReturn($userFolder);
2636
-
2637
-		$userFolder->expects($this->once())
2638
-			->method('get')
2639
-			->with('valid-path')
2640
-			->willReturn($path);
2641
-		$userFolder->method('getById')
2642
-			->willReturn([]);
2643
-
2644
-		$path->expects($this->once())
2645
-			->method('lock')
2646
-			->with(ILockingProvider::LOCK_SHARED);
2647
-
2648
-		$this->appManager->method('isEnabledForUser')
2649
-			->with('spreed')
2650
-			->willReturn(true);
2651
-
2652
-		// This is not possible anymore with PHPUnit 10+
2653
-		// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2654
-		// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2655
-		$helper = $this->getMockBuilder(\stdClass::class)
2656
-			->addMethods(['createShare'])
2657
-			->getMock();
2658
-		$helper->method('createShare')
2659
-			->with(
2660
-				$share,
2661
-				'recipientRoom',
2662
-				Constants::PERMISSION_ALL & ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE),
2663
-				''
2664
-			)->willReturnCallback(
2665
-				function ($share): void {
2666
-					throw new OCSNotFoundException('Exception thrown by the helper');
2667
-				}
2668
-			);
2669
-
2670
-		$this->serverContainer->method('get')
2671
-			->with('\OCA\Talk\Share\Helper\ShareAPIController')
2672
-			->willReturn($helper);
2673
-
2674
-		$this->shareManager->expects($this->never())->method('createShare');
2675
-
2676
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2677
-	}
2678
-
2679
-	/**
2680
-	 * Test for https://github.com/owncloud/core/issues/22587
2681
-	 * TODO: Remove once proper solution is in place
2682
-	 */
2683
-	public function testCreateReshareOfFederatedMountNoDeletePermissions(): void {
2684
-		$share = Server::get(IManager::class)->newShare();
2685
-		$this->shareManager->method('newShare')->willReturn($share);
2686
-
2687
-		/** @var ShareAPIController&MockObject $ocs */
2688
-		$ocs = $this->getMockBuilder(ShareAPIController::class)
2689
-			->setConstructorArgs([
2690
-				$this->appName,
2691
-				$this->request,
2692
-				$this->shareManager,
2693
-				$this->groupManager,
2694
-				$this->userManager,
2695
-				$this->rootFolder,
2696
-				$this->urlGenerator,
2697
-				$this->l,
2698
-				$this->config,
2699
-				$this->appManager,
2700
-				$this->serverContainer,
2701
-				$this->userStatusManager,
2702
-				$this->previewManager,
2703
-				$this->dateTimeZone,
2704
-				$this->logger,
2705
-				$this->factory,
2706
-				$this->mailer,
2707
-				$this->tagManager,
2708
-				$this->currentUser,
2709
-			])->onlyMethods(['formatShare'])
2710
-			->getMock();
2711
-
2712
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
2713
-		$this->rootFolder->expects($this->exactly(2))
2714
-			->method('getUserFolder')
2715
-			->with('currentUser')
2716
-			->willReturn($userFolder);
2717
-
2718
-		$path = $this->getMockBuilder(Folder::class)->getMock();
2719
-		$path->method('getId')->willReturn(42);
2720
-
2721
-		$storage = $this->createMock(IStorage::class);
2722
-		$storage->method('instanceOfStorage')
2723
-			->willReturnMap([
2724
-				['OCA\Files_Sharing\External\Storage', true],
2725
-				['OCA\Files_Sharing\SharedStorage', false],
2726
-			]);
2727
-		$userFolder->method('getStorage')->willReturn($storage);
2728
-		$path->method('getStorage')->willReturn($storage);
2729
-
2730
-		$path->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
2731
-		$userFolder->expects($this->once())
2732
-			->method('get')
2733
-			->with('valid-path')
2734
-			->willReturn($path);
2735
-		$userFolder->method('getById')
2736
-			->willReturn([]);
2737
-
2738
-		$this->userManager->method('userExists')->with('validUser')->willReturn(true);
2739
-
2740
-		$this->shareManager
2741
-			->expects($this->once())
2742
-			->method('createShare')
2743
-			->with($this->callback(function (IShare $share) {
2744
-				return $share->getPermissions() === Constants::PERMISSION_READ;
2745
-			}))
2746
-			->willReturnArgument(0);
2747
-
2748
-		$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
2749
-	}
2750
-
2751
-
2752
-	public function testUpdateShareCantAccess(): void {
2753
-		$this->expectException(OCSNotFoundException::class);
2754
-		$this->expectExceptionMessage('Wrong share ID, share does not exist');
2755
-
2756
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
2757
-		$share = $this->newShare();
2758
-		$share->setNode($node);
2759
-
2760
-		$node->expects($this->once())
2761
-			->method('lock')
2762
-			->with(ILockingProvider::LOCK_SHARED);
2763
-
2764
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2765
-
2766
-		$this->rootFolder->method('getUserFolder')
2767
-			->with($this->currentUser)
2768
-			->willReturn($userFolder);
2769
-
2770
-		$userFolder->method('getById')
2771
-			->with($share->getNodeId())
2772
-			->willReturn([$share->getNode()]);
2773
-
2774
-		$this->ocs->updateShare(42);
2775
-	}
2776
-
2777
-
2778
-	public function testUpdateNoParametersLink(): void {
2779
-		$this->expectException(OCSBadRequestException::class);
2780
-		$this->expectExceptionMessage('Wrong or no update parameter given');
2781
-
2782
-		$node = $this->getMockBuilder(Folder::class)->getMock();
2783
-		$share = $this->newShare();
2784
-		$share->setPermissions(Constants::PERMISSION_ALL)
2785
-			->setSharedBy($this->currentUser)
2786
-			->setShareType(IShare::TYPE_LINK)
2787
-			->setNode($node);
2788
-
2789
-		$node->expects($this->once())
2790
-			->method('lock')
2791
-			->with(ILockingProvider::LOCK_SHARED);
2792
-
2793
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2794
-
2795
-		$this->ocs->updateShare(42);
2796
-	}
2797
-
2798
-
2799
-	public function testUpdateNoParametersOther(): void {
2800
-		$this->expectException(OCSBadRequestException::class);
2801
-		$this->expectExceptionMessage('Wrong or no update parameter given');
2802
-
2803
-		$node = $this->getMockBuilder(Folder::class)->getMock();
2804
-		$share = $this->newShare();
2805
-		$share->setPermissions(Constants::PERMISSION_ALL)
2806
-			->setSharedBy($this->currentUser)
2807
-			->setShareType(IShare::TYPE_GROUP)
2808
-			->setNode($node);
2809
-
2810
-		$node->expects($this->once())
2811
-			->method('lock')
2812
-			->with(ILockingProvider::LOCK_SHARED);
2813
-
2814
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2815
-
2816
-		$this->ocs->updateShare(42);
2817
-	}
2818
-
2819
-	public function testUpdateLinkShareClear(): void {
2820
-		$ocs = $this->mockFormatShare();
2821
-
2822
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
2823
-		$node->method('getId')
2824
-			->willReturn(42);
2825
-		$share = $this->newShare();
2826
-		$share->setPermissions(Constants::PERMISSION_ALL)
2827
-			->setSharedBy($this->currentUser)
2828
-			->setShareType(IShare::TYPE_LINK)
2829
-			->setPassword('password')
2830
-			->setExpirationDate(new \DateTime())
2831
-			->setNote('note')
2832
-			->setLabel('label')
2833
-			->setHideDownload(true)
2834
-			->setPermissions(Constants::PERMISSION_ALL)
2835
-			->setNode($node);
2836
-
2837
-		$node->expects($this->once())
2838
-			->method('lock')
2839
-			->with(ILockingProvider::LOCK_SHARED);
2840
-
2841
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2842
-
2843
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
2844
-			$this->callback(function (IShare $share) {
2845
-				return $share->getPermissions() === Constants::PERMISSION_READ &&
2846
-				$share->getPassword() === null &&
2847
-				$share->getExpirationDate() === null &&
2848
-				// Once set a note or a label are never back to null, only to an
2849
-				// empty string.
2850
-				$share->getNote() === '' &&
2851
-				$share->getLabel() === '' &&
2852
-				$share->getHideDownload() === false;
2853
-			})
2854
-		)->willReturnArgument(0);
2855
-
2856
-		$this->shareManager->method('getSharedWith')
2857
-			->willReturn([]);
2858
-
2859
-		$this->rootFolder->method('getUserFolder')
2860
-			->with($this->currentUser)
2861
-			->willReturn($userFolder);
2862
-
2863
-		$userFolder->method('getById')
2864
-			->with(42)
2865
-			->willReturn([$node]);
2866
-		$userFolder->method('getFirstNodeById')
2867
-			->with(42)
2868
-			->willReturn($node);
2869
-
2870
-		$mountPoint = $this->createMock(IMountPoint::class);
2871
-		$node->method('getMountPoint')
2872
-			->willReturn($mountPoint);
2873
-		$mountPoint->method('getStorageRootId')
2874
-			->willReturn(42);
2875
-
2876
-		$expected = new DataResponse([]);
2877
-		$result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false');
2878
-
2879
-		$this->assertInstanceOf(get_class($expected), $result);
2880
-		$this->assertEquals($expected->getData(), $result->getData());
2881
-	}
2882
-
2883
-	public function testUpdateLinkShareSet(): void {
2884
-		$ocs = $this->mockFormatShare();
2885
-
2886
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
2887
-		$folder->method('getId')
2888
-			->willReturn(42);
2889
-
2890
-		$share = Server::get(IManager::class)->newShare();
2891
-		$share->setPermissions(Constants::PERMISSION_ALL)
2892
-			->setSharedBy($this->currentUser)
2893
-			->setShareType(IShare::TYPE_LINK)
2894
-			->setNode($folder);
2895
-
2896
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2897
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2898
-
2899
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
2900
-			$this->callback(function (IShare $share) {
2901
-				$date = new \DateTime('2000-01-01');
2902
-				$date->setTime(0, 0, 0);
2903
-
2904
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2905
-				$share->getPassword() === 'password' &&
2906
-				$share->getExpirationDate() == $date &&
2907
-				$share->getNote() === 'note' &&
2908
-				$share->getLabel() === 'label' &&
2909
-				$share->getHideDownload() === true;
2910
-			})
2911
-		)->willReturnArgument(0);
2912
-
2913
-		$this->shareManager->method('getSharedWith')
2914
-			->willReturn([]);
2915
-
2916
-		$this->rootFolder->method('getUserFolder')
2917
-			->with($this->currentUser)
2918
-			->willReturn($userFolder);
2919
-
2920
-		$userFolder->method('getById')
2921
-			->with(42)
2922
-			->willReturn([$folder]);
2923
-
2924
-		$mountPoint = $this->createMock(IMountPoint::class);
2925
-		$folder->method('getMountPoint')
2926
-			->willReturn($mountPoint);
2927
-		$mountPoint->method('getStorageRootId')
2928
-			->willReturn(42);
2929
-
2930
-		$expected = new DataResponse([]);
2931
-		$result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true');
2932
-
2933
-		$this->assertInstanceOf(get_class($expected), $result);
2934
-		$this->assertEquals($expected->getData(), $result->getData());
2935
-	}
2936
-
2937
-	/**
2938
-	 * @dataProvider publicUploadParamsProvider
2939
-	 */
2940
-	public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password): void {
2941
-		$ocs = $this->mockFormatShare();
2942
-
2943
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
2944
-		$folder->method('getId')
2945
-			->willReturn(42);
2946
-
2947
-		$share = Server::get(IManager::class)->newShare();
2948
-		$share->setPermissions(Constants::PERMISSION_ALL)
2949
-			->setSharedBy($this->currentUser)
2950
-			->setShareType(IShare::TYPE_LINK)
2951
-			->setPassword('password')
2952
-			->setNode($folder);
2953
-
2954
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2955
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2956
-		$this->shareManager->method('getSharedWith')->willReturn([]);
2957
-
2958
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
2959
-			$this->callback(function (IShare $share) {
2960
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2961
-				$share->getPassword() === 'password' &&
2962
-				$share->getExpirationDate() === null;
2963
-			})
2964
-		)->willReturnArgument(0);
2965
-
2966
-		$this->rootFolder->method('getUserFolder')
2967
-			->with($this->currentUser)
2968
-			->willReturn($userFolder);
2969
-
2970
-		$userFolder->method('getById')
2971
-			->with(42)
2972
-			->willReturn([$folder]);
2973
-
2974
-		$mountPoint = $this->createMock(IMountPoint::class);
2975
-		$folder->method('getMountPoint')
2976
-			->willReturn($mountPoint);
2977
-		$mountPoint->method('getStorageRootId')
2978
-			->willReturn(42);
2979
-
2980
-		$expected = new DataResponse([]);
2981
-		$result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
2982
-
2983
-		$this->assertInstanceOf(get_class($expected), $result);
2984
-		$this->assertEquals($expected->getData(), $result->getData());
2985
-	}
2986
-
2987
-
2988
-	public function publicLinkValidPermissionsProvider() {
2989
-		return [
2990
-			[Constants::PERMISSION_CREATE],
2991
-			[Constants::PERMISSION_READ],
2992
-			[Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE],
2993
-			[Constants::PERMISSION_READ | Constants::PERMISSION_DELETE],
2994
-			[Constants::PERMISSION_READ | Constants::PERMISSION_CREATE],
2995
-		];
2996
-	}
2997
-
2998
-	/**
2999
-	 * @dataProvider publicLinkValidPermissionsProvider
3000
-	 */
3001
-	public function testUpdateLinkShareSetCRUDPermissions($permissions): void {
3002
-		$ocs = $this->mockFormatShare();
3003
-
3004
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3005
-		$folder->method('getId')
3006
-			->willReturn(42);
3007
-
3008
-		$share = Server::get(IManager::class)->newShare();
3009
-		$share->setPermissions(Constants::PERMISSION_ALL)
3010
-			->setSharedBy($this->currentUser)
3011
-			->setShareType(IShare::TYPE_LINK)
3012
-			->setPassword('password')
3013
-			->setNode($folder);
3014
-
3015
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3016
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3017
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3018
-
3019
-		$this->shareManager
3020
-			->expects($this->any())
3021
-			->method('updateShare')
3022
-			->willReturnArgument(0);
3023
-
3024
-		$this->rootFolder->method('getUserFolder')
3025
-			->with($this->currentUser)
3026
-			->willReturn($userFolder);
3027
-
3028
-		$userFolder->method('getById')
3029
-			->with(42)
3030
-			->willReturn([$folder]);
3031
-
3032
-		$mountPoint = $this->createMock(IMountPoint::class);
3033
-		$folder->method('getMountPoint')
3034
-			->willReturn($mountPoint);
3035
-		$mountPoint->method('getStorageRootId')
3036
-			->willReturn(42);
3037
-
3038
-		$expected = new DataResponse([]);
3039
-		$result = $ocs->updateShare(42, $permissions, 'password', null, null, null);
3040
-
3041
-		$this->assertInstanceOf(get_class($expected), $result);
3042
-		$this->assertEquals($expected->getData(), $result->getData());
3043
-	}
3044
-
3045
-	public function publicLinkInvalidPermissionsProvider1() {
3046
-		return [
3047
-			[Constants::PERMISSION_DELETE],
3048
-			[Constants::PERMISSION_UPDATE],
3049
-			[Constants::PERMISSION_SHARE],
3050
-		];
3051
-	}
3052
-
3053
-	/**
3054
-	 * @dataProvider publicLinkInvalidPermissionsProvider1
3055
-	 */
3056
-	public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions): void {
3057
-		$this->expectException(OCSBadRequestException::class);
3058
-		$this->expectExceptionMessage('Share must at least have READ or CREATE permissions');
3059
-
3060
-		$this->testUpdateLinkShareSetCRUDPermissions($permissions, null);
3061
-	}
3062
-
3063
-	public function publicLinkInvalidPermissionsProvider2() {
3064
-		return [
3065
-			[Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE],
3066
-			[Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE],
3067
-		];
3068
-	}
3069
-
3070
-	/**
3071
-	 * @dataProvider publicLinkInvalidPermissionsProvider2
3072
-	 */
3073
-	public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions): void {
3074
-		$this->expectException(OCSBadRequestException::class);
3075
-		$this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set');
3076
-
3077
-		$this->testUpdateLinkShareSetCRUDPermissions($permissions);
3078
-	}
3079
-
3080
-	public function testUpdateLinkShareInvalidDate(): void {
3081
-		$this->expectException(OCSBadRequestException::class);
3082
-		$this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
3083
-
3084
-		$ocs = $this->mockFormatShare();
3085
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3086
-		$userFolder->method('getById')
3087
-			->with(42)
3088
-			->willReturn([$folder]);
3089
-		$this->rootFolder->method('getUserFolder')
3090
-			->with($this->currentUser)
3091
-			->willReturn($userFolder);
3092
-
3093
-		$folder->method('getId')
3094
-			->willReturn(42);
3095
-
3096
-		$share = Server::get(IManager::class)->newShare();
3097
-		$share->setPermissions(Constants::PERMISSION_ALL)
3098
-			->setSharedBy($this->currentUser)
3099
-			->setShareType(IShare::TYPE_LINK)
3100
-			->setNode($folder);
3101
-
3102
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3103
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3104
-
3105
-		$ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a');
3106
-	}
3107
-
3108
-	public function publicUploadParamsProvider() {
3109
-		return [
3110
-			[null, 'true', null, 'password'],
3111
-			// legacy had no delete
3112
-			[
3113
-				Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE,
3114
-				'true', null, 'password'
3115
-			],
3116
-			// correct
3117
-			[
3118
-				Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE,
3119
-				null, null, 'password'
3120
-			],
3121
-		];
3122
-	}
3123
-
3124
-	/**
3125
-	 * @dataProvider publicUploadParamsProvider
3126
-	 */
3127
-	public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password): void {
3128
-		$this->expectException(OCSForbiddenException::class);
3129
-		$this->expectExceptionMessage('Public upload disabled by the administrator');
3130
-
3131
-		$ocs = $this->mockFormatShare();
3132
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3133
-		$userFolder->method('getById')
3134
-			->with(42)
3135
-			->willReturn([$folder]);
3136
-		$this->rootFolder->method('getUserFolder')
3137
-			->with($this->currentUser)
3138
-			->willReturn($userFolder);
3139
-
3140
-		$folder->method('getId')->willReturn(42);
3141
-
3142
-		$share = Server::get(IManager::class)->newShare();
3143
-		$share->setPermissions(Constants::PERMISSION_ALL)
3144
-			->setSharedBy($this->currentUser)
3145
-			->setShareType(IShare::TYPE_LINK)
3146
-			->setNode($folder);
3147
-
3148
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3149
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
3150
-
3151
-		$ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
3152
-	}
3153
-
3154
-
3155
-	public function testUpdateLinkSharePublicUploadOnFile(): void {
3156
-		$this->expectException(OCSBadRequestException::class);
3157
-		$this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
3158
-
3159
-		$ocs = $this->mockFormatShare();
3160
-
3161
-		$file = $this->getMockBuilder(File::class)->getMock();
3162
-		$file->method('getId')
3163
-			->willReturn(42);
3164
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3165
-		$userFolder->method('getById')
3166
-			->with(42)
3167
-			->willReturn([$folder]);
3168
-		$this->rootFolder->method('getUserFolder')
3169
-			->with($this->currentUser)
3170
-			->willReturn($userFolder);
3171
-
3172
-		$share = Server::get(IManager::class)->newShare();
3173
-		$share->setPermissions(Constants::PERMISSION_ALL)
3174
-			->setSharedBy($this->currentUser)
3175
-			->setShareType(IShare::TYPE_LINK)
3176
-			->setNode($file);
3177
-
3178
-		$this->shareManager
3179
-			->method('getShareById')
3180
-			->with('ocinternal:42')
3181
-			->willReturn($share);
3182
-		$this->shareManager
3183
-			->method('shareApiLinkAllowPublicUpload')
3184
-			->willReturn(true);
3185
-		$this->shareManager
3186
-			->method('updateShare')
3187
-			->with($share)
3188
-			->willThrowException(new \InvalidArgumentException('File shares cannot have create or delete permissions'));
3189
-
3190
-		$ocs->updateShare(42, null, 'password', null, 'true', '');
3191
-	}
3192
-
3193
-	public function testUpdateLinkSharePasswordDoesNotChangeOther(): void {
3194
-		$ocs = $this->mockFormatShare();
3195
-
3196
-		$date = new \DateTime('2000-01-01');
3197
-		$date->setTime(0, 0, 0);
3198
-
3199
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3200
-		$node->method('getId')->willReturn(42);
3201
-		$userFolder->method('getById')
3202
-			->with(42)
3203
-			->willReturn([$node]);
3204
-		$this->rootFolder->method('getUserFolder')
3205
-			->with($this->currentUser)
3206
-			->willReturn($userFolder);
3207
-		$share = $this->newShare();
3208
-		$share->setPermissions(Constants::PERMISSION_ALL)
3209
-			->setSharedBy($this->currentUser)
3210
-			->setShareType(IShare::TYPE_LINK)
3211
-			->setPassword('password')
3212
-			->setSendPasswordByTalk(true)
3213
-			->setExpirationDate($date)
3214
-			->setNote('note')
3215
-			->setLabel('label')
3216
-			->setHideDownload(true)
3217
-			->setPermissions(Constants::PERMISSION_ALL)
3218
-			->setNode($node);
3219
-
3220
-		$node->expects($this->once())
3221
-			->method('lock')
3222
-			->with(ILockingProvider::LOCK_SHARED);
3223
-
3224
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3225
-
3226
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3227
-			$this->callback(function (IShare $share) use ($date) {
3228
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3229
-				$share->getPassword() === 'newpassword' &&
3230
-				$share->getSendPasswordByTalk() === true &&
3231
-				$share->getExpirationDate() === $date &&
3232
-				$share->getNote() === 'note' &&
3233
-				$share->getLabel() === 'label' &&
3234
-				$share->getHideDownload() === true;
3235
-			})
3236
-		)->willReturnArgument(0);
3237
-
3238
-		$expected = new DataResponse([]);
3239
-		$result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null);
3240
-
3241
-		$this->assertInstanceOf(get_class($expected), $result);
3242
-		$this->assertEquals($expected->getData(), $result->getData());
3243
-	}
3244
-
3245
-	public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther(): void {
3246
-		$ocs = $this->mockFormatShare();
3247
-
3248
-		$date = new \DateTime('2000-01-01');
3249
-		$date->setTime(0, 0, 0);
3250
-
3251
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3252
-		$userFolder->method('getById')
3253
-			->with(42)
3254
-			->willReturn([$node]);
3255
-		$this->rootFolder->method('getUserFolder')
3256
-			->with($this->currentUser)
3257
-			->willReturn($userFolder);
3258
-		$node->method('getId')->willReturn(42);
3259
-		$share = $this->newShare();
3260
-		$share->setPermissions(Constants::PERMISSION_ALL)
3261
-			->setSharedBy($this->currentUser)
3262
-			->setShareType(IShare::TYPE_LINK)
3263
-			->setPassword('password')
3264
-			->setSendPasswordByTalk(false)
3265
-			->setExpirationDate($date)
3266
-			->setNote('note')
3267
-			->setLabel('label')
3268
-			->setHideDownload(true)
3269
-			->setPermissions(Constants::PERMISSION_ALL)
3270
-			->setNode($node);
3271
-
3272
-		$node->expects($this->once())
3273
-			->method('lock')
3274
-			->with(ILockingProvider::LOCK_SHARED);
3275
-
3276
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3277
-
3278
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3279
-
3280
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3281
-			$this->callback(function (IShare $share) use ($date) {
3282
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3283
-				$share->getPassword() === 'password' &&
3284
-				$share->getSendPasswordByTalk() === true &&
3285
-				$share->getExpirationDate() === $date &&
3286
-				$share->getNote() === 'note' &&
3287
-				$share->getLabel() === 'label' &&
3288
-				$share->getHideDownload() === true;
3289
-			})
3290
-		)->willReturnArgument(0);
3291
-
3292
-		$expected = new DataResponse([]);
3293
-		$result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3294
-
3295
-		$this->assertInstanceOf(get_class($expected), $result);
3296
-		$this->assertEquals($expected->getData(), $result->getData());
3297
-	}
3298
-
3299
-
3300
-	public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3301
-		$this->expectException(OCSForbiddenException::class);
3302
-		$this->expectExceptionMessage('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.');
3303
-
3304
-		$ocs = $this->mockFormatShare();
3305
-
3306
-		$date = new \DateTime('2000-01-01');
3307
-		$date->setTime(0, 0, 0);
3308
-
3309
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3310
-		$userFolder->method('getById')
3311
-			->with(42)
3312
-			->willReturn([$node]);
3313
-		$this->rootFolder->method('getUserFolder')
3314
-			->with($this->currentUser)
3315
-			->willReturn($userFolder);
3316
-		$node->method('getId')->willReturn(42);
3317
-		$share = $this->newShare();
3318
-		$share->setPermissions(Constants::PERMISSION_ALL)
3319
-			->setSharedBy($this->currentUser)
3320
-			->setShareType(IShare::TYPE_LINK)
3321
-			->setPassword('password')
3322
-			->setSendPasswordByTalk(false)
3323
-			->setExpirationDate($date)
3324
-			->setNote('note')
3325
-			->setLabel('label')
3326
-			->setHideDownload(true)
3327
-			->setPermissions(Constants::PERMISSION_ALL)
3328
-			->setNode($node);
3329
-
3330
-		$node->expects($this->once())
3331
-			->method('lock')
3332
-			->with(ILockingProvider::LOCK_SHARED);
3333
-
3334
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3335
-
3336
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3337
-
3338
-		$this->shareManager->expects($this->never())->method('updateShare');
3339
-
3340
-		$ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3341
-	}
3342
-
3343
-	public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther(): void {
3344
-		$ocs = $this->mockFormatShare();
3345
-
3346
-		$date = new \DateTime('2000-01-01');
3347
-		$date->setTime(0, 0, 0);
3348
-
3349
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3350
-		$userFolder->method('getById')
3351
-			->with(42)
3352
-			->willReturn([$node]);
3353
-		$this->rootFolder->method('getUserFolder')
3354
-			->with($this->currentUser)
3355
-			->willReturn($userFolder);
3356
-		$node->method('getId')->willReturn(42);
3357
-		$share = $this->newShare();
3358
-		$share->setPermissions(Constants::PERMISSION_ALL)
3359
-			->setSharedBy($this->currentUser)
3360
-			->setShareType(IShare::TYPE_LINK)
3361
-			->setPassword('password')
3362
-			->setSendPasswordByTalk(true)
3363
-			->setExpirationDate($date)
3364
-			->setNote('note')
3365
-			->setLabel('label')
3366
-			->setHideDownload(true)
3367
-			->setPermissions(Constants::PERMISSION_ALL)
3368
-			->setNode($node);
3369
-
3370
-		$node->expects($this->once())
3371
-			->method('lock')
3372
-			->with(ILockingProvider::LOCK_SHARED);
3373
-
3374
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3375
-
3376
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3377
-
3378
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3379
-			$this->callback(function (IShare $share) use ($date) {
3380
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3381
-				$share->getPassword() === 'password' &&
3382
-				$share->getSendPasswordByTalk() === false &&
3383
-				$share->getExpirationDate() === $date &&
3384
-				$share->getNote() === 'note' &&
3385
-				$share->getLabel() === 'label' &&
3386
-				$share->getHideDownload() === true;
3387
-			})
3388
-		)->willReturnArgument(0);
3389
-
3390
-		$expected = new DataResponse([]);
3391
-		$result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3392
-
3393
-		$this->assertInstanceOf(get_class($expected), $result);
3394
-		$this->assertEquals($expected->getData(), $result->getData());
3395
-	}
3396
-
3397
-	public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3398
-		$ocs = $this->mockFormatShare();
3399
-
3400
-		$date = new \DateTime('2000-01-01');
3401
-		$date->setTime(0, 0, 0);
3402
-
3403
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3404
-		$node->method('getId')
3405
-			->willReturn(42);
3406
-
3407
-		$share = $this->newShare();
3408
-		$share->setPermissions(Constants::PERMISSION_ALL)
3409
-			->setSharedBy($this->currentUser)
3410
-			->setShareType(IShare::TYPE_LINK)
3411
-			->setPassword('password')
3412
-			->setSendPasswordByTalk(true)
3413
-			->setExpirationDate($date)
3414
-			->setNote('note')
3415
-			->setLabel('label')
3416
-			->setHideDownload(true)
3417
-			->setPermissions(Constants::PERMISSION_ALL)
3418
-			->setNode($node);
3419
-
3420
-		$node->expects($this->once())
3421
-			->method('lock')
3422
-			->with(ILockingProvider::LOCK_SHARED);
3423
-
3424
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3425
-
3426
-		$this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3427
-
3428
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3429
-			$this->callback(function (IShare $share) use ($date) {
3430
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3431
-				$share->getPassword() === 'password' &&
3432
-				$share->getSendPasswordByTalk() === false &&
3433
-				$share->getExpirationDate() === $date &&
3434
-				$share->getNote() === 'note' &&
3435
-				$share->getLabel() === 'label' &&
3436
-				$share->getHideDownload() === true;
3437
-			})
3438
-		)->willReturnArgument(0);
3439
-
3440
-		$this->rootFolder->method('getUserFolder')
3441
-			->with($this->currentUser)
3442
-			->willReturn($userFolder);
3443
-
3444
-		$userFolder->method('getById')
3445
-			->with(42)
3446
-			->willReturn([$node]);
3447
-
3448
-		$mountPoint = $this->createMock(IMountPoint::class);
3449
-		$node->method('getMountPoint')
3450
-			->willReturn($mountPoint);
3451
-		$mountPoint->method('getStorageRootId')
3452
-			->willReturn(42);
3453
-
3454
-		$mountPoint = $this->createMock(IMountPoint::class);
3455
-		$node->method('getMountPoint')
3456
-			->willReturn($mountPoint);
3457
-		$mountPoint->method('getStorageRootId')
3458
-			->willReturn(42);
3459
-
3460
-		$expected = new DataResponse([]);
3461
-		$result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3462
-
3463
-		$this->assertInstanceOf(get_class($expected), $result);
3464
-		$this->assertEquals($expected->getData(), $result->getData());
3465
-	}
3466
-
3467
-	public function testUpdateLinkShareExpireDateDoesNotChangeOther(): void {
3468
-		$ocs = $this->mockFormatShare();
3469
-
3470
-		[$userFolder, $node] = $this->getNonSharedUserFolder();
3471
-		$node->method('getId')
3472
-			->willReturn(42);
3473
-
3474
-		$share = $this->newShare();
3475
-		$share->setPermissions(Constants::PERMISSION_ALL)
3476
-			->setSharedBy($this->currentUser)
3477
-			->setShareType(IShare::TYPE_LINK)
3478
-			->setPassword('password')
3479
-			->setSendPasswordByTalk(true)
3480
-			->setExpirationDate(new \DateTime())
3481
-			->setNote('note')
3482
-			->setLabel('label')
3483
-			->setHideDownload(true)
3484
-			->setPermissions(Constants::PERMISSION_ALL)
3485
-			->setNode($node);
3486
-
3487
-		$node->expects($this->once())
3488
-			->method('lock')
3489
-			->with(ILockingProvider::LOCK_SHARED);
3490
-
3491
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3492
-
3493
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3494
-			$this->callback(function (IShare $share) {
3495
-				$date = new \DateTime('2010-12-23');
3496
-				$date->setTime(0, 0, 0);
3497
-
3498
-				return $share->getPermissions() === Constants::PERMISSION_ALL &&
3499
-				$share->getPassword() === 'password' &&
3500
-				$share->getSendPasswordByTalk() === true &&
3501
-				$share->getExpirationDate() == $date &&
3502
-				$share->getNote() === 'note' &&
3503
-				$share->getLabel() === 'label' &&
3504
-				$share->getHideDownload() === true;
3505
-			})
3506
-		)->willReturnArgument(0);
3507
-
3508
-		$this->rootFolder->method('getUserFolder')
3509
-			->with($this->currentUser)
3510
-			->willReturn($userFolder);
3511
-
3512
-		$userFolder->method('getById')
3513
-			->with(42)
3514
-			->willReturn([$node]);
3515
-
3516
-		$mountPoint = $this->createMock(IMountPoint::class);
3517
-		$node->method('getMountPoint')
3518
-			->willReturn($mountPoint);
3519
-		$mountPoint->method('getStorageRootId')
3520
-			->willReturn(42);
3521
-
3522
-		$expected = new DataResponse([]);
3523
-		$result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null);
3524
-
3525
-		$this->assertInstanceOf(get_class($expected), $result);
3526
-		$this->assertEquals($expected->getData(), $result->getData());
3527
-	}
3528
-
3529
-	public function testUpdateLinkSharePublicUploadDoesNotChangeOther(): void {
3530
-		$ocs = $this->mockFormatShare();
3531
-
3532
-		$date = new \DateTime('2000-01-01');
3533
-
3534
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3535
-		$folder->method('getId')
3536
-			->willReturn(42);
3537
-
3538
-		$share = Server::get(IManager::class)->newShare();
3539
-		$share->setPermissions(Constants::PERMISSION_ALL)
3540
-			->setSharedBy($this->currentUser)
3541
-			->setShareType(IShare::TYPE_LINK)
3542
-			->setPassword('password')
3543
-			->setSendPasswordByTalk(true)
3544
-			->setExpirationDate($date)
3545
-			->setNote('note')
3546
-			->setLabel('label')
3547
-			->setHideDownload(true)
3548
-			->setPermissions(Constants::PERMISSION_ALL)
3549
-			->setNode($folder);
3550
-
3551
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3552
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3553
-
3554
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3555
-			$this->callback(function (IShare $share) use ($date) {
3556
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3557
-				$share->getPassword() === 'password' &&
3558
-				$share->getSendPasswordByTalk() === true &&
3559
-				$share->getExpirationDate() === $date &&
3560
-				$share->getNote() === 'note' &&
3561
-				$share->getLabel() === 'label' &&
3562
-				$share->getHideDownload() === true;
3563
-			})
3564
-		)->willReturnArgument(0);
3565
-
3566
-		$this->shareManager->method('getSharedWith')
3567
-			->willReturn([]);
3568
-
3569
-		$this->rootFolder->method('getUserFolder')
3570
-			->with($this->currentUser)
3571
-			->willReturn($userFolder);
3572
-
3573
-		$userFolder->method('getById')
3574
-			->with(42)
3575
-			->willReturn([$folder]);
3576
-
3577
-		$mountPoint = $this->createMock(IMountPoint::class);
3578
-		$folder->method('getMountPoint')
3579
-			->willReturn($mountPoint);
3580
-		$mountPoint->method('getStorageRootId')
3581
-			->willReturn(42);
3582
-
3583
-		$expected = new DataResponse([]);
3584
-		$result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null);
3585
-
3586
-		$this->assertInstanceOf(get_class($expected), $result);
3587
-		$this->assertEquals($expected->getData(), $result->getData());
3588
-	}
3589
-
3590
-	public function testUpdateLinkSharePermissions(): void {
3591
-		$ocs = $this->mockFormatShare();
3592
-
3593
-		$date = new \DateTime('2000-01-01');
3594
-
3595
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3596
-		$folder->method('getId')
3597
-			->willReturn(42);
3598
-
3599
-		$share = Server::get(IManager::class)->newShare();
3600
-		$share->setPermissions(Constants::PERMISSION_ALL)
3601
-			->setSharedBy($this->currentUser)
3602
-			->setShareType(IShare::TYPE_LINK)
3603
-			->setPassword('password')
3604
-			->setSendPasswordByTalk(true)
3605
-			->setExpirationDate($date)
3606
-			->setNote('note')
3607
-			->setLabel('label')
3608
-			->setHideDownload(true)
3609
-			->setPermissions(Constants::PERMISSION_ALL)
3610
-			->setNode($folder);
3611
-
3612
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3613
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3614
-
3615
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3616
-			$this->callback(function (IShare $share) use ($date): bool {
3617
-				return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3618
-				$share->getPassword() === 'password' &&
3619
-				$share->getSendPasswordByTalk() === true &&
3620
-				$share->getExpirationDate() === $date &&
3621
-				$share->getNote() === 'note' &&
3622
-				$share->getLabel() === 'label' &&
3623
-				$share->getHideDownload() === true;
3624
-			})
3625
-		)->willReturnArgument(0);
3626
-
3627
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3628
-
3629
-		$this->rootFolder->method('getUserFolder')
3630
-			->with($this->currentUser)
3631
-			->willReturn($userFolder);
3632
-
3633
-		$userFolder->method('getById')
3634
-			->with(42)
3635
-			->willReturn([$folder]);
3636
-
3637
-		$mountPoint = $this->createMock(IMountPoint::class);
3638
-		$folder->method('getMountPoint')
3639
-			->willReturn($mountPoint);
3640
-		$mountPoint->method('getStorageRootId')
3641
-			->willReturn(42);
3642
-
3643
-		$expected = new DataResponse([]);
3644
-		$result = $ocs->updateShare(42, 7, null, null, 'true', null, null, null, null);
3645
-
3646
-		$this->assertInstanceOf(get_class($expected), $result);
3647
-		$this->assertEquals($expected->getData(), $result->getData());
3648
-	}
3649
-
3650
-	public function testUpdateLinkSharePermissionsShare(): void {
3651
-		$ocs = $this->mockFormatShare();
3652
-
3653
-		$date = new \DateTime('2000-01-01');
3654
-
3655
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3656
-		$folder->method('getId')
3657
-			->willReturn(42);
3658
-
3659
-		$share = Server::get(IManager::class)->newShare();
3660
-		$share->setPermissions(Constants::PERMISSION_ALL)
3661
-			->setSharedBy($this->currentUser)
3662
-			->setShareType(IShare::TYPE_LINK)
3663
-			->setPassword('password')
3664
-			->setSendPasswordByTalk(true)
3665
-			->setExpirationDate($date)
3666
-			->setNote('note')
3667
-			->setLabel('label')
3668
-			->setHideDownload(true)
3669
-			->setPermissions(Constants::PERMISSION_READ)
3670
-			->setNode($folder);
3671
-
3672
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3673
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3674
-
3675
-		$this->shareManager->expects($this->once())
3676
-			->method('updateShare')
3677
-			->with(
3678
-				$this->callback(function (IShare $share) use ($date) {
3679
-					return $share->getPermissions() === Constants::PERMISSION_ALL &&
3680
-						$share->getPassword() === 'password' &&
3681
-						$share->getSendPasswordByTalk() === true &&
3682
-						$share->getExpirationDate() === $date &&
3683
-						$share->getNote() === 'note' &&
3684
-						$share->getLabel() === 'label' &&
3685
-						$share->getHideDownload() === true;
3686
-				})
3687
-			)->willReturnArgument(0);
3688
-
3689
-		$this->rootFolder->method('getUserFolder')
3690
-			->with($this->currentUser)
3691
-			->willReturn($userFolder);
3692
-
3693
-		$userFolder->method('getById')
3694
-			->with(42)
3695
-			->willReturn([$folder]);
3696
-
3697
-		$mountPoint = $this->createMock(IMountPoint::class);
3698
-		$folder->method('getMountPoint')
3699
-			->willReturn($mountPoint);
3700
-		$mountPoint->method('getStorageRootId')
3701
-			->willReturn(42);
3702
-
3703
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3704
-
3705
-		$expected = new DataResponse([]);
3706
-		$result = $ocs->updateShare(42, Constants::PERMISSION_ALL, null, null, null, null, null, null, null);
3707
-
3708
-		$this->assertInstanceOf(get_class($expected), $result);
3709
-		$this->assertEquals($expected->getData(), $result->getData());
3710
-	}
3711
-
3712
-	public function testUpdateOtherPermissions(): void {
3713
-		$ocs = $this->mockFormatShare();
3714
-
3715
-		[$userFolder, $file] = $this->getNonSharedUserFolder();
3716
-		$file->method('getId')
3717
-			->willReturn(42);
3718
-
3719
-		$share = Server::get(IManager::class)->newShare();
3720
-		$share->setPermissions(Constants::PERMISSION_ALL)
3721
-			->setSharedBy($this->currentUser)
3722
-			->setShareType(IShare::TYPE_USER)
3723
-			->setNode($file);
3724
-
3725
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3726
-		$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3727
-
3728
-		$this->shareManager->expects($this->once())->method('updateShare')->with(
3729
-			$this->callback(function (IShare $share) {
3730
-				return $share->getPermissions() === Constants::PERMISSION_ALL;
3731
-			})
3732
-		)->willReturnArgument(0);
3733
-
3734
-		$this->shareManager->method('getSharedWith')->willReturn([]);
3735
-
3736
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3737
-		$this->rootFolder->method('getUserFolder')
3738
-			->with($this->currentUser)
3739
-			->willReturn($userFolder);
3740
-
3741
-		$userFolder->method('getById')
3742
-			->with(42)
3743
-			->willReturn([$file]);
3744
-
3745
-		$mountPoint = $this->createMock(IMountPoint::class);
3746
-		$file->method('getMountPoint')
3747
-			->willReturn($mountPoint);
3748
-		$mountPoint->method('getStorageRootId')
3749
-			->willReturn(42);
3750
-
3751
-		$expected = new DataResponse([]);
3752
-		$result = $ocs->updateShare(42, 31, null, null, null, null);
3753
-
3754
-		$this->assertInstanceOf(get_class($expected), $result);
3755
-		$this->assertEquals($expected->getData(), $result->getData());
3756
-	}
3757
-
3758
-	public function testUpdateShareCannotIncreasePermissions(): void {
3759
-		$ocs = $this->mockFormatShare();
3760
-
3761
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3762
-		$folder->method('getId')
3763
-			->willReturn(42);
3764
-
3765
-		$share = Server::get(IManager::class)->newShare();
3766
-		$share
3767
-			->setId(42)
3768
-			->setSharedBy($this->currentUser)
3769
-			->setShareOwner('anotheruser')
3770
-			->setShareType(IShare::TYPE_GROUP)
3771
-			->setSharedWith('group1')
3772
-			->setPermissions(Constants::PERMISSION_READ)
3773
-			->setNode($folder);
3774
-
3775
-		// note: updateShare will modify the received instance but getSharedWith will reread from the database,
3776
-		// so their values will be different
3777
-		$incomingShare = Server::get(IManager::class)->newShare();
3778
-		$incomingShare
3779
-			->setId(42)
3780
-			->setSharedBy($this->currentUser)
3781
-			->setShareOwner('anotheruser')
3782
-			->setShareType(IShare::TYPE_GROUP)
3783
-			->setSharedWith('group1')
3784
-			->setPermissions(Constants::PERMISSION_READ)
3785
-			->setNode($folder);
3786
-
3787
-		$this->request
3788
-			->method('getParam')
3789
-			->willReturnMap([
3790
-				['permissions', null, '31'],
3791
-			]);
3792
-
3793
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3794
-
3795
-		$this->shareManager->expects($this->any())
3796
-			->method('getSharedWith')
3797
-			->willReturnMap([
3798
-				['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3799
-				['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]],
3800
-				['currentUser', IShare::TYPE_ROOM, $share->getNode(), -1, 0, []]
3801
-			]);
3802
-
3803
-		$this->rootFolder->method('getUserFolder')
3804
-			->with($this->currentUser)
3805
-			->willReturn($userFolder);
3806
-
3807
-		$userFolder->method('getById')
3808
-			->with(42)
3809
-			->willReturn([$folder]);
3810
-		$userFolder->method('getFirstNodeById')
3811
-			->with(42)
3812
-			->willReturn($folder);
3813
-
3814
-		$mountPoint = $this->createMock(IMountPoint::class);
3815
-		$folder->method('getMountPoint')
3816
-			->willReturn($mountPoint);
3817
-		$mountPoint->method('getStorageRootId')
3818
-			->willReturn(42);
3819
-
3820
-		$this->shareManager->expects($this->once())
3821
-			->method('updateShare')
3822
-			->with($share)
3823
-			->willThrowException(new GenericShareException('Cannot increase permissions of path/file', 'Cannot increase permissions of path/file', 404));
3824
-
3825
-		try {
3826
-			$ocs->updateShare(42, 31);
3827
-			$this->fail();
3828
-		} catch (OCSException $e) {
3829
-			$this->assertEquals('Cannot increase permissions of path/file', $e->getMessage());
3830
-		}
3831
-	}
3832
-
3833
-	public function testUpdateShareCanIncreasePermissionsIfOwner(): void {
3834
-		$ocs = $this->mockFormatShare();
3835
-
3836
-		[$userFolder, $folder] = $this->getNonSharedUserFolder();
3837
-		$folder->method('getId')
3838
-			->willReturn(42);
3839
-
3840
-		$share = Server::get(IManager::class)->newShare();
3841
-		$share
3842
-			->setId(42)
3843
-			->setSharedBy($this->currentUser)
3844
-			->setShareOwner($this->currentUser)
3845
-			->setShareType(IShare::TYPE_GROUP)
3846
-			->setSharedWith('group1')
3847
-			->setPermissions(Constants::PERMISSION_READ)
3848
-			->setNode($folder);
3849
-
3850
-		// note: updateShare will modify the received instance but getSharedWith will reread from the database,
3851
-		// so their values will be different
3852
-		$incomingShare = Server::get(IManager::class)->newShare();
3853
-		$incomingShare
3854
-			->setId(42)
3855
-			->setSharedBy($this->currentUser)
3856
-			->setShareOwner($this->currentUser)
3857
-			->setShareType(IShare::TYPE_GROUP)
3858
-			->setSharedWith('group1')
3859
-			->setPermissions(Constants::PERMISSION_READ)
3860
-			->setNode($folder);
3861
-
3862
-		$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3863
-
3864
-		$this->shareManager->expects($this->any())
3865
-			->method('getSharedWith')
3866
-			->willReturnMap([
3867
-				['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3868
-				['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
3869
-			]);
3870
-
3871
-		$this->shareManager->expects($this->once())
3872
-			->method('updateShare')
3873
-			->with($share)
3874
-			->willReturn($share);
3875
-
3876
-		$this->rootFolder->method('getUserFolder')
3877
-			->with($this->currentUser)
3878
-			->willReturn($userFolder);
3879
-
3880
-		$userFolder->method('getById')
3881
-			->with(42)
3882
-			->willReturn([$folder]);
3883
-
3884
-		$mountPoint = $this->createMock(IMountPoint::class);
3885
-		$folder->method('getMountPoint')
3886
-			->willReturn($mountPoint);
3887
-		$mountPoint->method('getStorageRootId')
3888
-			->willReturn(42);
3889
-
3890
-		$result = $ocs->updateShare(42, 31);
3891
-		$this->assertInstanceOf(DataResponse::class, $result);
3892
-	}
3893
-
3894
-	public function testUpdateShareOwnerless(): void {
3895
-		$ocs = $this->mockFormatShare();
3896
-
3897
-		$mount = $this->createMock(IShareOwnerlessMount::class);
3898
-
3899
-		$file = $this->createMock(File::class);
3900
-		$file
3901
-			->expects($this->exactly(2))
3902
-			->method('getPermissions')
3903
-			->willReturn(Constants::PERMISSION_SHARE);
3904
-		$file
3905
-			->expects($this->once())
3906
-			->method('getMountPoint')
3907
-			->willReturn($mount);
3908
-
3909
-		$userFolder = $this->createMock(Folder::class);
3910
-		$userFolder->method('getById')
3911
-			->with(2)
3912
-			->willReturn([$file]);
3913
-		$userFolder->method('getFirstNodeById')
3914
-			->with(2)
3915
-			->willReturn($file);
3916
-
3917
-		$this->rootFolder
3918
-			->method('getUserFolder')
3919
-			->with($this->currentUser)
3920
-			->willReturn($userFolder);
3921
-
3922
-		$share = $this->createMock(IShare::class);
3923
-		$share
3924
-			->expects($this->once())
3925
-			->method('getNode')
3926
-			->willReturn($file);
3927
-		$share
3928
-			->expects($this->exactly(2))
3929
-			->method('getNodeId')
3930
-			->willReturn(2);
3931
-		$share
3932
-			->expects($this->exactly(2))
3933
-			->method('getPermissions')
3934
-			->willReturn(Constants::PERMISSION_SHARE);
3935
-
3936
-		$this->shareManager
3937
-			->expects($this->once())
3938
-			->method('getShareById')
3939
-			->with('ocinternal:1', $this->currentUser)
3940
-			->willReturn($share);
3941
-
3942
-		$this->shareManager
3943
-			->expects($this->once())
3944
-			->method('updateShare')
3945
-			->with($share)
3946
-			->willReturn($share);
3947
-
3948
-		$result = $ocs->updateShare(1, Constants::PERMISSION_ALL);
3949
-		$this->assertInstanceOf(DataResponse::class, $result);
3950
-	}
3951
-
3952
-	public function dataFormatShare() {
3953
-		$file = $this->getMockBuilder(File::class)->getMock();
3954
-		$folder = $this->getMockBuilder(Folder::class)->getMock();
3955
-		$parent = $this->getMockBuilder(Folder::class)->getMock();
3956
-		$fileWithPreview = $this->getMockBuilder(File::class)->getMock();
3957
-
3958
-		$file->method('getMimeType')->willReturn('myMimeType');
3959
-		$folder->method('getMimeType')->willReturn('myFolderMimeType');
3960
-		$fileWithPreview->method('getMimeType')->willReturn('mimeWithPreview');
3961
-
3962
-		$mountPoint = $this->createMock(IMountPoint::class);
3963
-		$mountPoint->method('getMountType')->willReturn('');
3964
-		$file->method('getMountPoint')->willReturn($mountPoint);
3965
-		$folder->method('getMountPoint')->willReturn($mountPoint);
3966
-		$fileWithPreview->method('getMountPoint')->willReturn($mountPoint);
3967
-
3968
-		$file->method('getPath')->willReturn('file');
3969
-		$folder->method('getPath')->willReturn('folder');
3970
-		$fileWithPreview->method('getPath')->willReturn('fileWithPreview');
3971
-
3972
-		$parent->method('getId')->willReturn(1);
3973
-		$folder->method('getId')->willReturn(2);
3974
-		$file->method('getId')->willReturn(3);
3975
-		$fileWithPreview->method('getId')->willReturn(4);
3976
-
3977
-		$file->method('getParent')->willReturn($parent);
3978
-		$folder->method('getParent')->willReturn($parent);
3979
-		$fileWithPreview->method('getParent')->willReturn($parent);
3980
-
3981
-		$file->method('getSize')->willReturn(123456);
3982
-		$folder->method('getSize')->willReturn(123456);
3983
-		$fileWithPreview->method('getSize')->willReturn(123456);
3984
-		$file->method('getMTime')->willReturn(1234567890);
3985
-		$folder->method('getMTime')->willReturn(1234567890);
3986
-		$fileWithPreview->method('getMTime')->willReturn(1234567890);
3987
-
3988
-		$cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
3989
-		$cache->method('getNumericStorageId')->willReturn(100);
3990
-		$storage = $this->createMock(IStorage::class);
3991
-		$storage->method('getId')->willReturn('storageId');
3992
-		$storage->method('getCache')->willReturn($cache);
3993
-
3994
-		$file->method('getStorage')->willReturn($storage);
3995
-		$folder->method('getStorage')->willReturn($storage);
3996
-		$fileWithPreview->method('getStorage')->willReturn($storage);
3997
-
3998
-
3999
-		$mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
4000
-		$mountPoint->method('getMountType')->willReturn('');
4001
-		$file->method('getMountPoint')->willReturn($mountPoint);
4002
-		$folder->method('getMountPoint')->willReturn($mountPoint);
4003
-
4004
-		$owner = $this->getMockBuilder(IUser::class)->getMock();
4005
-		$owner->method('getDisplayName')->willReturn('ownerDN');
4006
-		$initiator = $this->getMockBuilder(IUser::class)->getMock();
4007
-		$initiator->method('getDisplayName')->willReturn('initiatorDN');
4008
-		$recipient = $this->getMockBuilder(IUser::class)->getMock();
4009
-		$recipient->method('getDisplayName')->willReturn('recipientDN');
4010
-		$recipient->method('getSystemEMailAddress')->willReturn('recipient');
4011
-		[$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
4012
-
4013
-		$result = [];
4014
-
4015
-		$share = Server::get(IManager::class)->newShare();
4016
-		$share->setShareType(IShare::TYPE_USER)
4017
-			->setSharedWith('recipient')
4018
-			->setSharedBy('initiator')
4019
-			->setShareOwner('owner')
4020
-			->setPermissions(Constants::PERMISSION_READ)
4021
-			->setAttributes($shareAttributes)
4022
-			->setNode($file)
4023
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4024
-			->setTarget('myTarget')
4025
-			->setNote('personal note')
4026
-			->setId(42);
4027
-
4028
-		// User backend down
4029
-		$result[] = [
4030
-			[
4031
-				'id' => '42',
4032
-				'share_type' => IShare::TYPE_USER,
4033
-				'uid_owner' => 'initiator',
4034
-				'displayname_owner' => 'initiator',
4035
-				'permissions' => 1,
4036
-				'attributes' => $shareAttributesReturnJson,
4037
-				'stime' => 946684862,
4038
-				'parent' => null,
4039
-				'expiration' => null,
4040
-				'token' => null,
4041
-				'uid_file_owner' => 'owner',
4042
-				'displayname_file_owner' => 'owner',
4043
-				'path' => 'file',
4044
-				'item_type' => 'file',
4045
-				'storage_id' => 'storageId',
4046
-				'storage' => 100,
4047
-				'item_source' => 3,
4048
-				'file_source' => 3,
4049
-				'file_parent' => 1,
4050
-				'file_target' => 'myTarget',
4051
-				'share_with' => 'recipient',
4052
-				'share_with_displayname' => 'recipient',
4053
-				'share_with_displayname_unique' => 'recipient',
4054
-				'note' => 'personal note',
4055
-				'label' => '',
4056
-				'mail_send' => 0,
4057
-				'mimetype' => 'myMimeType',
4058
-				'has_preview' => false,
4059
-				'hide_download' => 0,
4060
-				'can_edit' => false,
4061
-				'can_delete' => false,
4062
-				'item_size' => 123456,
4063
-				'item_mtime' => 1234567890,
4064
-				'is-mount-root' => false,
4065
-				'mount-type' => '',
4066
-				'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4067
-				'item_permissions' => 1,
4068
-			], $share, [], false
4069
-		];
4070
-		// User backend up
4071
-		$result[] = [
4072
-			[
4073
-				'id' => '42',
4074
-				'share_type' => IShare::TYPE_USER,
4075
-				'uid_owner' => 'initiator',
4076
-				'displayname_owner' => 'initiatorDN',
4077
-				'permissions' => 1,
4078
-				'attributes' => $shareAttributesReturnJson,
4079
-				'stime' => 946684862,
4080
-				'parent' => null,
4081
-				'expiration' => null,
4082
-				'token' => null,
4083
-				'uid_file_owner' => 'owner',
4084
-				'displayname_file_owner' => 'ownerDN',
4085
-				'note' => 'personal note',
4086
-				'label' => '',
4087
-				'path' => 'file',
4088
-				'item_type' => 'file',
4089
-				'storage_id' => 'storageId',
4090
-				'storage' => 100,
4091
-				'item_source' => 3,
4092
-				'file_source' => 3,
4093
-				'file_parent' => 1,
4094
-				'file_target' => 'myTarget',
4095
-				'share_with' => 'recipient',
4096
-				'share_with_displayname' => 'recipientDN',
4097
-				'share_with_displayname_unique' => 'recipient',
4098
-				'mail_send' => 0,
4099
-				'mimetype' => 'myMimeType',
4100
-				'has_preview' => false,
4101
-				'hide_download' => 0,
4102
-				'can_edit' => false,
4103
-				'can_delete' => false,
4104
-				'item_size' => 123456,
4105
-				'item_mtime' => 1234567890,
4106
-				'is-mount-root' => false,
4107
-				'mount-type' => '',
4108
-				'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4109
-				'item_permissions' => 1,
4110
-			], $share, [
4111
-				['owner', $owner],
4112
-				['initiator', $initiator],
4113
-				['recipient', $recipient],
4114
-			], false
4115
-		];
4116
-
4117
-		$share = Server::get(IManager::class)->newShare();
4118
-		$share->setShareType(IShare::TYPE_USER)
4119
-			->setSharedWith('recipient')
4120
-			->setSharedBy('initiator')
4121
-			->setShareOwner('owner')
4122
-			->setPermissions(Constants::PERMISSION_READ)
4123
-			->setNode($file)
4124
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4125
-			->setTarget('myTarget')
4126
-			->setNote('personal note')
4127
-			->setId(42);
4128
-		// User backend down
4129
-		$result[] = [
4130
-			[
4131
-				'id' => '42',
4132
-				'share_type' => IShare::TYPE_USER,
4133
-				'uid_owner' => 'initiator',
4134
-				'displayname_owner' => 'initiator',
4135
-				'permissions' => 1,
4136
-				'attributes' => null,
4137
-				'stime' => 946684862,
4138
-				'parent' => null,
4139
-				'expiration' => null,
4140
-				'token' => null,
4141
-				'uid_file_owner' => 'owner',
4142
-				'displayname_file_owner' => 'owner',
4143
-				'note' => 'personal note',
4144
-				'label' => '',
4145
-				'path' => 'file',
4146
-				'item_type' => 'file',
4147
-				'storage_id' => 'storageId',
4148
-				'storage' => 100,
4149
-				'item_source' => 3,
4150
-				'file_source' => 3,
4151
-				'file_parent' => 1,
4152
-				'file_target' => 'myTarget',
4153
-				'share_with' => 'recipient',
4154
-				'share_with_displayname' => 'recipient',
4155
-				'share_with_displayname_unique' => 'recipient',
4156
-				'mail_send' => 0,
4157
-				'mimetype' => 'myMimeType',
4158
-				'has_preview' => false,
4159
-				'hide_download' => 0,
4160
-				'can_edit' => false,
4161
-				'can_delete' => false,
4162
-				'item_size' => 123456,
4163
-				'item_mtime' => 1234567890,
4164
-				'is-mount-root' => false,
4165
-				'mount-type' => '',
4166
-				'attributes' => null,
4167
-				'item_permissions' => 1,
4168
-			], $share, [], false
4169
-		];
4170
-
4171
-		$share = Server::get(IManager::class)->newShare();
4172
-		$share->setShareType(IShare::TYPE_USER)
4173
-			->setSharedWith('recipient')
4174
-			->setSharedBy('initiator')
4175
-			->setShareOwner('currentUser')
4176
-			->setPermissions(Constants::PERMISSION_READ)
4177
-			->setNode($file)
4178
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4179
-			->setTarget('myTarget')
4180
-			->setNote('personal note')
4181
-			->setId(42);
4182
-		// User backend down
4183
-		$result[] = [
4184
-			[
4185
-				'id' => '42',
4186
-				'share_type' => IShare::TYPE_USER,
4187
-				'uid_owner' => 'initiator',
4188
-				'displayname_owner' => 'initiator',
4189
-				'permissions' => 1,
4190
-				'attributes' => null,
4191
-				'stime' => 946684862,
4192
-				'parent' => null,
4193
-				'expiration' => null,
4194
-				'token' => null,
4195
-				'uid_file_owner' => 'currentUser',
4196
-				'displayname_file_owner' => 'currentUser',
4197
-				'note' => 'personal note',
4198
-				'label' => '',
4199
-				'path' => 'file',
4200
-				'item_type' => 'file',
4201
-				'storage_id' => 'storageId',
4202
-				'storage' => 100,
4203
-				'item_source' => 3,
4204
-				'file_source' => 3,
4205
-				'file_parent' => 1,
4206
-				'file_target' => 'myTarget',
4207
-				'share_with' => 'recipient',
4208
-				'share_with_displayname' => 'recipient',
4209
-				'share_with_displayname_unique' => 'recipient',
4210
-				'mail_send' => 0,
4211
-				'mimetype' => 'myMimeType',
4212
-				'has_preview' => false,
4213
-				'hide_download' => 0,
4214
-				'can_edit' => true,
4215
-				'can_delete' => true,
4216
-				'item_size' => 123456,
4217
-				'item_mtime' => 1234567890,
4218
-				'is-mount-root' => false,
4219
-				'mount-type' => '',
4220
-				'attributes' => null,
4221
-				'item_permissions' => 11,
4222
-			], $share, [], false
4223
-		];
4224
-
4225
-		// with existing group
4226
-
4227
-		$share = Server::get(IManager::class)->newShare();
4228
-		$share->setShareType(IShare::TYPE_GROUP)
4229
-			->setSharedWith('recipientGroup')
4230
-			->setSharedBy('initiator')
4231
-			->setShareOwner('owner')
4232
-			->setPermissions(Constants::PERMISSION_READ)
4233
-			->setNode($file)
4234
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4235
-			->setTarget('myTarget')
4236
-			->setNote('personal note')
4237
-			->setId(42);
4238
-
4239
-		$result[] = [
4240
-			[
4241
-				'id' => '42',
4242
-				'share_type' => IShare::TYPE_GROUP,
4243
-				'uid_owner' => 'initiator',
4244
-				'displayname_owner' => 'initiator',
4245
-				'permissions' => 1,
4246
-				'attributes' => null,
4247
-				'stime' => 946684862,
4248
-				'parent' => null,
4249
-				'expiration' => null,
4250
-				'token' => null,
4251
-				'uid_file_owner' => 'owner',
4252
-				'displayname_file_owner' => 'owner',
4253
-				'note' => 'personal note',
4254
-				'label' => '',
4255
-				'path' => 'file',
4256
-				'item_type' => 'file',
4257
-				'storage_id' => 'storageId',
4258
-				'storage' => 100,
4259
-				'item_source' => 3,
4260
-				'file_source' => 3,
4261
-				'file_parent' => 1,
4262
-				'file_target' => 'myTarget',
4263
-				'share_with' => 'recipientGroup',
4264
-				'share_with_displayname' => 'recipientGroupDisplayName',
4265
-				'mail_send' => 0,
4266
-				'mimetype' => 'myMimeType',
4267
-				'has_preview' => false,
4268
-				'hide_download' => 0,
4269
-				'can_edit' => false,
4270
-				'can_delete' => false,
4271
-				'item_size' => 123456,
4272
-				'item_mtime' => 1234567890,
4273
-				'is-mount-root' => false,
4274
-				'mount-type' => '',
4275
-				'attributes' => null,
4276
-				'item_permissions' => 1,
4277
-			], $share, [], false
4278
-		];
4279
-
4280
-		// with unknown group / no group backend
4281
-		$share = Server::get(IManager::class)->newShare();
4282
-		$share->setShareType(IShare::TYPE_GROUP)
4283
-			->setSharedWith('recipientGroup2')
4284
-			->setSharedBy('initiator')
4285
-			->setShareOwner('owner')
4286
-			->setPermissions(Constants::PERMISSION_READ)
4287
-			->setNode($file)
4288
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4289
-			->setTarget('myTarget')
4290
-			->setNote('personal note')
4291
-			->setId(42);
4292
-		$result[] = [
4293
-			[
4294
-				'id' => '42',
4295
-				'share_type' => IShare::TYPE_GROUP,
4296
-				'uid_owner' => 'initiator',
4297
-				'displayname_owner' => 'initiator',
4298
-				'permissions' => 1,
4299
-				'stime' => 946684862,
4300
-				'parent' => null,
4301
-				'expiration' => null,
4302
-				'token' => null,
4303
-				'uid_file_owner' => 'owner',
4304
-				'displayname_file_owner' => 'owner',
4305
-				'note' => 'personal note',
4306
-				'label' => '',
4307
-				'path' => 'file',
4308
-				'item_type' => 'file',
4309
-				'storage_id' => 'storageId',
4310
-				'storage' => 100,
4311
-				'item_source' => 3,
4312
-				'file_source' => 3,
4313
-				'file_parent' => 1,
4314
-				'file_target' => 'myTarget',
4315
-				'share_with' => 'recipientGroup2',
4316
-				'share_with_displayname' => 'recipientGroup2',
4317
-				'mail_send' => 0,
4318
-				'mimetype' => 'myMimeType',
4319
-				'has_preview' => false,
4320
-				'hide_download' => 0,
4321
-				'can_edit' => false,
4322
-				'can_delete' => false,
4323
-				'item_size' => 123456,
4324
-				'item_mtime' => 1234567890,
4325
-				'is-mount-root' => false,
4326
-				'mount-type' => '',
4327
-				'attributes' => null,
4328
-				'item_permissions' => 1,
4329
-			], $share, [], false
4330
-		];
4331
-
4332
-		$share = Server::get(IManager::class)->newShare();
4333
-		$share->setShareType(IShare::TYPE_LINK)
4334
-			->setSharedBy('initiator')
4335
-			->setShareOwner('owner')
4336
-			->setPermissions(Constants::PERMISSION_READ)
4337
-			->setNode($file)
4338
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4339
-			->setTarget('myTarget')
4340
-			->setPassword('mypassword')
4341
-			->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4342
-			->setToken('myToken')
4343
-			->setNote('personal note')
4344
-			->setLabel('new link share')
4345
-			->setId(42);
4346
-
4347
-		$result[] = [
4348
-			[
4349
-				'id' => '42',
4350
-				'share_type' => IShare::TYPE_LINK,
4351
-				'uid_owner' => 'initiator',
4352
-				'displayname_owner' => 'initiator',
4353
-				'permissions' => 1,
4354
-				'attributes' => null,
4355
-				'stime' => 946684862,
4356
-				'parent' => null,
4357
-				'expiration' => '2001-01-02 00:00:00',
4358
-				'token' => 'myToken',
4359
-				'uid_file_owner' => 'owner',
4360
-				'displayname_file_owner' => 'owner',
4361
-				'note' => 'personal note',
4362
-				'label' => 'new link share',
4363
-				'path' => 'file',
4364
-				'item_type' => 'file',
4365
-				'storage_id' => 'storageId',
4366
-				'storage' => 100,
4367
-				'item_source' => 3,
4368
-				'file_source' => 3,
4369
-				'file_parent' => 1,
4370
-				'file_target' => 'myTarget',
4371
-				'password' => 'mypassword',
4372
-				'share_with' => 'mypassword',
4373
-				'share_with_displayname' => '(Shared link)',
4374
-				'send_password_by_talk' => false,
4375
-				'mail_send' => 0,
4376
-				'url' => 'myLink',
4377
-				'mimetype' => 'myMimeType',
4378
-				'has_preview' => false,
4379
-				'hide_download' => 0,
4380
-				'can_edit' => false,
4381
-				'can_delete' => false,
4382
-				'item_size' => 123456,
4383
-				'item_mtime' => 1234567890,
4384
-				'is-mount-root' => false,
4385
-				'mount-type' => '',
4386
-				'attributes' => null,
4387
-				'item_permissions' => 1,
4388
-			], $share, [], false
4389
-		];
4390
-
4391
-		$share = Server::get(IManager::class)->newShare();
4392
-		$share->setShareType(IShare::TYPE_LINK)
4393
-			->setSharedBy('initiator')
4394
-			->setShareOwner('owner')
4395
-			->setPermissions(Constants::PERMISSION_READ)
4396
-			->setNode($file)
4397
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4398
-			->setTarget('myTarget')
4399
-			->setPassword('mypassword')
4400
-			->setSendPasswordByTalk(true)
4401
-			->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4402
-			->setToken('myToken')
4403
-			->setNote('personal note')
4404
-			->setLabel('new link share')
4405
-			->setId(42);
4406
-
4407
-		$result[] = [
4408
-			[
4409
-				'id' => '42',
4410
-				'share_type' => IShare::TYPE_LINK,
4411
-				'uid_owner' => 'initiator',
4412
-				'displayname_owner' => 'initiator',
4413
-				'permissions' => 1,
4414
-				'stime' => 946684862,
4415
-				'parent' => null,
4416
-				'expiration' => '2001-01-02 00:00:00',
4417
-				'token' => 'myToken',
4418
-				'uid_file_owner' => 'owner',
4419
-				'displayname_file_owner' => 'owner',
4420
-				'note' => 'personal note',
4421
-				'label' => 'new link share',
4422
-				'path' => 'file',
4423
-				'item_type' => 'file',
4424
-				'storage_id' => 'storageId',
4425
-				'storage' => 100,
4426
-				'item_source' => 3,
4427
-				'file_source' => 3,
4428
-				'file_parent' => 1,
4429
-				'file_target' => 'myTarget',
4430
-				'password' => 'mypassword',
4431
-				'share_with' => 'mypassword',
4432
-				'share_with_displayname' => '(Shared link)',
4433
-				'send_password_by_talk' => true,
4434
-				'mail_send' => 0,
4435
-				'url' => 'myLink',
4436
-				'mimetype' => 'myMimeType',
4437
-				'has_preview' => false,
4438
-				'hide_download' => 0,
4439
-				'can_edit' => false,
4440
-				'can_delete' => false,
4441
-				'item_size' => 123456,
4442
-				'item_mtime' => 1234567890,
4443
-				'is-mount-root' => false,
4444
-				'mount-type' => '',
4445
-				'attributes' => null,
4446
-				'item_permissions' => 1,
4447
-			], $share, [], false
4448
-		];
4449
-
4450
-		$share = Server::get(IManager::class)->newShare();
4451
-		$share->setShareType(IShare::TYPE_REMOTE)
4452
-			->setSharedBy('initiator')
4453
-			->setSharedWith('[email protected]')
4454
-			->setShareOwner('owner')
4455
-			->setPermissions(Constants::PERMISSION_READ)
4456
-			->setNode($folder)
4457
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4458
-			->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4459
-			->setTarget('myTarget')
4460
-			->setNote('personal note')
4461
-			->setId(42);
4462
-
4463
-		$result[] = [
4464
-			[
4465
-				'id' => '42',
4466
-				'share_type' => IShare::TYPE_REMOTE,
4467
-				'uid_owner' => 'initiator',
4468
-				'displayname_owner' => 'initiator',
4469
-				'permissions' => 1,
4470
-				'stime' => 946684862,
4471
-				'parent' => null,
4472
-				'expiration' => '2001-02-03 00:00:00',
4473
-				'token' => null,
4474
-				'uid_file_owner' => 'owner',
4475
-				'displayname_file_owner' => 'owner',
4476
-				'note' => 'personal note',
4477
-				'label' => '',
4478
-				'path' => 'folder',
4479
-				'item_type' => 'folder',
4480
-				'storage_id' => 'storageId',
4481
-				'storage' => 100,
4482
-				'item_source' => 2,
4483
-				'file_source' => 2,
4484
-				'file_parent' => 1,
4485
-				'file_target' => 'myTarget',
4486
-				'share_with' => '[email protected]',
4487
-				'share_with_displayname' => 'foobar',
4488
-				'mail_send' => 0,
4489
-				'mimetype' => 'myFolderMimeType',
4490
-				'has_preview' => false,
4491
-				'hide_download' => 0,
4492
-				'can_edit' => false,
4493
-				'can_delete' => false,
4494
-				'item_size' => 123456,
4495
-				'item_mtime' => 1234567890,
4496
-				'is-mount-root' => false,
4497
-				'mount-type' => '',
4498
-				'attributes' => null,
4499
-				'item_permissions' => 1,
4500
-			], $share, [], false
4501
-		];
4502
-
4503
-		$share = Server::get(IManager::class)->newShare();
4504
-		$share->setShareType(IShare::TYPE_REMOTE_GROUP)
4505
-			->setSharedBy('initiator')
4506
-			->setSharedWith('[email protected]')
4507
-			->setShareOwner('owner')
4508
-			->setPermissions(Constants::PERMISSION_READ)
4509
-			->setNode($folder)
4510
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4511
-			->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4512
-			->setTarget('myTarget')
4513
-			->setNote('personal note')
4514
-			->setId(42);
4515
-
4516
-		$result[] = [
4517
-			[
4518
-				'id' => '42',
4519
-				'share_type' => IShare::TYPE_REMOTE_GROUP,
4520
-				'uid_owner' => 'initiator',
4521
-				'displayname_owner' => 'initiator',
4522
-				'permissions' => 1,
4523
-				'stime' => 946684862,
4524
-				'parent' => null,
4525
-				'expiration' => '2001-02-03 00:00:00',
4526
-				'token' => null,
4527
-				'uid_file_owner' => 'owner',
4528
-				'displayname_file_owner' => 'owner',
4529
-				'note' => 'personal note',
4530
-				'label' => '',
4531
-				'path' => 'folder',
4532
-				'item_type' => 'folder',
4533
-				'storage_id' => 'storageId',
4534
-				'storage' => 100,
4535
-				'item_source' => 2,
4536
-				'file_source' => 2,
4537
-				'file_parent' => 1,
4538
-				'file_target' => 'myTarget',
4539
-				'share_with' => '[email protected]',
4540
-				'share_with_displayname' => 'foobar',
4541
-				'mail_send' => 0,
4542
-				'mimetype' => 'myFolderMimeType',
4543
-				'has_preview' => false,
4544
-				'hide_download' => 0,
4545
-				'can_edit' => false,
4546
-				'can_delete' => false,
4547
-				'item_size' => 123456,
4548
-				'item_mtime' => 1234567890,
4549
-				'is-mount-root' => false,
4550
-				'mount-type' => '',
4551
-				'attributes' => null,
4552
-				'item_permissions' => 1,
4553
-			], $share, [], false
4554
-		];
4555
-
4556
-		// Circle with id, display name and avatar set by the Circles app
4557
-		$share = Server::get(IManager::class)->newShare();
4558
-		$share->setShareType(IShare::TYPE_CIRCLE)
4559
-			->setSharedBy('initiator')
4560
-			->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4561
-			->setSharedWithDisplayName('The display name')
4562
-			->setSharedWithAvatar('path/to/the/avatar')
4563
-			->setShareOwner('owner')
4564
-			->setPermissions(Constants::PERMISSION_READ)
4565
-			->setNode($folder)
4566
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4567
-			->setTarget('myTarget')
4568
-			->setId(42);
4569
-
4570
-		$result[] = [
4571
-			[
4572
-				'id' => '42',
4573
-				'share_type' => IShare::TYPE_CIRCLE,
4574
-				'uid_owner' => 'initiator',
4575
-				'displayname_owner' => 'initiator',
4576
-				'permissions' => 1,
4577
-				'attributes' => null,
4578
-				'stime' => 946684862,
4579
-				'parent' => null,
4580
-				'expiration' => null,
4581
-				'token' => null,
4582
-				'uid_file_owner' => 'owner',
4583
-				'displayname_file_owner' => 'owner',
4584
-				'note' => '',
4585
-				'label' => '',
4586
-				'path' => 'folder',
4587
-				'item_type' => 'folder',
4588
-				'storage_id' => 'storageId',
4589
-				'storage' => 100,
4590
-				'item_source' => 2,
4591
-				'file_source' => 2,
4592
-				'file_parent' => 1,
4593
-				'file_target' => 'myTarget',
4594
-				'share_with' => '4815162342',
4595
-				'share_with_displayname' => 'The display name',
4596
-				'share_with_avatar' => 'path/to/the/avatar',
4597
-				'mail_send' => 0,
4598
-				'mimetype' => 'myFolderMimeType',
4599
-				'has_preview' => false,
4600
-				'hide_download' => 0,
4601
-				'can_edit' => false,
4602
-				'can_delete' => false,
4603
-				'item_size' => 123456,
4604
-				'item_mtime' => 1234567890,
4605
-				'is-mount-root' => false,
4606
-				'mount-type' => '',
4607
-				'attributes' => null,
4608
-				'item_permissions' => 1,
4609
-			], $share, [], false
4610
-		];
4611
-
4612
-		// Circle with id set by the Circles app
4613
-		$share = Server::get(IManager::class)->newShare();
4614
-		$share->setShareType(IShare::TYPE_CIRCLE)
4615
-			->setSharedBy('initiator')
4616
-			->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4617
-			->setShareOwner('owner')
4618
-			->setPermissions(Constants::PERMISSION_READ)
4619
-			->setNode($folder)
4620
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4621
-			->setTarget('myTarget')
4622
-			->setId(42);
4623
-
4624
-		$result[] = [
4625
-			[
4626
-				'id' => '42',
4627
-				'share_type' => IShare::TYPE_CIRCLE,
4628
-				'uid_owner' => 'initiator',
4629
-				'displayname_owner' => 'initiator',
4630
-				'permissions' => 1,
4631
-				'stime' => 946684862,
4632
-				'parent' => null,
4633
-				'expiration' => null,
4634
-				'token' => null,
4635
-				'uid_file_owner' => 'owner',
4636
-				'displayname_file_owner' => 'owner',
4637
-				'note' => '',
4638
-				'label' => '',
4639
-				'path' => 'folder',
4640
-				'item_type' => 'folder',
4641
-				'storage_id' => 'storageId',
4642
-				'storage' => 100,
4643
-				'item_source' => 2,
4644
-				'file_source' => 2,
4645
-				'file_parent' => 1,
4646
-				'file_target' => 'myTarget',
4647
-				'share_with' => '4815162342',
4648
-				'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4649
-				'share_with_avatar' => '',
4650
-				'mail_send' => 0,
4651
-				'mimetype' => 'myFolderMimeType',
4652
-				'has_preview' => false,
4653
-				'hide_download' => 0,
4654
-				'can_edit' => false,
4655
-				'can_delete' => false,
4656
-				'item_size' => 123456,
4657
-				'item_mtime' => 1234567890,
4658
-				'is-mount-root' => false,
4659
-				'mount-type' => '',
4660
-				'attributes' => null,
4661
-				'item_permissions' => 1,
4662
-			], $share, [], false
4663
-		];
4664
-
4665
-		// Circle with id not set by the Circles app
4666
-		$share = Server::get(IManager::class)->newShare();
4667
-		$share->setShareType(IShare::TYPE_CIRCLE)
4668
-			->setSharedBy('initiator')
4669
-			->setSharedWith('Circle (Public circle, circleOwner)')
4670
-			->setShareOwner('owner')
4671
-			->setPermissions(Constants::PERMISSION_READ)
4672
-			->setNode($folder)
4673
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4674
-			->setTarget('myTarget')
4675
-			->setId(42);
4676
-
4677
-		$result[] = [
4678
-			[
4679
-				'id' => '42',
4680
-				'share_type' => IShare::TYPE_CIRCLE,
4681
-				'uid_owner' => 'initiator',
4682
-				'displayname_owner' => 'initiator',
4683
-				'permissions' => 1,
4684
-				'stime' => 946684862,
4685
-				'parent' => null,
4686
-				'expiration' => null,
4687
-				'token' => null,
4688
-				'uid_file_owner' => 'owner',
4689
-				'displayname_file_owner' => 'owner',
4690
-				'note' => '',
4691
-				'label' => '',
4692
-				'path' => 'folder',
4693
-				'item_type' => 'folder',
4694
-				'storage_id' => 'storageId',
4695
-				'storage' => 100,
4696
-				'item_source' => 2,
4697
-				'file_source' => 2,
4698
-				'file_parent' => 1,
4699
-				'file_target' => 'myTarget',
4700
-				'share_with' => 'Circle',
4701
-				'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4702
-				'share_with_avatar' => '',
4703
-				'mail_send' => 0,
4704
-				'mimetype' => 'myFolderMimeType',
4705
-				'has_preview' => false,
4706
-				'hide_download' => 0,
4707
-				'can_edit' => false,
4708
-				'can_delete' => false,
4709
-				'item_size' => 123456,
4710
-				'item_mtime' => 1234567890,
4711
-				'is-mount-root' => false,
4712
-				'mount-type' => '',
4713
-				'attributes' => null,
4714
-				'item_permissions' => 1,
4715
-			], $share, [], false
4716
-		];
4717
-
4718
-		$share = Server::get(IManager::class)->newShare();
4719
-		$share->setShareType(IShare::TYPE_USER)
4720
-			->setSharedBy('initiator')
4721
-			->setSharedWith('recipient')
4722
-			->setShareOwner('owner')
4723
-			->setPermissions(Constants::PERMISSION_READ)
4724
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4725
-			->setTarget('myTarget')
4726
-			->setNote('personal note')
4727
-			->setId(42);
4728
-
4729
-		$result[] = [
4730
-			[], $share, [], true
4731
-		];
4732
-
4733
-		$share = Server::get(IManager::class)->newShare();
4734
-		$share->setShareType(IShare::TYPE_EMAIL)
4735
-			->setSharedBy('initiator')
4736
-			->setSharedWith('[email protected]')
4737
-			->setShareOwner('owner')
4738
-			->setPermissions(Constants::PERMISSION_READ)
4739
-			->setNode($folder)
4740
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4741
-			->setTarget('myTarget')
4742
-			->setId(42)
4743
-			->setPassword('password');
4744
-
4745
-		$result[] = [
4746
-			[
4747
-				'id' => '42',
4748
-				'share_type' => IShare::TYPE_EMAIL,
4749
-				'uid_owner' => 'initiator',
4750
-				'displayname_owner' => 'initiator',
4751
-				'permissions' => 1,
4752
-				'stime' => 946684862,
4753
-				'parent' => null,
4754
-				'expiration' => null,
4755
-				'token' => null,
4756
-				'uid_file_owner' => 'owner',
4757
-				'displayname_file_owner' => 'owner',
4758
-				'note' => '',
4759
-				'label' => '',
4760
-				'path' => 'folder',
4761
-				'item_type' => 'folder',
4762
-				'storage_id' => 'storageId',
4763
-				'storage' => 100,
4764
-				'item_source' => 2,
4765
-				'file_source' => 2,
4766
-				'file_parent' => 1,
4767
-				'file_target' => 'myTarget',
4768
-				'share_with' => '[email protected]',
4769
-				'share_with_displayname' => 'mail display name',
4770
-				'mail_send' => 0,
4771
-				'mimetype' => 'myFolderMimeType',
4772
-				'has_preview' => false,
4773
-				'password' => 'password',
4774
-				'send_password_by_talk' => false,
4775
-				'hide_download' => 0,
4776
-				'can_edit' => false,
4777
-				'can_delete' => false,
4778
-				'password_expiration_time' => null,
4779
-				'item_size' => 123456,
4780
-				'item_mtime' => 1234567890,
4781
-				'is-mount-root' => false,
4782
-				'mount-type' => '',
4783
-				'attributes' => null,
4784
-				'item_permissions' => 1,
4785
-			], $share, [], false
4786
-		];
4787
-
4788
-		$share = Server::get(IManager::class)->newShare();
4789
-		$share->setShareType(IShare::TYPE_EMAIL)
4790
-			->setSharedBy('initiator')
4791
-			->setSharedWith('[email protected]')
4792
-			->setShareOwner('owner')
4793
-			->setPermissions(Constants::PERMISSION_READ)
4794
-			->setNode($folder)
4795
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4796
-			->setTarget('myTarget')
4797
-			->setId(42)
4798
-			->setPassword('password')
4799
-			->setSendPasswordByTalk(true);
4800
-
4801
-		$result[] = [
4802
-			[
4803
-				'id' => '42',
4804
-				'share_type' => IShare::TYPE_EMAIL,
4805
-				'uid_owner' => 'initiator',
4806
-				'displayname_owner' => 'initiator',
4807
-				'permissions' => 1,
4808
-				'stime' => 946684862,
4809
-				'parent' => null,
4810
-				'expiration' => null,
4811
-				'token' => null,
4812
-				'uid_file_owner' => 'owner',
4813
-				'displayname_file_owner' => 'owner',
4814
-				'note' => '',
4815
-				'label' => '',
4816
-				'path' => 'folder',
4817
-				'item_type' => 'folder',
4818
-				'storage_id' => 'storageId',
4819
-				'storage' => 100,
4820
-				'item_source' => 2,
4821
-				'file_source' => 2,
4822
-				'file_parent' => 1,
4823
-				'file_target' => 'myTarget',
4824
-				'share_with' => '[email protected]',
4825
-				'share_with_displayname' => 'mail display name',
4826
-				'mail_send' => 0,
4827
-				'mimetype' => 'myFolderMimeType',
4828
-				'has_preview' => false,
4829
-				'password' => 'password',
4830
-				'send_password_by_talk' => true,
4831
-				'hide_download' => 0,
4832
-				'can_edit' => false,
4833
-				'can_delete' => false,
4834
-				'password_expiration_time' => null,
4835
-				'item_size' => 123456,
4836
-				'item_mtime' => 1234567890,
4837
-				'is-mount-root' => false,
4838
-				'mount-type' => '',
4839
-				'attributes' => null,
4840
-				'item_permissions' => 1,
4841
-			], $share, [], false
4842
-		];
4843
-
4844
-		// Preview is available
4845
-		$share = Server::get(IManager::class)->newShare();
4846
-		$share->setShareType(IShare::TYPE_USER)
4847
-			->setSharedWith('recipient')
4848
-			->setSharedBy('initiator')
4849
-			->setShareOwner('currentUser')
4850
-			->setPermissions(Constants::PERMISSION_READ)
4851
-			->setNode($fileWithPreview)
4852
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4853
-			->setTarget('myTarget')
4854
-			->setNote('personal note')
4855
-			->setId(42);
4856
-
4857
-		$result[] = [
4858
-			[
4859
-				'id' => '42',
4860
-				'share_type' => IShare::TYPE_USER,
4861
-				'uid_owner' => 'initiator',
4862
-				'displayname_owner' => 'initiator',
4863
-				'permissions' => 1,
4864
-				'stime' => 946684862,
4865
-				'parent' => null,
4866
-				'expiration' => null,
4867
-				'token' => null,
4868
-				'uid_file_owner' => 'currentUser',
4869
-				'displayname_file_owner' => 'currentUser',
4870
-				'note' => 'personal note',
4871
-				'label' => '',
4872
-				'path' => 'fileWithPreview',
4873
-				'item_type' => 'file',
4874
-				'storage_id' => 'storageId',
4875
-				'storage' => 100,
4876
-				'item_source' => 4,
4877
-				'file_source' => 4,
4878
-				'file_parent' => 1,
4879
-				'file_target' => 'myTarget',
4880
-				'share_with' => 'recipient',
4881
-				'share_with_displayname' => 'recipient',
4882
-				'share_with_displayname_unique' => 'recipient',
4883
-				'mail_send' => 0,
4884
-				'mimetype' => 'mimeWithPreview',
4885
-				'has_preview' => true,
4886
-				'hide_download' => 0,
4887
-				'can_edit' => true,
4888
-				'can_delete' => true,
4889
-				'item_size' => 123456,
4890
-				'item_mtime' => 1234567890,
4891
-				'is-mount-root' => false,
4892
-				'mount-type' => '',
4893
-				'attributes' => null,
4894
-				'item_permissions' => 11,
4895
-			], $share, [], false
4896
-		];
4897
-
4898
-		return $result;
4899
-	}
4900
-
4901
-	/**
4902
-	 * @dataProvider dataFormatShare
4903
-	 *
4904
-	 * @param array $expects
4905
-	 * @param IShare $share
4906
-	 * @param array $users
4907
-	 * @param $exception
4908
-	 */
4909
-	public function testFormatShare(array $expects, IShare $share, array $users, $exception): void {
4910
-		$this->userManager->method('get')->willReturnMap($users);
4911
-
4912
-		$recipientGroup = $this->createMock(IGroup::class);
4913
-		$recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
4914
-		$this->groupManager->method('get')->willReturnMap([
4915
-			['recipientGroup', $recipientGroup],
4916
-		]);
4917
-
4918
-		$this->urlGenerator->method('linkToRouteAbsolute')
4919
-			->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
4920
-			->willReturn('myLink');
4921
-
4922
-		$this->rootFolder->method('getUserFolder')
4923
-			->with($this->currentUser)
4924
-			->willReturnSelf();
4925
-		$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
4926
-
4927
-		if (!$exception) {
4928
-			$this->rootFolder->method('getFirstNodeById')
4929
-				->with($share->getNodeId())
4930
-				->willReturn($share->getNode());
4931
-
4932
-			$this->rootFolder->method('getRelativePath')
4933
-				->with($share->getNode()->getPath())
4934
-				->willReturnArgument(0);
4935
-		}
4936
-
4937
-		$cm = $this->createMock(\OCP\Contacts\IManager::class);
4938
-		$this->overwriteService(\OCP\Contacts\IManager::class, $cm);
4939
-
4940
-		$cm->method('search')
4941
-			->willReturnMap([
4942
-				['[email protected]', ['CLOUD'], [
4943
-					'limit' => 1,
4944
-					'enumeration' => false,
4945
-					'strict_search' => true,
4946
-				],
4947
-					[
4948
-						[
4949
-							'CLOUD' => [
4950
-								'[email protected]',
4951
-							],
4952
-							'FN' => 'foobar',
4953
-						],
4954
-					],
4955
-				],
4956
-				['[email protected]', ['EMAIL'], [
4957
-					'limit' => 1,
4958
-					'enumeration' => false,
4959
-					'strict_search' => true,
4960
-				],
4961
-					[
4962
-						[
4963
-							'EMAIL' => [
4964
-								'[email protected]',
4965
-							],
4966
-							'FN' => 'mail display name',
4967
-						],
4968
-					],
4969
-				],
4970
-			]);
4971
-
4972
-		try {
4973
-			$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
4974
-			$this->assertFalse($exception);
4975
-			$this->assertEquals($expects, $result);
4976
-		} catch (NotFoundException $e) {
4977
-			$this->assertTrue($exception);
4978
-		}
4979
-	}
4980
-
4981
-	public function dataFormatRoomShare() {
4982
-		$file = $this->getMockBuilder(File::class)->getMock();
4983
-		$parent = $this->getMockBuilder(Folder::class)->getMock();
4984
-
4985
-		$file->method('getMimeType')->willReturn('myMimeType');
4986
-
4987
-		$file->method('getPath')->willReturn('file');
4988
-
4989
-		$parent->method('getId')->willReturn(1);
4990
-		$file->method('getId')->willReturn(3);
4991
-
4992
-		$file->method('getParent')->willReturn($parent);
4993
-
4994
-		$file->method('getSize')->willReturn(123456);
4995
-		$file->method('getMTime')->willReturn(1234567890);
4996
-
4997
-		$mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
4998
-		$mountPoint->method('getMountType')->willReturn('');
4999
-		$file->method('getMountPoint')->willReturn($mountPoint);
5000
-
5001
-		$cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
5002
-		$cache->method('getNumericStorageId')->willReturn(100);
5003
-		$storage = $this->createMock(IStorage::class);
5004
-		$storage->method('getId')->willReturn('storageId');
5005
-		$storage->method('getCache')->willReturn($cache);
5006
-
5007
-		$file->method('getStorage')->willReturn($storage);
5008
-
5009
-		$result = [];
5010
-
5011
-		$share = Server::get(IManager::class)->newShare();
5012
-		$share->setShareType(IShare::TYPE_ROOM)
5013
-			->setSharedWith('recipientRoom')
5014
-			->setSharedBy('initiator')
5015
-			->setShareOwner('owner')
5016
-			->setPermissions(Constants::PERMISSION_READ)
5017
-			->setNode($file)
5018
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
5019
-			->setTarget('myTarget')
5020
-			->setNote('personal note')
5021
-			->setId(42);
5022
-
5023
-		$result[] = [
5024
-			[
5025
-				'id' => '42',
5026
-				'share_type' => IShare::TYPE_ROOM,
5027
-				'uid_owner' => 'initiator',
5028
-				'displayname_owner' => 'initiator',
5029
-				'permissions' => 1,
5030
-				'stime' => 946684862,
5031
-				'parent' => null,
5032
-				'expiration' => null,
5033
-				'token' => null,
5034
-				'uid_file_owner' => 'owner',
5035
-				'displayname_file_owner' => 'owner',
5036
-				'note' => 'personal note',
5037
-				'path' => 'file',
5038
-				'item_type' => 'file',
5039
-				'storage_id' => 'storageId',
5040
-				'storage' => 100,
5041
-				'item_source' => 3,
5042
-				'file_source' => 3,
5043
-				'file_parent' => 1,
5044
-				'file_target' => 'myTarget',
5045
-				'share_with' => 'recipientRoom',
5046
-				'share_with_displayname' => '',
5047
-				'mail_send' => 0,
5048
-				'mimetype' => 'myMimeType',
5049
-				'has_preview' => false,
5050
-				'hide_download' => 0,
5051
-				'label' => '',
5052
-				'can_edit' => false,
5053
-				'can_delete' => false,
5054
-				'item_size' => 123456,
5055
-				'item_mtime' => 1234567890,
5056
-				'is-mount-root' => false,
5057
-				'mount-type' => '',
5058
-				'attributes' => null,
5059
-				'item_permissions' => 1,
5060
-			], $share, false, []
5061
-		];
5062
-
5063
-		$share = Server::get(IManager::class)->newShare();
5064
-		$share->setShareType(IShare::TYPE_ROOM)
5065
-			->setSharedWith('recipientRoom')
5066
-			->setSharedBy('initiator')
5067
-			->setShareOwner('owner')
5068
-			->setPermissions(Constants::PERMISSION_READ)
5069
-			->setNode($file)
5070
-			->setShareTime(new \DateTime('2000-01-01T00:01:02'))
5071
-			->setTarget('myTarget')
5072
-			->setNote('personal note')
5073
-			->setId(42);
5074
-
5075
-		$result[] = [
5076
-			[
5077
-				'id' => '42',
5078
-				'share_type' => IShare::TYPE_ROOM,
5079
-				'uid_owner' => 'initiator',
5080
-				'displayname_owner' => 'initiator',
5081
-				'permissions' => 1,
5082
-				'stime' => 946684862,
5083
-				'parent' => null,
5084
-				'expiration' => null,
5085
-				'token' => null,
5086
-				'uid_file_owner' => 'owner',
5087
-				'displayname_file_owner' => 'owner',
5088
-				'note' => 'personal note',
5089
-				'path' => 'file',
5090
-				'item_type' => 'file',
5091
-				'storage_id' => 'storageId',
5092
-				'storage' => 100,
5093
-				'item_source' => 3,
5094
-				'file_source' => 3,
5095
-				'file_parent' => 1,
5096
-				'file_target' => 'myTarget',
5097
-				'share_with' => 'recipientRoom',
5098
-				'share_with_displayname' => 'recipientRoomName',
5099
-				'mail_send' => 0,
5100
-				'mimetype' => 'myMimeType',
5101
-				'has_preview' => false,
5102
-				'hide_download' => 0,
5103
-				'label' => '',
5104
-				'can_edit' => false,
5105
-				'can_delete' => false,
5106
-				'item_size' => 123456,
5107
-				'item_mtime' => 1234567890,
5108
-				'is-mount-root' => false,
5109
-				'mount-type' => '',
5110
-				'attributes' => null,
5111
-				'item_permissions' => 9,
5112
-			], $share, true, [
5113
-				'share_with_displayname' => 'recipientRoomName'
5114
-			]
5115
-		];
5116
-
5117
-		return $result;
5118
-	}
5119
-
5120
-	/**
5121
-	 * @dataProvider dataFormatRoomShare
5122
-	 *
5123
-	 * @param array $expects
5124
-	 * @param IShare $share
5125
-	 * @param bool $helperAvailable
5126
-	 * @param array $formatShareByHelper
5127
-	 */
5128
-	public function testFormatRoomShare(array $expects, IShare $share, bool $helperAvailable, array $formatShareByHelper): void {
5129
-		$this->rootFolder->method('getUserFolder')
5130
-			->with($this->currentUser)
5131
-			->willReturnSelf();
5132
-
5133
-		$this->rootFolder->method('getFirstNodeById')
5134
-			->with($share->getNodeId())
5135
-			->willReturn($share->getNode());
5136
-
5137
-		$this->rootFolder->method('getRelativePath')
5138
-			->with($share->getNode()->getPath())
5139
-			->willReturnArgument(0);
5140
-
5141
-		if (!$helperAvailable) {
5142
-			$this->appManager->method('isEnabledForUser')
5143
-				->with('spreed')
5144
-				->willReturn(false);
5145
-		} else {
5146
-			$this->appManager->method('isEnabledForUser')
5147
-				->with('spreed')
5148
-				->willReturn(true);
5149
-
5150
-			// This is not possible anymore with PHPUnit 10+
5151
-			// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
5152
-			// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
5153
-			$helper = $this->getMockBuilder(\stdClass::class)
5154
-				->addMethods(['formatShare', 'canAccessShare'])
5155
-				->getMock();
5156
-			$helper->method('formatShare')
5157
-				->with($share)
5158
-				->willReturn($formatShareByHelper);
5159
-			$helper->method('canAccessShare')
5160
-				->with($share)
5161
-				->willReturn(true);
5162
-
5163
-			$this->serverContainer->method('get')
5164
-				->with('\OCA\Talk\Share\Helper\ShareAPIController')
5165
-				->willReturn($helper);
5166
-		}
5167
-
5168
-		$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
5169
-		$this->assertEquals($expects, $result);
5170
-	}
5171
-
5172
-	/**
5173
-	 * @return list{Folder, Folder}
5174
-	 */
5175
-	private function getNonSharedUserFolder(): array {
5176
-		$node = $this->getMockBuilder(Folder::class)->getMock();
5177
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
5178
-		$storage = $this->createMock(IStorage::class);
5179
-		$storage->method('instanceOfStorage')
5180
-			->willReturnMap([
5181
-				['OCA\Files_Sharing\External\Storage', false],
5182
-				['OCA\Files_Sharing\SharedStorage', false],
5183
-			]);
5184
-		$userFolder->method('getStorage')->willReturn($storage);
5185
-		$node->method('getStorage')->willReturn($storage);
5186
-		$node->method('getId')->willReturn(42);
5187
-		$user = $this->createMock(IUser::class);
5188
-		$user->method('getUID')->willReturn($this->currentUser);
5189
-		$node->method('getOwner')->willReturn($user);
5190
-		return [$userFolder, $node];
5191
-	}
5192
-
5193
-	/**
5194
-	 * @return list{Folder, File}
5195
-	 */
5196
-	private function getNonSharedUserFile(): array {
5197
-		$node = $this->getMockBuilder(File::class)->getMock();
5198
-		$userFolder = $this->getMockBuilder(Folder::class)->getMock();
5199
-		$storage = $this->createMock(IStorage::class);
5200
-		$storage->method('instanceOfStorage')
5201
-			->willReturnMap([
5202
-				['OCA\Files_Sharing\External\Storage', false],
5203
-				['OCA\Files_Sharing\SharedStorage', false],
5204
-			]);
5205
-		$userFolder->method('getStorage')->willReturn($storage);
5206
-		$node->method('getStorage')->willReturn($storage);
5207
-		$node->method('getId')->willReturn(42);
5208
-		return [$userFolder, $node];
5209
-	}
5210
-
5211
-	public function testPopulateTags(): void {
5212
-		$tagger = $this->createMock(ITags::class);
5213
-		$this->tagManager->method('load')
5214
-			->with('files')
5215
-			->willReturn($tagger);
5216
-		$data = [
5217
-			['file_source' => 10],
5218
-			['file_source' => 22, 'foo' => 'bar'],
5219
-			['file_source' => 42, 'x' => 'y'],
5220
-		];
5221
-		$tags = [
5222
-			10 => ['tag3'],
5223
-			42 => ['tag1', 'tag2'],
5224
-		];
5225
-		$tagger->method('getTagsForObjects')
5226
-			->with([10, 22, 42])
5227
-			->willReturn($tags);
5228
-
5229
-		$result = self::invokePrivate($this->ocs, 'populateTags', [$data]);
5230
-		$this->assertSame([
5231
-			['file_source' => 10, 'tags' => ['tag3']],
5232
-			['file_source' => 22, 'foo' => 'bar', 'tags' => []],
5233
-			['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
5234
-		], $result);
5235
-	}
2129
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2130
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($file);
2131
+        $this->rootFolder->method('getById')
2132
+            ->willReturn([]);
2133
+
2134
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2135
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2136
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2137
+
2138
+        $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
2139
+    }
2140
+
2141
+    public function testCreateShareLinkPublicUploadFolder(): void {
2142
+        $ocs = $this->mockFormatShare();
2143
+
2144
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2145
+        $path->method('getId')->willReturn(1);
2146
+        $storage = $this->createMock(IStorage::class);
2147
+        $storage->method('instanceOfStorage')
2148
+            ->willReturnMap([
2149
+                ['OCA\Files_Sharing\External\Storage', false],
2150
+                ['OCA\Files_Sharing\SharedStorage', false],
2151
+            ]);
2152
+        $path->method('getStorage')->willReturn($storage);
2153
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2154
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2155
+        $this->rootFolder->method('getById')
2156
+            ->willReturn([]);
2157
+
2158
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2159
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2160
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2161
+
2162
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2163
+            $this->callback(function (IShare $share) use ($path) {
2164
+                return $share->getNode() === $path &&
2165
+                    $share->getShareType() === IShare::TYPE_LINK &&
2166
+                    $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2167
+                    $share->getSharedBy() === 'currentUser' &&
2168
+                    $share->getPassword() === null &&
2169
+                    $share->getExpirationDate() === null;
2170
+            })
2171
+        )->willReturnArgument(0);
2172
+
2173
+        $expected = new DataResponse([]);
2174
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', '', null, '');
2175
+
2176
+        $this->assertInstanceOf(get_class($expected), $result);
2177
+        $this->assertEquals($expected->getData(), $result->getData());
2178
+    }
2179
+
2180
+    public function testCreateShareLinkPassword(): void {
2181
+        $ocs = $this->mockFormatShare();
2182
+
2183
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2184
+        $path->method('getId')->willReturn(42);
2185
+        $storage = $this->createMock(IStorage::class);
2186
+        $storage->method('instanceOfStorage')
2187
+            ->willReturnMap([
2188
+                ['OCA\Files_Sharing\External\Storage', false],
2189
+                ['OCA\Files_Sharing\SharedStorage', false],
2190
+            ]);
2191
+        $path->method('getStorage')->willReturn($storage);
2192
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2193
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2194
+        $this->rootFolder->method('getById')
2195
+            ->willReturn([]);
2196
+
2197
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2198
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2199
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2200
+
2201
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2202
+            $this->callback(function (IShare $share) use ($path) {
2203
+                return $share->getNode() === $path
2204
+                && $share->getShareType() === IShare::TYPE_LINK
2205
+                && $share->getPermissions() === Constants::PERMISSION_READ // publicUpload was set to false
2206
+                && $share->getSharedBy() === 'currentUser'
2207
+                && $share->getPassword() === 'password'
2208
+                && $share->getExpirationDate() === null;
2209
+            })
2210
+        )->willReturnArgument(0);
2211
+
2212
+        $expected = new DataResponse([]);
2213
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_READ, IShare::TYPE_LINK, null, 'false', 'password', null, '');
2214
+
2215
+        $this->assertInstanceOf(get_class($expected), $result);
2216
+        $this->assertEquals($expected->getData(), $result->getData());
2217
+    }
2218
+
2219
+    public function testCreateShareLinkSendPasswordByTalk(): void {
2220
+        $ocs = $this->mockFormatShare();
2221
+
2222
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2223
+        $path->method('getId')->willReturn(42);
2224
+        $storage = $this->createMock(IStorage::class);
2225
+        $storage->method('instanceOfStorage')
2226
+            ->willReturnMap([
2227
+                ['OCA\Files_Sharing\External\Storage', false],
2228
+                ['OCA\Files_Sharing\SharedStorage', false],
2229
+            ]);
2230
+        $path->method('getStorage')->willReturn($storage);
2231
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2232
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2233
+        $this->rootFolder->method('getById')
2234
+            ->willReturn([]);
2235
+
2236
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2237
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2238
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2239
+
2240
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
2241
+
2242
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2243
+            $this->callback(function (IShare $share) use ($path) {
2244
+                return $share->getNode() === $path &&
2245
+                $share->getShareType() === IShare::TYPE_LINK &&
2246
+                $share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) &&
2247
+                $share->getSharedBy() === 'currentUser' &&
2248
+                $share->getPassword() === 'password' &&
2249
+                $share->getSendPasswordByTalk() === true &&
2250
+                $share->getExpirationDate() === null;
2251
+            })
2252
+        )->willReturnArgument(0);
2253
+
2254
+        $expected = new DataResponse([]);
2255
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', 'password', 'true', '');
2256
+
2257
+        $this->assertInstanceOf(get_class($expected), $result);
2258
+        $this->assertEquals($expected->getData(), $result->getData());
2259
+    }
2260
+
2261
+
2262
+    public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled(): void {
2263
+        $this->expectException(OCSForbiddenException::class);
2264
+        $this->expectExceptionMessage('Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled');
2265
+
2266
+        $ocs = $this->mockFormatShare();
2267
+
2268
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2269
+        $path->method('getId')->willReturn(42);
2270
+        $storage = $this->createMock(IStorage::class);
2271
+        $storage->method('instanceOfStorage')
2272
+            ->willReturnMap([
2273
+                ['OCA\Files_Sharing\External\Storage', false],
2274
+                ['OCA\Files_Sharing\SharedStorage', false],
2275
+            ]);
2276
+        $path->method('getStorage')->willReturn($storage);
2277
+        $path->method('getPath')->willReturn('valid-path');
2278
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2279
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2280
+        $this->rootFolder->method('getById')
2281
+            ->willReturn([]);
2282
+
2283
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2284
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2285
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2286
+
2287
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
2288
+
2289
+        $this->shareManager->expects($this->never())->method('createShare');
2290
+
2291
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', '');
2292
+    }
2293
+
2294
+    public function testCreateShareValidExpireDate(): void {
2295
+        $ocs = $this->mockFormatShare();
2296
+
2297
+        $this->request
2298
+            ->method('getParam')
2299
+            ->willReturnMap([
2300
+                ['path', null, 'valid-path'],
2301
+                ['shareType', '-1', IShare::TYPE_LINK],
2302
+                ['publicUpload', null, 'false'],
2303
+                ['expireDate', '', '2000-01-01'],
2304
+                ['password', '', ''],
2305
+            ]);
2306
+
2307
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2308
+        $path->method('getId')->willReturn(42);
2309
+        $storage = $this->createMock(IStorage::class);
2310
+        $storage->method('instanceOfStorage')
2311
+            ->willReturnMap([
2312
+                ['OCA\Files_Sharing\External\Storage', false],
2313
+                ['OCA\Files_Sharing\SharedStorage', false],
2314
+            ]);
2315
+        $path->method('getStorage')->willReturn($storage);
2316
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2317
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2318
+        $this->rootFolder->method('getById')
2319
+            ->willReturn([]);
2320
+
2321
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2322
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2323
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2324
+
2325
+        $this->shareManager->expects($this->once())->method('createShare')->with(
2326
+            $this->callback(function (IShare $share) use ($path) {
2327
+                $date = new \DateTime('2000-01-01');
2328
+                $date->setTime(0, 0, 0);
2329
+
2330
+                return $share->getNode() === $path &&
2331
+                $share->getShareType() === IShare::TYPE_LINK &&
2332
+                $share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE &&
2333
+                $share->getSharedBy() === 'currentUser' &&
2334
+                $share->getPassword() === null &&
2335
+                $share->getExpirationDate() == $date;
2336
+            })
2337
+        )->willReturnArgument(0);
2338
+
2339
+        $expected = new DataResponse([]);
2340
+        $result = $ocs->createShare('valid-path', null, IShare::TYPE_LINK, null, 'false', '', null, '2000-01-01');
2341
+
2342
+        $this->assertInstanceOf(get_class($expected), $result);
2343
+        $this->assertEquals($expected->getData(), $result->getData());
2344
+    }
2345
+
2346
+
2347
+    public function testCreateShareInvalidExpireDate(): void {
2348
+        $this->expectException(OCSNotFoundException::class);
2349
+        $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
2350
+
2351
+        $ocs = $this->mockFormatShare();
2352
+
2353
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2354
+        $path->method('getId')->willReturn(42);
2355
+        $storage = $this->createMock(IStorage::class);
2356
+        $storage->method('instanceOfStorage')
2357
+            ->willReturnMap([
2358
+                ['OCA\Files_Sharing\External\Storage', false],
2359
+                ['OCA\Files_Sharing\SharedStorage', false],
2360
+            ]);
2361
+        $path->method('getStorage')->willReturn($storage);
2362
+        $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
2363
+        $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
2364
+        $this->rootFolder->method('getById')
2365
+            ->willReturn([]);
2366
+
2367
+        $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare());
2368
+        $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
2369
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2370
+
2371
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3');
2372
+    }
2373
+
2374
+    public function testCreateShareRemote(): void {
2375
+        $share = $this->newShare();
2376
+        $this->shareManager->method('newShare')->willReturn($share);
2377
+
2378
+        /** @var ShareAPIController $ocs */
2379
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
2380
+            ->setConstructorArgs([
2381
+                $this->appName,
2382
+                $this->request,
2383
+                $this->shareManager,
2384
+                $this->groupManager,
2385
+                $this->userManager,
2386
+                $this->rootFolder,
2387
+                $this->urlGenerator,
2388
+                $this->l,
2389
+                $this->config,
2390
+                $this->appManager,
2391
+                $this->serverContainer,
2392
+                $this->userStatusManager,
2393
+                $this->previewManager,
2394
+                $this->dateTimeZone,
2395
+                $this->logger,
2396
+                $this->factory,
2397
+                $this->mailer,
2398
+                $this->tagManager,
2399
+                $this->currentUser,
2400
+            ])->onlyMethods(['formatShare'])
2401
+            ->getMock();
2402
+
2403
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2404
+        $this->rootFolder->expects($this->exactly(2))
2405
+            ->method('getUserFolder')
2406
+            ->with('currentUser')
2407
+            ->willReturn($userFolder);
2408
+
2409
+        $userFolder->expects($this->once())
2410
+            ->method('get')
2411
+            ->with('valid-path')
2412
+            ->willReturn($path);
2413
+        $userFolder->method('getById')
2414
+            ->willReturn([]);
2415
+
2416
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
2417
+
2418
+        $path->expects($this->once())
2419
+            ->method('lock')
2420
+            ->with(ILockingProvider::LOCK_SHARED);
2421
+
2422
+        $this->shareManager->method('createShare')
2423
+            ->with($this->callback(function (IShare $share) use ($path) {
2424
+                return $share->getNode() === $path &&
2425
+                    $share->getPermissions() === (
2426
+                        Constants::PERMISSION_ALL &
2427
+                        ~Constants::PERMISSION_DELETE &
2428
+                        ~Constants::PERMISSION_CREATE
2429
+                    ) &&
2430
+                    $share->getShareType() === IShare::TYPE_REMOTE &&
2431
+                    $share->getSharedWith() === '[email protected]' &&
2432
+                    $share->getSharedBy() === 'currentUser';
2433
+            }))
2434
+            ->willReturnArgument(0);
2435
+
2436
+        $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
2437
+
2438
+        $expected = new DataResponse([]);
2439
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, '[email protected]');
2440
+
2441
+        $this->assertInstanceOf(get_class($expected), $result);
2442
+        $this->assertEquals($expected->getData(), $result->getData());
2443
+    }
2444
+
2445
+    public function testCreateShareRemoteGroup(): void {
2446
+        $share = $this->newShare();
2447
+        $this->shareManager->method('newShare')->willReturn($share);
2448
+
2449
+        /** @var ShareAPIController $ocs */
2450
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
2451
+            ->setConstructorArgs([
2452
+                $this->appName,
2453
+                $this->request,
2454
+                $this->shareManager,
2455
+                $this->groupManager,
2456
+                $this->userManager,
2457
+                $this->rootFolder,
2458
+                $this->urlGenerator,
2459
+                $this->l,
2460
+                $this->config,
2461
+                $this->appManager,
2462
+                $this->serverContainer,
2463
+                $this->userStatusManager,
2464
+                $this->previewManager,
2465
+                $this->dateTimeZone,
2466
+                $this->logger,
2467
+                $this->factory,
2468
+                $this->mailer,
2469
+                $this->tagManager,
2470
+                $this->currentUser,
2471
+            ])->onlyMethods(['formatShare'])
2472
+            ->getMock();
2473
+
2474
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2475
+        $this->rootFolder->expects($this->exactly(2))
2476
+            ->method('getUserFolder')
2477
+            ->with('currentUser')
2478
+            ->willReturn($userFolder);
2479
+
2480
+        $userFolder->expects($this->once())
2481
+            ->method('get')
2482
+            ->with('valid-path')
2483
+            ->willReturn($path);
2484
+        $userFolder->method('getById')
2485
+            ->willReturn([]);
2486
+
2487
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
2488
+
2489
+        $path->expects($this->once())
2490
+            ->method('lock')
2491
+            ->with(ILockingProvider::LOCK_SHARED);
2492
+
2493
+        $this->shareManager->method('createShare')
2494
+            ->with($this->callback(function (IShare $share) use ($path) {
2495
+                return $share->getNode() === $path &&
2496
+                    $share->getPermissions() === (
2497
+                        Constants::PERMISSION_ALL &
2498
+                        ~Constants::PERMISSION_DELETE &
2499
+                        ~Constants::PERMISSION_CREATE
2500
+                    ) &&
2501
+                    $share->getShareType() === IShare::TYPE_REMOTE_GROUP &&
2502
+                    $share->getSharedWith() === '[email protected]' &&
2503
+                    $share->getSharedBy() === 'currentUser';
2504
+            }))
2505
+            ->willReturnArgument(0);
2506
+
2507
+        $this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true);
2508
+
2509
+        $expected = new DataResponse([]);
2510
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, '[email protected]');
2511
+
2512
+        $this->assertInstanceOf(get_class($expected), $result);
2513
+        $this->assertEquals($expected->getData(), $result->getData());
2514
+    }
2515
+
2516
+    public function testCreateShareRoom(): void {
2517
+        $ocs = $this->mockFormatShare();
2518
+
2519
+        $share = $this->newShare();
2520
+        $this->shareManager->method('newShare')->willReturn($share);
2521
+
2522
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2523
+        $this->rootFolder->expects($this->exactly(2))
2524
+            ->method('getUserFolder')
2525
+            ->with('currentUser')
2526
+            ->willReturn($userFolder);
2527
+
2528
+        $userFolder->expects($this->once())
2529
+            ->method('get')
2530
+            ->with('valid-path')
2531
+            ->willReturn($path);
2532
+        $userFolder->method('getById')
2533
+            ->willReturn([]);
2534
+
2535
+        $path->expects($this->once())
2536
+            ->method('lock')
2537
+            ->with(ILockingProvider::LOCK_SHARED);
2538
+
2539
+        $this->appManager->method('isEnabledForUser')
2540
+            ->with('spreed')
2541
+            ->willReturn(true);
2542
+
2543
+        // This is not possible anymore with PHPUnit 10+
2544
+        // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2545
+        // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2546
+        $helper = $this->getMockBuilder(\stdClass::class)
2547
+            ->addMethods(['createShare'])
2548
+            ->getMock();
2549
+        $helper->method('createShare')
2550
+            ->with(
2551
+                $share,
2552
+                'recipientRoom',
2553
+                Constants::PERMISSION_ALL &
2554
+                ~Constants::PERMISSION_DELETE &
2555
+                ~Constants::PERMISSION_CREATE,
2556
+                ''
2557
+            )->willReturnCallback(
2558
+                function ($share): void {
2559
+                    $share->setSharedWith('recipientRoom');
2560
+                    $share->setPermissions(Constants::PERMISSION_ALL);
2561
+                }
2562
+            );
2563
+
2564
+        $this->serverContainer->method('get')
2565
+            ->with('\OCA\Talk\Share\Helper\ShareAPIController')
2566
+            ->willReturn($helper);
2567
+
2568
+        $this->shareManager->method('createShare')
2569
+            ->with($this->callback(function (IShare $share) use ($path) {
2570
+                return $share->getNode() === $path
2571
+                    && $share->getPermissions() === Constants::PERMISSION_ALL
2572
+                    && $share->getShareType() === IShare::TYPE_ROOM
2573
+                    && $share->getSharedWith() === 'recipientRoom'
2574
+                    && $share->getSharedBy() === 'currentUser';
2575
+            }))
2576
+            ->willReturnArgument(0);
2577
+
2578
+        $expected = new DataResponse([]);
2579
+        $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2580
+
2581
+        $this->assertInstanceOf(get_class($expected), $result);
2582
+        $this->assertEquals($expected->getData(), $result->getData());
2583
+    }
2584
+
2585
+
2586
+    public function testCreateShareRoomHelperNotAvailable(): void {
2587
+        $this->expectException(OCSForbiddenException::class);
2588
+        $this->expectExceptionMessage('Sharing valid-path failed because the back end does not support room shares');
2589
+
2590
+        $ocs = $this->mockFormatShare();
2591
+
2592
+        $share = $this->newShare();
2593
+        $this->shareManager->method('newShare')->willReturn($share);
2594
+
2595
+        [$userFolder, $path] = $this->getNonSharedUserFolder();
2596
+        $this->rootFolder->method('getUserFolder')
2597
+            ->with('currentUser')
2598
+            ->willReturn($userFolder);
2599
+
2600
+        $path->method('getPath')->willReturn('valid-path');
2601
+        $userFolder->expects($this->once())
2602
+            ->method('get')
2603
+            ->with('valid-path')
2604
+            ->willReturn($path);
2605
+        $userFolder->method('getById')
2606
+            ->willReturn([]);
2607
+
2608
+        $path->expects($this->once())
2609
+            ->method('lock')
2610
+            ->with(ILockingProvider::LOCK_SHARED);
2611
+
2612
+        $this->appManager->method('isEnabledForUser')
2613
+            ->with('spreed')
2614
+            ->willReturn(false);
2615
+
2616
+        $this->shareManager->expects($this->never())->method('createShare');
2617
+
2618
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2619
+    }
2620
+
2621
+
2622
+    public function testCreateShareRoomHelperThrowException(): void {
2623
+        $this->expectException(OCSNotFoundException::class);
2624
+        $this->expectExceptionMessage('Exception thrown by the helper');
2625
+
2626
+        $ocs = $this->mockFormatShare();
2627
+
2628
+        $share = $this->newShare();
2629
+        $share->setSharedBy('currentUser');
2630
+        $this->shareManager->method('newShare')->willReturn($share);
2631
+
2632
+        [$userFolder, $path] = $this->getNonSharedUserFile();
2633
+        $this->rootFolder->method('getUserFolder')
2634
+            ->with('currentUser')
2635
+            ->willReturn($userFolder);
2636
+
2637
+        $userFolder->expects($this->once())
2638
+            ->method('get')
2639
+            ->with('valid-path')
2640
+            ->willReturn($path);
2641
+        $userFolder->method('getById')
2642
+            ->willReturn([]);
2643
+
2644
+        $path->expects($this->once())
2645
+            ->method('lock')
2646
+            ->with(ILockingProvider::LOCK_SHARED);
2647
+
2648
+        $this->appManager->method('isEnabledForUser')
2649
+            ->with('spreed')
2650
+            ->willReturn(true);
2651
+
2652
+        // This is not possible anymore with PHPUnit 10+
2653
+        // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2654
+        // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2655
+        $helper = $this->getMockBuilder(\stdClass::class)
2656
+            ->addMethods(['createShare'])
2657
+            ->getMock();
2658
+        $helper->method('createShare')
2659
+            ->with(
2660
+                $share,
2661
+                'recipientRoom',
2662
+                Constants::PERMISSION_ALL & ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE),
2663
+                ''
2664
+            )->willReturnCallback(
2665
+                function ($share): void {
2666
+                    throw new OCSNotFoundException('Exception thrown by the helper');
2667
+                }
2668
+            );
2669
+
2670
+        $this->serverContainer->method('get')
2671
+            ->with('\OCA\Talk\Share\Helper\ShareAPIController')
2672
+            ->willReturn($helper);
2673
+
2674
+        $this->shareManager->expects($this->never())->method('createShare');
2675
+
2676
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
2677
+    }
2678
+
2679
+    /**
2680
+     * Test for https://github.com/owncloud/core/issues/22587
2681
+     * TODO: Remove once proper solution is in place
2682
+     */
2683
+    public function testCreateReshareOfFederatedMountNoDeletePermissions(): void {
2684
+        $share = Server::get(IManager::class)->newShare();
2685
+        $this->shareManager->method('newShare')->willReturn($share);
2686
+
2687
+        /** @var ShareAPIController&MockObject $ocs */
2688
+        $ocs = $this->getMockBuilder(ShareAPIController::class)
2689
+            ->setConstructorArgs([
2690
+                $this->appName,
2691
+                $this->request,
2692
+                $this->shareManager,
2693
+                $this->groupManager,
2694
+                $this->userManager,
2695
+                $this->rootFolder,
2696
+                $this->urlGenerator,
2697
+                $this->l,
2698
+                $this->config,
2699
+                $this->appManager,
2700
+                $this->serverContainer,
2701
+                $this->userStatusManager,
2702
+                $this->previewManager,
2703
+                $this->dateTimeZone,
2704
+                $this->logger,
2705
+                $this->factory,
2706
+                $this->mailer,
2707
+                $this->tagManager,
2708
+                $this->currentUser,
2709
+            ])->onlyMethods(['formatShare'])
2710
+            ->getMock();
2711
+
2712
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
2713
+        $this->rootFolder->expects($this->exactly(2))
2714
+            ->method('getUserFolder')
2715
+            ->with('currentUser')
2716
+            ->willReturn($userFolder);
2717
+
2718
+        $path = $this->getMockBuilder(Folder::class)->getMock();
2719
+        $path->method('getId')->willReturn(42);
2720
+
2721
+        $storage = $this->createMock(IStorage::class);
2722
+        $storage->method('instanceOfStorage')
2723
+            ->willReturnMap([
2724
+                ['OCA\Files_Sharing\External\Storage', true],
2725
+                ['OCA\Files_Sharing\SharedStorage', false],
2726
+            ]);
2727
+        $userFolder->method('getStorage')->willReturn($storage);
2728
+        $path->method('getStorage')->willReturn($storage);
2729
+
2730
+        $path->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
2731
+        $userFolder->expects($this->once())
2732
+            ->method('get')
2733
+            ->with('valid-path')
2734
+            ->willReturn($path);
2735
+        $userFolder->method('getById')
2736
+            ->willReturn([]);
2737
+
2738
+        $this->userManager->method('userExists')->with('validUser')->willReturn(true);
2739
+
2740
+        $this->shareManager
2741
+            ->expects($this->once())
2742
+            ->method('createShare')
2743
+            ->with($this->callback(function (IShare $share) {
2744
+                return $share->getPermissions() === Constants::PERMISSION_READ;
2745
+            }))
2746
+            ->willReturnArgument(0);
2747
+
2748
+        $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
2749
+    }
2750
+
2751
+
2752
+    public function testUpdateShareCantAccess(): void {
2753
+        $this->expectException(OCSNotFoundException::class);
2754
+        $this->expectExceptionMessage('Wrong share ID, share does not exist');
2755
+
2756
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
2757
+        $share = $this->newShare();
2758
+        $share->setNode($node);
2759
+
2760
+        $node->expects($this->once())
2761
+            ->method('lock')
2762
+            ->with(ILockingProvider::LOCK_SHARED);
2763
+
2764
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2765
+
2766
+        $this->rootFolder->method('getUserFolder')
2767
+            ->with($this->currentUser)
2768
+            ->willReturn($userFolder);
2769
+
2770
+        $userFolder->method('getById')
2771
+            ->with($share->getNodeId())
2772
+            ->willReturn([$share->getNode()]);
2773
+
2774
+        $this->ocs->updateShare(42);
2775
+    }
2776
+
2777
+
2778
+    public function testUpdateNoParametersLink(): void {
2779
+        $this->expectException(OCSBadRequestException::class);
2780
+        $this->expectExceptionMessage('Wrong or no update parameter given');
2781
+
2782
+        $node = $this->getMockBuilder(Folder::class)->getMock();
2783
+        $share = $this->newShare();
2784
+        $share->setPermissions(Constants::PERMISSION_ALL)
2785
+            ->setSharedBy($this->currentUser)
2786
+            ->setShareType(IShare::TYPE_LINK)
2787
+            ->setNode($node);
2788
+
2789
+        $node->expects($this->once())
2790
+            ->method('lock')
2791
+            ->with(ILockingProvider::LOCK_SHARED);
2792
+
2793
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2794
+
2795
+        $this->ocs->updateShare(42);
2796
+    }
2797
+
2798
+
2799
+    public function testUpdateNoParametersOther(): void {
2800
+        $this->expectException(OCSBadRequestException::class);
2801
+        $this->expectExceptionMessage('Wrong or no update parameter given');
2802
+
2803
+        $node = $this->getMockBuilder(Folder::class)->getMock();
2804
+        $share = $this->newShare();
2805
+        $share->setPermissions(Constants::PERMISSION_ALL)
2806
+            ->setSharedBy($this->currentUser)
2807
+            ->setShareType(IShare::TYPE_GROUP)
2808
+            ->setNode($node);
2809
+
2810
+        $node->expects($this->once())
2811
+            ->method('lock')
2812
+            ->with(ILockingProvider::LOCK_SHARED);
2813
+
2814
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2815
+
2816
+        $this->ocs->updateShare(42);
2817
+    }
2818
+
2819
+    public function testUpdateLinkShareClear(): void {
2820
+        $ocs = $this->mockFormatShare();
2821
+
2822
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
2823
+        $node->method('getId')
2824
+            ->willReturn(42);
2825
+        $share = $this->newShare();
2826
+        $share->setPermissions(Constants::PERMISSION_ALL)
2827
+            ->setSharedBy($this->currentUser)
2828
+            ->setShareType(IShare::TYPE_LINK)
2829
+            ->setPassword('password')
2830
+            ->setExpirationDate(new \DateTime())
2831
+            ->setNote('note')
2832
+            ->setLabel('label')
2833
+            ->setHideDownload(true)
2834
+            ->setPermissions(Constants::PERMISSION_ALL)
2835
+            ->setNode($node);
2836
+
2837
+        $node->expects($this->once())
2838
+            ->method('lock')
2839
+            ->with(ILockingProvider::LOCK_SHARED);
2840
+
2841
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2842
+
2843
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
2844
+            $this->callback(function (IShare $share) {
2845
+                return $share->getPermissions() === Constants::PERMISSION_READ &&
2846
+                $share->getPassword() === null &&
2847
+                $share->getExpirationDate() === null &&
2848
+                // Once set a note or a label are never back to null, only to an
2849
+                // empty string.
2850
+                $share->getNote() === '' &&
2851
+                $share->getLabel() === '' &&
2852
+                $share->getHideDownload() === false;
2853
+            })
2854
+        )->willReturnArgument(0);
2855
+
2856
+        $this->shareManager->method('getSharedWith')
2857
+            ->willReturn([]);
2858
+
2859
+        $this->rootFolder->method('getUserFolder')
2860
+            ->with($this->currentUser)
2861
+            ->willReturn($userFolder);
2862
+
2863
+        $userFolder->method('getById')
2864
+            ->with(42)
2865
+            ->willReturn([$node]);
2866
+        $userFolder->method('getFirstNodeById')
2867
+            ->with(42)
2868
+            ->willReturn($node);
2869
+
2870
+        $mountPoint = $this->createMock(IMountPoint::class);
2871
+        $node->method('getMountPoint')
2872
+            ->willReturn($mountPoint);
2873
+        $mountPoint->method('getStorageRootId')
2874
+            ->willReturn(42);
2875
+
2876
+        $expected = new DataResponse([]);
2877
+        $result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false');
2878
+
2879
+        $this->assertInstanceOf(get_class($expected), $result);
2880
+        $this->assertEquals($expected->getData(), $result->getData());
2881
+    }
2882
+
2883
+    public function testUpdateLinkShareSet(): void {
2884
+        $ocs = $this->mockFormatShare();
2885
+
2886
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
2887
+        $folder->method('getId')
2888
+            ->willReturn(42);
2889
+
2890
+        $share = Server::get(IManager::class)->newShare();
2891
+        $share->setPermissions(Constants::PERMISSION_ALL)
2892
+            ->setSharedBy($this->currentUser)
2893
+            ->setShareType(IShare::TYPE_LINK)
2894
+            ->setNode($folder);
2895
+
2896
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2897
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2898
+
2899
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
2900
+            $this->callback(function (IShare $share) {
2901
+                $date = new \DateTime('2000-01-01');
2902
+                $date->setTime(0, 0, 0);
2903
+
2904
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2905
+                $share->getPassword() === 'password' &&
2906
+                $share->getExpirationDate() == $date &&
2907
+                $share->getNote() === 'note' &&
2908
+                $share->getLabel() === 'label' &&
2909
+                $share->getHideDownload() === true;
2910
+            })
2911
+        )->willReturnArgument(0);
2912
+
2913
+        $this->shareManager->method('getSharedWith')
2914
+            ->willReturn([]);
2915
+
2916
+        $this->rootFolder->method('getUserFolder')
2917
+            ->with($this->currentUser)
2918
+            ->willReturn($userFolder);
2919
+
2920
+        $userFolder->method('getById')
2921
+            ->with(42)
2922
+            ->willReturn([$folder]);
2923
+
2924
+        $mountPoint = $this->createMock(IMountPoint::class);
2925
+        $folder->method('getMountPoint')
2926
+            ->willReturn($mountPoint);
2927
+        $mountPoint->method('getStorageRootId')
2928
+            ->willReturn(42);
2929
+
2930
+        $expected = new DataResponse([]);
2931
+        $result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true');
2932
+
2933
+        $this->assertInstanceOf(get_class($expected), $result);
2934
+        $this->assertEquals($expected->getData(), $result->getData());
2935
+    }
2936
+
2937
+    /**
2938
+     * @dataProvider publicUploadParamsProvider
2939
+     */
2940
+    public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password): void {
2941
+        $ocs = $this->mockFormatShare();
2942
+
2943
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
2944
+        $folder->method('getId')
2945
+            ->willReturn(42);
2946
+
2947
+        $share = Server::get(IManager::class)->newShare();
2948
+        $share->setPermissions(Constants::PERMISSION_ALL)
2949
+            ->setSharedBy($this->currentUser)
2950
+            ->setShareType(IShare::TYPE_LINK)
2951
+            ->setPassword('password')
2952
+            ->setNode($folder);
2953
+
2954
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
2955
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
2956
+        $this->shareManager->method('getSharedWith')->willReturn([]);
2957
+
2958
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
2959
+            $this->callback(function (IShare $share) {
2960
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
2961
+                $share->getPassword() === 'password' &&
2962
+                $share->getExpirationDate() === null;
2963
+            })
2964
+        )->willReturnArgument(0);
2965
+
2966
+        $this->rootFolder->method('getUserFolder')
2967
+            ->with($this->currentUser)
2968
+            ->willReturn($userFolder);
2969
+
2970
+        $userFolder->method('getById')
2971
+            ->with(42)
2972
+            ->willReturn([$folder]);
2973
+
2974
+        $mountPoint = $this->createMock(IMountPoint::class);
2975
+        $folder->method('getMountPoint')
2976
+            ->willReturn($mountPoint);
2977
+        $mountPoint->method('getStorageRootId')
2978
+            ->willReturn(42);
2979
+
2980
+        $expected = new DataResponse([]);
2981
+        $result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
2982
+
2983
+        $this->assertInstanceOf(get_class($expected), $result);
2984
+        $this->assertEquals($expected->getData(), $result->getData());
2985
+    }
2986
+
2987
+
2988
+    public function publicLinkValidPermissionsProvider() {
2989
+        return [
2990
+            [Constants::PERMISSION_CREATE],
2991
+            [Constants::PERMISSION_READ],
2992
+            [Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE],
2993
+            [Constants::PERMISSION_READ | Constants::PERMISSION_DELETE],
2994
+            [Constants::PERMISSION_READ | Constants::PERMISSION_CREATE],
2995
+        ];
2996
+    }
2997
+
2998
+    /**
2999
+     * @dataProvider publicLinkValidPermissionsProvider
3000
+     */
3001
+    public function testUpdateLinkShareSetCRUDPermissions($permissions): void {
3002
+        $ocs = $this->mockFormatShare();
3003
+
3004
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3005
+        $folder->method('getId')
3006
+            ->willReturn(42);
3007
+
3008
+        $share = Server::get(IManager::class)->newShare();
3009
+        $share->setPermissions(Constants::PERMISSION_ALL)
3010
+            ->setSharedBy($this->currentUser)
3011
+            ->setShareType(IShare::TYPE_LINK)
3012
+            ->setPassword('password')
3013
+            ->setNode($folder);
3014
+
3015
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3016
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3017
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3018
+
3019
+        $this->shareManager
3020
+            ->expects($this->any())
3021
+            ->method('updateShare')
3022
+            ->willReturnArgument(0);
3023
+
3024
+        $this->rootFolder->method('getUserFolder')
3025
+            ->with($this->currentUser)
3026
+            ->willReturn($userFolder);
3027
+
3028
+        $userFolder->method('getById')
3029
+            ->with(42)
3030
+            ->willReturn([$folder]);
3031
+
3032
+        $mountPoint = $this->createMock(IMountPoint::class);
3033
+        $folder->method('getMountPoint')
3034
+            ->willReturn($mountPoint);
3035
+        $mountPoint->method('getStorageRootId')
3036
+            ->willReturn(42);
3037
+
3038
+        $expected = new DataResponse([]);
3039
+        $result = $ocs->updateShare(42, $permissions, 'password', null, null, null);
3040
+
3041
+        $this->assertInstanceOf(get_class($expected), $result);
3042
+        $this->assertEquals($expected->getData(), $result->getData());
3043
+    }
3044
+
3045
+    public function publicLinkInvalidPermissionsProvider1() {
3046
+        return [
3047
+            [Constants::PERMISSION_DELETE],
3048
+            [Constants::PERMISSION_UPDATE],
3049
+            [Constants::PERMISSION_SHARE],
3050
+        ];
3051
+    }
3052
+
3053
+    /**
3054
+     * @dataProvider publicLinkInvalidPermissionsProvider1
3055
+     */
3056
+    public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions): void {
3057
+        $this->expectException(OCSBadRequestException::class);
3058
+        $this->expectExceptionMessage('Share must at least have READ or CREATE permissions');
3059
+
3060
+        $this->testUpdateLinkShareSetCRUDPermissions($permissions, null);
3061
+    }
3062
+
3063
+    public function publicLinkInvalidPermissionsProvider2() {
3064
+        return [
3065
+            [Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE],
3066
+            [Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE],
3067
+        ];
3068
+    }
3069
+
3070
+    /**
3071
+     * @dataProvider publicLinkInvalidPermissionsProvider2
3072
+     */
3073
+    public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions): void {
3074
+        $this->expectException(OCSBadRequestException::class);
3075
+        $this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set');
3076
+
3077
+        $this->testUpdateLinkShareSetCRUDPermissions($permissions);
3078
+    }
3079
+
3080
+    public function testUpdateLinkShareInvalidDate(): void {
3081
+        $this->expectException(OCSBadRequestException::class);
3082
+        $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
3083
+
3084
+        $ocs = $this->mockFormatShare();
3085
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3086
+        $userFolder->method('getById')
3087
+            ->with(42)
3088
+            ->willReturn([$folder]);
3089
+        $this->rootFolder->method('getUserFolder')
3090
+            ->with($this->currentUser)
3091
+            ->willReturn($userFolder);
3092
+
3093
+        $folder->method('getId')
3094
+            ->willReturn(42);
3095
+
3096
+        $share = Server::get(IManager::class)->newShare();
3097
+        $share->setPermissions(Constants::PERMISSION_ALL)
3098
+            ->setSharedBy($this->currentUser)
3099
+            ->setShareType(IShare::TYPE_LINK)
3100
+            ->setNode($folder);
3101
+
3102
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3103
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3104
+
3105
+        $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a');
3106
+    }
3107
+
3108
+    public function publicUploadParamsProvider() {
3109
+        return [
3110
+            [null, 'true', null, 'password'],
3111
+            // legacy had no delete
3112
+            [
3113
+                Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE,
3114
+                'true', null, 'password'
3115
+            ],
3116
+            // correct
3117
+            [
3118
+                Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE,
3119
+                null, null, 'password'
3120
+            ],
3121
+        ];
3122
+    }
3123
+
3124
+    /**
3125
+     * @dataProvider publicUploadParamsProvider
3126
+     */
3127
+    public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password): void {
3128
+        $this->expectException(OCSForbiddenException::class);
3129
+        $this->expectExceptionMessage('Public upload disabled by the administrator');
3130
+
3131
+        $ocs = $this->mockFormatShare();
3132
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3133
+        $userFolder->method('getById')
3134
+            ->with(42)
3135
+            ->willReturn([$folder]);
3136
+        $this->rootFolder->method('getUserFolder')
3137
+            ->with($this->currentUser)
3138
+            ->willReturn($userFolder);
3139
+
3140
+        $folder->method('getId')->willReturn(42);
3141
+
3142
+        $share = Server::get(IManager::class)->newShare();
3143
+        $share->setPermissions(Constants::PERMISSION_ALL)
3144
+            ->setSharedBy($this->currentUser)
3145
+            ->setShareType(IShare::TYPE_LINK)
3146
+            ->setNode($folder);
3147
+
3148
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3149
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
3150
+
3151
+        $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
3152
+    }
3153
+
3154
+
3155
+    public function testUpdateLinkSharePublicUploadOnFile(): void {
3156
+        $this->expectException(OCSBadRequestException::class);
3157
+        $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
3158
+
3159
+        $ocs = $this->mockFormatShare();
3160
+
3161
+        $file = $this->getMockBuilder(File::class)->getMock();
3162
+        $file->method('getId')
3163
+            ->willReturn(42);
3164
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3165
+        $userFolder->method('getById')
3166
+            ->with(42)
3167
+            ->willReturn([$folder]);
3168
+        $this->rootFolder->method('getUserFolder')
3169
+            ->with($this->currentUser)
3170
+            ->willReturn($userFolder);
3171
+
3172
+        $share = Server::get(IManager::class)->newShare();
3173
+        $share->setPermissions(Constants::PERMISSION_ALL)
3174
+            ->setSharedBy($this->currentUser)
3175
+            ->setShareType(IShare::TYPE_LINK)
3176
+            ->setNode($file);
3177
+
3178
+        $this->shareManager
3179
+            ->method('getShareById')
3180
+            ->with('ocinternal:42')
3181
+            ->willReturn($share);
3182
+        $this->shareManager
3183
+            ->method('shareApiLinkAllowPublicUpload')
3184
+            ->willReturn(true);
3185
+        $this->shareManager
3186
+            ->method('updateShare')
3187
+            ->with($share)
3188
+            ->willThrowException(new \InvalidArgumentException('File shares cannot have create or delete permissions'));
3189
+
3190
+        $ocs->updateShare(42, null, 'password', null, 'true', '');
3191
+    }
3192
+
3193
+    public function testUpdateLinkSharePasswordDoesNotChangeOther(): void {
3194
+        $ocs = $this->mockFormatShare();
3195
+
3196
+        $date = new \DateTime('2000-01-01');
3197
+        $date->setTime(0, 0, 0);
3198
+
3199
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3200
+        $node->method('getId')->willReturn(42);
3201
+        $userFolder->method('getById')
3202
+            ->with(42)
3203
+            ->willReturn([$node]);
3204
+        $this->rootFolder->method('getUserFolder')
3205
+            ->with($this->currentUser)
3206
+            ->willReturn($userFolder);
3207
+        $share = $this->newShare();
3208
+        $share->setPermissions(Constants::PERMISSION_ALL)
3209
+            ->setSharedBy($this->currentUser)
3210
+            ->setShareType(IShare::TYPE_LINK)
3211
+            ->setPassword('password')
3212
+            ->setSendPasswordByTalk(true)
3213
+            ->setExpirationDate($date)
3214
+            ->setNote('note')
3215
+            ->setLabel('label')
3216
+            ->setHideDownload(true)
3217
+            ->setPermissions(Constants::PERMISSION_ALL)
3218
+            ->setNode($node);
3219
+
3220
+        $node->expects($this->once())
3221
+            ->method('lock')
3222
+            ->with(ILockingProvider::LOCK_SHARED);
3223
+
3224
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3225
+
3226
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3227
+            $this->callback(function (IShare $share) use ($date) {
3228
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3229
+                $share->getPassword() === 'newpassword' &&
3230
+                $share->getSendPasswordByTalk() === true &&
3231
+                $share->getExpirationDate() === $date &&
3232
+                $share->getNote() === 'note' &&
3233
+                $share->getLabel() === 'label' &&
3234
+                $share->getHideDownload() === true;
3235
+            })
3236
+        )->willReturnArgument(0);
3237
+
3238
+        $expected = new DataResponse([]);
3239
+        $result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null);
3240
+
3241
+        $this->assertInstanceOf(get_class($expected), $result);
3242
+        $this->assertEquals($expected->getData(), $result->getData());
3243
+    }
3244
+
3245
+    public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther(): void {
3246
+        $ocs = $this->mockFormatShare();
3247
+
3248
+        $date = new \DateTime('2000-01-01');
3249
+        $date->setTime(0, 0, 0);
3250
+
3251
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3252
+        $userFolder->method('getById')
3253
+            ->with(42)
3254
+            ->willReturn([$node]);
3255
+        $this->rootFolder->method('getUserFolder')
3256
+            ->with($this->currentUser)
3257
+            ->willReturn($userFolder);
3258
+        $node->method('getId')->willReturn(42);
3259
+        $share = $this->newShare();
3260
+        $share->setPermissions(Constants::PERMISSION_ALL)
3261
+            ->setSharedBy($this->currentUser)
3262
+            ->setShareType(IShare::TYPE_LINK)
3263
+            ->setPassword('password')
3264
+            ->setSendPasswordByTalk(false)
3265
+            ->setExpirationDate($date)
3266
+            ->setNote('note')
3267
+            ->setLabel('label')
3268
+            ->setHideDownload(true)
3269
+            ->setPermissions(Constants::PERMISSION_ALL)
3270
+            ->setNode($node);
3271
+
3272
+        $node->expects($this->once())
3273
+            ->method('lock')
3274
+            ->with(ILockingProvider::LOCK_SHARED);
3275
+
3276
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3277
+
3278
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3279
+
3280
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3281
+            $this->callback(function (IShare $share) use ($date) {
3282
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3283
+                $share->getPassword() === 'password' &&
3284
+                $share->getSendPasswordByTalk() === true &&
3285
+                $share->getExpirationDate() === $date &&
3286
+                $share->getNote() === 'note' &&
3287
+                $share->getLabel() === 'label' &&
3288
+                $share->getHideDownload() === true;
3289
+            })
3290
+        )->willReturnArgument(0);
3291
+
3292
+        $expected = new DataResponse([]);
3293
+        $result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3294
+
3295
+        $this->assertInstanceOf(get_class($expected), $result);
3296
+        $this->assertEquals($expected->getData(), $result->getData());
3297
+    }
3298
+
3299
+
3300
+    public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3301
+        $this->expectException(OCSForbiddenException::class);
3302
+        $this->expectExceptionMessage('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.');
3303
+
3304
+        $ocs = $this->mockFormatShare();
3305
+
3306
+        $date = new \DateTime('2000-01-01');
3307
+        $date->setTime(0, 0, 0);
3308
+
3309
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3310
+        $userFolder->method('getById')
3311
+            ->with(42)
3312
+            ->willReturn([$node]);
3313
+        $this->rootFolder->method('getUserFolder')
3314
+            ->with($this->currentUser)
3315
+            ->willReturn($userFolder);
3316
+        $node->method('getId')->willReturn(42);
3317
+        $share = $this->newShare();
3318
+        $share->setPermissions(Constants::PERMISSION_ALL)
3319
+            ->setSharedBy($this->currentUser)
3320
+            ->setShareType(IShare::TYPE_LINK)
3321
+            ->setPassword('password')
3322
+            ->setSendPasswordByTalk(false)
3323
+            ->setExpirationDate($date)
3324
+            ->setNote('note')
3325
+            ->setLabel('label')
3326
+            ->setHideDownload(true)
3327
+            ->setPermissions(Constants::PERMISSION_ALL)
3328
+            ->setNode($node);
3329
+
3330
+        $node->expects($this->once())
3331
+            ->method('lock')
3332
+            ->with(ILockingProvider::LOCK_SHARED);
3333
+
3334
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3335
+
3336
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3337
+
3338
+        $this->shareManager->expects($this->never())->method('updateShare');
3339
+
3340
+        $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
3341
+    }
3342
+
3343
+    public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther(): void {
3344
+        $ocs = $this->mockFormatShare();
3345
+
3346
+        $date = new \DateTime('2000-01-01');
3347
+        $date->setTime(0, 0, 0);
3348
+
3349
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3350
+        $userFolder->method('getById')
3351
+            ->with(42)
3352
+            ->willReturn([$node]);
3353
+        $this->rootFolder->method('getUserFolder')
3354
+            ->with($this->currentUser)
3355
+            ->willReturn($userFolder);
3356
+        $node->method('getId')->willReturn(42);
3357
+        $share = $this->newShare();
3358
+        $share->setPermissions(Constants::PERMISSION_ALL)
3359
+            ->setSharedBy($this->currentUser)
3360
+            ->setShareType(IShare::TYPE_LINK)
3361
+            ->setPassword('password')
3362
+            ->setSendPasswordByTalk(true)
3363
+            ->setExpirationDate($date)
3364
+            ->setNote('note')
3365
+            ->setLabel('label')
3366
+            ->setHideDownload(true)
3367
+            ->setPermissions(Constants::PERMISSION_ALL)
3368
+            ->setNode($node);
3369
+
3370
+        $node->expects($this->once())
3371
+            ->method('lock')
3372
+            ->with(ILockingProvider::LOCK_SHARED);
3373
+
3374
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3375
+
3376
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
3377
+
3378
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3379
+            $this->callback(function (IShare $share) use ($date) {
3380
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3381
+                $share->getPassword() === 'password' &&
3382
+                $share->getSendPasswordByTalk() === false &&
3383
+                $share->getExpirationDate() === $date &&
3384
+                $share->getNote() === 'note' &&
3385
+                $share->getLabel() === 'label' &&
3386
+                $share->getHideDownload() === true;
3387
+            })
3388
+        )->willReturnArgument(0);
3389
+
3390
+        $expected = new DataResponse([]);
3391
+        $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3392
+
3393
+        $this->assertInstanceOf(get_class($expected), $result);
3394
+        $this->assertEquals($expected->getData(), $result->getData());
3395
+    }
3396
+
3397
+    public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void {
3398
+        $ocs = $this->mockFormatShare();
3399
+
3400
+        $date = new \DateTime('2000-01-01');
3401
+        $date->setTime(0, 0, 0);
3402
+
3403
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3404
+        $node->method('getId')
3405
+            ->willReturn(42);
3406
+
3407
+        $share = $this->newShare();
3408
+        $share->setPermissions(Constants::PERMISSION_ALL)
3409
+            ->setSharedBy($this->currentUser)
3410
+            ->setShareType(IShare::TYPE_LINK)
3411
+            ->setPassword('password')
3412
+            ->setSendPasswordByTalk(true)
3413
+            ->setExpirationDate($date)
3414
+            ->setNote('note')
3415
+            ->setLabel('label')
3416
+            ->setHideDownload(true)
3417
+            ->setPermissions(Constants::PERMISSION_ALL)
3418
+            ->setNode($node);
3419
+
3420
+        $node->expects($this->once())
3421
+            ->method('lock')
3422
+            ->with(ILockingProvider::LOCK_SHARED);
3423
+
3424
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3425
+
3426
+        $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
3427
+
3428
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3429
+            $this->callback(function (IShare $share) use ($date) {
3430
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3431
+                $share->getPassword() === 'password' &&
3432
+                $share->getSendPasswordByTalk() === false &&
3433
+                $share->getExpirationDate() === $date &&
3434
+                $share->getNote() === 'note' &&
3435
+                $share->getLabel() === 'label' &&
3436
+                $share->getHideDownload() === true;
3437
+            })
3438
+        )->willReturnArgument(0);
3439
+
3440
+        $this->rootFolder->method('getUserFolder')
3441
+            ->with($this->currentUser)
3442
+            ->willReturn($userFolder);
3443
+
3444
+        $userFolder->method('getById')
3445
+            ->with(42)
3446
+            ->willReturn([$node]);
3447
+
3448
+        $mountPoint = $this->createMock(IMountPoint::class);
3449
+        $node->method('getMountPoint')
3450
+            ->willReturn($mountPoint);
3451
+        $mountPoint->method('getStorageRootId')
3452
+            ->willReturn(42);
3453
+
3454
+        $mountPoint = $this->createMock(IMountPoint::class);
3455
+        $node->method('getMountPoint')
3456
+            ->willReturn($mountPoint);
3457
+        $mountPoint->method('getStorageRootId')
3458
+            ->willReturn(42);
3459
+
3460
+        $expected = new DataResponse([]);
3461
+        $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
3462
+
3463
+        $this->assertInstanceOf(get_class($expected), $result);
3464
+        $this->assertEquals($expected->getData(), $result->getData());
3465
+    }
3466
+
3467
+    public function testUpdateLinkShareExpireDateDoesNotChangeOther(): void {
3468
+        $ocs = $this->mockFormatShare();
3469
+
3470
+        [$userFolder, $node] = $this->getNonSharedUserFolder();
3471
+        $node->method('getId')
3472
+            ->willReturn(42);
3473
+
3474
+        $share = $this->newShare();
3475
+        $share->setPermissions(Constants::PERMISSION_ALL)
3476
+            ->setSharedBy($this->currentUser)
3477
+            ->setShareType(IShare::TYPE_LINK)
3478
+            ->setPassword('password')
3479
+            ->setSendPasswordByTalk(true)
3480
+            ->setExpirationDate(new \DateTime())
3481
+            ->setNote('note')
3482
+            ->setLabel('label')
3483
+            ->setHideDownload(true)
3484
+            ->setPermissions(Constants::PERMISSION_ALL)
3485
+            ->setNode($node);
3486
+
3487
+        $node->expects($this->once())
3488
+            ->method('lock')
3489
+            ->with(ILockingProvider::LOCK_SHARED);
3490
+
3491
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3492
+
3493
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3494
+            $this->callback(function (IShare $share) {
3495
+                $date = new \DateTime('2010-12-23');
3496
+                $date->setTime(0, 0, 0);
3497
+
3498
+                return $share->getPermissions() === Constants::PERMISSION_ALL &&
3499
+                $share->getPassword() === 'password' &&
3500
+                $share->getSendPasswordByTalk() === true &&
3501
+                $share->getExpirationDate() == $date &&
3502
+                $share->getNote() === 'note' &&
3503
+                $share->getLabel() === 'label' &&
3504
+                $share->getHideDownload() === true;
3505
+            })
3506
+        )->willReturnArgument(0);
3507
+
3508
+        $this->rootFolder->method('getUserFolder')
3509
+            ->with($this->currentUser)
3510
+            ->willReturn($userFolder);
3511
+
3512
+        $userFolder->method('getById')
3513
+            ->with(42)
3514
+            ->willReturn([$node]);
3515
+
3516
+        $mountPoint = $this->createMock(IMountPoint::class);
3517
+        $node->method('getMountPoint')
3518
+            ->willReturn($mountPoint);
3519
+        $mountPoint->method('getStorageRootId')
3520
+            ->willReturn(42);
3521
+
3522
+        $expected = new DataResponse([]);
3523
+        $result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null);
3524
+
3525
+        $this->assertInstanceOf(get_class($expected), $result);
3526
+        $this->assertEquals($expected->getData(), $result->getData());
3527
+    }
3528
+
3529
+    public function testUpdateLinkSharePublicUploadDoesNotChangeOther(): void {
3530
+        $ocs = $this->mockFormatShare();
3531
+
3532
+        $date = new \DateTime('2000-01-01');
3533
+
3534
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3535
+        $folder->method('getId')
3536
+            ->willReturn(42);
3537
+
3538
+        $share = Server::get(IManager::class)->newShare();
3539
+        $share->setPermissions(Constants::PERMISSION_ALL)
3540
+            ->setSharedBy($this->currentUser)
3541
+            ->setShareType(IShare::TYPE_LINK)
3542
+            ->setPassword('password')
3543
+            ->setSendPasswordByTalk(true)
3544
+            ->setExpirationDate($date)
3545
+            ->setNote('note')
3546
+            ->setLabel('label')
3547
+            ->setHideDownload(true)
3548
+            ->setPermissions(Constants::PERMISSION_ALL)
3549
+            ->setNode($folder);
3550
+
3551
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3552
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3553
+
3554
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3555
+            $this->callback(function (IShare $share) use ($date) {
3556
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3557
+                $share->getPassword() === 'password' &&
3558
+                $share->getSendPasswordByTalk() === true &&
3559
+                $share->getExpirationDate() === $date &&
3560
+                $share->getNote() === 'note' &&
3561
+                $share->getLabel() === 'label' &&
3562
+                $share->getHideDownload() === true;
3563
+            })
3564
+        )->willReturnArgument(0);
3565
+
3566
+        $this->shareManager->method('getSharedWith')
3567
+            ->willReturn([]);
3568
+
3569
+        $this->rootFolder->method('getUserFolder')
3570
+            ->with($this->currentUser)
3571
+            ->willReturn($userFolder);
3572
+
3573
+        $userFolder->method('getById')
3574
+            ->with(42)
3575
+            ->willReturn([$folder]);
3576
+
3577
+        $mountPoint = $this->createMock(IMountPoint::class);
3578
+        $folder->method('getMountPoint')
3579
+            ->willReturn($mountPoint);
3580
+        $mountPoint->method('getStorageRootId')
3581
+            ->willReturn(42);
3582
+
3583
+        $expected = new DataResponse([]);
3584
+        $result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null);
3585
+
3586
+        $this->assertInstanceOf(get_class($expected), $result);
3587
+        $this->assertEquals($expected->getData(), $result->getData());
3588
+    }
3589
+
3590
+    public function testUpdateLinkSharePermissions(): void {
3591
+        $ocs = $this->mockFormatShare();
3592
+
3593
+        $date = new \DateTime('2000-01-01');
3594
+
3595
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3596
+        $folder->method('getId')
3597
+            ->willReturn(42);
3598
+
3599
+        $share = Server::get(IManager::class)->newShare();
3600
+        $share->setPermissions(Constants::PERMISSION_ALL)
3601
+            ->setSharedBy($this->currentUser)
3602
+            ->setShareType(IShare::TYPE_LINK)
3603
+            ->setPassword('password')
3604
+            ->setSendPasswordByTalk(true)
3605
+            ->setExpirationDate($date)
3606
+            ->setNote('note')
3607
+            ->setLabel('label')
3608
+            ->setHideDownload(true)
3609
+            ->setPermissions(Constants::PERMISSION_ALL)
3610
+            ->setNode($folder);
3611
+
3612
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3613
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3614
+
3615
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3616
+            $this->callback(function (IShare $share) use ($date): bool {
3617
+                return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) &&
3618
+                $share->getPassword() === 'password' &&
3619
+                $share->getSendPasswordByTalk() === true &&
3620
+                $share->getExpirationDate() === $date &&
3621
+                $share->getNote() === 'note' &&
3622
+                $share->getLabel() === 'label' &&
3623
+                $share->getHideDownload() === true;
3624
+            })
3625
+        )->willReturnArgument(0);
3626
+
3627
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3628
+
3629
+        $this->rootFolder->method('getUserFolder')
3630
+            ->with($this->currentUser)
3631
+            ->willReturn($userFolder);
3632
+
3633
+        $userFolder->method('getById')
3634
+            ->with(42)
3635
+            ->willReturn([$folder]);
3636
+
3637
+        $mountPoint = $this->createMock(IMountPoint::class);
3638
+        $folder->method('getMountPoint')
3639
+            ->willReturn($mountPoint);
3640
+        $mountPoint->method('getStorageRootId')
3641
+            ->willReturn(42);
3642
+
3643
+        $expected = new DataResponse([]);
3644
+        $result = $ocs->updateShare(42, 7, null, null, 'true', null, null, null, null);
3645
+
3646
+        $this->assertInstanceOf(get_class($expected), $result);
3647
+        $this->assertEquals($expected->getData(), $result->getData());
3648
+    }
3649
+
3650
+    public function testUpdateLinkSharePermissionsShare(): void {
3651
+        $ocs = $this->mockFormatShare();
3652
+
3653
+        $date = new \DateTime('2000-01-01');
3654
+
3655
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3656
+        $folder->method('getId')
3657
+            ->willReturn(42);
3658
+
3659
+        $share = Server::get(IManager::class)->newShare();
3660
+        $share->setPermissions(Constants::PERMISSION_ALL)
3661
+            ->setSharedBy($this->currentUser)
3662
+            ->setShareType(IShare::TYPE_LINK)
3663
+            ->setPassword('password')
3664
+            ->setSendPasswordByTalk(true)
3665
+            ->setExpirationDate($date)
3666
+            ->setNote('note')
3667
+            ->setLabel('label')
3668
+            ->setHideDownload(true)
3669
+            ->setPermissions(Constants::PERMISSION_READ)
3670
+            ->setNode($folder);
3671
+
3672
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3673
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3674
+
3675
+        $this->shareManager->expects($this->once())
3676
+            ->method('updateShare')
3677
+            ->with(
3678
+                $this->callback(function (IShare $share) use ($date) {
3679
+                    return $share->getPermissions() === Constants::PERMISSION_ALL &&
3680
+                        $share->getPassword() === 'password' &&
3681
+                        $share->getSendPasswordByTalk() === true &&
3682
+                        $share->getExpirationDate() === $date &&
3683
+                        $share->getNote() === 'note' &&
3684
+                        $share->getLabel() === 'label' &&
3685
+                        $share->getHideDownload() === true;
3686
+                })
3687
+            )->willReturnArgument(0);
3688
+
3689
+        $this->rootFolder->method('getUserFolder')
3690
+            ->with($this->currentUser)
3691
+            ->willReturn($userFolder);
3692
+
3693
+        $userFolder->method('getById')
3694
+            ->with(42)
3695
+            ->willReturn([$folder]);
3696
+
3697
+        $mountPoint = $this->createMock(IMountPoint::class);
3698
+        $folder->method('getMountPoint')
3699
+            ->willReturn($mountPoint);
3700
+        $mountPoint->method('getStorageRootId')
3701
+            ->willReturn(42);
3702
+
3703
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3704
+
3705
+        $expected = new DataResponse([]);
3706
+        $result = $ocs->updateShare(42, Constants::PERMISSION_ALL, null, null, null, null, null, null, null);
3707
+
3708
+        $this->assertInstanceOf(get_class($expected), $result);
3709
+        $this->assertEquals($expected->getData(), $result->getData());
3710
+    }
3711
+
3712
+    public function testUpdateOtherPermissions(): void {
3713
+        $ocs = $this->mockFormatShare();
3714
+
3715
+        [$userFolder, $file] = $this->getNonSharedUserFolder();
3716
+        $file->method('getId')
3717
+            ->willReturn(42);
3718
+
3719
+        $share = Server::get(IManager::class)->newShare();
3720
+        $share->setPermissions(Constants::PERMISSION_ALL)
3721
+            ->setSharedBy($this->currentUser)
3722
+            ->setShareType(IShare::TYPE_USER)
3723
+            ->setNode($file);
3724
+
3725
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3726
+        $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
3727
+
3728
+        $this->shareManager->expects($this->once())->method('updateShare')->with(
3729
+            $this->callback(function (IShare $share) {
3730
+                return $share->getPermissions() === Constants::PERMISSION_ALL;
3731
+            })
3732
+        )->willReturnArgument(0);
3733
+
3734
+        $this->shareManager->method('getSharedWith')->willReturn([]);
3735
+
3736
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3737
+        $this->rootFolder->method('getUserFolder')
3738
+            ->with($this->currentUser)
3739
+            ->willReturn($userFolder);
3740
+
3741
+        $userFolder->method('getById')
3742
+            ->with(42)
3743
+            ->willReturn([$file]);
3744
+
3745
+        $mountPoint = $this->createMock(IMountPoint::class);
3746
+        $file->method('getMountPoint')
3747
+            ->willReturn($mountPoint);
3748
+        $mountPoint->method('getStorageRootId')
3749
+            ->willReturn(42);
3750
+
3751
+        $expected = new DataResponse([]);
3752
+        $result = $ocs->updateShare(42, 31, null, null, null, null);
3753
+
3754
+        $this->assertInstanceOf(get_class($expected), $result);
3755
+        $this->assertEquals($expected->getData(), $result->getData());
3756
+    }
3757
+
3758
+    public function testUpdateShareCannotIncreasePermissions(): void {
3759
+        $ocs = $this->mockFormatShare();
3760
+
3761
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3762
+        $folder->method('getId')
3763
+            ->willReturn(42);
3764
+
3765
+        $share = Server::get(IManager::class)->newShare();
3766
+        $share
3767
+            ->setId(42)
3768
+            ->setSharedBy($this->currentUser)
3769
+            ->setShareOwner('anotheruser')
3770
+            ->setShareType(IShare::TYPE_GROUP)
3771
+            ->setSharedWith('group1')
3772
+            ->setPermissions(Constants::PERMISSION_READ)
3773
+            ->setNode($folder);
3774
+
3775
+        // note: updateShare will modify the received instance but getSharedWith will reread from the database,
3776
+        // so their values will be different
3777
+        $incomingShare = Server::get(IManager::class)->newShare();
3778
+        $incomingShare
3779
+            ->setId(42)
3780
+            ->setSharedBy($this->currentUser)
3781
+            ->setShareOwner('anotheruser')
3782
+            ->setShareType(IShare::TYPE_GROUP)
3783
+            ->setSharedWith('group1')
3784
+            ->setPermissions(Constants::PERMISSION_READ)
3785
+            ->setNode($folder);
3786
+
3787
+        $this->request
3788
+            ->method('getParam')
3789
+            ->willReturnMap([
3790
+                ['permissions', null, '31'],
3791
+            ]);
3792
+
3793
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3794
+
3795
+        $this->shareManager->expects($this->any())
3796
+            ->method('getSharedWith')
3797
+            ->willReturnMap([
3798
+                ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3799
+                ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]],
3800
+                ['currentUser', IShare::TYPE_ROOM, $share->getNode(), -1, 0, []]
3801
+            ]);
3802
+
3803
+        $this->rootFolder->method('getUserFolder')
3804
+            ->with($this->currentUser)
3805
+            ->willReturn($userFolder);
3806
+
3807
+        $userFolder->method('getById')
3808
+            ->with(42)
3809
+            ->willReturn([$folder]);
3810
+        $userFolder->method('getFirstNodeById')
3811
+            ->with(42)
3812
+            ->willReturn($folder);
3813
+
3814
+        $mountPoint = $this->createMock(IMountPoint::class);
3815
+        $folder->method('getMountPoint')
3816
+            ->willReturn($mountPoint);
3817
+        $mountPoint->method('getStorageRootId')
3818
+            ->willReturn(42);
3819
+
3820
+        $this->shareManager->expects($this->once())
3821
+            ->method('updateShare')
3822
+            ->with($share)
3823
+            ->willThrowException(new GenericShareException('Cannot increase permissions of path/file', 'Cannot increase permissions of path/file', 404));
3824
+
3825
+        try {
3826
+            $ocs->updateShare(42, 31);
3827
+            $this->fail();
3828
+        } catch (OCSException $e) {
3829
+            $this->assertEquals('Cannot increase permissions of path/file', $e->getMessage());
3830
+        }
3831
+    }
3832
+
3833
+    public function testUpdateShareCanIncreasePermissionsIfOwner(): void {
3834
+        $ocs = $this->mockFormatShare();
3835
+
3836
+        [$userFolder, $folder] = $this->getNonSharedUserFolder();
3837
+        $folder->method('getId')
3838
+            ->willReturn(42);
3839
+
3840
+        $share = Server::get(IManager::class)->newShare();
3841
+        $share
3842
+            ->setId(42)
3843
+            ->setSharedBy($this->currentUser)
3844
+            ->setShareOwner($this->currentUser)
3845
+            ->setShareType(IShare::TYPE_GROUP)
3846
+            ->setSharedWith('group1')
3847
+            ->setPermissions(Constants::PERMISSION_READ)
3848
+            ->setNode($folder);
3849
+
3850
+        // note: updateShare will modify the received instance but getSharedWith will reread from the database,
3851
+        // so their values will be different
3852
+        $incomingShare = Server::get(IManager::class)->newShare();
3853
+        $incomingShare
3854
+            ->setId(42)
3855
+            ->setSharedBy($this->currentUser)
3856
+            ->setShareOwner($this->currentUser)
3857
+            ->setShareType(IShare::TYPE_GROUP)
3858
+            ->setSharedWith('group1')
3859
+            ->setPermissions(Constants::PERMISSION_READ)
3860
+            ->setNode($folder);
3861
+
3862
+        $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
3863
+
3864
+        $this->shareManager->expects($this->any())
3865
+            ->method('getSharedWith')
3866
+            ->willReturnMap([
3867
+                ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
3868
+                ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
3869
+            ]);
3870
+
3871
+        $this->shareManager->expects($this->once())
3872
+            ->method('updateShare')
3873
+            ->with($share)
3874
+            ->willReturn($share);
3875
+
3876
+        $this->rootFolder->method('getUserFolder')
3877
+            ->with($this->currentUser)
3878
+            ->willReturn($userFolder);
3879
+
3880
+        $userFolder->method('getById')
3881
+            ->with(42)
3882
+            ->willReturn([$folder]);
3883
+
3884
+        $mountPoint = $this->createMock(IMountPoint::class);
3885
+        $folder->method('getMountPoint')
3886
+            ->willReturn($mountPoint);
3887
+        $mountPoint->method('getStorageRootId')
3888
+            ->willReturn(42);
3889
+
3890
+        $result = $ocs->updateShare(42, 31);
3891
+        $this->assertInstanceOf(DataResponse::class, $result);
3892
+    }
3893
+
3894
+    public function testUpdateShareOwnerless(): void {
3895
+        $ocs = $this->mockFormatShare();
3896
+
3897
+        $mount = $this->createMock(IShareOwnerlessMount::class);
3898
+
3899
+        $file = $this->createMock(File::class);
3900
+        $file
3901
+            ->expects($this->exactly(2))
3902
+            ->method('getPermissions')
3903
+            ->willReturn(Constants::PERMISSION_SHARE);
3904
+        $file
3905
+            ->expects($this->once())
3906
+            ->method('getMountPoint')
3907
+            ->willReturn($mount);
3908
+
3909
+        $userFolder = $this->createMock(Folder::class);
3910
+        $userFolder->method('getById')
3911
+            ->with(2)
3912
+            ->willReturn([$file]);
3913
+        $userFolder->method('getFirstNodeById')
3914
+            ->with(2)
3915
+            ->willReturn($file);
3916
+
3917
+        $this->rootFolder
3918
+            ->method('getUserFolder')
3919
+            ->with($this->currentUser)
3920
+            ->willReturn($userFolder);
3921
+
3922
+        $share = $this->createMock(IShare::class);
3923
+        $share
3924
+            ->expects($this->once())
3925
+            ->method('getNode')
3926
+            ->willReturn($file);
3927
+        $share
3928
+            ->expects($this->exactly(2))
3929
+            ->method('getNodeId')
3930
+            ->willReturn(2);
3931
+        $share
3932
+            ->expects($this->exactly(2))
3933
+            ->method('getPermissions')
3934
+            ->willReturn(Constants::PERMISSION_SHARE);
3935
+
3936
+        $this->shareManager
3937
+            ->expects($this->once())
3938
+            ->method('getShareById')
3939
+            ->with('ocinternal:1', $this->currentUser)
3940
+            ->willReturn($share);
3941
+
3942
+        $this->shareManager
3943
+            ->expects($this->once())
3944
+            ->method('updateShare')
3945
+            ->with($share)
3946
+            ->willReturn($share);
3947
+
3948
+        $result = $ocs->updateShare(1, Constants::PERMISSION_ALL);
3949
+        $this->assertInstanceOf(DataResponse::class, $result);
3950
+    }
3951
+
3952
+    public function dataFormatShare() {
3953
+        $file = $this->getMockBuilder(File::class)->getMock();
3954
+        $folder = $this->getMockBuilder(Folder::class)->getMock();
3955
+        $parent = $this->getMockBuilder(Folder::class)->getMock();
3956
+        $fileWithPreview = $this->getMockBuilder(File::class)->getMock();
3957
+
3958
+        $file->method('getMimeType')->willReturn('myMimeType');
3959
+        $folder->method('getMimeType')->willReturn('myFolderMimeType');
3960
+        $fileWithPreview->method('getMimeType')->willReturn('mimeWithPreview');
3961
+
3962
+        $mountPoint = $this->createMock(IMountPoint::class);
3963
+        $mountPoint->method('getMountType')->willReturn('');
3964
+        $file->method('getMountPoint')->willReturn($mountPoint);
3965
+        $folder->method('getMountPoint')->willReturn($mountPoint);
3966
+        $fileWithPreview->method('getMountPoint')->willReturn($mountPoint);
3967
+
3968
+        $file->method('getPath')->willReturn('file');
3969
+        $folder->method('getPath')->willReturn('folder');
3970
+        $fileWithPreview->method('getPath')->willReturn('fileWithPreview');
3971
+
3972
+        $parent->method('getId')->willReturn(1);
3973
+        $folder->method('getId')->willReturn(2);
3974
+        $file->method('getId')->willReturn(3);
3975
+        $fileWithPreview->method('getId')->willReturn(4);
3976
+
3977
+        $file->method('getParent')->willReturn($parent);
3978
+        $folder->method('getParent')->willReturn($parent);
3979
+        $fileWithPreview->method('getParent')->willReturn($parent);
3980
+
3981
+        $file->method('getSize')->willReturn(123456);
3982
+        $folder->method('getSize')->willReturn(123456);
3983
+        $fileWithPreview->method('getSize')->willReturn(123456);
3984
+        $file->method('getMTime')->willReturn(1234567890);
3985
+        $folder->method('getMTime')->willReturn(1234567890);
3986
+        $fileWithPreview->method('getMTime')->willReturn(1234567890);
3987
+
3988
+        $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
3989
+        $cache->method('getNumericStorageId')->willReturn(100);
3990
+        $storage = $this->createMock(IStorage::class);
3991
+        $storage->method('getId')->willReturn('storageId');
3992
+        $storage->method('getCache')->willReturn($cache);
3993
+
3994
+        $file->method('getStorage')->willReturn($storage);
3995
+        $folder->method('getStorage')->willReturn($storage);
3996
+        $fileWithPreview->method('getStorage')->willReturn($storage);
3997
+
3998
+
3999
+        $mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
4000
+        $mountPoint->method('getMountType')->willReturn('');
4001
+        $file->method('getMountPoint')->willReturn($mountPoint);
4002
+        $folder->method('getMountPoint')->willReturn($mountPoint);
4003
+
4004
+        $owner = $this->getMockBuilder(IUser::class)->getMock();
4005
+        $owner->method('getDisplayName')->willReturn('ownerDN');
4006
+        $initiator = $this->getMockBuilder(IUser::class)->getMock();
4007
+        $initiator->method('getDisplayName')->willReturn('initiatorDN');
4008
+        $recipient = $this->getMockBuilder(IUser::class)->getMock();
4009
+        $recipient->method('getDisplayName')->willReturn('recipientDN');
4010
+        $recipient->method('getSystemEMailAddress')->willReturn('recipient');
4011
+        [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
4012
+
4013
+        $result = [];
4014
+
4015
+        $share = Server::get(IManager::class)->newShare();
4016
+        $share->setShareType(IShare::TYPE_USER)
4017
+            ->setSharedWith('recipient')
4018
+            ->setSharedBy('initiator')
4019
+            ->setShareOwner('owner')
4020
+            ->setPermissions(Constants::PERMISSION_READ)
4021
+            ->setAttributes($shareAttributes)
4022
+            ->setNode($file)
4023
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4024
+            ->setTarget('myTarget')
4025
+            ->setNote('personal note')
4026
+            ->setId(42);
4027
+
4028
+        // User backend down
4029
+        $result[] = [
4030
+            [
4031
+                'id' => '42',
4032
+                'share_type' => IShare::TYPE_USER,
4033
+                'uid_owner' => 'initiator',
4034
+                'displayname_owner' => 'initiator',
4035
+                'permissions' => 1,
4036
+                'attributes' => $shareAttributesReturnJson,
4037
+                'stime' => 946684862,
4038
+                'parent' => null,
4039
+                'expiration' => null,
4040
+                'token' => null,
4041
+                'uid_file_owner' => 'owner',
4042
+                'displayname_file_owner' => 'owner',
4043
+                'path' => 'file',
4044
+                'item_type' => 'file',
4045
+                'storage_id' => 'storageId',
4046
+                'storage' => 100,
4047
+                'item_source' => 3,
4048
+                'file_source' => 3,
4049
+                'file_parent' => 1,
4050
+                'file_target' => 'myTarget',
4051
+                'share_with' => 'recipient',
4052
+                'share_with_displayname' => 'recipient',
4053
+                'share_with_displayname_unique' => 'recipient',
4054
+                'note' => 'personal note',
4055
+                'label' => '',
4056
+                'mail_send' => 0,
4057
+                'mimetype' => 'myMimeType',
4058
+                'has_preview' => false,
4059
+                'hide_download' => 0,
4060
+                'can_edit' => false,
4061
+                'can_delete' => false,
4062
+                'item_size' => 123456,
4063
+                'item_mtime' => 1234567890,
4064
+                'is-mount-root' => false,
4065
+                'mount-type' => '',
4066
+                'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4067
+                'item_permissions' => 1,
4068
+            ], $share, [], false
4069
+        ];
4070
+        // User backend up
4071
+        $result[] = [
4072
+            [
4073
+                'id' => '42',
4074
+                'share_type' => IShare::TYPE_USER,
4075
+                'uid_owner' => 'initiator',
4076
+                'displayname_owner' => 'initiatorDN',
4077
+                'permissions' => 1,
4078
+                'attributes' => $shareAttributesReturnJson,
4079
+                'stime' => 946684862,
4080
+                'parent' => null,
4081
+                'expiration' => null,
4082
+                'token' => null,
4083
+                'uid_file_owner' => 'owner',
4084
+                'displayname_file_owner' => 'ownerDN',
4085
+                'note' => 'personal note',
4086
+                'label' => '',
4087
+                'path' => 'file',
4088
+                'item_type' => 'file',
4089
+                'storage_id' => 'storageId',
4090
+                'storage' => 100,
4091
+                'item_source' => 3,
4092
+                'file_source' => 3,
4093
+                'file_parent' => 1,
4094
+                'file_target' => 'myTarget',
4095
+                'share_with' => 'recipient',
4096
+                'share_with_displayname' => 'recipientDN',
4097
+                'share_with_displayname_unique' => 'recipient',
4098
+                'mail_send' => 0,
4099
+                'mimetype' => 'myMimeType',
4100
+                'has_preview' => false,
4101
+                'hide_download' => 0,
4102
+                'can_edit' => false,
4103
+                'can_delete' => false,
4104
+                'item_size' => 123456,
4105
+                'item_mtime' => 1234567890,
4106
+                'is-mount-root' => false,
4107
+                'mount-type' => '',
4108
+                'attributes' => '[{"scope":"permissions","key":"download","value":true}]',
4109
+                'item_permissions' => 1,
4110
+            ], $share, [
4111
+                ['owner', $owner],
4112
+                ['initiator', $initiator],
4113
+                ['recipient', $recipient],
4114
+            ], false
4115
+        ];
4116
+
4117
+        $share = Server::get(IManager::class)->newShare();
4118
+        $share->setShareType(IShare::TYPE_USER)
4119
+            ->setSharedWith('recipient')
4120
+            ->setSharedBy('initiator')
4121
+            ->setShareOwner('owner')
4122
+            ->setPermissions(Constants::PERMISSION_READ)
4123
+            ->setNode($file)
4124
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4125
+            ->setTarget('myTarget')
4126
+            ->setNote('personal note')
4127
+            ->setId(42);
4128
+        // User backend down
4129
+        $result[] = [
4130
+            [
4131
+                'id' => '42',
4132
+                'share_type' => IShare::TYPE_USER,
4133
+                'uid_owner' => 'initiator',
4134
+                'displayname_owner' => 'initiator',
4135
+                'permissions' => 1,
4136
+                'attributes' => null,
4137
+                'stime' => 946684862,
4138
+                'parent' => null,
4139
+                'expiration' => null,
4140
+                'token' => null,
4141
+                'uid_file_owner' => 'owner',
4142
+                'displayname_file_owner' => 'owner',
4143
+                'note' => 'personal note',
4144
+                'label' => '',
4145
+                'path' => 'file',
4146
+                'item_type' => 'file',
4147
+                'storage_id' => 'storageId',
4148
+                'storage' => 100,
4149
+                'item_source' => 3,
4150
+                'file_source' => 3,
4151
+                'file_parent' => 1,
4152
+                'file_target' => 'myTarget',
4153
+                'share_with' => 'recipient',
4154
+                'share_with_displayname' => 'recipient',
4155
+                'share_with_displayname_unique' => 'recipient',
4156
+                'mail_send' => 0,
4157
+                'mimetype' => 'myMimeType',
4158
+                'has_preview' => false,
4159
+                'hide_download' => 0,
4160
+                'can_edit' => false,
4161
+                'can_delete' => false,
4162
+                'item_size' => 123456,
4163
+                'item_mtime' => 1234567890,
4164
+                'is-mount-root' => false,
4165
+                'mount-type' => '',
4166
+                'attributes' => null,
4167
+                'item_permissions' => 1,
4168
+            ], $share, [], false
4169
+        ];
4170
+
4171
+        $share = Server::get(IManager::class)->newShare();
4172
+        $share->setShareType(IShare::TYPE_USER)
4173
+            ->setSharedWith('recipient')
4174
+            ->setSharedBy('initiator')
4175
+            ->setShareOwner('currentUser')
4176
+            ->setPermissions(Constants::PERMISSION_READ)
4177
+            ->setNode($file)
4178
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4179
+            ->setTarget('myTarget')
4180
+            ->setNote('personal note')
4181
+            ->setId(42);
4182
+        // User backend down
4183
+        $result[] = [
4184
+            [
4185
+                'id' => '42',
4186
+                'share_type' => IShare::TYPE_USER,
4187
+                'uid_owner' => 'initiator',
4188
+                'displayname_owner' => 'initiator',
4189
+                'permissions' => 1,
4190
+                'attributes' => null,
4191
+                'stime' => 946684862,
4192
+                'parent' => null,
4193
+                'expiration' => null,
4194
+                'token' => null,
4195
+                'uid_file_owner' => 'currentUser',
4196
+                'displayname_file_owner' => 'currentUser',
4197
+                'note' => 'personal note',
4198
+                'label' => '',
4199
+                'path' => 'file',
4200
+                'item_type' => 'file',
4201
+                'storage_id' => 'storageId',
4202
+                'storage' => 100,
4203
+                'item_source' => 3,
4204
+                'file_source' => 3,
4205
+                'file_parent' => 1,
4206
+                'file_target' => 'myTarget',
4207
+                'share_with' => 'recipient',
4208
+                'share_with_displayname' => 'recipient',
4209
+                'share_with_displayname_unique' => 'recipient',
4210
+                'mail_send' => 0,
4211
+                'mimetype' => 'myMimeType',
4212
+                'has_preview' => false,
4213
+                'hide_download' => 0,
4214
+                'can_edit' => true,
4215
+                'can_delete' => true,
4216
+                'item_size' => 123456,
4217
+                'item_mtime' => 1234567890,
4218
+                'is-mount-root' => false,
4219
+                'mount-type' => '',
4220
+                'attributes' => null,
4221
+                'item_permissions' => 11,
4222
+            ], $share, [], false
4223
+        ];
4224
+
4225
+        // with existing group
4226
+
4227
+        $share = Server::get(IManager::class)->newShare();
4228
+        $share->setShareType(IShare::TYPE_GROUP)
4229
+            ->setSharedWith('recipientGroup')
4230
+            ->setSharedBy('initiator')
4231
+            ->setShareOwner('owner')
4232
+            ->setPermissions(Constants::PERMISSION_READ)
4233
+            ->setNode($file)
4234
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4235
+            ->setTarget('myTarget')
4236
+            ->setNote('personal note')
4237
+            ->setId(42);
4238
+
4239
+        $result[] = [
4240
+            [
4241
+                'id' => '42',
4242
+                'share_type' => IShare::TYPE_GROUP,
4243
+                'uid_owner' => 'initiator',
4244
+                'displayname_owner' => 'initiator',
4245
+                'permissions' => 1,
4246
+                'attributes' => null,
4247
+                'stime' => 946684862,
4248
+                'parent' => null,
4249
+                'expiration' => null,
4250
+                'token' => null,
4251
+                'uid_file_owner' => 'owner',
4252
+                'displayname_file_owner' => 'owner',
4253
+                'note' => 'personal note',
4254
+                'label' => '',
4255
+                'path' => 'file',
4256
+                'item_type' => 'file',
4257
+                'storage_id' => 'storageId',
4258
+                'storage' => 100,
4259
+                'item_source' => 3,
4260
+                'file_source' => 3,
4261
+                'file_parent' => 1,
4262
+                'file_target' => 'myTarget',
4263
+                'share_with' => 'recipientGroup',
4264
+                'share_with_displayname' => 'recipientGroupDisplayName',
4265
+                'mail_send' => 0,
4266
+                'mimetype' => 'myMimeType',
4267
+                'has_preview' => false,
4268
+                'hide_download' => 0,
4269
+                'can_edit' => false,
4270
+                'can_delete' => false,
4271
+                'item_size' => 123456,
4272
+                'item_mtime' => 1234567890,
4273
+                'is-mount-root' => false,
4274
+                'mount-type' => '',
4275
+                'attributes' => null,
4276
+                'item_permissions' => 1,
4277
+            ], $share, [], false
4278
+        ];
4279
+
4280
+        // with unknown group / no group backend
4281
+        $share = Server::get(IManager::class)->newShare();
4282
+        $share->setShareType(IShare::TYPE_GROUP)
4283
+            ->setSharedWith('recipientGroup2')
4284
+            ->setSharedBy('initiator')
4285
+            ->setShareOwner('owner')
4286
+            ->setPermissions(Constants::PERMISSION_READ)
4287
+            ->setNode($file)
4288
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4289
+            ->setTarget('myTarget')
4290
+            ->setNote('personal note')
4291
+            ->setId(42);
4292
+        $result[] = [
4293
+            [
4294
+                'id' => '42',
4295
+                'share_type' => IShare::TYPE_GROUP,
4296
+                'uid_owner' => 'initiator',
4297
+                'displayname_owner' => 'initiator',
4298
+                'permissions' => 1,
4299
+                'stime' => 946684862,
4300
+                'parent' => null,
4301
+                'expiration' => null,
4302
+                'token' => null,
4303
+                'uid_file_owner' => 'owner',
4304
+                'displayname_file_owner' => 'owner',
4305
+                'note' => 'personal note',
4306
+                'label' => '',
4307
+                'path' => 'file',
4308
+                'item_type' => 'file',
4309
+                'storage_id' => 'storageId',
4310
+                'storage' => 100,
4311
+                'item_source' => 3,
4312
+                'file_source' => 3,
4313
+                'file_parent' => 1,
4314
+                'file_target' => 'myTarget',
4315
+                'share_with' => 'recipientGroup2',
4316
+                'share_with_displayname' => 'recipientGroup2',
4317
+                'mail_send' => 0,
4318
+                'mimetype' => 'myMimeType',
4319
+                'has_preview' => false,
4320
+                'hide_download' => 0,
4321
+                'can_edit' => false,
4322
+                'can_delete' => false,
4323
+                'item_size' => 123456,
4324
+                'item_mtime' => 1234567890,
4325
+                'is-mount-root' => false,
4326
+                'mount-type' => '',
4327
+                'attributes' => null,
4328
+                'item_permissions' => 1,
4329
+            ], $share, [], false
4330
+        ];
4331
+
4332
+        $share = Server::get(IManager::class)->newShare();
4333
+        $share->setShareType(IShare::TYPE_LINK)
4334
+            ->setSharedBy('initiator')
4335
+            ->setShareOwner('owner')
4336
+            ->setPermissions(Constants::PERMISSION_READ)
4337
+            ->setNode($file)
4338
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4339
+            ->setTarget('myTarget')
4340
+            ->setPassword('mypassword')
4341
+            ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4342
+            ->setToken('myToken')
4343
+            ->setNote('personal note')
4344
+            ->setLabel('new link share')
4345
+            ->setId(42);
4346
+
4347
+        $result[] = [
4348
+            [
4349
+                'id' => '42',
4350
+                'share_type' => IShare::TYPE_LINK,
4351
+                'uid_owner' => 'initiator',
4352
+                'displayname_owner' => 'initiator',
4353
+                'permissions' => 1,
4354
+                'attributes' => null,
4355
+                'stime' => 946684862,
4356
+                'parent' => null,
4357
+                'expiration' => '2001-01-02 00:00:00',
4358
+                'token' => 'myToken',
4359
+                'uid_file_owner' => 'owner',
4360
+                'displayname_file_owner' => 'owner',
4361
+                'note' => 'personal note',
4362
+                'label' => 'new link share',
4363
+                'path' => 'file',
4364
+                'item_type' => 'file',
4365
+                'storage_id' => 'storageId',
4366
+                'storage' => 100,
4367
+                'item_source' => 3,
4368
+                'file_source' => 3,
4369
+                'file_parent' => 1,
4370
+                'file_target' => 'myTarget',
4371
+                'password' => 'mypassword',
4372
+                'share_with' => 'mypassword',
4373
+                'share_with_displayname' => '(Shared link)',
4374
+                'send_password_by_talk' => false,
4375
+                'mail_send' => 0,
4376
+                'url' => 'myLink',
4377
+                'mimetype' => 'myMimeType',
4378
+                'has_preview' => false,
4379
+                'hide_download' => 0,
4380
+                'can_edit' => false,
4381
+                'can_delete' => false,
4382
+                'item_size' => 123456,
4383
+                'item_mtime' => 1234567890,
4384
+                'is-mount-root' => false,
4385
+                'mount-type' => '',
4386
+                'attributes' => null,
4387
+                'item_permissions' => 1,
4388
+            ], $share, [], false
4389
+        ];
4390
+
4391
+        $share = Server::get(IManager::class)->newShare();
4392
+        $share->setShareType(IShare::TYPE_LINK)
4393
+            ->setSharedBy('initiator')
4394
+            ->setShareOwner('owner')
4395
+            ->setPermissions(Constants::PERMISSION_READ)
4396
+            ->setNode($file)
4397
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4398
+            ->setTarget('myTarget')
4399
+            ->setPassword('mypassword')
4400
+            ->setSendPasswordByTalk(true)
4401
+            ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
4402
+            ->setToken('myToken')
4403
+            ->setNote('personal note')
4404
+            ->setLabel('new link share')
4405
+            ->setId(42);
4406
+
4407
+        $result[] = [
4408
+            [
4409
+                'id' => '42',
4410
+                'share_type' => IShare::TYPE_LINK,
4411
+                'uid_owner' => 'initiator',
4412
+                'displayname_owner' => 'initiator',
4413
+                'permissions' => 1,
4414
+                'stime' => 946684862,
4415
+                'parent' => null,
4416
+                'expiration' => '2001-01-02 00:00:00',
4417
+                'token' => 'myToken',
4418
+                'uid_file_owner' => 'owner',
4419
+                'displayname_file_owner' => 'owner',
4420
+                'note' => 'personal note',
4421
+                'label' => 'new link share',
4422
+                'path' => 'file',
4423
+                'item_type' => 'file',
4424
+                'storage_id' => 'storageId',
4425
+                'storage' => 100,
4426
+                'item_source' => 3,
4427
+                'file_source' => 3,
4428
+                'file_parent' => 1,
4429
+                'file_target' => 'myTarget',
4430
+                'password' => 'mypassword',
4431
+                'share_with' => 'mypassword',
4432
+                'share_with_displayname' => '(Shared link)',
4433
+                'send_password_by_talk' => true,
4434
+                'mail_send' => 0,
4435
+                'url' => 'myLink',
4436
+                'mimetype' => 'myMimeType',
4437
+                'has_preview' => false,
4438
+                'hide_download' => 0,
4439
+                'can_edit' => false,
4440
+                'can_delete' => false,
4441
+                'item_size' => 123456,
4442
+                'item_mtime' => 1234567890,
4443
+                'is-mount-root' => false,
4444
+                'mount-type' => '',
4445
+                'attributes' => null,
4446
+                'item_permissions' => 1,
4447
+            ], $share, [], false
4448
+        ];
4449
+
4450
+        $share = Server::get(IManager::class)->newShare();
4451
+        $share->setShareType(IShare::TYPE_REMOTE)
4452
+            ->setSharedBy('initiator')
4453
+            ->setSharedWith('[email protected]')
4454
+            ->setShareOwner('owner')
4455
+            ->setPermissions(Constants::PERMISSION_READ)
4456
+            ->setNode($folder)
4457
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4458
+            ->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4459
+            ->setTarget('myTarget')
4460
+            ->setNote('personal note')
4461
+            ->setId(42);
4462
+
4463
+        $result[] = [
4464
+            [
4465
+                'id' => '42',
4466
+                'share_type' => IShare::TYPE_REMOTE,
4467
+                'uid_owner' => 'initiator',
4468
+                'displayname_owner' => 'initiator',
4469
+                'permissions' => 1,
4470
+                'stime' => 946684862,
4471
+                'parent' => null,
4472
+                'expiration' => '2001-02-03 00:00:00',
4473
+                'token' => null,
4474
+                'uid_file_owner' => 'owner',
4475
+                'displayname_file_owner' => 'owner',
4476
+                'note' => 'personal note',
4477
+                'label' => '',
4478
+                'path' => 'folder',
4479
+                'item_type' => 'folder',
4480
+                'storage_id' => 'storageId',
4481
+                'storage' => 100,
4482
+                'item_source' => 2,
4483
+                'file_source' => 2,
4484
+                'file_parent' => 1,
4485
+                'file_target' => 'myTarget',
4486
+                'share_with' => '[email protected]',
4487
+                'share_with_displayname' => 'foobar',
4488
+                'mail_send' => 0,
4489
+                'mimetype' => 'myFolderMimeType',
4490
+                'has_preview' => false,
4491
+                'hide_download' => 0,
4492
+                'can_edit' => false,
4493
+                'can_delete' => false,
4494
+                'item_size' => 123456,
4495
+                'item_mtime' => 1234567890,
4496
+                'is-mount-root' => false,
4497
+                'mount-type' => '',
4498
+                'attributes' => null,
4499
+                'item_permissions' => 1,
4500
+            ], $share, [], false
4501
+        ];
4502
+
4503
+        $share = Server::get(IManager::class)->newShare();
4504
+        $share->setShareType(IShare::TYPE_REMOTE_GROUP)
4505
+            ->setSharedBy('initiator')
4506
+            ->setSharedWith('[email protected]')
4507
+            ->setShareOwner('owner')
4508
+            ->setPermissions(Constants::PERMISSION_READ)
4509
+            ->setNode($folder)
4510
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4511
+            ->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
4512
+            ->setTarget('myTarget')
4513
+            ->setNote('personal note')
4514
+            ->setId(42);
4515
+
4516
+        $result[] = [
4517
+            [
4518
+                'id' => '42',
4519
+                'share_type' => IShare::TYPE_REMOTE_GROUP,
4520
+                'uid_owner' => 'initiator',
4521
+                'displayname_owner' => 'initiator',
4522
+                'permissions' => 1,
4523
+                'stime' => 946684862,
4524
+                'parent' => null,
4525
+                'expiration' => '2001-02-03 00:00:00',
4526
+                'token' => null,
4527
+                'uid_file_owner' => 'owner',
4528
+                'displayname_file_owner' => 'owner',
4529
+                'note' => 'personal note',
4530
+                'label' => '',
4531
+                'path' => 'folder',
4532
+                'item_type' => 'folder',
4533
+                'storage_id' => 'storageId',
4534
+                'storage' => 100,
4535
+                'item_source' => 2,
4536
+                'file_source' => 2,
4537
+                'file_parent' => 1,
4538
+                'file_target' => 'myTarget',
4539
+                'share_with' => '[email protected]',
4540
+                'share_with_displayname' => 'foobar',
4541
+                'mail_send' => 0,
4542
+                'mimetype' => 'myFolderMimeType',
4543
+                'has_preview' => false,
4544
+                'hide_download' => 0,
4545
+                'can_edit' => false,
4546
+                'can_delete' => false,
4547
+                'item_size' => 123456,
4548
+                'item_mtime' => 1234567890,
4549
+                'is-mount-root' => false,
4550
+                'mount-type' => '',
4551
+                'attributes' => null,
4552
+                'item_permissions' => 1,
4553
+            ], $share, [], false
4554
+        ];
4555
+
4556
+        // Circle with id, display name and avatar set by the Circles app
4557
+        $share = Server::get(IManager::class)->newShare();
4558
+        $share->setShareType(IShare::TYPE_CIRCLE)
4559
+            ->setSharedBy('initiator')
4560
+            ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4561
+            ->setSharedWithDisplayName('The display name')
4562
+            ->setSharedWithAvatar('path/to/the/avatar')
4563
+            ->setShareOwner('owner')
4564
+            ->setPermissions(Constants::PERMISSION_READ)
4565
+            ->setNode($folder)
4566
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4567
+            ->setTarget('myTarget')
4568
+            ->setId(42);
4569
+
4570
+        $result[] = [
4571
+            [
4572
+                'id' => '42',
4573
+                'share_type' => IShare::TYPE_CIRCLE,
4574
+                'uid_owner' => 'initiator',
4575
+                'displayname_owner' => 'initiator',
4576
+                'permissions' => 1,
4577
+                'attributes' => null,
4578
+                'stime' => 946684862,
4579
+                'parent' => null,
4580
+                'expiration' => null,
4581
+                'token' => null,
4582
+                'uid_file_owner' => 'owner',
4583
+                'displayname_file_owner' => 'owner',
4584
+                'note' => '',
4585
+                'label' => '',
4586
+                'path' => 'folder',
4587
+                'item_type' => 'folder',
4588
+                'storage_id' => 'storageId',
4589
+                'storage' => 100,
4590
+                'item_source' => 2,
4591
+                'file_source' => 2,
4592
+                'file_parent' => 1,
4593
+                'file_target' => 'myTarget',
4594
+                'share_with' => '4815162342',
4595
+                'share_with_displayname' => 'The display name',
4596
+                'share_with_avatar' => 'path/to/the/avatar',
4597
+                'mail_send' => 0,
4598
+                'mimetype' => 'myFolderMimeType',
4599
+                'has_preview' => false,
4600
+                'hide_download' => 0,
4601
+                'can_edit' => false,
4602
+                'can_delete' => false,
4603
+                'item_size' => 123456,
4604
+                'item_mtime' => 1234567890,
4605
+                'is-mount-root' => false,
4606
+                'mount-type' => '',
4607
+                'attributes' => null,
4608
+                'item_permissions' => 1,
4609
+            ], $share, [], false
4610
+        ];
4611
+
4612
+        // Circle with id set by the Circles app
4613
+        $share = Server::get(IManager::class)->newShare();
4614
+        $share->setShareType(IShare::TYPE_CIRCLE)
4615
+            ->setSharedBy('initiator')
4616
+            ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
4617
+            ->setShareOwner('owner')
4618
+            ->setPermissions(Constants::PERMISSION_READ)
4619
+            ->setNode($folder)
4620
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4621
+            ->setTarget('myTarget')
4622
+            ->setId(42);
4623
+
4624
+        $result[] = [
4625
+            [
4626
+                'id' => '42',
4627
+                'share_type' => IShare::TYPE_CIRCLE,
4628
+                'uid_owner' => 'initiator',
4629
+                'displayname_owner' => 'initiator',
4630
+                'permissions' => 1,
4631
+                'stime' => 946684862,
4632
+                'parent' => null,
4633
+                'expiration' => null,
4634
+                'token' => null,
4635
+                'uid_file_owner' => 'owner',
4636
+                'displayname_file_owner' => 'owner',
4637
+                'note' => '',
4638
+                'label' => '',
4639
+                'path' => 'folder',
4640
+                'item_type' => 'folder',
4641
+                'storage_id' => 'storageId',
4642
+                'storage' => 100,
4643
+                'item_source' => 2,
4644
+                'file_source' => 2,
4645
+                'file_parent' => 1,
4646
+                'file_target' => 'myTarget',
4647
+                'share_with' => '4815162342',
4648
+                'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4649
+                'share_with_avatar' => '',
4650
+                'mail_send' => 0,
4651
+                'mimetype' => 'myFolderMimeType',
4652
+                'has_preview' => false,
4653
+                'hide_download' => 0,
4654
+                'can_edit' => false,
4655
+                'can_delete' => false,
4656
+                'item_size' => 123456,
4657
+                'item_mtime' => 1234567890,
4658
+                'is-mount-root' => false,
4659
+                'mount-type' => '',
4660
+                'attributes' => null,
4661
+                'item_permissions' => 1,
4662
+            ], $share, [], false
4663
+        ];
4664
+
4665
+        // Circle with id not set by the Circles app
4666
+        $share = Server::get(IManager::class)->newShare();
4667
+        $share->setShareType(IShare::TYPE_CIRCLE)
4668
+            ->setSharedBy('initiator')
4669
+            ->setSharedWith('Circle (Public circle, circleOwner)')
4670
+            ->setShareOwner('owner')
4671
+            ->setPermissions(Constants::PERMISSION_READ)
4672
+            ->setNode($folder)
4673
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4674
+            ->setTarget('myTarget')
4675
+            ->setId(42);
4676
+
4677
+        $result[] = [
4678
+            [
4679
+                'id' => '42',
4680
+                'share_type' => IShare::TYPE_CIRCLE,
4681
+                'uid_owner' => 'initiator',
4682
+                'displayname_owner' => 'initiator',
4683
+                'permissions' => 1,
4684
+                'stime' => 946684862,
4685
+                'parent' => null,
4686
+                'expiration' => null,
4687
+                'token' => null,
4688
+                'uid_file_owner' => 'owner',
4689
+                'displayname_file_owner' => 'owner',
4690
+                'note' => '',
4691
+                'label' => '',
4692
+                'path' => 'folder',
4693
+                'item_type' => 'folder',
4694
+                'storage_id' => 'storageId',
4695
+                'storage' => 100,
4696
+                'item_source' => 2,
4697
+                'file_source' => 2,
4698
+                'file_parent' => 1,
4699
+                'file_target' => 'myTarget',
4700
+                'share_with' => 'Circle',
4701
+                'share_with_displayname' => 'Circle (Public circle, circleOwner)',
4702
+                'share_with_avatar' => '',
4703
+                'mail_send' => 0,
4704
+                'mimetype' => 'myFolderMimeType',
4705
+                'has_preview' => false,
4706
+                'hide_download' => 0,
4707
+                'can_edit' => false,
4708
+                'can_delete' => false,
4709
+                'item_size' => 123456,
4710
+                'item_mtime' => 1234567890,
4711
+                'is-mount-root' => false,
4712
+                'mount-type' => '',
4713
+                'attributes' => null,
4714
+                'item_permissions' => 1,
4715
+            ], $share, [], false
4716
+        ];
4717
+
4718
+        $share = Server::get(IManager::class)->newShare();
4719
+        $share->setShareType(IShare::TYPE_USER)
4720
+            ->setSharedBy('initiator')
4721
+            ->setSharedWith('recipient')
4722
+            ->setShareOwner('owner')
4723
+            ->setPermissions(Constants::PERMISSION_READ)
4724
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4725
+            ->setTarget('myTarget')
4726
+            ->setNote('personal note')
4727
+            ->setId(42);
4728
+
4729
+        $result[] = [
4730
+            [], $share, [], true
4731
+        ];
4732
+
4733
+        $share = Server::get(IManager::class)->newShare();
4734
+        $share->setShareType(IShare::TYPE_EMAIL)
4735
+            ->setSharedBy('initiator')
4736
+            ->setSharedWith('[email protected]')
4737
+            ->setShareOwner('owner')
4738
+            ->setPermissions(Constants::PERMISSION_READ)
4739
+            ->setNode($folder)
4740
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4741
+            ->setTarget('myTarget')
4742
+            ->setId(42)
4743
+            ->setPassword('password');
4744
+
4745
+        $result[] = [
4746
+            [
4747
+                'id' => '42',
4748
+                'share_type' => IShare::TYPE_EMAIL,
4749
+                'uid_owner' => 'initiator',
4750
+                'displayname_owner' => 'initiator',
4751
+                'permissions' => 1,
4752
+                'stime' => 946684862,
4753
+                'parent' => null,
4754
+                'expiration' => null,
4755
+                'token' => null,
4756
+                'uid_file_owner' => 'owner',
4757
+                'displayname_file_owner' => 'owner',
4758
+                'note' => '',
4759
+                'label' => '',
4760
+                'path' => 'folder',
4761
+                'item_type' => 'folder',
4762
+                'storage_id' => 'storageId',
4763
+                'storage' => 100,
4764
+                'item_source' => 2,
4765
+                'file_source' => 2,
4766
+                'file_parent' => 1,
4767
+                'file_target' => 'myTarget',
4768
+                'share_with' => '[email protected]',
4769
+                'share_with_displayname' => 'mail display name',
4770
+                'mail_send' => 0,
4771
+                'mimetype' => 'myFolderMimeType',
4772
+                'has_preview' => false,
4773
+                'password' => 'password',
4774
+                'send_password_by_talk' => false,
4775
+                'hide_download' => 0,
4776
+                'can_edit' => false,
4777
+                'can_delete' => false,
4778
+                'password_expiration_time' => null,
4779
+                'item_size' => 123456,
4780
+                'item_mtime' => 1234567890,
4781
+                'is-mount-root' => false,
4782
+                'mount-type' => '',
4783
+                'attributes' => null,
4784
+                'item_permissions' => 1,
4785
+            ], $share, [], false
4786
+        ];
4787
+
4788
+        $share = Server::get(IManager::class)->newShare();
4789
+        $share->setShareType(IShare::TYPE_EMAIL)
4790
+            ->setSharedBy('initiator')
4791
+            ->setSharedWith('[email protected]')
4792
+            ->setShareOwner('owner')
4793
+            ->setPermissions(Constants::PERMISSION_READ)
4794
+            ->setNode($folder)
4795
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4796
+            ->setTarget('myTarget')
4797
+            ->setId(42)
4798
+            ->setPassword('password')
4799
+            ->setSendPasswordByTalk(true);
4800
+
4801
+        $result[] = [
4802
+            [
4803
+                'id' => '42',
4804
+                'share_type' => IShare::TYPE_EMAIL,
4805
+                'uid_owner' => 'initiator',
4806
+                'displayname_owner' => 'initiator',
4807
+                'permissions' => 1,
4808
+                'stime' => 946684862,
4809
+                'parent' => null,
4810
+                'expiration' => null,
4811
+                'token' => null,
4812
+                'uid_file_owner' => 'owner',
4813
+                'displayname_file_owner' => 'owner',
4814
+                'note' => '',
4815
+                'label' => '',
4816
+                'path' => 'folder',
4817
+                'item_type' => 'folder',
4818
+                'storage_id' => 'storageId',
4819
+                'storage' => 100,
4820
+                'item_source' => 2,
4821
+                'file_source' => 2,
4822
+                'file_parent' => 1,
4823
+                'file_target' => 'myTarget',
4824
+                'share_with' => '[email protected]',
4825
+                'share_with_displayname' => 'mail display name',
4826
+                'mail_send' => 0,
4827
+                'mimetype' => 'myFolderMimeType',
4828
+                'has_preview' => false,
4829
+                'password' => 'password',
4830
+                'send_password_by_talk' => true,
4831
+                'hide_download' => 0,
4832
+                'can_edit' => false,
4833
+                'can_delete' => false,
4834
+                'password_expiration_time' => null,
4835
+                'item_size' => 123456,
4836
+                'item_mtime' => 1234567890,
4837
+                'is-mount-root' => false,
4838
+                'mount-type' => '',
4839
+                'attributes' => null,
4840
+                'item_permissions' => 1,
4841
+            ], $share, [], false
4842
+        ];
4843
+
4844
+        // Preview is available
4845
+        $share = Server::get(IManager::class)->newShare();
4846
+        $share->setShareType(IShare::TYPE_USER)
4847
+            ->setSharedWith('recipient')
4848
+            ->setSharedBy('initiator')
4849
+            ->setShareOwner('currentUser')
4850
+            ->setPermissions(Constants::PERMISSION_READ)
4851
+            ->setNode($fileWithPreview)
4852
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
4853
+            ->setTarget('myTarget')
4854
+            ->setNote('personal note')
4855
+            ->setId(42);
4856
+
4857
+        $result[] = [
4858
+            [
4859
+                'id' => '42',
4860
+                'share_type' => IShare::TYPE_USER,
4861
+                'uid_owner' => 'initiator',
4862
+                'displayname_owner' => 'initiator',
4863
+                'permissions' => 1,
4864
+                'stime' => 946684862,
4865
+                'parent' => null,
4866
+                'expiration' => null,
4867
+                'token' => null,
4868
+                'uid_file_owner' => 'currentUser',
4869
+                'displayname_file_owner' => 'currentUser',
4870
+                'note' => 'personal note',
4871
+                'label' => '',
4872
+                'path' => 'fileWithPreview',
4873
+                'item_type' => 'file',
4874
+                'storage_id' => 'storageId',
4875
+                'storage' => 100,
4876
+                'item_source' => 4,
4877
+                'file_source' => 4,
4878
+                'file_parent' => 1,
4879
+                'file_target' => 'myTarget',
4880
+                'share_with' => 'recipient',
4881
+                'share_with_displayname' => 'recipient',
4882
+                'share_with_displayname_unique' => 'recipient',
4883
+                'mail_send' => 0,
4884
+                'mimetype' => 'mimeWithPreview',
4885
+                'has_preview' => true,
4886
+                'hide_download' => 0,
4887
+                'can_edit' => true,
4888
+                'can_delete' => true,
4889
+                'item_size' => 123456,
4890
+                'item_mtime' => 1234567890,
4891
+                'is-mount-root' => false,
4892
+                'mount-type' => '',
4893
+                'attributes' => null,
4894
+                'item_permissions' => 11,
4895
+            ], $share, [], false
4896
+        ];
4897
+
4898
+        return $result;
4899
+    }
4900
+
4901
+    /**
4902
+     * @dataProvider dataFormatShare
4903
+     *
4904
+     * @param array $expects
4905
+     * @param IShare $share
4906
+     * @param array $users
4907
+     * @param $exception
4908
+     */
4909
+    public function testFormatShare(array $expects, IShare $share, array $users, $exception): void {
4910
+        $this->userManager->method('get')->willReturnMap($users);
4911
+
4912
+        $recipientGroup = $this->createMock(IGroup::class);
4913
+        $recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
4914
+        $this->groupManager->method('get')->willReturnMap([
4915
+            ['recipientGroup', $recipientGroup],
4916
+        ]);
4917
+
4918
+        $this->urlGenerator->method('linkToRouteAbsolute')
4919
+            ->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
4920
+            ->willReturn('myLink');
4921
+
4922
+        $this->rootFolder->method('getUserFolder')
4923
+            ->with($this->currentUser)
4924
+            ->willReturnSelf();
4925
+        $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
4926
+
4927
+        if (!$exception) {
4928
+            $this->rootFolder->method('getFirstNodeById')
4929
+                ->with($share->getNodeId())
4930
+                ->willReturn($share->getNode());
4931
+
4932
+            $this->rootFolder->method('getRelativePath')
4933
+                ->with($share->getNode()->getPath())
4934
+                ->willReturnArgument(0);
4935
+        }
4936
+
4937
+        $cm = $this->createMock(\OCP\Contacts\IManager::class);
4938
+        $this->overwriteService(\OCP\Contacts\IManager::class, $cm);
4939
+
4940
+        $cm->method('search')
4941
+            ->willReturnMap([
4942
+                ['[email protected]', ['CLOUD'], [
4943
+                    'limit' => 1,
4944
+                    'enumeration' => false,
4945
+                    'strict_search' => true,
4946
+                ],
4947
+                    [
4948
+                        [
4949
+                            'CLOUD' => [
4950
+                                '[email protected]',
4951
+                            ],
4952
+                            'FN' => 'foobar',
4953
+                        ],
4954
+                    ],
4955
+                ],
4956
+                ['[email protected]', ['EMAIL'], [
4957
+                    'limit' => 1,
4958
+                    'enumeration' => false,
4959
+                    'strict_search' => true,
4960
+                ],
4961
+                    [
4962
+                        [
4963
+                            'EMAIL' => [
4964
+                                '[email protected]',
4965
+                            ],
4966
+                            'FN' => 'mail display name',
4967
+                        ],
4968
+                    ],
4969
+                ],
4970
+            ]);
4971
+
4972
+        try {
4973
+            $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
4974
+            $this->assertFalse($exception);
4975
+            $this->assertEquals($expects, $result);
4976
+        } catch (NotFoundException $e) {
4977
+            $this->assertTrue($exception);
4978
+        }
4979
+    }
4980
+
4981
+    public function dataFormatRoomShare() {
4982
+        $file = $this->getMockBuilder(File::class)->getMock();
4983
+        $parent = $this->getMockBuilder(Folder::class)->getMock();
4984
+
4985
+        $file->method('getMimeType')->willReturn('myMimeType');
4986
+
4987
+        $file->method('getPath')->willReturn('file');
4988
+
4989
+        $parent->method('getId')->willReturn(1);
4990
+        $file->method('getId')->willReturn(3);
4991
+
4992
+        $file->method('getParent')->willReturn($parent);
4993
+
4994
+        $file->method('getSize')->willReturn(123456);
4995
+        $file->method('getMTime')->willReturn(1234567890);
4996
+
4997
+        $mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock();
4998
+        $mountPoint->method('getMountType')->willReturn('');
4999
+        $file->method('getMountPoint')->willReturn($mountPoint);
5000
+
5001
+        $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
5002
+        $cache->method('getNumericStorageId')->willReturn(100);
5003
+        $storage = $this->createMock(IStorage::class);
5004
+        $storage->method('getId')->willReturn('storageId');
5005
+        $storage->method('getCache')->willReturn($cache);
5006
+
5007
+        $file->method('getStorage')->willReturn($storage);
5008
+
5009
+        $result = [];
5010
+
5011
+        $share = Server::get(IManager::class)->newShare();
5012
+        $share->setShareType(IShare::TYPE_ROOM)
5013
+            ->setSharedWith('recipientRoom')
5014
+            ->setSharedBy('initiator')
5015
+            ->setShareOwner('owner')
5016
+            ->setPermissions(Constants::PERMISSION_READ)
5017
+            ->setNode($file)
5018
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
5019
+            ->setTarget('myTarget')
5020
+            ->setNote('personal note')
5021
+            ->setId(42);
5022
+
5023
+        $result[] = [
5024
+            [
5025
+                'id' => '42',
5026
+                'share_type' => IShare::TYPE_ROOM,
5027
+                'uid_owner' => 'initiator',
5028
+                'displayname_owner' => 'initiator',
5029
+                'permissions' => 1,
5030
+                'stime' => 946684862,
5031
+                'parent' => null,
5032
+                'expiration' => null,
5033
+                'token' => null,
5034
+                'uid_file_owner' => 'owner',
5035
+                'displayname_file_owner' => 'owner',
5036
+                'note' => 'personal note',
5037
+                'path' => 'file',
5038
+                'item_type' => 'file',
5039
+                'storage_id' => 'storageId',
5040
+                'storage' => 100,
5041
+                'item_source' => 3,
5042
+                'file_source' => 3,
5043
+                'file_parent' => 1,
5044
+                'file_target' => 'myTarget',
5045
+                'share_with' => 'recipientRoom',
5046
+                'share_with_displayname' => '',
5047
+                'mail_send' => 0,
5048
+                'mimetype' => 'myMimeType',
5049
+                'has_preview' => false,
5050
+                'hide_download' => 0,
5051
+                'label' => '',
5052
+                'can_edit' => false,
5053
+                'can_delete' => false,
5054
+                'item_size' => 123456,
5055
+                'item_mtime' => 1234567890,
5056
+                'is-mount-root' => false,
5057
+                'mount-type' => '',
5058
+                'attributes' => null,
5059
+                'item_permissions' => 1,
5060
+            ], $share, false, []
5061
+        ];
5062
+
5063
+        $share = Server::get(IManager::class)->newShare();
5064
+        $share->setShareType(IShare::TYPE_ROOM)
5065
+            ->setSharedWith('recipientRoom')
5066
+            ->setSharedBy('initiator')
5067
+            ->setShareOwner('owner')
5068
+            ->setPermissions(Constants::PERMISSION_READ)
5069
+            ->setNode($file)
5070
+            ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
5071
+            ->setTarget('myTarget')
5072
+            ->setNote('personal note')
5073
+            ->setId(42);
5074
+
5075
+        $result[] = [
5076
+            [
5077
+                'id' => '42',
5078
+                'share_type' => IShare::TYPE_ROOM,
5079
+                'uid_owner' => 'initiator',
5080
+                'displayname_owner' => 'initiator',
5081
+                'permissions' => 1,
5082
+                'stime' => 946684862,
5083
+                'parent' => null,
5084
+                'expiration' => null,
5085
+                'token' => null,
5086
+                'uid_file_owner' => 'owner',
5087
+                'displayname_file_owner' => 'owner',
5088
+                'note' => 'personal note',
5089
+                'path' => 'file',
5090
+                'item_type' => 'file',
5091
+                'storage_id' => 'storageId',
5092
+                'storage' => 100,
5093
+                'item_source' => 3,
5094
+                'file_source' => 3,
5095
+                'file_parent' => 1,
5096
+                'file_target' => 'myTarget',
5097
+                'share_with' => 'recipientRoom',
5098
+                'share_with_displayname' => 'recipientRoomName',
5099
+                'mail_send' => 0,
5100
+                'mimetype' => 'myMimeType',
5101
+                'has_preview' => false,
5102
+                'hide_download' => 0,
5103
+                'label' => '',
5104
+                'can_edit' => false,
5105
+                'can_delete' => false,
5106
+                'item_size' => 123456,
5107
+                'item_mtime' => 1234567890,
5108
+                'is-mount-root' => false,
5109
+                'mount-type' => '',
5110
+                'attributes' => null,
5111
+                'item_permissions' => 9,
5112
+            ], $share, true, [
5113
+                'share_with_displayname' => 'recipientRoomName'
5114
+            ]
5115
+        ];
5116
+
5117
+        return $result;
5118
+    }
5119
+
5120
+    /**
5121
+     * @dataProvider dataFormatRoomShare
5122
+     *
5123
+     * @param array $expects
5124
+     * @param IShare $share
5125
+     * @param bool $helperAvailable
5126
+     * @param array $formatShareByHelper
5127
+     */
5128
+    public function testFormatRoomShare(array $expects, IShare $share, bool $helperAvailable, array $formatShareByHelper): void {
5129
+        $this->rootFolder->method('getUserFolder')
5130
+            ->with($this->currentUser)
5131
+            ->willReturnSelf();
5132
+
5133
+        $this->rootFolder->method('getFirstNodeById')
5134
+            ->with($share->getNodeId())
5135
+            ->willReturn($share->getNode());
5136
+
5137
+        $this->rootFolder->method('getRelativePath')
5138
+            ->with($share->getNode()->getPath())
5139
+            ->willReturnArgument(0);
5140
+
5141
+        if (!$helperAvailable) {
5142
+            $this->appManager->method('isEnabledForUser')
5143
+                ->with('spreed')
5144
+                ->willReturn(false);
5145
+        } else {
5146
+            $this->appManager->method('isEnabledForUser')
5147
+                ->with('spreed')
5148
+                ->willReturn(true);
5149
+
5150
+            // This is not possible anymore with PHPUnit 10+
5151
+            // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
5152
+            // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
5153
+            $helper = $this->getMockBuilder(\stdClass::class)
5154
+                ->addMethods(['formatShare', 'canAccessShare'])
5155
+                ->getMock();
5156
+            $helper->method('formatShare')
5157
+                ->with($share)
5158
+                ->willReturn($formatShareByHelper);
5159
+            $helper->method('canAccessShare')
5160
+                ->with($share)
5161
+                ->willReturn(true);
5162
+
5163
+            $this->serverContainer->method('get')
5164
+                ->with('\OCA\Talk\Share\Helper\ShareAPIController')
5165
+                ->willReturn($helper);
5166
+        }
5167
+
5168
+        $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
5169
+        $this->assertEquals($expects, $result);
5170
+    }
5171
+
5172
+    /**
5173
+     * @return list{Folder, Folder}
5174
+     */
5175
+    private function getNonSharedUserFolder(): array {
5176
+        $node = $this->getMockBuilder(Folder::class)->getMock();
5177
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
5178
+        $storage = $this->createMock(IStorage::class);
5179
+        $storage->method('instanceOfStorage')
5180
+            ->willReturnMap([
5181
+                ['OCA\Files_Sharing\External\Storage', false],
5182
+                ['OCA\Files_Sharing\SharedStorage', false],
5183
+            ]);
5184
+        $userFolder->method('getStorage')->willReturn($storage);
5185
+        $node->method('getStorage')->willReturn($storage);
5186
+        $node->method('getId')->willReturn(42);
5187
+        $user = $this->createMock(IUser::class);
5188
+        $user->method('getUID')->willReturn($this->currentUser);
5189
+        $node->method('getOwner')->willReturn($user);
5190
+        return [$userFolder, $node];
5191
+    }
5192
+
5193
+    /**
5194
+     * @return list{Folder, File}
5195
+     */
5196
+    private function getNonSharedUserFile(): array {
5197
+        $node = $this->getMockBuilder(File::class)->getMock();
5198
+        $userFolder = $this->getMockBuilder(Folder::class)->getMock();
5199
+        $storage = $this->createMock(IStorage::class);
5200
+        $storage->method('instanceOfStorage')
5201
+            ->willReturnMap([
5202
+                ['OCA\Files_Sharing\External\Storage', false],
5203
+                ['OCA\Files_Sharing\SharedStorage', false],
5204
+            ]);
5205
+        $userFolder->method('getStorage')->willReturn($storage);
5206
+        $node->method('getStorage')->willReturn($storage);
5207
+        $node->method('getId')->willReturn(42);
5208
+        return [$userFolder, $node];
5209
+    }
5210
+
5211
+    public function testPopulateTags(): void {
5212
+        $tagger = $this->createMock(ITags::class);
5213
+        $this->tagManager->method('load')
5214
+            ->with('files')
5215
+            ->willReturn($tagger);
5216
+        $data = [
5217
+            ['file_source' => 10],
5218
+            ['file_source' => 22, 'foo' => 'bar'],
5219
+            ['file_source' => 42, 'x' => 'y'],
5220
+        ];
5221
+        $tags = [
5222
+            10 => ['tag3'],
5223
+            42 => ['tag1', 'tag2'],
5224
+        ];
5225
+        $tagger->method('getTagsForObjects')
5226
+            ->with([10, 22, 42])
5227
+            ->willReturn($tags);
5228
+
5229
+        $result = self::invokePrivate($this->ocs, 'populateTags', [$data]);
5230
+        $this->assertSame([
5231
+            ['file_source' => 10, 'tags' => ['tag3']],
5232
+            ['file_source' => 22, 'foo' => 'bar', 'tags' => []],
5233
+            ['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
5234
+        ], $result);
5235
+    }
5236 5236
 }
Please login to merge, or discard this patch.