BackgroundCleanupJobTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Files\AppData\Factory;
  24. use OC\Preview\BackgroundCleanupJob;
  25. use OC\PreviewManager;
  26. use OC\SystemConfig;
  27. use OCP\Files\IRootFolder;
  28. use OCP\IDBConnection;
  29. use Test\Traits\MountProviderTrait;
  30. use Test\Traits\UserTrait;
  31. /**
  32. * Class BackgroundCleanupJobTest
  33. *
  34. * @group DB
  35. *
  36. * @package Test\Preview
  37. */
  38. class BackgroundCleanupJobTest extends \Test\TestCase {
  39. use MountProviderTrait;
  40. use UserTrait;
  41. /** @var string */
  42. private $userId;
  43. /** @var bool */
  44. private $trashEnabled;
  45. /** @var Factory */
  46. private $appDataFactory;
  47. /** @var IDBConnection */
  48. private $connection;
  49. /** @var PreviewManager */
  50. private $previewManager;
  51. /** @var IRootFolder */
  52. private $rootFolder;
  53. public function setUp() {
  54. parent::setUp();
  55. $this->userId = $this->getUniqueID();
  56. $this->createUser($this->userId, $this->userId);
  57. $storage = new \OC\Files\Storage\Temporary([]);
  58. $this->registerMount($this->userId, $storage, '');
  59. $this->loginAsUser($this->userId);
  60. $this->logout();
  61. $this->loginAsUser($this->userId);
  62. $appManager = \OC::$server->getAppManager();
  63. $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $this->userId);
  64. $appManager->disableApp('files_trashbin');
  65. $this->appDataFactory = new Factory(
  66. \OC::$server->getRootFolder(),
  67. \OC::$server->getSystemConfig()
  68. );
  69. $this->connection = \OC::$server->getDatabaseConnection();
  70. $this->previewManager = \OC::$server->getPreviewManager();
  71. $this->rootFolder = \OC::$server->getRootFolder();
  72. }
  73. public function tearDown() {
  74. if ($this->trashEnabled) {
  75. $appManager = \OC::$server->getAppManager();
  76. $appManager->enableApp('files_trashbin');
  77. }
  78. $this->logout();
  79. return parent::tearDown();
  80. }
  81. private function setup11Previews(): array {
  82. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  83. $files = [];
  84. for ($i = 0; $i < 11; $i++) {
  85. $file = $userFolder->newFile($i.'.txt');
  86. $file->putContent('hello world!');
  87. $this->previewManager->getPreview($file);
  88. $files[] = $file;
  89. }
  90. return $files;
  91. }
  92. public function testCleanupSystemCron() {
  93. $files = $this->setup11Previews();
  94. $preview = $this->appDataFactory->get('preview');
  95. $previews = $preview->getDirectoryListing();
  96. $this->assertCount(11, $previews);
  97. $job = new BackgroundCleanupJob($this->connection, $this->appDataFactory, true);
  98. $job->run([]);
  99. foreach ($files as $file) {
  100. $file->delete();
  101. }
  102. $this->assertCount(11, $previews);
  103. $job->run([]);
  104. $previews = $preview->getDirectoryListing();
  105. $this->assertCount(0, $previews);
  106. }
  107. public function testCleanupAjax() {
  108. $files = $this->setup11Previews();
  109. $preview = $this->appDataFactory->get('preview');
  110. $previews = $preview->getDirectoryListing();
  111. $this->assertCount(11, $previews);
  112. $job = new BackgroundCleanupJob($this->connection, $this->appDataFactory, false);
  113. $job->run([]);
  114. foreach ($files as $file) {
  115. $file->delete();
  116. }
  117. $this->assertCount(11, $previews);
  118. $job->run([]);
  119. $previews = $preview->getDirectoryListing();
  120. $this->assertCount(1, $previews);
  121. $job->run([]);
  122. $previews = $preview->getDirectoryListing();
  123. $this->assertCount(0, $previews);
  124. }
  125. }