diff --git a/apps/dav/lib/Command/RemoveInvalidShares.php b/apps/dav/lib/Command/RemoveInvalidShares.php index 340e878a91288..6337a938fa943 100644 --- a/apps/dav/lib/Command/RemoveInvalidShares.php +++ b/apps/dav/lib/Command/RemoveInvalidShares.php @@ -11,6 +11,7 @@ namespace OCA\DAV\Command; use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\DAV\RemoteUserPrincipalBackend; use OCP\IDBConnection; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -24,6 +25,7 @@ class RemoveInvalidShares extends Command { public function __construct( private IDBConnection $connection, private Principal $principalBackend, + private RemoteUserPrincipalBackend $remoteUserPrincipalBackend, ) { parent::__construct(); } @@ -42,7 +44,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int while ($row = $result->fetch()) { $principaluri = $row['principaluri']; - $p = $this->principalBackend->getPrincipalByPath($principaluri); + $p = $this->principalBackend->getPrincipalByPath($principaluri) + ?? $this->remoteUserPrincipalBackend->getPrincipalByPath($principaluri); if ($p === null) { $this->deleteSharesForPrincipal($principaluri); } diff --git a/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php b/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php index ec56aa64eb221..7a8518ac70c6f 100644 --- a/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php +++ b/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php @@ -10,8 +10,11 @@ use OCA\DAV\Command\RemoveInvalidShares; use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\DAV\RemoteUserPrincipalBackend; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -23,32 +26,118 @@ * @group DB */ class RemoveInvalidSharesTest extends TestCase { + private RemoveInvalidShares $command; + + private IDBConnection $db; + private Principal&MockObject $principalBackend; + private RemoteUserPrincipalBackend&MockObject $remoteUserPrincipalBackend; + protected function setUp(): void { parent::setUp(); - $db = Server::get(IDBConnection::class); - $db->insertIfNotExist('*PREFIX*dav_shares', [ + $this->db = Server::get(IDBConnection::class); + $this->principalBackend = $this->createMock(Principal::class); + $this->remoteUserPrincipalBackend = $this->createMock(RemoteUserPrincipalBackend::class); + + $this->db->insertIfNotExist('*PREFIX*dav_shares', [ 'principaluri' => 'principal:unknown', 'type' => 'calendar', 'access' => 2, 'resourceid' => 666, ]); - } - - public function test(): void { - $db = Server::get(IDBConnection::class); - $principal = $this->createMock(Principal::class); + $this->db->insertIfNotExist('*PREFIX*dav_shares', [ + 'principaluri' => 'principals/remote-users/foobar', + 'type' => 'calendar', + 'access' => 2, + 'resourceid' => 666, + ]); - $repair = new RemoveInvalidShares($db, $principal); - $this->invokePrivate($repair, 'run', [$this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)]); + $this->command = new RemoveInvalidShares( + $this->db, + $this->principalBackend, + $this->remoteUserPrincipalBackend, + ); + } - $query = $db->getQueryBuilder(); + private function selectShares(): array { + $query = $this->db->getQueryBuilder(); $query->select('*') ->from('dav_shares') - ->where($query->expr()->eq('principaluri', $query->createNamedParameter('principal:unknown'))); + ->where($query->expr()->in( + 'principaluri', + $query->createNamedParameter( + ['principal:unknown', 'principals/remote-users/foobar'], + IQueryBuilder::PARAM_STR_ARRAY, + ), + )); $result = $query->executeQuery(); $data = $result->fetchAll(); $result->closeCursor(); - $this->assertEquals(0, count($data)); + + return $data; + } + + public function testWithoutPrincipals(): void { + $this->principalBackend->method('getPrincipalByPath') + ->willReturnMap([ + ['principal:unknown', null], + ['principals/remote-users/foobar', null], + ]); + $this->remoteUserPrincipalBackend->method('getPrincipalByPath') + ->willReturnMap([ + ['principal:unknown', null], + ['principals/remote-users/foobar', null], + ]); + + $this->command->run( + $this->createMock(InputInterface::class), + $this->createMock(OutputInterface::class), + ); + + $data = $this->selectShares(); + $this->assertCount(0, $data); + } + + public function testWithLocalPrincipal(): void { + $this->principalBackend->method('getPrincipalByPath') + ->willReturnMap([ + ['principal:unknown', ['uri' => 'principal:unknown']], + ['principals/remote-users/foobar', null], + ]); + $this->remoteUserPrincipalBackend->method('getPrincipalByPath') + ->willReturnMap([ + ['principals/remote-users/foobar', null], + ]); + + $this->command->run( + $this->createMock(InputInterface::class), + $this->createMock(OutputInterface::class), + ); + + $data = $this->selectShares(); + $this->assertCount(1, $data); + $this->assertEquals('principal:unknown', $data[0]['principaluri']); + } + + public function testWithRemotePrincipal() { + $this->principalBackend->method('getPrincipalByPath') + ->willReturnMap([ + ['principal:unknown', null], + ['principals/remote-users/foobar', null], + ]); + $this->remoteUserPrincipalBackend->method('getPrincipalByPath') + ->willReturnMap([ + ['principal:unknown', null], + ['principals/remote-users/foobar', ['uri' => 'principals/remote-users/foobar']], + ]); + + $this->command->run( + $this->createMock(InputInterface::class), + $this->createMock(OutputInterface::class), + ); + + $data = $this->selectShares(); + $this->assertCount(1, $data); + $this->assertEquals('principals/remote-users/foobar', $data[0]['principaluri']); } }