Skip to content

Commit

Permalink
Merge pull request shopware5#2549 from shopware5/fix/sw-27151/seperat…
Browse files Browse the repository at this point in the history
…e-emails-before-validation

fix(SW-27151): split emails before validation
  • Loading branch information
mitelg authored Jul 10, 2023
2 parents 1f4f8a7 + ad0a373 commit 15a32e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
15 changes: 10 additions & 5 deletions engine/Shopware/Controllers/Backend/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,15 +1043,20 @@ public function validateEmailAction()
$this->Front()->Plugins()->ViewRenderer()->setNoRender();
$this->Front()->Plugins()->Json()->setRenderer(false);

$email = $this->Request()->getParam('value');
$emails = explode(',', (string) $this->Request()->getParam('value', ''));

/** @var EmailValidatorInterface $emailValidator */
$emailValidator = $this->container->get(EmailValidator::class);
if ($emailValidator->isValid($email)) {
$this->Response()->setContent(1);
} else {
$this->Response()->setContent('');

foreach ($emails as $email) {
if (!$emailValidator->isValid($email)) {
$this->Response()->setContent('');

return;
}
}

$this->Response()->setContent(1);
}

public function getSalutationsAction()
Expand Down
38 changes: 38 additions & 0 deletions tests/Functional/Controllers/Backend/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

use Enlight_Components_Test_Controller_TestCase;
use Enlight_Controller_Request_RequestTestCase;
use Enlight_Controller_Response_ResponseTestCase;
use Enlight_Template_Manager;
use Enlight_View_Default;
use Generator;
use Shopware\Tests\Functional\Traits\ContainerTrait;
use Shopware\Tests\Functional\Traits\DatabaseTransactionBehaviour;
use Shopware_Controllers_Backend_Base;
Expand Down Expand Up @@ -214,6 +216,42 @@ public function provideSearchString(): array
];
}

/**
* @dataProvider provideEmails
*/
public function testValidateEmailAction(string $emails, bool $expectedIsValid): void
{
$request = new Enlight_Controller_Request_RequestTestCase();
$response = new Enlight_Controller_Response_ResponseTestCase();
$request->setParam('value', $emails);

$controller = $this->createController();
$controller->setRequest($request);
$controller->setResponse($response);
$controller->validateEmailAction();

static::assertSame($expectedIsValid, (bool) $response->getContent());
}

/**
* @return Generator<string, array{emails: string, expectedIsValid: bool}>
*/
public function provideEmails(): Generator
{
yield '1 mail shall be valid' => [
'emails' => '[email protected]',
'expectedIsValid' => true,
];
yield '3 mails shall be valid' => [
'emails' => '[email protected],[email protected],[email protected]',
'expectedIsValid' => true,
];
yield '3 mail shall be invalid' => [
'emails' => '[email protected],[email protected],[email protected]||test',
'expectedIsValid' => false,
];
}

private function createController(): Shopware_Controllers_Backend_Base
{
$controller = $this->getContainer()->get('shopware_controllers_backend_base');
Expand Down

0 comments on commit 15a32e3

Please sign in to comment.