userId = $this->getUniqueID(); $user = $this->createUser($this->userId, $this->userId); $storage = new Temporary([]); $this->registerMount($this->userId, $storage, ''); $this->loginAsUser($this->userId); $this->logout(); $this->loginAsUser($this->userId); $appManager = Server::get(IAppManager::class); $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $user); $appManager->disableApp('files_trashbin'); $this->connection = Server::get(IDBConnection::class); $this->previewManager = Server::get(IPreview::class); $this->rootFolder = Server::get(IRootFolder::class); $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class); $this->timeFactory = Server::get(ITimeFactory::class); $this->previewService = Server::get(PreviewService::class); } protected function tearDown(): void { if ($this->trashEnabled) { $appManager = Server::get(IAppManager::class); $appManager->enableApp('files_trashbin'); } $this->logout(); foreach ($this->previewService->getAvailablePreviewsForFile(5) as $preview) { $this->previewService->deletePreview($preview); } parent::tearDown(); } private function setup11Previews(): array { $userFolder = $this->rootFolder->getUserFolder($this->userId); $files = []; foreach (range(0, 10) as $i) { $file = $userFolder->newFile($i . '.txt'); $file->putContent('hello world!'); $this->previewManager->getPreview($file); $files[] = $file; } return $files; } private function countPreviews(PreviewService $previewService, array $fileIds): int { $previews = $previewService->getAvailablePreviews($fileIds); return array_reduce($previews, fn (int $result, array $previews) => $result + count($previews), 0); } public function testCleanupSystemCron(): void { $files = $this->setup11Previews(); $fileIds = array_map(fn (File $f): int => $f->getId(), $files); $this->assertSame(11, $this->countPreviews($this->previewService, $fileIds)); $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->previewService, true); $job->run([]); foreach ($files as $file) { $file->delete(); } $this->assertSame(11, $this->countPreviews($this->previewService, $fileIds)); $job->run([]); $this->assertSame(0, $this->countPreviews($this->previewService, $fileIds)); } public function testCleanupAjax(): void { if ($this->connection->getShardDefinition('filecache')) { $this->markTestSkipped('ajax cron is not supported for sharded setups'); } $files = $this->setup11Previews(); $fileIds = array_map(fn (File $f): int => $f->getId(), $files); $this->assertSame(11, $this->countPreviews($this->previewService, $fileIds)); $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->previewService, false); $job->run([]); foreach ($files as $file) { $file->delete(); } $this->assertSame(11, $this->countPreviews($this->previewService, $fileIds)); $job->run([]); $this->assertSame(1, $this->countPreviews($this->previewService, $fileIds)); $job->run([]); $this->assertSame(0, $this->countPreviews($this->previewService, $fileIds)); } }