DeleteOrphanedFilesTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\Tests\Command;
  8. use OC\Files\View;
  9. use OCA\Files\Command\DeleteOrphanedFiles;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\StorageNotAvailableException;
  12. use OCP\IDBConnection;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Test\TestCase;
  16. /**
  17. * Class DeleteOrphanedFilesTest
  18. *
  19. * @group DB
  20. *
  21. * @package OCA\Files\Tests\Command
  22. */
  23. class DeleteOrphanedFilesTest extends TestCase {
  24. private DeleteOrphanedFiles $command;
  25. private IDBConnection $connection;
  26. private string $user1;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->connection = \OCP\Server::get(IDBConnection::class);
  30. $this->user1 = $this->getUniqueID('user1_');
  31. $userManager = \OC::$server->getUserManager();
  32. $userManager->createUser($this->user1, 'pass');
  33. $this->command = new DeleteOrphanedFiles($this->connection);
  34. }
  35. protected function tearDown(): void {
  36. $userManager = \OC::$server->getUserManager();
  37. $user1 = $userManager->get($this->user1);
  38. if ($user1) {
  39. $user1->delete();
  40. }
  41. $this->logout();
  42. parent::tearDown();
  43. }
  44. protected function getFile($fileId) {
  45. $query = $this->connection->getQueryBuilder();
  46. $query->select('*')
  47. ->from('filecache')
  48. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId)));
  49. return $query->executeQuery()->fetchAll();
  50. }
  51. protected function getMounts($storageId) {
  52. $query = $this->connection->getQueryBuilder();
  53. $query->select('*')
  54. ->from('mounts')
  55. ->where($query->expr()->eq('storage_id', $query->createNamedParameter($storageId)));
  56. return $query->executeQuery()->fetchAll();
  57. }
  58. /**
  59. * Test clearing orphaned files
  60. */
  61. public function testClearFiles(): void {
  62. $input = $this->getMockBuilder(InputInterface::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $output = $this->getMockBuilder(OutputInterface::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $rootFolder = \OCP\Server::get(IRootFolder::class);
  69. // scan home storage so that mounts are properly setup
  70. $rootFolder->getUserFolder($this->user1)->getStorage()->getScanner()->scan('');
  71. $this->loginAsUser($this->user1);
  72. $view = new View('/' . $this->user1 . '/');
  73. $view->mkdir('files/test');
  74. $fileInfo = $view->getFileInfo('files/test');
  75. $storageId = $fileInfo->getStorage()->getId();
  76. $numericStorageId = $fileInfo->getStorage()->getStorageCache()->getNumericId();
  77. $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is available');
  78. $this->assertCount(1, $this->getMounts($numericStorageId), 'Asserts that mount is available');
  79. $this->command->execute($input, $output);
  80. $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available');
  81. $this->assertCount(1, $this->getMounts($numericStorageId), 'Asserts that mount is still available');
  82. $deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]);
  83. $this->assertNotNull($deletedRows, 'Asserts that storage got deleted');
  84. $this->assertSame(1, $deletedRows, 'Asserts that storage got deleted');
  85. // parent folder, `files`, ´test` and `welcome.txt` => 4 elements
  86. $output
  87. ->expects($this->exactly(3))
  88. ->method('writeln')
  89. ->withConsecutive(
  90. ['3 orphaned file cache entries deleted'],
  91. ['0 orphaned file cache extended entries deleted'],
  92. ['1 orphaned mount entries deleted'],
  93. );
  94. $this->command->execute($input, $output);
  95. $this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up');
  96. $this->assertCount(0, $this->getMounts($numericStorageId), 'Asserts that mount gets cleaned up');
  97. // Rescan folder to add back to cache before deleting
  98. $rootFolder->getUserFolder($this->user1)->getStorage()->getScanner()->scan('');
  99. // since we deleted the storage it might throw a (valid) StorageNotAvailableException
  100. try {
  101. $view->unlink('files/test');
  102. } catch (StorageNotAvailableException $e) {
  103. }
  104. }
  105. }