|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\Settings\Tests; |
| 10 | + |
| 11 | +use OCA\Settings\SetupChecks\TaskProcessingWorkerIsRunning; |
| 12 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 13 | +use OCP\IAppConfig; |
| 14 | +use OCP\IL10N; |
| 15 | +use OCP\SetupCheck\SetupResult; |
| 16 | +use OCP\TaskProcessing\IManager; |
| 17 | +use OCP\TaskProcessing\Task; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use Test\TestCase; |
| 20 | + |
| 21 | +class TaskProcessingWorkerIsRunningTest extends TestCase { |
| 22 | + private IL10N&MockObject $l10n; |
| 23 | + private ITimeFactory&MockObject $timeFactory; |
| 24 | + private IManager&MockObject $taskProcessingManager; |
| 25 | + private IAppConfig&MockObject $appConfig; |
| 26 | + |
| 27 | + private TaskProcessingWorkerIsRunning $check; |
| 28 | + |
| 29 | + protected function setUp(): void { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + $this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); |
| 33 | + $this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock(); |
| 34 | + $this->taskProcessingManager = $this->getMockBuilder(IManager::class)->getMock(); |
| 35 | + $this->appConfig = $this->getMockBuilder(IAppConfig::class)->getMock(); |
| 36 | + |
| 37 | + $this->check = new TaskProcessingWorkerIsRunning( |
| 38 | + $this->l10n, |
| 39 | + $this->taskProcessingManager, |
| 40 | + $this->timeFactory, |
| 41 | + $this->appConfig |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + public function testPass(): void { |
| 46 | + $tasks = []; |
| 47 | + for ($i = 0; $i < 10; $i++) { |
| 48 | + $task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i); |
| 49 | + $task->setStartedAt($this->timeFactory->now()->getTimestamp()); |
| 50 | + $task->setScheduledAt($this->timeFactory->now()->getTimestamp()); |
| 51 | + $task->setEndedAt($this->timeFactory->now()->getTimestamp()); |
| 52 | + $task->setStatus(Task::STATUS_SUCCESSFUL); |
| 53 | + $tasks[] = $task; |
| 54 | + } |
| 55 | + $this->taskProcessingManager->method('getTasks')->willReturn($tasks); |
| 56 | + $this->timeFactory->method('now')->willReturn(new \DateTimeImmutable()); |
| 57 | + $this->appConfig->method('getValueString')->willReturn((string)$this->timeFactory->now()->getTimestamp()); |
| 58 | + |
| 59 | + $this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testFail(): void { |
| 63 | + $tasks = []; |
| 64 | + for ($i = 0; $i < 10; $i++) { |
| 65 | + $task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i); |
| 66 | + $task->setStartedAt($this->timeFactory->now()->getTimestamp()); |
| 67 | + $task->setScheduledAt($this->timeFactory->now()->getTimestamp()); |
| 68 | + $task->setEndedAt($this->timeFactory->now()->getTimestamp()); |
| 69 | + $task->setStatus(Task::STATUS_SUCCESSFUL); |
| 70 | + $tasks[] = $task; |
| 71 | + } |
| 72 | + $this->taskProcessingManager->method('getTasks')->willReturn($tasks); |
| 73 | + $this->timeFactory->method('now')->willReturn(new \DateTimeImmutable()); |
| 74 | + $this->appConfig->method('getValueString')->willReturn((string)($this->timeFactory->now()->getTimestamp() - 60 * 10)); |
| 75 | + |
| 76 | + $this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity()); |
| 77 | + } |
| 78 | +} |
0 commit comments