Files
mautic-marketing-program-bu…/Tests/Unit/Model/SmartLinkModelTest.php
T
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- Correct getChoices() docblock to reflect translated label => enum
  backing value order instead of value => label
- Re-indent Smart Links menu config to match actual array nesting
- Align assignment operators in RefreshStatsCommand
- Add missing newline at end of RefreshStatsCommand.php
2026-11-21 11:23:33 +01:00

254 lines
11 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Tests\Unit\Model;
use Doctrine\ORM\EntityManagerInterface;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\PageBundle\Model\PageModel;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterCatalog;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
use MauticPlugin\MT3SMarketingProgramBundle\Model\SmartLinkModel;
use MauticPlugin\MT3SMarketingProgramBundle\Service\UtmConfigurationNormalizer;
use MauticPlugin\MT3SMarketingProgramBundle\Service\UtmConfigurationProvider;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final class SmartLinkModelTest extends TestCase
{
/**
* A real provider with the given raw configuration, served through the normalizer.
*
* @param array<string, mixed> $raw
*/
private function createProvider(array $raw): UtmConfigurationProvider
{
$parameters = $this->createMock(CoreParametersHelper::class);
$parameters->method('get')->willReturnMap([
[UtmConfigurationProvider::CONFIG_PARAMETER, [], $raw],
]);
$translator = $this->createMock(TranslatorInterface::class);
$translator->method('trans')->willReturnCallback(fn (string $id): string => $id);
return new UtmConfigurationProvider(
$parameters,
new UtmConfigurationNormalizer(new UtmParameterCatalog()),
new UtmParameterCatalog(),
$translator
);
}
/**
* The model with mocked core dependencies; the UTM configuration is real.
*
* @param array<string, mixed> $raw
*/
private function createModel(array $raw, ?PageModel $pageModel = null, ?UrlGeneratorInterface $router = null): SmartLinkModel
{
return new SmartLinkModel(
$this->createMock(EntityManagerInterface::class),
$this->createMock(CorePermissions::class),
$this->createMock(EventDispatcherInterface::class),
$router ?? $this->createMock(UrlGeneratorInterface::class),
$this->createMock(TranslatorInterface::class),
$this->createMock(UserHelper::class),
$this->createMock(LoggerInterface::class),
$this->createMock(CoreParametersHelper::class),
$this->createProvider($raw),
$pageModel ?? $this->createMock(PageModel::class)
);
}
public function testSlugRulesApplyOnNewLink(): void
{
$model = $this->createModel(['separator_replacement' => '_', 'enforce_lowercase' => '1']);
$link = $model->getEntity();
$model->applySlugRules($link, 'Big Camp');
$this->assertSame('big_camp', $link->getSlug());
}
public function testSlugRulesWithLowercaseDisabled(): void
{
$model = $this->createModel(['separator_replacement' => '-', 'enforce_lowercase' => '']);
$link = $model->getEntity();
$model->applySlugRules($link, 'Big Camp');
$this->assertSame('Big-Camp', $link->getSlug());
}
public function testSlugRulesKeepStoredSlugOnEdit(): void
{
$model = $this->createModel([]);
$reflection = new \ReflectionClass(\MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink::class);
$link = new \MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink();
$reflection->getProperty('id')->setValue($link, 1);
$link->setSlug('stored');
// The user never edits the slug of a saved link: the submitted value is ignored...
$model->applySlugRules($link);
$this->assertSame('stored', $link->getSlug());
// ...and passing a different slug explicitly is refused.
$this->expectException(\LogicException::class);
$model->applySlugRules($link, 'other');
}
public function testNormalizeUtmValues(): void
{
$model = $this->createModel(['enforce_lowercase' => '1']);
$this->assertSame(
// The model sorts the keys, so the stored array is deterministic.
['utm_medium' => 'email', 'utm_source' => 'acme'],
$model->normalizeUtmValues(['utm_source' => 'ACME ', 'utm_medium' => 'Email', 'utm_term' => ''])
);
$modelNoLower = $this->createModel(['enforce_lowercase' => '']);
$this->assertSame(
['utm_source' => 'ACME'],
$modelNoLower->normalizeUtmValues(['utm_source' => 'ACME'])
);
}
public function testNormalizeUtmValuesUsesValuePatternToo(): void
{
$model = $this->createModel(['enforce_lowercase' => '1']);
// Values of disabled parameters and empty values are dropped by the form; the model
// only applies the case and separator rules.
$this->assertSame(['a' => 'b'], $model->normalizeUtmValues(['a' => 'b', 'x' => null]));
}
public function testEnabledParameterDefaultModeIsRespected(): void
{
// An empty configuration leaves the utm_source dropdown switched off: an enabled
// dropdown needs values, and they are configured on the Configuration page.
$provider = $this->createProvider([]);
$source = $provider->getParameter('utm_source');
$this->assertNotNull($source);
$this->assertFalse($source['enabled']);
$this->assertSame(UtmParameterMode::DROPDOWN, $source['mode']);
$this->assertSame([], $provider->getDropdownValues('utm_source'));
// Enabled with stored values it stays a dropdown and serves them.
$enabled = $this->createProvider(['parameters' => [
'utm_source' => [
'enabled' => '1',
'label' => 'Source',
'mode' => 'dropdown',
'values' => [['value' => 'newsletter', 'label' => 'Newsletter']],
],
]]);
$source = $enabled->getParameter('utm_source');
$this->assertTrue($source['enabled']);
$this->assertSame(UtmParameterMode::DROPDOWN, $source['mode']);
$this->assertNotSame([], $enabled->getDropdownValues('utm_source'));
}
/**
* @param array<string, string> $utms
*/
#[DataProvider('redirectUrlProvider')]
public function testBuildRedirectUrl(string $destination, array $utms, string $expected): void
{
$link = new \MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink();
$link->setDestinationUrl($destination);
$link->setUtmParameters($utms);
$this->assertSame($expected, $this->createModel([])->buildRedirectUrl($link));
}
/**
* @return iterable<string, array{0: string, 1: array<string, string>, 2: string}>
*/
public static function redirectUrlProvider(): iterable
{
yield 'no utm parameters keeps destination as-is' => [
'https://www.example.com/spring',
[],
'https://www.example.com/spring',
];
yield 'parameters are appended to a bare destination' => [
'https://www.example.com/spring',
['utm_source' => 'newsletter', 'utm_medium' => 'email'],
'https://www.example.com/spring?utm_source=newsletter&utm_medium=email',
];
yield 'existing query is kept, link parameters override same-named ones' => [
'https://www.example.com/spring?foo=bar&utm_medium=paid',
['utm_medium' => 'email', 'utm_source' => 'newsletter'],
'https://www.example.com/spring?foo=bar&utm_medium=email&utm_source=newsletter',
];
yield 'fragment and parts of the destination survive' => [
'https://user.example.com:8443/path/page#section',
['utm_source' => 'x'],
'https://user.example.com:8443/path/page?utm_source=x#section',
];
}
public function testBuildMauticDestinationUrlWithoutDestinationIsEmpty(): void
{
$link = new \MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink();
$this->assertSame('', $this->createModel([])->buildMauticDestinationUrl($link));
}
public function testBuildMauticDestinationUrlUsesThePageModel(): void
{
$pageModel = $this->createMock(PageModel::class);
$pageModel->method('generateUrl')->willReturn('https://mautic.example.com/landing');
$link = new \MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink();
$reflection = new \ReflectionClass(\MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink::class);
$reflection->getProperty('page')->setValue($link, $this->createMock(\Mautic\PageBundle\Entity\Page::class));
$this->assertSame('https://mautic.example.com/landing', $this->createModel([], $pageModel)->buildMauticDestinationUrl($link));
}
public function testBuildMauticDestinationUrlUsesTheFormPreviewRoute(): void
{
$router = $this->createMock(UrlGeneratorInterface::class);
$router->method('generate')
->with('mautic_form_preview', ['id' => 7], UrlGeneratorInterface::ABSOLUTE_URL)
->willReturn('https://mautic.example.com/form/7');
$form = $this->createMock(\Mautic\FormBundle\Entity\Form::class);
$form->method('getId')->willReturn(7);
$link = new \MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink();
$reflection = new \ReflectionClass(\MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink::class);
$reflection->getProperty('form')->setValue($link, $form);
$this->assertSame('https://mautic.example.com/form/7', $this->createModel([], null, $router)->buildMauticDestinationUrl($link));
}
public function testBuildRedirectUrlAppendsUtmToMauticDestinations(): void
{
$pageModel = $this->createMock(PageModel::class);
$pageModel->method('generateUrl')->willReturn('https://mautic.example.com/landing');
$link = new \MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink();
$link->setRedirectType(\MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectType::Mautic);
$link->setUtmParameters(['utm_source' => 'newsletter']);
$reflection = new \ReflectionClass(\MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink::class);
$reflection->getProperty('page')->setValue($link, $this->createMock(\Mautic\PageBundle\Entity\Page::class));
$this->assertSame(
'https://mautic.example.com/landing?utm_source=newsletter',
$this->createModel([], $pageModel)->buildRedirectUrl($link)
);
}
}