Skip to content

Commit 5d0acf8

Browse files
committed
test: add test for share target repair
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent aedae2c commit 5d0acf8

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2026
5+
* SPDX-License-Identifier: AGPL-3.0-only
6+
*/
7+
namespace OCA\Files_Sharing\Tests\Repair;
8+
9+
use OC\Migration\NullOutput;
10+
use OCA\Files_Sharing\Repair\CleanupShareTarget;
11+
use OCA\Files_Sharing\Tests\TestCase;
12+
use OCP\Files\NotFoundException;
13+
use OCP\Server;
14+
use OCP\Share\IShare;
15+
use PHPUnit\Framework\Attributes\Group;
16+
17+
/**
18+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
19+
* SPDX-License-Identifier: AGPL-3.0-or-later
20+
*/
21+
#[Group(name: 'DB')]
22+
class CleanupShareTargetTest extends TestCase {
23+
public const TEST_FOLDER_NAME = '/folder_share_api_test';
24+
25+
private CleanupShareTarget $cleanupShareTarget;
26+
27+
protected function setUp(): void {
28+
parent::setUp();
29+
$this->cleanupShareTarget = Server::get(CleanupShareTarget::class);
30+
}
31+
32+
private function createUserShare(string $by, string $target = self::TEST_FOLDER_NAME): IShare {
33+
$userFolder = $this->rootFolder->getUserFolder($by);
34+
35+
try {
36+
$node = $userFolder->get(self::TEST_FOLDER_NAME);
37+
} catch (NotFoundException $e) {
38+
$node = $userFolder->newFolder(self::TEST_FOLDER_NAME);
39+
}
40+
$share1 = $this->shareManager->newShare();
41+
$share1->setNode($node)
42+
->setSharedBy($by)
43+
->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
44+
->setShareType(IShare::TYPE_USER)
45+
->setPermissions(31);
46+
$share = $this->shareManager->createShare($share1);
47+
$share->setStatus(IShare::STATUS_ACCEPTED);
48+
$this->shareManager->updateShare($share);
49+
50+
$share->setTarget($target);
51+
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
52+
53+
$share = $this->shareManager->getShareById($share->getFullId());
54+
$this->assertEquals($target, $share->getTarget());
55+
56+
return $share;
57+
}
58+
59+
public function testBasicRepair() {
60+
$share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)');
61+
62+
$this->cleanupShareTarget->run(new NullOutput());
63+
64+
$share = $this->shareManager->getShareById($share->getFullId());
65+
$this->assertEquals(self::TEST_FOLDER_NAME, $share->getTarget());
66+
}
67+
68+
public function testRepairConflictFile() {
69+
$share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)');
70+
71+
$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
72+
$userFolder2 = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
73+
$folder = $userFolder2->newFolder(self::TEST_FOLDER_NAME);
74+
75+
$this->cleanupShareTarget->run(new NullOutput());
76+
$folder->delete();
77+
78+
$share = $this->shareManager->getShareById($share->getFullId());
79+
$this->assertEquals(self::TEST_FOLDER_NAME . ' (2)', $share->getTarget());
80+
}
81+
82+
public function testRepairConflictShare() {
83+
$share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)');
84+
85+
$share2 = $this->createUserShare(self::TEST_FILES_SHARING_API_USER3);
86+
87+
$this->cleanupShareTarget->run(new NullOutput());
88+
89+
$share2 = $this->shareManager->getShareById($share2->getFullId());
90+
$this->assertEquals(self::TEST_FOLDER_NAME, $share2->getTarget());
91+
$share = $this->shareManager->getShareById($share->getFullId());
92+
$this->assertEquals(self::TEST_FOLDER_NAME . ' (2)', $share->getTarget());
93+
}
94+
95+
public function testRepairMultipleConflicting() {
96+
$share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)');
97+
$share2 = $this->createUserShare(self::TEST_FILES_SHARING_API_USER3, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2) (2)');
98+
99+
$this->cleanupShareTarget->run(new NullOutput());
100+
101+
$share = $this->shareManager->getShareById($share->getFullId());
102+
$share2 = $this->shareManager->getShareById($share2->getFullId());
103+
104+
// there is no guarantee for what order the 2 shares got repaired by
105+
$targets = [
106+
$share->getTarget(),
107+
$share2->getTarget(),
108+
];
109+
sort($targets);
110+
$this->assertEquals([
111+
self::TEST_FOLDER_NAME,
112+
self::TEST_FOLDER_NAME . ' (2)'
113+
], $targets);
114+
}
115+
}

0 commit comments

Comments
 (0)