2015-04-07 05:44:34 -04:00
|
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
|
declare(strict_types=1);
|
2015-04-07 05:44:34 -04:00
|
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-04-07 05:44:34 -04:00
|
|
|
|
*/
|
|
|
|
|
|
namespace OCA\Files\Tests\Command;
|
|
|
|
|
|
|
2016-09-23 11:21:07 -04:00
|
|
|
|
use OC\Files\View;
|
2015-04-07 05:44:34 -04:00
|
|
|
|
use OCA\Files\Command\DeleteOrphanedFiles;
|
2024-09-23 12:12:17 -04:00
|
|
|
|
use OCP\Files\IRootFolder;
|
2015-10-14 07:27:05 -04:00
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2024-09-23 12:12:17 -04:00
|
|
|
|
use OCP\IDBConnection;
|
2025-02-03 09:34:01 -05:00
|
|
|
|
use OCP\IUserManager;
|
2024-10-10 06:40:31 -04:00
|
|
|
|
use OCP\Server;
|
2017-10-26 07:46:16 -04:00
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2016-09-23 11:21:07 -04:00
|
|
|
|
use Test\TestCase;
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
2015-11-19 08:39:05 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Class DeleteOrphanedFilesTest
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* @package OCA\Files\Tests\Command
|
|
|
|
|
|
*/
|
2026-01-07 08:21:48 -05:00
|
|
|
|
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
2016-09-23 11:21:07 -04:00
|
|
|
|
class DeleteOrphanedFilesTest extends TestCase {
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
2024-09-23 12:12:17 -04:00
|
|
|
|
private DeleteOrphanedFiles $command;
|
|
|
|
|
|
private IDBConnection $connection;
|
|
|
|
|
|
private string $user1;
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
|
protected function setUp(): void {
|
2015-04-07 05:44:34 -04:00
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
|
$this->connection = Server::get(IDBConnection::class);
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
|
|
|
|
|
$this->user1 = $this->getUniqueID('user1_');
|
|
|
|
|
|
|
2025-02-03 09:34:01 -05:00
|
|
|
|
$userManager = Server::get(IUserManager::class);
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$userManager->createUser($this->user1, 'pass');
|
|
|
|
|
|
|
|
|
|
|
|
$this->command = new DeleteOrphanedFiles($this->connection);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
|
protected function tearDown(): void {
|
2025-02-03 09:34:01 -05:00
|
|
|
|
$userManager = Server::get(IUserManager::class);
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$user1 = $userManager->get($this->user1);
|
2020-04-10 08:19:56 -04:00
|
|
|
|
if ($user1) {
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$user1->delete();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->logout();
|
|
|
|
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
|
protected function getFile(int $fileId): array {
|
2024-08-15 12:04:55 -04:00
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
$query->select('*')
|
|
|
|
|
|
->from('filecache')
|
|
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId)));
|
2025-11-17 06:20:11 -05:00
|
|
|
|
return $query->executeQuery()->fetchAllAssociative();
|
2015-04-07 05:44:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-10 10:32:53 -05:00
|
|
|
|
protected function getMountsCount(int $storageId): int {
|
2024-08-15 12:04:55 -04:00
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2025-12-10 10:32:53 -05:00
|
|
|
|
$query->select($query->func()->count())
|
2024-08-15 12:04:55 -04:00
|
|
|
|
->from('mounts')
|
|
|
|
|
|
->where($query->expr()->eq('storage_id', $query->createNamedParameter($storageId)));
|
2025-12-10 10:32:53 -05:00
|
|
|
|
return (int)$query->executeQuery()->fetchOne();
|
2020-07-08 03:54:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-07 05:44:34 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Test clearing orphaned files
|
|
|
|
|
|
*/
|
2024-09-15 16:32:31 -04:00
|
|
|
|
public function testClearFiles(): void {
|
2025-05-29 06:19:28 -04:00
|
|
|
|
$input = $this->createMock(InputInterface::class);
|
|
|
|
|
|
$output = $this->createMock(OutputInterface::class);
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
|
$rootFolder = Server::get(IRootFolder::class);
|
2024-09-23 12:12:17 -04:00
|
|
|
|
|
2020-07-08 03:54:26 -04:00
|
|
|
|
// scan home storage so that mounts are properly setup
|
2024-09-23 12:12:17 -04:00
|
|
|
|
$rootFolder->getUserFolder($this->user1)->getStorage()->getScanner()->scan('');
|
2020-07-08 03:54:26 -04:00
|
|
|
|
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$this->loginAsUser($this->user1);
|
|
|
|
|
|
|
2016-09-23 11:21:07 -04:00
|
|
|
|
$view = new View('/' . $this->user1 . '/');
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$view->mkdir('files/test');
|
|
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test');
|
|
|
|
|
|
|
|
|
|
|
|
$storageId = $fileInfo->getStorage()->getId();
|
2026-01-07 08:21:48 -05:00
|
|
|
|
$numericStorageId = $fileInfo->getStorage()->getCache()->getNumericStorageId();
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is available');
|
2025-12-10 10:32:53 -05:00
|
|
|
|
$this->assertEquals(1, $this->getMountsCount($numericStorageId), 'Asserts that mount is available');
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
|
|
|
|
|
$this->command->execute($input, $output);
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available');
|
2025-12-10 10:32:53 -05:00
|
|
|
|
$this->assertEquals(1, $this->getMountsCount($numericStorageId), 'Asserts that mount is still available');
|
2020-07-08 03:54:26 -04:00
|
|
|
|
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
|
|
|
|
|
$deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]);
|
|
|
|
|
|
$this->assertNotNull($deletedRows, 'Asserts that storage got deleted');
|
|
|
|
|
|
$this->assertSame(1, $deletedRows, 'Asserts that storage got deleted');
|
|
|
|
|
|
|
|
|
|
|
|
// parent folder, `files`, ´test` and `welcome.txt` => 4 elements
|
2025-05-29 06:19:28 -04:00
|
|
|
|
$calls = [
|
|
|
|
|
|
'3 orphaned file cache entries deleted',
|
|
|
|
|
|
'0 orphaned file cache extended entries deleted',
|
|
|
|
|
|
'1 orphaned mount entries deleted',
|
|
|
|
|
|
];
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$output
|
2024-02-09 08:49:56 -05:00
|
|
|
|
->expects($this->exactly(3))
|
2015-04-07 05:44:34 -04:00
|
|
|
|
->method('writeln')
|
2025-06-30 10:56:59 -04:00
|
|
|
|
->willReturnCallback(function (string $message) use (&$calls): void {
|
2025-05-29 06:19:28 -04:00
|
|
|
|
$expected = array_shift($calls);
|
|
|
|
|
|
$this->assertSame($expected, $message);
|
|
|
|
|
|
});
|
2020-07-08 03:54:26 -04:00
|
|
|
|
|
2015-04-07 05:44:34 -04:00
|
|
|
|
$this->command->execute($input, $output);
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up');
|
2025-12-10 10:32:53 -05:00
|
|
|
|
$this->assertEquals(0, $this->getMountsCount($numericStorageId), 'Asserts that mount gets cleaned up');
|
2015-04-07 05:44:34 -04:00
|
|
|
|
|
2024-09-23 12:12:17 -04:00
|
|
|
|
// Rescan folder to add back to cache before deleting
|
|
|
|
|
|
$rootFolder->getUserFolder($this->user1)->getStorage()->getScanner()->scan('');
|
2015-10-14 07:27:05 -04:00
|
|
|
|
// since we deleted the storage it might throw a (valid) StorageNotAvailableException
|
|
|
|
|
|
try {
|
|
|
|
|
|
$view->unlink('files/test');
|
|
|
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
|
|
}
|
2015-04-07 05:44:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|