$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 $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 $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, 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) ); } }