Two domains, one MT3S bundle: Marketing Programs (unchanged) and the
imported Smart Links (smart links + UTM taxonomies), each keeping its
own menu, tables, routes and translations.
- Code moved from MauticPlugin\SmartlinksBundle\ to
MauticPlugin\MT3SMarketingProgramBundle\; template namespace
@Smartlinks/ -> @MT3SMarketingProgram/; smartlinks.js ->
Assets/js/mt3s-mp-smartlinks.js; icon -> Assets/img/smartlinks-icon.svg.
- Incoming migrations renamed Version_0_X_0_SmartLinks.php to avoid
the class-name collision; schema untouched, data intact.
- Permissions folded into MT3SMarketingProgramPermissions:
smart_links + taxonomies under the mt3smarketingprogram: base
(roles that had the old smartlinks: grants must be re-granted).
- config.php: Smart Links routes + menu group, public /l/{slug},
SmartlinksIntegration services, smartlinks_* parameters (legacy
utm_manager_* keys still readable), version 0.18.0. The 0.17.0
placeholder /s/marketing-program-utm-taxonomies is replaced by the
real /s/utm-taxonomies.
- Translations merged (en_US) and added (fr_FR); Tests/ + phpunit moved
in; readme documents both domains and the 0.18.0 merge history.
Verified live: menus, both config tabs, both lists + forms, program
pages, public redirect with taxonomy UTM values, host + unknown-slug
404s, 90/90 unit tests, full php -l, all ini catalogs parse.
251 lines
11 KiB
PHP
251 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\MockObject\MockObject;
|
|
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.
|
|
*/
|
|
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.
|
|
*/
|
|
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)
|
|
);
|
|
}
|
|
}
|