1
0

DeleteOrphanedFilesTest.php 3.7 KB

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