Skip to content

Commit 4900913

Browse files
Merge pull request #62354 from nextcloud/fix/sharingbackend/modify-value-on-save-for-default-value
fix(SharingBackend): Call ISharePropertyTypeModifyValue::modifyValueOnSave when saving default value
2 parents 4354bc5 + ff75969 commit 4900913

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

apps/sharing/lib/SharingBackend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ private function list(ShareAccessContext $accessContext, ?string $filterShareID,
831831
->values([
832832
'share_id' => $qb->createNamedParameter($id),
833833
'property_class' => $qb->createNamedParameter($propertyTypeClass),
834-
'property_value' => $qb->createNamedParameter($value),
834+
'property_value' => $qb->createNamedParameter($propertyType instanceof ISharePropertyTypeModifyValue ? $propertyType->modifyValueOnSave(null, $value) : $value),
835835
])
836836
->executeStatement();
837837

lib/public/Sharing/Property/ISharePropertyType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public function isRequired(): bool;
7373
*
7474
* A default value must be returned, if {@see self::isRequired()} returns true.
7575
*
76+
* If the class also implements {@see ISharePropertyTypeModifyValue}, {@see ISharePropertyTypeModifyValue::modifyValueOnSave()} will be called when the value is saved to the database, but the value will be returned to the user as-is.
77+
*
7678
* @since 35.0.0
7779
*/
7880
public function getDefaultValue(): ?string;

tests/lib/Sharing/AbstractSharingManagerTests.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,11 +1370,65 @@ public function testUpdateSharePropertyModifyProperties(): void {
13701370
$id = $this->manager->createShare($accessContext);
13711371
$this->manager->addShareSource($accessContext, $id, new ShareSource(TestShareSourceType1::class, 'source1'));
13721372
$this->manager->addShareRecipient($accessContext, $id, new ShareRecipient(TestShareRecipientType1::class, 'recipient1', null));
1373-
$this->manager->getShare($accessContext, $id);
1374-
$this->manager->updateShareProperty($accessContext, $id, new ShareProperty(TestSharePropertyTypeModifyValue::class, 'old-value'));
13751373

13761374
$this->dbConnection->commit();
13771375

1376+
$share = $this->getShare($accessContext, $id);
1377+
$this->assertEquals([
1378+
[
1379+
'class' => TestSharePropertyType1::class,
1380+
'display_name' => 'TestSharePropertyType1',
1381+
'hint' => 'hint TestSharePropertyType1',
1382+
'priority' => 1,
1383+
'advanced' => false,
1384+
'required' => false,
1385+
'value' => null,
1386+
'type' => 'enum',
1387+
'valid_values' => ['valid1'],
1388+
],
1389+
[
1390+
'class' => TestSharePropertyTypeModifyValue::class,
1391+
'display_name' => 'TestSharePropertyTypeModifyValue',
1392+
'hint' => 'hint TestSharePropertyTypeModifyValue',
1393+
'priority' => 1,
1394+
'advanced' => false,
1395+
'required' => false,
1396+
'value' => 'modify-on-save',
1397+
'type' => 'enum',
1398+
'valid_values' => ['old-value', 'modify-on-save-old-value', 'modify-on-save', 'modify-on-load'],
1399+
],
1400+
], $share['properties']);
1401+
1402+
$share = $this->getShare($accessContext, $id);
1403+
$this->assertEquals([
1404+
[
1405+
'class' => TestSharePropertyType1::class,
1406+
'display_name' => 'TestSharePropertyType1',
1407+
'hint' => 'hint TestSharePropertyType1',
1408+
'priority' => 1,
1409+
'advanced' => false,
1410+
'required' => false,
1411+
'value' => null,
1412+
'type' => 'enum',
1413+
'valid_values' => ['valid1'],
1414+
],
1415+
[
1416+
'class' => TestSharePropertyTypeModifyValue::class,
1417+
'display_name' => 'TestSharePropertyTypeModifyValue',
1418+
'hint' => 'hint TestSharePropertyTypeModifyValue',
1419+
'priority' => 1,
1420+
'advanced' => false,
1421+
'required' => false,
1422+
'value' => 'modified-on-save',
1423+
'type' => 'enum',
1424+
'valid_values' => ['old-value', 'modify-on-save-old-value', 'modify-on-save', 'modify-on-load'],
1425+
],
1426+
], $share['properties']);
1427+
1428+
$this->dbConnection->beginTransaction();
1429+
$this->manager->updateShareProperty($accessContext, $id, new ShareProperty(TestSharePropertyTypeModifyValue::class, 'old-value'));
1430+
$this->dbConnection->commit();
1431+
13781432
$before = $this->manager->generateTimestamp();
13791433
$share = $this->updateShareProperty($accessContext, $id, new ShareProperty(TestSharePropertyTypeModifyValue::class, 'modify-on-save-old-value'));
13801434
$after = $this->manager->generateTimestamp();

tests/lib/Sharing/TestSharePropertyTypeModifyValue.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
use OCP\Sharing\Property\ISharePropertyTypeModifyValue;
1313

1414
final class TestSharePropertyTypeModifyValue extends TestSharePropertyType1 implements ISharePropertyTypeModifyValue {
15+
16+
#[\Override]
17+
public function getDefaultValue(): string {
18+
return 'modify-on-save';
19+
}
20+
1521
#[\Override]
1622
public function modifyValueOnSave(?string $oldValue, ?string $newValue): ?string {
1723
if ($newValue === 'modify-on-save') {

0 commit comments

Comments
 (0)