|
| 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 | + $userFolder2 = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER2); |
| 72 | + $folder = $userFolder2->newFolder(self::TEST_FOLDER_NAME); |
| 73 | + |
| 74 | + $this->cleanupShareTarget->run(new NullOutput()); |
| 75 | + $folder->delete(); |
| 76 | + |
| 77 | + $share = $this->shareManager->getShareById($share->getFullId()); |
| 78 | + $this->assertEquals(self::TEST_FOLDER_NAME . ' (2)', $share->getTarget()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testRepairConflictShare() { |
| 82 | + $share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)'); |
| 83 | + |
| 84 | + $share2 = $this->createUserShare(self::TEST_FILES_SHARING_API_USER3); |
| 85 | + |
| 86 | + $this->cleanupShareTarget->run(new NullOutput()); |
| 87 | + |
| 88 | + $share2 = $this->shareManager->getShareById($share2->getFullId()); |
| 89 | + $this->assertEquals(self::TEST_FOLDER_NAME, $share2->getTarget()); |
| 90 | + $share = $this->shareManager->getShareById($share->getFullId()); |
| 91 | + $this->assertEquals(self::TEST_FOLDER_NAME . ' (2)', $share->getTarget()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testRepairMultipleConflicting() { |
| 95 | + $share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)'); |
| 96 | + $share2 = $this->createUserShare(self::TEST_FILES_SHARING_API_USER3, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2) (2)'); |
| 97 | + |
| 98 | + $this->cleanupShareTarget->run(new NullOutput()); |
| 99 | + |
| 100 | + $share = $this->shareManager->getShareById($share->getFullId()); |
| 101 | + $share2 = $this->shareManager->getShareById($share2->getFullId()); |
| 102 | + |
| 103 | + // there is no guarantee for what order the 2 shares got repaired by |
| 104 | + $targets = [ |
| 105 | + $share->getTarget(), |
| 106 | + $share2->getTarget(), |
| 107 | + ]; |
| 108 | + sort($targets); |
| 109 | + $this->assertEquals([ |
| 110 | + self::TEST_FOLDER_NAME, |
| 111 | + self::TEST_FOLDER_NAME . ' (2)' |
| 112 | + ], $targets); |
| 113 | + } |
| 114 | +} |
0 commit comments