Skip to content

Commit 054f8e5

Browse files
authored
Merge pull request #5535 from LibreSign/fix/make-possible-to-test-with-dates
fix: make possible to test with dates
2 parents 6d6b046 + 007a7b4 commit 054f8e5

2 files changed

Lines changed: 44 additions & 13 deletions

File tree

lib/Service/ReminderService.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace OCA\Libresign\Service;
1010

11+
use DateMalformedStringException;
1112
use DateTime;
1213
use OCA\Libresign\AppInfo\Application;
1314
use OCA\Libresign\BackgroundJob\Reminder;
@@ -17,6 +18,7 @@
1718
use OCP\BackgroundJob\IJobList;
1819
use OCP\IAppConfig;
1920
use OCP\IDateTimeZone;
21+
use Psr\Log\LoggerInterface;
2022

2123
class ReminderService {
2224
public function __construct(
@@ -26,6 +28,7 @@ public function __construct(
2628
protected ITimeFactory $time,
2729
protected SignRequestMapper $signRequestMapper,
2830
protected IdentifyMethodService $identifyMethodService,
31+
protected LoggerInterface $logger,
2932
) {
3033
}
3134

@@ -136,14 +139,22 @@ protected function scheduleJob(string $startTime): ?DateTime {
136139
protected function getStartTime(string $startTime): ?\DateTime {
137140
$timezone = $this->dateTimeZone->getTimeZone();
138141

139-
$dateTime = new \DateTime($startTime, $timezone);
140-
$dateTime->setTimezone(new \DateTimeZone('UTC'));
142+
$now = $this->time->getDateTime('now', new \DateTimeZone('UTC'));
143+
$dateTime = clone $now;
141144

142-
$now = new \DateTime('now', new \DateTimeZone('UTC'));
143-
if ($dateTime > $now) {
144-
return $dateTime;
145+
try {
146+
$time = new \DateTime($startTime, $timezone);
147+
} catch (DateMalformedStringException $e) {
148+
$this->logger->error('Failed to parse reminder send time: ' . $e->getMessage());
149+
return null;
150+
}
151+
// 'G' = 24-hour format hour (no leading zeros),
152+
// 'i' = minutes with leading zeros
153+
$dateTime->setTime((int)$time->format('G'), (int)$time->format('i'));
154+
$dateTime->setTimezone(new \DateTimeZone('UTC'));
155+
if ($dateTime <= $now) {
156+
$dateTime->modify('+1 day');
145157
}
146-
$dateTime->modify('+1 day');
147158

148159
return $dateTime;
149160
}

tests/php/Unit/Service/ReminderServiceTest.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@
1818
use OCP\Server;
1919
use PHPUnit\Framework\Attributes\DataProvider;
2020
use PHPUnit\Framework\MockObject\MockObject;
21+
use Psr\Log\LoggerInterface;
2122

2223
final class ReminderServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
2324
protected IJobList|MockObject $jobList;
2425
protected IAppConfig $appConfig;
2526
protected IDateTimeZone $dateTimeZone;
26-
protected ITimeFactory $time;
27+
protected ITimeFactory|MockObject $time;
2728
protected SignRequestMapper|MockObject $signRequestMapper;
2829
protected IdentifyMethodService|MockObject $identifyMethodService;
30+
protected LoggerInterface|MockObject $logger;
2931

3032
public function setUp(): void {
3133
$this->jobList = $this->createMock(IJobList::class);
3234
$this->appConfig = $this->getMockAppConfig();
3335
$this->dateTimeZone = Server::get(IDateTimeZone::class);
34-
$this->time = Server::get(ITimeFactory::class);
36+
$this->time = $this->createMock(ITimeFactory::class);
3537
$this->signRequestMapper = $this->createMock(SignRequestMapper::class);
3638
$this->identifyMethodService = $this->createMock(IdentifyMethodService::class);
39+
$this->logger = $this->createMock(LoggerInterface::class);
3740
}
3841

3942
private function getService(array $methods = []): ReminderService|MockObject {
@@ -46,6 +49,7 @@ private function getService(array $methods = []): ReminderService|MockObject {
4649
$this->time,
4750
$this->signRequestMapper,
4851
$this->identifyMethodService,
52+
$this->logger,
4953
])
5054
->onlyMethods($methods)
5155
->getMock();
@@ -57,6 +61,7 @@ private function getService(array $methods = []): ReminderService|MockObject {
5761
$this->time,
5862
$this->signRequestMapper,
5963
$this->identifyMethodService,
64+
$this->logger,
6065
);
6166
}
6267

@@ -105,7 +110,7 @@ public function testWillNotify(array $summarized, \DateTime $now, int $daysBefor
105110
}
106111

107112
public static function providerWillNotify(): array {
108-
$now = (new DateTime())->setTime(12, 0);
113+
$now = new DateTime('2025-10-09 12:00:00', new \DateTimeZone('UTC'));
109114

110115
return [
111116
'no notifications, should not send with all zero and null' => [
@@ -327,6 +332,11 @@ public function testSave(
327332
string $sendTimer,
328333
array $expected,
329334
): void {
335+
// Setup fixed time for consistent testing
336+
$fixedTime = new DateTime('2025-10-09 09:00:00', new \DateTimeZone('UTC'));
337+
$this->time->method('getDateTime')
338+
->willReturn($fixedTime);
339+
330340
$service = $this->getService();
331341
$actual = $service->save($daysBefore, $daysBetween, $max, $sendTimer);
332342
$this->assertEquals($expected, $actual);
@@ -347,7 +357,7 @@ public function testSave(
347357
}
348358

349359
public static function providerSave(): array {
350-
$now = (new DateTime());
360+
$now = (new DateTime('2025-10-09 09:00:00', new \DateTimeZone('UTC')));
351361
return [
352362
[
353363
'daysBefore' => 0, 'daysBetween' => 0, 'max' => 0, 'sendTimer' => '',
@@ -415,7 +425,7 @@ public static function providerSave(): array {
415425
'days_before' => 1,
416426
'days_between' => 1,
417427
'max' => 1,
418-
'next_run' => (clone $now)->modify('+1 day')->setTime(10, 0),
428+
'next_run' => (clone $now)->setTime(10, 0),
419429
'send_timer' => '10:00',
420430
],
421431
],
@@ -425,17 +435,27 @@ public static function providerSave(): array {
425435
'days_before' => 1,
426436
'days_between' => 1,
427437
'max' => 1,
428-
'next_run' => (clone $now)->modify('+1 day')->setTime(10, 0),
438+
'next_run' => (clone $now)->setTime(10, 0),
429439
'send_timer' => '10:00',
430440
],
431441
],
442+
[
443+
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '08:05',
444+
'expected' => [
445+
'days_before' => 1,
446+
'days_between' => 1,
447+
'max' => 1,
448+
'next_run' => (clone $now)->modify('+1 day')->setTime(8, 5),
449+
'send_timer' => '08:05',
450+
],
451+
],
432452
[
433453
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '11:05',
434454
'expected' => [
435455
'days_before' => 1,
436456
'days_between' => 1,
437457
'max' => 1,
438-
'next_run' => (clone $now)->modify('+1 day')->setTime(11, 5),
458+
'next_run' => (clone $now)->setTime(11, 5),
439459
'send_timer' => '11:05',
440460
],
441461
],

0 commit comments

Comments
 (0)