BackgroundCleanupJobTest.php 5.3 KB

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