BackgroundCleanupJobTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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() {
  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() {
  115. $files = $this->setup11Previews();
  116. $fileIds = array_map(function (File $f) {
  117. return $f->getId();
  118. }, $files);
  119. $root = $this->getRoot();
  120. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  121. $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $root, $this->mimeTypeLoader, false);
  122. $job->run([]);
  123. foreach ($files as $file) {
  124. $file->delete();
  125. }
  126. $root = $this->getRoot();
  127. $this->assertSame(11, $this->countPreviews($root, $fileIds));
  128. $job->run([]);
  129. $root = $this->getRoot();
  130. $this->assertSame(1, $this->countPreviews($root, $fileIds));
  131. $job->run([]);
  132. $root = $this->getRoot();
  133. $this->assertSame(0, $this->countPreviews($root, $fileIds));
  134. }
  135. public function testOldPreviews() {
  136. $appdata = \OC::$server->getAppDataDir('preview');
  137. $f1 = $appdata->newFolder('123456781');
  138. $f1->newFile('foo.jpg', 'foo');
  139. $f2 = $appdata->newFolder('123456782');
  140. $f2->newFile('foo.jpg', 'foo');
  141. $f2 = $appdata->newFolder((string)PHP_INT_MAX - 1);
  142. $f2->newFile('foo.jpg', 'foo');
  143. $appdata = \OC::$server->getAppDataDir('preview');
  144. $this->assertSame(3, count($appdata->getDirectoryListing()));
  145. $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true);
  146. $job->run([]);
  147. $appdata = \OC::$server->getAppDataDir('preview');
  148. $this->assertSame(0, count($appdata->getDirectoryListing()));
  149. }
  150. }