Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions tests/Unit/BackgroundJob/LogCleanUpTaskBehaviourTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/**
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Test
* @package OCA\OpenRegister\Tests\Unit\BackgroundJob
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
* @version GIT: <git-id>
* @link https://OpenRegister.app
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Tests\Unit\BackgroundJob;

use DateTimeInterface;
use OCA\OpenRegister\BackgroundJob\LogCleanUpTask;
use OCA\OpenRegister\BackgroundJob\Support\LogCleanUpTrails;
use OCA\OpenRegister\Db\AuditTrailMapperInterface;
use OCA\OpenRegister\Db\SearchTrailCleanupInterface;
use OCA\OpenRegister\Db\StateHistoryMapperInterface;
use OCA\OpenRegister\Service\Settings\RetentionSettingsReaderInterface;
use OCP\AppFramework\Utility\ITimeFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* The state-history prune step of LogCleanUpTask.
*/
class LogCleanUpTaskBehaviourTest extends TestCase {

private AuditTrailMapperInterface&MockObject $audit;

private StateHistoryMapperInterface&MockObject $history;

/** @var array<int, array{0: string, 1: string}> */
private array $logged = [];

protected function setUp(): void {
parent::setUp();
$this->audit = $this->createMock(AuditTrailMapperInterface::class);
$this->history = $this->createMock(StateHistoryMapperInterface::class);
$this->logged = [];
}

private function runTask(): void {
$logger = $this->createMock(LoggerInterface::class);
foreach (['info', 'debug', 'error', 'warning'] as $level) {
$logger->method($level)->willReturnCallback(
function (string $message, array $context = []) use ($level): void {
$this->logged[] = [$level, $message];
}
);
}
$retention = $this->createMock(RetentionSettingsReaderInterface::class);
$retention->method('getRetentionSettingsOnly')->willReturn(['searchTrailRetention' => 0]);

$task = new LogCleanUpTask(
$this->createMock(ITimeFactory::class),
new LogCleanUpTrails($this->audit, $this->createMock(SearchTrailCleanupInterface::class), $this->history),
$retention,
$logger
);
(new \ReflectionMethod($task, 'run'))->invoke($task, null);
}

/** @return string[] */
private function messages(string $level): array {
$out = [];
foreach ($this->logged as [$lvl, $msg]) {
if ($lvl === $level && str_contains($msg, 'tate-history') === true) {
$out[] = $msg;
}
}
return $out;
}

public function testPurgedHorizonsPruneClosedIntervalsAndSkipEmptyHorizons(): void {
$this->audit->method('findPurgedHorizons')->willReturn([
'obj-1' => '2026-01-01T00:00:00+00:00',
'obj-2' => '',
'obj-3' => '2025-06-01T00:00:00+00:00',
]);
$calls = [];
$this->history->expects($this->exactly(2))->method('pruneClosedIntervalsBefore')->willReturnCallback(
function (string $objectUuid, DateTimeInterface $horizon) use (&$calls): int {
$calls[] = [$objectUuid, $horizon->format('Y-m-d')];
return 2;
}
);

$this->runTask();

$this->assertSame([['obj-1', '2026-01-01'], ['obj-3', '2025-06-01']], $calls);
$this->assertSame(['[LogCleanUpTask] Pruned 4 state-history intervals whose trail was purged'], $this->messages('info'));
}

public function testNothingPrunedLogsNothing(): void {
$this->audit->method('findPurgedHorizons')->willReturn(['obj-1' => '2026-01-01']);
$this->history->method('pruneClosedIntervalsBefore')->willReturn(0);

$this->runTask();

$this->assertSame([], $this->messages('info'));
$this->assertSame([], $this->messages('warning'));
}

public function testNoPurgedObjectsPrunesNothing(): void {
$this->audit->method('findPurgedHorizons')->willReturn([]);
$this->history->expects($this->never())->method('pruneClosedIntervalsBefore');

$this->runTask();

$this->assertSame([], $this->messages('info'));
}

public function testPruneFailureIsOnlyLoggedAsWarning(): void {
$this->audit->method('findPurgedHorizons')->willThrowException(new \RuntimeException('db gone'));

$this->runTask();

$this->assertSame(['[LogCleanUpTask] State-history prune failed: db gone'], $this->messages('warning'));
}
}
48 changes: 48 additions & 0 deletions tests/Unit/BackgroundJob/ReviewReminderJobBehaviourTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Test
* @package OCA\OpenRegister\Tests\Unit\BackgroundJob
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
* @version GIT: <git-id>
* @link https://OpenRegister.app
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Tests\Unit\BackgroundJob;

use OCA\OpenRegister\BackgroundJob\ReviewReminderJob;
use OCA\OpenRegister\Service\Settings\ArchivalSettingsReaderInterface;
use OCP\AppFramework\Utility\ITimeFactory;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

/**
* The reminder interval derived from the configured frequency.
*/
class ReviewReminderJobBehaviourTest extends TestCase {

private function intervalFor(string $frequency): int {
$settings = $this->createMock(ArchivalSettingsReaderInterface::class);
$settings->method('getArchivalSettingsOnly')->willReturn(['reviewReminderFrequency' => $frequency]);
$container = $this->createMock(ContainerInterface::class);
$container->method('get')->with(ArchivalSettingsReaderInterface::class)->willReturn($settings);

$job = new ReviewReminderJob($this->createMock(ITimeFactory::class), $container);
return (int)(new \ReflectionProperty($job, 'interval'))->getValue($job);
}

public function testAZeroFrequencyFallsBackToAWeek(): void {
$this->assertSame(604800, $this->intervalFor('PT0S'));
}

public function testAPositiveFrequencyIsUsedAsIs(): void {
$this->assertSame(7200, $this->intervalFor('PT2H'));
}
}
133 changes: 133 additions & 0 deletions tests/Unit/BackgroundJob/StateHistoryRebuildJobBehaviourTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/**
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Test
* @package OCA\OpenRegister\Tests\Unit\BackgroundJob
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
* @version GIT: <git-id>
* @link https://OpenRegister.app
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Tests\Unit\BackgroundJob;

use OCA\OpenRegister\BackgroundJob\StateHistoryRebuildJob;
use OCA\OpenRegister\BackgroundJob\Support\StateHistoryRebuildSources;
use OCA\OpenRegister\Db\AuditTrail;
use OCA\OpenRegister\Db\AuditTrailMapperInterface;
use OCA\OpenRegister\Db\Schema;
use OCA\OpenRegister\Db\SchemaMapperInterface;
use OCA\OpenRegister\Service\History\StateHistoryRebuildInterface;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* The refusal branches of StateHistoryRebuildJob's per-object context and its
* batch-failure handling.
*/
class StateHistoryRebuildJobBehaviourTest extends TestCase {

private StateHistoryRebuildInterface&MockObject $rebuild;

private AuditTrailMapperInterface&MockObject $audit;

private SchemaMapperInterface&MockObject $schemas;

private IAppConfig&MockObject $appConfig;

private LoggerInterface&MockObject $logger;

protected function setUp(): void {
parent::setUp();
$this->rebuild = $this->createMock(StateHistoryRebuildInterface::class);
$this->audit = $this->createMock(AuditTrailMapperInterface::class);
$this->schemas = $this->createMock(SchemaMapperInterface::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->appConfig->method('getValueBool')->willReturn(true);
$this->appConfig->method('getValueString')->willReturn('');
}

private function runJob(): void {
$job = new StateHistoryRebuildJob(
$this->createMock(ITimeFactory::class),
new StateHistoryRebuildSources($this->rebuild, $this->audit, $this->schemas),
$this->appConfig,
$this->logger
);
(new \ReflectionMethod($job, 'run'))->invoke($job, null);
}

private function oneObjectWithSchema(): void {
$entry = new AuditTrail();
$entry->setSchema(7);
$entry->setRegisterUuid('reg-1');
$this->audit->method('findObjectUuidsAfter')->willReturn(['o-1']);
$this->audit->method('findForObjectByAction')->willReturn([$entry]);
}

public function testBatchFailureLeavesTheCursorAndWarns(): void {
$this->audit->method('findObjectUuidsAfter')->willThrowException(new \RuntimeException('db down'));
$this->appConfig->expects($this->never())->method('setValueString');
$this->logger->expects($this->once())->method('warning')->with(
'[StateHistoryRebuildJob] Batch failed, the cursor stands: {error}',
$this->callback(static fn (array $context): bool => $context['error'] === 'db down')
);

$this->runJob();
}

public function testUnfindableSchemaSkipsTheObjectButMovesTheCursor(): void {
$this->oneObjectWithSchema();
$this->schemas->method('find')->willThrowException(new \RuntimeException('gone'));
$this->rebuild->expects($this->never())->method('rebuildObject');
$this->appConfig->expects($this->once())->method('setValueString')
->with('openregister', StateHistoryRebuildJob::CURSOR, 'o-1');
$this->logger->expects($this->never())->method('warning');

$this->runJob();
}

public function testSchemaWithoutLifecycleAnnotationIsSkipped(): void {
$this->oneObjectWithSchema();
$schema = new Schema();
$schema->setConfiguration(['x-openregister-lifecycle' => 'status']);
$this->schemas->method('find')->willReturn($schema);
$this->rebuild->expects($this->never())->method('rebuildObject');
$this->appConfig->expects($this->once())->method('setValueString')
->with('openregister', StateHistoryRebuildJob::CURSOR, 'o-1');

$this->runJob();
}

public function testAnnotationWithoutFieldIsSkipped(): void {
$this->oneObjectWithSchema();
$schema = new Schema();
$schema->setConfiguration(['x-openregister-lifecycle' => ['states' => ['a', 'b']]]);
$this->schemas->method('find')->willReturn($schema);
$this->rebuild->expects($this->never())->method('rebuildObject');

$this->runJob();
}

public function testAnnotationPropertyKeyIsAcceptedAsField(): void {
$this->oneObjectWithSchema();
$schema = new Schema();
$schema->setSlug('cases');
$schema->setConfiguration(['x-openregister-lifecycle' => ['property' => 'phase']]);
$this->schemas->method('find')->willReturn($schema);
$this->rebuild->expects($this->once())->method('rebuildObject')
->with('o-1', 'phase', 'reg-1', 'cases')->willReturn(1);

$this->runJob();
}
}
Loading
Loading