DeleteOrphanedFilesTest.php 3.6 KB

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