Skip to content

Commit fe8f75f

Browse files
committed
Merge branch 'b-7.4.x-multiple-error-messages-OXDEV-8786' into b-7.4.x
2 parents df3739f + 8bdd829 commit fe8f75f

4 files changed

Lines changed: 36 additions & 25 deletions

File tree

src/PasswordPolicy/Intrastructure/ExceptionFactory.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,43 @@
1010
namespace OxidEsales\SecurityModule\PasswordPolicy\Intrastructure;
1111

1212
use OxidEsales\Eshop\Core\Exception\InputException;
13+
use OxidEsales\Eshop\Core\Language;
14+
use OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception\PasswordCollectionException;
1315
use OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception\PasswordValidateException;
1416

15-
class ExceptionFactory implements ExceptionFactoryInterface
17+
readonly class ExceptionFactory implements ExceptionFactoryInterface
1618
{
1719
public function __construct(
18-
private readonly \OxidEsales\Eshop\Core\Language $language
20+
private Language $language
1921
) {
2022
}
2123

2224
public function create(PasswordValidateException $exception): InputException
2325
{
24-
$exception = oxNew(
25-
InputException::class,
26-
sprintf(
27-
/** @phpstan-ignore-next-line */
28-
$this->language->translateString($exception->getMessage()),
29-
...$exception->getTranslationParameters()
30-
)
31-
);
26+
if ($exception instanceof PasswordCollectionException) {
27+
return $this->createCollection($exception);
28+
}
29+
30+
return oxNew(InputException::class, $this->formatExceptionMessage($exception));
31+
}
32+
33+
private function createCollection(PasswordCollectionException $collection): InputException
34+
{
35+
$lines = [];
36+
foreach ($collection->getValidationExceptions() as $exception) {
37+
$lines[] = $this->formatExceptionMessage($exception);
38+
}
39+
$message = '<ul><li>' . implode('</li><li>', $lines) . '</li></ul>';
3240

33-
return $exception;
41+
return oxNew(InputException::class, $message);
42+
}
43+
44+
private function formatExceptionMessage(PasswordValidateException $exception): string
45+
{
46+
return sprintf(
47+
/** @phpstan-ignore-next-line */
48+
$this->language->translateString($exception->getMessage()),
49+
...$exception->getTranslationParameters()
50+
);
3451
}
3552
}

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)