fix(task): task creation broke on save - task_kind could reach the database as null

A new Task's taskKind property defaulted to null (the 0.18.x task rework), while
the task_kind column is NOT NULL: Doctrine writes every column explicitly, so
the column's own default never applied and any save whose form field did not
map failed with "1048 Column 'task_kind' cannot be null".

- Task::$taskKind now defaults to TaskKind::Task, like $status: the property
  is never null anymore and the getter is straight
- setTaskKind()/setStatus() accept null as "keep the current kind/status", so
  a missing form field can neither throw at mapping time nor wipe the value
- TaskType defaults an absent or empty taskKind to a regular task on PRE_SUBMIT
- regression tests: Tests/Unit/Entity/TaskTest.php
This commit is contained in:
alexandre_gain_mt3s committed 2026-11-21 14:31:49 +01:00
1 parent d6b3488cad
commit 603e564037
3 files changed
+110 -14

No files matched your search

+27 -14
View File
@@ -66,7 +66,12 @@ class Task extends FormEntity implements DateRangeInterface
private TaskStatus $status = TaskStatus::New;
private ?TaskKind $taskKind = null;
/**
* Defaults to a regular task, like $status: Doctrine writes every column explicitly, so
* the database column's own default never applies. A save whose form field didn't map
* (a POST missing the kind) must never hit the NOT NULL column with null.
*/
private TaskKind $taskKind = TaskKind::Task;
private ?string $definitionOfDone = null;
@@ -291,32 +296,40 @@ class Task extends FormEntity implements DateRangeInterface
return $this->status;
}
public function setStatus(TaskStatus $status): static
public function setStatus(?TaskStatus $status): static
{
if ($status !== $this->status) {
// isChanged() can't log enums (they aren't stringable), so log the backing values.
$this->addChange('status', [$this->status->value, $status->value]);
$this->status = $status;
// Null (a status field missing from the submitted form) never wipes the status: like
// the kind, it keeps its default (New) or whatever the task already has.
if (null === $status || $status === $this->status) {
return $this;
}
// isChanged() can't log enums (they aren't stringable), so log the backing values.
$this->addChange('status', [$this->status->value, $status->value]);
$this->status = $status;
return $this;
}
public function getTaskKind(): TaskKind
{
// Never null once stored (the column defaults to 'task'); a new entity reads as a plain task.
return $this->taskKind ?? TaskKind::Task;
// Never null: the property starts as a regular task and the column is NOT NULL.
return $this->taskKind;
}
public function setTaskKind(TaskKind $taskKind): static
public function setTaskKind(?TaskKind $taskKind): static
{
if ($taskKind !== $this->taskKind) {
// isChanged() can't log enums (they aren't stringable), so log the backing values.
$this->addChange('taskKind', [$this->taskKind?->value, $taskKind->value]);
$taskKind->enslaveDates($this);
$this->taskKind = $taskKind;
// Null (a kind field missing from the submitted form) never wipes the kind: a new
// task keeps its regular-task default, an existing task keeps whatever it already has.
if (null === $taskKind || $taskKind === $this->taskKind) {
return $this;
}
// isChanged() can't log enums (they aren't stringable), so log the backing values.
$this->addChange('taskKind', [$this->taskKind->value, $taskKind->value]);
$taskKind->enslaveDates($this);
$this->taskKind = $taskKind;
return $this;
}
+18
View File
@@ -22,6 +22,8 @@ use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
@@ -59,6 +61,22 @@ class TaskType extends AbstractType
'choice_label' => fn (TaskKind $k): string => $k->getLabelKey(),
]);
// A submitted form always carries a kind: an absent or empty taskKind (a browser
// POST that lost the select, or an ajax save) defaults to a regular task, so the
// NOT NULL column can never receive null (see Task::$taskKind).
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
$data = $event->getData();
if (!is_array($data)) {
return;
}
if (!isset($data['taskKind']) || '' === $data['taskKind']) {
$data['taskKind'] = TaskKind::Task->value;
$event->setData($data);
}
});
$builder->add('type', ChoiceType::class, [
'label' => 'mt3s.marketing_program.task.field.type',
'label_attr' => ['class' => 'control-label'],
+65
View File
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Tests\Unit\Entity;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\TaskKind;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\TaskStatus;
use PHPUnit\Framework\TestCase;
/**
* The task kind must never be null: the task_kind column is NOT NULL and Doctrine writes
* every column explicitly, so the database default never applies (0.18.1 regression).
*/
final class TaskTest extends TestCase
{
public function testNewTaskDefaultsToRegularKind(): void
{
$task = new Task();
self::assertSame(TaskKind::Task, $task->getTaskKind());
}
public function testNullKindNeverWipesTheKind(): void
{
$task = new Task();
$task->setTaskKind(TaskKind::Milestone);
$task->setTaskKind(null);
self::assertSame(TaskKind::Milestone, $task->getTaskKind());
}
public function testSettingTheSameKindChangesNothing(): void
{
$task = new Task();
$task->setTaskKind(TaskKind::Task);
self::assertSame(TaskKind::Task, $task->getTaskKind());
}
public function testNullStatusNeverWipesTheStatus(): void
{
$task = new Task();
$task->setStatus(TaskStatus::Done);
$task->setStatus(null);
self::assertSame(TaskStatus::Done, $task->getStatus());
}
public function testMilestoneKindEnslavesTheEndDate(): void
{
$task = new Task();
$start = new \DateTime('2026-01-10');
$task->setStartDate($start);
$task->setEndDate(new \DateTime('2027-05-20'));
$task->setTaskKind(TaskKind::Milestone);
self::assertSame('2026-01-10', $task->getEndDate()->format('Y-m-d'));
}
}