DeleteOrphanedFilesTest.php 3.8 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\StorageNotAvailableException;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. /**
  15. * Class DeleteOrphanedFilesTest
  16. *
  17. * @group DB
  18. *
  19. * @package OCA\Files\Tests\Command
  20. */
  21. class DeleteOrphanedFilesTest extends TestCase {
  22. /**
  23. * @var DeleteOrphanedFiles
  24. */
  25. private $command;
  26. /**
  27. * @var \OCP\IDBConnection
  28. */
  29. private $connection;
  30. /**
  31. * @var string
  32. */
  33. private $user1;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->connection = \OC::$server->getDatabaseConnection();
  37. $this->user1 = $this->getUniqueID('user1_');
  38. $userManager = \OC::$server->getUserManager();
  39. $userManager->createUser($this->user1, 'pass');
  40. $this->command = new DeleteOrphanedFiles($this->connection);
  41. }
  42. protected function tearDown(): void {
  43. $userManager = \OC::$server->getUserManager();
  44. $user1 = $userManager->get($this->user1);
  45. if ($user1) {
  46. $user1->delete();
  47. }
  48. $this->logout();
  49. parent::tearDown();
  50. }
  51. protected function getFile($fileId) {
  52. $stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*filecache` WHERE `fileid` = ?', [$fileId]);
  53. return $stmt->fetchAll();
  54. }
  55. protected function getMounts($storageId) {
  56. $stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*mounts` WHERE `storage_id` = ?', [$storageId]);
  57. return $stmt->fetchAll();
  58. }
  59. /**
  60. * Test clearing orphaned files
  61. */
  62. public function testClearFiles() {
  63. $input = $this->getMockBuilder(InputInterface::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $output = $this->getMockBuilder(OutputInterface::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. // scan home storage so that mounts are properly setup
  70. \OC::$server->getRootFolder()->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. // since we deleted the storage it might throw a (valid) StorageNotAvailableException
  98. try {
  99. $view->unlink('files/test');
  100. } catch (StorageNotAvailableException $e) {
  101. }
  102. }
  103. }