Compare commits

...

4 Commits

Author SHA1 Message Date
dc4e485553 Add changie 2025-10-15 11:02:21 +02:00
0ca1f46c84 Update .junie/guidelines.md to refine testing guidelines
- Removed instructions for running tests on specific bundles.
- Added recommendation to test only specific files instead of running all tests or the full suite.
2025-10-15 11:00:42 +02:00
6a764353f9 Add tests for DailyNotificationDigestCronJob and improve robustness of execution state handling
- Introduced unit tests for `DailyNotificationDigestCronJob` to ensure correct dispatching and state handling.
- Improved handling of invalid execution state by adding fallback to the previous day.
- Updated date formatting in the returned state to use `DateTimeInterface::ATOM` for consistency.
2025-10-15 11:00:31 +02:00
9c304ed6d3 Add i18n support and improve type safety for WorkflowAttachment components
- Replaced hardcoded strings with translation keys for internationalization.
- Introduced new utility types and functions to validate attachment structures.
- Updated components to utilize computed properties for cleaner reactivity.
- Extended support for stored objects in `GenericDoc` types and validated context.
2025-10-13 16:03:39 +02:00
10 changed files with 224 additions and 23 deletions

View File

@@ -0,0 +1,6 @@
kind: Fixed
body: Fix the execution of daily cronjob notification, when the previous last execution storage was invalid
time: 2025-10-15T11:02:02.704326098+02:00
custom:
Issue: "448"
SchemaChange: No schema change

View File

@@ -240,9 +240,6 @@ The tests are run from the project's root (not from the bundle's root).
# Run all tests
vendor/bin/phpunit
# Run tests for a specific bundle
vendor/bin/phpunit --testsuite NameBundle
# Run a specific test file
vendor/bin/phpunit path/to/TestFile.php
@@ -250,6 +247,9 @@ vendor/bin/phpunit path/to/TestFile.php
vendor/bin/phpunit --filter methodName path/to/TestFile.php
```
When writing tests, only test specific files. Do not run all tests or the full
test suite.
#### Test Structure
Tests are organized by bundle and follow the same structure as the bundle itself:

View File

@@ -36,6 +36,18 @@ export interface GenericDocForAccompanyingPeriod extends GenericDoc {
context: "accompanying-period";
}
export function isGenericDocForAccompanyingPeriod(
doc: GenericDoc,
): doc is GenericDocForAccompanyingPeriod {
return doc.context === "accompanying-period";
}
export function isGenericDocWithStoredObject(
doc: GenericDoc,
): doc is GenericDoc & { storedObject: StoredObject } {
return doc.storedObject !== null;
}
interface BaseMetadataWithHtml extends BaseMetadata {
html: string;
}
@@ -44,28 +56,33 @@ export interface GenericDocForAccompanyingCourseDocument
extends GenericDocForAccompanyingPeriod {
key: "accompanying_course_document";
metadata: BaseMetadataWithHtml;
storedObject: StoredObject;
}
export interface GenericDocForAccompanyingCourseActivityDocument
extends GenericDocForAccompanyingPeriod {
key: "accompanying_course_activity_document";
metadata: BaseMetadataWithHtml;
storedObject: StoredObject;
}
export interface GenericDocForAccompanyingCourseCalendarDocument
extends GenericDocForAccompanyingPeriod {
key: "accompanying_course_calendar_document";
metadata: BaseMetadataWithHtml;
storedObject: StoredObject;
}
export interface GenericDocForAccompanyingCoursePersonDocument
extends GenericDocForAccompanyingPeriod {
key: "person_document";
metadata: BaseMetadataWithHtml;
storedObject: StoredObject;
}
export interface GenericDocForAccompanyingCourseWorkEvaluationDocument
extends GenericDocForAccompanyingPeriod {
key: "accompanying_period_work_evaluation_document";
metadata: BaseMetadataWithHtml;
storedObject: StoredObject;
}

View File

@@ -53,11 +53,16 @@ readonly class DailyNotificationDigestCronjob implements CronJobInterface
public function run(array $lastExecutionData): ?array
{
$now = $this->clock->now();
if (isset($lastExecutionData['last_execution'])) {
$lastExecution = \DateTimeImmutable::createFromFormat(
\DateTimeImmutable::ATOM,
$lastExecutionData['last_execution']
);
if (false === $lastExecution) {
$lastExecution = $now->sub(new \DateInterval('P1D'));
}
} else {
$lastExecution = $now->sub(new \DateInterval('P1D'));
}
@@ -96,7 +101,7 @@ readonly class DailyNotificationDigestCronjob implements CronJobInterface
]);
return [
'last_execution' => $now->format('Y-m-d-H:i:s.u e'),
'last_execution' => $now->format(\DateTimeInterface::ATOM),
];
}
}

View File

@@ -1,4 +1,7 @@
import { GenericDoc } from "ChillDocStoreAssets/types/generic_doc";
import {
GenericDoc,
isGenericDocWithStoredObject,
} from "ChillDocStoreAssets/types/generic_doc";
import { StoredObject, StoredObjectStatus } from "ChillDocStoreAssets/types";
import { Person } from "../../../ChillPersonBundle/Resources/public/types";
@@ -203,6 +206,25 @@ export interface WorkflowAttachment {
genericDoc: null | GenericDoc;
}
export type AttachmentWithDocAndStored = WorkflowAttachment & {
genericDoc: GenericDoc & { storedObject: StoredObject };
};
export function isAttachmentWithDocAndStored(
a: WorkflowAttachment,
): a is AttachmentWithDocAndStored {
return (
isWorkflowAttachmentWithGenericDoc(a) &&
isGenericDocWithStoredObject(a.genericDoc)
);
}
export function isWorkflowAttachmentWithGenericDoc(
attachment: WorkflowAttachment,
): attachment is WorkflowAttachment & { genericDoc: GenericDoc } {
return attachment.genericDoc !== null;
}
export interface Workflow {
name: string;
text: string;

View File

@@ -6,6 +6,7 @@ import { GenericDocForAccompanyingPeriod } from "ChillDocStoreAssets/types/gener
import AttachmentList from "ChillMainAssets/vuejs/WorkflowAttachment/Component/AttachmentList.vue";
import { GenericDoc } from "ChillDocStoreAssets/types";
import { fetchWorkflow } from "ChillMainAssets/lib/workflow/api";
import { trans, WORKFLOW_ATTACHMENTS_ADD_AN_ATTACHMENT } from "translator";
interface AppConfig {
workflowId: number;
@@ -83,7 +84,7 @@ const canEditAttachement = computed<boolean>(() => {
<ul v-if="canEditAttachement" class="record_actions">
<li>
<button type="button" class="btn btn-create" @click="openModal">
Ajouter une pièce jointe
{{ trans(WORKFLOW_ATTACHMENTS_ADD_AN_ATTACHMENT) }}
</button>
</li>
</ul>

View File

@@ -1,7 +1,14 @@
<script setup lang="ts">
import { EntityWorkflow, WorkflowAttachment } from "ChillMainAssets/types";
import {
AttachmentWithDocAndStored,
EntityWorkflow,
isAttachmentWithDocAndStored,
WorkflowAttachment,
} from "ChillMainAssets/types";
import GenericDocItemBox from "ChillMainAssets/vuejs/WorkflowAttachment/Component/GenericDocItemBox.vue";
import DocumentActionButtonsGroup from "ChillDocStoreAssets/vuejs/DocumentActionButtonsGroup.vue";
import { computed } from "vue";
import { trans, WORKFLOW_ATTACHMENTS_NO_ATTACHMENT } from "translator";
interface AttachmentListProps {
attachments: WorkflowAttachment[];
@@ -14,35 +21,43 @@ const emit = defineEmits<{
}>();
const props = defineProps<AttachmentListProps>();
const notNullAttachments = computed<AttachmentWithDocAndStored[]>(() =>
props.attachments.filter(
(a: WorkflowAttachment): a is AttachmentWithDocAndStored =>
isAttachmentWithDocAndStored(a),
),
);
const canRemove = computed<boolean>((): boolean => {
if (null === props.workflow) {
return false;
}
return props.workflow._permissions.CHILL_MAIN_WORKFLOW_ATTACHMENT_EDIT;
});
</script>
<template>
<p
v-if="props.attachments.length === 0"
v-if="notNullAttachments.length === 0"
class="chill-no-data-statement text-center"
>
Aucune pièce jointe
{{ trans(WORKFLOW_ATTACHMENTS_NO_ATTACHMENT) }}
</p>
<!-- TODO translate -->
<div else class="flex-table">
<div v-for="a in props.attachments" :key="a.id" class="item-bloc">
<div v-else class="flex-table">
<div v-for="a in notNullAttachments" :key="a.id" class="item-bloc">
<generic-doc-item-box
v-if="a.genericDoc !== null"
:generic-doc="a.genericDoc"
></generic-doc-item-box>
<div class="item-row separator">
<ul class="record_actions">
<li v-if="a.genericDoc?.storedObject !== null">
<li>
<document-action-buttons-group
:stored-object="a.genericDoc.storedObject"
></document-action-buttons-group>
</li>
<li
v-if="
!workflow?._permissions
.CHILL_MAIN_WORKFLOW_ATTACHMENT_EDIT
"
>
<li v-if="canRemove">
<button
type="button"
class="btn btn-delete"

View File

@@ -1,8 +1,8 @@
<script setup lang="ts">
import { GenericDocForAccompanyingPeriod } from "ChillDocStoreAssets/types/generic_doc";
import { GenericDoc } from "ChillDocStoreAssets/types/generic_doc";
interface GenericDocItemBoxProps {
genericDoc: GenericDocForAccompanyingPeriod;
genericDoc: GenericDoc;
}
const props = defineProps<GenericDocItemBoxProps>();

View File

@@ -12,16 +12,21 @@ declare(strict_types=1);
namespace Chill\MainBundle\Tests\Notification\Email;
use Chill\MainBundle\Notification\Email\DailyNotificationDigestCronjob;
use Chill\MainBundle\Notification\Email\NotificationEmailMessages\ScheduleDailyNotificationDigestMessage;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Statement;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
/**
* @internal
*
* @coversNothing
* @covers \DailyNotificationDigestCronjob
*/
class DailyNotificationDigestCronJobTest extends TestCase
{
@@ -30,6 +35,7 @@ class DailyNotificationDigestCronJobTest extends TestCase
private MessageBusInterface $messageBus;
private LoggerInterface $logger;
private DailyNotificationDigestCronjob $cronjob;
private \DateTimeImmutable $firstNow;
protected function setUp(): void
{
@@ -38,6 +44,8 @@ class DailyNotificationDigestCronJobTest extends TestCase
$this->messageBus = $this->createMock(MessageBusInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->firstNow = new \DateTimeImmutable('2024-01-02T07:15:00+00:00');
$this->cronjob = new DailyNotificationDigestCronjob(
$this->clock,
$this->connection,
@@ -78,4 +86,129 @@ class DailyNotificationDigestCronJobTest extends TestCase
'hour 23 - should not run' => [23, false],
];
}
public function testRunFirstExecutionReturnsStateAndDispatches(): array
{
// Use MockClock for deterministic time
$firstNow = $this->firstNow;
$clock = new MockClock($firstNow);
// Mock DBAL statement/result
$statement = $this->createMock(Statement::class);
$result = $this->createMock(Result::class);
$this->connection->method('prepare')->willReturn($statement);
$statement->method('bindValue')->willReturnSelf();
$statement->method('executeQuery')->willReturn($result);
$rows = [
['user_id' => 10],
['user_id' => 42],
];
$result->method('fetchAllAssociative')->willReturn($rows);
$dispatched = [];
$this->messageBus->method('dispatch')->willReturnCallback(function ($message) use (&$dispatched) {
$dispatched[] = $message;
return new Envelope($message);
});
$cron = new DailyNotificationDigestCronjob($clock, $this->connection, $this->messageBus, $this->logger);
$state = $cron->run([]);
// Assert dispatch count and message contents
self::assertCount(2, $dispatched);
$expectedLast = $firstNow->sub(new \DateInterval('P1D'));
foreach ($dispatched as $i => $msg) {
self::assertInstanceOf(ScheduleDailyNotificationDigestMessage::class, $msg);
self::assertTrue(in_array($msg->getUserId(), [10, 42], true));
self::assertEquals($firstNow, $msg->getCurrentDateTime(), 'compare the current date');
self::assertEquals($expectedLast, $msg->getLastExecutionDateTime(), 'compare the last execution date');
}
// Assert returned state
self::assertIsArray($state);
self::assertArrayHasKey('last_execution', $state);
self::assertSame($firstNow->format(\DateTimeInterface::ATOM), $state['last_execution']);
return $state;
}
/**
* @depends testRunFirstExecutionReturnsStateAndDispatches
*/
public function testRunSecondExecutionUsesPreviousState(array $previousState): void
{
$firstNow = $this->firstNow;
$secondNow = $firstNow->add(new \DateInterval('P1D'));
$clock = new MockClock($secondNow);
// Mock DBAL for a single user this time
$statement = $this->createMock(Statement::class);
$result = $this->createMock(Result::class);
$this->connection->method('prepare')->willReturn($statement);
$statement->method('bindValue')->willReturnSelf();
$statement->method('executeQuery')->willReturn($result);
$rows = [
['user_id' => 7],
];
$result->method('fetchAllAssociative')->willReturn($rows);
$captured = [];
$this->messageBus->method('dispatch')->willReturnCallback(function ($message) use (&$captured) {
$captured[] = $message;
return new Envelope($message);
});
$cron = new DailyNotificationDigestCronjob($clock, $this->connection, $this->messageBus, $this->logger);
$cron->run($previousState);
self::assertCount(1, $captured);
$msg = $captured[0];
self::assertInstanceOf(ScheduleDailyNotificationDigestMessage::class, $msg);
self::assertEquals(7, $msg->getUserId());
self::assertEquals($secondNow, $msg->getCurrentDateTime(), 'compare the current date');
self::assertEquals($firstNow, $msg->getLastExecutionDateTime(), 'compare the last execution date');
}
public function testRunWithInvalidExecutionState(): void
{
$firstNow = $this->firstNow;
$previousExpected = $firstNow->sub(new \DateInterval('P1D'));
$clock = new MockClock($firstNow);
// Mock DBAL for a single user this time
$statement = $this->createMock(Statement::class);
$result = $this->createMock(Result::class);
$this->connection->method('prepare')->willReturn($statement);
$statement->method('bindValue')->willReturnSelf();
$statement->method('executeQuery')->willReturn($result);
$rows = [
['user_id' => 7],
];
$result->method('fetchAllAssociative')->willReturn($rows);
$captured = [];
$this->messageBus->method('dispatch')->willReturnCallback(function ($message) use (&$captured) {
$captured[] = $message;
return new Envelope($message);
});
$cron = new DailyNotificationDigestCronjob($clock, $this->connection, $this->messageBus, $this->logger);
$cron->run(['last_execution' => 'invalid data']);
self::assertCount(1, $captured);
$msg = $captured[0];
self::assertInstanceOf(ScheduleDailyNotificationDigestMessage::class, $msg);
self::assertEquals(7, $msg->getUserId());
self::assertEquals($firstNow, $msg->getCurrentDateTime(), 'compare the current date');
self::assertEquals($previousExpected, $msg->getLastExecutionDateTime(), 'compare the last execution date');
}
}

View File

@@ -670,6 +670,8 @@ workflow:
attachments:
title: Pièces jointes
no_attachment: Aucune pièce jointe
Add_an_attachment: Ajouter une pièce jointe
wait:
title: En attente de traitement