feat(call-script): add the Call Script component (0.19.0)
A new Mautic component, listed under Components > Call Scripts (/s/call-scripts),
built like the other domains on AbstractStandardFormController/FormModel.
- CallScript (mt3s_call_scripts): name, description, category, owner, status,
the publish window shown as start/end date and time, the HTML script with
dynamic tokens kept as typed (html cleaning mask), the calling window
(weekdays, hours, sentinel in the contact's or a fixed timezone) and the retry
policy (max attempts, delay, backoff: none/linear/exponential)
- CallScriptOutcome (mt3s_call_script_outcomes, ON DELETE CASCADE): label,
data type (checkbox/radio/free text/dropdown), options JSON ({value,label})
and position; nested collections reuse the program-collection JS
- validators: ValidCallingWindow, ValidCallScriptOutcomeOptions, and an atPath
option on EndDateAfterStartDate so its violation lands on publishDown
- config: routes, the Components menu item (parent mautic.core.components),
call_script category type, model alias, call_scripts permissions, version 0.19.0
- migration Version_0_19_0 creates both tables and their FKs immediately
(users/categories SET NULL, outcomes CASCADE), idempotent
- translations en_US/fr_FR, unit tests, readme/AGENTS documentation
This commit is contained in:
1 parent
88256ca345
commit
4fb36eebfc
33 files changed
+2537
-6
No files matched your search
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\MT3SMarketingProgramBundle\Tests\Unit\Entity;
|
||||
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScript;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptOutcome;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptOutcomeInputType;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptRetryBackoff;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptTimezoneMode;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CallScriptTest extends TestCase
|
||||
{
|
||||
public function testNewScriptDefaults(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
|
||||
self::assertSame(CallScriptTimezoneMode::Contact, $script->getCallingTimezoneMode());
|
||||
self::assertSame(3, $script->getRetryMaxAttempts());
|
||||
self::assertSame(30, $script->getRetryDelayMinutes());
|
||||
self::assertSame(CallScriptRetryBackoff::None, $script->getRetryBackoff());
|
||||
self::assertSame([], $script->getCallingDays());
|
||||
self::assertCount(0, $script->getOutcomes());
|
||||
}
|
||||
|
||||
public function testCallingDaysAreFilteredAndDeduplicated(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
|
||||
$script->setCallingDays(['mon', 'mon', 'xxx', 'fri']);
|
||||
|
||||
self::assertSame(['mon', 'fri'], $script->getCallingDays());
|
||||
}
|
||||
|
||||
public function testNormalizePositionsRenumbersTheOutcomes(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$first = (new CallScriptOutcome())->setLabel('First')->setPosition(7);
|
||||
$second = (new CallScriptOutcome())->setLabel('Second')->setPosition(2);
|
||||
$script->addOutcome($first);
|
||||
$script->addOutcome($second);
|
||||
|
||||
$script->normalizePositions();
|
||||
|
||||
self::assertSame(0, $second->getPosition());
|
||||
self::assertSame(1, $first->getPosition());
|
||||
self::assertSame(['Second', 'First'], array_map(fn (CallScriptOutcome $o): ?string => $o->getLabel(), $script->getOutcomes()->toArray()));
|
||||
}
|
||||
|
||||
public function testOutcomeOptionsAreSanitized(): void
|
||||
{
|
||||
$outcome = new CallScriptOutcome();
|
||||
|
||||
$outcome->setOptions([
|
||||
['value' => 'yes', 'label' => 'Yes'],
|
||||
['value' => '', 'label' => ''],
|
||||
['value' => 'maybe', 'label' => ''],
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
['value' => 'yes', 'label' => 'Yes'],
|
||||
['value' => 'maybe', 'label' => 'maybe'],
|
||||
], $outcome->getOptions());
|
||||
}
|
||||
|
||||
public function testNullEnumsAndNumbersKeepTheCurrentValues(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$script->setCallingTimezoneMode(CallScriptTimezoneMode::Fixed);
|
||||
$script->setRetryBackoff(CallScriptRetryBackoff::Exponential);
|
||||
$script->setRetryMaxAttempts(5);
|
||||
|
||||
$script->setCallingTimezoneMode(null);
|
||||
$script->setRetryBackoff(null);
|
||||
$script->setRetryMaxAttempts(null);
|
||||
|
||||
self::assertSame(CallScriptTimezoneMode::Fixed, $script->getCallingTimezoneMode());
|
||||
self::assertSame(CallScriptRetryBackoff::Exponential, $script->getRetryBackoff());
|
||||
self::assertSame(5, $script->getRetryMaxAttempts());
|
||||
}
|
||||
|
||||
public function testDateRangeInterfaceMapsTheValidityWindow(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$start = new \DateTime('2026-01-10 09:00:00');
|
||||
$end = new \DateTime('2026-01-12 18:00:00');
|
||||
|
||||
$script->setPublishUp($start);
|
||||
$script->setPublishDown($end);
|
||||
|
||||
self::assertSame($start, $script->getStartDate());
|
||||
self::assertSame($end, $script->getEndDate());
|
||||
}
|
||||
|
||||
public function testOutcomeInputTypesDeclareWhetherTheyHaveOptions(): void
|
||||
{
|
||||
self::assertFalse(CallScriptOutcomeInputType::FreeText->hasOptions());
|
||||
self::assertTrue(CallScriptOutcomeInputType::Checkbox->hasOptions());
|
||||
self::assertTrue(CallScriptOutcomeInputType::Radio->hasOptions());
|
||||
self::assertTrue(CallScriptOutcomeInputType::Dropdown->hasOptions());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\MT3SMarketingProgramBundle\Tests\Unit\Validator;
|
||||
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptOutcome;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptOutcomeInputType;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Validator\Constraints\ValidCallScriptOutcomeOptions;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Validator\Constraints\ValidCallScriptOutcomeOptionsValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
|
||||
|
||||
final class ValidCallScriptOutcomeOptionsValidatorTest extends TestCase
|
||||
{
|
||||
public function testAFreeTextOutcomeWithoutOptionsIsValid(): void
|
||||
{
|
||||
$outcome = (new CallScriptOutcome())->setInputType(CallScriptOutcomeInputType::FreeText);
|
||||
|
||||
$this->validate($outcome, []);
|
||||
}
|
||||
|
||||
public function testAChoiceOutcomeWithOptionsIsValid(): void
|
||||
{
|
||||
$outcome = (new CallScriptOutcome())
|
||||
->setInputType(CallScriptOutcomeInputType::Dropdown)
|
||||
->setOptions([['value' => 'yes', 'label' => 'Yes']]);
|
||||
|
||||
$this->validate($outcome, []);
|
||||
}
|
||||
|
||||
public function testAChoiceOutcomeWithoutOptionsIsRefused(): void
|
||||
{
|
||||
$outcome = (new CallScriptOutcome())->setInputType(CallScriptOutcomeInputType::Radio);
|
||||
|
||||
$this->validate($outcome, ['mt3s.call_script.outcome.options.required']);
|
||||
}
|
||||
|
||||
public function testAFreeTextOutcomeWithOptionsIsRefused(): void
|
||||
{
|
||||
$outcome = (new CallScriptOutcome())
|
||||
->setInputType(CallScriptOutcomeInputType::FreeText)
|
||||
->setOptions([['value' => 'yes', 'label' => 'Yes']]);
|
||||
|
||||
$this->validate($outcome, ['mt3s.call_script.outcome.options.free_text_forbidden']);
|
||||
}
|
||||
|
||||
public function testOptionsNeedAValue(): void
|
||||
{
|
||||
$outcome = (new CallScriptOutcome())
|
||||
->setInputType(CallScriptOutcomeInputType::Radio)
|
||||
->setOptions([['value' => '', 'label' => 'No value']]);
|
||||
|
||||
$this->validate($outcome, ['mt3s.call_script.outcome.options.value_required']);
|
||||
}
|
||||
|
||||
public function testOptionValuesMustBeUnique(): void
|
||||
{
|
||||
$outcome = (new CallScriptOutcome())
|
||||
->setInputType(CallScriptOutcomeInputType::Checkbox)
|
||||
->setOptions([
|
||||
['value' => 'yes', 'label' => 'Yes'],
|
||||
['value' => 'yes', 'label' => 'Again'],
|
||||
]);
|
||||
|
||||
$this->validate($outcome, ['mt3s.call_script.outcome.options.duplicate_value']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $expectedMessages
|
||||
*/
|
||||
private function validate(CallScriptOutcome $outcome, array $expectedMessages): void
|
||||
{
|
||||
$violations = [];
|
||||
$builder = $this->createMock(ConstraintViolationBuilderInterface::class);
|
||||
$builder->method('atPath')->willReturnSelf();
|
||||
$builder->method('addViolation')->willReturnCallback(function () use (&$violations, $builder): void {
|
||||
$violations[] = $builder;
|
||||
});
|
||||
|
||||
$context = $this->createMock(ExecutionContextInterface::class);
|
||||
$context->method('buildViolation')->willReturnCallback(function (string $message) use (&$violations, $builder): ConstraintViolationBuilderInterface {
|
||||
$violations[] = $message;
|
||||
|
||||
return $builder;
|
||||
});
|
||||
|
||||
$validator = new ValidCallScriptOutcomeOptionsValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($outcome, new ValidCallScriptOutcomeOptions());
|
||||
|
||||
$messages = array_values(array_filter($violations, fn ($violation): bool => is_string($violation)));
|
||||
self::assertSame($expectedMessages, $messages);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\MT3SMarketingProgramBundle\Tests\Unit\Validator;
|
||||
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScript;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptTimezoneMode;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Validator\Constraints\ValidCallingWindow;
|
||||
use MauticPlugin\MT3SMarketingProgramBundle\Validator\Constraints\ValidCallingWindowValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
|
||||
|
||||
final class ValidCallingWindowValidatorTest extends TestCase
|
||||
{
|
||||
public function testACompleteAndOrderedWindowIsValid(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$script->setCallingFrom(new \DateTime('09:00'));
|
||||
$script->setCallingTo(new \DateTime('18:00'));
|
||||
|
||||
$this->validate($script, null);
|
||||
}
|
||||
|
||||
public function testAnEmptyWindowIsValid(): void
|
||||
{
|
||||
$this->validate(new CallScript(), null);
|
||||
}
|
||||
|
||||
public function testPartialHoursAreRefused(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$script->setCallingFrom(new \DateTime('09:00'));
|
||||
|
||||
$this->validate($script, 'mt3s.call_script.calling_window.partial_hours');
|
||||
}
|
||||
|
||||
public function testTheEndMustBeAfterTheStart(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$script->setCallingFrom(new \DateTime('18:00'));
|
||||
$script->setCallingTo(new \DateTime('09:00'));
|
||||
|
||||
$this->validate($script, 'mt3s.call_script.calling_window.to_before_from');
|
||||
}
|
||||
|
||||
public function testAFixedModeNeedsATimezone(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$script->setCallingTimezoneMode(CallScriptTimezoneMode::Fixed);
|
||||
|
||||
$this->validate($script, 'mt3s.call_script.calling_window.timezone_required');
|
||||
}
|
||||
|
||||
public function testAFixedModeWithATimezoneIsValid(): void
|
||||
{
|
||||
$script = new CallScript();
|
||||
$script->setCallingTimezoneMode(CallScriptTimezoneMode::Fixed);
|
||||
$script->setCallingTimezone('Europe/Paris');
|
||||
|
||||
$this->validate($script, null);
|
||||
}
|
||||
|
||||
private function validate(CallScript $script, ?string $expectedMessage): void
|
||||
{
|
||||
$violation = $this->createMock(ConstraintViolationBuilderInterface::class);
|
||||
$violation->method('atPath')->willReturnSelf();
|
||||
|
||||
$context = $this->createMock(ExecutionContextInterface::class);
|
||||
if (null === $expectedMessage) {
|
||||
$context->expects(self::never())->method('buildViolation');
|
||||
} else {
|
||||
$context->expects(self::once())
|
||||
->method('buildViolation')
|
||||
->with($expectedMessage)
|
||||
->willReturn($violation);
|
||||
$violation->expects(self::once())->method('addViolation');
|
||||
}
|
||||
|
||||
$validator = new ValidCallingWindowValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($script, new ValidCallingWindow());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user