BackgroundCleanupJobTest.php 3.8 KB

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