1
0

DeleteOrphanedFilesTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files\Tests\Command;
  28. use OC\Files\View;
  29. use OCA\Files\Command\DeleteOrphanedFiles;
  30. use OCP\Files\StorageNotAvailableException;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. use Test\TestCase;
  34. /**
  35. * Class DeleteOrphanedFilesTest
  36. *
  37. * @group DB
  38. *
  39. * @package OCA\Files\Tests\Command
  40. */
  41. class DeleteOrphanedFilesTest extends TestCase {
  42. /**
  43. * @var DeleteOrphanedFiles
  44. */
  45. private $command;
  46. /**
  47. * @var \OCP\IDBConnection
  48. */
  49. private $connection;
  50. /**
  51. * @var string
  52. */
  53. private $user1;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->connection = \OC::$server->getDatabaseConnection();
  57. $this->user1 = $this->getUniqueID('user1_');
  58. $userManager = \OC::$server->getUserManager();
  59. $userManager->createUser($this->user1, 'pass');
  60. $this->command = new DeleteOrphanedFiles($this->connection);
  61. }
  62. protected function tearDown(): void {
  63. $userManager = \OC::$server->getUserManager();
  64. $user1 = $userManager->get($this->user1);
  65. if ($user1) {
  66. $user1->delete();
  67. }
  68. $this->logout();
  69. parent::tearDown();
  70. }
  71. protected function getFile($fileId) {
  72. $stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*filecache` WHERE `fileid` = ?', [$fileId]);
  73. return $stmt->fetchAll();
  74. }
  75. protected function getMounts($storageId) {
  76. $stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*mounts` WHERE `storage_id` = ?', [$storageId]);
  77. return $stmt->fetchAll();
  78. }
  79. /**
  80. * Test clearing orphaned files
  81. */
  82. public function testClearFiles() {
  83. $input = $this->getMockBuilder(InputInterface::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $output = $this->getMockBuilder(OutputInterface::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. // scan home storage so that mounts are properly setup
  90. \OC::$server->getRootFolder()->getUserFolder($this->user1)->getStorage()->getScanner()->scan('');
  91. $this->loginAsUser($this->user1);
  92. $view = new View('/' . $this->user1 . '/');
  93. $view->mkdir('files/test');
  94. $fileInfo = $view->getFileInfo('files/test');
  95. $storageId = $fileInfo->getStorage()->getId();
  96. $numericStorageId = $fileInfo->getStorage()->getStorageCache()->getNumericId();
  97. $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is available');
  98. $this->assertCount(1, $this->getMounts($numericStorageId), 'Asserts that mount is available');
  99. $this->command->execute($input, $output);
  100. $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available');
  101. $this->assertCount(1, $this->getMounts($numericStorageId), 'Asserts that mount is still available');
  102. $deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]);
  103. $this->assertNotNull($deletedRows, 'Asserts that storage got deleted');
  104. $this->assertSame(1, $deletedRows, 'Asserts that storage got deleted');
  105. // parent folder, `files`, ´test` and `welcome.txt` => 4 elements
  106. $output
  107. ->expects($this->exactly(3))
  108. ->method('writeln')
  109. ->withConsecutive(
  110. ['3 orphaned file cache entries deleted'],
  111. ['0 orphaned file cache extended entries deleted'],
  112. ['1 orphaned mount entries deleted'],
  113. );
  114. $this->command->execute($input, $output);
  115. $this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up');
  116. $this->assertCount(0, $this->getMounts($numericStorageId), 'Asserts that mount gets cleaned up');
  117. // since we deleted the storage it might throw a (valid) StorageNotAvailableException
  118. try {
  119. $view->unlink('files/test');
  120. } catch (StorageNotAvailableException $e) {
  121. }
  122. }
  123. }