Skip to content

Commit d79d7aa

Browse files
committed
Fix more tests
Signed-off-by: Vincent Petry <vincent@nextcloud.com>
1 parent 3a905a4 commit d79d7aa

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public function deleteShare(string $id): DataResponse {
442442
* @param string $sendPasswordByTalk
443443
* @param string $expireDate
444444
* @param string $label
445+
* @param string $attributes
445446
*
446447
* @return DataResponse
447448
* @throws NotFoundException
@@ -462,7 +463,8 @@ public function createShare(
462463
string $sendPasswordByTalk = null,
463464
string $expireDate = '',
464465
string $note = '',
465-
string $label = ''
466+
string $label = '',
467+
string $attributes = null
466468
): DataResponse {
467469
$share = $this->shareManager->newShare();
468470

@@ -680,7 +682,7 @@ public function createShare(
680682
$share->setNote($note);
681683
}
682684

683-
$share = $this->setShareAttributes($share, $this->request->getParam('attributes', null));
685+
$share = $this->setShareAttributes($share, $attributes);
684686

685687
try {
686688
$share = $this->shareManager->createShare($share);
@@ -1043,6 +1045,7 @@ private function hasPermission(int $permissionsSet, int $permissionsToCheck): bo
10431045
* @param string $note
10441046
* @param string $label
10451047
* @param string $hideDownload
1048+
* @param string $attributes
10461049
* @return DataResponse
10471050
* @throws LockedException
10481051
* @throws NotFoundException
@@ -1059,7 +1062,8 @@ public function updateShare(
10591062
string $expireDate = null,
10601063
string $note = null,
10611064
string $label = null,
1062-
string $hideDownload = null
1065+
string $hideDownload = null,
1066+
string $attributes = null
10631067
): DataResponse {
10641068
try {
10651069
$share = $this->getShareById($id);
@@ -1077,8 +1081,6 @@ public function updateShare(
10771081
throw new OCSForbiddenException('You are not allowed to edit incoming shares');
10781082
}
10791083

1080-
$shareAttributes = $this->request->getParam('attributes', null);
1081-
10821084
if (
10831085
$permissions === null &&
10841086
$password === null &&
@@ -1088,7 +1090,7 @@ public function updateShare(
10881090
$note === null &&
10891091
$label === null &&
10901092
$hideDownload === null &&
1091-
$shareAttributes === null
1093+
$attributes === null
10921094
) {
10931095
throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
10941096
}
@@ -1227,7 +1229,7 @@ public function updateShare(
12271229
}
12281230
}
12291231

1230-
$share = $this->setShareAttributes($share, $shareAttributes);
1232+
$share = $this->setShareAttributes($share, $attributes);
12311233

12321234
try {
12331235
$share = $this->shareManager->updateShare($share);
@@ -1848,18 +1850,23 @@ private function mergeFormattedShares(array &$shares, array $newShares) {
18481850

18491851
/**
18501852
* @param IShare $share
1851-
* @param string[][]|null $formattedShareAttributes
1853+
* @param string|null $attributesString
18521854
* @return IShare modified share
18531855
*/
1854-
private function setShareAttributes(IShare $share, $formattedShareAttributes) {
1856+
private function setShareAttributes(IShare $share, ?string $attributesString) {
18551857
$newShareAttributes = $this->shareManager->newShare()->newAttributes();
1856-
if ($formattedShareAttributes !== null) {
1857-
foreach ($formattedShareAttributes as $formattedAttr) {
1858-
$newShareAttributes->setAttribute(
1859-
$formattedAttr['scope'],
1860-
$formattedAttr['key'],
1861-
(bool) \json_decode($formattedAttr['enabled'])
1862-
);
1858+
if ($attributesString !== null) {
1859+
$formattedShareAttributes = \json_decode($attributesString, true);
1860+
if (is_array($formattedShareAttributes)) {
1861+
foreach ($formattedShareAttributes as $formattedAttr) {
1862+
$newShareAttributes->setAttribute(
1863+
$formattedAttr['scope'],
1864+
$formattedAttr['key'],
1865+
is_string($formattedAttr['enabled']) ? (bool) \json_decode($formattedAttr['enabled']) : $formattedAttr['enabled']
1866+
);
1867+
}
1868+
} else {
1869+
throw new OCSBadRequestException('Invalid share attributes provided: \"' . $attributesString . '\"');
18631870
}
18641871
}
18651872
$share->setAttributes($newShareAttributes);

apps/files_sharing/tests/ApiTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,13 @@ public function testUpdateShare() {
950950
->setShareType(IShare::TYPE_USER)
951951
->setPermissions(19)
952952
->setAttributes($this->shareManager->newShare()->newAttributes());
953+
954+
$this->assertNotNull($share1->getAttributes());
953955
$share1 = $this->shareManager->createShare($share1);
956+
$this->assertNull($share1->getAttributes());
954957
$this->assertEquals(19, $share1->getPermissions());
955-
$this->assertEquals(null, $share1->getAttributes());
958+
// attributes get cleared when empty
959+
$this->assertNull($share1->getAttributes());
956960

957961
$share2 = $this->shareManager->newShare();
958962
$share2->setNode($node1)
@@ -964,7 +968,10 @@ public function testUpdateShare() {
964968

965969
// update permissions
966970
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
967-
$ocs->updateShare($share1->getId(), 1);
971+
$ocs->updateShare(
972+
$share1->getId(), 1, null, null, null, null, null, null, null,
973+
'[{"scope": "app1", "key": "attr1", "enabled": true}]'
974+
);
968975
$ocs->cleanup();
969976

970977
$share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());

apps/files_sharing/tests/MountProviderTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OCP\ILogger;
4040
use OCP\IUser;
4141
use OCP\IUserManager;
42+
use OCP\Share\IAttributes as IShareAttributes;
4243
use OCP\Share\IManager;
4344
use OCP\Share\IShare;
4445

@@ -102,7 +103,7 @@ private function makeMockShareAttributes($attrs) {
102103
return $shareAttributes;
103104
}
104105

105-
private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31, $attributes) {
106+
private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31, $attributes = null) {
106107
$share = $this->createMock(IShare::class);
107108
$share->expects($this->any())
108109
->method('getPermissions')
@@ -376,10 +377,10 @@ public function testMergeShares($userShares, $groupShares, $expectedShares, $mov
376377
$userManager = $this->createMock(IUserManager::class);
377378

378379
$userShares = array_map(function ($shareSpec) {
379-
return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]);
380+
return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
380381
}, $userShares);
381382
$groupShares = array_map(function ($shareSpec) {
382-
return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]);
383+
return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]);
383384
}, $groupShares);
384385

385386
$this->user->expects($this->any())

lib/private/Share20/Manager.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,16 +2089,4 @@ public function getAllShares(): iterable {
20892089
yield from $provider->getAllShares();
20902090
}
20912091
}
2092-
2093-
/**
2094-
* @param IAttributes|null $perms
2095-
* @return string
2096-
*/
2097-
private function hashAttributes($perms) {
2098-
if ($perms === null || empty($perms->toArray())) {
2099-
return "";
2100-
}
2101-
2102-
return \md5(\json_encode($perms->toArray()));
2103-
}
21042092
}

lib/private/Share20/Share.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ public function newAttributes() {
345345
/**
346346
* @inheritdoc
347347
*/
348-
public function setAttributes(IAttributes $attributes) {
348+
public function setAttributes(?IAttributes $attributes) {
349349
$this->attributes = $attributes;
350350
return $this;
351351
}
352352

353353
/**
354354
* @inheritdoc
355355
*/
356-
public function getAttributes() {
356+
public function getAttributes(): ?IAttributes {
357357
return $this->attributes;
358358
}
359359

lib/public/Share/IShare.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function setAttributes(?IAttributes $attributes);
337337
* @since 25.0.0
338338
* @return ?IAttributes
339339
*/
340-
public function getAttributes();
340+
public function getAttributes(): ?IAttributes;
341341

342342
/**
343343
* Set the accepted status

0 commit comments

Comments
 (0)