Skip to content

Commit 3460b69

Browse files
authored
Merge pull request #392 from nextcloud/better-testing
Improve testing
2 parents a4c2341 + c1d7fda commit 3460b69

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

lib/Service/ApprovalService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ public function request(int $fileId, int $ruleId, ?string $userId, bool $createS
451451
return ['error' => $this->l10n->t('You do not have access to this file')];
452452
}
453453

454+
if ($createShares && !$this->utilsService->userCanShareFile($fileId, $userId)) {
455+
return ['error' => $this->l10n->t('You can not share this file')];
456+
}
457+
454458
$rule = $this->ruleService->getRule($ruleId);
455459
if (is_null($rule)) {
456460
return ['error' => $this->l10n->t('Rule does not exist')];

lib/Service/UtilsService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ public function userHasAccessTo(int $fileId, ?string $userId): bool {
134134
return false;
135135
}
136136

137+
/**
138+
* Check if user can share a given file
139+
*
140+
* @param int $fileId
141+
* @param string|null $userId
142+
* @return bool
143+
*/
144+
public function userCanShareFile(int $fileId, ?string $userId): bool {
145+
$user = $this->userManager->get($userId);
146+
if ($user instanceof IUser) {
147+
$userFolder = $this->root->getUserFolder($userId);
148+
$node = $userFolder->getFirstNodeById($fileId);
149+
return $node !== null && $node->isShareable();
150+
}
151+
return false;
152+
}
153+
137154
/**
138155
* @param string $name of the new tag
139156
* @return array

tests/unit/Service/ApprovalServiceTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
use OCP\Share\IManager as IShareManager;
2525

26+
use OCP\Share\IShare;
27+
2628
use OCP\SystemTag\ISystemTagManager;
2729
use OCP\SystemTag\ISystemTagObjectMapper;
2830

@@ -384,4 +386,41 @@ public function testApproval() {
384386
$stateForUser1 = $this->approvalService->getApprovalState($fileToReject->getId(), 'user1');
385387
$this->assertEquals(Application::STATE_REJECTED, $stateForUser1['state']);
386388
}
389+
390+
public function testRequestWithCreateSharesWhenUserCannotShareReturnsError(): void {
391+
// Share a file from user1 to user2 with read-only (no share permission).
392+
// user2 has access but cannot share -> request with createShares true must return error.
393+
$uf1 = $this->root->getUserFolder('user1');
394+
$file = $uf1->newFile('file_no_share.txt', 'content');
395+
$shared = $this->utilsService->createShare(
396+
$file,
397+
IShare::TYPE_USER,
398+
'user2',
399+
'user1',
400+
'label'
401+
);
402+
$this->assertTrue($shared);
403+
404+
// Add some tags
405+
$r = $this->utilsService->createTag('pending4');
406+
$idTagPending4 = $r['id'];
407+
$r = $this->utilsService->createTag('approved4');
408+
$idTagApproved4 = $r['id'];
409+
$r = $this->utilsService->createTag('rejected4');
410+
$idTagRejected4 = $r['id'];
411+
412+
$r = $this->ruleService->createRule(
413+
$idTagPending4, $idTagApproved4, $idTagRejected4,
414+
[['entityId' => 'user3', 'type' => 'user']],
415+
[['entityId' => 'user2', 'type' => 'user']],
416+
'user 2 request, 3 approves',
417+
false
418+
);
419+
$ruleId = $r['id'];
420+
421+
$result = $this->approvalService->request($file->getId(), $ruleId, 'user2', true);
422+
$this->assertArrayHasKey('error', $result);
423+
424+
$this->ruleService->deleteRule($ruleId);
425+
}
387426
}

0 commit comments

Comments
 (0)