|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Files_Sharing\Tests\Command; |
| 11 | + |
| 12 | +use OCA\Files_Sharing\Command\FixOwncloudGroupSubshareStatus; |
| 13 | +use OCA\Files_Sharing\Tests\TestCase; |
| 14 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 15 | +use OCP\IDBConnection; |
| 16 | +use OCP\Server; |
| 17 | +use OCP\Share\IShare; |
| 18 | +use Symfony\Component\Console\Tester\CommandTester; |
| 19 | + |
| 20 | +#[\PHPUnit\Framework\Attributes\Group(name: 'DB')] |
| 21 | +class FixOwncloudGroupSubshareStatusTest extends TestCase { |
| 22 | + |
| 23 | + private IDBConnection $connection; |
| 24 | + private CommandTester $commandTester; |
| 25 | + |
| 26 | + protected function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + $this->connection = Server::get(IDBConnection::class); |
| 29 | + $this->commandTester = new CommandTester(new FixOwncloudGroupSubshareStatus($this->connection)); |
| 30 | + $this->cleanDB(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function tearDown(): void { |
| 34 | + $this->cleanDB(); |
| 35 | + parent::tearDown(); |
| 36 | + } |
| 37 | + |
| 38 | + private function cleanDB(): void { |
| 39 | + $this->connection->getQueryBuilder()->delete('share')->executeStatement(); |
| 40 | + } |
| 41 | + |
| 42 | + private function insertShare(int $shareType, int $accepted, int $permissions): int { |
| 43 | + $qb = $this->connection->getQueryBuilder(); |
| 44 | + $qb->insert('share') |
| 45 | + ->values([ |
| 46 | + 'share_type' => $qb->createNamedParameter($shareType, IQueryBuilder::PARAM_INT), |
| 47 | + 'share_with' => $qb->createNamedParameter('user1'), |
| 48 | + 'uid_owner' => $qb->createNamedParameter('owner'), |
| 49 | + 'uid_initiator' => $qb->createNamedParameter('owner'), |
| 50 | + 'parent' => $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
| 51 | + 'item_type' => $qb->createNamedParameter('file'), |
| 52 | + 'item_source' => $qb->createNamedParameter('42'), |
| 53 | + 'item_target' => $qb->createNamedParameter('/file'), |
| 54 | + 'file_source' => $qb->createNamedParameter(42, IQueryBuilder::PARAM_INT), |
| 55 | + 'file_target' => $qb->createNamedParameter('/file'), |
| 56 | + 'permissions' => $qb->createNamedParameter($permissions, IQueryBuilder::PARAM_INT), |
| 57 | + 'stime' => $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT), |
| 58 | + 'accepted' => $qb->createNamedParameter($accepted, IQueryBuilder::PARAM_INT), |
| 59 | + ]) |
| 60 | + ->executeStatement(); |
| 61 | + return (int)$this->connection->lastInsertId('*PREFIX*share'); |
| 62 | + } |
| 63 | + |
| 64 | + private function getAccepted(int $id): int { |
| 65 | + $qb = $this->connection->getQueryBuilder(); |
| 66 | + return (int)$qb->select('accepted') |
| 67 | + ->from('share') |
| 68 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))) |
| 69 | + ->executeQuery() |
| 70 | + ->fetchOne(); |
| 71 | + } |
| 72 | + |
| 73 | + public function testFixesPendingSubshareWithPermissions(): void { |
| 74 | + $id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 31); |
| 75 | + |
| 76 | + $this->commandTester->execute([]); |
| 77 | + |
| 78 | + $this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id)); |
| 79 | + $this->assertStringContainsString('Fixed', $this->commandTester->getDisplay()); |
| 80 | + } |
| 81 | + |
| 82 | + public function testDryRunShowsCountWithoutChanging(): void { |
| 83 | + $id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 31); |
| 84 | + |
| 85 | + $this->commandTester->execute(['--dry-run' => true]); |
| 86 | + |
| 87 | + $this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($id)); |
| 88 | + $this->assertStringContainsString('dry-run', $this->commandTester->getDisplay()); |
| 89 | + } |
| 90 | + |
| 91 | + public function testDoesNotTouchDeclinedSubshare(): void { |
| 92 | + // permissions = 0 means the user explicitly declined the share |
| 93 | + $id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 0); |
| 94 | + |
| 95 | + $this->commandTester->execute([]); |
| 96 | + |
| 97 | + $this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($id)); |
| 98 | + $this->assertStringContainsString('No affected', $this->commandTester->getDisplay()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testDoesNotTouchAlreadyAcceptedSubshare(): void { |
| 102 | + $id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_ACCEPTED, 31); |
| 103 | + |
| 104 | + $this->commandTester->execute([]); |
| 105 | + |
| 106 | + $this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id)); |
| 107 | + $this->assertStringContainsString('No affected', $this->commandTester->getDisplay()); |
| 108 | + } |
| 109 | + |
| 110 | + public function testDoesNotTouchNonUsergroupShares(): void { |
| 111 | + $id = $this->insertShare(IShare::TYPE_GROUP, IShare::STATUS_PENDING, 31); |
| 112 | + |
| 113 | + $this->commandTester->execute([]); |
| 114 | + |
| 115 | + $this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($id)); |
| 116 | + $this->assertStringContainsString('No affected', $this->commandTester->getDisplay()); |
| 117 | + } |
| 118 | + |
| 119 | + public function testFixesMultipleAffectedRows(): void { |
| 120 | + $id1 = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 31); |
| 121 | + $id2 = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 17); |
| 122 | + $idDeclined = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 0); |
| 123 | + |
| 124 | + $this->commandTester->execute([]); |
| 125 | + |
| 126 | + $this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id1)); |
| 127 | + $this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id2)); |
| 128 | + $this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($idDeclined)); |
| 129 | + $this->assertStringContainsString('2', $this->commandTester->getDisplay()); |
| 130 | + } |
| 131 | +} |
0 commit comments