Skip to content

Commit

Permalink
fix(share): Return empty string if no label is set
Browse files Browse the repository at this point in the history
* Resolves: #48629

While the database supports NULL, the typing has always said it only returns *string*.
So to not break any apps that might trust the typings we should return `''` if the database is set to `NULL`.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux authored and backportbot[bot] committed Oct 14, 2024
1 parent 4c06e32 commit dfe8f64
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ public function getShareByToken($token) {
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
))
->execute();
->executeQuery();

$data = $cursor->fetch();

Expand Down Expand Up @@ -1021,7 +1021,7 @@ private function createShare($data) {
->setNote((string)$data['note'])
->setMailSend((bool)$data['mail_send'])
->setStatus((int)$data['accepted'])
->setLabel($data['label']);
->setLabel($data['label'] ?? '');

$shareTime = new \DateTime();
$shareTime->setTimestamp((int)$data['stime']);
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ public function testGetShareByToken() {
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'token' => $qb->expr()->literal('secrettoken'),
'label' => $qb->expr()->literal('the label'),
]);
$qb->execute();
$id = $qb->getLastInsertId();
Expand All @@ -899,10 +900,43 @@ public function testGetShareByToken() {
$this->assertSame('sharedBy', $share->getSharedBy());
$this->assertSame('secrettoken', $share->getToken());
$this->assertSame('password', $share->getPassword());
$this->assertSame('the label', $share->getLabel());
$this->assertSame(true, $share->getSendPasswordByTalk());
$this->assertSame(null, $share->getSharedWith());
}

/**
* Assert that if no label is provided the label is correctly,
* as types on IShare, a string and not null
*/
public function testGetShareByTokenNullLabel(): void {
$qb = $this->dbConn->getQueryBuilder();

$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(IShare::TYPE_LINK),
'password' => $qb->expr()->literal('password'),
'password_by_talk' => $qb->expr()->literal(true),
'uid_owner' => $qb->expr()->literal('shareOwner'),
'uid_initiator' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'token' => $qb->expr()->literal('secrettoken'),
]);
$qb->executeStatement();
$id = $qb->getLastInsertId();

$file = $this->createMock(File::class);

$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
$this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file);

$share = $this->provider->getShareByToken('secrettoken');
$this->assertEquals($id, $share->getId());
$this->assertSame('', $share->getLabel());
}

public function testGetShareByTokenNotFound() {
$this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
Expand Down

0 comments on commit dfe8f64

Please sign in to comment.