Skip to content

Commit 10bcde3

Browse files
author
ogorkun
committed
MC-34385: Filter fields allowing HTML
1 parent cf27f47 commit 10bcde3

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Validator.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\Message\ManagerInterface;
13+
use Magento\Framework\Message\MessageInterface;
1314
use Magento\Framework\Validation\ValidationException;
1415
use Magento\Framework\Validator\HTML\WYSIWYGValidatorInterface;
1516
use Psr\Log\LoggerInterface;
17+
use Magento\Framework\Message\Factory as MessageFactory;
1618

1719
/**
1820
* Processes backend validator results.
@@ -41,22 +43,30 @@ class Validator implements WYSIWYGValidatorInterface
4143
*/
4244
private $logger;
4345

46+
/**
47+
* @var MessageFactory
48+
*/
49+
private $messageFactory;
50+
4451
/**
4552
* @param WYSIWYGValidatorInterface $validator
4653
* @param ManagerInterface $messages
4754
* @param ScopeConfigInterface $config
4855
* @param LoggerInterface $logger
56+
* @param MessageFactory $messageFactory
4957
*/
5058
public function __construct(
5159
WYSIWYGValidatorInterface $validator,
5260
ManagerInterface $messages,
5361
ScopeConfigInterface $config,
54-
LoggerInterface $logger
62+
LoggerInterface $logger,
63+
MessageFactory $messageFactory
5564
) {
5665
$this->validator = $validator;
5766
$this->messages = $messages;
5867
$this->config = $config;
5968
$this->logger = $logger;
69+
$this->messageFactory = $messageFactory;
6070
}
6171

6272
/**
@@ -71,18 +81,30 @@ public function validate(string $content): void
7181
if ($throwException) {
7282
throw $exception;
7383
} else {
74-
$this->messages->addWarningMessage(
75-
__(
76-
'Temporarily allowed to save HTML value that contains restricted elements. %1',
77-
$exception->getMessage()
78-
)
84+
$this->messages->addUniqueMessages(
85+
[
86+
$this->messageFactory->create(
87+
MessageInterface::TYPE_WARNING,
88+
(string)__(
89+
'Temporarily allowed to save HTML value that contains restricted elements. %1',
90+
$exception->getMessage()
91+
)
92+
)
93+
]
7994
);
8095
}
8196
} catch (\Throwable $exception) {
8297
if ($throwException) {
8398
throw $exception;
8499
} else {
85-
$this->messages->addWarningMessage(__('Invalid HTML provided')->render());
100+
$this->messages->addUniqueMessages(
101+
[
102+
$this->messageFactory->create(
103+
MessageInterface::TYPE_WARNING,
104+
(string)__('Invalid HTML provided')
105+
)
106+
]
107+
);
86108
$this->logger->error($exception);
87109
}
88110
}

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/ValidatorTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use Magento\Cms\Model\Wysiwyg\Validator;
1212
use Magento\Framework\App\Config\ScopeConfigInterface;
1313
use Magento\Framework\Message\ManagerInterface;
14+
use Magento\Framework\Message\MessageInterface;
1415
use Magento\Framework\Validation\ValidationException;
1516
use Magento\Framework\Validator\HTML\WYSIWYGValidatorInterface;
1617
use PHPUnit\Framework\TestCase;
1718
use Psr\Log\LoggerInterface;
19+
use Magento\Framework\Message\Factory as MessageFactory;
1820

1921
class ValidatorTest extends TestCase
2022
{
@@ -45,6 +47,13 @@ public function testValidate(bool $isFlagSet, ?\Throwable $thrown, bool $excepti
4547
{
4648
$actuallyWarned = false;
4749

50+
$messageFactoryMock = $this->createMock(MessageFactory::class);
51+
$messageFactoryMock->method('create')
52+
->willReturnCallback(
53+
function () {
54+
return $this->getMockForAbstractClass(MessageInterface::class);
55+
}
56+
);
4857
$configMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
4958
$configMock->method('isSetFlag')
5059
->with(Validator::CONFIG_PATH_THROW_EXCEPTION)
@@ -56,7 +65,7 @@ public function testValidate(bool $isFlagSet, ?\Throwable $thrown, bool $excepti
5665
}
5766

5867
$messagesMock = $this->getMockForAbstractClass(ManagerInterface::class);
59-
$messagesMock->method('addWarningMessage')
68+
$messagesMock->method('addUniqueMessages')
6069
->willReturnCallback(
6170
function () use (&$actuallyWarned): void {
6271
$actuallyWarned = true;
@@ -65,7 +74,7 @@ function () use (&$actuallyWarned): void {
6574

6675
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
6776

68-
$validator = new Validator($backendMock, $messagesMock, $configMock, $loggerMock);
77+
$validator = new Validator($backendMock, $messagesMock, $configMock, $loggerMock, $messageFactoryMock);
6978
try {
7079
$validator->validate('content');
7180
$actuallyThrown = false;

0 commit comments

Comments
 (0)