Skip to content

Commit f53045c

Browse files
committed
Add custom DSN validation for Sparkpost region
1 parent a446c7a commit f53045c

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

Mailer/Factory/SparkpostTransportFactory.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use Symfony\Component\Mailer\Transport\Dsn;
1515
use Symfony\Component\Mailer\Transport\TransportInterface;
1616
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
1718

1819
class SparkpostTransportFactory extends AbstractTransportFactory
1920
{
2021
public function __construct(
2122
private TransportCallback $transportCallback,
23+
private TranslatorInterface $translator,
2224
EventDispatcherInterface $eventDispatcher,
2325
HttpClientInterface $client = null,
2426
LoggerInterface $logger = null
@@ -38,11 +40,15 @@ public function create(Dsn $dsn): TransportInterface
3840
{
3941
if (SparkpostTransport::MAUTIC_SPARKPOST_API_SCHEME === $dsn->getScheme()) {
4042
if (!$region = $dsn->getOption('region')) {
41-
throw new InvalidArgumentException('Empty region');
43+
throw new InvalidArgumentException(
44+
$this->translator->trans('mautic.sparkpost.plugin.region.empty', [], 'validators')
45+
);
4246
}
4347

4448
if (!array_key_exists($region, SparkpostTransport::SPARK_POST_HOSTS)) {
45-
throw new InvalidArgumentException('Invalid region');
49+
throw new InvalidArgumentException(
50+
$this->translator->trans('mautic.sparkpost.plugin.region.invalid', [], 'validators')
51+
);
4652
}
4753

4854
return new SparkpostTransport(

Tests/Functional/Mailer/Transport/SparkpostTransportTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
use Symfony\Component\HttpClient\Response\MockResponse;
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Contracts\HttpClient\HttpClientInterface;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
1617

1718
class SparkpostTransportTest extends MauticMysqlTestCase
1819
{
20+
private TranslatorInterface $translator;
21+
1922
protected function setUp(): void
2023
{
2124
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us';
2225
$this->configParams['messenger_dsn_email'] = 'sync://';
2326
$this->configParams['mailer_custom_headers'] = ['x-global-custom-header' => 'value123'];
2427
$this->configParams['mailer_from_email'] = '[email protected]';
2528
parent::setUp();
29+
$this->translator = self::getContainer()->get('translator');
2630
}
2731

2832
public function testEmailSendToContactSync(): void
@@ -114,4 +118,45 @@ private function createContact(string $email): Lead
114118

115119
return $lead;
116120
}
121+
122+
/**
123+
* @dataProvider dataInvalidDsn
124+
*
125+
* @param array<string, string> $data
126+
*/
127+
public function testInvalidDsn(array $data, string $expectedMessage): void
128+
{
129+
// Request config edit page
130+
$crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');
131+
Assert::assertTrue($this->client->getResponse()->isOk());
132+
133+
// Set form data
134+
$form = $crawler->selectButton('config[buttons][save]')->form();
135+
$form->setValues($data + ['config[leadconfig][contact_columns]' => ['name', 'email', 'id']]);
136+
137+
// Check if there is the given validation error
138+
$crawler = $this->client->submit($form);
139+
Assert::assertTrue($this->client->getResponse()->isOk());
140+
Assert::assertStringContainsString($this->translator->trans($expectedMessage, [], 'validators'), $crawler->text());
141+
}
142+
143+
/**
144+
* @return array<string, mixed[]>
145+
*/
146+
public function dataInvalidDsn(): iterable
147+
{
148+
yield 'Empty region' => [
149+
[
150+
'config[emailconfig][mailer_dsn][options][list][0][value]' => '',
151+
],
152+
'mautic.sparkpost.plugin.region.empty'
153+
];
154+
155+
yield 'Invalid region' => [
156+
[
157+
'config[emailconfig][mailer_dsn][options][list][0][value]' => 'invalid_region',
158+
],
159+
'mautic.sparkpost.plugin.region.invalid'
160+
];
161+
}
117162
}

Translations/en_US/validators.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mautic.sparkpost.plugin.region.empty="Sparkpost region is empty. Add 'region' as a option."
2+
mautic.sparkpost.plugin.region.invalid="Sparkpost region is invalid. Add 'us' or 'eu' as a suitable region."

0 commit comments

Comments
 (0)