BackgroundCleanupJobTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Preview;
  23. use OC\Preview\BackgroundCleanupJob;
  24. use OC\Preview\Storage\Root;
  25. use OC\PreviewManager;
  26. use OCP\Files\File;
  27. use OCP\Files\IMimeTypeLoader;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\IDBConnection;
  31. use Test\Traits\MountProviderTrait;
  32. use Test\Traits\UserTrait;
  33. /**
  34. * Class BackgroundCleanupJobTest
  35. *
  36. * @group DB
  37. *
  38. * @package Test\Preview
  39. */
  40. class BackgroundCleanupJobTest extends \Test\TestCase {
  41. use MountProviderTrait;
  42. use UserTrait;
  43. /** @var string */
  44. private $userId;
  45. /** @var bool */
  46. private $trashEnabled;
  47. /** @var IDBConnection */
  48. private $connection;
  49. /** @var PreviewManager */
  50. private $previewManager;
  51. /** @var IRootFolder */
  52. private $rootFolder;
  53. /** @var IMimeTypeLoader */
  54. private $mimeTypeLoader;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->userId = $this->getUniqueID();
  58. $this->createUser($this->userId, $this->userId);
  59. $storage = new \OC\Files\Storage\Temporary([]);
  60. $this->registerMount($this->userId, $storage, '');
  61. $this->loginAsUser($this->userId);
  62. $this->logout();
  63. $this->loginAsUser($this->userId);
  64. $appManager = \OC::$server->getAppManager();
  65. $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $this->userId);
  66. $appManager->disableApp('files_trashbin');
  67. $this->connection = \OC::$server->getDatabaseConnection();
  68. $this->previewManager = \OC::$server->getPreviewManager();
  69. $this->rootFolder = \OC::$server->getRootFolder();
  70. $this->mimeTypeLoader = \OC::$server->getMimeTypeLoader();
  71. }
  72. protected function tearDown(): void {
  73. if ($this->trashEnabled) {
  74. $appManager = \OC::$server->getAppManager();
  75. $appManager->enableApp('files_trashbin');
  76. }
  77. $this->logout();
  78. parent::tearDown();
  79. }
  80. private function getRoot(): Root {
  81. return new Root(
  82. \OC::$server->getRootFolder(),
  83. \OC::$server->getSystemConfig()
  84. );
  85. }
  86. private function setup11Previews(): array {
  87. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  88. $files = [];
  89. for ($i = 0; $i < 11; $i++) {
  90. $file = $userFolder->newFile($i.'.txt');
  91. $file->putContent('hello world!');
  92. $this->previewManager->getPreview($file);
  93. $files[] = $file;
  94. }
  95. return $files;
  96. }
  97. private function countPreviews(Root $previewRoot, array $fileIds): int {
  98. $i = 0;
  99. foreach ($fileIds as $fileId) {
  100. try {
  101. $previewRoot->getFolder((string)$fileId);
  102. } catch (NotFoundException $e) {
  103. continue;
  104. }
  105. $i++;
  106. }
  107. return $i;
  108. }
  109. public function testCleanupSystemCron() {
  110. $files = $this->setup11Previews();
  111. $fileIds = array_map(function (File $f) {
  112. return $f->getId();
  113. }, $files);
  114. $root = $this->getRoot();
  115. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  116. $job = new BackgroundCleanupJob($this->connection, $root, $this->mimeTypeLoader, true);
  117. $job->run([]);
  118. foreach ($files as $file) {
  119. $file->delete();
  120. }
  121. $root = $this->getRoot();
  122. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  123. $job->run([]);
  124. $root = $this->getRoot();
  125. $this->assertSame(0, $this->countPreviews($root, $fileIds));
  126. }
  127. public function testCleanupAjax() {
  128. $files = $this->setup11Previews();
  129. $fileIds = array_map(function (File $f) {
  130. return $f->getId();
  131. }, $files);
  132. $root = $this->getRoot();
  133. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  134. $job = new BackgroundCleanupJob($this->connection, $root, $this->mimeTypeLoader, false);
  135. $job->run([]);
  136. foreach ($files as $file) {
  137. $file->delete();
  138. }
  139. $root = $this->getRoot();
  140. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  141. $job->run([]);
  142. $root = $this->getRoot();
  143. $this->assertSame(1, $this->countPreviews($root, $fileIds));
  144. $job->run([]);
  145. $root = $this->getRoot();
  146. $this->assertSame(0, $this->countPreviews($root, $fileIds));
  147. }
  148. public function testOldPreviews() {
  149. $appdata = \OC::$server->getAppDataDir('preview');
  150. $f1 = $appdata->newFolder('123456781');
  151. $f1->newFile('foo.jpg', 'foo');
  152. $f2 = $appdata->newFolder('123456782');
  153. $f2->newFile('foo.jpg', 'foo');
  154. $appdata = \OC::$server->getAppDataDir('preview');
  155. $this->assertSame(2, count($appdata->getDirectoryListing()));
  156. $job = new BackgroundCleanupJob($this->connection, $this->getRoot(), $this->mimeTypeLoader, true);
  157. $job->run([]);
  158. $appdata = \OC::$server->getAppDataDir('preview');
  159. $this->assertSame(0, count($appdata->getDirectoryListing()));
  160. }
  161. }