Skip to content

Commit 4064bfe

Browse files
committed
OXDEV-8786 Add multiple error messages in one exception
1 parent df3739f commit 4064bfe

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/PasswordPolicy/Intrastructure/ExceptionFactory.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OxidEsales\SecurityModule\PasswordPolicy\Intrastructure;
1111

1212
use OxidEsales\Eshop\Core\Exception\InputException;
13+
use OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception\PasswordCollectionException;
1314
use OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception\PasswordValidateException;
1415

1516
class ExceptionFactory implements ExceptionFactoryInterface
@@ -21,6 +22,10 @@ public function __construct(
2122

2223
public function create(PasswordValidateException $exception): InputException
2324
{
25+
if ($exception instanceof PasswordCollectionException) {
26+
return $this->createCollection($exception);
27+
}
28+
2429
$exception = oxNew(
2530
InputException::class,
2631
sprintf(
@@ -32,4 +37,20 @@ public function create(PasswordValidateException $exception): InputException
3237

3338
return $exception;
3439
}
40+
41+
private function createCollection(PasswordCollectionException $collection): InputException
42+
{
43+
$lines = [];
44+
foreach ($collection->getValidationExceptions() as $ex) {
45+
$lines[] = sprintf(
46+
/** @phpstan-ignore-next-line */
47+
$this->language->translateString($ex->getMessage()),
48+
...$ex->getTranslationParameters()
49+
);
50+
}
51+
/** @phpstan-ignore-next-line */
52+
$message = '<ul><li>' . implode('</li><li>', $lines) . '</li></ul>';
53+
54+
return oxNew(InputException::class, $message);
55+
}
3556
}

src/PasswordPolicy/Validation/Exception/PasswordCollectionException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
namespace OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception;
1111

12-
use OxidEsales\Eshop\Core\Exception\InputException;
13-
14-
class PasswordCollectionException extends InputException
12+
class PasswordCollectionException extends PasswordValidateException
1513
{
1614
/**
1715
* @var PasswordValidateException[]
@@ -20,6 +18,7 @@ class PasswordCollectionException extends InputException
2018

2119
public function __construct(array $exceptions)
2220
{
21+
parent::__construct();
2322
$this->exceptions = $exceptions;
2423
}
2524

src/Shared/Core/InputValidator.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,11 @@ public function checkPassword($user, $newPassword, $confirmationPassword, $shoul
3939
$passwordValidator->validatePassword($newPassword);
4040
} catch (PasswordCollectionException $e) {
4141
$exceptionFactory = $this->getService(ExceptionFactoryInterface::class);
42-
foreach ($e->getValidationExceptions() as $key => $error) {
43-
if ($key === count($e->getValidationExceptions()) - 1) {
44-
return $this->addValidationError(
45-
"oxuser__oxpassword",
46-
$exceptionFactory->create($error)
47-
);
48-
}
49-
50-
Registry::getUtilsView()->addErrorToDisplay($exceptionFactory->create($error));
51-
}
42+
43+
return $this->addValidationError(
44+
"oxuser__oxpassword",
45+
$exceptionFactory->create($e)
46+
);
5247
}
5348

5449
return parent::checkPassword($user, $newPassword, $confirmationPassword, $shouldCheckPasswordLength);

tests/Integration/Shared/Core/InputValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testInputValidationError($password, $expectedException): void
3232
$exception = $validator->checkPassword($userModelMock, $password, $password);
3333

3434
$this->assertInstanceOf(InputException::class, $exception);
35-
$this->assertSame($expectedException, $exception->getMessage());
35+
$this->assertStringContainsString($expectedException, $exception->getMessage());
3636
}
3737

3838
public static function dataProviderPasswordError(): iterable

0 commit comments

Comments
 (0)