BackgroundCleanupJobTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace Test\Preview;
  7. use OC\Preview\BackgroundCleanupJob;
  8. use OC\Preview\Storage\Root;
  9. use OC\PreviewManager;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\Files\File;
  12. use OCP\Files\IMimeTypeLoader;
  13. use OCP\Files\IRootFolder;
  14. use OCP\Files\NotFoundException;
  15. use OCP\IDBConnection;
  16. use Test\Traits\MountProviderTrait;
  17. use Test\Traits\UserTrait;
  18. /**
  19. * Class BackgroundCleanupJobTest
  20. *
  21. * @group DB
  22. *
  23. * @package Test\Preview
  24. */
  25. class BackgroundCleanupJobTest extends \Test\TestCase {
  26. use MountProviderTrait;
  27. use UserTrait;
  28. /** @var string */
  29. private $userId;
  30. /** @var bool */
  31. private $trashEnabled;
  32. /** @var IDBConnection */
  33. private $connection;
  34. /** @var PreviewManager */
  35. private $previewManager;
  36. /** @var IRootFolder */
  37. private $rootFolder;
  38. /** @var IMimeTypeLoader */
  39. private $mimeTypeLoader;
  40. private ITimeFactory $timeFactory;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->userId = $this->getUniqueID();
  44. $user = $this->createUser($this->userId, $this->userId);
  45. $storage = new \OC\Files\Storage\Temporary([]);
  46. $this->registerMount($this->userId, $storage, '');
  47. $this->loginAsUser($this->userId);
  48. $this->logout();
  49. $this->loginAsUser($this->userId);
  50. $appManager = \OC::$server->getAppManager();
  51. $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $user);
  52. $appManager->disableApp('files_trashbin');
  53. $this->connection = \OC::$server->getDatabaseConnection();
  54. $this->previewManager = \OC::$server->getPreviewManager();
  55. $this->rootFolder = \OC::$server->get(IRootFolder::class);
  56. $this->mimeTypeLoader = \OC::$server->getMimeTypeLoader();
  57. $this->timeFactory = \OCP\Server::get(ITimeFactory::class);
  58. }
  59. protected function tearDown(): void {
  60. if ($this->trashEnabled) {
  61. $appManager = \OC::$server->getAppManager();
  62. $appManager->enableApp('files_trashbin');
  63. }
  64. $this->logout();
  65. parent::tearDown();
  66. }
  67. private function getRoot(): Root {
  68. return new Root(
  69. \OC::$server->get(IRootFolder::class),
  70. \OC::$server->getSystemConfig()
  71. );
  72. }
  73. private function setup11Previews(): array {
  74. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  75. $files = [];
  76. for ($i = 0; $i < 11; $i++) {
  77. $file = $userFolder->newFile($i.'.txt');
  78. $file->putContent('hello world!');
  79. $this->previewManager->getPreview($file);
  80. $files[] = $file;
  81. }
  82. return $files;
  83. }
  84. private function countPreviews(Root $previewRoot, array $fileIds): int {
  85. $i = 0;
  86. foreach ($fileIds as $fileId) {
  87. try {
  88. $previewRoot->getFolder((string)$fileId);
  89. } catch (NotFoundException $e) {
  90. continue;
  91. }
  92. $i++;
  93. }
  94. return $i;
  95. }
  96. public function testCleanupSystemCron(): void {
  97. $files = $this->setup11Previews();
  98. $fileIds = array_map(function (File $f) {
  99. return $f->getId();
  100. }, $files);
  101. $root = $this->getRoot();
  102. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  103. $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $root, $this->mimeTypeLoader, true);
  104. $job->run([]);
  105. foreach ($files as $file) {
  106. $file->delete();
  107. }
  108. $root = $this->getRoot();
  109. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  110. $job->run([]);
  111. $root = $this->getRoot();
  112. $this->assertSame(0, $this->countPreviews($root, $fileIds));
  113. }
  114. public function testCleanupAjax(): void {
  115. if ($this->connection->getShardDefinition('filecache')) {
  116. $this->markTestSkipped('ajax cron is not supported for sharded setups');
  117. return;
  118. }
  119. $files = $this->setup11Previews();
  120. $fileIds = array_map(function (File $f) {
  121. return $f->getId();
  122. }, $files);
  123. $root = $this->getRoot();
  124. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  125. $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $root, $this->mimeTypeLoader, false);
  126. $job->run([]);
  127. foreach ($files as $file) {
  128. $file->delete();
  129. }
  130. $root = $this->getRoot();
  131. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  132. $job->run([]);
  133. $root = $this->getRoot();
  134. $this->assertSame(1, $this->countPreviews($root, $fileIds));
  135. $job->run([]);
  136. $root = $this->getRoot();
  137. $this->assertSame(0, $this->countPreviews($root, $fileIds));
  138. }
  139. public function testOldPreviews(): void {
  140. if ($this->connection->getShardDefinition('filecache')) {
  141. $this->markTestSkipped('old previews are not supported for sharded setups');
  142. return;
  143. }
  144. $appdata = \OC::$server->getAppDataDir('preview');
  145. $f1 = $appdata->newFolder('123456781');
  146. $f1->newFile('foo.jpg', 'foo');
  147. $f2 = $appdata->newFolder('123456782');
  148. $f2->newFile('foo.jpg', 'foo');
  149. $f2 = $appdata->newFolder((string)PHP_INT_MAX - 1);
  150. $f2->newFile('foo.jpg', 'foo');
  151. $appdata = \OC::$server->getAppDataDir('preview');
  152. $this->assertSame(3, count($appdata->getDirectoryListing()));
  153. $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true);
  154. $job->run([]);
  155. $appdata = \OC::$server->getAppDataDir('preview');
  156. $this->assertSame(0, count($appdata->getDirectoryListing()));
  157. }
  158. }