diff options
Diffstat (limited to 'apps/files_sharing/tests/Command')
-rw-r--r-- | apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php | 150 | ||||
-rw-r--r-- | apps/files_sharing/tests/Command/FixShareOwnersTest.php | 117 |
2 files changed, 181 insertions, 86 deletions
diff --git a/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php b/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php index 55f719b8f88..6f0960bf46c 100644 --- a/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php +++ b/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php @@ -1,33 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud GmbH. - * - * @author Daniel Calviño Sánchez <danxuliu@gmail.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud GmbH. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Sharing\Tests\Command; use OCA\Files_Sharing\Command\CleanupRemoteStorages; use OCP\Federation\ICloudId; use OCP\Federation\ICloudIdManager; +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; @@ -41,20 +26,9 @@ use Test\TestCase; */ class CleanupRemoteStoragesTest extends TestCase { - /** - * @var CleanupRemoteStorages - */ - private $command; - - /** - * @var \OCP\IDBConnection - */ - private $connection; - - /** - * @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject - */ - private $cloudIdManager; + protected IDBConnection $connection; + protected CleanupRemoteStorages $command; + private ICloudIdManager&MockObject $cloudIdManager; private $storages = [ ['id' => 'shared::7b4a322b22f9d0047c38d77d471ce3cf', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e1', 'remote' => 'https://hostname.tld/owncloud1', 'user' => 'user1'], @@ -69,49 +43,53 @@ class CleanupRemoteStoragesTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); - $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $storageQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $storageQuery->insert('storages') - ->setValue('id', '?'); + ->setValue('id', $storageQuery->createParameter('id')); - $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $shareExternalQuery->insert('share_external') - ->setValue('share_token', '?') - ->setValue('remote', '?') - ->setValue('name', '?')->setParameter(2, 'irrelevant') - ->setValue('owner', '?')->setParameter(3, 'irrelevant') - ->setValue('user', '?') - ->setValue('mountpoint', '?')->setParameter(5, 'irrelevant') - ->setValue('mountpoint_hash', '?')->setParameter(6, 'irrelevant'); - - $filesQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + ->setValue('share_token', $shareExternalQuery->createParameter('share_token')) + ->setValue('remote', $shareExternalQuery->createParameter('remote')) + ->setValue('name', $shareExternalQuery->createParameter('name')) + ->setValue('owner', $shareExternalQuery->createParameter('owner')) + ->setValue('user', $shareExternalQuery->createParameter('user')) + ->setValue('mountpoint', $shareExternalQuery->createParameter('mountpoint')) + ->setValue('mountpoint_hash', $shareExternalQuery->createParameter('mountpoint_hash')); + + $filesQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $filesQuery->insert('filecache') - ->setValue('storage', '?') - ->setValue('path', '?') - ->setValue('path_hash', '?'); + ->setValue('storage', $filesQuery->createParameter('storage')) + ->setValue('path', $filesQuery->createParameter('path')) + ->setValue('path_hash', $filesQuery->createParameter('path_hash')); foreach ($this->storages as &$storage) { if (isset($storage['id'])) { - $storageQuery->setParameter(0, $storage['id']); - $storageQuery->execute(); - $storage['numeric_id'] = $this->connection->lastInsertId('*PREFIX*storages'); + $storageQuery->setParameter('id', $storage['id']); + $storageQuery->executeStatement(); + $storage['numeric_id'] = $storageQuery->getLastInsertId(); } if (isset($storage['share_token'])) { $shareExternalQuery - ->setParameter(0, $storage['share_token']) - ->setParameter(1, $storage['remote']) - ->setParameter(4, $storage['user']); - $shareExternalQuery->execute(); + ->setParameter('share_token', $storage['share_token']) + ->setParameter('remote', $storage['remote']) + ->setParameter('name', 'irrelevant') + ->setParameter('owner', 'irrelevant') + ->setParameter('user', $storage['user']) + ->setParameter('mountpoint', 'irrelevant') + ->setParameter('mountpoint_hash', 'irrelevant'); + $shareExternalQuery->executeStatement(); } if (isset($storage['files_count'])) { for ($i = 0; $i < $storage['files_count']; $i++) { - $filesQuery->setParameter(0, $storage['numeric_id']); - $filesQuery->setParameter(1, 'file' . $i); - $filesQuery->setParameter(2, md5('file' . $i)); - $filesQuery->execute(); + $filesQuery->setParameter('storage', $storage['numeric_id']); + $filesQuery->setParameter('path', 'file' . $i); + $filesQuery->setParameter('path_hash', md5('file' . $i)); + $filesQuery->executeStatement(); } } } @@ -122,11 +100,11 @@ class CleanupRemoteStoragesTest extends TestCase { } protected function tearDown(): void { - $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $storageQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $storageQuery->delete('storages') ->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id'))); - $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $shareExternalQuery->delete('share_external') ->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token'))) ->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote'))); @@ -134,13 +112,13 @@ class CleanupRemoteStoragesTest extends TestCase { foreach ($this->storages as $storage) { if (isset($storage['id'])) { $storageQuery->setParameter('id', $storage['id']); - $storageQuery->execute(); + $storageQuery->executeStatement(); } if (isset($storage['share_token'])) { $shareExternalQuery->setParameter('share_token', $storage['share_token']); $shareExternalQuery->setParameter('remote', $storage['remote']); - $shareExternalQuery->execute(); + $shareExternalQuery->executeStatement(); } } @@ -148,25 +126,25 @@ class CleanupRemoteStoragesTest extends TestCase { } private function doesStorageExist($numericId) { - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); $qb->select('*') ->from('storages') ->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId))); - $qResult = $qb->execute(); - $result = $qResult->fetchAll(); + $qResult = $qb->executeQuery(); + $result = $qResult->fetch(); $qResult->closeCursor(); if (!empty($result)) { return true; } - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); $qb->select('*') ->from('filecache') ->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId))); - $qResult = $qb->execute(); - $result = $qResult->fetchAll(); + $qResult = $qb->executeQuery(); + $result = $qResult->fetch(); $qResult->closeCursor(); if (!empty($result)) { return true; @@ -178,7 +156,7 @@ class CleanupRemoteStoragesTest extends TestCase { /** * Test cleanup of orphaned storages */ - public function testCleanup() { + public function testCleanup(): void { $input = $this->getMockBuilder(InputInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -186,24 +164,19 @@ class CleanupRemoteStoragesTest extends TestCase { ->disableOriginalConstructor() ->getMock(); - // - // parent folder, `files`, ´test` and `welcome.txt` => 4 elements - - $at = 0; - $output - ->expects($this->at($at++)) - ->method('writeln') - ->with('5 remote storage(s) need(s) to be checked'); + $outputCalls = []; $output - ->expects($this->at($at++)) + ->expects($this->any()) ->method('writeln') - ->with('5 remote share(s) exist'); + ->willReturnCallback(function (string $text) use (&$outputCalls): void { + $outputCalls[] = $text; + }); $this->cloudIdManager ->expects($this->any()) ->method('getCloudId') - ->will($this->returnCallback(function (string $user, string $remote) { + ->willReturnCallback(function (string $user, string $remote) { $cloudIdMock = $this->createMock(ICloudId::class); // The remotes are already sanitized in the original data, so @@ -214,7 +187,7 @@ class CleanupRemoteStoragesTest extends TestCase { ->willReturn($remote); return $cloudIdMock; - })); + }); $this->command->execute($input, $output); @@ -223,5 +196,10 @@ class CleanupRemoteStoragesTest extends TestCase { $this->assertFalse($this->doesStorageExist($this->storages[3]['numeric_id'])); $this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id'])); $this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id'])); + + $this->assertEquals([ + '5 remote storage(s) need(s) to be checked', + '5 remote share(s) exist', + ], array_slice($outputCalls, 0, 2)); } } diff --git a/apps/files_sharing/tests/Command/FixShareOwnersTest.php b/apps/files_sharing/tests/Command/FixShareOwnersTest.php new file mode 100644 index 00000000000..0fde61895b1 --- /dev/null +++ b/apps/files_sharing/tests/Command/FixShareOwnersTest.php @@ -0,0 +1,117 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OCA\Files_Sharing\Tests\Command; + +use OCA\Files_Sharing\Command\FixShareOwners; +use OCA\Files_Sharing\OrphanHelper; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +/** + * Class FixShareOwnersTest + * + * @package OCA\Files_Sharing\Tests\Command + */ +class FixShareOwnersTest extends TestCase { + /** + * @var FixShareOwners + */ + private $command; + + /** + * @var OrphanHelper|\PHPUnit\Framework\MockObject\MockObject + */ + private $orphanHelper; + + protected function setUp(): void { + parent::setUp(); + + $this->orphanHelper = $this->createMock(OrphanHelper::class); + $this->command = new FixShareOwners($this->orphanHelper); + } + + public function testExecuteNoSharesDetected() { + $this->orphanHelper->expects($this->once()) + ->method('getAllShares') + ->willReturn([ + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], + ]); + $this->orphanHelper->expects($this->exactly(2)) + ->method('isShareValid') + ->willReturn(true); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $output->expects($this->once()) + ->method('writeln') + ->with('No broken shares detected'); + $this->command->execute($input, $output); + } + + public function testExecuteSharesDetected() { + $this->orphanHelper->expects($this->once()) + ->method('getAllShares') + ->willReturn([ + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], + ]); + $this->orphanHelper->expects($this->exactly(2)) + ->method('isShareValid') + ->willReturnOnConsecutiveCalls(true, false); + $this->orphanHelper->expects($this->once()) + ->method('fileExists') + ->willReturn(true); + $this->orphanHelper->expects($this->once()) + ->method('findOwner') + ->willReturn('newOwner'); + $this->orphanHelper->expects($this->once()) + ->method('updateShareOwner'); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $output->expects($this->once()) + ->method('writeln') + ->with('Share with id <info>2</info> (target: <info>target2</info>) updated to owner <info>newOwner</info>'); + $this->command->execute($input, $output); + } + + public function testExecuteSharesDetectedDryRun() { + $this->orphanHelper->expects($this->once()) + ->method('getAllShares') + ->willReturn([ + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], + ]); + $this->orphanHelper->expects($this->exactly(2)) + ->method('isShareValid') + ->willReturnOnConsecutiveCalls(true, false); + $this->orphanHelper->expects($this->once()) + ->method('fileExists') + ->willReturn(true); + $this->orphanHelper->expects($this->once()) + ->method('findOwner') + ->willReturn('newOwner'); + $this->orphanHelper->expects($this->never()) + ->method('updateShareOwner'); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $output->expects($this->once()) + ->method('writeln') + ->with('Share with id <info>2</info> (target: <info>target2</info>) can be updated to owner <info>newOwner</info>'); + $input->expects($this->once()) + ->method('getOption') + ->with('dry-run') + ->willReturn(true); + $this->command->execute($input, $output); + } +} |