-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathReminderServiceTest.php
More file actions
464 lines (447 loc) · 14.4 KB
/
ReminderServiceTest.php
File metadata and controls
464 lines (447 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Service\IdentifyMethodService;
use OCA\Libresign\Service\ReminderService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IAppConfig;
use OCP\IDateTimeZone;
use OCP\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
final class ReminderServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
protected IJobList|MockObject $jobList;
protected IAppConfig $appConfig;
protected IDateTimeZone $dateTimeZone;
protected ITimeFactory|MockObject $time;
protected SignRequestMapper|MockObject $signRequestMapper;
protected IdentifyMethodService|MockObject $identifyMethodService;
protected LoggerInterface|MockObject $logger;
public function setUp(): void {
$this->jobList = $this->createMock(IJobList::class);
$this->appConfig = $this->getMockAppConfigWithReset();
$this->dateTimeZone = Server::get(IDateTimeZone::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->signRequestMapper = $this->createMock(SignRequestMapper::class);
$this->identifyMethodService = $this->createMock(IdentifyMethodService::class);
$this->logger = $this->createMock(LoggerInterface::class);
}
private function getService(array $methods = []): ReminderService|MockObject {
if ($methods) {
return $this->getMockBuilder(ReminderService::class)
->setConstructorArgs([
$this->jobList,
$this->appConfig,
$this->dateTimeZone,
$this->time,
$this->signRequestMapper,
$this->identifyMethodService,
$this->logger,
])
->onlyMethods($methods)
->getMock();
}
return new ReminderService(
$this->jobList,
$this->appConfig,
$this->dateTimeZone,
$this->time,
$this->signRequestMapper,
$this->identifyMethodService,
$this->logger,
);
}
#[DataProvider('providerSummarizeNotifications')]
public function testSummarizeNotifications(array $notifications, array $expected): void {
$service = $this->getService();
$actual = self::invokePrivate($service, 'getNotificationsSummarized', [$notifications]);
$this->assertEquals($expected, $actual);
}
public static function providerSummarizeNotifications(): array {
$now = (new DateTime())->setTime(12, 0);
return [
'empty' => [[], ['first' => null, 'last' => null, 'total' => 0]],
'only one' => [
[['date' => (clone $now)->getTimestamp(), 'method' => 'activity']],
['first' => (clone $now)->setTime(0, 0), 'last' => (clone $now)->setTime(0, 0), 'total' => 1]
],
'same day mixed' => [
[
['date' => (clone $now)->getTimestamp(), 'method' => 'activity'],
['date' => (clone $now)->modify('+ 1 hour')->getTimestamp(), 'method' => 'notify'],
['date' => (clone $now)->modify('- 1 hour')->getTimestamp(), 'method' => 'mail'],
],
['first' => (clone $now)->setTime(0, 0), 'last' => (clone $now)->setTime(0, 0), 'total' => 1]
],
'two days mixed' => [
[
['date' => (clone $now)->getTimestamp(), 'method' => 'activity'],
['date' => (clone $now)->modify('+ 1 hour')->getTimestamp(), 'method' => 'notify'],
['date' => (clone $now)->modify('- 1 hour')->getTimestamp(), 'method' => 'mail'],
['date' => (clone $now)->modify('-2 days')->getTimestamp(), 'method' => 'activity'],
['date' => (clone $now)->modify('-2 days')->modify('+ 1 hour')->getTimestamp(), 'method' => 'notify'],
['date' => (clone $now)->modify('-2 days')->modify('- 1 hour')->getTimestamp(), 'method' => 'mail'],
],
['first' => (clone $now)->modify('-2 days')->setTime(0, 0), 'last' => (clone $now)->setTime(0, 0), 'total' => 2]
],
];
}
#[DataProvider('providerWillNotify')]
public function testWillNotify(array $summarized, \DateTime $now, int $daysBefore, int $daysBetween, int $max, bool $expected): void {
$service = $this->getService();
$actual = self::invokePrivate($service, 'willNotify', [$summarized, $now, $daysBefore, $daysBetween, $max]);
$this->assertEquals($expected, $actual);
}
public static function providerWillNotify(): array {
$now = new DateTime('2025-10-09 12:00:00', new \DateTimeZone('UTC'));
return [
'no notifications, should not send with all zero and null' => [
[
'first' => null,
'last' => null,
'total' => 0,
],
'now' => $now, 'daysBefore' => 0, 'daysBetween' => 0, 'max' => 0, false,
],
'no notifications, should not send with daysBetween === 0' => [
[
'first' => null,
'last' => null,
'total' => 0,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 0, 'max' => 0, false,
],
'no notifications, should not send with daysBetween > 0' => [
[
'first' => null,
'last' => null,
'total' => 0,
],
'now' => $now, 'daysBefore' => 0, 'daysBetween' => 1, 'max' => 0, false,
],
'no notifications, should not send with daysBefore and daysBetween > 0' => [
[
'first' => null,
'last' => null,
'total' => 0,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 0, false,
],
'no notification, scheduled for today, between = 0' => [
[
'first' => (clone $now)->setTime(0, 0),
'last' => (clone $now)->setTime(0, 0),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 0, 'max' => 5, false,
],
'no notification, scheduled for today, between = 1' => [
[
'first' => (clone $now)->setTime(0, 0),
'last' => (clone $now)->setTime(0, 0),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, false,
],
'no notification, scheduled for yesterday, between = 0' => [
[
'first' => (clone $now)->modify('-1 day')->setTime(0, 0),
'last' => (clone $now)->modify('-1 day')->setTime(0, 0),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 0, 'max' => 5, false,
],
'one notification, scheduled for yesterday, between = 1' => [
[
'first' => (clone $now)->modify('-1 day')->setTime(0, 0),
'last' => (clone $now)->modify('-1 day')->setTime(0, 0),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, true,
],
'one notification, should send' => [
[
'first' => (clone $now)->modify('-2 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, true,
],
'one notification, should not send with daysBefore <= 0' => [
[
'first' => (clone $now)->modify('-1 day'),
'last' => (clone $now)->modify('-1 day'),
'total' => 1,
],
'now' => $now, 'daysBefore' => 0, 'daysBetween' => 1, 'max' => 5, false,
],
'two notifications, should send with between === 1 and last === 2' => [
[
'first' => (clone $now)->modify('-3 days'),
'last' => (clone $now)->modify('-2 day'),
'total' => 2,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, true,
],
'two notifications, should not send with max limit reached' => [
[
'first' => (clone $now)->modify('-3 days'),
'last' => (clone $now)->modify('-2 day'),
'total' => 5,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, false,
],
'two notifications, should send without max limit' => [
[
'first' => (clone $now)->modify('-3 days'),
'last' => (clone $now)->modify('-2 day'),
'total' => 5,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 0, true,
],
'two notifications, should not send with daysBetween <= 0' => [
[
'first' => (clone $now)->modify('-2 days'),
'last' => (clone $now)->modify('-1 day'),
'total' => 2,
],
'now' => $now, 'daysBefore' => 0, 'daysBetween' => 0, 'max' => 5, false,
],
'one notification, exact daysBefore limit, should notify' => [
[
'first' => (clone $now)->modify('-1 day')->setTime(0, 0),
'last' => (clone $now)->modify('-1 day')->setTime(0, 0),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, true,
],
'two notifications, exact daysBetween limit, should notify' => [
[
'first' => (clone $now)->modify('-3 days'),
'last' => (clone $now)->modify('-2 days')->setTime(0, 0),
'total' => 2,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 2, 'max' => 5, true,
],
'no notifications, valid config but daysBetween = 0' => [
[
'first' => null,
'last' => null,
'total' => 0,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 0, 'max' => 5, false,
],
'inconsistent data: total > 0 but null dates' => [
[
'first' => null,
'last' => null,
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, false,
],
'max = 0 means no limit, should send with valid config' => [
[
'first' => (clone $now)->modify('-2 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 0, true,
],
'max = 0, high total count, should still send' => [
[
'first' => (clone $now)->modify('-10 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 100,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 0, true,
],
'total exceeds max, should not send' => [
[
'first' => (clone $now)->modify('-5 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 6,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, false,
],
'notification today, should not send' => [
[
'first' => (clone $now)->setTime(0, 0),
'last' => (clone $now)->setTime(0, 0),
'total' => 1,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => 5, false,
],
'multiple notifications, insufficient daysBetween' => [
[
'first' => (clone $now)->modify('-5 days'),
'last' => (clone $now)->setTime(0, 0),
'total' => 3,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 2, 'max' => 5, false,
],
'negative daysBefore, should not send' => [
[
'first' => (clone $now)->modify('-2 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 1,
],
'now' => $now, 'daysBefore' => -1, 'daysBetween' => 1, 'max' => 5, false,
],
'negative daysBetween, should not send' => [
[
'first' => (clone $now)->modify('-5 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 2,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => -1, 'max' => 5, false,
],
'negative max acts as no limit, should send' => [
[
'first' => (clone $now)->modify('-3 days'),
'last' => (clone $now)->modify('-2 days'),
'total' => 10,
],
'now' => $now, 'daysBefore' => 1, 'daysBetween' => 1, 'max' => -1, true,
],
];
}
#[DataProvider('providerSave')]
public function testSave(
int $daysBefore,
int $daysBetween,
int $max,
string $sendTimer,
array $expected,
): void {
// Setup fixed time for consistent testing
$fixedTime = new DateTime('2025-10-09 09:00:00', new \DateTimeZone('UTC'));
$this->time->method('getDateTime')
->willReturn($fixedTime);
$service = $this->getService();
$actual = $service->save($daysBefore, $daysBetween, $max, $sendTimer);
$this->assertEquals($expected, $actual);
$keys = [
'days_before',
'days_between',
'max',
];
foreach ($keys as $key) {
$actualConfig = $this->appConfig->getValueInt(Application::APP_ID, 'reminder_' . $key, $actual[$key]);
$this->assertEquals($actual[$key], $actualConfig);
}
$actualConfig = $this->appConfig->getValueString(Application::APP_ID, 'reminder_send_timer', $actual['send_timer']);
$this->assertEquals($actual['send_timer'], $actualConfig);
}
public static function providerSave(): array {
$now = (new DateTime('2025-10-09 09:00:00', new \DateTimeZone('UTC')));
return [
[
'daysBefore' => 0, 'daysBetween' => 0, 'max' => 0, 'sendTimer' => '',
'expected' => [
'days_before' => 0,
'days_between' => 0,
'max' => 0,
'next_run' => null,
'send_timer' => '',
],
],
[
'daysBefore' => 0, 'daysBetween' => 0, 'max' => 1, 'sendTimer' => '',
'expected' => [
'days_before' => 0,
'days_between' => 0,
'max' => 0,
'next_run' => null,
'send_timer' => '',
],
],
[
'daysBefore' => 0, 'daysBetween' => 1, 'max' => 0, 'sendTimer' => '',
'expected' => [
'days_before' => 0,
'days_between' => 0,
'max' => 0,
'next_run' => null,
'send_timer' => '',
],
],
[
'daysBefore' => 0, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '',
'expected' => [
'days_before' => 0,
'days_between' => 0,
'max' => 0,
'next_run' => null,
'send_timer' => '',
],
],
[
'daysBefore' => 1, 'daysBetween' => 0, 'max' => 0, 'sendTimer' => '',
'expected' => [
'days_before' => 0,
'days_between' => 0,
'max' => 0,
'next_run' => null,
'send_timer' => '',
],
],
[
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 0, 'sendTimer' => '',
'expected' => [
'days_before' => 0,
'days_between' => 0,
'max' => 0,
'next_run' => null,
'send_timer' => '',
],
],
[
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '',
'expected' => [
'days_before' => 1,
'days_between' => 1,
'max' => 1,
'next_run' => (clone $now)->setTime(10, 0),
'send_timer' => '10:00',
],
],
[
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '11:05:00', // Invalid timer, need to be HH:mm
'expected' => [
'days_before' => 1,
'days_between' => 1,
'max' => 1,
'next_run' => (clone $now)->setTime(10, 0),
'send_timer' => '10:00',
],
],
[
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '08:05',
'expected' => [
'days_before' => 1,
'days_between' => 1,
'max' => 1,
'next_run' => (clone $now)->modify('+1 day')->setTime(8, 5),
'send_timer' => '08:05',
],
],
[
'daysBefore' => 1, 'daysBetween' => 1, 'max' => 1, 'sendTimer' => '11:05',
'expected' => [
'days_before' => 1,
'days_between' => 1,
'max' => 1,
'next_run' => (clone $now)->setTime(11, 5),
'send_timer' => '11:05',
],
],
];
}
}