deleteorphanedfilestest.php 3.5 KB

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